Skip to content

feat: provide a sandboxed subset of WASI to Wasm plugins - #1303

Merged
dsherret merged 4 commits into
mainfrom
feat-wasm-wasi-imports
Oct 7, 2026
Merged

dsherret merged 4 commits into
mainfrom
feat-wasm-wasi-imports

Conversation

@dsherret

@dsherret dsherret commented Oct 4, 2026 •

Copy link
Copy Markdown
Member

Lets Wasm plugins built with toolchains that need a WASI libc (ex. .NET NativeAOT-LLVM, Go) be loaded, without giving them a way out of the sandbox. The motivating case is compiling dprint-plugin-roslyn to Wasm instead of shipping it as a process plugin.

What a plugin gets

A hand-written subset of wasi_snapshot_preview1 (no wasmtime-wasi, no preopens):

Function Behaviour
clock_time_get, clock_res_get Realtime and monotonic clocks, rounded down to 1ms
random_get Random bytes from the host's CSPRNG
fd_write fd 1 and 2 only. The text is logged the same way env.fd_write already was
proc_exit Traps the instance. Never exits the process
args_*, environ_* Always empty
fd_read Stdin is always at EOF. The real stdin is never read
fd_fdstat_get, fd_filestat_get, fd_seek, fd_close Constant answers for fds 0-2, EBADF otherwise
fd_prestat_get, fd_prestat_dir_name, fd_advise, fd_pread, fd_readdir, path_open, path_filestat_get, path_readlink, path_unlink_file Always EBADF. Arguments are never looked at
poll_oneoff Sleeps the plugin's thread for clock subscriptions. Stdin/stdout/stderr subscriptions are always ready, and are reported along with any clocks that have already elapsed
Any other wasi_snapshot_preview1 function a module imports, including the remaining file system functions (ex. path_create_directory, path_rename, fd_pwrite) ENOSYS (or a trap when it doesn't return an errno)

So the only things a plugin can read from the host are the time and random bytes, the only thing it can write is log text, and it can sleep its own thread. There is no file system, environment, process, or network access.

Other changes

  • _initialize is called after instantiation when a module exports it, which WASI reactors need before any other export is called.
  • A module built as a WASI command (exports _start without _initialize) fails to load with an error saying to build it as a reactor.
  • Imports from any module other than wasi_snapshot_preview1 are still strict. A plugin importing an unknown dprint or env function fails to load like before.
  • env.fd_write now shares the WASI implementation. This replaces the old one, which panicked when called from a start function, allocated up to 4GiB before bounds checking, and had unchecked pointer arithmetic.
  • Plugin output is now sanitized before being logged: control characters other than \n and \t are replaced, so a plugin can't send escape sequences to the terminal (this was possible before through env.fd_write). A single write is capped at 1024 buffers and 64KiB, and is logged once per call instead of once per buffer.

Things worth a look

  • Plugins could not observe time or randomness before, so their output was a pure function of their input. With this a plugin can behave differently depending on the date or at random. That seems inherent to supporting these runtimes, but it is a change to what a Wasm plugin can do.
  • The clocks are coarsened to 1ms so plugins aren't handed a high resolution timer. Nothing a formatter does should need more.
  • A sleep isn't interrupted by cancellation. A plugin could already loop forever without checking for cancellation, so this isn't a new way to hang.

Tests

Unit tests in wasi.rs drive each function from small WAT modules (wat is a new dev-dependency): missing or non-memory memory exports, out of bounds and wrapping pointers, fd validation, struct layouts, the write limits, output sanitization, sleeping, and that unknown imports from other modules still fail. load_instance.rs covers _initialize and the WASI command error. Also ran dprint-plugin-roslyn compiled to wasm32-wasip1 through dprint fmt/check/output-resolved-config locally.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Mixed polling can starve expired timers, and supported WASI functions are unavailable to schema-v3 plugins.

Review effort: Balanced
Findings: 3 Medium severity · 1 Low severity

Open (4)
What changed in this PR

Adds a sandboxed WASI Preview 1 subset for Wasm plugins and initializes WASI reactors safely.

Changes:

  • Implements restricted clocks, randomness, streams, polling, and filesystem stubs.
  • Adds reactor initialization and command-module rejection.
  • Adds comprehensive WAT-based tests and output sanitization.
File Description
plugin.rs Transfers linker ownership during loading.
load_instance.rs Adds WASI fallbacks and reactor initialization.
instance/​wasi.rs Implements and tests sandboxed WASI functions.
instance/​v4.rs Connects WASI and shared output logging.
instance/​mod.rs Exposes WASI and routes plugin output.
compile.rs Updates linker ownership during compilation.
Cargo.toml Adds the WAT test dependency.
Cargo.lock Locks the new dependency graph.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +74 to +75
wasi::add_wasi_imports(linker)?;
linker.func_wrap("env", "fd_write", wasi::fd_write)?;
Comment on lines +128 to +132
linker.func_new(MODULE, import.name(), ty, move |_, _, results| {
if returns_errno {
results[0] = Val::I32(ERRNO_NOSYS);
Ok(())
} else {
Comment on lines +315 to +317
if !ready_events.is_empty() {
return Ok((ready_events, None));
}
Comment on lines +384 to +388
fn get_memory(caller: &mut Caller<'_, WasmHostState>) -> Option<Memory> {
match caller.get_export("memory") {
Some(Extern::Memory(memory)) => Some(memory),
_ => None,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants