import assert from 'node:assert/strict'; import {readFileSync} from 'node:fs'; import {gunzipSync} from 'node:zlib'; import {inspectTcpStreamEvidence,tcpStreamPhysicalPlan} from './tcp-stream-proof.mjs'; import {streamLivePages} from './tcp-stream-capture.mjs'; const recorded=JSON.parse(gunzipSync(readFileSync(new URL('../../kernel/tests/os/fixtures/tcp-stream-physical-v1.bin',import.meta.url)))); assert.equal(recorded.schema,'turkos-tcp-stream-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<=27;stage++){ const data=Object.fromEntries(Object.entries(recorded.snapshots[stage-1]).map(([k,v])=>[k,Buffer.from(v,'base64')])); if(stage===24){const before=streamLivePages(Buffer.from(recorded.snapshots[22].authority,'base64'),compiled.authorityLayout);owned=streamLivePages(data.authority,compiled.authorityLayout).filter(p=>!before.includes(p));} const plan=tcpStreamPhysicalPlan(data,symbols,owned); const capture={stage,ownedPages:[...owned],captures:[],dumps:{},gateAddress:symbols.OS_K1890_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:18900+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:18900+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(inspectTcpStreamEvidence(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 sw=(f,s,i,v)=>f.files.get(`stage-${s}/streamNetEvidence.bin`).writeBigUInt64LE(BigInt(v),i*8); const controls=[ ['wrong milestone',f=>w(f,1,0,1885)],['wrong handshake milestone',f=>nw(f,1,0,1885)],['wrong stream milestone',f=>sw(f,11,0,1885)],['wrong boot',f=>f.bootId='0000000000000000'],['baseline free',f=>w(f,1,3,1)],['return free',f=>w(f,27,5,1)],['handshake free',f=>nw(f,10,5,1)],['stream free',f=>sw(f,23,5,1)],['receipt absent',f=>w(f,25,32,0)],['exit absent',f=>w(f,26,34,0)],['reap absent',f=>w(f,27,35,0)],['wrong ASID',f=>w(f,25,9,0)],['EL1 result',f=>w(f,24,12,0x3c5)],['owner missing',f=>w(f,24,17,0)], ['SYN not completed',f=>nw(f,4,153,0)],['handshake 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 handshake RX consumed',f=>nw(f,7,43,72)],['handshake NIC not reset',f=>nw(f,9,38,0)],['handshake NIC pages not returned',f=>nw(f,10,39,0)], ['stream first ACK not completed',f=>sw(f,18,153,0)],['stream final ACK not completed',f=>sw(f,21,153,1)],['outside-window not refused',f=>sw(f,16,167,0)],['extra accepted stream segment',f=>sw(f,19,166,4)],['stream NIC not reset',f=>sw(f,22,38,0)],['stream NIC pages not returned',f=>sw(f,23,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 session layout',f=>flip(f,7,'sessionLayout',0)],['changed allocator layout',f=>flip(f,1,'authorityLayout',0)],['changed frame layout',f=>flip(f,25,'frameLayout',0)],['changed stream layout',f=>flip(f,19,'streamLayout',0)],['changed compiled stream layout',f=>f.compiled.streamLayout[0]^=1], ['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[23].ownedPages.pop()], ]; for(const stage of[1,3,4,6,7,8,10,11,27])for(let field=0;field<17;field++)controls.push(['session '+stage+' word '+field,f=>flip(f,stage,'session',field*8)]); for(const stage of[10,11,14,15,16,19,24,25,27])for(const offset of[0,8,24,64,104,120,136,142,147,647,648,654,659,1159,...Array.from({length:10},(_,i)=>1160+i*8)])controls.push(['raw stream '+stage+' byte '+offset,f=>flip(f,stage,'stream',offset)]); for(const [stage,name]of[[3,'sink'],[8,'sink'],[17,'streamSink'],[21,'streamSink']])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,name,offset)]); for(const [stage,name]of[[5,'frame3'],[6,'frame4'],[7,'frame5'],[13,'streamFrame3'],[15,'streamFrame4'],[16,'streamFrame5'],[19,'streamFrame6']])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=>{if(offset===10)f.files.get(`stage-${stage}/${name}.bin`).writeUInt16LE(2,10);else flip(f,stage,name,offset);}]); for(const [stage,name]of[[4,'txFrame3'],[8,'txFrame4'],[18,'streamTxFrame3'],[21,'streamTxFrame4']])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,name,offset)]); for(const stage of[2,4,5,6,7,8,9,12,13,16,18,19,21,22])for(const suffix of['Frame0','Frame1','Frame2','TxFrame0','TxFrame1','TxFrame2']){const name=stage<=10?suffix[0].toLowerCase()+suffix.slice(1):'stream'+suffix;controls.push(['ring '+stage+' '+name,f=>flip(f,stage,name,2)]);} for(const stage of[1,6,9,10,11,16,22,23])for(const suffix of['Owners','TxOwners','Registry','TxRegistry']){const name=stage<=10?suffix[0].toLowerCase()+suffix.slice(1):'stream'+suffix;controls.push(['ownership '+stage+' '+name,f=>{if(suffix.endsWith('Registry')){const e=f.files.get(`stage-${stage}/${stage<=10?'netEvidence':'streamNetEvidence'}.bin`),at=Number(e.readBigUInt64LE((suffix==='Registry'?46:142)*8));flip(f,stage,name,at);}else flip(f,stage,name,0);}]);} for(const stage of[6,7,8,10,14,15,16,19,21,23])for(const suffix of['Results','TxResults','Verdicts']){const name=stage<=10?suffix[0].toLowerCase()+suffix.slice(1):'stream'+suffix;controls.push(['completion '+stage+' '+name,f=>flip(f,stage,name,0)]);} for(const stage of[24,25,26])for(const offset of[0,6,11,12,31,32,40,48,56,64,71,72,127,128,4095])controls.push(['user page '+stage+' byte '+offset,f=>userData(f,stage)[offset]^=1]); for(const stage of[25,26])for(const offset of[0,8,16,24,32,56,64,160,168,176,184,192,248,256,264,272,280,296,824,832,840])controls.push(['SVC '+stage+' offset '+offset,f=>flip(f,stage,'trapFrame',offset)]); for(const [stage,prefix]of[[10,''],[23,'stream']]){for(let i=0;i<7;i++)controls.push(['RX returned page '+stage+'/'+i+' dirty',f=>flip(f,stage,prefix?prefix+'Frame'+i:'frame'+i,0)]);for(let i=0;i<5;i++)controls.push(['TX returned page '+stage+'/'+i+' dirty',f=>flip(f,stage,prefix?prefix+'TxFrame'+i:'txFrame'+i,0)]);}for(let i=0;i<17;i++)controls.push(['process returned page '+i+' dirty',f=>flip(f,27,'owned'+i,0)]); for(const[name,change]of controls){const f=fixture();change(f);assert.throws(()=>inspectTcpStreamEvidence(f),undefined,name);} console.log(JSON.stringify({status:'TCP_STREAM_PROOF_TESTS_PASSED',positive:1,forgedControlsRejected:controls.length}));