Files
2026-11-23 03:28:32 +02:00

245 lines
13 KiB
C

/* Native Turtle has one authenticated world stream. Preserve the original
* NetClient serializer, suspended gameplay queue, critical section, transport
* and allocator; route its logical instance packets through the real realm
* pipe. No instruction patches, extra socket, fake connection or auth events.
* Entry/call contracts: real ResumeComms traces and native_lifecycle_audit.py.
*/
#include <windows.h>
#include "wine_channel_policy.h"
struct store {uint64_t table;void* data;uint32_t capacity,length,cursor,pad;};
struct node {void* prev;void* next;unsigned char requested,pad[7];struct store buffer;};
_Static_assert(sizeof(struct node)==56,"Deferred node layout differs");
extern int WINAPI gw_wire_native_channel_ready(void*);
extern int WINAPI gw_wire_native_channel_transfer_ready(void*);
__declspec(dllexport) struct gw_channel_contract_status gw_wire_channel_contracts={.abi=1,.size=32};
__declspec(dllexport) struct gw_channel_policy_control gw_wire_channel_policy={.abi=1,.size=48};
#ifdef GW_CHANNEL_FIXTURE
extern void* gw_fixture_owner;
extern void WINAPI gw_fixture_send(void*,void*,unsigned char);
extern void WINAPI gw_fixture_release_store(void*,void**,uint32_t*);
extern unsigned char WINAPI gw_fixture_release(void*,const char*,int,unsigned int);
#define CURRENT_OWNER gw_fixture_owner
#define SEND gw_fixture_send
#define RELEASE_STORE gw_fixture_release_store
#define RELEASE gw_fixture_release
#define SOURCE "owned-fixture"
#else
#define CURRENT_OWNER (*(void**)0x145e7a098ULL)
#define SEND ((void (WINAPI *)(void*,void*,unsigned char))0x141e46a80ULL)
#define RELEASE_STORE ((void (WINAPI *)(void*,void**,uint32_t*))0x1445b7db0ULL)
#define RELEASE ((unsigned char (WINAPI *)(void*,const char*,int,unsigned int))0x14452a5d0ULL)
#define SOURCE ((const char*)0x145cdda90ULL)
#endif
static int readable(const void* p,SIZE_T length){
MEMORY_BASIC_INFORMATION m;uintptr_t start=(uintptr_t)p,end=start+length;
if(start<0x10000 || end<start || !VirtualQuery(p,&m,sizeof(m)) || m.State!=MEM_COMMIT ||
(m.Protect&(PAGE_GUARD|PAGE_NOACCESS)) || end>(uintptr_t)m.BaseAddress+m.RegionSize)return 0;
return 1;
}
static uint32_t code_status(const void* p,const unsigned char* prefix,SIZE_T bytes){
MEMORY_BASIC_INFORMATION m;SIZE_T i;uintptr_t end=(uintptr_t)p+bytes;
if(!VirtualQuery(p,&m,sizeof(m)) || m.State!=MEM_COMMIT)return 1;
if(m.Protect&(PAGE_GUARD|PAGE_NOACCESS))return 2;
if(!(m.Protect&(PAGE_EXECUTE|PAGE_EXECUTE_READ|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY)))return 4;
if(end<(uintptr_t)p || end>(uintptr_t)m.BaseAddress+m.RegionSize)return 16;
for(i=0;i<bytes;++i)if(((const unsigned char*)p)[i]!=prefix[i])return 8;
return 0;
}
#ifndef GW_CHANNEL_FIXTURE
static int targets_ready(void){
static const unsigned char send[]={0x40,0x56,0x57,0x41,0x54,0x41,0x55,0x48,0x83,0xec,0x68};
static const unsigned char buffer_release[]={0x48,0x89,0x5c,0x24,0x08,0x57,0x48,0x83,0xec,0x20,0x41,0x83,0x38,0};
static const unsigned char release[]={0x48,0x89,0x5c,0x24,0x10,0x48,0x89,0x6c,0x24,0x18};
struct gw_channel_contract_status* d=&gw_wire_channel_contracts;
++d->samples;
d->send_status=code_status((const void*)SEND,send,sizeof(send));
d->store_status=code_status((const void*)RELEASE_STORE,buffer_release,sizeof(buffer_release));
d->release_status=code_status((const void*)RELEASE,release,sizeof(release));
if(d->send_status || d->store_status || d->release_status){++d->failures;return 0;}
return 1;
}
#else
extern uint32_t gw_fixture_contract_status;
static int targets_ready(void){
gw_wire_channel_contracts.send_status=0;
gw_wire_channel_contracts.store_status=gw_fixture_contract_status;
gw_wire_channel_contracts.release_status=0;
return !gw_fixture_contract_status;
}
#endif
/* The original node-specific wrapper is cold in a native one-channel session.
* Pop only a canonical FIRST link, whose backlinks were verified in the real
* native queue. Reuse the shared CDataStore release helper also called by the
* genuine main-thread receive dispatcher; never force the cold wrapper live. */
static int canonical_head(struct node* n,void** head){
uintptr_t next=(uintptr_t)n->next;
if(n->prev!=head || head[1]!=n || !next)return 0;
if(next&1)return next==((uintptr_t)head|1) && head[0]==n;
return readable((void*)next,16) && ((struct node*)next)->prev==n;
}
static int finish_node(struct node* n,void** head){
void* next=n->next;
RELEASE_STORE(&n->buffer,&n->buffer.data,&n->buffer.capacity);
if(n->buffer.data || n->buffer.capacity)return 0;
if((uintptr_t)next&1)head[0]=head;
else ((struct node*)next)->prev=head;
head[1]=next;n->prev=n->next=0;
return RELEASE(n,SOURCE,-2,0)!=0;
}
void WINAPI gw_wire_channel_world_left(void* owner){
struct gw_channel_policy_control* c=&gw_wire_channel_policy;
if(c->abi!=1 || c->size!=48 || c->owner!=(uint64_t)owner)return;
/* Same-account logout to character-select keeps the authenticated realm
* socket, so the full session retirement that resets this policy
* (gw_wire_channel_retire_generation) does not run. Previously this only
* re-armed command 3 while leaving c->owner pinned to the first session;
* the owned auto-enable path (gw_wire_channel_poll) re-binds a world
* session only when c->owner is unset, so a second world entry could never
* re-enable the outbound gameplay drain and every gen-2 action stayed
* queued and undrained. Clear the owned-channel binding to the pristine
* first-login state so the next world entry re-arms exactly like the
* first. The engine send queue at owner+0x200 is left untouched. */
InterlockedExchange((LONG*)&c->enabled,0);
InterlockedExchange((LONG*)&c->error,0);
InterlockedExchange((LONG*)&c->command,0);
c->owner=0;
}
static unsigned protection_waits;
static ULONGLONG protection_started;
static int awaiting_world;
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
static int auto_disabled;
#endif
__declspec(dllexport) void WINAPI gw_wire_channel_poll(void* owner){
struct gw_channel_policy_control* c=&gw_wire_channel_policy;
unsigned char* p=(unsigned char*)owner;void** head;unsigned int count;LONG command,thread;
if(c->abi!=1 || c->size!=48)return;
/* A world-left (gw_wire_channel_world_left) clears the owned binding, but a
* teardown-race poll may already have latched error=3 (world readiness gone
* while still enabled). Once the owner is unbound that latched error is
* stale: it no longer describes a live session, yet it permanently blocks
* every subsequent world entry from re-arming (auto-enable requires
* !c->error, and the early-return below bails on any error), which is why
* gameplay died on the third and later logins. error=3 is only ever set
* with an owner bound (the owner/thread guard precedes it), so error==3 with
* no owner is always a stale world-left artifact, never a live mid-world
* fault -- recover it here so re-login re-arms cleanly without limit. */
if(c->error==3 && !c->owner) InterlockedExchange((LONG*)&c->error,0);
command=InterlockedExchange((LONG*)&c->command,0);
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
if(command==2)auto_disabled=1;
else if(command==1)auto_disabled=0;
#endif
if(command==2){awaiting_world=0;c->enabled=0;return;}
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
/* Profile5 has no external controller after a direct login. Publish the
* same owned command1 used by that controller only after the actual Source
* World/self-create proof. The existing command consumer still validates
* the owner, thread, native state and deferred queue before sending. */
if(!command && !c->enabled && !c->error && !awaiting_world &&
!auto_disabled && !c->owner && owner && CURRENT_OWNER==owner){
EnterCriticalSection((CRITICAL_SECTION*)((unsigned char*)owner+0x70));
if(gw_wire_native_channel_ready(owner)){
c->owner=(uint64_t)owner;
command=1;
}
LeaveCriticalSection((CRITICAL_SECTION*)((unsigned char*)owner+0x70));
}
#endif
if(!command && (c->error || (!c->enabled && !awaiting_world)))return;
if(command!=0 && command!=1 && command!=3){c->error=1;c->enabled=0;return;}
thread=(LONG)GetCurrentThreadId();
if(!owner || c->owner!=(uint64_t)owner || CURRENT_OWNER!=owner || (c->thread && c->thread!=thread)){
c->error=2;c->enabled=0;return;
}
c->thread=thread;
if(command==3){awaiting_world=1;c->enabled=0;protection_waits=0;return;}
EnterCriticalSection((CRITICAL_SECTION*)(p+0x70));
/* This readiness predicate verifies the live native account/pipe and real
* server world auth/self-create. It never writes engine login state. */
if((!gw_wire_native_channel_ready(owner) && !gw_wire_native_channel_transfer_ready(owner)) || *(LONG*)(p+0x98)!=5 ||
!*(void**)(p+0x1a0) || *(void**)(p+0x1a8) || *(void**)(p+0x1b8) ||
p[0x218]!=0 || p[0x219]!=1){
if(!awaiting_world){c->error=3;c->enabled=0;}
goto done;
}
awaiting_world=0;
c->enabled=1;++c->polls;head=(void**)(p+0x200);
for(count=0;count<32;++count){
struct node* n=(struct node*)head[1];uint32_t bytes;
if(!n || ((uintptr_t)n&1))break;
if(!readable(n,sizeof(*n)) || !canonical_head(n,head) || n->requested<1 || n->requested>3 ||
n->buffer.table!=0x144c69b00ULL || n->buffer.cursor!=16 || n->buffer.length<20 ||
n->buffer.length>n->buffer.capacity || n->buffer.capacity>1048592 ||
!readable(n->buffer.data,n->buffer.length)){
c->error=4;c->enabled=0;break;
}
bytes=n->buffer.length-n->buffer.cursor;c->last_requested=n->requested;
/* Some original code pages are naturally protected while idle. Do
* nothing with the queued node until all original call checks pass.
* Never touch protected code, change protections, or bypass signatures.
* Only the observed guard/noaccess condition may defer; all other
* failures and a prolonged protection wait remain fail-closed. */
if(!targets_ready()){
struct gw_channel_contract_status* d=&gw_wire_channel_contracts;
ULONGLONG now=GetTickCount64();
if(!protection_waits)protection_started=now;
if(!(d->send_status&~2u) && !(d->store_status&~2u) && !(d->release_status&~2u) &&
++protection_waits<=3000 && now-protection_started<=5000){
++d->protection_deferrals;break;
}
c->error=8;c->enabled=0;break;
}
protection_waits=0;
/* The original replay passes this embedded CDataStore, not its data
* allocation or node. Explicit0 selects the authenticated Turtle pipe.
* The original sender borrows/copies the bytes synchronously. */
SEND(owner,&n->buffer,0);
if(!finish_node(n,head)){c->error=5;c->enabled=0;break;}
++c->drained;c->bytes+=bytes;
if(!gw_wire_native_channel_ready(owner) && !gw_wire_native_channel_transfer_ready(owner)){c->error=7;c->enabled=0;break;}
}
done:
LeaveCriticalSection((CRITICAL_SECTION*)(p+0x70));
}
/* Native SEND holds this owner CS before reaching the pipe callback's SRW
* lock. Retirement follows the same order, using a bounded try only. */
int WINAPI gw_wire_channel_retirement_lock(void* owner){
struct gw_channel_policy_control* c=&gw_wire_channel_policy;
if(!owner || CURRENT_OWNER!=owner || c->abi!=1 || c->size!=48 ||
(c->owner && c->owner!=(uint64_t)owner))return -20;
if(((uintptr_t)owner&(sizeof(void*)-1)) || !readable(owner,0x230))return -21;
if(!TryEnterCriticalSection((CRITICAL_SECTION*)((unsigned char*)owner+0x70)))return -23;
if(CURRENT_OWNER!=owner || c->abi!=1 || c->size!=48 ||
(c->owner && c->owner!=(uint64_t)owner)){
LeaveCriticalSection((CRITICAL_SECTION*)((unsigned char*)owner+0x70));return -20;
}
return 0;
}
void WINAPI gw_wire_channel_retirement_unlock(void* owner){
LeaveCriticalSection((CRITICAL_SECTION*)((unsigned char*)owner+0x70));
}
/* A pending native send queue is never dropped by source-session retirement.
* The established TSList empty shape is both exact head backlinks. NULL or
* an arbitrary tagged value does not establish an initialized empty list. */
int WINAPI gw_wire_channel_retirement_ready(void* owner){
int result=gw_wire_channel_retirement_lock(owner);void** head;
if(result)return result;
head=(void**)((unsigned char*)owner+0x200);
result=head[0]==head && (uintptr_t)head[1]==((uintptr_t)head|1)?0:-22;
gw_wire_channel_retirement_unlock(owner);
return result;
}
void WINAPI gw_wire_channel_retire_generation(void){
gw_wire_channel_policy=(struct gw_channel_policy_control){.abi=1,.size=48};
gw_wire_channel_contracts=(struct gw_channel_contract_status){.abi=1,.size=32};
awaiting_world=0;protection_waits=0;protection_started=0;
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
auto_disabled=0;
#endif
}