Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349// SPDX-License-Identifier: MIT
import { platform } from "./platform.ts";import { read_wasm_function_imports } from "./wasm_binary.ts";
export interface Instance extends WebAssembly.Instance { exports: { __indirect_function_table: WebAssembly.Table; boot(): void; trigger_irq(irq: number): void; // Linked directly into each user instance as linux.syscall, so a syscall // never passes through JS. The number, every argument slot and the result // are user words. syscall( nr: UserWord, arg0: UserWord, arg1: UserWord, arg2: UserWord, arg3: UserWord, arg4: UserWord, arg5: UserWord, ): UserWord; get_thread_area(): UserWord; copy_siginfo(to: UserWord): number; clear_siginfo(): void; };}
export interface UserContext { module: WebAssembly.Module; memory: WebAssembly.Memory; // The JS API cannot recover a memory's maximum after construction. maximum_pages: number;}
const supported_user_module_imports = new Set([ "env\0memory\0memory", "linux\0syscall\0function", "linux\0get_thread_area\0function", "linux\0copy_siginfo\0function",]);
/** * The address type of a user memory. Pointers, longs and syscall argument * slots cross the user ABI at this width. */export type UserWidth = "i32" | "i64";
/** * A pointer or long crossing the user ABI: a number where the user width is * i32, and a bigint where it is i64. */export type UserWord = number | bigint;
/** Converts a user word, such as a function table index, to a number. */export function user_word_number(word: UserWord): number { return typeof word === "bigint" ? Number(word) : word >>> 0;}
// The signature of each kernel function a user module may import. A 64-bit// syscall argument spans two slots where the width is i32; the kernel's// asm/syscall_wrapper.h reassembles it.function user_module_function_types(width: UserWidth) { return new Map([ ["linux\0syscall", `${Array(7).fill(width).join(",")}->${width}`], ["linux\0get_thread_area", `->${width}`], ["linux\0copy_siginfo", `${width}->i32`], ]);}
/** * Whether every import can be supplied when a userspace module of the given * width is instantiated. A binary built for another ABI fails here, so exec * reports ENOEXEC rather than failing to link after the point of no return. */export function user_module_imports_supported( module: WebAssembly.Module, bytes: Uint8Array, width: UserWidth,): boolean { const function_types = user_module_function_types(width); return ( WebAssembly.Module.imports(module).every(({ module, name, kind }) => supported_user_module_imports.has(`${module}\0${name}\0${kind}`), ) && read_wasm_function_imports(bytes).every( ({ module, name, type }) => function_types.get(`${module}\0${name}`) === type, ) );}
/** * Allocates a shared memory, halving the maximum whenever the engine refuses * to reserve that much address space, degrading as far as the initial size. */export function allocate_shared_memory( initial_pages: number, preferred_maximum_pages: number, allocate: (descriptor: WebAssembly.MemoryDescriptor) => WebAssembly.Memory = (descriptor) => new WebAssembly.Memory(descriptor),): { memory: WebAssembly.Memory; maximum_pages: number } { let maximum_pages = preferred_maximum_pages; for (;;) { try { return { memory: allocate({ initial: initial_pages, maximum: maximum_pages, shared: true, }), maximum_pages, }; } catch (error) { const smaller_maximum = Math.max(initial_pages, Math.floor(maximum_pages / 2)); if (!(error instanceof RangeError) || smaller_maximum >= maximum_pages) { throw error; } maximum_pages = smaller_maximum; } }}
/* * Read memory.buffer immediately before constructing a view so growth in * another worker is visible. Turn invalid bounds and host exceptions into an * ordinary failure result for kernel copy helpers. * * Omitting length returns the remainder of the current memory. Fork uses this * to derive the child's initial page count and bytes from the same view. */export function memory_bytes( memory: WebAssembly.Memory, address: number, length?: number,): Uint8Array<ArrayBufferLike> | null { try { const buffer = memory.buffer; const view_length = length ?? buffer.byteLength - address; if ( !Number.isSafeInteger(address) || !Number.isSafeInteger(view_length) || address < 0 || view_length < 0 || view_length > buffer.byteLength || address > buffer.byteLength - view_length ) { return null; } return new Uint8Array(buffer, address, view_length); } catch { return null; }}
const WASM_USER_MEMORY_NONE = 0;const WASM_USER_MEMORY_SHARE = 1;const WASM_USER_MEMORY_COPY = 2;
/** Values for the kernel.terminate_machine guest/host ABI. */export const MachineTerminationReason = { Clean: 0, Panic: 1,} as const;
/** Values for the kernel.terminate_machine guest/host ABI. */export type MachineTerminationReason = (typeof MachineTerminationReason)[keyof typeof MachineTerminationReason];
export interface Imports { env: { memory: WebAssembly.Memory }; boot: { get_devicetree(buf: number, size: number): number; get_initramfs(buf: number, size: number): number; }; kernel: { breakpoint(): void; halt_worker(): void; /** Reports that the whole machine ended, rather than only this worker. */ terminate_machine(reason: MachineTerminationReason): void; boot_console_write(msg: number, len: number): void; boot_console_close(): void; return_address(_level: number): number; /** Unix time in nanoseconds, monotonically advancing during this session. */ get_now_nsec(): bigint; get_stacktrace(buf: number, size: number): void; spawn_worker( fn: number, arg: number, comm: number, comm_len: number, user_memory: number, ): number; run_on_main(fn: number, arg: number): void; }; user: { compile_begin(size: number): number; compile_write(buf: number, offset: number, size: number): number; compile_end(maximum_memory_pages: number): number; compile_abort(): void; instantiate(fresh_memory: number): void; call(): void; switch_entry(fn: UserWord, arg: UserWord): void; call_signal_handler(fn: UserWord, sig: number): void; /** Abandons the user stack after an execve replaced the instance. */ unwind(): never; call_siginfo_handler(trampoline: UserWord, fn: UserWord, sig: number): number; read(to: number, from: number, n: number): number; write(to: number, from: number, n: number): number; write_zeroes(to: number, n: number): number; futex_atomic_op(oldval: number, uaddr: number, op: number, oparg: number): number; futex_atomic_cmpxchg( oldval: number, uaddr: number, expected: number, replacement: number, ): number; }; virtio: { set_features(dev: number, features: bigint): void;
setup(dev: number, config_irq: number, config_addr: number, config_len: number): void; /** `done` is a guest word set to 1 once the device has finished with guest memory. */ reset(dev: number, done: number): void;
enable_vring(dev: number, vq: number, size: number, desc_addr: number, irq: number): void; disable_vring(dev: number, vq: number): void;
notify(dev: number, vq: number): void; };}
export const HALT_KERNEL = Symbol("halt kernel");
export function kernel_imports({ is_worker, memory, spawn_worker, boot_console_write, boot_console_close, terminate_machine, run_on_main, get_user_context, worker_exit,}: { is_worker: boolean; memory: WebAssembly.Memory; spawn_worker: ( fn: number, arg: number, name: string, user: UserContext | null, copy_user_memory: boolean, ) => number; boot_console_write: (message: ArrayBuffer) => void; boot_console_close: () => void; terminate_machine: (reason: MachineTerminationReason) => void; run_on_main: (fn: number, arg: number) => void; get_user_context: () => UserContext | null; /** Reports that this worker's kernel thread halted and the worker is closing. */ worker_exit: () => void;}): Imports["kernel"] { return { breakpoint: () => { debugger; }, halt_worker: () => { if (!is_worker) throw new Error("Halt called in main thread"); // Messages posted after platform.quit() are not guaranteed to arrive. worker_exit(); platform.quit(); throw HALT_KERNEL; }, terminate_machine: (reason) => { if (!is_worker) { throw new Error("Machine termination called in main thread"); } terminate_machine(reason); throw HALT_KERNEL; },
boot_console_write: (msg, len) => { const address = msg >>> 0; const length = len >>> 0; boot_console_write(new Uint8Array(memory.buffer, address, length).slice().buffer); }, boot_console_close,
return_address: (_level) => { return 0; },
get_now_nsec: () => { /* The more straightforward way to do this is `BigInt(Math.round(performance.now() * 1_000_000))`. Below is semantically identical but has less floating point inaccuracy. `performance.now()` has 5μs precision in the browser. In server runtimes it has full nanosecond precision, but this code rounds to the same 5μs precision. */ return BigInt(Math.round((performance.now() + performance.timeOrigin) * 200)) * 5000n; },
get_stacktrace: (buf, size) => { const address = buf >>> 0; const capacity = size >>> 0; // 5 lines: strip Error, strip 4 common lines of stack const trace = new TextEncoder().encode(new Error().stack?.split("\n").slice(5).join("\n")); if (trace.byteLength > capacity && capacity >= 3) { /// 46 = "." trace[capacity - 1] = 46; trace[capacity - 2] = 46; trace[capacity - 3] = 46; } new Uint8Array(memory.buffer).set(trace.subarray(0, capacity), address); },
spawn_worker: (fn, arg, comm, comm_len, user_memory) => { const comm_address = comm >>> 0; const comm_length = comm_len >>> 0; const name = new TextDecoder().decode( new Uint8Array(memory.buffer, comm_address, comm_length).slice(), // copy to transfer to non-shared backing ); let user: UserContext | null = null; let copy_user_memory = false; if (user_memory !== WASM_USER_MEMORY_NONE) { const context = get_user_context(); if (!context) return -22; // invalid argument
switch (user_memory) { case WASM_USER_MEMORY_SHARE: user = context; break; case WASM_USER_MEMORY_COPY: user = context; copy_user_memory = true; break; default: return -22; // invalid argument } } return spawn_worker(fn, arg, name, user, copy_user_memory); },
run_on_main, };}