Repository navigation
feat: provide a sandboxed subset of WASI to Wasm plugins - #1303
Merged
Merged
Conversation
There was a problem hiding this comment.
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
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, | ||
| } |
This was referenced Oct 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


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(nowasmtime-wasi, no preopens):clock_time_get,clock_res_getrandom_getfd_writeenv.fd_writealready wasproc_exitargs_*,environ_*fd_readfd_fdstat_get,fd_filestat_get,fd_seek,fd_closeEBADFotherwisefd_prestat_get,fd_prestat_dir_name,fd_advise,fd_pread,fd_readdir,path_open,path_filestat_get,path_readlink,path_unlink_fileEBADF. Arguments are never looked atpoll_oneoffwasi_snapshot_preview1function 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
_initializeis called after instantiation when a module exports it, which WASI reactors need before any other export is called._startwithout_initialize) fails to load with an error saying to build it as a reactor.wasi_snapshot_preview1are still strict. A plugin importing an unknowndprintorenvfunction fails to load like before.env.fd_writenow shares the WASI implementation. This replaces the old one, which panicked when called from astartfunction, allocated up to 4GiB before bounds checking, and had unchecked pointer arithmetic.\nand\tare replaced, so a plugin can't send escape sequences to the terminal (this was possible before throughenv.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
Tests
Unit tests in
wasi.rsdrive each function from small WAT modules (watis a new dev-dependency): missing or non-memorymemoryexports, 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.rscovers_initializeand the WASI command error. Also ran dprint-plugin-roslyn compiled towasm32-wasip1throughdprint fmt/check/output-resolved-configlocally.