883 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
60 lines
4.0 KiB
Python
60 lines
4.0 KiB
Python
import os,struct,sys,tempfile,unittest
|
|
from collections import defaultdict
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'ops'))
|
|
import engine_pipe_host as pipe
|
|
import engine_wire_host as wire
|
|
|
|
|
|
class PublicMotionReadTest(unittest.TestCase):
|
|
def inspect_fixture(self,abi,size,*,real_memory=False):
|
|
base=0x100000;core=base+0x1000;motion=base+0x4000;hotfix=base+0x3000;queries=base+0x6000;world=base+0x7000
|
|
qry=struct.pack('<2I',1,1168)+bytes(1160);wld=struct.pack('<2I',1,432)+bytes(424)
|
|
hot=struct.pack('<9Ii',1,40,0,0,0,0,0,0,0,0)
|
|
raw=bytearray(pipe.SIZE);struct.pack_into('<4I',raw,0,1,pipe.SIZE,0,0)
|
|
view=struct.pack('<8I',abi,size,0,0,0,0,0,0)+bytes(size-32)
|
|
class Access:
|
|
def __init__(self,pid):
|
|
self.maps=[(queries,queries+1168,'r--p',0,'queries'),(world,world+432,'r--p',0,'world'),(hotfix,hotfix+40,'r--p',0,'hotfix'),(core,core+len(raw),'r--p',0,'core'),(motion,motion+1056,'r--p',0,'motion-prefix'),(motion+1056,motion+size,'r--p',0,'action-tail')]
|
|
def read(self,address,length):
|
|
if core<=address and address+length<=core+len(raw):return bytes(raw[address-core:address-core+length])
|
|
if hotfix<=address and address+length<=hotfix+40:return hot[address-hotfix:address-hotfix+length]
|
|
if queries<=address and address+length<=queries+1168:return qry[address-queries:address-queries+length]
|
|
if world<=address and address+length<=world+432:return wld[address-world:address-world+length]
|
|
if not any(low<=address and address+length<=high for low,high,*_ in self.maps):raise ValueError('Read spans public maps')
|
|
if motion<=address and address+length<=motion+size:return view[address-motion:address-motion+length]
|
|
raise ValueError('Unmapped fixture read')
|
|
def close(self):pass
|
|
exports=defaultdict(int,gw_wire_pipe_data=0x1000,gw_wire_motion_data=0x4000,gw_wire_hotfix_data=0x3000,gw_wire_pipe_queries=0x6000,gw_wire_world_data=0x7000)
|
|
memory=None
|
|
factory=Access
|
|
if real_memory:
|
|
memory=tempfile.TemporaryFile()
|
|
for address,data in [(core,bytes(raw)),(hotfix,hot),(queries,qry),(world,wld),(motion,view)]:
|
|
memory.seek(address);memory.write(data)
|
|
memory.flush()
|
|
access=object.__new__(wire.host.Access);access.pid=123;access.fd=os.dup(memory.fileno())
|
|
access.maps=[(core,core+len(raw),'r--p',0,'core'),(hotfix,hotfix+40,'r--p',0,'hotfix'),(queries,queries+1168,'r--p',0,'queries'),(world,world+432,'r--p',0,'world'),(motion,motion+size,'r--p',0,'contiguous-motion')]
|
|
factory=lambda pid:access
|
|
try:
|
|
with patch.object(pipe.host,'Access',factory),patch.object(pipe.wire,'module',return_value=(base,{'exports':exports},None)),patch.object(pipe,'inspect_code_contracts',return_value={}):
|
|
return pipe.inspect(123)
|
|
finally:
|
|
if memory is not None:memory.close()
|
|
def test_actual_pipe_inspection_reads_abi2_across_public_maps(self):
|
|
result=self.inspect_fixture(2,2208);self.assertEqual(result['motion']['abi'],2);self.assertEqual(result['motion']['snapshot_bytes'],2208)
|
|
def test_abi1_public_motion_boundary_remains_compatible(self):
|
|
result=self.inspect_fixture(1,1056);self.assertEqual(result['motion']['abi'],1);self.assertEqual(result['motion']['snapshot_bytes'],1056)
|
|
def test_contiguous_abi2_uses_the_actual_owned_memory_block_limit(self):
|
|
result=self.inspect_fixture(2,2208,real_memory=True)
|
|
self.assertEqual(result['motion']['snapshot_bytes'],2208)
|
|
def test_other_public_reads_keep_existing_bound(self):
|
|
class Access:
|
|
def read(self,address,length):return bytes(length)
|
|
with self.assertRaises(ValueError):wire.read_public_bytes(Access(),1,2049)
|
|
with self.assertRaises(ValueError):wire.read_public_bytes(Access(),1,2209,max_size=2208)
|
|
|
|
|
|
if __name__=='__main__':unittest.main()
|