Skip to content
You are reading next, which describes the unreleased compiler. Khora is unstable before v1: syntax, standard-library APIs and behavior may change. The current release is documented under v0.4.

Plugins

A plugin is Khora code that a host program loads and runs without trusting it. The plugin is compiled to a WebAssembly module. The host runs the module inside a sandbox and gives each loaded instance its own grant: the directories it may read, the environment variables it may see, a memory cap and a deadline. Anything outside the grant does not exist for the plugin.

State: experimental. Building a plugin works and is tested. A Khora program cannot host one. The only host is a Rust library in the Khora repository, used by the toolchain’s own tests, and a Khora program has no way to link it in. A Rust program can depend on it.

A plugin is a library package. A host calls its pub extern fns by name, passing Ints and getting an Int back (or nothing, which arrives as 0):

module price::lib;
pub extern fn price(units: Int, scale: Int) -> Int { units * scale }

khora build --plugin writes build/<name>.wasm. The flag is short for --target wasm32-khora-plugin, which you can also select with KHORA_TARGET=wasm32-khora-plugin. Only the package’s own pub extern fns are exported; a dependency’s are linked in and callable from the plugin, but not by the host.

Nothing in a package’s manifest says it is a plugin, so khora check and the editor check it as a native library unless KHORA_TARGET says otherwise. Set KHORA_TARGET=plugin (or the full triple) for them to use the plugin’s standard library and its forging rule, which are what khora build --plugin applies.

A plugin uses the ordinary capabilities. FsRead, Env and Log are the same types as in a native build, so a library that takes them as capabilities builds as a plugin. Two things it may call are missing or behave differently: Log::json and Log::json_using do not exist, because a plugin has no clock to stamp lines with, and read_dir and is_dir compile but raise Failed when called:

module reader::lib;
import std::core::{print};
import std::fs::{FsRead, IoError, read_text};
fn first_line(path: String) -> String
with { reads: FsRead }
{
let text = read_text(path)! catch {
IoError::Denied(p) => return "not granted: ${p}",
IoError::NotFound(p) => return "no such file: ${p}",
IoError::Failed(p) => return "could not read ${p}",
};
text
}
pub extern fn show() -> Int {
with { reads: FsRead::real() } { print(first_line("config/settings.txt")) };
0
}

What is different inside a plugin:

  • FsRead::real() and Env::real() ask the host. They carry no authority of their own. A path is readable only if it lies beneath a directory the host granted to this instance, and is named starting with the name the host gave that directory (config/ above). A variable is visible only if the host put it into this instance’s map. Anything else raises Denied.
  • Only an export may make a capability. A plugin has no main, so its pub extern fns are its entry points, and only they may call FsRead::real() and the like. A helper takes the capability from its caller, so its signature says what it reaches, as in any dependency. An export is an entry point only when the host calls it. Calling an export that makes a capability from inside the plugin, whether from a helper or from another export, is refused, because the caller would reach the capability with no row saying so. Importing it under another name (import p::exports::{reader as harmless}) is the same function and is refused the same way. An export that makes none may be called from anywhere. A const holding a constructor, such as const MAKE = FsRead::real;, is refused even if only an export uses it, because a constant is checked as code outside every function. Write FsRead::real() in the export itself.
  • Reading only. FsWrite::real() refuses every write with Denied. Listing a directory and asking whether a path is a directory raise Failed. A plugin has no command-line arguments.
  • No network and no processes. std::net::http, std::net::socket, std::net::tls and std::process do not exist on this target, so a plugin that imports them does not build. An extern fn of a plugin’s own, such as open, does not link. Only the standard library declares the functions through which a plugin reaches its host.
  • print and a trap’s message go to the host, which decides where they end up.
  • One thread of control, as for any WebAssembly module: no Fiber::spawn, and nothing can be canceled from inside.

