175 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
162 lines
8.0 KiB
C
162 lines
8.0 KiB
C
/* Source-owned generation retirement. Never authenticates, writes engine
|
|
* pointers or consumes an original Login action. After genuine Back, qualified
|
|
* original native Disconnect completes retained-connection cleanup before the
|
|
* unchanged full-retirement guards admit a new Source generation. */
|
|
#include <windows.h>
|
|
#include "wine_session_retirement.h"
|
|
#include "wine_wire_host.h"
|
|
#include "wine_pipe_host.h"
|
|
#include "wine_channel_policy.h"
|
|
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
|
|
#include "nohelper/wine_preopen_hook.private.h"
|
|
#include "nohelper/native_disconnect_retirement.private.h"
|
|
#include "nohelper/closed_send_queue_discard.private.h"
|
|
extern struct GwWirePreOpenHook gw_wire_preopen_hook_data;
|
|
extern struct gw_wire_session_lease gw_wire_session_lease_data;
|
|
#endif
|
|
extern struct gw_wire_auth_control gw_wire_auth_data;
|
|
extern struct gw_pipe_control gw_wire_pipe_data;
|
|
extern struct gw_pipe_world_failure gw_wire_world_failure;
|
|
extern struct gw_channel_policy_control gw_wire_channel_policy;
|
|
__declspec(dllexport) struct gw_session_retirement_control gw_wire_session_retirement_data={.abi=1,.size=56,.next_credentials_generation=1};
|
|
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
|
|
/* Back legitimately returns state2 with a retained primary. Complete original
|
|
* Disconnect while the Source worker is terminal, then dispose the old native
|
|
* suspended SEND lists using their original allocation contract. No new Source
|
|
* authentication is possible until the unchanged full-retirement guards pass.
|
|
* NativeCS precedes Pipe SRW; the latter is NEVER held across native cleanup.
|
|
*/
|
|
static int gw_wire_session_finish_native_close(void* owner,uint32_t expected){
|
|
static int allocator_failure;
|
|
const struct GwWirePreOpenHook* pre=&gw_wire_preopen_hook_data;
|
|
const struct gw_pipe_control* pipe=&gw_wire_pipe_data;
|
|
unsigned char* object=(unsigned char*)owner;
|
|
uint64_t primary,holder,target=0;
|
|
int result,pipe_locked=0;
|
|
if(allocator_failure)return allocator_failure;
|
|
if(gw_wire_account_retirement_ready(expected))return -50;
|
|
if(pre->abi!=1 || pre->size!=sizeof(*pre) || pre->ready!=1 || pre->error ||
|
|
pre->owner!=owner || !pre->primary_thread || !pre->bridge_module || !pre->callback ||
|
|
pre->primary_thread!=GetCurrentThreadId())return -51;
|
|
if(gw_wire_session_lease_data.abi!=1 || gw_wire_session_lease_data.size!=32 ||
|
|
gw_wire_session_lease_data.attached || !gw_wire_session_lease_data.released ||
|
|
gw_wire_session_lease_data.reserved)return -52;
|
|
result=gw_wire_channel_retirement_lock(owner);
|
|
if(result)return result;
|
|
result=gw_wire_pipe_retirement_lock();
|
|
if(result)goto done;
|
|
pipe_locked=1;
|
|
/* Snapshot exact old binding while callbacks are fenced by Pipe SRW. The
|
|
* original disconnect can already have cleared either native reference. */
|
|
if(!owner || !gw_native_disconnect_readable(owner,0xc4b) ||
|
|
expected!=gw_wire_pipe_generation() || !expected || expected>=GW_SESSION_EPOCH_LIMIT ||
|
|
pipe->abi!=1 || pipe->size!=sizeof(*pipe) || pipe->stage!=1 || !pipe->closed ||
|
|
!pipe->world_auth || pipe->owner!=(uint64_t)(uintptr_t)owner ||
|
|
!pipe->pipe || !pipe->connection || !gw_wire_pipe_receiver_allowed(pipe->pipe) ||
|
|
gw_wire_session_pipe_receivers.objects[expected-1]){result=-53;goto done;}
|
|
primary=*(uint64_t*)(object+0x1a0);holder=*(uint64_t*)(object+0x228);
|
|
if(holder){
|
|
if(!gw_native_disconnect_readable((void*)(uintptr_t)holder,8)){result=-54;goto done;}
|
|
target=*(uint64_t*)(uintptr_t)holder;
|
|
}
|
|
if((primary && primary!=pipe->connection) || (target && target!=pipe->pipe)){
|
|
result=-54;goto done;
|
|
}
|
|
gw_wire_pipe_retirement_unlock();pipe_locked=0;
|
|
result=gw_wire_account_retirement_ready(expected);
|
|
if(result)goto done;
|
|
result=gw_native_disconnect_retained_primary(owner,pre->primary_thread);
|
|
if(result)goto done;
|
|
result=gw_closed_send_queues_discard(owner);
|
|
/* Allocator/post-release failure cannot become a false success on a later
|
|
* frame merely because the original unlink already changed the list. */
|
|
if(result==-84 || result==-85)allocator_failure=result;
|
|
done:
|
|
if(pipe_locked)gw_wire_pipe_retirement_unlock();
|
|
gw_wire_channel_retirement_unlock(owner);
|
|
return result;
|
|
}
|
|
#endif
|
|
int WINAPI gw_wire_session_retirement_request_if_terminal(void* owner){
|
|
struct gw_session_retirement_control* c=&gw_wire_session_retirement_data;
|
|
uint32_t expected=gw_wire_account_generation();LONG prior,state;
|
|
int result;
|
|
if(!owner || !expected || expected>=GW_SESSION_EPOCH_LIMIT ||
|
|
c->abi!=1 || c->size!=sizeof(*c) ||
|
|
c->next_credentials_generation!=expected)return 0;
|
|
prior=expected==1?0:2;
|
|
state=InterlockedCompareExchange((LONG*)&c->state,0,0);
|
|
if(state==3){
|
|
/* A blocked original primary is not permission to replace it. Native-CS
|
|
* contention also retries only after the same complete terminal guards. */
|
|
if((c->result!=-12 && c->result!=-23) || c->owner!=(uint64_t)(uintptr_t)owner ||
|
|
c->expected_credentials_generation!=expected ||
|
|
c->completed_credentials_generation!=expected-1)return 0;
|
|
prior=3;
|
|
}else if(state!=prior ||
|
|
c->expected_credentials_generation!=expected-1 ||
|
|
c->completed_credentials_generation!=expected-1 ||
|
|
(expected>1 && c->owner!=(uint64_t)(uintptr_t)owner))return 0;
|
|
if(gw_wire_account_retirement_ready(expected))return 0;
|
|
result=gw_wire_channel_retirement_lock(owner);
|
|
if(result)return 0;
|
|
result=gw_wire_pipe_retirement_lock();
|
|
if(result){gw_wire_channel_retirement_unlock(owner);return 0;}
|
|
result=gw_wire_pipe_retirement_ready(owner,expected);
|
|
if(!result)result=gw_wire_channel_retirement_ready(owner);
|
|
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
|
|
if(result==-12 || result==-13 || result==-22){
|
|
/* Never call original Disconnect or allocation helpers under Pipe SRW. */
|
|
gw_wire_pipe_retirement_unlock();
|
|
result=gw_wire_session_finish_native_close(owner,expected);
|
|
if(result){
|
|
gw_wire_channel_retirement_unlock(owner);
|
|
if(c->state!=3)c->result=result; /* Preserve an existing retryable commit result. */
|
|
return 0;
|
|
}
|
|
result=gw_wire_pipe_retirement_lock();
|
|
if(result){gw_wire_channel_retirement_unlock(owner);return 0;}
|
|
result=gw_wire_account_retirement_ready(expected);
|
|
if(!result)result=gw_wire_pipe_retirement_ready(owner,expected);
|
|
if(!result)result=gw_wire_channel_retirement_ready(owner);
|
|
}
|
|
#endif
|
|
gw_wire_pipe_retirement_unlock();
|
|
gw_wire_channel_retirement_unlock(owner);
|
|
if(result)return 0;
|
|
c->owner=(uint64_t)(uintptr_t)owner;
|
|
c->expected_credentials_generation=expected;
|
|
c->result=0;
|
|
MemoryBarrier();
|
|
return InterlockedCompareExchange((LONG*)&c->state,1,prior)==prior;
|
|
}
|
|
void WINAPI gw_wire_session_retire(void* owner,int32_t previous_frame_error){
|
|
struct gw_session_retirement_control* c=&gw_wire_session_retirement_data;
|
|
uint32_t expected;int result,locked=0,native_locked=0;
|
|
if(c->abi!=1 || c->size!=56 || c->state!=1)return;
|
|
expected=c->expected_credentials_generation;
|
|
c->owner=(uint64_t)owner;c->result=0;
|
|
c->previous_account_error=gw_wire_auth_data.error;
|
|
c->previous_pipe_error=gw_wire_pipe_data.error;
|
|
c->previous_world_error=gw_wire_world_failure.result;
|
|
c->previous_policy_error=gw_wire_channel_policy.error;
|
|
c->previous_frame_error=previous_frame_error;
|
|
result=gw_wire_account_retirement_ready(expected);
|
|
if(!result){result=gw_wire_channel_retirement_lock(owner);if(!result)native_locked=1;}
|
|
if(!result){result=gw_wire_pipe_retirement_lock();if(!result)locked=1;}
|
|
if(!result)result=gw_wire_pipe_retirement_ready(owner,expected);
|
|
if(!result)result=gw_wire_channel_retirement_ready(owner);
|
|
if(!result)result=gw_wire_account_retire_generation(expected);
|
|
if(!result){
|
|
gw_wire_realm_retire_generation();
|
|
gw_wire_channel_retire_generation();
|
|
gw_wire_pipe_retire_generation(expected+1);
|
|
c->completed_credentials_generation=expected;
|
|
c->next_credentials_generation=expected+1;
|
|
}
|
|
if(locked)gw_wire_pipe_retirement_unlock();
|
|
if(native_locked)gw_wire_channel_retirement_unlock(owner);
|
|
c->result=result;
|
|
InterlockedExchange((LONG*)&c->state,result?3:2);
|
|
}
|