import assert from 'node:assert/strict'; import {readFileSync} from 'node:fs'; import {gunzipSync} from 'node:zlib'; import {inspectIpv4RoutingEvidence,ipv4RoutingPhysicalPlan} from './ipv4-routing-proof.mjs'; import {routingLivePages} from './ipv4-routing-capture.mjs'; const recorded=JSON.parse(gunzipSync(readFileSync(new URL('../../kernel/tests/os/fixtures/ipv4-routing-physical-v1.bin',import.meta.url)))); assert.equal(recorded.schema,'turkos-ipv4-routing-physical-fixture/v1'); function fixture(){ const symbols=structuredClone(recorded.symbols),captures=[],qmp=[],timeline=[],wholeRsp=[],files=new Map();let requestId=0,rspId=0,owned=[];const compiled=Object.fromEntries(Object.entries(recorded.compiled).map(([k,b])=>[k,Buffer.from(b,'base64')])); for(let stage=1;stage<=8;stage++){ const data=Object.fromEntries(Object.entries(recorded.snapshots[stage-1]).map(([k,v])=>[k,Buffer.from(v,'base64')])); if(stage===2){const before=routingLivePages(Buffer.from(recorded.snapshots[0].authority,'base64'),compiled.authorityLayout);owned=routingLivePages(data.authority,compiled.authorityLayout).filter(p=>!before.includes(p));} const plan=ipv4RoutingPhysicalPlan(data,symbols,owned); const capture={stage,ownedPages:[...owned],captures:[],dumps:{},gateAddress:symbols.OS_K1850_CAPTURE_GATE.address,rsp:[]}; const add=(execute,args,result={})=>{const id=++requestId;qmp.push({direction:'tx',message:{execute,...(args?{arguments:args}:{}),id}},{direction:'rx',message:{return:result,id}}); timeline.push({stage:18500+stage,kind:'qmp',id,execute});return id;}; add('stop');add('query-status',undefined,{status:'paused'}); for(const [i,[name,bytes]] of Object.entries(data).entries()){ const {symbol,val}=plan[i];assert.equal(plan[i].name,name); const path=`stage-${stage}/${name}.bin`,filename='/tmp/'+path,size=bytes.length; files.set(path,bytes);capture.dumps[name]={path};capture.captures.push({name,val,size,filename,requestId:add('pmemsave',{val,size,filename}),...(symbol?{symbol}:{})}); } const gate=capture.gateAddress.toString(16); for(const [tx,rx] of [[`m${gate},1`,'00'],[`M${gate},1:01`,'OK'],[`m${gate},1`,'01']]){ const id='K1080-BATCH:'+ ++rspId; for(const [direction,payload] of [['tx',tx],['rx',rx]]){const checksum=Buffer.from(payload).reduce((s,v)=>(s+v)&255,0).toString(16).padStart(2,'0'); const row={direction,id,payload,packet:`$${payload}#${checksum}`};capture.rsp.push(row);wholeRsp.push(row);timeline.push({stage:18500+stage,kind:'rsp',id,direction});} } add('cont');add('query-status',undefined,{status:'running'});captures.push(capture); } return {compiled,captures,memoryMiB:256,symbols,qmp,timeline,wholeRsp,files,bootId:recorded.bootId,readArtifact:i=>files.get(i.path)}; } assert.equal(inspectIpv4RoutingEvidence(fixture()).el0Receipts,4); const w=(f,s,i,v)=>f.files.get(`stage-${s}/evidence.bin`).writeBigUInt64LE(BigInt(v),i*8); const flip=(f,s,name,index)=>{f.files.get(`stage-${s}/${name}.bin`)[index]^=1;}; const page=(f,stage,address)=>{const i=f.captures[stage-1].ownedPages.indexOf(Number(address));assert.ok(i>=0);return f.files.get(`stage-${stage}/owned${i}.bin`);}; const userData=(f,stage)=>{const e=f.files.get(`stage-${stage}/evidence.bin`),mask=0x0000fffffffff000n,root=page(f,stage,e.readBigUInt64LE(64)),l2=page(f,stage,root.readBigUInt64LE(16)&mask),l3=page(f,stage,l2.readBigUInt64LE(0)&mask);return page(f,stage,l3.readBigUInt64LE(8)&mask);}; const controls=[ ['wrong milestone',f=>w(f,1,0,1800)],['wrong boot',f=>f.bootId='0000000000000000'],['baseline free',f=>w(f,2,3,1)],['return free',f=>w(f,8,5,1)],['invalid routes not rejected',f=>w(f,1,30,2)],['missing receipt',f=>w(f,6,32,3)],['exit not observed',f=>w(f,7,34,0)],['reap not observed',f=>w(f,8,35,0)], ['default gateway changed',f=>flip(f,3,'routes',32)],['specific prefix changed',f=>flip(f,5,'routes',64)],['connected mask changed',f=>flip(f,3,'routes',88)],['default retained on no route',f=>flip(f,6,'routes',0)], ['wrong ASID',f=>w(f,3,9,0)],['entry changed',f=>w(f,2,10,0x80000004)],['EL1 return',f=>w(f,2,12,0x3c5)],['kernel stack shrunk',f=>w(f,3,14,0)],['process generation missing',f=>w(f,2,17,0)],['stale trap ttbr',f=>flip(f,6,'trapTtbr',0)],['missing active owner',f=>flip(f,3,'active',0)],['kernel frame changed',f=>flip(f,4,'kernelFrame',0)], ['mutable frame layout',f=>flip(f,3,'frameLayout',0)],['mutable allocator layout',f=>flip(f,1,'authorityLayout',0)],['allocator changed during request',f=>flip(f,4,'authority',0)],['return free map changed',f=>flip(f,8,'authority',5032)],['root changed',f=>flip(f,4,'kernelRoot',8)],['kernel mapping changed',f=>flip(f,4,'kernelL2',0)], ['user direct next hop changed',f=>userData(f,3)[8]^=1],['user default prefix changed',f=>userData(f,4)[48]^=1],['user specific gateway changed',f=>userData(f,5)[72]^=1],['user no-route error lost',f=>userData(f,6)[96]^=1],['user early result',f=>userData(f,2)[0]=0],['user data tail changed',f=>userData(f,6)[128]=1], ['SVC result changed',f=>flip(f,6,'trapFrame',0)],['SVC opcode changed',f=>flip(f,4,'trapFrame',64)],['SVC version changed',f=>flip(f,4,'trapFrame',56)],['SVC ordinal changed',f=>flip(f,5,'trapFrame',32)],['user data pointer changed',f=>flip(f,4,'trapFrame',160)], ['missing physical page',f=>f.captures[2].ownedPages.pop()],['foreign QMP address',f=>f.qmp.find(r=>r.message.execute==='pmemsave').message.arguments.val+=4096],['missing gate reply',f=>f.captures[2].rsp.splice(3,1)], ]; for(let n=2;n<15;n++)if(n!==7&&n!==9&&n!==10&&n!==11)controls.push(['trap field '+n,f=>{const at=Number(f.compiled.frameLayout.readBigUInt64LE(n*8));flip(f,4,'trapFrame',at);}]); for(let i=0;i<14;i++)controls.push(['returned page '+i+' dirty',f=>flip(f,8,'owned'+i,0)]); for(const[name,change]of controls){const f=fixture();change(f);assert.throws(()=>inspectIpv4RoutingEvidence(f),undefined,name);} console.log(JSON.stringify({status:'IPV4_ROUTING_PROOF_TESTS_PASSED',positive:1,forgedControlsRejected:controls.length}));