mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-02 05:20:28 +08:00
Sockets: sock_accept, sock_recv, sock_send and sock_shutdown on sockets the program inherited, with socket file types, poll and non-blocking; KWWASI_TCPLISTEN=ADDR:PORT[,...] opens listening TCP sockets at descriptors 4, 5, ... (exit 126 when one is already open); a write to a socket whose peer went away is EPIPE, not SIGPIPE. Threads: modules that import wasi.thread-spawn and a shared env.memory run on the shared path, each wasm thread with a worker, a channel and a kwwasi connection of its own (a kwwasi pthread sharing the process's descriptors), so a thread blocked on stdin holds up no other; proc_exit or a trap in any thread ends the program; the message path refuses such a module with exit 126. Clocks: realtime and monotonic follow the guest's (kwwasi sends both readings at START, no request per call); the CPU clocks are the worker's CPU time on Node, the process clock covering every thread through worker.cpuUsage(); unknown clock ids are EINVAL and the resolution is the timer's measured step. Files: fd_allocate; fd_fdstat_set_flags refuses a sync flag change with ENOTSUP; more Linux errnos mapped; KWWASI_CWD=cwd resolves every path without a leading "/" from the cwd, with no guessing. A wasm64 module is refused with exit 126, and a sleep on the message path no longer ends a fraction of a millisecond early. The kwwasi protocol is version 3. test/wasi-binfmt.ts runs its guest commands as root and covers the above.
41 lines
2.0 KiB
TypeScript
41 lines
2.0 KiB
TypeScript
// SPDX-License-Identifier: MIT
|
|
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
// The guest's WASI programs on Node (core/wasi-exec.ts): each in a
|
|
// worker_threads Worker running wasi-entry.ts, started by the owner of
|
|
// vm.vsock. serveWasiPrograms() is core's with this spawner, which also
|
|
// gives each worker's CPU time (worker.cpuUsage(), for threaded programs'
|
|
// process clock).
|
|
//
|
|
// From source, Node starts the worker from wasi-entry.ts itself. `pnpm
|
|
// build` (build.config.ts, WORKERS) replaces the URL below (it must appear
|
|
// exactly once in this file) with the bundled entry as a string literal.
|
|
|
|
import { Worker } from "node:worker_threads";
|
|
import type { Vsock } from "../core/vsock.ts";
|
|
import { serveWasiProgramsWith, type WasiExecOptions, type WasiExecServer } from "../core/wasi-exec.ts";
|
|
import type { WasiFromProgram, WasiProgramEndpoint } from "../core/wasi-program.ts";
|
|
|
|
export function spawnNodeWasi(): WasiProgramEndpoint {
|
|
const entry: URL | string = new URL("./wasi-entry.ts", import.meta.url);
|
|
const opts = { name: "unkernel-wasi" };
|
|
const w = typeof entry === "string" ? new Worker(entry, { ...opts, eval: true }) : new Worker(entry, opts);
|
|
// A program never keeps the process alive: it belongs to the machine, whose end ends it.
|
|
w.unref();
|
|
return {
|
|
post: (msg, transfer) => w.postMessage(msg, transfer),
|
|
listen(h) {
|
|
w.on("message", (m: WasiFromProgram) => h.message(m));
|
|
w.on("error", h.error);
|
|
w.on("exit", () => h.exit());
|
|
},
|
|
terminate: () => void w.terminate(),
|
|
// Read on the worker's thread (an interrupt, also while it computes or waits); Node 24.6 and later.
|
|
...(typeof w.cpuUsage === "function" && { cpuUsage: () => w.cpuUsage().then((u) => u.user + u.system) }),
|
|
};
|
|
}
|
|
|
|
/** Run the guest's WASI programs (kwwasi, vsock port 1021) on this host, each in a worker thread. */
|
|
export function serveWasiPrograms(vsock: Vsock, opts: WasiExecOptions = {}): WasiExecServer {
|
|
return serveWasiProgramsWith(vsock, { ...opts, spawn: spawnNodeWasi });
|
|
}
|