Skip to content

WebAssembly hosts (X2) ​

Ready

The hub recognises the services this package fits and proposes the compose part of the change, if any; the source change stays by hand, with the steps and examples in the proposal's review. Nothing changes until a player approves it (Hook proposals). The cheat sheets have the steps by hand.

X2 has no code of its own: a host gets the hook through the Rust (R1) or C (C1) package, and a sample wasmtime host shows the host-specific steps.

A WebAssembly service runs inside a host process: wasmtime serve, Spin, WasmEdge, wasmer, or a custom host that embeds one of their runtimes. The guest has no syscalls of its own. Every socket, file and process the guest touches goes through host functions (WASI) running in the host process, and the kernel attributes it to the host. So "which request" means "which request of the host", and only the host can say. A hook therefore always goes into the host, never into the guest module.

Covers ​

HostVersionsServing modelWhat the kernel links without a hook
wasmtime serve (wasi:http components)wasmtime 14+tokio + hyper, one component instance per request, many in flight on a thread poolnothing per request (event loop)
Spin (Fermyon)2.x, 3.xwasmtime underneath, tokio/hyper trigger, instance per requestnothing per request
WasmEdge0.13+its own HTTP plugins or a host app; often one instance per connection in a threadper thread, if one client socket per thread
wasmer4.x, 5.xWASIX: guest sockets are real host sockets, often thread per connectionper thread, if one client socket per thread
Custom embeddersany (sample: wasmtime 25 in a hyper 1 / tokio host, Rust 1.x on musl)whatever the host does (typically tokio + hyper with a pooled Store per request)as for any Rust/C/Go server

WASI preview 1 guests that use sock_accept (WASIX, WasmEdge sockets) behave like a single-threaded native server inside the host thread. The C1 rules apply to the host thread: the kernel links it exactly only while that thread holds one client socket.

Process spawning: WASI has no process API. A guest can only start processes through a host function that the host chose to expose. Without one, a "spawned process" from a WebAssembly service means the host was compromised, which the kernel reports anyway.

How it gets in ​

Source change in the host, only when the host is code the team builds:

  • A Rust host (custom wasmtime/wasmer embedding, a fork of wasmtime serve): vendor R1's crate as for any tokio/hyper service (copy /usr/local/lib/vulnboxd/trace/rust to vendor/vbxtrace; the build stays offline). Then:

    1. Request boundaries. Wrap the hyper service with R1's Trace::with_conn(service, ConnInfo::from_tokio(&stream)), or use vbxtrace_hook::axum::trace(app) for axum. The request context is then the task-local of the request's task, with exact per-connection request indexes.
    2. The guest call. Hosts usually run the guest somewhere the task-local does not follow:
      • On tokio's blocking pool: tokio::task::spawn_blocking(f) becomes vbxtrace_hook::tokio::spawn_blocking(f). It emits context_enter/context_exit on the blocking thread and sets the context for everything inside.
      • On another task: tokio::spawn becomes vbxtrace_hook::tokio::spawn. Any other future becomes vbxtrace_hook::bind(fut); this covers wasmtime-wasi-http's spawned guest call in the ProxyPre::instantiate_async path.
      • wasmtime's async mode (call_async) runs the guest on fibers inside the calling task, so it needs nothing.
    3. The host functions. These are what the guest imports (Linker::func_wrap, custom WASI-style functions), and they are where a guest's effects actually happen. Swap the std/tokio calls for R1's traced wrappers:
      • Command::spawn → spawn_traced (vbxtrace_hook::process::CommandExt) gives spawned;
      • std::fs::* → vbxtrace_hook::fs::* gives opened;
      • TcpStream::connect → vbxtrace_hook::net::connect gives connected.

    The diff, as the hub's template (hooks/wasm/x2/proposals/wasmtime-host.toml) proposes it:

    diff
     [dependencies]
     wasmtime = "25"
    +vbxtrace-hook = { path = "vendor/vbxtrace/vbxtrace-hook", features = ["hyper1", "tokio"] }
    diff
     fn host_save(_: Caller<'_, ()>) -> i32 {
    -    if std::fs::write("/data/out", "...").is_ok() { 0 } else { -1 }
    +    if vbxtrace_hook::fs::write("/data/out", "...").is_ok() { 0 } else { -1 }
     }
     ...
    -    let result = tokio::task::spawn_blocking(work).await;
    +    let result = vbxtrace_hook::tokio::spawn_blocking(work).await;
     ...
             let (stream, _) = listener.accept().await.unwrap();
    +        let conn = vbxtrace_hook::ConnInfo::from_tokio(&stream);
     ...
                 let svc = service_fn(move |req| handler(host.clone(), req));
    +            let svc = vbxtrace_hook::Trace::with_conn(svc, conn);
  • A C/C++ host (WasmEdge C API, wasmer C API, wasm3, WAMR): use C1's vbxh_begin_fd/vbxh_end around each request the host dispatches to the guest. Call vbxh_opened/vbxh_connected_fd in the host functions that open files or sockets.

  • Off-the-shelf hosts (wasmtime serve, Spin, the WasmEdge CLI): there is no source to change in the service directory. The options are rebuilding the host from source with the hook (a fork, which the hub will not propose), or accepting kernel-only linking. Upstream wasmtime and Spin already emit tracing spans per request. A future R1 tracing subscriber bridge could turn those spans into request_begin/request_end without a fork. Not built yet.

