// Explicit synthetic checker fixture. This is not a VM execution or evidence // suitable for guest admission. It exercises the independent raw-byte checker. import assert from 'node:assert/strict'; import {readFileSync} from 'node:fs'; import {checkDmaSnapshots,DMA_STAGES,DMA_FORGERIES} from '../../scripts/os-acceptance/features/k2155-dma-check-v2.mjs'; import {ledger,released} from '../../scripts/os-acceptance/features/k2155-dma-authority-check.mjs'; import {dmaPacket} from '../../scripts/os-acceptance/dma-network.mjs'; const nativePacket=(bootId,queue)=>dmaPacket((BigInt('0x'+bootId)^0x4b32313535444d41n).toString(16).padStart(16,'0'),queue); const candidate=JSON.parse(readFileSync(process.argv[2])),image=readFileSync(candidate.artifacts.image.path),nm=readFileSync(candidate.artifacts.nm.path,'utf8'); const name='OS_K1000_AUTHORITY_LAYOUT_EXPECTED',match=nm.match(new RegExp('^([a-f0-9]{16}) ([a-f0-9]{16}) [A-Za-z] '+name+'$','m')); assert.ok(match);const symbol={address:parseInt(match[1],16),size:parseInt(match[2],16)},at=symbol.address-candidate.imageSpan.start; const compiled={[name]:image.subarray(at,at+symbol.size)},symbols={[name]:symbol},l=Array.from({length:24},(_,i)=>Number(compiled[name].readBigUInt64LE(i*8))); const bootId='1122334455667788',freeBefore=64n,freeHeld=52n,addresses=Array.from({length:12},(_,i)=>0x48000000n+BigInt(i)*4096n),authority=Buffer.alloc(l[0]); authority.writeBigUInt64LE(1n,l[1]+l[3]);authority.writeBigUInt64LE(0x4800c000n,l[1]+l[2]+l[21]);authority.writeBigUInt64LE(0x48040000n,l[1]+l[2]+l[22]); for(const [i,address]of addresses.entries()){const at=l[5]+i*l[6];authority[at+l[9]]=1;authority.writeBigUInt64LE(address,at+l[7]);authority.writeBigUInt64LE(BigInt(i+1),at+l[8]);} const after=released(authority,l,ledger(authority,l),addresses),sector=Buffer.alloc(512,0x55); const baseline={sent:1,received:2},exchange={afterCapturedStage:22,tx:nativePacket(bootId,1).toString('hex'),rx:nativePacket(bootId,0).toString('hex'),sentCount:2,receivedCount:3}; function words(values){const b=Buffer.alloc(values.length*8);values.forEach((v,i)=>b.writeBigUInt64LE(BigInt(v),i*8));return b;} function desc(b,at,address,length,flags,next){b.writeBigUInt64LE(address,at);b.writeUInt32LE(length,at+8);b.writeUInt16LE(flags,at+12);b.writeUInt16LE(next,at+14);} const snapshots=[]; for(const stage of DMA_STAGES){ const phase=Math.floor(stage/10),selected=stage%10-1; const e=Array(32).fill(0n);e[0]=0x4b32313535444d41n;e[1]=BigInt(stage);e[2]=freeBefore;e[3]=freeHeld;e[4]=BigInt('0x'+bootId); if(phase>=2)e[5]=e[6]=1n;if(phase>=3)e[7]=1n;if(phase>=4)e[8]=1n;if(phase===5){e[9]=freeBefore;e[10]=1n;} const s={OS_K2155_EVIDENCE:words(e),[name]:Buffer.from(compiled[name]),OS_K2155_DMA_AUTHORITY:words([0x41000000,l[0],symbol.address,192]),authority:Buffer.from(phase===5?after:authority)}; const all=Array(128).fill(0n); for(let d=0;d<2;d++){ const count=d===0?1:2,row=Array(64).fill(0n),id=d===0?2:1,base=d===0?0xa003e00:0xa003c00; row.splice(0,12,...[base,BigInt('0x'+bootId),id,0x554d4551,phase>=4?0:15,phase>=4?0:1,phase>=4||d===0?0:1,phase>=2&&phase<4?1:0,phase>=2?1:0,phase>=3?1:0,phase===5?1:0,d===0?0:selected].map(BigInt)); for(const [label,value]of [['magic',0x74726976],['version',2],['device',id],['vendor',0x554d4551],['ready',row[5+(d===0?0:selected)]],['status',row[4]],['interrupt',row[7]]]){ const b=Buffer.alloc(4);b.writeUInt32LE(Number(value));s[`mmio-${d}-${label}`]=b; } for(let q=0;qBuffer.alloc(4096)); row.splice(12+q*20,4,...frames); if(phase>=2){const len=pending?0:d===0?513:q===0?72:0;row.splice(16+q*20,6,1n,1n,pending?0n:1n,0n,BigInt(len),0n);} if(phase>=2&&phase<5){const [de,av,us,bu]=pages;av.writeUInt16LE(1,2);us.writeUInt16LE(pending?0:1,2);us.writeUInt32LE(pending?0:d===0?513:q===0?72:0,8); if(d===0){desc(de,0,frames[3],16,1,1);desc(de,16,frames[3]+512n,512,3,2);desc(de,32,frames[3]+16n,1,2,0);bu.writeBigUInt64LE(111n,8);sector.copy(bu,512);} else{desc(de,0,frames[3],q===0?2048:72,q===0?2:0,0);if(!pending)nativePacket(bootId,q).copy(bu,12);bu[2048]=0xa5;} } for(let p=0;p<4;p++)s[`dma-${d}-${q}-${p}`]=pages[p]; } all.splice(d*64,64,...row); } s.OS_K2155_DMA_DEVICES=words(all);snapshots.push(s); } const input={snapshots,compiled,symbols,memoryMiB:256,bootId,sector,exchange,baseline}; assert.equal(checkDmaSnapshots(input).allocatorExact,true); const rejected=[]; for(const[name,step,key,offset]of DMA_FORGERIES){ const altered=snapshots.map(s=>({...s}));altered[step][key]=Buffer.from(altered[step][key]);altered[step][key][offset]^=1; assert.throws(()=>checkDmaSnapshots({...input,snapshots:altered}),name);rejected.push(name); } for(const [name,change]of [ ['inherited-packet-same-boot',{exchange:{...exchange,tx:dmaPacket(bootId,1).toString('hex')}}], ['reuse-inherited-tx',{exchange:{...exchange,receivedCount:baseline.received}}], ['missing-receive',{exchange:{...exchange,sentCount:baseline.sent}}], ['wrong-boot-packet',{exchange:{...exchange,rx:nativePacket('0000000000000001',0).toString('hex')}}], ['missing-stage',{snapshots:snapshots.slice(1)}], ]){assert.throws(()=>checkDmaSnapshots({...input,...change}),name);rejected.push(name);} console.log(JSON.stringify({status:'DMA_CHECKER_HOST_TESTS_PASSED',guestAccepted:false,productAccepted:false,syntheticFixture:true,positiveFixtures:1,rejected,scope:'Synthetic byte fixture only; no MMIO, DMA, allocator or QEMU executed.'}));