"""Bounded checks of native worker stack zeroing and release prerequisites. The zero/write/read loops use actual Image instructions and explicit host memory. CFG call edges assume callees return; MMU/allocator implementations remain separate boundaries. This does not create a guest observation. """ from pathlib import Path import hashlib import importlib.util import json import re import shutil import subprocess import sys import tempfile out = Path(tempfile.mkdtemp(prefix='K2100-native-stack-release-proof-', dir='reports/os-implementation')) machine_path = Path(__file__).with_name('k2100-aarch64-admission-machine.py') for path, name in [(Path(__file__), 'harness.py.txt'), (machine_path, 'machine.py.txt')]: shutil.copyfile(path, out/name) spec = importlib.util.spec_from_file_location('machine', machine_path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) def artifact(path): return dict(path=str(path), sha256=hashlib.sha256(Path(path).read_bytes()).hexdigest()) def checked(a): assert artifact(a['path'])['sha256'] == a['sha256'], a['path'] return Path(a['path']).read_bytes() try: pre_path, closure_path = map(Path, sys.argv[1:]) pre = json.loads(pre_path.read_text()) closure = json.loads(closure_path.read_text()) assert pre['status'] == 'NATIVE_MODELS_COMPILED' assert closure['status'] == 'PROFILE_CLOSURE_PHYSICAL_COMPONENT_PASSED' assert closure['cpuCount'] == 4 assert closure['image'] == pre['artifacts']['image'] image = checked(pre['artifacts']['image']) checked(pre['artifacts']['elf']) symbols = [] for line in checked(pre['artifacts']['nm']).decode().splitlines(): a = line.split() if len(a) == 4: symbols.append(dict(address=int(a[0], 16), size=int(a[1], 16), name=a[3])) base = next(s['address'] for s in symbols if s['name'] == '_image_start') sysroot = subprocess.check_output(['rustc', '--print', 'sysroot'], text=True).strip() host = next(l[6:] for l in subprocess.check_output(['rustc', '-vV'], text=True).splitlines() if l.startswith('host: ')) objdump = sysroot + '/lib/rustlib/' + host + '/bin/llvm-objdump' functions, assembly, instructions = {}, [], {} for name, address in [('release', 0x402617b8), ('reap', 0x40139dec), ('cycle', 0x4023fc5c), ('storeWord', 0x402c5d8c), ('releaseLeases', 0x402bffb0), ('guardResult', 0x402c318c)]: matches = [s for s in symbols if s['address'] == address and s['size']] assert matches and len({s['size'] for s in matches}) == 1 symbol = matches[0] text = subprocess.check_output([objdump, '-d', '--start-address='+str(address), '--stop-address='+str(address+symbol['size']), pre['artifacts']['elf']['path']], text=True) path = out/(name+'.txt') path.write_text(text) assembly.append(artifact(path)) ins = {} for line in text.splitlines(): m = re.match(r'^\s*([0-9a-f]+):\s+([0-9a-f]{8})\s+(\S+)(.*)$', line) if m: pc, raw = int(m[1], 16), int(m[2], 16) assert raw == int.from_bytes(image[pc-base:pc-base+4], 'little') ins[pc] = (m[3], re.sub(r'<[^>]*>', '', m[4].split('//', 1)[0]).strip()) assert len(ins)*4 == symbol['size'] graph = {} for pc, (op, arg) in ins.items(): assert op not in ('br', 'blr') if op == 'ret': nexts = [] elif op == 'b': target = int(arg, 0) nexts = [target] if target in ins else [] elif op.startswith('b.') or op in ('cbz', 'cbnz', 'tbz', 'tbnz'): nexts = [pc+4, int(arg.split(',')[-1].strip(), 0)] else: nexts = [pc+4] assert all(x in ins for x in nexts), (name, hex(pc)) graph[pc] = nexts functions[name] = dict(symbol=symbol, instructions=ins, graph=graph) instructions.update(ins) def wanted(pc, op, arg): assert instructions[pc] == (op, arg), (hex(pc), instructions[pc]) def reaches(name, start, target, removed=None): graph = functions[name]['graph'] seen, todo = set(), [start] while todo: pc = todo.pop() if pc in seen or pc == removed: continue if pc == target: return True seen.add(pc) todo.extend(graph[pc]) return False prerequisites = [] def requires(name, first, target): entry = functions[name]['symbol']['address'] assert reaches(name, entry, target) assert not reaches(name, entry, target, first) prerequisites.append(dict(function=name, prerequisite=first, target=target)) wanted(0x40261850, 'bl', '0x402d74f0') # restore exact owned guard wanted(0x402c318c, 'ldrb', 'w8, [sp, #0x28]') wanted(0x402c3190, 'cmp', 'w8, #0x2') wanted(0x40261858, 'b.ne', '0x40261940') wanted(0x40261860, 'bl', '0x402c5d8c') wanted(0x402c5d8c, 'str', 'xzr, [x23, x8]') wanted(0x402c5d90, 'add', 'x8, x8, #0x8') wanted(0x402c5d94, 'ret', '') wanted(0x4026186c, 'dsb', 'ish') wanted(0x40261878, 'cbnz', 'x9, 0x40261994') wanted(0x40261904, 'bl', '0x402bffb0') wanted(0x402bffb0, 'ldr', 'x0, [x8]') wanted(0x402bffb4, 'b', '0x402d6050') assert any(s['address'] == 0x402d6050 and '19release_many_leases' in s['name'] for s in symbols) wanted(0x40139ee4, 'bl', '0x402617b8') wanted(0x402401e4, 'bl', '0x40139dec') wanted(0x40240278, 'mov', 'w8, #0x7c') wanted(0x4024027c, 'stlr', 'x8, [x24]') for first in [0x402617d0, 0x4026181c, 0x4026182c, 0x40261850, 0x40261858, 0x4026185c, 0x40261860, 0x4026186c, 0x40261870, 0x40261878, 0x40261884]: requires('release', first, 0x40261904) for first in [0x40139e14, 0x40139e44, 0x40139e54, 0x40139e64, 0x40139e70, 0x40139ea8]: requires('reap', first, 0x40139ee4) for first in [0x402401c0, 0x402401e4, 0x40240208, 0x4024022c]: requires('cycle', first, 0x4024027c) for failure in [0x40261928, 0x40261940, 0x40261958, 0x40261994]: assert not reaches('release', failure, 0x40261904) physical = json.loads(checked(closure['physical'])) records = checked(physical['checkpoints'][1]['dumps']['OS_K2100_WORKER_RECORDS']) actual_bases = [int.from_bytes(records[(cpu*32+5)*8:(cpu*32+6)*8], 'little') for cpu in (1, 2, 3)] visited, cases = set(), [] for address in actual_bases: for pattern in (0, 1, 0x55, 0xff): original = bytes([0x93])*16 + bytes([pattern])*20480 + bytes([0x6d])*16 machine = module.AdmissionMachine(instructions, [(address-16, original)], {}) machine.assign('x23', address) machine.run(0x4026185c, 0x4026186c, maximum=20000) assert machine.writes == [(address+i, 8) for i in range(0, 20480, 8)] assert machine.read(address-16, 16) == int.from_bytes(original[:16], 'little') assert machine.read(address+20480, 16) == int.from_bytes(original[-16:], 'little') assert machine.read(address, 20480) == 0 assert machine.run(0x40261870, {0x40261888, 0x40261994}, maximum=15000) == 0x40261888 visited.update(machine.trace) cases.append(dict(kind='zero-all-owned-words-and-read-back', address=address, pattern=pattern, writes=len(machine.writes))) for slot in [0, 1, 511, 512, 1023, 1024, 1535, 1536, 2047, 2048, 2558, 2559]: for bit in [0, 31, 63]: memory = bytearray(20480) memory[slot*8:slot*8+8] = (1 << bit).to_bytes(8, 'little') machine = module.AdmissionMachine(instructions, [(address, memory)], {}) machine.assign('x23', address) assert machine.run(0x40261870, {0x40261888, 0x40261994}, maximum=15000) == 0x40261994 assert not machine.writes visited.update(machine.trace) cases.append(dict(kind='nonzero-read-back-refused-before-release', address=address, slot=slot, bit=bit)) expected_visited = set(range(0x4026185c, 0x4026186c, 4)) | set(range(0x40261870, 0x40261888, 4)) | {0x402c5d8c, 0x402c5d90, 0x402c5d94} assert visited == expected_visited sources = [artifact(pre_path.parent/'sources/kernel/src/k2100-smp-workers.rs'), artifact(pre_path.parent/'sources/kernel/src/k2045-percpu-runqueue-backend.rs')] report = dict(status='NATIVE_STACK_RELEASE_COMPONENT_PASSED', guestAccepted=False, productAccepted=False, scope='Actual Image/ELF zeroing loop writes every one of 2560 owned words exactly once, executes DSB ISH, and reads all words before allocator release. Host fixtures bind spans to three actual captured worker stacks. Selected compiled CFG prerequisites bind guard restoration, zero/read-back, reap, empty-table checks and release acknowledgement. Callee MMU/allocator semantics and all eighteen long profiles remain separate obligations; this is not new guest evidence.', preflight=artifact(pre_path), closure=artifact(closure_path), image=pre['artifacts']['image'], elf=pre['artifacts']['elf'], nm=pre['artifacts']['nm'], sources=sources, assembly=assembly, prerequisites=prerequisites, cases=cases, caseCount=len(cases), coverage=dict(selectedInstructions=len(expected_visited), visitedInstructions=len(visited), entireReleaseInstructions=functions['release']['symbol']['size']//4), zeroedBytesPerWorker=20480, fullZeroWordsPerWorker=2560, barrier='DSB ISH', harness=artifact(out/'harness.py.txt'), machine=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), prerequisites=len(prerequisites)))) except Exception as error: (out/'report.json').write_text(json.dumps(dict(status='NATIVE_STACK_RELEASE_COMPONENT_FAILED', guestAccepted=False, productAccepted=False, error=repr(error), harness=artifact(out/'harness.py.txt')), indent=2)+'\n') print(out/'report.json', file=sys.stderr) raise