import assert from 'node:assert/strict'; import {readFileSync} from 'node:fs'; import {inspectUserStacksEvidence} from './user-stacks-proof.mjs'; import {userStacksSymbols,userStacksGlobals} from './user-stacks-capture.mjs'; const file=readFileSync(new URL('../../kernel/tests/os/fixtures/elf-stacks-rust-v1.elf',import.meta.url)); const segmentPages=Array.from({length:3},(_,i)=>{const at=64+i*56,offset=Number(file.readBigUInt64LE(at+8)),size=Number(file.readBigUInt64LE(at+32)); const bytes=Buffer.alloc(4096);file.copy(bytes,0,offset,offset+size);return bytes;}); const svc=segmentPages[0].indexOf(Buffer.from('010000d4','hex'));assert.ok(svc>=64&&svc%4===0); function fixture(){ const symbols=Object.fromEntries(Object.entries(userStacksSymbols).map(([name,size],i)=>[name,{address:0x40200000+i*4096,size}])); const captures=[],qmp=[],timeline=[],wholeRsp=[],files=new Map();let requestId=0,rspId=0; const frames=Array.from({length:18},(_,i)=>0x41000000n+BigInt(i)*4096n),kernel=Buffer.alloc(4096); const kernelRoot=0x40400000n,kernelFrame=0x40804000n,ttbr=frames[0]|(0x7en<<48n);kernel.writeBigUInt64LE(0x40401003n,8); for(let stage=1;stage<=6;stage++){ const capture={stage,captures:[],dumps:{},gateAddress:symbols.OS_K1735_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:17350+stage,kind:'qmp',id,execute});return id;}; add('stop');add('query-status',undefined,{status:'paused'}); const evidence=Buffer.alloc(512),set=(i,v)=>evidence.writeBigUInt64LE(BigInt(v),i*8),done=Math.min(stage-1,4),last=Math.max(0,done-1); for(const [i,v] of [[0,1735],[1,stage],[2,file.length],[3,1000],[4,982],[5,stage===6?1000:0],[6,kernelRoot],[7,ttbr],[8,0x80000040],[9,1], [10,0x80004000],[11,18],[12,2],[13,done],[14,3],[34,11],[35,0x80011000],[36,0x80013000],[37,2],[38,12],[39,0x80015000], [40,0x80017000],[41,2],[42,4],[43,4],[44,stage===6?1:0],[57,done?11+last%2:0],[58,done?1+Math.floor(last/2):0]])set(i,v); frames.forEach((p,i)=>set(16+i,p)); for(let i=0;i<3;i++){const at=64+i*56;set(45+i*4,file.readBigUInt64LE(at+16));set(46+i*4,file.readBigUInt64LE(at+32));set(47+i*4,file.readBigUInt64LE(at+40));set(48+i*4,file.readUInt32LE(at+4));} const pages=frames.map(()=>Buffer.alloc(4096)),traps=Buffer.alloc(3392),contexts=Buffer.alloc(64); for(let step=0;step<4;step++){ const thread=step%2,base=[0x80011000,0x80015000][thread],trap=traps.subarray(step*848,(step+1)*848); trap.writeBigUInt64LE(4n,824);trap.writeBigUInt64LE(0x4b3932304652414dn,832); if(stepbytes.copy(pages[3+i])); for(let thread=0;thread<2;thread++)if(done>thread){ const tid=BigInt(11+thread),phase=done>thread+2?2:1,base=[0x80011000,0x80015000][thread],stack=14+thread*2; [tid,phase,base+4096,64].forEach((v,i)=>pages[5].writeBigUInt64LE(BigInt(v),(1+thread*4+i)*8)); pages[stack].writeBigUInt64LE(0x1735000000000000n|tid,0);pages[stack].writeBigUInt64LE(0x1735ffff00000000n|tid,8); for(let i=0;i<64;i++)pages[stack+1].writeBigUInt64LE((tid<<32n)|BigInt(i),i*8); } } const scalar=v=>{const b=Buffer.alloc(8);b.writeBigUInt64LE(done?v:0n);return b;}; const data={evidence,traps,contexts,trapTtbr:scalar(ttbr),kernelFrame:scalar(kernelFrame),kernelRootGlobal:scalar(kernelRoot),kernelRoot:Buffer.from(kernel),...Object.fromEntries(pages.map((b,i)=>['frame'+i,b]))}; for(const [i,[name,bytes]] of Object.entries(data).entries()){ const symbol=userStacksGlobals[i]?.[1],val=symbol?symbols[symbol].address:name==='kernelRoot'?Number(kernelRoot):Number(frames[Number(name.slice(5))]); 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:17350+stage,kind:'rsp',id,direction});} } add('cont');add('query-status',undefined,{status:'running'});captures.push(capture); } return{captures,memoryMiB:256,symbols,qmp,timeline,wholeRsp,files,file,readArtifact:i=>files.get(i.path)}; } assert.equal(inspectUserStacksEvidence(fixture()).threadCount,2); const w=(f,s,i,v)=>f.files.get(`stage-${s}/evidence.bin`).writeBigUInt64LE(BigInt(v),i*8); const controls=[ ['wrong entry',f=>{f.file=Buffer.from(f.file);f.file.writeBigUInt64LE(0x80000000n,24);} ], ['missing final resume',f=>w(f,5,13,3)], ['duplicate stack backing',f=>w(f,1,32,0x4100e000)], ['lost return',f=>w(f,6,5,999)], ['wrong root ASID',f=>w(f,1,7,0x41000000)], ['kernel modified',f=>f.files.get('stage-2/kernelRoot.bin')[100]=1], ['RX writable',f=>f.files.get('stage-1/frame2.bin')[0]^=0x80], ['stack executable',f=>f.files.get('stage-1/frame2.bin')[17*8+6]^=0x40], ['A lower guard mapped',f=>f.files.get('stage-1/frame2.bin')[16*8]=3], ['A upper guard mapped',f=>f.files.get('stage-1/frame2.bin')[19*8]=3], ['B lower guard mapped',f=>f.files.get('stage-1/frame2.bin')[20*8]=3], ['B upper guard mapped',f=>f.files.get('stage-1/frame2.bin')[23*8]=3], ['initial stack dirty',f=>f.files.get('stage-1/frame14.bin')[2000]=1], ['A stack local lost',f=>f.files.get('stage-2/frame15.bin')[0]=0], ['B stack local overwritten',f=>f.files.get('stage-4/frame17.bin')[9]=0], ['A changed while B ran',f=>f.files.get('stage-3/frame14.bin')[3000]=1], ['B changed while A ran',f=>f.files.get('stage-4/frame16.bin')[3000]=1], ['A changed on B exit',f=>f.files.get('stage-5/frame14.bin')[3000]=1], ['missing completed phase',f=>f.files.get('stage-4/frame5.bin').writeBigUInt64LE(1n,16)], ['wrong stack object pointer',f=>f.files.get('stage-2/frame5.bin').writeBigUInt64LE(0x80016000n,24)], ['negative published mappings',f=>w(f,1,15,1)], ['quota lost',f=>w(f,1,42,8)], ['uncleared second stack',f=>f.files.get('stage-6/frame17.bin')[4000]=1], ['uncleared first stack',f=>f.files.get('stage-6/frame14.bin')[10]=1], ['wrong thread identity',f=>{for(let s=3;s<=6;s++)f.files.get(`stage-${s}/traps.bin`).writeBigUInt64LE(11n,848+8);}], ['wrong saved SP',f=>{for(let s=3;s<=6;s++)f.files.get(`stage-${s}/traps.bin`).writeBigUInt64LE(0x80012000n,848+256);}], ['switch uses another root',f=>{for(let s=3;s<=6;s++)f.files.get(`stage-${s}/contexts.bin`).writeBigUInt64LE(0x41001000n,16);}], ['wrong ESR',f=>{for(let s=3;s<=6;s++)f.files.get(`stage-${s}/traps.bin`).writeBigUInt64LE(0x96000000n,848+280);}], ['missing capture',f=>f.captures.pop()], ['wrong QMP source',f=>f.qmp.find(r=>r.message.execute==='pmemsave').message.arguments.val+=4096], ['missing gate reply',f=>f.captures[1].rsp.splice(3,1)], ['gate writes evidence',f=>f.captures[1].gateAddress=f.symbols.OS_K1735_EVIDENCE.address], ['early resume',f=>{const i=f.timeline.findIndex(r=>r.execute==='cont');[f.timeline[i],f.timeline[i-1]]=[f.timeline[i-1],f.timeline[i]];}], ]; for(const [name,change] of controls){const f=fixture();change(f);assert.throws(()=>inspectUserStacksEvidence(f),undefined,name);} console.log(JSON.stringify({status:'USER_STACKS_PROOF_TESTS_PASSED',positive:1,forgedControlsRejected:controls.length}));