#7037·wasmer

Cranelift: host-thrown exception not caught by try_table, "uncaught exception"

Author: khagankhanCreated Sep 25, 2026Updated Sep 29, 2026

Describe the bug

With the Cranelift backend, a WebAssembly exception thrown by a host (imported) function is never delivered to the module's exception handlers. The host throws it the way the API documents it:

rust
let exn = Exception::new(&mut store, &tag, &[Value::I32(77)]);
Err(RuntimeError::exception(&store, exn))  // doc: "the exception will be thrown in the WebAssembly code instead of the usual trapping"

and the module calls the import inside try_table, but neither catch $tag nor catch_all runs: the exception unwinds past every handler and Function::call returns a RuntimeError whose message is uncaught exception. This happens whether the tag was created on the host with Tag::new and imported by the module, or defined and exported by the module and fetched by the host after instantiation. Exceptions thrown by throw inside the module are caught correctly with Cranelift; only host-thrown ones are lost. The LLVM backend behaves as expected in all cases.

bash
$ wasmer -vV; rustc -vV
wasmer 7.4.2
binary: wasmer-cli
commit-hash: ca719eb81fa450a06bcd6f0dd46e0b7cca15d7a9
commit-date: 2026-09-25
host: x86_64-unknown-linux-gnu
CPU flags: sse2 sse3 ssse3 sse4.1 sse4.2 popcnt avx bmi bmi2 avx2 lzcnt fma
runtimes: Singlepass, Cranelift, LLVM
features: wasix
rustc 1.98.1 (48a229cea 2026-09-01)
binary: rustc
commit-hash: 48a229ceaefd4985c50990b14116b6d856af0985
commit-date: 2026-09-01
host: x86_64-unknown-linux-gnu
release: 1.98.1
LLVM version: 22.1.8

Reproduced with the wasmer crate 7.4.2 from crates.io and with main at ca719eb (2026-09-24), Cranelift backend, Linux x86_64.

Steps to reproduce

  1. Create a crate with the Cargo.toml and src/main.rs below (the program is self-contained: the module is inline WAT, the only dependency is wasmer with default features, i.e. the sys backend with Cranelift).
  2. cargo run --release
  3. See the output; the program prints what it got against what it expected and exits 1 when the bug is present.
  4. Optional, for comparison with the LLVM backend: cargo run --release --features llvm with WASMER_BACKEND=llvm in the environment (needs LLVM 22 and llvm-sys). The LLVM backend gives the expected results.
Cargo.toml
toml
[package]
name = "wasmer-repro"
version = "0.1.0"
edition = "2021"

[dependencies]
wasmer = "7.4.2"

[features]
llvm = ["wasmer/llvm"]   # optional: WASMER_BACKEND=llvm for comparison
src/main.rs
rust
//! A WebAssembly exception thrown by a host function (Exception::new +
//! RuntimeError::exception, with a Tag created on the host and imported by
//! the module) is not delivered to the module's handlers with the Cranelift
//! backend: neither `catch $tag` nor `catch_all` runs, and the call returns
//! "uncaught exception".  Expected (and what the LLVM backend and other
//! engines do): `catch_tag` returns the payload 77, `catch_all` returns 1.

use wasmer::sys::{Cranelift, EngineBuilder, Features};
use wasmer::*;

const WAT: &str = r#"
(module
  (import "env" "throw" (func $throw))
  (import "env" "tag" (tag $tag (param i32)))
  ;; case B: the module defines and exports the tag, the host throws it
  (import "env" "throw_own" (func $throw_own))
  (tag $own (export "own") (param i32))
  (func (export "catch_own") (result i32)
    (block $h (result i32)
      (try_table (result i32) (catch $own $h)
        (call $throw_own)
        (i32.const -1))))
  ;; the handler receives the payload; -1 if the call returned normally
  (func (export "catch_tag") (result i32)
    (block $h (result i32)
      (try_table (result i32) (catch $tag $h)
        (call $throw)
        (i32.const -1))))
  ;; 1 if catch_all ran, -1 if the call returned normally
  (func (export "catch_all") (result i32)
    (block $h
      (try_table (catch_all $h) (call $throw))
      (return (i32.const -1)))
    (i32.const 1)))
