Files
WoW-Hybrid-gateway/tests/spell_locator_guard_diagnostics_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

151 lines
13 KiB
Python

#!/usr/bin/env python3
"""Exercise the actual guard functions with numeric guest-query fixtures only."""
import os
from pathlib import Path
import subprocess
import unittest
ROOT = Path(__file__).resolve().parents[1]
class GuardDiagnosticsTest(unittest.TestCase):
def test_actual_guard_branches_preserve_strict_type(self):
source = (ROOT / "native/wine_pipe_host.c").read_text()
functions = source[source.index("static int spell_barrier_guard_failure("):source.index("static int spell_barrier_initial_ids_result(")]
fixture = (ROOT / "tests/world_locator_fifo_test.c").read_text()
fixture = fixture[fixture.index("static void fixture_header("):fixture.index("int main(void)")]
output = Path(os.environ.get("GW_TEST_OUTPUT_DIR", ROOT / "build/test-spell-locator-guard")).resolve()
output.mkdir(parents=True, exist_ok=True)
unit = output / "guard-diagnostics.c"
unit.write_text(r'''
#include <assert.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include "wine_pipe_host.h"
#include "world_locator_fifo.h"
#include "engine_original_main_header.h"
#include "engine_spell_locator_ready.h"
typedef size_t SIZE_T;
typedef uint32_t DWORD;
typedef struct {void *BaseAddress,*AllocationBase;DWORD AllocationProtect;SIZE_T RegionSize;DWORD State,Protect,Type;} MEMORY_BASIC_INFORMATION;
#define MEM_IMAGE 0x1000000u
#define MEM_MAPPED 0x40000u
#define MEM_PRIVATE 0x20000u
#define MEM_COMMIT 0x1000u
#define PAGE_READONLY 2u
#define PAGE_EXECUTE_READWRITE 64u
static const uintptr_t main_base=UINT64_C(0x140000000);
static uintptr_t spell_barrier_main,module_base;
static DWORD spell_barrier_main_type,spell_barrier_main_protect,spell_barrier_main_allocation_protect,spell_barrier_main_state;
static unsigned char spell_barrier_main_now[4096],spell_barrier_main_header[4096],fixture[4096];
static uint32_t pipe_generation=1;
static struct gw_pipe_spell_locator_guard gw_wire_spell_locator_guard;
static MEMORY_BASIC_INFORMATION header_info,fixed_info;
static int identity,header_readable,fixed_readable,header_query,fixed_query,copies,queries;
static void put(unsigned char* p,uint32_t v){for(unsigned i=0;i<4;++i)p[i]=(unsigned char)(v>>(i*8));}
static void put64(unsigned char* p,uint64_t v){put(p,(uint32_t)v);put(p+4,(uint32_t)(v>>32));}
static void* GetModuleHandleA(const char* name){(void)name;return (void*)module_base;}
static DWORD GetLastError(void){return 87;}
static int spell_barrier_identity(void){return identity;}
static int retirement_readable(const void* pointer,SIZE_T bytes){(void)bytes;return (uintptr_t)pointer==module_base?header_readable:fixed_readable;}
static SIZE_T VirtualQuery(const void* pointer,MEMORY_BASIC_INFORMATION* info,SIZE_T bytes){
int header=(uintptr_t)pointer==module_base;assert(bytes==sizeof(*info));++queries;
if(!(header?header_query:fixed_query))return 0;
*info=header?header_info:fixed_info;return sizeof(*info);
}
static void* fixture_copy(void* out,const void* in,size_t bytes){
uintptr_t address=(uintptr_t)in;++copies;
if(address>=main_base&&address<main_base+4096){assert(bytes<=main_base+4096-address);return memcpy(out,fixture+address-main_base,bytes);}
if(address>=main_base+0x79c3d70&&address<main_base+0x79c3ff8){memset(out,0xa5,bytes);return out;}
if(address>=main_base+GW_SPELL_LOCATOR_PROVIDER_VTABLE_RVA&&address<main_base+GW_SPELL_LOCATOR_PROVIDER_VTABLE_RVA+16){memset(out,0xa5,bytes);return out;}
return memcpy(out,in,bytes);
}
#define __builtin_memcpy fixture_copy
''' + functions + '\n#undef __builtin_memcpy\n' + fixture + r'''
static void reset(void){
gw_wire_spell_locator_guard=(struct gw_pipe_spell_locator_guard){.abi=1,.size=sizeof(struct gw_pipe_spell_locator_guard)};
spell_barrier_main=0;module_base=main_base;identity=header_readable=fixed_readable=header_query=fixed_query=1;
spell_barrier_main_type=spell_barrier_main_protect=spell_barrier_main_allocation_protect=spell_barrier_main_state=0;
queries=copies=0;fixture_header(fixture);
header_info=(MEMORY_BASIC_INFORMATION){.BaseAddress=(void*)main_base,.AllocationBase=(void*)main_base,.AllocationProtect=2,.RegionSize=4096,.State=0x1000,.Protect=2,.Type=MEM_IMAGE};
fixed_info=header_info;fixed_info.BaseAddress=(void*)(main_base+0x79c3000);fixed_info.RegionSize=4096;fixed_info.Protect=4;
}
static void mapped(void){
reset();memcpy(fixture,gw_engine_original_main_header_bytes,GW_ENGINE_ORIGINAL_MAIN_HEADER_BYTES);
header_info.Type=fixed_info.Type=MEM_MAPPED;
header_info.AllocationProtect=fixed_info.AllocationProtect=PAGE_EXECUTE_READWRITE;
}
int main(void){
unsigned char value[16];uintptr_t fixed=main_base+0x79c3d70+0x278;
assert(sizeof(struct gw_pipe_spell_locator_barrier)==88&&sizeof(struct gw_pipe_spell_locator_guard)==144);
reset();identity=0;assert(!spell_barrier_main_qualified(1)&&gw_wire_spell_locator_guard.failed_condition==1&&!queries&&!copies);
reset();module_base+=0x1000;assert(!spell_barrier_main_qualified(1)&&gw_wire_spell_locator_guard.failed_condition==2&&!queries);
reset();module_base=0;assert(!spell_barrier_main_qualified(1)&&!gw_wire_spell_locator_guard.module_present&&gw_wire_spell_locator_guard.module_error==87);
reset();header_readable=0;assert(!spell_barrier_main_qualified(1)&&gw_wire_spell_locator_guard.failed_condition==3&&!copies);
assert(gw_wire_spell_locator_guard.header.observed&&!gw_wire_spell_locator_guard.header.readable&&gw_wire_spell_locator_guard.header.vq_result_bytes==sizeof(header_info));
assert(gw_wire_spell_locator_guard.header.state==0x1000&&gw_wire_spell_locator_guard.header.protect==2&&gw_wire_spell_locator_guard.header.allocation_matches_main&&gw_wire_spell_locator_guard.header.span_contained);
reset();header_query=0;assert(!spell_barrier_main_qualified(1)&&gw_wire_spell_locator_guard.failed_condition==4&&!copies);
assert(!gw_wire_spell_locator_guard.header.vq_result_bytes&&gw_wire_spell_locator_guard.header.win32_error==87);
reset();header_info.AllocationBase=(void*)(main_base+0x1000);assert(!spell_barrier_main_qualified(1)&&gw_wire_spell_locator_guard.failed_condition==5&&!gw_wire_spell_locator_guard.header.allocation_matches_main);
reset();header_info.Type=MEM_PRIVATE;assert(!spell_barrier_main_qualified(1)&&gw_wire_spell_locator_guard.failed_condition==6&&!copies&&gw_wire_spell_locator_guard.header.type==MEM_PRIVATE);
reset();header_info.Type=MEM_MAPPED;assert(!spell_barrier_main_qualified(1)&&gw_wire_spell_locator_guard.failed_condition==15&&!copies);
reset();fixture[0]=0;assert(!spell_barrier_main_qualified(1)&&gw_wire_spell_locator_guard.failed_condition==7&&!gw_wire_spell_locator_guard.header_valid);
reset();assert(spell_barrier_main_qualified(1)&&gw_wire_spell_locator_guard.header_valid&&gw_wire_spell_locator_guard.header_unchanged&&!gw_wire_spell_locator_guard.failed_condition);
fixture[0x20]^=1;assert(!spell_barrier_main_qualified(0)&&gw_wire_spell_locator_guard.failed_condition==8);
reset();assert(spell_barrier_main_qualified(1));identity=0;assert(!spell_barrier_read(0,fixed,value,16)&&gw_wire_spell_locator_guard.failed_condition==1);
reset();assert(spell_barrier_main_qualified(1));fixed_readable=0;assert(!spell_barrier_read(0,fixed,value,16)&&gw_wire_spell_locator_guard.failed_condition==10&&gw_wire_spell_locator_guard.failed_span==2);
assert(gw_wire_spell_locator_guard.fixed.observed&&!gw_wire_spell_locator_guard.fixed.readable&&gw_wire_spell_locator_guard.fixed.protect==4);
reset();assert(spell_barrier_main_qualified(1));fixed_query=0;assert(!spell_barrier_read(0,fixed,value,16)&&gw_wire_spell_locator_guard.failed_condition==11&&gw_wire_spell_locator_guard.fixed.win32_error==87);
reset();assert(spell_barrier_main_qualified(1));fixed_info.AllocationBase=(void*)(main_base+0x1000);assert(!spell_barrier_read(0,fixed,value,16)&&gw_wire_spell_locator_guard.failed_condition==12);
reset();assert(spell_barrier_main_qualified(1));fixed_info.Type=MEM_PRIVATE;assert(!spell_barrier_read(0,fixed,value,16)&&gw_wire_spell_locator_guard.failed_condition==13);
reset();assert(spell_barrier_main_qualified(1));fixed_info.Type=MEM_MAPPED;assert(!spell_barrier_read(0,fixed,value,16)&&gw_wire_spell_locator_guard.failed_condition==13);
reset();assert(spell_barrier_main_qualified(1));assert(spell_barrier_read(0,fixed,value,16)&&value[0]==0xa5&&!gw_wire_spell_locator_guard.failed_condition&&gw_wire_spell_locator_guard.fixed.allocation_matches_main);
assert(!spell_barrier_read(0,main_base+0x79c3d70+0x287,value,2)&&gw_wire_spell_locator_guard.failed_condition==14);
reset();assert(spell_barrier_main_qualified(1));fixed_readable=0;assert(!spell_barrier_read(0,0x10000,value,8)&&gw_wire_spell_locator_guard.failed_condition==10&&gw_wire_spell_locator_guard.failed_span==3);
assert(!spell_barrier_read(0,fixed,0,16)&&gw_wire_spell_locator_guard.failed_condition==9);
/* Actual J2e profile admits only registered expected Main + exact header.
* Existing independent helper checks establish the pinned byte authority. */
mapped();assert(spell_barrier_main_qualified(1)&&spell_barrier_main_type==MEM_MAPPED);
assert(spell_barrier_read(0,fixed,value,16)&&gw_wire_spell_locator_guard.fixed.type==MEM_MAPPED);
assert(spell_barrier_main_qualified(0)&&gw_wire_spell_locator_guard.header_unchanged);
mapped();identity=0;assert(!spell_barrier_main_qualified(1)&&gw_wire_spell_locator_guard.failed_condition==1);
mapped();module_base+=0x1000;assert(!spell_barrier_main_qualified(1)&&gw_wire_spell_locator_guard.failed_condition==2);
mapped();header_info.AllocationBase=(void*)(main_base+0x1000);assert(!spell_barrier_main_qualified(1)&&gw_wire_spell_locator_guard.failed_condition==5);
mapped();header_info.Type=MEM_PRIVATE;assert(!spell_barrier_main_qualified(1)&&gw_wire_spell_locator_guard.failed_condition==6);
mapped();header_info.Protect=4;assert(!spell_barrier_main_qualified(1)&&gw_wire_spell_locator_guard.failed_condition==15&&!copies);
mapped();header_info.AllocationProtect=2;assert(!spell_barrier_main_qualified(1)&&gw_wire_spell_locator_guard.failed_condition==15&&!copies);
mapped();header_info.State=0x2000;assert(!spell_barrier_main_qualified(1)&&gw_wire_spell_locator_guard.failed_condition==15&&!copies);
mapped();header_info.RegionSize=512;assert(!spell_barrier_main_qualified(1)&&gw_wire_spell_locator_guard.failed_condition==15&&!copies);
mapped();fixture[0x20]^=1;assert(!spell_barrier_main_qualified(1)&&gw_wire_spell_locator_guard.failed_condition==16&&gw_wire_spell_locator_guard.header_valid);
reset();header_info.Type=MEM_MAPPED;header_info.AllocationProtect=64;
assert(!spell_barrier_main_qualified(1)&&gw_wire_spell_locator_guard.failed_condition==16);
mapped();assert(spell_barrier_main_qualified(1));header_info.Type=MEM_IMAGE;
assert(!spell_barrier_main_qualified(0)&&gw_wire_spell_locator_guard.failed_condition==17);
mapped();assert(spell_barrier_main_qualified(1));fixed_info.Type=MEM_IMAGE;
assert(!spell_barrier_read(0,fixed,value,16)&&gw_wire_spell_locator_guard.failed_condition==13);
mapped();assert(spell_barrier_main_qualified(1));fixed_info.Type=MEM_PRIVATE;
assert(!spell_barrier_read(0,fixed,value,16)&&gw_wire_spell_locator_guard.failed_condition==13);
mapped();assert(spell_barrier_main_qualified(1));fixed_info.AllocationBase=(void*)(main_base+0x1000);
assert(!spell_barrier_read(0,fixed,value,16)&&gw_wire_spell_locator_guard.failed_condition==12);
mapped();assert(spell_barrier_main_qualified(1));fixed_info.AllocationProtect=2;
assert(!spell_barrier_read(0,fixed,value,16)&&gw_wire_spell_locator_guard.failed_condition==18);
mapped();assert(spell_barrier_main_qualified(1));fixed_info.State=0x2000;
assert(!spell_barrier_read(0,fixed,value,16)&&gw_wire_spell_locator_guard.failed_condition==18);
mapped();assert(spell_barrier_main_qualified(1));fixed_info.RegionSize=1;
assert(!spell_barrier_read(0,fixed,value,16)&&gw_wire_spell_locator_guard.failed_condition==18);
mapped();assert(spell_barrier_main_qualified(1));fixed_info.BaseAddress=(void*)((main_base+GW_SPELL_LOCATOR_PROVIDER_VTABLE_RVA)&~(uintptr_t)0xfff);
assert(spell_barrier_read(0,main_base+GW_SPELL_LOCATOR_PROVIDER_VTABLE_RVA+8,value,8)&&gw_wire_spell_locator_guard.fixed.type==MEM_MAPPED);
assert(!spell_barrier_read(0,main_base+GW_SPELL_LOCATOR_PROVIDER_VTABLE_RVA+12,value,8)&&gw_wire_spell_locator_guard.failed_condition==14);
return 0;
}
''')
command = ["gcc", "-std=c11", "-Wall", "-Wextra", "-Werror", "-fsanitize=address,undefined", "-fno-omit-frame-pointer", "-I", str(ROOT / "native"), str(unit), "-o", str(output / "guard-diagnostics")]
with (output / "compile-and-run.private.log").open("w") as log:
subprocess.run(command, check=True, stdout=log, stderr=subprocess.STDOUT)
subprocess.run([str(output / "guard-diagnostics")], check=True, stdout=log, stderr=subprocess.STDOUT)
if __name__ == "__main__":
unittest.main()