"""Bounded interpretation of the actual compiled PID rejection path. The execution-context gate result is an explicit boundary fixture. This does not execute that gate, the successful wake backend, or a new guest run. """ from pathlib import Path import hashlib import importlib.util import json import re import shutil import subprocess import sys import tempfile machine_path = Path(__file__).with_name('k2100-aarch64-admission-machine.py') spec = importlib.util.spec_from_file_location('admission_machine', machine_path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) out = Path(tempfile.mkdtemp(prefix='K2100-native-wake-refusal-proof-', dir='reports/os-implementation')) shutil.copyfile(__file__, out / 'harness.py.txt') shutil.copyfile(machine_path, out / 'machine.py.txt') def artifact(path): path = Path(path) return dict(path=str(path), sha256=hashlib.sha256(path.read_bytes()).hexdigest()) def checked(a): assert artifact(a['path'])['sha256'] == a['sha256'], a['path'] return Path(a['path']).read_bytes() def pack(values): return b''.join(v.to_bytes(8, 'little') for v in values) try: preflight_path = Path(sys.argv[1]) pre = json.loads(preflight_path.read_text()) assert pre['status'] == 'NATIVE_MODELS_COMPILED' image = checked(pre['artifacts']['image']) checked(pre['artifacts']['elf']) symbols = [] for line in checked(pre['artifacts']['nm']).decode().splitlines(): fields = line.split() if len(fields) == 4: symbols.append(dict(address=int(fields[0], 16), size=int(fields[1], 16), name=fields[3])) matches = [s for s in symbols if '13process_table' in s['name'] and '12wake_process' in s['name']] assert len(matches) == 1 function = matches[0] base = next(s['address'] for s in symbols if s['name'] == '_image_start') start = function['address'] sysroot = subprocess.check_output(['rustc', '--print', 'sysroot'], text=True).strip() host = next(line[6:] for line in subprocess.check_output(['rustc', '-vV'], text=True).splitlines() if line.startswith('host: ')) objdump = sysroot + '/lib/rustlib/' + host + '/bin/llvm-objdump' assembly = subprocess.check_output([objdump, '-d', '--start-address=' + str(start), '--stop-address=' + str(start + function['size']), pre['artifacts']['elf']['path']], text=True) (out / 'wake.txt').write_text(assembly) instructions = {} for line in assembly.splitlines(): match = re.match(r'^\s*([0-9a-f]+):\s+([0-9a-f]{8})\s+(\S+)(.*)$', line) if match: pc, raw = int(match[1], 16), int(match[2], 16) assert raw == int.from_bytes(image[pc-base:pc-base+4], 'little') instructions[pc] = (match[3], re.sub(r'<[^>]*>', '', match[4].split('//', 1)[0]).strip()) assert len(instructions) * 4 == function['size'] # Offsets are pinned to the exact compiled function and checked below. # The accepted PID path stops before any record/backend ownership work. entry, valid_exit, refusal_exit = start + 0x30, start + 0x9c, start + 0x140 selected = set(range(entry, valid_exit, 4)) | set(range(start+0x118, refusal_exit, 4)) | {start+0x33c, start+0x340} expected = { 0x30: ('ldr', 'x26, [sp, #0x6a8]'), 0x34: ('ldrb', 'w27, [sp, #0x6b0]'), 0x4c: ('ldr', 'x9, [x20, #0x6028]'), 0x58: ('ldr', 'x8, [x22, #0x8]'), 0x64: ('ldr', 'x9, [x22, #0x10]'), 0x70: ('mov', 'w10, #0x6d8'), 0x78: ('ldr', 'x8, [x24, #0x6c0]'), 0x84: ('ldrb', 'w8, [x24, #0x6d0]'), 0x33c: ('mov', 'w25, #0x6'), } for offset, wanted in expected.items(): assert instructions[start+offset] == wanted, (hex(offset), instructions[start+offset]) gate = instructions[entry-4] assert gate[0] == 'bl' gate_target = int(gate[1], 0) assert any(s['address'] == gate_target and '13process_table' in s['name'] and '4gate' in s['name'] for s in symbols) source_path = Path(preflight_path.parent, 'sources/kernel/src/runtime/process_table.rs') source = source_path.read_text() assert 'Err(error)=>return Err(ProcessWakeFailure::Before(error,token))' in source assert 'if e.generation != id.generation || id.generation == 0' in source assert 'if id.table != self.identity' in source assert 'Phase::Published | Phase::Zombie' in source table, pid, wake, result, stack = 0x60000000, 0x60010000, 0x60020000, 0x60030000, 0x70000000 cases, visited = [], set() max64 = (1 << 64)-1 def execute(label, slot=0, generation=2, current=2, phase=3, table_identity=0x2100, pid_identity=0x2100, gate_error=None, salt=0): owner_bytes = bytearray((i*37+salt*29+11) & 255 for i in range(0x6200)) owner_bytes[0x6028:0x6030] = pack([table_identity]) for index in range(4): offset = index*0x6d8 owner_bytes[offset+0x6c0:offset+0x6c8] = pack([current if index==slot else 0x8000+index]) owner_bytes[offset+0x6d0] = phase if index==slot else 3 original_pid = pack([pid_identity, slot, generation]) # Four distinct, nonzero 64-bit fields expose partial/misordered copies. original_wake = pack([0x1020304050607080 ^ salt, 0x0807060504030201 ^ salt, max64-salt, 0x8877665544332211 ^ salt]) original_result = bytes((i*11+23) & 255 for i in range(64)) original_stack = bytearray((i*17+7) & 255 for i in range(0x1000)) original_stack[0x6a8:0x6b0] = pack([2 if gate_error is not None else 1]) original_stack[0x6b0] = gate_error if gate_error is not None else 0 m = module.AdmissionMachine(instructions, [(table, owner_bytes), (pid, original_pid), (wake, original_wake), (result, original_result), (stack, original_stack)], {}) for reg, address in [('x19', result), ('x20', table), ('x21', wake), ('x22', pid)]: m.assign(reg, address) m.sp = stack stopped = m.run(entry, {valid_exit, refusal_exit}, maximum=64) error = gate_error if error is None: if pid_identity != table_identity: error = 2 # Error::Foreign elif slot >= 4 or generation == 0 or generation != current or phase not in (3, 4): error = 3 # Error::Stale elif phase != 3: error = 6 # Error::State: a zombie is not wakeable. expected_result = bytearray(original_result) if error is None: assert stopped == valid_exit and not m.writes, label else: assert stopped == refusal_exit, (label, hex(stopped)) expected_result[:8] = pack([2]) # ProcessWakeFailure::Before expected_result[8:40] = original_wake expected_result[40] = error for address, original in [(table, owner_bytes), (pid, original_pid), (wake, original_wake), (stack, original_stack), (result, expected_result)]: region, at = m.locate(address, len(original)) assert region[at:at+len(original)] == original, (label, 'changed protected bytes', hex(address)) for address, size in m.writes: assert result <= address and address+size <= result+41, (label, 'unexpected write') assert set(m.trace) <= selected, (label, 'left selected path') visited.update(m.trace) cases.append(dict(label=label, slot=slot, generation=generation, currentGeneration=current, phase=phase, tableIdentity=table_identity, pidIdentity=pid_identity, gateError=gate_error, expectedError=error, instructions=len(m.trace))) for slot in range(4): for current in (1, 2, 0xffffffff, 0x100000000, max64): execute('matching published PID reaches record path', slot=slot, current=current, generation=current, salt=slot+current%251) for stale in sorted({0, (current-1) & max64, (current+1) & max64, current ^ (1<<63)} - {current}): execute('stale generation preserves owners and returns exact token', slot=slot, current=current, generation=stale, salt=slot+stale%251) for phase in (0, 1, 2, 4, 5, 255): execute('phase rejection', slot=slot, phase=phase, salt=phase+slot) execute('zero generation even when entry also zero', slot=slot, generation=0, current=0) for identity in (0, 1, max64): execute('foreign table wins before slot lookup', slot=slot, pid_identity=identity, salt=identity%251) execute('high table identity accepted', slot=slot, table_identity=max64, pid_identity=max64) for slot in (4, 5, 0xffffffff, max64): execute('out of range slot refused before owner access', slot=slot) execute('foreign table takes precedence over invalid slot', slot=slot, pid_identity=1) for error in range(14): execute('context gate error returned without PID/owner mutation', gate_error=error, slot=max64, pid_identity=1) assert visited == selected, ('selected path coverage', sorted(selected-visited)) (out/'cases.json').write_text(json.dumps(cases, indent=2)+'\n') report = dict(status='NATIVE_WAKE_EARLY_REFUSAL_COMPONENT_PASSED', guestAccepted=False, productAccepted=False, scope='Actual compiled ProcessTable::wake_process path after its execution gate and before record/backend work. Explicit host fixtures cover full table identity, all four slots, generation boundaries, zero/foreign/stale identities and entry phases. Rejection preserves every byte of the modeled owner region, PID, input token and stack; Before returns all 32 original token bytes and the exact error. Matching published IDs reach the next record path without writes. Context-gate results are boundary inputs; neither that callee nor a successful backend wake nor new guest execution is claimed.', preflight=artifact(preflight_path), image=pre['artifacts']['image'], function=function, boundary=dict(entry=entry, contextGateTarget=gate_target, validExit=valid_exit, refusalExit=refusal_exit, modeledOwnerRegionBytes=0x6200, originalTokenBytes=32), coverage=dict(selectedInstructions=len(selected), visitedInstructions=len(visited), entireFunctionInstructions=function['size']//4), cases=len(cases), casesArtifact=artifact(out/'cases.json'), disassembly=artifact(out/'wake.txt'), source=artifact(source_path), harness=artifact(out/'harness.py.txt'), interpreter=artifact(out/'machine.py.txt')) (out/'report.json').write_text(json.dumps(report, indent=2)+'\n') print(json.dumps(dict(status=report['status'], report=str(out/'report.json'), cases=len(cases), coverage=report['coverage']))) except BaseException: import traceback detail = traceback.format_exc() (out/'report.json').write_text(json.dumps(dict(status='NATIVE_WAKE_EARLY_REFUSAL_COMPONENT_FAILED', guestAccepted=False, productAccepted=False, error=detail), indent=2)+'\n') print(str(out/'report.json'), file=sys.stderr) print(detail, file=sys.stderr) sys.exit(1)