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 block-geometry-local.mjs '); const root = process.cwd(); const predecessorPath = resolve(process.argv[2]); const predecessor = JSON.parse(readFileSync(predecessorPath, 'utf8')); assert.equal(predecessor.status, 'K1500_VIRTIO_BACKPRESSURE_DEVELOPMENT_MATRIX_VALIDATED'); const dir = mkdtempSync(join(root, 'reports/os-implementation/K1505-block-geometry-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-block-geometry/src/lib.rs'); const fixture = join(sourceRoot, 'kernel/tests/os/block_geometry.rs'); const noStdRoot = join(sourceRoot, 'no-std-root.rs'); mkdirSync(sourceRoot, {recursive: true}); writeFileSync(noStdRoot, '#![no_std]\n#[path = "os-block-geometry/src/lib.rs"]\npub mod block_geometry;\n'); const sources = { model: copy('os-block-geometry/src/lib.rs', model), fixture: copy('kernel/tests/os/block_geometry.rs', fixture), contract: copy('kernel/BLOCK_GEOMETRY_CONTRACT.md', join(sourceRoot, 'kernel/BLOCK_GEOMETRY_CONTRACT.md')), noStdRoot: artifact(noStdRoot), backpressureReference: copy('os-virtio-backpressure/src/lib.rs', join(sourceRoot, 'os-virtio-backpressure/src/lib.rs')), driverReference: copy('kernel/src/virtio_ro_turkfs.rs', join(sourceRoot, 'kernel/src/virtio_ro_turkfs.rs')), harness: artifact(join(root, 'scripts/os-acceptance/block-geometry-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, 'block-geometry-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_block_io_geometry_first_and_last \.\.\. ok/); assert.match(greenText, /os_block_io_geometry_overflow_and_outside \.\.\. ok/); assert.match(greenText, /2 passed; 0 failed/); const mutantRoot = join(dir, 'mutant'); const mutantModel = join(mutantRoot, 'os-block-geometry/src/lib.rs'); const mutantFixture = join(mutantRoot, 'kernel/tests/os/block_geometry.rs'); copy('kernel/tests/os/block_geometry.rs', mutantFixture); mkdirSync(join(mutantModel, '..'), {recursive: true}); const original = readFileSync(model, 'utf8'); const sectorGuard = 'if end_sector > self.sectors { return Err(Error::OutsideCapacity); }'; const byteGuard = 'if end_byte > self.capacity_bytes { return Err(Error::OutsideCapacity); }'; assert.equal(original.split(sectorGuard).length, 2, 'sector capacity guard must exist once'); assert.equal(original.split(byteGuard).length, 2, 'byte capacity guard must exist once'); writeFileSync(mutantModel, original.replace(sectorGuard, 'if false { return Err(Error::OutsideCapacity); }').replace(byteGuard, 'if false { return Err(Error::OutsideCapacity); }')); const mutant = artifact(mutantModel); const mutantBinary = join(dir, 'block-geometry-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_block_io_geometry_overflow_and_outside \.\.\. FAILED/); const report = { schema: 'turkos-k1505-block-geometry-local/v1', status: 'K1501_K1504_LOCAL_VALIDATED', guestAccepted: false, productAccepted: false, predecessor: artifact(predecessorPath), sources, mutant, tests: {...tests, greenCount: 2, mutationRejected: true}, scope: 'The pure 512-byte sector geometry checks nonzero capacity, checked LBA addition and byte multiplication, first and last sector boundaries, and out-of-range refusal before queue publication. Two host tests, AArch64 no_std compilation, and a removed-capacity-end-guard mutant are recorded. Physical block boundary reads remain for K1505.'}; 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}));