Skip to content

Conversation

@shogochiai
Copy link

@shogochiai shogochiai commented Jan 16, 2026

Problem

When using ic-wasm instrument on a canister WASM compiled with Emscripten, the resulting instrumented WASM contains WASI imports that IC rejects at install time:

Error: Wasm module has an invalid import section.
Module imports function 'fd_close' from 'wasi_snapshot_preview1'
that is not exported by the runtime.

Use Case

For languages that compile through Emscripten/C (e.g., Idris2), WASI imports are introduced by the toolchain, not by user code. This flag provides a practical workaround until native WASM backends become available.

Solution

Add --stub-wasi flag to the instrument subcommand that replaces WASI imports with local stub functions:

WASI Function Stub Behavior
fd_close Returns 0 (success)
fd_write Writes 0 to nwritten, returns 0
fd_read Writes 0 to nread, returns 0
fd_seek Writes 0 to newoffset, returns 0
environ_sizes_get Writes 0 to both params, returns 0
environ_get Returns 0
proc_exit Traps (unreachable)
Others Returns 0

Usage

ic-wasm canister.wasm -o instrumented.wasm instrument --stub-wasi

⚠️ Important Note

This is a workaround for edge cases (Emscripten, wasi-sdk).

The recommended approach is to build without WASI imports:

  • Rust: Use wasm32-unknown-unknown target with ic-cdk
  • C/C++: Configure toolchain to avoid WASI dependencies

Stub functions return success (0), which may hide real failures if your code actually depends on WASI functionality.

Test plan

  • Added stub_wasi test that verifies WASI imports are removed
  • All existing tests pass
  • cargo clippy and cargo fmt pass

🤖 Generated with Claude Code

…tubs

Problem:
When using `ic-wasm instrument` on a canister WASM compiled with Emscripten,
the resulting instrumented WASM contains WASI imports (fd_close, fd_write,
fd_seek, etc.) that IC rejects at install time:

  Error: Wasm module has an invalid import section.
  Module imports function 'fd_close' from 'wasi_snapshot_preview1'
  that is not exported by the runtime.

Solution:
Add `--stub-wasi` flag that replaces WASI imports with local stub functions
returning 0 (success) or trapping for proc_exit. This allows Emscripten-
generated WASMs to be instrumented and deployed to IC.

Usage:
  ic-wasm canister.wasm -o out.wasm instrument --stub-wasi
@cla-idx-bot
Copy link

cla-idx-bot bot commented Jan 16, 2026

Dear @shogochiai,

In order to potentially merge your code in this open-source repository and therefore proceed with your contribution, we need to have your approval on DFINITY's CLA.

If you decide to agree with it, please visit this issue and read the instructions there. Once you have signed it, re-trigger the workflow on this PR to see if your code can be merged.

— The DFINITY Foundation

@shogochiai shogochiai closed this Jan 16, 2026
@shogochiai shogochiai reopened this Jan 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant