// Synthetic raw-memory inputs for the native worker subproof, not QEMU data. import assert from 'node:assert/strict'; import {readFileSync} from 'node:fs'; import {fixture,candidate,compiled,symbols,words,guards,pages,root,stackLow,stackTop} from './native_fixture.mjs'; import {checkNativeWorkers} from '../../scripts/os-acceptance/features/k2155-native-worker-check.mjs'; const sym=symbols.OS_K2155_THREAD_LAYOUT,image=readFileSync(candidate.artifacts.image.path),offset=sym.address-candidate.imageSpan.start; compiled.OS_K2155_THREAD_LAYOUT=image.subarray(offset,offset+sym.size); const W=b=>Array.from({length:b.length/8},(_,i)=>b.readBigUInt64LE(i*8)),tl=W(compiled.OS_K2155_THREAD_LAYOUT).map(Number),al=W(compiled.OS_K1000_AUTHORITY_LAYOUT_EXPECTED).map(Number); function fill(first){ const input=fixture(first);const states=[[1,1],[2,1],[1,2],[1,1],[4,1],[4,4],[4,4],[0,0]],id=42n,gens=[1n,2n]; for(const [index,s]of input.snapshots.entries()){ const stage=Number(s.OS_K2155_EVIDENCE.readBigUInt64LE(8));s.OS_K2155_THREAD_LAYOUT=Buffer.from(compiled.OS_K2155_THREAD_LAYOUT); if(stage===0){s.OS_K2155_THREAD_TABLE=Buffer.alloc(80);s.OS_K2155_THREAD_RECORDS=Buffer.alloc(512);s.OS_K2155_THREAD_GUARDS=Buffer.alloc(96);s.OS_K2155_THREAD_SLOTS=Buffer.alloc(192);continue;} const phase=index<4?index-1:index<17?3:index===17?4:index===18?5:index<=21?6:index===22?7:8; const raw=Buffer.alloc(tl[0]),table=stackLow+8192n,slots=[],records=[]; raw.writeBigUInt64LE(id,tl[1]); s.OS_K2155_THREAD_TABLE=words([phase===8?0n:table,tl[0],2,phase>=7?0:2,0x41010000,al[0],symbols.OS_K1000_AUTHORITY_LAYOUT_EXPECTED.address,root,sym.address,128]); const l2=Buffer.alloc(4096),l3=Buffer.alloc(4096);l2.writeBigUInt64LE(0x41002003n,64*8); for(let i=0;i<2;i++){ const g=guards[i*6],state=phase===8?0:states[phase][i],empty=state===0,base=empty?0n:g+4096n,bytes=empty?0n:16384n,entry=empty?0n:BigInt(symbols.__os_k2155_worker_body.address),arg=empty?0n:BigInt(i+1); const at=tl[3]+i*tl[4];raw[at+tl[7]]=state;raw.writeBigUInt64LE(gens[i],at+tl[5]); for(const[field,value]of [[tl[9]+tl[10],base],[tl[9]+tl[11],base],[tl[9]+tl[12],bytes],[tl[13],entry],[tl[14],arg]])raw.writeBigUInt64LE(value,at+field); slots.push(id,BigInt(i),gens[i],BigInt(state),base,base,bytes,entry,arg,0n,empty?0n:1n,0n); for(let p=0;p<5;p++){const address=g+BigInt(p)*4096n;l3.writeBigUInt64LE(p===0&&phase<7?0n:address|0x0060000000000703n,Number(address>>12n&511n)*8);} s['guard-'+i]=words([phase>=7?guards[i*6+2]:0n]); if(phase===0){const b=Buffer.alloc(20480);b.fill(0xa5,0,4096);b.writeBigUInt64LE(0x4b313032304c4f57n,4096);b.writeBigUInt64LE(0x4b31303230484947n,20472);s['stack-'+i]=b;} else if(phase>=7)s['stack-'+i]=Buffer.alloc(20480); else{s['low-canary-'+i]=words([0x4b313032304c4f57n]);s['high-canary-'+i]=words([0x4b31303230484947n]);} const r=Array(32).fill(0n),worked=phase>=i+1,returned=phase>=i+2,stopped=phase>=i+4,stopReturned=phase>=i+5; for(const[o,present]of [[0,worked],[16,stopped]])if(present)r.splice(o,10,...[id,BigInt(i),gens[i],g,g+4096n,16384n,g+12288n,0n,1n,0n]); r[10]=worked?1n:0n;r[11]=returned?stackTop-2048n:0n;r[26]=stopped?1n:0n;r[27]=stopReturned?stackTop-2048n:0n;r[28]=phase>=7?1n:0n;records.push(...r); } s['l2-41001000']=l2;s['l3-41002000']=l3;s.OS_K2155_THREAD_SLOTS=words(slots);s.OS_K2155_THREAD_RECORDS=words(records); if(phase<8)s['thread-table']=raw; } return input; } const rejected=[]; for(const first of [100,110]){ const input=fill(first);assert.equal(checkNativeWorkers(input).workers,2); for(const[name,stage,key,at]of [ ['pretend-stopped-running-worker',210,'thread-table',tl[3]+tl[7]],['wrong-worker-generation',211,'thread-table',tl[3]+tl[5]], ['forged-thread-identity',20,'thread-table',tl[1]],['wrong-stack-base',210,'thread-table',tl[3]+tl[9]+tl[10]], ['wrong-worker-entry',1,'thread-table',tl[3]+tl[13]],['wrong-table-cpu',1,'thread-table',tl[2]],['wrong-table-root',330,'OS_K2155_THREAD_TABLE',56], ['false-empty-slots',30,'OS_K2155_THREAD_SLOTS',24],['slot-layout-mismatch',220,'OS_K2155_THREAD_SLOTS',32], ['guard-restored-too-soon',20,'guard-0',0],['guard-mapping-accessible',210,'l3-41002000',12*8],['wrong-stack-permission',221,'l3-41002000',13*8], ['stack-low-canary',211,'low-canary-1',0],['stack-high-canary',220,'high-canary-0',0],['initial-guard-poison',1,'stack-0',0], ['released-stack-not-erased',230,'stack-1',4096],['unreclaimed-guard',60,'stack-0',0], ['false-work-return-before-eret',210,'OS_K2155_THREAD_RECORDS',11*8],['false-stop-return-on-stack',220,'OS_K2155_THREAD_RECORDS',27*8], ['current-stack-reap-not-refused',220,'OS_K2155_THREAD_RECORDS',26*8],['unproved-second-worker-stop',30,'OS_K2155_THREAD_RECORDS',(32+26)*8], ['wrong-recorded-cpu',210,'OS_K2155_THREAD_RECORDS',7*8],['wrong-recorded-irq-context',211,'OS_K2155_THREAD_RECORDS',(32+9)*8], ['fake-initial-worker-record',0,'OS_K2155_THREAD_RECORDS',0],['changed-thread-abi',1,'OS_K2155_THREAD_LAYOUT',0], ]){ const snapshots=input.snapshots.map(s=>({...s})),i=snapshots.findIndex(s=>Number(s.OS_K2155_EVIDENCE.readBigUInt64LE(8))===stage);snapshots[i][key]=Buffer.from(snapshots[i][key]);snapshots[i][key][at]^=1; assert.throws(()=>checkNativeWorkers({...input,snapshots}),name);rejected.push(first+':'+name); } } console.log(JSON.stringify({status:'NATIVE_WORKER_HOST_TESTS_PASSED',positiveFixtures:2,rejected,syntheticFixture:true,guestAccepted:false,productAccepted:false,fullNativeShutdownVerified:false}));