883 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
95 lines
6.3 KiB
Python
95 lines
6.3 KiB
Python
"""Exercise the production drain admission around a mocked native call boundary."""
|
|
from pathlib import Path
|
|
import os
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
class PendingDefinitionDrainTest(unittest.TestCase):
|
|
def test_native_dispatch_progresses_per_poll_nonrecursive_outside_lock(self):
|
|
source = (ROOT / "native/wine_pipe_host.c").read_text()
|
|
start = source.index("static LONG spell_pending_drain_active;")
|
|
end = source.index("/* Called only by our verified original-frame", start)
|
|
actual = source[start:end]
|
|
self.assertEqual(actual.count("((void (WINAPI *)(void))entry)();"), 1)
|
|
actual = actual.replace("((void (WINAPI *)(void))entry)();", "mock_native_drain();")
|
|
fixture = r'''
|
|
#include <assert.h>
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include <string.h>
|
|
#include "world_locator_fifo.h"
|
|
typedef long LONG;
|
|
typedef unsigned long DWORD;
|
|
typedef unsigned short USHORT;
|
|
typedef struct {void *BaseAddress,*AllocationBase;size_t RegionSize;DWORD State,Protect;} MEMORY_BASIC_INFORMATION;
|
|
#define MEM_COMMIT 0x1000u
|
|
#define PAGE_GUARD 0x100u
|
|
#define PAGE_NOACCESS 1u
|
|
#define PAGE_EXECUTE_READ 0x20u
|
|
#define PAGE_EXECUTE_READWRITE 0x40u
|
|
#define PAGE_EXECUTE_WRITECOPY 0x80u
|
|
struct gw_pipe_spell_locator_barrier {uint32_t state,classified,known_count,generation,required_count;uint64_t started_ms;};
|
|
static struct gw_pipe_spell_locator_barrier gw_wire_spell_locator_barrier;
|
|
static uintptr_t spell_barrier_main;
|
|
static uint64_t spell_barrier_owner=1,spell_barrier_connection=2,spell_barrier_pipe=3;
|
|
static LONG pipe_generation=1;
|
|
static int stack_mode,retire;
|
|
static uint64_t now;
|
|
static uint64_t GetTickCount64(void){return now;}
|
|
static int lock,held,busy,enabled=1,identity_ok=1,main_ok=1,calls,notes,nested;
|
|
static DWORD protection=PAGE_EXECUTE_READ;
|
|
/* Independently retained original70334 64E630 prefix (qualified native receipt). */
|
|
static unsigned char native_code[]={0x48,0x89,0x5c,0x24,0x18,0x55,0x41,0x56,0x41,0x57,0x48,0x83,0xec,0x50,0x48,0x8d,0x0d,0x83,0xdb,0x39,0x07,0xff,0x15,0x1d,0x3f,0x17,0x04};
|
|
static USHORT CaptureStackBackTrace(DWORD skip,DWORD cap,void**frames,void*hash){assert(skip==0&&cap==64&&!hash);if(stack_mode==2)return 0;if(stack_mode==3)return 64;frames[0]=(void*)(spell_barrier_main+(stack_mode==1?0x64e700u:0x23f927bu));return 1;}
|
|
static LONG InterlockedCompareExchange(LONG*p,LONG value,LONG expected){LONG old=*p;if(old==expected)*p=value;return old;}
|
|
static void InterlockedExchange(LONG*p,LONG value){*p=value;}
|
|
static DWORD GetEnvironmentVariableA(const char*n,char*out,DWORD cap){(void)n;assert(cap==2);if(enabled==2)return 0;out[0]=enabled?'1':'0';out[1]=0;return 1;}
|
|
static int TryAcquireSRWLockExclusive(int*p){assert(p==&lock);if(busy)return 0;assert(!held);held=1;return 1;}
|
|
static void ReleaseSRWLockExclusive(int*p){assert(p==&lock&&held);held=0;}
|
|
static int spell_barrier_identity(void){assert(held);return identity_ok;}
|
|
static int spell_barrier_main_qualified(int initial){assert(held&&!initial);return main_ok;}
|
|
static size_t VirtualQuery(void*entry,MEMORY_BASIC_INFORMATION*out,size_t size){assert(held&&entry==native_code);*out=(MEMORY_BASIC_INFORMATION){native_code,(void*)spell_barrier_main,sizeof(native_code),MEM_COMMIT,protection};return size;}
|
|
static void pipe_note(const char*label,unsigned value){assert(!held&&(value==1||value==2));assert(strstr(label,"pending-drain"));++notes;}
|
|
static int spell_pending_drain_before_barrier(void);
|
|
static void mock_native_drain(void){assert(!held);++calls;nested=spell_pending_drain_before_barrier();assert(nested==0);if(retire){++pipe_generation;gw_wire_spell_locator_barrier.state=0;}}
|
|
'''
|
|
fixture += actual
|
|
fixture += r'''
|
|
static void epoch(unsigned value){now=value+1;gw_wire_spell_locator_barrier=(struct gw_pipe_spell_locator_barrier){.state=1,.classified=3,.known_count=3,.generation=(uint32_t)pipe_generation,.required_count=2,.started_ms=value};}
|
|
int main(void){
|
|
spell_barrier_main=(uintptr_t)native_code-0x64e630u;epoch(1);
|
|
stack_mode=1;assert(spell_pending_drain_before_barrier()==1&&calls==0);stack_mode=2;assert(spell_pending_drain_before_barrier()==1&&calls==0);stack_mode=3;assert(spell_pending_drain_before_barrier()==1&&calls==0);stack_mode=0;
|
|
enabled=0;assert(spell_pending_drain_before_barrier()==1&&calls==0);enabled=1;
|
|
busy=1;assert(spell_pending_drain_before_barrier()==1&&calls==0&&!held);busy=0;
|
|
identity_ok=0;assert(spell_pending_drain_before_barrier()==1&&calls==0&&!held);identity_ok=1;
|
|
main_ok=0;assert(spell_pending_drain_before_barrier()==1&&calls==0&&!held);main_ok=1;
|
|
protection=PAGE_EXECUTE_READ|PAGE_GUARD;assert(spell_pending_drain_before_barrier()==1&&calls==0);protection=PAGE_EXECUTE_READ;
|
|
native_code[0]^=1;assert(spell_pending_drain_before_barrier()==1&&calls==0);native_code[0]^=1;
|
|
gw_wire_spell_locator_barrier.classified=2;assert(spell_pending_drain_before_barrier()==1&&calls==0);gw_wire_spell_locator_barrier.classified=3;
|
|
enabled=2; /* Normal play: no private variable is required. */
|
|
assert(spell_pending_drain_before_barrier()==1&&calls==1&¬es==1&&!held&&!spell_pending_drain_active);
|
|
assert(spell_pending_drain_before_barrier()==1&&calls==2&¬es==2);
|
|
now=5001;assert(spell_pending_drain_before_barrier()==1&&calls==2);
|
|
epoch(2);assert(spell_pending_drain_before_barrier()==1&&calls==3&¬es==3&&!held);
|
|
++pipe_generation;epoch(2);assert(spell_pending_drain_before_barrier()==1&&calls==4&¬es==4);
|
|
retire=1;epoch(3);assert(spell_pending_drain_before_barrier()==0&&calls==5&¬es==5&&!held&&!spell_pending_drain_active);retire=0;
|
|
gw_wire_spell_locator_barrier.state=3;epoch(4);gw_wire_spell_locator_barrier.state=3;assert(spell_pending_drain_before_barrier()==1&&calls==5);
|
|
return 0;
|
|
}
|
|
'''
|
|
with tempfile.TemporaryDirectory(prefix="pending-definition-", dir=ROOT / "runtime") as temporary:
|
|
output = Path(temporary)
|
|
(output / "fixture.c").write_text(fixture)
|
|
subprocess.run(["cc", "-std=c11", "-Wall", "-Wextra", "-Werror",
|
|
"-fsanitize=address,undefined", "-fno-omit-frame-pointer",
|
|
"-I", str(ROOT / "native"), str(output / "fixture.c"), "-o", str(output / "fixture")], check=True)
|
|
subprocess.run([str(output / "fixture")], env={**os.environ, "ASAN_OPTIONS": "detect_leaks=1"}, check=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|