import assert from 'node:assert/strict'; import {inspectPhysicalCheckpoint} from './physical-checkpoint-proof.mjs'; import {remoteWakePhysicalPlan,inspectRemoteWakeSnapshots} from './remote-wake-proof.mjs'; import {remoteWakeGlobals} from './remote-wake-capture.mjs'; import {inspectRemoteWakeInputs} from './remote-wake-inputs-proof.mjs'; import {migrationGlobals,migrationSymbols} from './thread-migration-capture.mjs'; import {inspectThreadMigrationGraph} from './thread-migration-graph-proof.mjs'; import {migrationLedger} from './thread-migration-ledger-proof.mjs'; const u=(b,p)=>b.readBigUInt64LE(p),W=b=>Array.from({length:b.length/8},(_,i)=>u(b,i*8)),num=b=>W(b).map(Number),zero=b=>assert.ok(b.every(v=>v===0)); const TICKET=[1248,16,0,1168,1200,304,1232,1240,32,0,8,24,16,28,864,16,1,1264,304,0,1248]; const TRANSFER=[304,8,0,216,296,300,80,0,72,24,48,56,64,24,0,8,16]; export function inspectThreadMigrationInputs(compiled,symbols){ const input=inspectRemoteWakeInputs(compiled,symbols); for(const[name,size]of Object.entries(migrationSymbols)){assert.equal(symbols[name]?.size,size,name);assert.equal(compiled[name]?.length,size,name);} assert.deepEqual(num(compiled.OS_K2055_TICKET_LAYOUT),TICKET);assert.deepEqual(num(compiled.OS_K2055_TRANSFER_LAYOUT),TRANSFER); assert.equal(symbols.OS_K2055_TICKETS.size,TICKET[17]*2);assert.equal(symbols.OS_K2055_TICKETS.address%16,0); return{...input,migrationTicket:TICKET,migrationTransfer:TRANSFER}; } export function migrationPhysicalPlan(data,symbols){return [...migrationGlobals.map(([name,symbol])=>({name,symbol,val:symbols[symbol].address,size:symbols[symbol].size})),...remoteWakePhysicalPlan(data,symbols).slice(remoteWakeGlobals.length)];} function binding(bytes,cpu){assert.equal(bytes.length,32);assert.equal(u(bytes,0),1n);assert.equal(u(bytes,8),0x2045n);assert.equal(u(bytes,16),1n);assert.equal(bytes.readUInt32LE(24),cpu);assert.equal(bytes[28],1);} function ticket(bytes,source,generation,data,layout,ledger,symbols){ const e=W(data.OS_K2045_EVIDENCE),{backend:b,guard:gd,lease:le}=layout,t=TRANSFER,k=TICKET,guard=e[40+(source-1)*2],base=guard+4096n,root=e[6],record=k[2]+t[3],id=(BigInt(source)<<32n)|1n,target=3-source; assert.equal(bytes.length,k[0]);assert.deepEqual(W(bytes.subarray(record,record+24)),[id,0n,BigInt(generation)]);assert.deepEqual(W(bytes.subarray(record+24,record+48)),[base,base,16384n]);assert.equal(u(bytes,record+56),BigInt(source*10));assert.equal(u(bytes,record+64),0n);assert.equal(bytes[record+72],1); const pc=u(bytes,record+48);assert.ok(pc>=BigInt(symbols._text_start.address)&&pc>21n&511n)*8)&0x0000fffffffff000n,slot=l3+(guard>>12n&511n)*8n;assert.equal(u(bytes,token+gd[4]),slot);assert.equal(u(data['kernelL3-'+l3.toString(16)],Number(guard>>12n&511n)*8),0n); for(let i=0;i<5;i++){const at=stack+b[4]+i*le[0],address=u(bytes,at+le[1]),generation=u(bytes,at+le[3]),index=Number(u(bytes,at+le[4]));assert.equal(address,guard+BigInt(i)*4096n);assert.equal(bytes.readUInt32LE(at+le[2]),ledger.owner);assert.equal(ledger.live.get(address),generation);assert.ok(index>=0&&indexn.startsWith('kernelL3-')||n.startsWith('thread')||n.startsWith('OS_K2050_')),'OS_K2045_EVIDENCE','OS_K2045_GRAPH','OS_K2045_HANDLES','OS_K2045_IDENTITIES','OS_K2045_ONLINE','OS_K2045_RETURNED','OS_K2045_CALLS','OS_K2045_BUNDLES','OS_K2045_RECEIPTS'])assert.deepEqual(data[key],initial[key],key+' remains unchanged during ownership transfer'); for(let cpu=0;cpu<3;cpu++){binding(data.OS_K2055_BINDINGS.subarray(cpu*32,(cpu+1)*32),cpu+1);const at=Number(u(data.OS_K2055_GRAPH,cpu*512)-e[33+cpu]);assert.ok(at>=16&&at+data['local'+cpu].length<=131072-16);assert.deepEqual(data['local'+cpu],data['executive'+cpu].subarray(at,at+data['local'+cpu].length));views.push(inspectThreadMigrationGraph({data,cpu,stage,layout,ledger,symbols}));} const occupied=stage===10||stage===11?[1,0]:stage===13?[1,1]:[0,0];let live=0; for(let slot=0;slot<2;slot++){const at=slot*TICKET[17];assert.equal(u(data.OS_K2055_TICKETS,at+TICKET[20]),BigInt(occupied[slot]));if(occupied[slot]){ticket(data.OS_K2055_TICKETS.subarray(at,at+TICKET[0]),slot+1,slot===0&&stage===13?2:1,data,layout,ledger,symbols);live++;totalTickets++;}} assert.equal(views.slice(-3).reduce((sum,v)=>sum+v.actualThreadStacks,0)+live,6,'every original stack has exactly one live typed owner'); const ack=stage===10?[10,0,0]:stage===11?[10,11,0]:stage===12?[12,11,0]:stage===13?[13,13,0]:[14,14,0];assert.deepEqual(W(data.OS_K2055_ACKS),ack.map(BigInt)); for(let cpu=0;cpu<3;cpu++){const command=u(data.OS_K2045_COMMANDS,cpu*128);assert.equal(command,BigInt(ack[cpu]||2));zero(data.OS_K2045_COMMANDS.subarray(cpu*128+8,(cpu+1)*128));} const count=[1,2,3,5,7][index],command=[10,11,12,13,13,14,14],actualCpu=[1,2,1,1,2,1,2],oldCpu=[1,1,1,1,2,2,1],oldGen=[1,1,1,2,1,1,2],newGen=[0,0,2,0,0,3,2],refusals=[3,1,0,3,3,0,0]; for(let row=0;row<7;row++){const bytes=data.OS_K2055_HISTORY.subarray(row*256,(row+1)*256);if(row>=count){zero(bytes);continue}const h=W(bytes),cpu=actualCpu[row],expected=[BigInt(command[row]),BigInt(cpu),0x3c0n,e[6],h[4],(BigInt(oldCpu[row])<<32n)|1n,0n,BigInt(oldGen[row]),newGen[row]?(BigInt(cpu)<<32n)|1n:0n,0n,BigInt(newGen[row]),e[40+(oldCpu[row]-1)*2],BigInt(refusals[row])];assert.deepEqual(h,[...expected,...Array(32-expected.length).fill(0n)]);assert.ok(h[4]%16n===0n&&h[4]>=e[33+cpu-1]+16n&&h[4]0&&row<[1,2,3,5,7][index-1])assert.deepEqual(bytes,snapshots[index-1].OS_K2055_HISTORY.subarray(row*256,(row+1)*256));} } // Full-target refusal preserves the actual ticket's defined fields and all // private queues. Rust may change the seven trailing padding bytes after // Class while moving the same ticket out of and back into its mailbox. const first=snapshots[0].OS_K2055_TICKETS,second=snapshots[1].OS_K2055_TICKETS; assert.deepEqual(second.subarray(0,TICKET[7]+1),first.subarray(0,TICKET[7]+1)); assert.deepEqual(second.subarray(TICKET[0]),first.subarray(TICKET[0])); for(const key of ['OS_K2055_GRAPH','local0','local1','local2'])assert.deepEqual(snapshots[1][key],snapshots[0][key]); return{...base,physicalCheckpoints:14,migrationCheckpoints:5,migrationPrivateGraphViews:views.length,liveTicketViews:totalTickets,readyThreadsMigrated:2,sourceRecalls:1,fullTargetRefusals:1,staleSourceHandleRefusals:9,exactOnlineGenerationsBound:true,originalStackLeasesAndGuardsTransferred:true,originalInitialImagesAndPriorityPreserved:true,allSixStacksAlwaysOwned:true,concurrentQueueOperationsAccepted:false,runningThreadMigrationAccepted:false,productAccepted:false}; } export function inspectThreadMigrationEvidence(p){ const {captures,migration,symbols,qmp,timeline,wholeRsp,readArtifact}=p;assert.equal(captures.length,9);assert.equal(migration.length,5); const snapshots=[],extra=[];let lastQmp=-1,lastRsp=-1; for(const c of [...captures.slice(0,4),...migration,...captures.slice(4)]){ const data=Object.fromEntries(Object.entries(c.dumps).map(([k,v])=>[k,readArtifact(v)])),isMigration=c.stage>=10,stage=20550+c.stage,expected=isMigration?migrationPhysicalPlan(data,symbols):remoteWakePhysicalPlan(data,symbols),gateAddress=symbols[isMigration?'OS_K2055_CAPTURE_GATE':'OS_K2045_CAPTURE_GATE'].address; const snapshot=inspectPhysicalCheckpoint({capture:c,expected,stage,qmp,timeline,wholeRsp,readArtifact,gateAddress});const steps=timeline.filter(r=>r.stage===stage),start=qmp.findIndex(r=>r.direction==='tx'&&r.message.id===steps[0].id),end=qmp.findIndex(r=>r.direction==='rx'&&r.message.id===steps.at(-1).id);assert.ok(start>lastQmp);lastQmp=end; const rsp=c.rsp.filter(r=>r.id!==null),rs=wholeRsp.findIndex(r=>r.id===rsp[0].id&&r.direction==='tx'),re=wholeRsp.findIndex(r=>r.id===rsp.at(-1).id&&r.direction==='rx');assert.ok(rs>lastRsp);lastRsp=re;(isMigration?extra:snapshots).push(snapshot); } return inspectThreadMigrationSnapshots(snapshots,extra,p.memoryMiB,p.bootId,symbols,p.compiled,p.bootDtb,p.cpus); }