import assert from 'node:assert/strict'; import {copyFileSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync} from 'node:fs'; import {spawnSync} from 'node:child_process'; import {createHash} from 'node:crypto'; import {join, relative, resolve} from 'node:path'; assert.equal(process.argv.length, 3, 'usage: node pci-bar-size-local.mjs '); const root = process.cwd(); const predecessorPath = resolve(process.argv[2]); const predecessor = JSON.parse(readFileSync(predecessorPath, 'utf8')); assert.equal(predecessor.status, 'K1440_PCI_BRIDGES_DEVELOPMENT_MATRIX_VALIDATED'); const dir = mkdtempSync(join(root, 'reports/os-implementation/K1445-pci-bar-size-local-')); const sha = path => createHash('sha256').update(readFileSync(path)).digest('hex'); const artifact = path => ({path: relative(root, path), sha256: sha(path)}); const copy = (source, target) => { mkdirSync(join(target, '..'), {recursive: true}); copyFileSync(join(root, source), target); return artifact(target); }; const sourceRoot = join(dir, 'source'); const model = join(sourceRoot, 'os-pci-resources/src/lib.rs'); const fixture = join(sourceRoot, 'kernel/tests/os/pci_resources.rs'); const noStdRoot = join(sourceRoot, 'no-std-root.rs'); mkdirSync(sourceRoot, {recursive: true}); writeFileSync(noStdRoot, '#![no_std]\n#[path = "os-pci-resources/src/lib.rs"]\npub mod pci_resources;\n'); const sources = { model: copy('os-pci-resources/src/lib.rs', model), fixture: copy('kernel/tests/os/pci_resources.rs', fixture), contract: copy('kernel/PCI_BAR_CONTRACT.md', join(sourceRoot, 'kernel/PCI_BAR_CONTRACT.md')), noStdRoot: artifact(noStdRoot), dtbReference: copy('kernel/src/dtb.rs', join(sourceRoot, 'kernel/src/dtb.rs')), harness: artifact(join(root, 'scripts/os-acceptance/pci-bar-size-local.mjs')), }; const run = (name, program, args, expected) => { const result = spawnSync(program, args, {cwd: root, encoding: 'utf8'}); const log = join(dir, `${name}.log`); writeFileSync(log, `${result.stdout ?? ''}${result.stderr ?? ''}`); assert.equal(result.status, expected, `${name}: expected exit ${expected}, observed ${result.status}; ${artifact(log).path}`); return {exitCode: result.status, log: artifact(log)}; }; const binary = join(dir, 'pci-bar-size-tests'); const tests = { compile: run('host-compile', 'rustc', ['--edition=2024', '--test', fixture, '-o', binary], 0), green: run('host-tests', binary, [], 0), target: run('aarch64-no-std-check', 'rustc', ['--edition=2024', '--crate-type=lib', '--target=aarch64-unknown-none', '--emit=metadata', noStdRoot, '-o', join(dir, 'model.rmeta')], 0), }; const greenText = readFileSync(join(dir, 'host-tests.log'), 'utf8'); assert.match(greenText, /os_pci_resources_bar_size_positive \.\.\. ok/); assert.match(greenText, /os_pci_resources_bar_size_negative \.\.\. ok/); assert.match(greenText, /2 passed; 0 failed/); const mutantRoot = join(dir, 'mutant'); const mutantModel = join(mutantRoot, 'os-pci-resources/src/lib.rs'); const mutantFixture = join(mutantRoot, 'kernel/tests/os/pci_resources.rs'); copy('kernel/tests/os/pci_resources.rs', mutantFixture); mkdirSync(join(mutantModel, '..'), {recursive: true}); const original = readFileSync(model, 'utf8'); const guard = 'if size < 16 || !size.is_power_of_two() { return Err(Error::MalformedMask); }'; assert.equal(original.split(guard).length, 2, 'size-mask guard must exist once'); writeFileSync(mutantModel, original.replace(guard, 'if false { return Err(Error::MalformedMask); }')); const mutant = artifact(mutantModel); const mutantBinary = join(dir, 'pci-bar-size-mutant-tests'); tests.mutantCompile = run('mutant-compile', 'rustc', ['--edition=2024', '--test', mutantFixture, '-o', mutantBinary], 0); tests.red = run('mutant-tests', mutantBinary, [], 101); const redText = readFileSync(join(dir, 'mutant-tests.log'), 'utf8'); assert.match(redText, /os_pci_resources_bar_size_negative \.\.\. FAILED/); const report = { schema: 'turkos-k1445-pci-bar-size-local/v1', status: 'K1441_K1444_LOCAL_VALIDATED', guestAccepted: false, productAccepted: false, predecessor: artifact(predecessorPath), sources, mutant, tests: {...tests, greenCount: 2, mutationRejected: true}, scope: 'The local BAR decoder separates I/O from 32-bit and 64-bit memory ranges, derives size and type for unassigned BARs, checks power-of-two size, alignment and overflow before a range claim, and rejects a malformed size mask without changing prior ownership. Two host tests and AArch64 no_std compilation passed; removing the size-mask guard made the negative test fail. This does not prove physical PCI BAR probe/restore.', }; const reportPath = join(dir, 'report.json'); writeFileSync(reportPath, JSON.stringify(report, null, 2) + '\n'); console.log(JSON.stringify({status: report.status, report: relative(root, reportPath), tests: report.tests.greenCount, mutationRejected: report.tests.mutationRejected}));