import assert from 'node:assert/strict'; import {inspectPhysicalCheckpoint} from './physical-checkpoint-proof.mjs'; import {elfEntrySymbols} from './elf-entry-capture.mjs'; const canary=0x4b3932304652414dn; const words=bytes=>{assert.equal(bytes.length,384);return Array.from({length:48},(_,i)=>bytes.readBigUInt64LE(i*8));}; const word=(bytes,index)=>bytes.readBigUInt64LE(index*8); const page=bytes=>{assert.equal(bytes.length,4096);return bytes;}; const zero=bytes=>assert.ok(bytes.every(value=>value===0),'zeroed page'); 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); return 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}; }); } export function inspectElfEntrySnapshots(snapshots,memoryMiB,file) { assert.equal(snapshots.length,3);assert.ok([256,512,1024].includes(memoryMiB)); const segments=program(file),ramEnd=0x40000000n+BigInt(memoryMiB)*1024n*1024n; const physical=value=>assert.ok(value%4096n===0n&&value>=0x40000000n&&value+4096n<=ramEnd); const all=snapshots.map(s=>words(s.evidence));const [a,b,c]=all; for(const [index,w] of all.entries()) { for(const [at,value] of [[0,1720],[1,index+1],[2,file.length],[8,0x80000040],[9,0x80012000], [10,3],[11,7],[12,16],[13,0],[14,index===0?0:1],[15,index===2?1:0],[23,0]])assert.equal(w[at],BigInt(value)); for(let i=36;i<48;i++)assert.equal(w[i],0n); physical(w[6]);for(const address of w.slice(16,23))physical(address); assert.equal(new Set([w[6],...w.slice(16,23)]).size,8); assert.equal(w[7],w[16]|(0x7an<<48n)); assert.ok(w[3]>=7n&&w[3]<=BigInt(memoryMiB)*256n);assert.equal(w[4],w[3]-7n); assert.equal(w[5],index===2?w[3]:0n); for(const [i,segment] of segments.entries())assert.deepEqual(w.slice(24+i*4,28+i*4),[segment.va,segment.size,segment.memory,BigInt(segment.flags)]); for(let i=0;i<48;i++)if(![1,5,14,15].includes(i))assert.equal(w[i],a[i]); } const kernel=page(snapshots[0].kernelRoot);assert.equal(word(kernel,2),0n); for(const snap of snapshots)assert.deepEqual(page(snap.kernelRoot),kernel); const expectedRoot=Buffer.from(kernel);expectedRoot.writeBigUInt64LE(a[17]|3n,16); const expectedL2=Buffer.alloc(4096);expectedL2.writeBigUInt64LE(a[18]|3n); const expectedL3=Buffer.alloc(4096); expectedL3.writeBigUInt64LE(a[19]|0x20000000000fc3n,0); expectedL3.writeBigUInt64LE(a[20]|0x60000000000fc3n,8); expectedL3.writeBigUInt64LE(a[21]|0x60000000000f43n,16); expectedL3.writeBigUInt64LE(a[22]|0x60000000000f43n,17*8); for(const snap of snapshots.slice(0,2)) { assert.deepEqual(page(snap.frame0),expectedRoot);assert.deepEqual(page(snap.frame1),expectedL2); assert.deepEqual(page(snap.frame2),expectedL3); } for(let i=0;i<3;i++)assert.deepEqual(page(snapshots[0]['frame'+(3+i)]),segments[i].bytes); zero(page(snapshots[0].frame6)); for(let i=0;i<2;i++)assert.deepEqual(page(snapshots[1]['frame'+(3+i)]),segments[i].bytes); const changed=Buffer.from(segments[2].bytes); changed.writeBigUInt64LE(0x17205257deadbeefn,0);changed.writeBigUInt64LE(0x1720b55000000001n,8); changed.writeBigUInt64LE(0x1720b55000000064n,512); assert.deepEqual(page(snapshots[1].frame5),changed,'actual EL0 data/BSS writes'); page(snapshots[1].frame6); for(let i=0;i<7;i++)zero(page(snapshots[2]['frame'+i])); const empty=Buffer.alloc(848);empty.writeBigUInt64LE(4n,824);empty.writeBigUInt64LE(canary,832); assert.deepEqual(snapshots[0].trap,empty); for(const name of ['trapTtbr','kernelFrame','kernelRootGlobal']) { assert.equal(snapshots[0][name].length,8);assert.equal(word(snapshots[0][name],0),0n); assert.equal(snapshots[1][name].length,8);assert.deepEqual(snapshots[2][name],snapshots[1][name]); } assert.equal(word(snapshots[1].trapTtbr,0),a[7]);assert.equal(word(snapshots[1].kernelRootGlobal,0),a[6]); const kernelFrame=word(snapshots[1].kernelFrame,0); assert.ok(kernelFrame%16n===0n&&kernelFrame>=0x40000000n+848n&&kernelFrame+176n<=ramEnd); const trap=snapshots[1].trap;assert.equal(trap.length,848);assert.deepEqual(snapshots[2].trap,trap); assert.equal(word(trap,0),0n);assert.equal(word(trap,8),0x1720n);assert.equal(word(trap,31),kernelFrame); const sp=word(trap,32),pc=word(trap,33); assert.ok(sp%16n===0n&&sp>=0x80011000n&&sp<=0x80012000n); assert.ok(pc%4n===0n&&pc>=0x80000044n&&pc<=0x80000000n+segments[0].size); assert.equal(segments[0].bytes.readUInt32LE(Number(pc-0x80000004n)),0xd4000001,'SVC instruction before actual saved PC'); 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); return {segmentCount:3,permissions:['RX','RO','RW'],negativeCases:16,negativeAcquisitions:0, privateFrames:7,zeroedReturnedFrames:7,kernelRootUnchanged:true,el0ExitCode:0, entryAddress:0x80000040,brkPrefixInstructions:16,realRustExecuted:true,segmentPayloadLoaded:true,ownerRetainedOnRefusal:true}; } export function inspectElfEntryEvidence({captures,memoryMiB,symbols,qmp,timeline,wholeRsp,readArtifact,file}) { assert.equal(captures.length,3); for(const [name,size] of Object.entries(elfEntrySymbols))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=[['evidence','OS_K1720_EVIDENCE'],['trap','OS_K1720_TRAP'], ['trapTtbr','OS_K1720_TRAP_TTBR'],['kernelFrame','OS_K1720_KERNEL_FRAME'],['kernelRootGlobal','OS_K1720_KERNEL_ROOT']] .map(([name,symbol])=>({name,symbol,val:symbols[symbol].address,size:symbols[symbol].size})); expected.push({name:'kernelRoot',val:Number(evidence[6]),size:4096}); for(let i=0;i<7;i++)expected.push({name:'frame'+i,val:Number(evidence[16+i]),size:4096}); const stage=17201+index; const snapshot=inspectPhysicalCheckpoint({capture,expected,stage,qmp,timeline,wholeRsp,readArtifact, gateAddress:symbols.OS_K1720_CAPTURE_GATE.address}); const steps=timeline.filter(row=>row.stage===stage); const qstart=qmp.findIndex(row=>row.direction==='tx'&&row.message.id===steps[0].id); const 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); const rstart=wholeRsp.findIndex(row=>row.id===rsp[0].id&&row.direction==='tx'); const rend=wholeRsp.findIndex(row=>row.id===rsp.at(-1).id&&row.direction==='rx'); assert.ok(rstart>previousRsp);previousRsp=rend; snapshots.push(snapshot); } return inspectElfEntrySnapshots(snapshots,memoryMiB,file); }