import assert from 'node:assert/strict'; import {readFileSync} from 'node:fs'; import {gunzipSync} from 'node:zlib'; import {inspectTcpHandshakeEvidence,tcpHandshakePhysicalPlan} from './tcp-handshake-proof.mjs'; import {handshakeLivePages} from './tcp-handshake-capture.mjs'; const recorded=JSON.parse(gunzipSync(readFileSync(new URL('../../kernel/tests/os/fixtures/tcp-handshake-physical-v1.bin',import.meta.url)))); assert.equal(recorded.schema,'turkos-tcp-handshake-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<=14;stage++){ const data=Object.fromEntries(Object.entries(recorded.snapshots[stage-1]).map(([k,v])=>[k,Buffer.from(v,'base64')])); if(stage===11){const before=handshakeLivePages(Buffer.from(recorded.snapshots[9].authority,'base64'),compiled.authorityLayout);owned=handshakeLivePages(data.authority,compiled.authorityLayout).filter(p=>!before.includes(p));} const plan=tcpHandshakePhysicalPlan(data,symbols,owned); const capture={stage,ownedPages:[...owned],captures:[],dumps:{},gateAddress:symbols.OS_K1885_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:18850+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:18850+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(inspectTcpHandshakeEvidence(fixture()).el0Receipts,1); 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 nw=(f,s,i,v)=>f.files.get(`stage-${s}/netEvidence.bin`).writeBigUInt64LE(BigInt(v),i*8); const controls=[ ['wrong milestone',f=>w(f,1,0,1880)],['wrong network milestone',f=>nw(f,1,0,1880)],['wrong boot',f=>f.bootId='0000000000000000'],['baseline free',f=>w(f,1,3,1)],['return free',f=>w(f,14,5,1)],['network free',f=>nw(f,10,5,1)],['receipt absent',f=>w(f,12,32,0)],['exit absent',f=>w(f,13,34,0)],['reap absent',f=>w(f,14,35,0)],['wrong ASID',f=>w(f,12,9,0)],['EL1 result',f=>w(f,11,12,0x3c5)],['owner missing',f=>w(f,11,17,0)], ['SYN not completed',f=>nw(f,4,153,0)],['ACK not completed',f=>nw(f,8,153,1)],['bad ACK not refused',f=>nw(f,6,167,0)],['extra accepted SYNACK',f=>nw(f,7,166,2)],['fourth RX consumed',f=>nw(f,7,43,72)],['NIC not reset',f=>nw(f,9,38,0)],['NIC pages not returned',f=>nw(f,10,39,0)], ['nonce unmasked',f=>flip(f,1,'nonceMask',0)],['TX nonce unmasked',f=>flip(f,1,'txNonceMask',0)],['session nonce unmasked',f=>flip(f,1,'sessionNonce',0)],['changed layout',f=>flip(f,7,'sessionLayout',0)],['changed allocator layout',f=>flip(f,1,'authorityLayout',0)],['changed frame layout',f=>flip(f,12,'frameLayout',0)], ['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)],['missing process page',f=>f.captures[10].ownedPages.pop()], ]; for(const stage of[1,3,4,6,7,8,14])for(let field=0;field<17;field++)controls.push(['session '+stage+' word '+field,f=>flip(f,stage,'session',field*8)]); for(const stage of[3,7,8])for(const offset of[0,12,26,30,34,36,38,42,46,47,48,50,53,54,59,60,63,64,114,127,255])controls.push(['control output '+stage+' byte '+offset,f=>flip(f,stage,'sink',offset)]); for(const stage of[5,6,7,9])for(const offset of[0,10,12,38,42,46,48,50,54,58,62,65,66,71,72,2048])controls.push(['RX data '+stage+' byte '+offset,f=>flip(f,stage,'frame3',offset)]); for(const stage of[3,4,7,8])for(const offset of[0,10,12,38,42,46,48,50,54,58,62,65,66,71,72,2048])controls.push(['TX data '+stage+' byte '+offset,f=>flip(f,stage,stage<7?'txFrame3':'txFrame4',offset)]); for(const stage of[2,4,5,6,7,8,9])for(const name of['frame0','frame1','frame2','txFrame0','txFrame1','txFrame2'])controls.push(['ring '+stage+' '+name,f=>flip(f,stage,name,2)]); for(const stage of[1,6,9,10])for(const name of['owners','txOwners','registry','txRegistry'])controls.push(['ownership '+stage+' '+name,f=>flip(f,stage,name,0)]); for(const stage of[6,7,8,10])for(const name of['results','txResults','verdicts'])controls.push(['completion '+stage+' '+name,f=>flip(f,stage,name,0)]); for(const stage of[11,12,13])for(const offset of[0,8,16,24,32,127,128,4095])controls.push(['user page '+stage+' byte '+offset,f=>userData(f,stage)[offset]^=1]); for(const stage of[12,13])for(const offset of[0,8,16,24,32,56,64,160,248,256,264,272,280,296,824,832,840])controls.push(['SVC '+stage+' offset '+offset,f=>flip(f,stage,'trapFrame',offset)]); for(let i=0;i<7;i++)controls.push(['RX returned page '+i+' dirty',f=>flip(f,10,'frame'+i,0)]);for(let i=0;i<5;i++)controls.push(['TX returned page '+i+' dirty',f=>flip(f,10,'txFrame'+i,0)]);for(let i=0;i<17;i++)controls.push(['process returned page '+i+' dirty',f=>flip(f,14,'owned'+i,0)]); for(const[name,change]of controls){const f=fixture();change(f);assert.throws(()=>inspectTcpHandshakeEvidence(f),undefined,name);} console.log(JSON.stringify({status:'TCP_HANDSHAKE_PROOF_TESTS_PASSED',positive:1,forgedControlsRejected:controls.length}));