"#;


fn engine() -> Engine {
    let mut features = Features::default();
    features.exceptions(true);
    let backend = std::env::var("WASMER_BACKEND").unwrap_or_else(|_| "cranelift".to_string());
    println!("backend: {}", backend);
    match backend.as_str() {
        "cranelift" => EngineBuilder::new(Cranelift::default()).set_features(Some(features)).engine().into(),
        #[cfg(feature = "llvm")]
        "llvm" => EngineBuilder::new(wasmer::sys::LLVM::default()).set_features(Some(features)).engine().into(),
        other => panic!("backend {} not available (build with --features llvm for llvm)", other),
    }
}

fn main() {
    let mut store = Store::new(engine());
    let module = Module::new(&store, WAT).expect("module");

    // case A: a tag created on the host and imported by the module
    let tag = Tag::new(&mut store, [Type::I32]);
    let env = FunctionEnv::new(&mut store, tag.clone());
    let throw = Function::new_with_env(&mut store, &env, FunctionType::new([], []), |mut env, _args| {
        let tag = env.data().clone();
        let (_, mut store) = env.data_and_store_mut();
        let exn = Exception::new(&mut store, &tag, &[Value::I32(77)]);
        Err(RuntimeError::exception(&store, exn))
    });
    // case B: the module's own exported tag, fetched after instantiation
    let own: std::sync::Arc<std::sync::Mutex<Option<Tag>>> = Default::default();
    let env_b = FunctionEnv::new(&mut store, own.clone());
    let throw_own = Function::new_with_env(&mut store, &env_b, FunctionType::new([], []), |mut env, _args| {
        let tag = env.data().lock().unwrap().clone().expect("tag set after instantiation");
        let (_, mut store) = env.data_and_store_mut();
        let exn = Exception::new(&mut store, &tag, &[Value::I32(88)]);
        Err(RuntimeError::exception(&store, exn))
    });
    let mut imports = Imports::new();
    imports.define("env", "throw", throw);
    imports.define("env", "tag", tag);
    imports.define("env", "throw_own", throw_own);
    let instance = Instance::new(&mut store, &module, &imports).expect("instantiate");
    if let Some(Extern::Tag(t)) = instance.exports.get_extern("own") { *own.lock().unwrap() = Some(t.clone()); }

    let mut ok = true;
    for (name, expected) in [("catch_tag", 77), ("catch_all", 1), ("catch_own", 88)] {
        let f = instance.exports.get_function(name).unwrap();
        match f.call(&mut store, &[]) {
            Ok(v) => {
                let got = match v[0] { Value::I32(x) => x, _ => unreachable!() };
                println!("{}: returned {} (expected {}) {}", name, got, expected, if got == expected { "OK" } else { "WRONG" });
                ok &= got == expected;
            }
            Err(e) => {
                println!("{}: error {:?} (expected a return of {}) WRONG", name, e.message(), expected);
                ok = false;
            }
        }
    }
    std::process::exit(if ok { 0 } else { 1 });
}

Expected behavior

The three exported functions catch the host-thrown exception and return:

backend: llvm
catch_tag: returned 77 (expected 77) OK
catch_all: returned 1 (expected 1) OK
catch_own: returned 88 (expected 88) OK

This is what the LLVM backend prints (WASMER_BACKEND=llvm), and wasmtime and the JavaScript engines (SpiderMonkey, JavaScriptCore) catch the same host-thrown exception on the same module.

Actual behavior

backend: cranelift
catch_tag: error "uncaught exception" (expected a return of 77) WRONG
catch_all: error "uncaught exception" (expected a return of 1) WRONG
catch_own: error "uncaught exception" (expected a return of 88) WRONG

Same output with wasmer 7.4.2 from crates.io and with main at ca719eb.

Additional context

  • catch_tag: the tag is created on the host (Tag::new(&mut store, [Type::I32])) and imported by the module as env.tag; the handler receives the i32 payload.
  • catch_all: same import, caught by catch_all.
  • catch_own: the module declares (tag $own (export "own") (param i32)); the host fetches it from the exports after instantiation and throws it from a second import.
  • The engine is built with EngineBuilder::new(Cranelift::default()).set_features(Some(features)) with features.exceptions(true).