Each refusal happens in a different place, and none depends on the plugin’s author:

  • At build: a module that has no plugin version, such as std::net::http, cannot be imported.
  • At link: the only undefined symbols the linker allows are the host’s functions.
  • When the host loads the module: an import that is not one of the host’s functions, or that has a different signature, is refused before any plugin code runs. This covers modules built by hand without the Khora toolchain, including ones that import WASI. A module with a 64-bit memory is refused too, so every plugin’s memory is bounded at 4 GiB by its type.
  • On every call into the host: the grant is checked. A path is opened beneath the granted directory, so .., an absolute path, and a symbolic link that leads out are all refused because of where the file actually is, not how its name is spelled. An absolute symbolic link is refused even when it points back inside the grant. Only a regular file is read: a FIFO, device or directory under a granted name answers Failed at once, without waiting for a writer. Every pointer and length the plugin passes is checked against the plugin’s own memory before anything is read or written.
  • For resources: a memory cap and a wall-clock deadline are required in every grant. A plugin that exceeds either is stopped, and the host gets an error. The deadline holds for as long as the plugin lives, even after the host that loaded it is dropped. A table, the other memory a plugin can ask for, is held to 10,000 entries: a larger initial table is refused when the plugin is loaded, and growing past that answers the plugin with a failure. A loaded plugin reserves address space for the memory it has and 64 MiB more, not for all it may grow to, so many fit in a process with an address-space limit. When this process cannot provide the memory a plugin starts with, loading it fails with an error that says so, as distinct from the plugin being refused. Running out partway through a call is different: the plugin is told its memory could not grow, as at its cap, and the host sees whatever the plugin does next, which is usually a trap.

A trap in a plugin is an error for the host to handle; it does not end the host process. An out-of-bounds index, for example, ends a native Khora program with status 134. In a plugin, the call returns an error that carries the trap’s message. The instance is then spent and refuses further calls, because the trapped call’s objects were never freed. Load a fresh instance.

An adversarial plugin runs inside wasmtime’s linear-memory sandbox with no ambient imports. It can corrupt only its own memory. It reaches the outside world only through the host functions, and every pointer it passes is bounds-checked against its own memory.

What it can still do:

  • Burn CPU until its deadline. The deadline is checked every 10 ms, so a call given a deadline of D runs for at least D and can overshoot it by up to one tick: at most D plus 10 ms.
  • Allocate up to its memory cap, plus up to four tables of 10,000 entries (80 KB each) outside it.
  • Make as many granted host calls as it likes within its deadline. This includes reading large granted files over and over. Time spent inside a host call is not interrupted, so each host call is bounded on its own. It opens without blocking, reads at most what the buffer holds, straight into the plugin’s own memory, and touches nothing but the grant’s directories and map. So one call can run past the deadline by as long as reading one buffer-full takes, about a millisecond per MiB from a local disk. A slow file system, such as a hung network mount under a granted directory, holds the call for as long as the file system does.
  • Cost the host its compile time. Compiling a module happens when the host loads it, before the grant applies: neither the memory cap nor the deadline covers it, and Cranelift’s time and memory grow with the module’s size (about 2 s and 120 MiB for a 29 MiB module). A host loading modules it did not build should cap their size before loading them.
  • Observe timing. It can measure wall-clock time through the host’s own latency, and use cache and Spectre-class side channels within the process. The sandbox mitigates these and does not exclude them; a separate process is stronger isolation.
  • Return wrong or hostile answers. The host must treat everything a plugin returns as untrusted input.
  • Do anything its grant allows, including passing on what it can read: a granted file is a file it can leak through its return value.

The trusted computing base is:

  • wasmtime and Cranelift;
  • the host’s grant checks and pointer checks;
  • cap-std’s beneath-the-directory path resolution.

Not goals:

  • protection against timing side channels;
  • a limit on host-call I/O within a deadline;
  • any guarantee about what a plugin returns.
  • A plugin module is large. It carries the Khora runtime, so a debug build of a one-function plugin is about 2.4 MB.
  • Every access a plugin makes to its own memory is bounds-checked explicitly. The host installs no signal handlers. A Khora program uses those handlers for its fibers’ stack guard pages, so the sandbox leaves them alone and checks each access instead. As a result, Cranelift’s Spectre mitigation on memory accesses is off. Loading a plugin and calling it leave the thread’s alternate signal stack as they found it.
  • A plugin’s memory moves when it grows past 64 MiB beyond its last move. Growth within that room is free. Past it, growth maps a new region with another 64 MiB of room and copies the old memory, once per 64 MiB of growth.
  • A host carries wasmtime, which is many megabytes of machine code. A program that hosts no plugins does not include it.

Khora 0.4.0built fromee766ae43388on 2026-11-26 22:56ZVersioningSecurityContributingReleasesSource