import assert from 'node:assert/strict'; import {readFileSync,writeFileSync,mkdtempSync,copyFileSync}from'node:fs'; import {createHash}from'node:crypto'; import {join}from'node:path'; import{reopenStages,checkReopenStageOrder,readOnlyImageSpans,collectReopenPhysical}from'../../scripts/os-acceptance/features/k2160-capture.mjs'; const[candidatePath]=process.argv.slice(2);assert.equal(process.argv.length,3); const art=path=>({path,sha256:createHash('sha256').update(readFileSync(path)).digest('hex')}); const candidate=JSON.parse(readFileSync(candidatePath));assert.equal(candidate.status,'COMPILED_FEATURE_CANDIDATE'); const elf=readFileSync(candidate.artifacts.elf.path);assert.deepEqual(art(candidate.artifacts.elf.path),candidate.artifacts.elf); const symbols={};for(const line of readFileSync(candidate.artifacts.nm.path,'utf8').split('\n')){const m=line.match(/^([a-f0-9]{16}) (?:[a-f0-9]{16} )?[A-Za-z] (\S+)$/);if(m)symbols[m[2]]={address:parseInt(m[1],16)};} const out=mkdtempSync('reports/os-implementation/K2160-capture-plan-host-'),rejected=[],files=[]; const report={status:'CAPTURE_PLAN_HOST_RUNNING',guestAccepted:false,productAccepted:false,candidate:art(candidatePath),files,rejected}; const reject=(name,f)=>{assert.throws(f,undefined,name);rejected.push(name);}; const asyncReject=async(name,f)=>{await assert.rejects(f,undefined,name);rejected.push(name);}; try{ const imageSpans=readOnlyImageSpans(elf,candidate.imageSpan);assert.equal(imageSpans.length,2);assert.deepEqual(imageSpans.map(s=>s.flags),[5,4]); for(const[name,span]of[ ['infinite-image-bound',{...candidate.imageSpan,end:Infinity}],['negative-image-start',{...candidate.imageSpan,start:-1}], ['reversed-image-range',{start:candidate.imageSpan.end,end:candidate.imageSpan.start}], ['oversized-image-window',{start:candidate.imageSpan.start,end:candidate.imageSpan.start+8*1024*1024+1}], ])reject(name,()=>readOnlyImageSpans(elf,span)); const ph=Number(elf.readBigUInt64LE(32)),stride=elf.readUInt16LE(54); for(const[name,change]of[ ['wrong-machine',b=>b.writeUInt16LE(62,18)],['not-64-bit',b=>{b[4]=1;}],['wrong-endian',b=>{b[5]=2;}],['not-executable',b=>b.writeUInt16LE(3,16)], ['unbounded-program-table',b=>b.writeUInt16LE(33,56)],['wrong-program-stride',b=>b.writeUInt16LE(55,54)], ['program-offset-outside-file',b=>b.writeBigUInt64LE(BigInt(b.length),32)],['physical-virtual-mismatch',b=>b.writeBigUInt64LE(0x40081000n,ph+24)], ['load-before-image',b=>{b.writeBigUInt64LE(0x40000000n,ph+16);b.writeBigUInt64LE(0x40000000n,ph+24);}], ['overlapping-loads',b=>{b.writeBigUInt64LE(0x40080000n,ph+stride+16);b.writeBigUInt64LE(0x40080000n,ph+stride+24);}], ['writable-executable',b=>b.writeUInt32LE(7,ph+4)],['no-executable-proof',b=>b.writeUInt32LE(4,ph+4)], ['file-range-outside-elf',b=>b.writeBigUInt64LE(BigInt(b.length),ph+8)],['memory-shorter-than-file',b=>b.writeBigUInt64LE(1n,ph+40)], ]){const b=Buffer.from(elf);change(b);reject(name,()=>readOnlyImageSpans(b,candidate.imageSpan));} let gates=0,spans=0; const fixtures=[]; function fixture(stage,phase,caseId,plan){ const data={};for(const[name,words]of[['OS_K2160_EVIDENCE',512],['OS_K2160_CPU_STATE',8],['OS_K2160_AUTHORITY',4],['OS_K2160_USER_SPACES',24],['OS_K2160_EL0_REQUESTS',192]])data[name]=Buffer.alloc(words*8); const word=(name,i,v)=>data[name].writeBigUInt64LE(BigInt(v),i*8),base=0xa003e00; for(const[i,v]of[[1,stage],[6,phase],[7,caseId],[16,base],...[0,1,2,3].map(i=>[17+i,0x40500000+i*4096])])word('OS_K2160_EVIDENCE',i,v); word('OS_K2160_CPU_STATE',0,0x40100000);word('OS_K2160_AUTHORITY',0,0x40200000);word('OS_K2160_AUTHORITY',1,4096); word('OS_K2160_AUTHORITY',2,symbols.OS_K1000_AUTHORITY_LAYOUT_EXPECTED.address);word('OS_K2160_AUTHORITY',3,192); const pages=new Map(); if([330,331,332].includes(stage)){ const index=stage-330,root=0x40300000,l2=root+4096,l3=root+8192;word('OS_K2160_CPU_STATE',0,root); for(const[i,v]of[root,root+12288,root+16384,root+20480,0x80000000,0x80001000,0x80002000,0x80003000].entries())word('OS_K2160_USER_SPACES',index*8+i,v); const a=Buffer.alloc(4096),b=Buffer.alloc(4096);a.writeBigUInt64LE(BigInt(l2)|3n,16);b.writeBigUInt64LE(BigInt(l3)|3n,0);pages.set(root,a);pages.set(l2,b); word('OS_K2160_EL0_REQUESTS',[0,4,10][index]*16+15,0x40600000); } const taken=[];return {stage,phase,caseId,data,symbols,plan,taken,take:async(name,address,size,mmio)=>{taken.push({name,address,size,mmio});return pages.get(address)??Buffer.alloc(size);}}; } for(const[phase,caseId]of[[1,0],[2,0],[2,1],[2,2],[2,3],[2,4]]){ const plan={ramStart:0x40000000,ramEnd:0x50000000,allowedMmioBases:new Set([0xa003e00]),imageSpans},stages=reopenStages(phase,caseId); checkReopenStageOrder(stages,phase,caseId); reject('missing-stage-'+phase+'-'+caseId,()=>checkReopenStageOrder(stages.slice(0,-1),phase,caseId)); reject('repeated-stage-'+phase+'-'+caseId,()=>checkReopenStageOrder([...stages,stages.at(-1)],phase,caseId)); for(const stage of stages){ const f=fixture(stage,phase,caseId,plan),r=await collectReopenPhysical(f);assert.equal(r.acceptanceGranted,false);gates++;spans+=r.spans; const names=f.taken.map(x=>x.name);assert.equal(new Set(names).size,names.length); assert.equal(names.some(n=>n.startsWith('live-user-')),[330,331,332].includes(stage)); assert.equal(names.some(n=>n.startsWith('dma-')),stage===2||(stage>=101&&stage<=116)||stage===3); if([4,9,300,301,302].includes(stage))assert.ok(!names.some(n=>n.startsWith('live-user-')||n.startsWith('dma-'))); fixtures.push({phase,caseId,stage,spans:r.spans}); } } const basePlan=()=>({ramStart:0x40000000,ramEnd:0x50000000,allowedMmioBases:new Set([0xa003e00]),imageSpans}); for(const[name,change]of[ ['unknown-stage',f=>{f.stage=8;}],['wrong-phase-in-capture',f=>f.data.OS_K2160_EVIDENCE.writeBigUInt64LE(2n,6*8)], ['authority-outside-ram',f=>f.data.OS_K2160_AUTHORITY.writeBigUInt64LE(0x1000n,0)], ['oversized-authority',f=>f.data.OS_K2160_AUTHORITY.writeBigUInt64LE(4n*1024n*1024n,8)], ['wrong-layout-pointer',f=>f.data.OS_K2160_AUTHORITY.writeBigUInt64LE(0x40100000n,16)], ['unaligned-kernel-root',f=>f.data.OS_K2160_CPU_STATE.writeBigUInt64LE(0x40100001n,0)], ]){const f=fixture(0,1,0,basePlan());change(f);await asyncReject(name,()=>collectReopenPhysical(f));} for(const[name,change]of[ ['unknown-device-mmio',f=>f.data.OS_K2160_EVIDENCE.writeBigUInt64LE(0xa004000n,16*8)], ['duplicate-dma-pages',f=>f.data.OS_K2160_EVIDENCE.writeBigUInt64LE(0x40500000n,18*8)], ['unaligned-dma-page',f=>f.data.OS_K2160_EVIDENCE.writeBigUInt64LE(0x40500001n,17*8)], ['dma-page-outside-ram',f=>f.data.OS_K2160_EVIDENCE.writeBigUInt64LE(0x50000000n,17*8)], ]){const plan=basePlan();plan.kernelRoot=0x40100000n;const f=fixture(2,1,0,plan);change(f);await asyncReject(name,()=>collectReopenPhysical(f));} for(const[name,change]of[ ['wrong-live-user-root',f=>f.data.OS_K2160_CPU_STATE.writeBigUInt64LE(0x40301000n,0)], ['unaligned-live-frame',f=>f.data.OS_K2160_EL0_REQUESTS.writeBigUInt64LE(0x40600001n,15*8)], ['broken-user-page-table',f=>{const original=f.take;f.take=async(n,a,s,m)=>n==='live-user-root'?Buffer.alloc(4096):original(n,a,s,m);}], ]){const plan=basePlan();plan.kernelRoot=0x40100000n;const f=fixture(330,1,0,plan);change(f);await asyncReject(name,()=>collectReopenPhysical(f));} reject('writer-fault-case',()=>reopenStages(1,1));reject('unknown-phase',()=>reopenStages(3,0));reject('unknown-case',()=>reopenStages(2,5)); for(const p of ['scripts/os-acceptance/features/k2160-capture.mjs','tools/reopen_verify/test_capture.mjs']){const dst=join(out,p.split('/').at(-1)+'.txt');copyFileSync(p,dst);files.push(art(dst));} report.status='CAPTURE_PLAN_HOST_PASSED';report.syntheticSequences=6;report.syntheticGates=gates;report.plannedPhysicalReads=spans;report.fixtures=fixtures;report.readOnlyImageSpans=imageSpans; report.scope='Host-only bounded capture planning on six synthetic sequences. Existing candidate ELF read-only ranges and malformed ELF/capture metadata are checked. No guest bytes or native execution are created, and this collector grants no acceptance. Independent physical proof semantics and real runner remain required.'; }catch(error){report.status='CAPTURE_PLAN_HOST_FAILED';report.error=String(error.stack??error);process.exitCode=1;} const path=join(out,'report.json');writeFileSync(path,JSON.stringify(report,null,2)+'\n');console.log(JSON.stringify({status:report.status,report:path}));