Files
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

122 lines
7.2 KiB
Python

#!/usr/bin/env python3
"""Exercise breakpoint lifetime/cleanup against a disposable own Linux child.
No WoW process, engine calls, game compatibility claim or process code writes.
"""
import ctypes as C
import json
import os
from pathlib import Path
import signal
import sys
import tempfile
import time
ROOT=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(ROOT/'ops'))
import engine_main_thread_probe as probe_module
with tempfile.TemporaryDirectory(prefix='native-engine-probe-') as temporary:
root=Path(temporary);(root/'runtime').mkdir();(root/'build').mkdir()
(root/'build/native-ptrace-abi').symlink_to(Path(sys.argv[1]).resolve() if len(sys.argv)>1 else ROOT/'build/native-ptrace-abi')
readfd,writefd=os.pipe();pid=os.fork()
if pid==0:
try:
os.close(readfd)
libc=C.CDLL(None);libc.mmap.argtypes=[C.c_void_p,C.c_size_t,C.c_int,C.c_int,C.c_int,C.c_long];libc.mmap.restype=C.c_void_p
address=0x140001000
mapped=libc.mmap(address,4096,3,0x100022,-1,0) # MAP_FIXED_NOREPLACE|PRIVATE|ANONYMOUS, Linux.
if mapped!=address:os._exit(2)
C.memmove(address,b'\xc3',1)
C.memmove(address+32,b'\xc3',1);C.memmove(address+64,b'\xc3',1)
if libc.mprotect(C.c_void_p(address),4096,5):os._exit(3)
os.write(writefd,b'ready');os.close(writefd)
function=C.CFUNCTYPE(None)(address)
second=C.CFUNCTYPE(None)(address+32)
end=time.monotonic()+5
while time.monotonic()<end:function();second();time.sleep(.01)
os._exit(0)
except BaseException:os._exit(4)
os.close(writefd)
try:
assert os.read(readfd,5)==b'ready'
probe_module.ROOT=root
plan={'pid':pid,'login_callback':'0x140001000','bridge_callback':'0x0'}
plan_path=root/'runtime/owned-observation-plan.json';result_path=root/'runtime/owned-observation-result.json'
plan_path.write_text(json.dumps(plan))
probe=probe_module.Probe(pid,observe_only=True,plan_path=plan_path,result_path=result_path,armed_timeout=2,
metadata_at_observation=lambda actual,registers:{'owned_child_matches':actual==pid})
deadline=time.monotonic()+3
while not probe.done and time.monotonic()<deadline:probe.poll();time.sleep(.01)
assert probe.done and not probe.attached
result=json.loads(result_path.read_text())
assert result['status']=='real-callback-observed-only' and result['hardware_registers_restored']
assert result['linux_thread_id']==pid and result['observed_entry']=='0x140001000'
assert result['execution_redirected'] is False and result['caller_return'] is None
assert result['metadata']=={'owned_child_matches':True}
# A fixed register-aware owned-data binding must not become a function
# call/redirection. The observed child resumes every original instruction.
bound=[]
def register_binding(actual,registers):
bound.append((actual,registers.rip))
return {'owned_data_published':True,'credential_values_exported':False}
probe=probe_module.Probe(pid,observe_only=True,plan_path=plan_path,result_path=result_path,armed_timeout=2,
register_data_binding=register_binding)
deadline=time.monotonic()+3
while not probe.done and time.monotonic()<deadline:probe.poll();time.sleep(.01)
result=json.loads(result_path.read_text())
assert bound==[(pid,0x140001000)] and result['status']=='engine-safepoint-data-binding'
assert not result['execution_redirected'] and not result['instruction_pointer_changed']
assert result['hardware_registers_restored'] and result['metadata']['owned_data_published']
def rejected_metadata(actual,registers):raise ValueError('private-canary-do-not-export')
probe=probe_module.Probe(pid,observe_only=True,plan_path=plan_path,result_path=result_path,armed_timeout=2,
metadata_at_observation=rejected_metadata)
deadline=time.monotonic()+3
while not probe.done and time.monotonic()<deadline:probe.poll();time.sleep(.01)
result=json.loads(result_path.read_text())
assert probe.done and not probe.attached and result['hardware_registers_restored']
assert result['status']=='observation-callback-failed' and result['execution_redirected'] is False
assert 'private-canary' not in json.dumps(result)
actions=[]
probe=probe_module.Probe(pid,observe_only=True,plan_path=plan_path,result_path=result_path,armed_timeout=.3,
at_observation=lambda actual,caller:actions.append('first'),
followup=(0x140001020,b'\xc3',lambda actual,caller:actions.append('second')))
deadline=time.monotonic()+2
while not probe.done and time.monotonic()<deadline:probe.poll();time.sleep(.005)
result=json.loads(result_path.read_text())
assert actions==['first','second'] and probe.done and not probe.attached
assert result['observed_entry']=='0x140001020' and result['hardware_registers_restored'] and not result['instruction_pointer_changed']
actions=[]
probe=probe_module.Probe(pid,observe_only=True,plan_path=plan_path,result_path=result_path,armed_timeout=.15,
at_observation=lambda actual,caller:actions.append('first'),
followup=(0x140001040,b'\xc3',lambda actual,caller:actions.append('unreachable')))
deadline=time.monotonic()+1
while not probe.done and time.monotonic()<deadline:probe.poll();time.sleep(.005)
result=json.loads(result_path.read_text());assert actions==['first'] and result['status']=='no-callback-before-timeout'
assert probe.done and not probe.attached
# Child keeps executing normally after detach; no SIGTRAP leak.
waited,status=os.waitpid(pid,0);assert waited==pid and os.WIFEXITED(status) and os.WEXITSTATUS(status)==0
print('Own-child hardware execution breakpoint observed once, registers restored, child resumed normally; no code writes.')
finally:
os.close(readfd)
try:os.kill(pid,signal.SIGTERM)
except ProcessLookupError:pass
# Regression: deadline must apply even if every poll receives another genuine
# signal. Previously a continuously busy Wine startup escaped the timeout.
fake=probe_module.Probe.__new__(probe_module.Probe)
fake.pid=4242;fake.done=False;fake.attached=True;fake.stopped=False;fake.stage='armed'
fake.deadline=0;fake.abi={'wall':0};fake.saved={};fake.stops=0
calls=[];records=[]
fake.ptrace=lambda *args:calls.append(args)
fake.restore_debug=lambda:calls.append(('restored',))
fake.record=lambda *args,**kwargs:records.append((args,kwargs))
old_waitpid=probe_module.os.waitpid
try:
probe_module.os.waitpid=lambda *args:(4242,(signal.SIGUSR1<<8)|0x7f)
fake.poll()
finally:probe_module.os.waitpid=old_waitpid
assert fake.done and not fake.attached
assert ('interrupt',) in calls and ('restored',) in calls
assert ('detach',0,signal.SIGUSR1) in calls
assert records[0][0]==('no-callback-before-timeout',)
print('Busy-signal timeout restores debug registers and forwards the pending genuine signal.')