883 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
87 lines
4.0 KiB
Python
87 lines
4.0 KiB
Python
import ctypes
|
|
import json
|
|
import struct
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
import sys
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / 'ops'))
|
|
import map_lod_cell_trace as trace
|
|
from engine_main_thread_probe import Registers
|
|
|
|
|
|
class FakeProbe:
|
|
def __init__(self):
|
|
self.registers = Registers()
|
|
self.followup = None
|
|
self.validated = False
|
|
|
|
def ptrace(self, request, _, output):
|
|
if request != 'getregs':
|
|
raise ValueError('unexpected fake debugger action')
|
|
ctypes.memmove(output, ctypes.addressof(self.registers), ctypes.sizeof(Registers))
|
|
|
|
def validate_followup(self):
|
|
self.validated = True
|
|
|
|
|
|
class CaptureBoundary(unittest.TestCase):
|
|
def capture(self, output, *, returned_delta=0):
|
|
pid, child, parent, table, rsp = 19, 0x1000, 0x2000, 0x100000, 0x3000
|
|
a, b, width, height = 3, 4, 8, 8
|
|
record = table + (b * width + a) * 72
|
|
raw = bytearray(72); struct.pack_into('<I', raw, 0x30, 1286612)
|
|
bounds = bytearray(40)
|
|
struct.pack_into('<iiiiii', bounds, 0, 0, 0, 7, 7, width, height)
|
|
struct.pack_into('<Q', bounds, 0x20, table)
|
|
segments = {child + 0x18: struct.pack('<Q', parent), child + 0x4c: struct.pack('<ii', a, b),
|
|
parent + 0x438: bytes(bounds), record: bytes(raw), record + 0x30: bytes(raw[0x30:0x34])}
|
|
def read(actual_pid, address, size):
|
|
if actual_pid != pid or address not in segments or len(segments[address]) != size:
|
|
raise ValueError('unexpected selected read')
|
|
return segments[address]
|
|
observer = trace.CellCapture(pid, trace.IMAGE_BASE, output, read=read)
|
|
observer.prefixes = {trace.LOAD_RVA: b'load'}
|
|
observer.probe = FakeProbe()
|
|
pre = Registers(); pre.rip = trace.IMAGE_BASE + trace.PRE_RVA
|
|
pre.rbx, pre.rcx, pre.rdx, pre.r8, pre.rsp = child, parent, a, b, rsp
|
|
observer.pre_call(pid, pre)
|
|
returned = observer.probe.registers
|
|
returned.rip = trace.IMAGE_BASE + trace.RETURN_RVA
|
|
returned.rbx, returned.rsp, returned.rax = child, rsp, record + 0x20 + returned_delta
|
|
observer.after_return(pid, None)
|
|
return observer, record, child, rsp
|
|
|
|
def test_correlated_capture_and_invalid_return_are_distinct(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
output = Path(directory) / 'capture.json'
|
|
observer, record, child, rsp = self.capture(output)
|
|
self.assertTrue(observer.probe.validated)
|
|
regs = observer.probe.registers
|
|
regs.rip, regs.rbx, regs.rsp, regs.rsi = trace.IMAGE_BASE + trace.LOAD_RVA, child, rsp, record + 0x20
|
|
observer.at_load(observer.pid, None)
|
|
result = json.loads(output.read_text())
|
|
self.assertEqual(result['status'], 'PASS_SAME_THREAD_CELL_CAPTURE_WDT_IDENTITY_HELD')
|
|
self.assertEqual(result['events'][-1]['consumed_dword'], 1286612)
|
|
self.assertEqual(result['runtime_wdt_identity'], 'HELD_UNPROVED')
|
|
self.assertEqual(len(observer.partial.read_text().splitlines()), 3)
|
|
held = Path(directory) / 'held.json'
|
|
observer, *_ = self.capture(held, returned_delta=1)
|
|
self.assertEqual(json.loads(held.read_text())['status'], 'HELD_CELL_ADDRESS')
|
|
self.assertIsNone(observer.probe.followup)
|
|
|
|
def test_non_candidate_publication_is_never_armed(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory)
|
|
(root / 'linux-release.json').write_text(json.dumps({'private_asset_publication': {'composed_root': 'other'}}))
|
|
with patch.object(trace, 'ROOT', root), patch.object(trace.lifecycle, 'read_launch', return_value={'owned': True}):
|
|
with self.assertRaisesRegex(ValueError, 'candidate publication'):
|
|
trace.start(19, clean_copy=True, existing_probe=None)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|