100 lines
7.4 KiB
Python
100 lines
7.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Original state2 cleanup boundary guards; synthetic native call, no engine."""
|
|
import json
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
class OriginalDisconnectBoundary(unittest.TestCase):
|
|
def test_original_call_contract_and_refusals(self):
|
|
with tempfile.TemporaryDirectory(prefix='native-disconnect-',dir=ROOT/'build')as tmp:
|
|
d=Path(tmp)
|
|
(d/'windows.h').write_text('''#ifndef GW_TEST_WINDOWS_H
|
|
#define GW_TEST_WINDOWS_H
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#define WINAPI
|
|
#define __declspec(x)
|
|
typedef uint32_t DWORD; typedef int32_t LONG; typedef size_t SIZE_T;
|
|
#define MEM_COMMIT 0x1000
|
|
#define MEM_IMAGE 0x1000000
|
|
#define MEM_MAPPED 0x40000
|
|
#define PAGE_NOACCESS 1
|
|
#define PAGE_READWRITE 4
|
|
#define PAGE_GUARD 0x100
|
|
#define PAGE_EXECUTE 0x10
|
|
#define PAGE_EXECUTE_READ 0x20
|
|
#define PAGE_EXECUTE_READWRITE 0x40
|
|
#define PAGE_EXECUTE_WRITECOPY 0x80
|
|
typedef struct {void* BaseAddress;void* AllocationBase;size_t RegionSize;DWORD State,Protect,Type;} MEMORY_BASIC_INFORMATION;
|
|
DWORD GetCurrentThreadId(void);SIZE_T VirtualQuery(const void*,MEMORY_BASIC_INFORMATION*,SIZE_T);
|
|
#endif
|
|
''')
|
|
source=r'''#include <assert.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <windows.h>
|
|
static _Alignas(8) unsigned char owner[4096],code[64];static uint64_t holder;
|
|
static void* current_owner=owner;static unsigned calls,locks,unlocks,depth;
|
|
static int lock_result,post_failure,code_readable=1,owner_readable=1,code_type=MEM_IMAGE,code_protect=PAGE_EXECUTE_READ,touch_protect;static unsigned touches;static void* code_allocation=code;static DWORD thread=36;
|
|
DWORD GetCurrentThreadId(void){return thread;}
|
|
SIZE_T VirtualQuery(const void* p,MEMORY_BASIC_INFORMATION* m,SIZE_T n){
|
|
(void)n;uintptr_t a=(uintptr_t)p;
|
|
if(a>=(uintptr_t)owner&&a<(uintptr_t)owner+sizeof(owner)){if(!owner_readable)return 0;*m=(MEMORY_BASIC_INFORMATION){owner,owner,sizeof(owner),MEM_COMMIT,PAGE_READWRITE,0};return sizeof(*m);}
|
|
if(a>=(uintptr_t)code&&a<(uintptr_t)code+sizeof(code)){if(!code_readable)return 0;*m=(MEMORY_BASIC_INFORMATION){code,code_allocation,sizeof(code),MEM_COMMIT,code_protect,code_type};return sizeof(*m);}
|
|
if(p==&holder){*m=(MEMORY_BASIC_INFORMATION){&holder,&holder,8,MEM_COMMIT,PAGE_READWRITE,0};return sizeof(*m);}return 0;
|
|
}
|
|
int gw_wire_channel_retirement_lock(void* p){assert(p==owner);++locks;if(lock_result)return lock_result;++depth;return 0;}
|
|
void gw_wire_channel_retirement_unlock(void* p){assert(p==owner&&depth);--depth;++unlocks;}
|
|
static void original(void* p,uint32_t reason){assert(p==owner&&reason==14&&depth);++calls;assert(*(LONG*)(owner+0x98)==2);
|
|
if(post_failure==1)return;if(post_failure==2){*(LONG*)(owner+0x98)=0;return;}if(post_failure==3){current_owner=code;return;}
|
|
*(uint64_t*)(owner+0x1a0)=0;holder=post_failure==4?1:0;
|
|
}
|
|
#define GW_NATIVE_DISCONNECT_CURRENT_OWNER current_owner
|
|
#define GW_NATIVE_DISCONNECT_CODE code
|
|
#define GW_NATIVE_DISCONNECT_CALL(p,r) original(p,r)
|
|
#define GW_NATIVE_DISCONNECT_MAIN_BASE ((uintptr_t)code)
|
|
static void touch(void){assert(depth&&code_protect==PAGE_NOACCESS&&(code_type==MEM_MAPPED||code_type==MEM_IMAGE)&&code_allocation==code);++touches;if(touch_protect)code_protect=touch_protect;}
|
|
#define GW_NATIVE_DISCONNECT_PAGE_TOUCH() touch()
|
|
#include "native/nohelper/native_disconnect_retirement.private.h"
|
|
static void reset(void){memset(owner,0,sizeof(owner));current_owner=owner;holder=1;*(uint64_t*)(owner+0x228)=(uintptr_t)&holder;*(uint64_t*)(owner+0x1a0)=0x1234;*(LONG*)(owner+0x98)=2;
|
|
const unsigned char bytes[]={0x89,0x54,0x24,0x10,0x55,0x56,0x41,0x54,0x41,0x55,0x48,0x8d,0xac,0x24,0x18,0xff,0xff,0xff};memcpy(code,bytes,sizeof(bytes));calls=locks=unlocks=depth=0;lock_result=post_failure=0;code_readable=owner_readable=1;code_type=MEM_IMAGE;code_protect=PAGE_EXECUTE_READ;code_allocation=code;touch_protect=0;touches=0;thread=36;}
|
|
static int run(void){int r=gw_native_disconnect_retained_primary(owner,36);assert(depth==0);if(!lock_result)assert(locks==unlocks);return r;}
|
|
int main(void){
|
|
reset();assert(run()==0&&calls==1&&!*(uint64_t*)(owner+0x1a0)&&!holder);assert(run()==0&&calls==1); /* no duplicate native call */
|
|
for(unsigned i=0;i<9;i++){reset();if(i<3)*(LONG*)(owner+0x98)=i==0?0:i==1?5:6;else if(i<5)owner[0xc49+i-3]=1;else if(i<8)*(uint64_t*)(owner+0x1a8+(i-5)*8)=1;else current_owner=code;assert(run()==-42&&!calls);}
|
|
{static const int expected[]={-43,-47,-46,-45};
|
|
for(unsigned i=0;i<4;i++){reset();if(i==0)code_readable=0;if(i==1)code[0]^=1;if(i==2)code_protect=PAGE_READWRITE;if(i==3)code_type=0;assert(run()==expected[i]&&!calls&&!touches);}}
|
|
/* Actual Wine guest: Main code is MEM_MAPPED at the pinned allocation base. */
|
|
reset();code_type=MEM_MAPPED;assert(run()==0&&calls==1&&!touches);
|
|
reset();code_type=MEM_MAPPED;code_allocation=owner;assert(run()==-45&&!calls&&!touches);
|
|
reset();code_type=MEM_MAPPED;code_protect=PAGE_READWRITE;assert(run()==-46&&!calls&&!touches);
|
|
reset();code_type=MEM_MAPPED;code_protect=PAGE_EXECUTE_READWRITE;assert(run()==-46&&!calls&&!touches);
|
|
/* Lazily protected page: one in-page execution, then the same checks. */
|
|
reset();code_type=MEM_MAPPED;code_protect=PAGE_NOACCESS;touch_protect=PAGE_EXECUTE_READ;assert(run()==0&&calls==1&&touches==1);
|
|
reset();code_type=MEM_MAPPED;code_protect=PAGE_NOACCESS;assert(run()==-48&&!calls&&touches==1);
|
|
reset();code_type=MEM_MAPPED;code_protect=PAGE_NOACCESS;touch_protect=PAGE_EXECUTE_READ;code[0]^=1;assert(run()==-47&&!calls&&touches==1);
|
|
reset();code_type=MEM_MAPPED;code_protect=PAGE_NOACCESS;touch_protect=PAGE_READWRITE;assert(run()==-46&&!calls&&touches==1);
|
|
/* Idle image page (native Windows): same single execution, only inside Main. */
|
|
reset();code_type=MEM_IMAGE;code_protect=PAGE_NOACCESS;touch_protect=PAGE_EXECUTE_READ;assert(run()==0&&calls==1&&touches==1);
|
|
reset();code_type=MEM_IMAGE;code_protect=PAGE_NOACCESS;assert(run()==-48&&!calls&&touches==1);
|
|
reset();code_type=MEM_IMAGE;code_protect=PAGE_NOACCESS;code_allocation=owner;assert(run()==-46&&!calls&&!touches); /* never executed outside Main */
|
|
reset();*(uint64_t*)(owner+0x1a0)=0;holder=0;code_type=MEM_MAPPED;code_protect=PAGE_NOACCESS;assert(run()==0&&!calls&&!touches); /* clean state needs no code */
|
|
for(unsigned i=1;i<=4;i++){reset();post_failure=i;assert(run()==-44&&calls==1);}
|
|
reset();depth=1;assert(gw_native_disconnect_retained_primary(owner,36)==0&&calls==1&&depth==1&&locks==unlocks);depth=0; /* caller outer CS retained */
|
|
reset();lock_result=-23;assert(run()==-23&&!calls&&!unlocks);
|
|
reset();thread=37;assert(gw_native_disconnect_retained_primary(owner,36)==-40&&!calls&&!locks);
|
|
reset();owner_readable=0;assert(gw_native_disconnect_retained_primary(owner,36)==-41&&!calls&&!locks);
|
|
reset();assert(gw_native_disconnect_retained_primary(0,36)==-40&&!calls);
|
|
reset();*(uint64_t*)(owner+0x1a0)=0;assert(run()==-44&&!calls); /* orphan holder never forcibly cleared */
|
|
puts("Original Disconnect boundary PASS: state2/reason14/void ABI; balanced NativeCS; no repeated call; state/flags/secondary/postcondition refusals; code contract -43/-45/-46/-47/-48; mapped Main profile; one lazy-page execution");return 0;
|
|
}
|
|
'''
|
|
(d/'fixture.c').write_text(source)
|
|
subprocess.run(['cc','-std=c11','-Wall','-Wextra','-Werror','-Wno-misleading-indentation','-I'+str(d),'-I'+str(ROOT),str(d/'fixture.c'),'-o',str(d/'fixture')],check=True)
|
|
r=subprocess.run([str(d/'fixture')],capture_output=True,text=True)
|
|
self.assertEqual(r.returncode,0,r.stderr)
|
|
self.assertIn('boundary PASS',r.stdout)
|
|
if __name__=='__main__':unittest.main()
|