import {createHash} from 'node:crypto'; import assert from 'node:assert/strict'; import {inspectPhysicalCheckpoint} from './physical-checkpoint-proof.mjs'; import {privateFileSymbols,privateFileGlobals} from './private-file-capture.mjs'; const canary=0x4b3932304652414dn; const word=(b,i)=>b.readBigUInt64LE(i*8); const words=b=>{assert.equal(b.length,1024);return Array.from({length:128},(_,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,16n);assert.equal(segments[2].memory,48n); assert.equal(word(segments[2].bytes,0),0x5257175589abcdefn);zero(segments[2].bytes.subarray(8));return segments; } function diskFixture(disk){ assert.equal(disk.length,1024*1024); // Independently validate the on-disk inode identity and both disjoint payload ranges. const superblock=Buffer.alloc(512);Buffer.from([84,85,82,75,70,83,0,0]).copy(superblock); superblock.writeUInt32LE(1,8);superblock.writeUInt32LE(512,12);superblock.writeBigUInt64LE(2048n,16); superblock.writeBigUInt64LE(17n,24);superblock.writeUInt32LE(2,32);superblock.writeBigUInt64LE(18n,36); assert.deepEqual(disk.subarray(16*512,17*512),superblock); const root=Buffer.alloc(512),inodes=Buffer.alloc(512); for(const [slot,name,size,lba] of [[0,'TAIL',513,30],[1,'NEIGHBOR',512,32]]){ root[slot*32]=1;root[slot*32+1]=name.length;root.writeUInt32LE(slot+1,slot*32+4);root.write(name,slot*32+8); inodes.writeUInt32LE(slot+1,slot*32);inodes.writeBigUInt64LE(BigInt(size),slot*32+8);inodes.writeBigUInt64LE(BigInt(lba),slot*32+16); } assert.deepEqual(disk.subarray(17*512,18*512),root);assert.deepEqual(disk.subarray(18*512,19*512),inodes); const sector0=Buffer.from(Array.from({length:512},(_,i)=>i%251+1)),sector1=Buffer.alloc(512,0xe7);sector1[0]=0x42; assert.deepEqual(disk.subarray(30*512,32*512),Buffer.concat([sector0,sector1])); assert.deepEqual(disk.subarray(32*512,33*512),Buffer.alloc(512,0xd3)); const filePage=Buffer.alloc(4096);sector0.copy(filePage);filePage[512]=0x42; const metadata=Buffer.concat([superblock,root,inodes,disk.subarray(32*512,33*512)]); return {filePage,metadata,sectors:Buffer.concat([sector0,sector1])}; } export function inspectPrivateFileSnapshots(snapshots,memoryMiB,file,disk){ assert.equal(snapshots.length,7);assert.ok([256,512,1024].includes(memoryMiB)); const segments=program(file),fixture=diskFixture(disk),digest=createHash('sha256').update(fixture.filePage.subarray(0,513)).digest(); assert.equal(word(segments[1].bytes,0),0x524f175501234567n); const all=snapshots.map(s=>words(s.evidence)),initial=all[0],a=initial.slice(16,23),b=initial.slice(23,30),shared=initial[30],kernel=page(snapshots[0].kernelRoot); const ramEnd=0x40000000n+BigInt(memoryMiB)*1024n*1024n,physical=v=>assert.ok(v%4096n===0n&&v>=0x40000000n&&v+4096n<=ramEnd); [initial[6],...a,...b,shared].forEach(physical);assert.equal(new Set([initial[6],...a,...b,shared]).size,16);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],count=[0,1,2,2,3,4,4][index],live=stage<4?15:stage<7?16:0,commits=stage<4?1:stage<7?2:4, ha=stage<4?2:stage<7?3:4,hb=stage<7?2:3,privatePage=w[31]; for(const [i,v] of [[0,1755],[1,stage],[2,file.length],[8,0x80000040],[9,0x80012000],[10,0x80020000],[11,513], [12,live],[13,count],[14,stage>=3?1:0],[15,stage>=4?1:0],[32,1],[33,stage>=4?2:0], [34,commits],[35,commits],[36,stage<3?1:stage===3?2:3],[37,stage>=3?1:0],[38,ha],[39,hb],[76,stage===7?8:6],[77,0], [81,stage<4?1:stage<7?2:0],[82,stage<7?2:0],[83,stage<7?2:0]])assert.equal(w[i],BigInt(v),'word '+i); for(let i=84;i<128;i++)assert.equal(w[i],0n); assert.ok(w[3]>=21n&&w[3]<=BigInt(memoryMiB)*256n);assert.equal(w[4],w[3]-BigInt(live));assert.equal(w[5],stage===7?w[3]:0n); for(const i of [0,2,3,6,8,9,10,11,...Array.from({length:15},(_,i)=>16+i),32,79,80])assert.equal(w[i],initial[i]); if(stage<4)assert.equal(privatePage,0n);else{physical(privatePage);assert.equal(privatePage,all[3][31]);assert.ok(![initial[6],...a,...b,shared].includes(privatePage),'private copy cannot replace a live page');} const history=[Array.from({length:ha},(_,i)=>w.slice(40+i*3,43+i*3)),Array.from({length:hb},(_,i)=>w.slice(52+i*3,55+i*3))]; for(const [side,h] of history.entries()){ for(let i=h.length*3;i<12;i++)assert.equal(w[40+side*12+i],0n); assert.deepEqual(h[1],(side===0?a:b).slice(0,3)); for(const [generation,t] of h.entries()){t.forEach(physical);assert.equal(new Set(t).size,3);if(index>0&&generationassert.equal(new Set(values).size,values.length,'overlapping live leases'); unique([...history[0][0],...history[1][0],...fixed]); unique([...history[0][0],...history[1][0],...history[0][1],...history[1][1],...fixed]); if(stage>=4)unique([...history[0][1],...history[1][1],...history[0][2],privatePage,...fixed]); if(stage===7){unique([...history[0][2],...history[1][1],...history[0][3],privatePage,...fixed]);unique([...history[0][3],...history[1][1],...history[1][2],...fixed]);} const tables=history.map(h=>h.at(-1)),ttbrA=tables[0][0]|(0x82n<<48n),ttbrB=tables[1][0]|(0x83n<<48n); assert.equal(w[7],ttbrA);assert.equal(w[78],ttbrB);assert.equal(w[79],a[0]|(0x82n<<48n));assert.equal(w[80],b[0]|(0x83n<<48n)); for(const [i,s] of segments.entries())assert.deepEqual(w.slice(64+i*4,68+i*4),[s.va,s.size,s.memory,BigInt(s.flags)]); const owners=Buffer.alloc(128),records=Buffer.alloc(64),pages=Buffer.alloc(64); if(stage<7){ [...tables[0],...a.slice(3),...tables[1],...b.slice(3),shared,...(stage>=4?[privatePage]:[])].forEach((v,i)=>owners.writeBigUInt64LE(v,i*8)); [[1n,stage>=4?privatePage:shared,stage>=4?2n:1n,stage>=4?0n:1n],[2n,shared,1n,1n]].flat().forEach((v,i)=>records.writeBigUInt64LE(v,i*8)); [[shared,1n,stage>=4?1n:2n,0n],...(stage>=4?[[privatePage,2n,1n,0n]]:[])].flat().forEach((v,i)=>pages.writeBigUInt64LE(v,i*8)); } assert.deepEqual(snap.owners,owners);assert.deepEqual(snap.records,records);assert.deepEqual(snap.pages,pages); assert.deepEqual(page(snap.kernelRoot),kernel);assert.deepEqual(snap.metadata,fixture.metadata);assert.deepEqual(snap.sectors,fixture.sectors); assert.deepEqual(snap.afterSectors,stage===7?fixture.sectors:Buffer.alloc(1024));assert.deepEqual(page(snap.source),fixture.filePage); assert.deepEqual(snap.hashes,Buffer.concat([digest,stage===7?digest:Buffer.alloc(32)])); const reads=Buffer.alloc(64);[16,17,18,32,30,31,...(stage===7?[30,31]:[0,0])].forEach((v,i)=>reads.writeBigUInt64LE(BigInt(v),i*8));assert.deepEqual(snap.reads,reads); const expected=new Map([[w[6],kernel]]),set=(address,bytes)=>{if(expected.has(address))assert.deepEqual(expected.get(address),bytes,'conflicting live frame');else expected.set(address,bytes);}; if(stage<7){ for(let side=0;side<2;side++){ const original=side===0?a:b,t=tables[side],l1=Buffer.from(kernel),l2=Buffer.alloc(4096),l3=Buffer.alloc(4096); l1.writeBigUInt64LE(t[1]|3n,16);l2.writeBigUInt64LE(t[2]|3n); for(const [slot,i,flags] of [[0,3,0x20000000000fc3n],[1,4,0x60000000000fc3n],[2,5,0x60000000000f43n],[17,6,0x60000000000f43n]])l3.writeBigUInt64LE(original[i]|flags,slot*8); const privateA=side===0&&stage>=4;l3.writeBigUInt64LE((privateA?privatePage:shared)|(privateA?0x60000000000f43n:0x60000000000fc3n),32*8); [l1,l2,l3].forEach((p,i)=>set(t[i],p)); for(let i=0;i<3;i++){ const bytes=Buffer.from(segments[i].bytes); if(i===2){bytes.writeBigUInt64LE(BigInt(side),8); if(side===1&&stage>=2)[BigInt(stage>=6?2:1),513n,3583n,0x1755n].forEach((v,j)=>bytes.writeBigUInt64LE(v,(2+j)*8)); if(side===0&&stage>=3){bytes.writeBigUInt64LE(stage>=5?2n:1n,16);if(stage>=5)[513n,3583n,0x1755n].forEach((v,j)=>bytes.writeBigUInt64LE(v,(3+j)*8));} }set(original[3+i],bytes); } const stackStage=side===0?(stage<3?0:stage<5?3:5):(stage<2?0:stage<6?2:6); set(original[6],stackStage===0?Buffer.alloc(4096):page(snapshots[stackStage-1][side===0?'originalA6':'originalB6'])); } set(shared,fixture.filePage);if(stage>=4){const copied=Buffer.from(fixture.filePage);if(stage>=5)copied[0]=0xee;set(privatePage,copied);} } const captures=[...a.map((v,i)=>['originalA'+i,v]),...b.map((v,i)=>['originalB'+i,v]),['shared',shared],...(stage>=4?[['private',privatePage]]:[]), ...history[0].flat().map((v,i)=>['historyA'+i,v]),...history[1].flat().map((v,i)=>['historyB'+i,v])]; const seen=new Map();for(const [name,address] of captures){const bytes=page(snap[name]);if(seen.has(address))assert.deepEqual(bytes,seen.get(address),'duplicate physical bytes');seen.set(address,bytes);assert.deepEqual(bytes,expected.get(address)??Buffer.alloc(4096),'live or zeroed frame '+name);} assert.equal(snap.traps.length,3392);assert.equal(snap.contexts.length,64);assert.equal(snap.resume.length,848); assert.deepEqual(snap.resume,stage>=4?snapshots[2].traps.subarray(848,1696):empty,'exact retained fault frame'); for(const name of ['trapTtbr','kernelFrame','kernelRootGlobal'])assert.equal(snap[name].length,8); for(let trapIndex=0;trapIndex<4;trapIndex++){ const trap=snap.traps.subarray(trapIndex*848,(trapIndex+1)*848),ctx=snap.contexts.subarray(trapIndex*16,(trapIndex+1)*16); if(trapIndex>=count){assert.deepEqual(trap,empty);zero(ctx);continue;} const first=snapshots[[1,2,4,5][trapIndex]];assert.deepEqual(trap,first.traps.subarray(trapIndex*848,(trapIndex+1)*848));assert.deepEqual(ctx,first.contexts.subarray(trapIndex*16,(trapIndex+1)*16)); const user=trapIndex===1?w[79]:trapIndex===2?all[3][7]:w[80];assert.equal(word(ctx,0),user); const saved=word(ctx,1);assert.ok(saved%16n===0n&&saved>=0x40000000n+848n&&saved+176n<=ramEnd);assert.equal(word(trap,31),saved); const sp=word(trap,32),pc=word(trap,33);assert.ok(sp%16n===0n&&sp>=0x80011000n&&sp<=0x80012000n);assert.ok(pc%4n===0n&&pc>=0x80000040n&&pc<0x80000000n+segments[0].size); assert.equal(word(trap,34)&0x3ffn,0x3c0n);assert.equal(word(trap,37),8n);assert.equal(word(trap,103),4n);assert.equal(word(trap,104),canary);assert.equal(word(trap,105),0n); if(trapIndex===1){ assert.equal(word(trap,35)&0xfe0007ffn,0x9200004fn);assert.equal(word(trap,36),0x80020000n); const insn=segments[0].bytes.readUInt32LE(Number(pc-0x80000000n));assert.equal((insn&0xffc00000)>>>0,0x39000000); const rn=(insn>>>5)&31,rt=insn&31,offset=(insn>>>10)&4095;assert.ok(rn<31&&rt<31);assert.equal(word(trap,rn)+BigInt(offset),0x80020000n);assert.equal(word(trap,rt)&255n,0xeen); }else{ for(const [i,v] of [[0,0],[1,trapIndex===2?0:1],[2,trapIndex===0?1:2],[3,0x80020000],[8,0x1755],[35,0x56000000]])assert.equal(word(trap,i),BigInt(v)); assert.equal(segments[0].bytes.readUInt32LE(Number(pc-0x80000004n)),0xd4000001,'saved PC follows SVC'); } } if(!count){for(const name of ['trapTtbr','kernelFrame','kernelRootGlobal'])zero(snap[name]);} else{assert.equal(word(snap.trapTtbr,0),word(snap.contexts,(count-1)*2));assert.equal(word(snap.kernelFrame,0),word(snap.contexts,(count-1)*2+1));assert.equal(word(snap.kernelRootGlobal,0),w[6]);} } return {segmentCount:3,permissions:['RX','RO','RW'],fileBytes:513,eofZeroBytes:3583,processes:2,initiallySharedRO:true, failedCowPreserved:true,failedCowAllocatedFrames:0,privateCopyAllocated:true,exactFaultFrameResumed:true,privateWriteByte:0xee, peerUnchanged:true,sourceHashUnchanged:true,actualDiskReads:8,nativeTraps:4,cowCommits:4,zeroedReturnedOwners:16,finalOwners:0, kernelRootUnchanged:true,allRecordedRetiredFramesZero:true,el0ExitCode:0,entryAddress:0x80000040,brkPrefixInstructions:16}; } export function inspectPrivateFileEvidence({captures,memoryMiB,symbols,qmp,timeline,wholeRsp,readArtifact,file,disk}){ assert.equal(captures.length,7);for(const [name,size] of Object.entries(privateFileSymbols))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=privateFileGlobals.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:'originalA'+i,val:Number(evidence[16+i]),size:4096}); for(let i=0;i<7;i++)expected.push({name:'originalB'+i,val:Number(evidence[23+i]),size:4096}); expected.push({name:'shared',val:Number(evidence[30]),size:4096}); if(index>=3)expected.push({name:'private',val:Number(evidence[31]),size:4096}); for(let i=0;irow.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),rstart=wholeRsp.findIndex(row=>row.id===rsp[0].id&&row.direction==='tx'),rend=wholeRsp.findIndex(row=>row.id===rsp.at(-1).id&&row.direction==='rx'); assert.ok(rstart>previousRsp);previousRsp=rend;snapshots.push(snapshot); } return inspectPrivateFileSnapshots(snapshots,memoryMiB,file,disk); }