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

103 lines
6.4 KiB
Python

#!/usr/bin/env python3
"""Qualify same-PE recursion handling using the retained accepted carrier bytes.
Only a native host test and Windows-target syntax compilation run. No DLL load,
client, graphics call, build of the runtime tuple, or credential access occurs.
"""
from pathlib import Path
import hashlib
import subprocess
import pefile
ROOT = Path(__file__).resolve().parents[1]
OUT = ROOT / 'runtime/continuation-20261113/codex/Direct-entry-assessment/pe-wine-harness/shared-forwarding'
CARRIER = OUT / 'accepted-carrier.private.dll'
WITNESS = ROOT / 'runtime/continuation-20261113/codex/PE-managed-test-preparation/C53PECarrierGM-header'
SOURCE = r'''
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "dx12_carrier_forwarding.h"
static unsigned char image[0x22000];
static unsigned reads, deny, short_read;
static uint32_t reason;
static int read_carrier(void *module,uint32_t offset,void *out,size_t count,int execute) {
uintptr_t base=(uintptr_t)module;
++reads; assert(module==image);
if(deny || offset+count>sizeof(image))return -1;
if(short_read)return 0;
if(!gw_dx12_carrier_region_valid(base,base+offset,count,base,base,sizeof(image),
0x1000u,execute?0x20u:2u,0x1000000u,execute))return 0;
memcpy(out,image+offset,count);return 1;
}
static int original(int x,void **out){*out=(void *)(uintptr_t)0x1234;return -x;}
int main(int argc,char **argv) {
assert(argc==3);FILE *f=fopen(argv[1],"rb");assert(f);
assert(fread(image,1,sizeof(image),f)==sizeof(image));assert(!fclose(f));
void *self=(void *)(uintptr_t)0x5555,*direct=(void *)(uintptr_t)&original,*out=0;
void *resolved=gw_dx12_carrier_resolve(image,direct,self,read_carrier,&reason);
assert(resolved==direct && reads==0 && reason==0);
assert(((int (*)(int,void **))resolved)(17,&out)==-17 && out==(void *)(uintptr_t)0x1234);
assert(!gw_dx12_carrier_resolve(image,0,self,read_carrier,&reason) && reads==0);
assert(gw_dx12_carrier_resolve(image,self,self,read_carrier,&reason)==image+0x11d80 && reads==3 && reason==0);
/* Actual loaded header has a third preferred address; actual module
* binding remains the independently checked allocation, never this value. */
f=fopen(argv[2],"rb");assert(f);assert(fread(image,1,1024,f)==1024);assert(!fclose(f));
assert(gw_dx12_carrier_resolve(image,self,self,read_carrier,&reason)==image+0x11d80 && reason==0);
unsigned char preferred[8];memcpy(preferred,image+176,8);
memset(image+176,0,8);assert(!gw_dx12_carrier_resolve(image,self,self,read_carrier,&reason) && reason==11);
memcpy(image+176,preferred,8);image[176]|=1;
assert(!gw_dx12_carrier_resolve(image,self,self,read_carrier,&reason) && reason==11);
memcpy(image+176,preferred,8);
const unsigned changes[]={0,175,184,0x118,0x21000,0x21171,0x11d80,0x11dee};
for(unsigned i=0;i<sizeof(changes)/sizeof(changes[0]);++i){
image[changes[i]]^=1;assert(!gw_dx12_carrier_resolve(image,self,self,read_carrier,&reason));assert(reason==(i<4?11u:i<6?12u:13u));image[changes[i]]^=1;
}
deny=1;assert(!gw_dx12_carrier_resolve(image,self,self,read_carrier,&reason));assert(reason==20);deny=0;
short_read=1;assert(!gw_dx12_carrier_resolve(image,self,self,read_carrier,&reason));assert(reason==21);short_read=0;
assert(!gw_dx12_carrier_resolve(0,self,self,read_carrier,&reason));
assert(!gw_dx12_carrier_resolve(image,self,self,0,&reason));
assert(!gw_dx12_carrier_resolve((void *)(UINTPTR_MAX-32),self,self,read_carrier,&reason));
uintptr_t b=(uintptr_t)image;
assert(gw_dx12_carrier_region_valid(b,b+0x11d80,111,b,b,sizeof(image),0x1000,0x20,0x1000000,1));
assert(!gw_dx12_carrier_region_valid(b,b+0x11d80,111,b+1,b,sizeof(image),0x1000,0x20,0x1000000,1));
assert(!gw_dx12_carrier_region_valid(b,b+0x11d80,111,b,b,sizeof(image),0x1000,0x20,0x20000,1));
assert(!gw_dx12_carrier_region_valid(b,b+0x11d80,111,b,b,sizeof(image),0x2000,0x20,0x1000000,1));
assert(!gw_dx12_carrier_region_valid(b,b+0x11d80,111,b,b,sizeof(image),0x1000,0x120,0x1000000,1));
assert(!gw_dx12_carrier_region_valid(b,b+0x11d80,111,b,b,sizeof(image),0x1000,4,0x1000000,1));
assert(!gw_dx12_carrier_region_valid(b,b+0x11d80,111,b,b,0x11dee,0x1000,0x20,0x1000000,1));
assert(!gw_dx12_carrier_region_valid(b,b-1,111,b,b,sizeof(image),0x1000,0x20,0x1000000,1));
puts("PASS captured third-address ImageBase, zero/unaligned and adjacent-field refusal; native-direct, exact entry/export and allocation guards");
}
'''
def main():
data = CARRIER.read_bytes()
assert hashlib.sha256(data).hexdigest() == '403b1036c7635a4c9fc48d1e5eb183d47ad6a478c5ad82b9f84ee01bd1e75f95'
pe = pefile.PE(data=data)
mapped = bytearray(pe.OPTIONAL_HEADER.SizeOfImage)
mapped[:pe.OPTIONAL_HEADER.SizeOfHeaders] = data[:pe.OPTIONAL_HEADER.SizeOfHeaders]
for section in pe.sections:
body=section.get_data();mapped[section.VirtualAddress:section.VirtualAddress+len(body)]=body
# Consume the exact second owned capture; verify its unaffected guard spans.
captured=(WITNESS/'mapped-header.bin').read_bytes()
assert len(captured)==1024
assert [i for i,(a,b) in enumerate(zip(data[:1024],captured)) if a!=b]==[178,179,180,181]
assert int.from_bytes(captured[176:184],'little')==0x6ffffa080000
# Captured0x1c000 is retained original .edata; active forwarded
# directory remains .gwexp0x21000 and keeps its existing exact guard.
assert pe.OPTIONAL_HEADER.DATA_DIRECTORY[0].VirtualAddress==0x21000
assert pe.OPTIONAL_HEADER.DATA_DIRECTORY[0].Size==413
assert (WITNESS/'export.bin').read_bytes()==mapped[0x1c000:0x1c000+413]
assert (WITNESS/'body.bin').read_bytes()==mapped[0x11d80:0x11d80+111]
fixture=OUT/'accepted-carrier-mapped.private.bin';fixture.write_bytes(mapped)
harness=OUT/'forwarding-test.c';harness.write_text(SOURCE)
binary=OUT/'forwarding-test'
subprocess.run(['clang','-std=c11','-Wall','-Wextra','-Werror','-I'+str(ROOT/'native/nohelper'),str(harness),'-o',str(binary)],check=True)
subprocess.run([str(binary),str(fixture),str(WITNESS/"mapped-header.bin")],check=True)
subprocess.run(['clang','--target=x86_64-pc-windows-msvc','-fsyntax-only','-ffreestanding','-Wall','-Wextra','-Werror','-Wno-missing-field-initializers','-DGW_SOURCE_ONLY_PROFILE=2','-DGW_WINDOWS_NATIVE_D3D11=1',str(ROOT/'native/nohelper/startup_nohelper.c')],check=True)
print('PASS actual common Windows-target startup source syntax')
if __name__ == '__main__':
main()