import assert from 'node:assert/strict'; import {readFileSync} from 'node:fs'; import {gunzipSync} from 'node:zlib'; import {inspectUdpDatagramsEvidence,udpDatagramsPhysicalPlan} from './udp-datagrams-proof.mjs'; import {datagramsLivePages} from './udp-datagrams-capture.mjs'; const recorded=JSON.parse(gunzipSync(readFileSync(new URL('../../kernel/tests/os/fixtures/udp-datagrams-physical-v1.bin',import.meta.url)))); assert.equal(recorded.schema,'turkos-udp-datagrams-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<=13;stage++){ const data=Object.fromEntries(Object.entries(recorded.snapshots[stage-1]).map(([k,v])=>[k,Buffer.from(v,'base64')])); if(stage===5){const before=datagramsLivePages(Buffer.from(recorded.snapshots[3].authority,'base64'),compiled.authorityLayout);owned=datagramsLivePages(data.authority,compiled.authorityLayout).filter(p=>!before.includes(p));} const plan=udpDatagramsPhysicalPlan(data,symbols,owned); const capture={stage,ownedPages:[...owned],captures:[],dumps:{},gateAddress:symbols.OS_K1870_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:18700+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:18700+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(inspectUdpDatagramsEvidence(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,5,3,1)],['return free',f=>w(f,12,5,1)],['invalid configs missing',f=>w(f,5,30,2)],['missing receipt',f=>w(f,10,32,3)],['exit missing',f=>w(f,11,34,0)],['reap missing',f=>w(f,12,35,0)], ['invalid packets delivered',f=>w(f,6,36,1)],['empty delivery lost',f=>w(f,7,37,0)],['payload length changed',f=>w(f,8,38,10)],['EAGAIN missing',f=>w(f,9,39,0)],['close missing',f=>w(f,10,40,0)], ['wrong ASID',f=>w(f,5,9,0)],['entry changed',f=>w(f,5,10,0x80000004)],['EL1 return',f=>w(f,5,12,0x3c5)],['kernel stack shrunk',f=>w(f,6,14,0)],['process generation missing',f=>w(f,5,17,0)],['stale trap TTBR',f=>flip(f,10,'trapTtbr',0)],['missing active owner',f=>flip(f,6,'active',0)],['kernel frame changed',f=>flip(f,7,'kernelFrame',0)], ['mutable frame layout',f=>flip(f,6,'frameLayout',0)],['mutable allocator layout',f=>flip(f,1,'authorityLayout',0)],['allocator changed during request',f=>flip(f,7,'authority',0)],['return free map changed',f=>flip(f,12,'authority',5032)],['root changed',f=>flip(f,7,'kernelRoot',8)],['kernel mapping changed',f=>flip(f,7,'kernelL2',0)], ['empty payload length changed',f=>userData(f,7)[0]^=1],['empty peer changed',f=>userData(f,7)[8]^=1],['payload port changed',f=>userData(f,8)[48]^=1],['EAGAIN changed',f=>userData(f,9)[64]^=1],['close count changed',f=>userData(f,10)[104]^=1],['early user result',f=>userData(f,5)[0]=0],['bad packet touched output',f=>userData(f,6)[128]=0],['empty packet touched output',f=>userData(f,7)[128]=0],['payload nonce changed',f=>userData(f,8)[128]^=1],['payload suffix changed',f=>userData(f,8)[138]^=1],['payload overrun',f=>userData(f,8)[139]=0],['user data tail changed',f=>userData(f,10)[256]=1], ['SVC result changed',f=>flip(f,10,'trapFrame',0)],['SVC opcode changed',f=>flip(f,7,'trapFrame',64)],['SVC version changed',f=>flip(f,7,'trapFrame',56)],['SVC ordinal changed',f=>flip(f,8,'trapFrame',32)],['user data pointer changed',f=>flip(f,7,'trapFrame',160)],['receive address changed',f=>flip(f,6,'trapFrame',0)],['receive capacity changed',f=>flip(f,6,'trapFrame',8)],['boot word changed',f=>flip(f,8,'trapFrame',40)], ['missing physical page',f=>f.captures[4].ownedPages.pop()],['foreign QMP address',f=>f.qmp.find(r=>r.message.execute==='pmemsave').message.arguments.val+=4096],['missing gate reply',f=>f.captures[5].rsp.splice(3,1)], ['binding layout changed',f=>flip(f,5,'bindingLayout',0)],['unmasked binding issuer',f=>flip(f,5,'bindNonceMask',0)],['unmasked RX issuer',f=>flip(f,1,'nonceMask',0)],['delivery layout changed',f=>flip(f,5,'deliveryLayout',0)],['delivery pointer changed',f=>flip(f,5,'deliveryPtr',0)],['local MAC changed',f=>flip(f,3,'localMac',0)],['closed binding live',f=>flip(f,10,'bindings',0)], ['length refusal changed',f=>flip(f,6,'verdicts',0)],['checksum refusal changed',f=>flip(f,6,'verdicts',8)],['cursor skips empty',f=>flip(f,6,'delivery',40)],['cursor duplicates payload',f=>flip(f,8,'delivery',40)],['cursor wrong address',f=>flip(f,5,'delivery',0)],['RX final free map changed',f=>flip(f,13,'authority',5032)], ]; for(let field=0;field<6;field++)controls.push(['opaque handle field '+field,f=>flip(f,7,'handle',field*8)]); for(let slot=0;slot<2;slot++)for(let field=0;field<7;field++)controls.push(['binding slot '+slot+' field '+field,f=>flip(f,7,'bindings',slot*56+field*8)]); 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,8,'trapFrame',at);}]); for(let i=0;i<17;i++)controls.push(['reaped process page '+i+' dirty',f=>flip(f,12,'owned'+i,0)]); for(let i=0;i<7;i++)controls.push(['returned RX page '+i+' dirty',f=>flip(f,13,'frame'+i,0)],['RX retained bytes '+i+' changed',f=>flip(f,12,'frame'+i,0)]); for(let i=0;i<4;i++)for(const offset of[12+34+4,12+34+6,12+42])controls.push(['physical packet '+i+' byte '+offset,f=>flip(f,3,'frame'+(3+i),offset)]); for(let i=0;i<7;i++)for(const field of[4,5,6,7,8])controls.push(['raw RX ownership '+i+' field '+field,f=>{const e=f.files.get('stage-4/rxEvidence.bin'),l=Array.from({length:12},(_,k)=>Number(e.readBigUInt64LE((45+k)*8)));flip(f,4,'registry',l[2]+i*l[3]+l[field]);}]); for(const[name,change]of controls){const f=fixture();change(f);assert.throws(()=>inspectUdpDatagramsEvidence(f),undefined,name);} console.log(JSON.stringify({status:'UDP_DATAGRAMS_PROOF_TESTS_PASSED',positive:1,forgedControlsRejected:controls.length}));