import assert from 'node:assert/strict'; import {programStaticSymbols} from './program-static-capture.mjs'; import {inspectProgramStaticEvidence} from './program-static-proof.mjs'; import {mkdtempSync, readFileSync, writeFileSync} from 'node:fs'; import {join, relative, resolve} from 'node:path'; import {hashAt} from './boot-contract-report.mjs'; assert.equal(process.argv.length, 5, 'usage: node program-static-matrix-check.mjs '); const root = process.cwd(); const candidatePath = relative(root, resolve(process.argv[2])); const matrixPath = relative(root, resolve(process.argv[3])); const localPath = relative(root, resolve(process.argv[4])); assert.match(candidatePath, /^reports\/os-implementation\/K1785-program-static-candidate-[A-Za-z0-9_-]+\/report\.json$/); assert.match(matrixPath, /^reports\/os-implementation\/K1785-program-static-matrix-[A-Za-z0-9_-]+$/); assert.match(localPath, /^reports\/os-implementation\/K1785-program-static-local-[A-Za-z0-9_-]+\/report\.json$/); const read = path => JSON.parse(readFileSync(join(root, path), 'utf8')); const artifact = path => ({path, sha256: hashAt(root, path)}); const check = item => assert.deepEqual(artifact(item.path), item); const candidate = read(candidatePath); assert.equal(candidate.status, 'COMPILED_K1785_PROGRAM_STATIC_CANDIDATE'); assert.equal(candidate.build.exitCode, 0); assert.equal(candidate.productAccepted, false); for (const item of [candidate.predecessor, candidate.baseCandidate, candidate.user, candidate.fixture, candidate.elfFixture, candidate.contract, candidate.elfWire, candidate.userWire, candidate.segmentUserReport, ...Object.values(candidate.nativeUserSources), candidate.segmentNewUserReport, ...Object.values(candidate.nativeNewUserSources), candidate.newUserWire, candidate.journalWire, candidate.orphansWire, candidate.build.log, ...Object.values(candidate.sources), ...Object.values(candidate.artifacts)]) check(item); const predecessor = read(candidate.predecessor.path); assert.equal(predecessor.status, 'K1780_EXEC_CLOSE_DEVELOPMENT_MATRIX_VALIDATED'); assert.equal(predecessor.guestAccepted, true); const local = read(localPath); assert.equal(local.status, 'K1781_K1784_LOCAL_VALIDATED'); assert.equal(local.tests.green.exitCode, 0); assert.equal(local.tests.target.exitCode, 0); assert.equal(local.tests.red.exitCode,101); assert.equal(local.tests.proof.exitCode,0); assert.equal(local.tests.greenCount,2);assert.equal(local.tests.mutationRejected,true); for (const item of [local.predecessor, ...Object.values(local.sources), local.mutant, ...Object.values(local.tests).filter(value => value?.log).map(value => value.log)]) check(item); assert.deepEqual(candidate.predecessor, local.predecessor); assert.equal(candidate.sources.model.sha256, local.sources.runtimeModel.sha256); assert.equal(candidate.sources.segments.sha256,local.sources.segments.sha256); assert.equal(readFileSync(join(root,local.sources.runtimeAbi.path),'utf8').replace('#![no_std]\n',''),readFileSync(join(root,candidate.sources.runtimeAbi.path),'utf8')); assert.ok(candidate.imageSpan.bytes>0&&candidate.imageSpan.bytes<=8*1024*1024); assert.deepEqual(local.frozenBefore,local.frozenAfter); assert.deepEqual(candidate.frozenBefore,local.frozenBefore); assert.deepEqual(candidate.frozenAfter,local.frozenAfter); assert.equal(candidate.elfWire.sha256,local.sources.wire.sha256); assert.equal(candidate.sources.embeddedElf.sha256,local.sources.userWire.sha256); assert.equal(hashAt(root,'scripts/os-acceptance/program-static-proof.mjs'),local.sources.proof.sha256); assert.equal(hashAt(root,'scripts/os-acceptance/test-program-static-proof.mjs'),local.sources.proofTest.sha256); assert.equal(hashAt(root,'scripts/os-acceptance/physical-checkpoint-proof.mjs'),local.sources.physicalProof.sha256); assert.equal(candidate.sources.headers.sha256,local.sources.headers.sha256); assert.equal(candidate.userWire.sha256,local.sources.userWire.sha256); const userReport=read(candidate.segmentUserReport.path); assert.equal(userReport.status,'STAGING_BUILD_PASSED');assert.equal(userReport.build.exitCode,0); for(const item of [...userReport.sources,...userReport.dependencySources.map(({path,sha256})=>({path,sha256})),userReport.harness,userReport.elf,userReport.build.log])check(item); assert.equal(userReport.elf.sha256,candidate.sources.embeddedElf.sha256); for(const [index,path] of ['Cargo.toml','linker.ld','src/main.rs'].entries())assert.equal(candidate.nativeUserSources[path].sha256,userReport.sources[index].sha256); const newUserReport=read(candidate.segmentNewUserReport.path); assert.equal(newUserReport.status,'STAGING_BUILD_PASSED');assert.equal(newUserReport.build.exitCode,0); for(const item of [...newUserReport.sources,...newUserReport.dependencySources.map(({path,sha256})=>({path,sha256})),newUserReport.harness,newUserReport.elf,newUserReport.build.log])check(item); assert.equal(newUserReport.elf.sha256,candidate.sources.embeddedNewElf.sha256); assert.equal(candidate.newUserWire.sha256,local.sources.newUserWire.sha256); assert.equal(candidate.sources.embeddedNewElf.sha256,local.sources.newUserWire.sha256); for(const [index,path] of ['Cargo.toml','linker.ld','src/main.rs'].entries())assert.equal(candidate.nativeNewUserSources[path].sha256,newUserReport.sources[index].sha256); for(const [report,sources,role]of [[userReport,candidate.nativeUserSources,'sum'],[newUserReport,candidate.nativeNewUserSources,'text']]){ assert.equal(report.sourceLayout,'repository');assert.equal(report.appDirectory,'userspace/static-'+role); for(const[index,name]of ['Cargo.toml','linker.ld','src/main.rs'].entries())assert.equal(report.sources[index].path,report.sourceDirectory+'/'+name); assert.ok(report.dependencySources.length>=8); for(const item of report.dependencySources){ assert.equal(sources['dependency/'+item.originalPath].sha256,item.sha256); if(!item.originalPath.startsWith('os-user-runtime-client/'))assert.equal(hashAt(root,candidate.sourceDirectory+'/'+item.originalPath),item.sha256,'shared kernel ABI dependency differs '+item.originalPath); } } for(const item of local.librarySources) { check({path:item.path,sha256:item.sha256}); assert.equal(hashAt(root,candidate.sourceDirectory+'/'+item.source),item.sha256,'compiled source differs '+item.source); } assert.equal(candidate.elfFixture.sha256, local.sources.fixture.sha256); assert.equal(candidate.contract.sha256, local.sources.contract.sha256); const lifecyclePath = `${matrixPath}/scoped-lifecycle.json`; const lifecycle = read(lifecyclePath); assert.equal(lifecycle.status, 'RUNNER_PASSED'); assert.equal(lifecycle.sourceInputsStable, true); assert.equal(lifecycle.allOwnedGuestsStopped, true); assert.equal(lifecycle.guestCleanup.length, 6); const preflightPath = `${matrixPath}/preflight.json`; const preflight = read(preflightPath); assert.equal(preflight.images.find(row => row.kind === 'product')?.sha256, candidate.artifacts.image.sha256); assert.equal(preflight.loaders.find(row => row.kind === 'product')?.imageSha256, candidate.artifacts.image.sha256); check(preflight.programStaticCaptureHarness); assert.equal(preflight.programStaticCaptureHarness.sha256,local.sources.capture.sha256); assert.deepEqual(preflight.protectedBefore,local.frozenBefore); check(preflight.programStaticDiskHarness);assert.equal(preflight.programStaticDiskHarness.sha256,local.sources.diskFixture.sha256); const symbols=preflight.symbols.product; const nm=readFileSync(join(root,candidate.artifacts.nm.path),'utf8'); for(const name of Object.keys(programStaticSymbols)) { const match=nm.match(new RegExp('^([a-f0-9]{16}) ([a-f0-9]{16}) [A-Za-z] '+name+'$','m')); assert.ok(match);assert.deepEqual(symbols[name],{address:Number.parseInt(match[1],16),size:Number.parseInt(match[2],16)}); } const disk = artifact(`${matrixPath}/fixtures/readonly-block.img`); const diskBytes = readFileSync(join(root, disk.path)); assert.equal(diskBytes.length, 1024 * 1024); assert.deepEqual([...diskBytes.subarray(510, 512)], [0x55, 0xaa]); const cases = [], bootIds = new Set(); for (const route of ['direct', 'uefi']) for (const memoryMiB of [256, 512, 1024]) { const id = `product-one-bank-${route}-${memoryMiB}-mode-148`; const diagnosticPath = `${matrixPath}/${id}/diagnostic.json`; const diagnostic = read(diagnosticPath); assert.equal(diagnostic.status, 'GUEST_RETURNED'); assert.deepEqual(diagnostic.sourceBuild, artifact(candidatePath)); assert.deepEqual(diagnostic.blockDisk, disk); assert.equal(diagnostic.row.id, id); assert.equal(diagnostic.row.route, route); assert.equal(diagnostic.row.memoryMiB, memoryMiB); assert.equal(diagnostic.row.policyProbeMode, 148); assert.ok(!bootIds.has(diagnostic.row.bootId)); bootIds.add(diagnostic.row.bootId); assert.equal(diagnostic.archives.length, 175); assert.equal(diagnostic.archives.at(-1).code, '0x400'); assert.equal(diagnostic.waitCheckpoints, 72); assert.equal(diagnostic.deferredBatches, 11); for (const item of [diagnostic.serial, diagnostic.qmp, diagnostic.predecessorFinal, diagnostic.ancestorProof, diagnostic.wholeRsp, diagnostic.timeline]) check(item); const serial = readFileSync(join(root, diagnostic.serial.path), 'latin1'); let cursor = -1; for (const marker of ['K1785-STATIC: CAPTURE INTERPRETER-REFUSED','K1785-STATIC: CAPTURE SUM-LOADED','K1785-STATIC: CAPTURE SUM-EXECUTED','K1785-STATIC: CAPTURE TEXT-LOADED','K1785-STATIC: CAPTURE TEXT-EXECUTED','K1785-STATIC: CAPTURE SUM-RETURNED','K1785-STATIC: CAPTURE RETURNED','K1785-STATIC: PT-INTERP=UNSUPPORTED ACQUIRE=0 TWO-NATIVE-RUST=EXIT-0','K1785-STATIC: CLIENT=LINKED VERSION=1 EXIT-WIRE=2 TRAPS=4 FINAL-ZERO=14','K1785: PASS', 'K1100: DRAFT-ACTUAL-POLICY-RETURN NO-CANONICAL-CLAIM','K640: PASS']) { const next = serial.indexOf(marker, cursor + 1); assert.ok(next > cursor, `${id}: missing/out of order ${marker}`); cursor = next; } assert.equal(serial.split('K1785: PASS').length - 1, 1); const qmp = read(diagnostic.qmp.path); assert.ok(qmp.some(row => row.direction === 'rx' && row.message?.return?.status === 'running')); const proof=inspectProgramStaticEvidence({captures:diagnostic.programStatic,memoryMiB,symbols,qmp, file:readFileSync(join(root,candidate.sources.embeddedElf.path)),newFile:readFileSync(join(root,candidate.sources.embeddedNewElf.path)), timeline:read(diagnostic.timeline.path),wholeRsp:read(diagnostic.wholeRsp.path), readArtifact:item=>{check(item);return readFileSync(join(root,item.path));}}); cases.push({id, route, memoryMiB, bootId: diagnostic.row.bootId,...proof, diagnostic: artifact(diagnosticPath), serial: diagnostic.serial, qmp: diagnostic.qmp}); console.log(`K1785 PROGRAM STATIC CHECK ${id}`); } assert.equal(cases.length, 6); assert.equal(bootIds.size, 6); const directory = mkdtempSync(join(root, 'reports/os-implementation/K1785-program-static-matrix-check-')); const reportPath = join(directory, 'report.json'); const report = {schema: 'turkos-k1785-program-static-development-matrix/v1', status: 'K1785_PROGRAM_STATIC_DEVELOPMENT_MATRIX_VALIDATED', guestAccepted: true, productAccepted: false, candidate: artifact(candidatePath), user: candidate.user, local: artifact(localPath), image: candidate.artifacts.image, fixture: local.sources.fixture, contract: local.sources.contract, proof:local.sources.proof, physicalProof:local.sources.physicalProof, nativeUser:candidate.sources.embeddedElf, nativeNewUser:candidate.sources.embeddedNewElf, captureHarness:preflight.programStaticCaptureHarness, elfWire:candidate.elfWire, blockDisk: disk, journalWire: candidate.journalWire, harness: artifact('scripts/os-acceptance/.k1785-program-static-matrix-generated.mjs'), diskFixture:preflight.programStaticDiskHarness, matrixChecker:artifact('scripts/os-acceptance/program-static-matrix-check.mjs'), matrix: {path: matrixPath, lifecycle: artifact(lifecyclePath), preflight: artifact(preflightPath)}, cases, scope:'Two no_std Rust tools link the real native syscall client. Both execute version V1 and exit V2 under isolated RX/RO/RW images. PT_INTERP rejects the Linux loader before acquisition; host PT_DYNAMIC rejection is explicit. Seven physical checkpoints bind all four traps, two exact resumes, independent outputs, preservation and fourteen zero-returned frames. Development gate only.'}; writeFileSync(reportPath, JSON.stringify(report, null, 2) + '\n'); console.log(JSON.stringify({status: report.status, report: relative(root, reportPath), profiles: cases.length}));