import assert from 'node:assert/strict'; import {inspectPhysicalCheckpoint} from './physical-checkpoint-proof.mjs'; import {userStacksSymbols,userStacksGlobals} from './user-stacks-capture.mjs'; const canary=0x4b3932304652414dn; const word=(b,i)=>b.readBigUInt64LE(i*8); const words=b=>{assert.equal(b.length,512);return Array.from({length:64},(_,i)=>word(b,i));}; const page=b=>{assert.equal(b.length,4096);return b;}; const zero=b=>assert.ok(b.every(v=>v===0),'zeroed bytes'); function program(file){ assert.deepEqual([...file.subarray(0,7)],[0x7f,69,76,70,2,1,1]); assert.equal(file.readUInt16LE(16),2);assert.equal(file.readUInt16LE(18),183); assert.equal(file.readBigUInt64LE(24),0x80000040n);assert.equal(file.readBigUInt64LE(32),64n); assert.equal(file.readUInt16LE(54),56);assert.equal(file.readUInt16LE(56),3); const segments=Array.from({length:3},(_,i)=>{ const at=64+i*56;assert.equal(file.readUInt32LE(at),1); const flags=file.readUInt32LE(at+4),offset=file.readBigUInt64LE(at+8),va=file.readBigUInt64LE(at+16), size=file.readBigUInt64LE(at+32),memory=file.readBigUInt64LE(at+40); assert.equal(flags,[5,4,6][i]);assert.equal(va,0x80000000n+BigInt(i)*4096n);assert.equal(file.readBigUInt64LE(at+48),4096n); assert.ok(offset%4096n===0n&&size>0n&&size<=memory&&memory<=4096n&&offset+size<=BigInt(file.length)); const bytes=Buffer.alloc(4096);file.copy(bytes,0,Number(offset),Number(offset+size)); if(i===0)for(let at=0;at<64;at+=4)assert.equal(bytes.readUInt32LE(at),0xd4200000,'BRK prefix'); return{flags,offset,va,size,memory,bytes}; }); assert.equal(segments[2].size,8n);assert.equal(segments[2].memory,72n); assert.equal(word(segments[2].bytes,0),0x5257173589abcdefn);zero(segments[2].bytes.subarray(8));return segments; } export function inspectUserStacksSnapshots(snapshots,memoryMiB,file){ assert.equal(snapshots.length,6);assert.ok([256,512,1024].includes(memoryMiB)); const segments=program(file),ramEnd=0x40000000n+BigInt(memoryMiB)*1024n*1024n; const physical=v=>assert.ok(v%4096n===0n&&v>=0x40000000n&&v+4096n<=ramEnd); const all=snapshots.map(s=>words(s.evidence)),initial=all[0],kernel=page(snapshots[0].kernelRoot); assert.equal(word(kernel,2),0n); const empty=Buffer.alloc(848);empty.writeBigUInt64LE(4n,824);empty.writeBigUInt64LE(canary,832); for(const [index,w] of all.entries()){ const stage=index+1,snap=snapshots[index],completed=Math.min(index,4),last=Math.max(0,completed-1); for(const [i,v] of [[0,1735],[1,stage],[2,file.length],[8,0x80000040],[9,1],[10,0x80004000],[11,18],[12,2],[13,completed],[14,3],[15,0], [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,completed?11+last%2:0],[58,completed?1+Math.floor(last/2):0]])assert.equal(w[i],BigInt(v)); for(let i=59;i<64;i++)assert.equal(w[i],0n); physical(w[6]);for(const p of w.slice(16,34))physical(p);assert.equal(new Set([w[6],...w.slice(16,34)]).size,19); assert.equal(w[7],w[16]|(0x7en<<48n));assert.ok(w[3]>=18n&&w[3]<=BigInt(memoryMiB)*256n);assert.equal(w[4],w[3]-18n);assert.equal(w[5],stage===6?w[3]:0n); for(let i=0;i<64;i++)if(![1,5,13,44,57,58].includes(i))assert.equal(w[i],initial[i]); for(const [i,s] of segments.entries())assert.deepEqual(w.slice(45+i*4,49+i*4),[s.va,s.size,s.memory,BigInt(s.flags)]); assert.deepEqual(page(snap.kernelRoot),kernel);assert.equal(snap.traps.length,3392);assert.equal(snap.contexts.length,64); for(const name of ['trapTtbr','kernelFrame','kernelRootGlobal'])assert.equal(snap[name].length,8); for(let step=0;step<4;step++){ const trap=snap.traps.subarray(step*848,(step+1)*848),ttbr=word(snap.contexts,step*2),saved=word(snap.contexts,step*2+1); if(step>=completed){assert.deepEqual(trap,empty);assert.equal(ttbr,0n);assert.equal(saved,0n);continue;} const thread=step%2,base=[0x80011000n,0x80015000n][thread],top=base+8192n; assert.equal(ttbr,w[7]);assert.ok(saved%16n===0n&&saved>=0x40000000n+848n&&saved+176n<=ramEnd); assert.equal(saved,word(snap.contexts,1));assert.equal(word(trap,0),BigInt(1+Math.floor(step/2))); assert.equal(word(trap,1),BigInt(11+thread));assert.equal(word(trap,3),512n);assert.equal(word(trap,8),0x1735n);assert.equal(word(trap,31),saved); const buffer=word(trap,2),sp=word(trap,32),pc=word(trap,33); assert.ok(sp%16n===0n&&sp>base&&sp<=top);assert.ok(buffer%8n===0n&&buffer>=base+16n&&buffer+512n<=top); assert.ok(pc%4n===0n&&pc>=0x80000044n&&pc<=0x80000000n+segments[0].size); assert.equal(segments[0].bytes.readUInt32LE(Number(pc-0x80000004n)),0xd4000001); assert.equal(word(trap,34)&0x3ffn,0x3c0n);assert.equal(word(trap,35),0x56000000n);assert.equal(word(trap,37),8n); assert.equal(word(trap,103),4n);assert.equal(word(trap,104),canary);assert.equal(word(trap,105),0n); const first=snapshots[step+1];assert.deepEqual(trap,first.traps.subarray(step*848,(step+1)*848)); assert.deepEqual(snap.contexts.subarray(step*16,(step+1)*16),first.contexts.subarray(step*16,(step+1)*16)); if(step>=2)assert.equal(buffer,word(snap.traps.subarray((step-2)*848,(step-1)*848),2),'same retained stack object after resume'); } if(!completed){for(const n of ['trapTtbr','kernelFrame','kernelRootGlobal'])zero(snap[n]);} else{assert.equal(word(snap.trapTtbr,0),w[7]);assert.equal(word(snap.kernelFrame,0),word(snap.contexts,1));assert.equal(word(snap.kernelRootGlobal,0),w[6]);} if(stage===6){for(let i=0;i<18;i++)zero(page(snap['frame'+i]));continue;} const frames=w.slice(16,34),l1=Buffer.from(kernel);l1.writeBigUInt64LE(frames[1]|3n,16); const l2=Buffer.alloc(4096);l2.writeBigUInt64LE(frames[2]|3n); const l3=Buffer.alloc(4096); for(const [slot,at,flags] of [[0,3,0x20000000000fc3n],[1,4,0x60000000000fc3n],[2,5,0x60000000000f43n], [17,14,0x60000000000f43n],[18,15,0x60000000000f43n],[21,16,0x60000000000f43n],[22,17,0x60000000000f43n]])l3.writeBigUInt64LE(frames[at]|flags,slot*8); for(let i=0;i<8;i++)l3.writeBigUInt64LE(frames[6+i]|0x60000000000f43n,(4+i)*8); for(const [i,p] of [l1,l2,l3,segments[0].bytes,segments[1].bytes].entries())assert.deepEqual(page(snap['frame'+i]),p); const data=Buffer.from(segments[2].bytes); for(let thread=0;thread<2;thread++){ const stack=Buffer.concat([page(snap['frame'+(14+thread*2)]),page(snap['frame'+(15+thread*2)])]); if(completed<=thread){zero(stack);continue;} const tid=BigInt(11+thread),base=[0x80011000n,0x80015000n][thread],phase=completed>thread+2?2n:1n; const firstTrap=snap.traps.subarray(thread*848,(thread+1)*848),pointer=word(firstTrap,2); [tid,phase,pointer,64n].forEach((v,i)=>data.writeBigUInt64LE(v,(1+thread*4+i)*8)); assert.equal(word(stack,0),0x1735000000000000n|tid);assert.equal(word(stack,1),0x1735ffff00000000n|tid); const offset=Number(pointer-base);for(let i=0;i<64;i++)assert.equal(stack.readBigUInt64LE(offset+i*8),(tid<<32n)|BigInt(i)); } assert.deepEqual(page(snap.frame5),data);for(let i=6;i<14;i++)zero(page(snap['frame'+i])); } for(const [from,to,thread] of [[1,2,0],[2,3,1],[3,4,0]])for(let i=0;i<2;i++){ const name='frame'+(14+thread*2+i);assert.deepEqual(snapshots[to][name],snapshots[from][name],'peer execution preserves retained stack'); } return{segmentCount:3,permissions:['RX','RO','RW'],processCount:1,threadCount:2,sameTtbr:true, schedule:['A-yield','B-yield','A-exit','B-exit'],resumedStackObjects:2,stackPagesPerThread:2,guardPages:4,processStackQuota:4, negativeCases:3,negativePublishCallbacks:0,peerStacksPreserved:true,privateFrames:18,zeroedReturnedFrames:18, kernelRootUnchanged:true,ownerRetainedOnRefusal:true,entryAddress:0x80000040,brkPrefixInstructions:16}; } export function inspectUserStacksEvidence({captures,memoryMiB,symbols,qmp,timeline,wholeRsp,readArtifact,file}){ assert.equal(captures.length,6);for(const [name,size] of Object.entries(userStacksSymbols))assert.equal(symbols[name].size,size); const snapshots=[];let previousQmp=-1,previousRsp=-1; for(const [index,capture] of captures.entries()){ assert.equal(capture.stage,index+1);const evidence=words(readArtifact(capture.dumps.evidence)); const expected=userStacksGlobals.map(([name,symbol])=>({name,symbol,val:symbols[symbol].address,size:symbols[symbol].size})); expected.push({name:'kernelRoot',val:Number(evidence[6]),size:4096});assert.equal(evidence[11],18n); for(let i=0;i<18;i++)expected.push({name:'frame'+i,val:Number(evidence[16+i]),size:4096}); const stage=17351+index,snapshot=inspectPhysicalCheckpoint({capture,expected,stage,qmp,timeline,wholeRsp,readArtifact,gateAddress:symbols.OS_K1735_CAPTURE_GATE.address}); const steps=timeline.filter(row=>row.stage===stage),qstart=qmp.findIndex(row=>row.direction==='tx'&&row.message.id===steps[0].id),qend=qmp.findIndex(row=>row.direction==='rx'&&row.message.id===steps.at(-1).id); assert.ok(qstart>previousQmp);previousQmp=qend; const rsp=capture.rsp.filter(row=>row.id!==null),start=wholeRsp.findIndex(row=>row.id===rsp[0].id&&row.direction==='tx'),end=wholeRsp.findIndex(row=>row.id===rsp.at(-1).id&&row.direction==='rx'); assert.ok(start>previousRsp);previousRsp=end;snapshots.push(snapshot); } return inspectUserStacksSnapshots(snapshots,memoryMiB,file); }