Something went wrong. Try again.
An experimental port of NetBSD, FreeBSD, OpenBSD, and XNU to unkernel unkernel.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245#!/usr/bin/env node// SPDX-License-Identifier: MIT// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>// Boot the kernel with Node's own node:wasi (uvwasi) providing every// wasi_snapshot_preview1 import, instead of the SDK's js/core/wasi.ts. This checks// that the kernel works with a third-party WASI preview 1 implementation.// Only the `unkernel` imports come from here: task switching (JSPI) and// stubs for no block device, no network device and no JIT (kwemu interprets).//// node test/wasi-node.ts [--verbose]// UNKERNEL_TEST_BASE_DIR=dir node test/wasi-node.ts # base.cpio and base-test.cpio from dir (a KW_BASE_OUT;// # default vendor/build/initramfs)// node test/wasi-node.ts --run KERNEL INITRD "CMDLINE" [MiB]//// Cases: kwtest on vmlinux32.wasm and on vmlinux (memory64), and the base's// base shell on vmlinux32.wasm with commands typed into a stdin pipe (there// is no block device here, so no distro). The shell case guards against uvwasi's zero-timeout poll_oneoff, which reports only the// clock event even when stdin is readable (kernel wasi.c, wasi_stdin_readable).// --run boots one kernel with the console on stdio; proc_exit's code// becomes the process exit code.import { spawn } from "node:child_process";import fs from "node:fs";import os from "node:os";import path from "node:path";import { fileURLToPath } from "node:url";import { moduleIsMemory64 } from "../js/core/kernel.ts";
const here = path.dirname(fileURLToPath(import.meta.url));const root = path.resolve(here, "..");const args = process.argv.slice(2);
if (args[0] === "--run") await run(args[1]!, args[2]!, args[3], args[4]);else await test();
async function run(kernelPath: string, initrdPath: string, cmdline = "console=hvc0", mib = "256"): Promise<void> { const { WASI } = await import("node:wasi"); const dir = fs.mkdtempSync(path.join(os.tmpdir(), "unkernel-wasi-")); process.on("exit", () => fs.rmSync(dir, { recursive: true, force: true })); fs.copyFileSync(initrdPath, path.join(dir, "initrd"));
const wasi = new WASI({ version: "preview1", args: ["vmlinux", cmdline], env: {}, preopens: { "/": dir }, returnOnExit: false, // proc_exit -> process.exit(code) }); const module = new WebAssembly.Module(fs.readFileSync(kernelPath)); const pages = Math.ceil((Number(mib) * 1048576) / 65536); const memory = moduleIsMemory64(module) ? new WebAssembly.Memory({ initial: BigInt(pages), maximum: BigInt(pages), address: "i64" } as unknown as WebAssembly.MemoryDescriptor) : new WebAssembly.Memory({ initial: pages, maximum: pages }); wasi.initialize({ exports: { memory } }); // binds node:wasi to the imported memory
const fatal = (m: string): never => { process.stderr.write(`[wasi-node] ${m}\n`); process.exit(255); }; const ENODEV = 19; const resumers = new Map<bigint, (t: bigint) => void>(); let retFromFork: (prev: bigint, next: bigint) => Promise<unknown>; const unkernel = { // Same contract as kernel.mjs: park prev, run next; resolves with the // task that later switches back to prev. wasm_switch_to: new WebAssembly.Suspending((prev: bigint, next: bigint, isNew: unknown, prevDead: unknown) => { const parked = prevDead ? new Promise(() => {}) : new Promise<bigint>((r) => resumers.set(prev, r)); if (isNew) { resumers.delete(next); // a parked task resumes on a fresh stack (park.c) queueMicrotask(() => retFromFork(prev, next).then( () => fatal("task returned from ret_from_fork"), (e) => fatal(`wasm trap: ${e?.stack ?? e}`))); } else { const resume = resumers.get(next); if (!resume) fatal(`switch to unknown task 0x${next.toString(16)}`); resumers.delete(next); resume!(prev); } return parked; }), wasm_dump_stacktrace: () => {}, wasm_snapshot_point: () => -95, // EOPNOTSUPP; called only after IRQ 8, which this host never raises wasm_driver_console_size: () => 0, wasm_driver_block_info: () => -ENODEV, wasm_driver_block_submit: () => -ENODEV, wasm_driver_block_reap: () => 0n, wasm_driver_net_init: () => -BigInt(ENODEV), wasm_driver_net_rx_len: () => 0n, wasm_driver_net_recv: () => 0n, wasm_driver_net_send: () => 0n, wasm_driver_fb_init: () => -BigInt(ENODEV), wasm_driver_fb_attach: () => 0, wasm_driver_fb_mode: () => 0, wasm_driver_input_init: () => -BigInt(ENODEV), wasm_driver_input_read: () => 0n, wasm_driver_input_state: () => {}, wasm_driver_sound_init: () => -BigInt(ENODEV), wasm_driver_sound_event: () => {}, wasm_driver_sound_write: () => 0n, wasm_driver_sound_capture_event: () => {}, wasm_driver_sound_read: () => 0n, wasm_driver_camera_init: () => -BigInt(ENODEV), wasm_driver_camera_stream: () => {}, wasm_driver_camera_frame: () => 0n, wasm_driver_vsock_init: () => -ENODEV, wasm_driver_vsock_send: () => -ENODEV, wasm_driver_vsock_rx_len: () => 0, wasm_driver_vsock_recv: () => 0, wasm_driver_vsock_keep: () => 0, wasm_driver_shm_init: () => -ENODEV, wasm_driver_shm_request: () => 0, wasm_driver_shm_attach: () => {}, wasm_driver_shm_detach: () => {}, wasm_driver_shm_reply: () => {}, // kwvirtio: no devices (0 for device 0), so nothing else is called. wasm_driver_virtio_init: () => 0, wasm_driver_virtio_features: () => 0n, wasm_driver_virtio_set_features: () => -ENODEV, wasm_driver_virtio_config: () => {}, wasm_driver_virtio_status: () => {}, wasm_driver_virtio_queue_max: () => 0, wasm_driver_virtio_queue_set: () => -ENODEV, wasm_driver_virtio_notify: () => {}, wasm_driver_virtio_interrupt: () => 0, // kwserial: no ports, so nothing else is called. wasm_driver_serial_init: () => -ENODEV, wasm_driver_serial_open: () => -ENODEV, wasm_driver_serial_close: () => {}, wasm_driver_serial_config: () => {}, wasm_driver_serial_write: () => 0, wasm_driver_serial_read: () => 0, wasm_driver_serial_set_signals: () => {}, wasm_driver_serial_status: () => 0, // kwbt: no device, so nothing else is called. wasm_driver_bt_init: () => -ENODEV, wasm_driver_bt_status: () => 0, wasm_driver_bt_info: () => -ENODEV, wasm_driver_bt_open: () => -ENODEV, wasm_driver_bt_close: () => {}, wasm_driver_bt_send: () => -ENODEV, wasm_driver_bt_rx_len: () => 0, wasm_driver_bt_recv: () => 0, // kwusb: no controller, so nothing else is called. wasm_driver_usb_init: () => -ENODEV, wasm_driver_usb_port: () => 0, wasm_driver_usb_port_op: () => -ENODEV, wasm_driver_usb_changed: () => 0, wasm_driver_usb_submit: () => -ENODEV, wasm_driver_usb_cancel: () => {}, wasm_driver_usb_reap: () => 0, // kwemu's JIT tier (js/core/jit.ts): not offered here, so kwemu // only interprets and never calls the other two. kw_host_jit_caps: () => 0, kw_host_jit_compile: () => -1, kw_host_jit_free: () => {}, }; const instance = new WebAssembly.Instance(module, { env: { memory }, unkernel, wasi_snapshot_preview1: wasi.wasiImport, } as unknown as WebAssembly.Imports); retFromFork = WebAssembly.promising(instance.exports.ret_from_fork as () => unknown); WebAssembly.promising(instance.exports._start as () => unknown)().then( () => fatal("_start returned"), (e) => fatal(`wasm trap: ${e?.stack ?? e}`));}
interface Case { name: string; kernel: string; initrd: string; cmdline?: string; mib?: number; /** [wait for, then type] */ script?: Array<[RegExp, string] & { from?: number }>; expect: RegExp[];}
async function test(): Promise<void> { const verbose = args.includes("--verbose"); const k32 = path.join(root, "vendor/build/kernel/vmlinux32.wasm"); const k64 = path.join(root, "vendor/build/kernel/vmlinux"); const baseDir = path.resolve(process.env.UNKERNEL_TEST_BASE_DIR ?? path.join(root, "vendor/build/initramfs")); const baseCpio = path.join(baseDir, "base.cpio"); const testCpio = path.join(baseDir, "base-test.cpio"); const kwtest = { initrd: testCpio, cmdline: "console=hvc0 rdinit=/usr/bin/kwtest", expect: [/kwtest: 0 failure\(s\)/] }; const cases: Case[] = [ { name: "kwtest vmlinux32.wasm", kernel: k32, ...kwtest }, { name: "kwtest vmlinux (memory64)", kernel: k64, ...kwtest }, { name: "base shell vmlinux32.wasm, stdin pipe", kernel: k32, initrd: baseCpio, mib: 512, cmdline: "console=hvc0 rdinit=/init", // [wait for, then type] script: [[/base\x1b\[0m \S* # /, "uname -a; busybox | head -1; echo done-$((6*7))\n"], [/done-42/, "poweroff\n"]], expect: [/Linux unkernel .* aarch64 Linux/, /BusyBox v\S+/, /reboot: Power down/], }, ];
const results: boolean[] = []; for (const c of cases) { const missing = [c.kernel, c.initrd].filter((f) => !fs.existsSync(f)); if (missing.length) { console.log(`FAIL ${c.name}: missing ${missing.map((f) => path.relative(root, f)).join(", ")}`); results.push(false); continue; } const { code, out } = await boot(c, verbose); const unmet = c.expect.filter((re) => !re.test(out)); const ok = code === 0 && !unmet.length; const detail = ok ? `exit 0` : `exit ${code}${unmet.length ? `, no match for ${unmet.join(" ")}` : ""}`; console.log(`${ok ? "ok " : "FAIL"} ${c.name}: ${detail}`); if (!ok && !verbose) process.stdout.write(out.split("\n").slice(-15).map((l) => ` | ${l}`).join("\n") + "\n"); results.push(ok); } const ok = results.every(Boolean); console.log(ok ? "PASS" : "FAIL"); process.exit(ok ? 0 : 1);}
function boot(c: Case, verbose: boolean, timeoutMs = 120000): Promise<{ code: number | string; out: string }> { return new Promise((resolve) => { const child = spawn(process.execPath, ["--no-warnings", fileURLToPath(import.meta.url), "--run", c.kernel, c.initrd, c.cmdline ?? "console=hvc0", String(c.mib ?? 256)], { stdio: [c.script ? "pipe" : "ignore", "pipe", "pipe"] }); let out = ""; let step = 0; const onData = (d: string) => { out += d; if (verbose) process.stdout.write(d); // Type the next line once its trigger shows up after the previous one. while (c.script && step < c.script.length && c.script[step]![0].test(out.slice(c.script[step]!.from ?? 0))) { child.stdin!.write(c.script[step]![1]); if (++step < c.script.length) c.script[step]!.from = out.length; } }; child.stdout!.setEncoding("utf8").on("data", onData); child.stderr!.setEncoding("utf8").on("data", onData); const timer = setTimeout(() => { out += `\n[wasi-node] timeout after ${timeoutMs / 1000} s\n`; child.kill("SIGKILL"); }, timeoutMs); child.on("exit", (code, signal) => { clearTimeout(timer); resolve({ code: code ?? signal ?? "?", out }); }); });}