Stream: git-wasmtime

Topic: wasmtime / issue #15281 Guest debugging: `FrameHandle::is...


view this post on Zulip Wasmtime GitHub notifications bot (Nov 13 2026 at 22:22):

alexcrichton opened issue #15281:

This program:

use std::sync::{Arc, Mutex};
use std::task::{Context, Poll, Waker};
use wasmtime::component::{Component, Linker};
use wasmtime::*;

type Slot = Arc<Mutex<Option<FrameHandle>>>;

#[derive(Clone)]
struct OnTrap(Slot);

impl DebugHandler for OnTrap {
    type Data = ();
    async fn handle(&self, mut store: StoreContextMut<'_, ()>, event: DebugEvent<'_>) {
        if let DebugEvent::Trap(_) = event {
            *self.0.lock().unwrap() = store.debug_exit_frames().next();
        }
    }
}

fn main() -> Result<()> {
    let mut config = Config::new();
    config.guest_debug(true);
    let engine = Engine::new(&config)?;

    // A frame handle taken in a `DebugEvent::Trap` handler.
    let module = Module::new(
        &engine,
        r#"(module (func (export "f") (local i32) unreachable))"#,
    )?;
    let mut store = Store::new(&engine, ());
    let slot = Slot::default();
    store.set_debug_handler(OnTrap(slot.clone()));
    let instance = block_on(Instance::new_async(&mut store, &module, &[]))?;
    let f = instance.get_typed_func::<(), ()>(&mut store, "f")?;
    assert!(block_on(f.call_async(&mut store, ())).is_err());
    let frame = slot.lock().unwrap().take().unwrap();
    println!("trap: is_valid = {}", frame.is_valid(&mut store));
    println!("trap: local 0 = {:?}", frame.local(&mut store, 0));

    // A frame handle taken in a component host import.
    let component = Component::new(
        &engine,
        r#"(component
             (import "capture" (func $capture))
             (import "check" (func $check))
             (core func $capture (canon lower (func $capture)))
             (core func $check (canon lower (func $check)))
             (core module $m
               (import "" "capture" (func $capture))
               (import "" "check" (func $check))
               (func $victim (local i32) (call $capture))
               (func (export "run") (call $victim) (call $check)))
             (core instance $i (instantiate $m
               (with "" (instance
                 (export "capture" (func $capture))
                 (export "check" (func $check))))))
             (func (export "run") (canon lift (core func $i "run"))))"#,
    )?;
    let mut linker = Linker::<Option<FrameHandle>>::new(&engine);
    linker.root().func_wrap("capture", |mut store, (): ()| {
        let frame = store.debug_exit_frames().next();
        *store.data_mut() = frame;
        Ok(())
    })?;
    linker.root().func_wrap("check", |mut store, (): ()| {
        let frame = store.data_mut().take().unwrap();
        println!("component: is_valid = {}", frame.is_valid(&mut store));
        println!("component: local 0 = {:?}", frame.local(&mut store, 0));
        Ok(())
    })?;
    let mut store = Store::new(&engine, None);
    let instance = linker.instantiate(&mut store, &component)?;
    let run = instance.get_typed_func::<(), ()>(&mut store, "run")?;
    run.call(&mut store, ())?;
    Ok(())
}

fn block_on<F: Future>(f: F) -> F::Output {
    let mut f = std::pin::pin!(f);
    let mut cx = Context::from_waker(Waker::noop());
    loop {
        if let Poll::Ready(v) = f.as_mut().poll(&mut cx) {
            return v;
        }
    }
}

with wasmtime at bytecodealliance/wasmtime@ffbf0e0c13, prints:

$ cargo run
trap: is_valid = true
trap: local 0 = Ok(I32(4))
component: is_valid = true
component: local 0 = Ok(I32(-340539887))

Both handles should be invalid, because their frames are gone by then. Instead, local reads stale stack memory. With a funcref local, the same stale read builds a Func from a guest-written pointer and segfaults. The same happens for handles taken in DebugEvent::HostcallError and DebugEvent::Exception handlers.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 13 2026 at 22:22):