The hub proposes the Rust host template when a Rust service depends on wasmtime, wasmtime-wasi, wasmtime-wasi-http, wasmer or wasmer-wasix. It ranks above R1's generic templates. A C host is detected as C/C++ (C1).

What it records ​

EventEmitted fromJoin key
request_begin / request_endthe host's HTTP layer (R1 Trace around the hyper service / C dispatch)peer address and port, request index
context_enter / context_exitthe guest call on another thread (R1 spawn_blocking)the thread
spawnedhost functions that start processes (R1 spawn_traced)child pid
openedhost functions that open files (R1 fs wrappers)pid + fd
connectedhost functions that connect (R1 net::connect)local port

Context following ​

The context lives in the host. With R1, it is a tokio task-local that follows .await inside the request's task. A guest call that the host spawns into a new task needs bind, as described above. wasmtime's async support runs guest code on fibers within that same task, so host functions called from the guest see the task-local. The fiber switch is invisible to tokio.

With C1 hosts, the thread-current slot (vbxh_set_current) works when the host runs one guest call per thread at a time.

Known gaps ​

  • wasmtime-wasi's own I/O (wasi:filesystem, wasi:sockets as implemented by wasmtime-wasi) cannot be wrapped without forking it, so its files and sockets get no hook events. It may also move file I/O to its own blocking tasks, which carry no context. Effects that matter should go through host functions the host defines itself, which R1 reports exactly. This is untested with wasmtime-wasi and is not part of the sample.
  • wasi:http components (wasmtime-wasi-http, cargo component guests) are not sampled. The recipe (R1 Trace around the hyper service, bind around the spawned guest call) is the same as for the sample, but it is unverified.
  • wasmer, WasmEdge, Spin: no samples. wasmer and WasmEdge embeddings follow the same recipe (R1 for Rust, C1 for their C APIs). Spin and the WasmEdge CLI are off-the-shelf hosts (below).
  • musl: wasmtime on musl prints libunwind: __unw_add_dynamic_fde: bad fde at start-up (unwind-info registration for JIT code); harmless and unrelated to the hook.
  • Off-the-shelf hosts stay kernel-only (see How it gets in).
  • Pooling allocator / instance reuse: one Store serving several requests in turn is fine as long as each request's work runs under its own context. The context belongs to the host task, not the instance.

Safety ​

The same as the package the host uses: R1 (probes inlined, nothing allocated, nothing that can fail; the wrappers return the std results untouched) or C1 (guarded calls, off after 5 internal errors, VBXTRACE=0). Guests cannot reach the hook: it lives entirely in host code, outside the sandbox.

The sample hooks/wasm/x2/samples/wasmtime-host is a hyper 1 server embedding wasmtime 25 on musl.

  • Each request instantiates a fresh guest module (a WAT text module, one Store per request) on tokio's blocking pool.
  • The guest calls the host functions host.spawn, host.open and host.connect.
  • It runs the standard script: a keep-alive connection with two requests, then /spawn, /file and /connect.
ModeResult
events23 events match: exact request indexes (0, 1 on the keep-alive connection), context_enter/context_exit around every guest call, spawned / opened (flags=0x80241) / connected inside them
chaosresponses byte-identical
baseline (built without the hook)responses byte-identical

Overhead (p99 with the hook loaded and the sensor detached, and attached) has not been measured yet. It is planned for a later phase.

Turning it off ​

Set VBXTRACE=0 in the host's environment and restart it, or revert the change that added the hook; vulnboxd's rollback covers a hook that breaks a service.

Troubleshooting ​

  • Are the probes there? For a Rust host with R1 vendored, the probes are inside the host binary: readelf -n <host binary> lists vbxtrace notes. For a C host using the default C1 build, they are in /usr/local/lib/vulnboxd/trace/libvbxtrace-x86_64.so, mapped into the host (grep vbxtrace /proc/<pid>/maps).
  • Guest I/O not linked to requests: the guest call runs on a thread or task that lost the context. Use vbxtrace_hook::tokio::spawn_blocking / spawn, or wrap the future with bind. The I/O must go through a traced wrapper in the host function.
  • Which links did a flow get? The hub shows each link's method: hook:r1 or hook:c1 for hosts that embed a hook, kernel methods otherwise.