Files
WoW-Hybrid-gateway/tests/cold_start_disconnect_trace_test.py
praxisandClaude Fable 5.2 7f2139d8d0 Offline test fixtures
883 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

85 lines
4.0 KiB
Python

#!/usr/bin/env python3
"""Exercise the real local-only cold disconnect diagnostic's privacy bounds."""
import pathlib
import subprocess
import tempfile
import unittest
ROOT = pathlib.Path(__file__).resolve().parents[1]
class ColdDisconnectTrace(unittest.TestCase):
def test_gates_and_executable_main_filter(self):
source = (ROOT / 'native/wine_pipe_host.c').read_text()
begin = source.index('static void cold_start_disconnect_trace(')
end = source.index('\n#endif', begin)
fixture = r'''
#include <stdint.h>
#include <stddef.h>
#include <assert.h>
#include <string.h>
typedef long LONG;typedef uint32_t DWORD;typedef uint16_t USHORT;
typedef struct { void* AllocationBase;DWORD State,Protect; } MEMORY_BASIC_INFORMATION;
#define MEM_COMMIT 0x1000
#define PAGE_GUARD 0x100
#define PAGE_NOACCESS 1
#define PAGE_READWRITE 4
#define PAGE_EXECUTE_READ 0x20
#define PAGE_EXECUTE_READWRITE 0x40
#define PAGE_EXECUTE_WRITECOPY 0x80
static struct { unsigned world_auth,closed,error,auth_request_sent; } gw_wire_pipe_data;
static uintptr_t base=0x140000000ULL;
static unsigned option=1,captures,notes,frame_notes;
static void* GetModuleHandleW(void* unused){(void)unused;return (void*)base;}
static DWORD GetEnvironmentVariableA(const char* key,char* out,DWORD size){assert(!strcmp(key,"GW_PRIVATE_COLD_START_TRACE")&&size==2);out[0]=option?'1':'0';out[1]=0;return 1;}
static LONG InterlockedCompareExchange(LONG* p,LONG next,LONG old){LONG value=*p;if(value==old)*p=next;return value;}
static USHORT CaptureStackBackTrace(DWORD skip,DWORD size,void** out,void* hash){
assert(skip==0&&size==64&&!hash);++captures;
uintptr_t rows[]={0x140001001ULL,0x140001002ULL,0x140001003ULL,0x140001004ULL,0x140001005ULL,0x140001006ULL,0x140001007ULL,0x140001008ULL,0x140000100ULL,0x1447c145cULL,0x700000001000ULL};
for(unsigned i=0;i<sizeof(rows)/sizeof(*rows);++i)out[i]=(void*)rows[i];return sizeof(rows)/sizeof(*rows);
}
static size_t VirtualQuery(void* pc,MEMORY_BASIC_INFORMATION* info,size_t bytes){
uintptr_t rva=(uintptr_t)pc-base;info->AllocationBase=(void*)base;info->State=MEM_COMMIT;info->Protect=PAGE_EXECUTE_READ;
if(rva==0x1002)return 0;
if(rva==0x1003)info->State=0;
if(rva==0x1004)info->AllocationBase=(void*)0x700000000000ULL;
if(rva==0x1005)info->Protect|=PAGE_GUARD;
if(rva==0x1006)info->Protect=PAGE_READWRITE;
if(rva==0x1007)info->Protect=PAGE_EXECUTE_READWRITE;
if(rva==0x1008)info->Protect=PAGE_EXECUTE_WRITECOPY;
return bytes;
}
static void pipe_note(const char* label,uint32_t value){
++notes;
if(!strcmp(label,"GW_COLD_START disconnect-stack-depth=")){assert(value==11);return;}
assert(!strcmp(label,"GW_COLD_START main-frame-rva="));
assert(value==0x1001||value==0x1007||value==0x1008);++frame_notes;
}
'''
fixture += source[begin:end]
fixture += r'''
int main(void){
cold_start_disconnect_trace(14);assert(!captures);
gw_wire_pipe_data.auth_request_sent=1;
cold_start_disconnect_trace(13);assert(!captures);
option=0;cold_start_disconnect_trace(14);assert(!captures);option=1;
base=0x700000000000ULL;cold_start_disconnect_trace(14);assert(!captures);base=0x140000000ULL;
gw_wire_pipe_data.world_auth=1;cold_start_disconnect_trace(14);assert(!captures);gw_wire_pipe_data.world_auth=0;
gw_wire_pipe_data.closed=1;cold_start_disconnect_trace(14);assert(!captures);gw_wire_pipe_data.closed=0;
gw_wire_pipe_data.error=1;cold_start_disconnect_trace(14);assert(!captures);gw_wire_pipe_data.error=0;
cold_start_disconnect_trace(14);assert(captures==1&&notes==4&&frame_notes==3);
cold_start_disconnect_trace(14);assert(captures==1&&notes==4);
}
'''
with tempfile.TemporaryDirectory() as directory:
p = pathlib.Path(directory)
(p / 'trace.c').write_text(fixture)
subprocess.run(['cc', '-std=c11', '-fsanitize=address,undefined',
'-fno-omit-frame-pointer', str(p / 'trace.c'),
'-o', str(p / 'trace')], check=True)
subprocess.run([str(p / 'trace')], check=True)
if __name__ == '__main__':
unittest.main()