alexcrichton added the bug label to Issue #15281.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 13 2026 at 22:22):

alexcrichton added the wasmtime:debugging label to Issue #15281.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 19 2026 at 15:51):

alexcrichton closed issue #15281:

This program:

use std::sync::{Arc, Mutex};
use std::task::{Context, Poll, Waker};
use wasmtime::component::{Component, Linker};
use wasmtime::*;

type Slot = Arc<Mutex<Option<FrameHandle>>>;

#[derive(Clone)]
struct OnTrap(Slot);

impl DebugHandler for OnTrap {
    type Data = ();
    async fn handle(&self, mut store: StoreContextMut<'_, ()>, event: DebugEvent<'_>) {
        if let DebugEvent::Trap(_) = event {
            *self.0.lock().unwrap() = store.debug_exit_frames().next();
        }
    }
}

fn main() -> Result<()> {
    let mut config = Config::new();
    config.guest_debug(true);
    let engine = Engine::new(&config)?;

    // A frame handle taken in a `DebugEvent::Trap` handler.
    let module = Module::new(
        &engine,
        r#"(module (func (export "f") (local i32) unreachable))"#,
    )?;
    let mut store = Store::new(&engine, ());
    let slot = Slot::default();
    store.set_debug_handler(OnTrap(slot.clone()));
    let instance = block_on(Instance::new_async(&mut store, &module, &[]))?;
    let f = instance.get_typed_func::<(), ()>(&mut store, "f")?;
    assert!(block_on(f.call_async(&mut store, ())).is_err());
    let frame = slot.lock().unwrap().take().unwrap();
    println!("trap: is_valid = {}", frame.is_valid(&mut store));
    println!("trap: local 0 = {:?}", frame.local(&mut store, 0));

    // A frame handle taken in a component host import.
    let component = Component::new(
        &engine,
        r#"(component
             (import "capture" (func $capture))
             (import "check" (func $check))
             (core func $capture (canon lower (func $capture)))
             (core func $check (canon lower (func $check)))
             (core module $m
               (import "" "capture" (func $capture))
               (import "" "check" (func $check))
               (func $victim (local i32) (call $capture))
               (func (export "run") (call $victim) (call $check)))
             (core instance $i (instantiate $m
               (with "" (instance
                 (export "capture" (func $capture))
                 (export "check" (func $check))))))
             (func (export "run") (canon lift (core func $i "run"))))"#,
    )?;
    let mut linker = Linker::<Option<FrameHandle>>::new(&engine);
    linker.root().func_wrap("capture", |mut store, (): ()| {
        let frame = store.debug_exit_frames().next();
        *store.data_mut() = frame;
        Ok(())
    })?;
    linker.root().func_wrap("check", |mut store, (): ()| {
        let frame = store.data_mut().take().unwrap();
        println!("component: is_valid = {}", frame.is_valid(&mut store));
        println!("component: local 0 = {:?}", frame.local(&mut store, 0));
        Ok(())
    })?;
    let mut store = Store::new(&engine, None);
    let instance = linker.instantiate(&mut store, &component)?;
    let run = instance.get_typed_func::<(), ()>(&mut store, "run")?;
    run.call(&mut store, ())?;
    Ok(())
}

fn block_on<F: Future>(f: F) -> F::Output {
    let mut f = std::pin::pin!(f);
    let mut cx = Context::from_waker(Waker::noop());
    loop {
        if let Poll::Ready(v) = f.as_mut().poll(&mut cx) {
            return v;
        }
    }
}

with wasmtime at bytecodealliance/wasmtime@ffbf0e0c13, prints:

$ cargo run
trap: is_valid = true
trap: local 0 = Ok(I32(4))
component: is_valid = true
component: local 0 = Ok(I32(-340539887))

Both handles should be invalid, because their frames are gone by then. Instead, local reads stale stack memory. With a funcref local, the same stale read builds a Func from a guest-written pointer and segfaults. The same happens for handles taken in DebugEvent::HostcallError and DebugEvent::Exception handlers.


Last updated: Nov 19 2026 at 20:05 UTC