import assert from 'node:assert/strict'; import {copyFileSync, cpSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, writeFileSync} from 'node:fs'; import {spawnSync} from 'node:child_process'; import {dirname, join, relative, resolve} from 'node:path'; import {frozenSnapshot, hashAt} from './boot-contract-report.mjs'; assert.equal(process.argv.length,3,'usage: node exec-replace-local.mjs |--staging'); const root=process.cwd(), staging=process.argv[2]==='--staging'; const predecessorPath=staging?null:resolve(process.argv[2]); if(!staging) { const predecessor=JSON.parse(readFileSync(predecessorPath)); assert.equal(predecessor.status,'K1765_EXEC_PATH_EXEC_DEVELOPMENT_MATRIX_VALIDATED'); assert.equal(predecessor.guestAccepted,true);assert.equal(predecessor.productAccepted,false); assert.equal(predecessor.cases.length,6); } const frozenBefore=frozenSnapshot(root); const directory=mkdtempSync(join(root,'reports/os-implementation/K1770-exec-replace-'+(staging?'staging-local-':'local-'))); const source=join(directory,'source'); const artifact=path=>({path:relative(root,path),sha256:hashAt(root,relative(root,path))}); const copy=(path,target=join(source,path))=>{mkdirSync(dirname(target),{recursive:true});copyFileSync(join(root,path),target);return artifact(target);}; const sources={}; for(const [key,path] of Object.entries({model:'os-exec-replace/src/replace.rs',runtimeModel:'reports/os-implementation/K1770-exec-replace-work-sources/kernel/src/runtime/exec_replace.rs',authority:'os-frame-authority/src/authority.rs',diskFixture:'scripts/os-acceptance/file-map-disk.mjs',newUserWire:'kernel/tests/os/fixtures/elf-exec-replace-new-rust-v1.elf',segments:'os-elf-loader/src/segments.rs',headers:'os-elf-loader/src/headers.rs', fixture:'kernel/tests/os/exec_replace.rs',wire:'kernel/tests/os/fixtures/elf-three-load-v1.bin',userWire:'kernel/tests/os/fixtures/elf-exec-replace-old-rust-v1.elf', contract:'kernel/EXEC_REPLACE_CONTRACT.md',manifest:'os-exec-replace/Cargo.toml', lock:'os-exec-replace/Cargo.lock',noStdRoot:'os-exec-replace/src/lib.rs', capture:'scripts/os-acceptance/exec-replace-capture.mjs',proof:'scripts/os-acceptance/exec-replace-proof.mjs', proofTest:'scripts/os-acceptance/test-exec-replace-proof.mjs',physicalFixture:'kernel/tests/os/fixtures/exec-replace-physical-v1.bin',fixtureBuilder:'scripts/os-acceptance/exec-replace-physical-fixture.mjs',physicalProof:'scripts/os-acceptance/physical-checkpoint-proof.mjs'}))sources[key]=copy(path); sources.harness=artifact(join(root,'scripts/os-acceptance/exec-replace-local.mjs')); const librarySources=[]; const work=join(root,'reports/os-implementation/K1770-exec-replace-work-sources'); for(const component of readdirSync(work).filter(name=>!['bootloader','userspace'].includes(name)&&existsSync(join(work,name,'Cargo.toml')))) { cpSync(join(work,component,'src'),join(source,component,'src'),{recursive:true}); copyFileSync(join(work,component,'Cargo.toml'),join(source,component,'Cargo.toml')); librarySources.push({source:component+'/Cargo.toml',...artifact(join(source,component,'Cargo.toml'))}); function walk(path) {for(const entry of readdirSync(path,{withFileTypes:true})) { const child=join(path,entry.name);if(entry.isDirectory())walk(child); else librarySources.push({source:relative(source,child),...artifact(child)}); }} walk(join(source,component,'src')); } assert.equal(sources.runtimeModel.sha256,artifact(join(source,'kernel/src/runtime/exec_replace.rs')).sha256); assert.equal(sources.segments.sha256,artifact(join(source,'kernel/src/runtime/elf_segments.rs')).sha256); assert.equal(sources.userWire.sha256,artifact(join(source,'kernel/src/k1770-old-user.elf')).sha256); assert.equal(sources.headers.sha256,artifact(join(source,'kernel/src/runtime/elf_loader.rs')).sha256); assert.equal(sources.model.sha256,sources.runtimeModel.sha256); assert.equal(sources.newUserWire.sha256,artifact(join(source,'kernel/src/k1770-new-user.elf')).sha256); assert.equal(sources.authority.sha256,artifact(join(source,'os-frame-authority/src/authority.rs')).sha256); const env={...process.env,CARGO_TARGET_DIR:join(directory,'target')}; delete env.RUSTFLAGS;delete env.CARGO_ENCODED_RUSTFLAGS; const run=(name,args,expected,executable='cargo')=>{ const result=spawnSync(executable,args,{cwd:root,env,encoding:'utf8',timeout:600000,maxBuffer:64*1024*1024}); const log=join(directory,name+'.log');writeFileSync(log,(result.stdout??'')+(result.stderr??'')); assert.equal(result.error,undefined,`${name}: ${artifact(log).path}`); assert.equal(result.signal,null);assert.equal(result.status,expected,`${name}: ${artifact(log).path}`); return {exitCode:result.status,log:artifact(log)}; }; const manifest=join(source,'os-exec-replace/Cargo.toml'); const tests={green:run('host-tests',['test','--manifest-path',manifest,'--locked','--offline','--test','replace'],0), target:run('aarch64-kernel-check',['check','--manifest-path',join(source,'kernel/Cargo.toml'), '--offline','--bin','turkos-kernel','--target','aarch64-unknown-none','--no-default-features','--features','os-runtime-product'],0)}; tests.proof=run('proof-tests',[join(source,'scripts/os-acceptance/test-exec-replace-proof.mjs')],0,process.execReplace); assert.deepEqual(JSON.parse(readFileSync(join(directory,'proof-tests.log'),'utf8')), {status:'EXEC_REPLACE_PROOF_TESTS_PASSED',positive:1,forgedControlsRejected:36}); const green=readFileSync(join(directory,'host-tests.log'),'utf8'); for(const name of ['positive','negative'])assert.match(green,new RegExp('os_exec_replace_'+name+' \\.\\.\\. ok')); assert.match(green,/2 passed; 0 failed/); const mutantRoot=join(directory,'mutant');cpSync(source,mutantRoot,{recursive:true}); const mutantModel=join(mutantRoot,'os-exec-replace/src/replace.rs'); const model=readFileSync(mutantModel,'utf8'); const guard='if current.identity()!=self.old{return Err(self);}'; assert.equal(model.split(guard).length,2); writeFileSync(mutantModel,model.replace(guard,'if false&¤t.identity()!=self.old{return Err(self);}')); env.CARGO_TARGET_DIR=join(directory,'mutant-target'); tests.red=run('mutant-tests',['test','--manifest-path',join(mutantRoot,'os-exec-replace/Cargo.toml'), '--locked','--offline','--test','replace'],101); assert.match(readFileSync(join(directory,'mutant-tests.log'),'utf8'),/os_exec_replace_negative \.\.\. FAILED/); const frozenAfter=frozenSnapshot(root);assert.deepEqual(frozenAfter,frozenBefore); const report={schema:'turkos-k1770-exec-replace-local/v1',status:staging?'K1770_EXEC_REPLACE_STAGING_VALIDATED':'K1766_K1769_LOCAL_VALIDATED', guestAccepted:false,productAccepted:false,predecessor:predecessorPath?artifact(predecessorPath):null,sources,librarySources, mutant:artifact(mutantModel),tests:{...tests,greenCount:2,mutationRejected:true},frozenBefore,frozenAfter, scope:'Atomic image replacement prepares a complete private image before retiring the old root. A real stack quota failure returns six acquired frames zero and resumes the exact old SVC frame with ENOSPC. The old Rust program continues; a later successful preparation retains both seven-frame images until commit, then zero-retires the old one and executes the new Rust program. Eight checkpoints bind real tables, all frames and three native traps. Development gate only.'}; const reportPath=join(directory,'report.json');writeFileSync(reportPath,JSON.stringify(report,null,2)+'\n'); console.log(JSON.stringify({status:report.status,report:relative(root,reportPath),tests:2,mutationRejected:true}));