"""Source-bound host and target compilation for the K2155 drain adapter. Host Device fixtures are explicitly modeled. No QEMU is started and no roadmap or physical-device acceptance is granted by this preparation. """ from pathlib import Path import hashlib, json, os, re, shutil, subprocess, tempfile directory = Path(tempfile.mkdtemp(prefix='K2155-lease-io-preparation-', dir='reports/os-implementation')) source = directory / 'source' source.mkdir() for name in ['src', 'tests']: shutil.copytree(Path('os-shutdown-io') / name, source / name) for name in ['Cargo.toml', 'Cargo.lock']: shutil.copyfile(Path('os-shutdown-io') / name, source / name) shutil.copyfile(__file__, directory / 'harness.py.txt') shutil.copyfile('kernel/SHUTDOWN_IO_LEASES_CONTRACT.md', directory / 'contract.md') artifact = lambda p: dict(path=str(p), sha256=hashlib.sha256(Path(p).read_bytes()).hexdigest()) commands = [] def run(name, argv, env=None, expected=0): result = subprocess.run(argv, env=env, capture_output=True, text=True, timeout=120) log = directory / (name + '.log') log.write_text(result.stdout + result.stderr) commands.append(dict(name=name, argv=argv, exitCode=result.returncode, log=artifact(log))) if expected is not None: assert result.returncode == expected, str(log) return result mutants = [ ('wrong-owner', 'if caller != self.resource.owner {', 'if false {', 1), ('repeat-release', 'if self.attempted {', 'if false {', 1), ('stale-identity', 'if value.resource != self.resource {', 'if false {', 1), ('ignore-deadline', 'now >= deadline', 'false', 1), ('ignore-submission-gate', 'if !value.submissions_stopped {', 'if false {', 1), ('ignore-irq-mask', 'if !value.irq_masked || value.irq_active != 0 {', 'if false {', 1), ('ignore-live-workers', 'if value.live_threads != 0 {', 'if false {', 1), ('ignore-live-dma', 'if value.dma_inflight != 0 {', 'if false {', 1), ('ignore-reset-status', 'value.device_status == 0 && value.queues_ready == 0', 'value.queues_ready == 0', 1), ('ignore-ready-queues', 'value.device_status == 0 && value.queues_ready == 0', 'value.device_status == 0', 1), ('skip-stop-command', 'Phase::StopSubmissions => self.backend.stop_submissions(deadline),', 'Phase::StopSubmissions => Ok(()),', 1), ('skip-reset-command', 'Phase::Reset => self.backend.reset(deadline),', 'Phase::Reset => Ok(()),', 1), ('skip-release-command', 'Phase::Release => self.backend.release(),', 'Phase::Release => Ok(()),', 1), ('skip-held-accounting', 'if value.held_pages != self.resource.pages || value.free_pages != initial.free_pages {', 'if false {', 1), ('skip-final-accounting', 'if final_state.held_pages != 0 || final_state.free_pages != free_after {', 'if false {', 1), ('fabricate-empty-retention', 'Outcome::Unverified => None,', 'Outcome::Unverified => Some(0),', 1), ('stale-snapshot-current', 'self.current = false;', 'self.current = true;', 3), ] results = [] try: env = dict(os.environ, CARGO_TARGET_DIR=str((directory / 'cargo-target').resolve())) full = run('cargo-tests', ['cargo', 'test', '--manifest-path', str(source / 'Cargo.toml'), '--locked', '--offline'], env=env) counts = [int(n) for n in re.findall(r'test result: ok\. (\d+) passed;', full.stdout)] assert sum(counts) == 29, counts binary = directory / 'host-tests' run('host-compile', ['rustc', '--edition=2024', '--test', str(source / 'tests/lease_io.rs'), '-o', str(binary)]) host = run('host-tests', [str(binary)]) assert '10 passed; 0 failed' in host.stdout binary.unlink() original = (source / 'src/lease_io.rs').read_text() for name, before, after, count in mutants: assert original.count(before) == count, (name, original.count(before)) case = directory / ('mutant-' + name) (case / 'src').mkdir(parents=True) (case / 'tests').mkdir() (case / 'src/lease_io.rs').write_text(original.replace(before, after)) shutil.copyfile(source / 'tests/lease_io.rs', case / 'tests/lease_io.rs') binary = case / 'tests-bin' run(name + '-compile', ['rustc', '--edition=2024', '--test', str(case / 'tests/lease_io.rs'), '-o', str(binary)]) bad = run(name + '-tests', [str(binary)], expected=None) assert bad.returncode != 0 and 'test result: FAILED' in bad.stdout, name results.append(dict(name=name, rejected=True, source=artifact(case / 'src/lease_io.rs'), log=artifact(directory / (name + '-tests.log')))) binary.unlink() wrapper = directory / 'native.rs' wrapper.write_text('#![no_std]\n#[path="source/src/lease_io.rs"] pub mod lease_io;\n') run('aarch64', ['rustc', '--edition=2024', '--crate-type=lib', '--target=aarch64-unknown-none', str(wrapper), '-o', str(directory / 'lease_io.rlib')]) report = dict(status='DRIVER_DRAIN_ADAPTER_COMPONENT_PASSED', guestAccepted=False, productAccepted=False, physicalDrainVerified=False, scope='Driver-backed command/readback ordering exercised with explicit host fixtures. Native IRQ, DMA, thread and allocator integration plus all guest profiles remain required.', hostTests=10, fullCrateTests=29, sourceMutantsRejected=len(results), deviceFailureSchedules=26, identityReadbackCases=35, target='aarch64-unknown-none', commands=commands, mutants=results, sources=[artifact(p) for p in sorted(source.rglob('*')) if p.is_file()], contract=artifact(directory / 'contract.md'), harness=artifact(directory / 'harness.py.txt'), native=artifact(directory / 'lease_io.rlib')) except BaseException: import traceback report = dict(status='DRIVER_DRAIN_ADAPTER_COMPONENT_FAILED', guestAccepted=False, productAccepted=False, physicalDrainVerified=False, error=traceback.format_exc(), commands=commands, mutants=results) raise finally: shutil.rmtree(directory / 'cargo-target', ignore_errors=True) (directory / 'report.json').write_text(json.dumps(report, indent=2) + '\n') print(json.dumps(dict(status=report['status'], report=str(directory / 'report.json'), hostTests=report.get('hostTests'), mutants=len(results))), flush=True)