import assert from 'node:assert/strict'; import {spawnSync} from 'node:child_process'; import {copyFileSync, cpSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync} from 'node:fs'; import {join, relative, resolve} from 'node:path'; import {frozenSnapshot, hashAt} from './boot-contract-report.mjs'; assert.equal(process.argv.length, 3, 'usage: node anonymous-map-candidate.mjs '); const root = process.cwd(); const frozenBefore = frozenSnapshot(root); const previousPath = relative(root, resolve(process.argv[2])); const previous = JSON.parse(readFileSync(join(root, previousPath), 'utf8')); assert.equal(previous.status, 'K1740_USER_HEAP_RECLAIM_DEVELOPMENT_MATRIX_VALIDATED'); assert.equal(previous.guestAccepted, true); const base = JSON.parse(readFileSync(join(root, previous.candidate.path), 'utf8')); assert.equal(base.status, 'COMPILED_K1740_USER_HEAP_RECLAIM_CANDIDATE'); const directory = mkdtempSync(join(root, 'reports/os-implementation/K1745-anonymous-map-candidate-')); const sources = join(directory, 'sources'); cpSync(join(root, 'reports/os-implementation/K1745-anonymous-map-work-sources'), sources, {recursive: true, filter: path => !path.split('/').includes('target')}); const artifact = path => ({path: relative(root, path), sha256: hashAt(root, relative(root, path))}); for (const item of [previous.candidate, base.sources.inheritedDupUserElf, base.sources.inheritedUserElf, base.sources.inheritedStatUserElf]) assert.equal(hashAt(root, item.path), item.sha256); const elfCopies = [ [base.sources.inheritedUserElf, 'kernel/src/k1215-user.elf'], [base.sources.inheritedStatUserElf, 'kernel/src/k1575-stat-user.elf'], [base.sources.inheritedDupUserElf, 'kernel/src/k1580-dup-user.elf'], ]; for (const [item, target] of elfCopies) copyFileSync(join(root, item.path), join(sources, target)); for (const [item, target] of [[base.sources.inheritedReplayModel, 'kernel/src/runtime/replay/replay.rs'], [base.sources.inheritedOrphansModel, 'kernel/src/runtime/turkfs_orphans.rs'], [base.sources.orphansTransport, 'kernel/src/k1695-virtio-orphans.rs'], [base.sources.replayFormat, 'kernel/src/runtime/replay/lib.rs'], [base.sources.replayTransport, 'kernel/src/k1685-virtio-replay.rs'], [base.sources.secondCutTransport, 'kernel/src/k1690-virtio-second-cut.rs'], [base.sources.inheritedCheckpointModel, 'kernel/src/runtime/checkpoint/checkpoint.rs'], [base.sources.inheritedCheckpointFormat, 'kernel/src/runtime/checkpoint/lib.rs'], [base.sources.inheritedCheckpointTransport, 'kernel/src/k1680-virtio-checkpoint.rs'], [base.sources.inheritedReservationModel, 'kernel/src/runtime/turkfs_journal_reservation.rs'], [base.sources.inheritedOrderingModel, 'kernel/src/runtime/turkfs_journal_ordering.rs'], [base.sources.inheritedJournalModel, 'kernel/src/runtime/turkfs_journal.rs'], [base.sources.inheritedNamespaceModel, 'kernel/src/runtime/turkfs_namespace.rs'], [base.sources.inheritedOpenUnlinkModel, 'kernel/src/runtime/turkfs_open_unlink.rs'], [base.sources.orderingTransport, 'kernel/src/k1670-virtio-journal.rs'], [base.journalWire, 'kernel/src/k1665-journal.bin']]) { assert.equal(hashAt(root, item.path), item.sha256); copyFileSync(join(root, item.path), join(sources, target)); } assert.equal(hashAt(root,base.sources.headers.path),base.sources.headers.sha256); copyFileSync(join(root,base.sources.headers.path),join(sources,'kernel/src/runtime/elf_loader.rs')); const userReportPath=join(root,'reports/os-implementation/K1745-anonymous-map-user-staging-taWsNV/report.json'); const userReport=JSON.parse(readFileSync(userReportPath)); assert.equal(userReport.status,'STAGING_BUILD_PASSED');assert.equal(userReport.build.exitCode,0); for(const item of [...userReport.sources,userReport.elf,userReport.build.log])assert.equal(hashAt(root,item.path),item.sha256); const nativeUserSources={}; for(const [index,path] of ['Cargo.toml','linker.ld','src/main.rs'].entries()) { const item=userReport.sources[index]; assert.ok(item.path.endsWith('/source/'+path)); assert.equal(hashAt(root,'userspace/elf-anonymous/'+path),item.sha256); const target=join(directory,'native-user-source',path); mkdirSync(join(directory,'native-user-source','src'),{recursive:true}); copyFileSync(join(root,item.path),target);nativeUserSources[path]=artifact(target); } copyFileSync(join(root,userReport.elf.path),join(sources,'kernel/src/k1745-anonymous-user.elf')); assert.equal(hashAt(root,'kernel/tests/os/fixtures/elf-anonymous-rust-v1.elf'),userReport.elf.sha256); const replayDirectory = join(sources, 'kernel/src/runtime/replay'); const heapModel = join(sources, 'kernel/src/runtime/anonymous_map.rs'); const env = {...process.env, CARGO_TARGET_DIR: join(directory, 'target'), RUSTFLAGS: '-C relocation-model=static -C link-arg=-T' + join(sources, 'kernel/linker.ld') + ' -C link-arg=--build-id=none -A warnings'}; delete env.CARGO_ENCODED_RUSTFLAGS; const build = spawnSync('cargo', ['build', '--manifest-path', join(sources, 'kernel/Cargo.toml'), '--offline', '--release', '--target', 'aarch64-unknown-none', '--bin', 'turkos-kernel', '--no-default-features', '--features', 'os-runtime-product'], {cwd: root, env, encoding: 'utf8', timeout: 600000, maxBuffer: 128 * 1024 * 1024}); const log = join(directory, 'kernel-product.log'); writeFileSync(log, (build.stdout ?? '') + (build.stderr ?? '')); const report = {schema: 'turkos-k1745-anonymous-map-candidate/v1', status: !build.error && build.signal === null && build.status === 0 ? 'COMPILED_K1745_USER_MAPPINGS_ANONYMOUS_CANDIDATE' : 'K1745_USER_MAPPINGS_ANONYMOUS_COMPILE_FAILED', guestAccepted: false, productAccepted: false, predecessor: artifact(join(root, previousPath)), baseCandidate: previous.candidate, user: previous.user, sourceDirectory: relative(root, sources), sources: { main: artifact(join(sources, 'kernel/src/main.rs')), guest: artifact(join(sources, 'kernel/src/k1215-alloc-guest.rs')), probe: artifact(join(sources, 'kernel/src/k1745-anonymous-map.rs')), model: artifact(heapModel), mapping:artifact(join(sources,'kernel/src/runtime/user_copy_mapping.rs')), inheritedHeap: artifact(join(sources,'kernel/src/runtime/process_heap.rs')), segments:artifact(join(sources,'kernel/src/runtime/elf_segments.rs')), headers: artifact(join(sources,'kernel/src/runtime/elf_loader.rs')), space: artifact(join(sources,'kernel/src/runtime/anonymous_space.rs')), vectors: artifact(join(sources,'kernel/src/os_k1745_vectors.S')), frameAsm: artifact(join(sources,'kernel/src/k1745-frame-asm.rs')), barrier: artifact(join(sources, 'kernel/src/lib.rs')), inheritedWriteGate: artifact(join(sources, 'kernel/src/write_release.rs')), embeddedElf: artifact(join(sources, 'kernel/src/k1745-anonymous-user.elf')), inheritedOrphansModel: artifact(join(sources, 'kernel/src/runtime/turkfs_orphans.rs')), inheritedReplayModel: artifact(join(replayDirectory, 'replay.rs')), inheritedCheckpointModel: artifact(join(sources, 'kernel/src/runtime/checkpoint/checkpoint.rs')), inheritedCheckpointFormat: artifact(join(sources, 'kernel/src/runtime/checkpoint/lib.rs')), inheritedCheckpointTransport: artifact(join(sources, 'kernel/src/k1680-virtio-checkpoint.rs')), replayFormat: artifact(join(replayDirectory, 'lib.rs')), replayTransport: artifact(join(sources, 'kernel/src/k1685-virtio-replay.rs')), secondCutTransport: artifact(join(sources, 'kernel/src/k1690-virtio-second-cut.rs')), orphansTransport: artifact(join(sources, 'kernel/src/k1695-virtio-orphans.rs')), inheritedReservationModel: artifact(join(sources, 'kernel/src/runtime/turkfs_journal_reservation.rs')), orderingTransport: artifact(join(sources, 'kernel/src/k1670-virtio-journal.rs')), inheritedOrderingModel: artifact(join(sources, 'kernel/src/runtime/turkfs_journal_ordering.rs')), inheritedJournalModel: artifact(join(sources, 'kernel/src/runtime/turkfs_journal.rs')), embeddedJournal: artifact(join(sources, 'kernel/src/k1665-journal.bin')), inheritedNamespaceModel: artifact(join(sources, 'kernel/src/runtime/turkfs_namespace.rs')), inheritedOpenUnlinkModel: artifact(join(sources, 'kernel/src/runtime/turkfs_open_unlink.rs')), inheritedAllocationModel: artifact(join(sources, 'kernel/src/runtime/turkfs_allocation.rs')), inheritedDirectoryModel: artifact(join(sources, 'kernel/src/runtime/turkfs_directory.rs')), inheritedReaderModel: artifact(join(sources, 'kernel/src/runtime/turkfs_reader.rs')), diskTransport: artifact(join(sources, 'kernel/src/virtio_ro_turkfs.rs')), inheritedDupUserElf: artifact(join(sources, 'kernel/src/k1580-dup-user.elf')), inheritedStatUserElf: artifact(join(sources, 'kernel/src/k1575-stat-user.elf')), inheritedUserElf: artifact(join(sources, 'kernel/src/k1215-user.elf')), linker: artifact(join(sources, 'kernel/linker.ld'))}, fixture: artifact(join(root, 'userspace/tests/service_registry.rs')), elfFixture: artifact(join(root, 'kernel/tests/os/user_anonymous_map.rs')), elfWire: artifact(join(root, 'kernel/tests/os/fixtures/elf-three-load-v1.bin')), userWire: artifact(join(root, 'kernel/tests/os/fixtures/elf-anonymous-rust-v1.elf')), segmentUserReport: artifact(userReportPath), nativeUserSources, journalWire: artifact(join(root, 'kernel/tests/os/fixtures/turkfs-journal-v1.bin')), orphansWire: artifact(join(root, 'kernel/tests/os/fixtures/turkfs-orphans-p12.bin')), contract: artifact(join(root, 'kernel/ANONYMOUS_MAP_CONTRACT.md')), build: {exitCode: build.status, error: build.error?.message ?? null, log: artifact(log)}}; if (!build.error && build.signal === null && build.status === 0) { const elf = join(directory, 'product.elf'); copyFileSync(join(directory, 'target/aarch64-unknown-none/release/turkos-kernel'), elf); const sysroot = spawnSync('rustc', ['--print', 'sysroot'], {encoding: 'utf8'}); const rust = spawnSync('rustc', ['-vV'], {encoding: 'utf8'}); assert.equal(sysroot.status, 0); assert.equal(rust.status, 0); const host = rust.stdout.match(/^host: (.+)$/m)?.[1]; assert.ok(host); const tool = name => join(sysroot.stdout.trim(), 'lib/rustlib', host, 'bin', name); const command = (name, args) => { const answer = spawnSync(tool(name), args, {cwd: root, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024}); assert.equal(answer.status, 0, answer.stderr); return answer.stdout; }; const image = join(directory, 'product.Image'); command('llvm-objcopy', ['-O', 'binary', elf, image]); const nm = join(directory, 'product.nm'); writeFileSync(nm, command('llvm-nm', ['-n', '-S', elf])); report.artifacts = {elf: artifact(elf), image: artifact(image), nm: artifact(nm)}; } report.frozenBefore = frozenBefore; report.frozenAfter = frozenSnapshot(root); assert.deepEqual(report.frozenAfter, frozenBefore); writeFileSync(join(directory, 'report.json'), JSON.stringify(report, null, 2) + '\n'); console.log(JSON.stringify({status: report.status, report: relative(root, join(directory, 'report.json')), log: report.build.log.path})); if (build.error || build.signal !== null || build.status !== 0) process.exitCode = 1;