Stream: git-wasmtime

Topic: wasmtime / issue #12112 Why calling host API will error o...


view this post on Zulip Wasmtime GitHub notifications bot (Dec 03 2025 at 03:27):

sammyne opened issue #12112:

It's broken to run wasm component model using wrapped realloc with host call.

The full reproduction repository goes as https://github.com/sammyne/broken-memory-inspector-with-host-call

Environment

Project layout

folder description
guest C++-written wasm component
host-rs Rust-written embedded wasmtime host, exposing a inspect api to called by guest's __wrap_realloc

Core logic

Guest: __wrap_realloc will call the real realloc and then call inspect to recorded the allocated ptr

void inspect(void *p, bool alloc_or_free) {
  sammyne_hellohost_api_inspect(uint64_t(p), alloc_or_free);
}

void *__wrap_realloc(void *ptr, size_t size) {
  // 不能直接调用 realloc,否则递归导致栈溢出。
  auto p = __real_realloc(ptr, size);

  // TODO: 排查清楚加这行代码会导致失败的原因
  // printf("realloc(%zu)=%p\n", size, p);
  inspect(p, true);

  return p;
}

Host: MyHost will be injected into wasmtime's Store

impl Host for MyHost {
    fn inspect(&mut self, addr: u64, alloc_or_free: bool) {
        println!("addr={addr},alloc-or-free={alloc_or_free}");
    }
}

Reproduction

Using the host to load the guest wasm to tiggering call of realloc as

make run

Error output as

Error: call

Caused by:
    0: error while executing at wasm backtrace:
           0:   0x1e58 - greeter.wasm!sammyne_hellohost_api_inspect
           1:    0xe28 - greeter.wasm!inspect(void*, bool)
           2:    0xf49 - greeter.wasm!__wrap_realloc
           3:   0x1d40 - greeter.wasm!cabi_realloc
       note: using the `WASMTIME_BACKTRACE_DETAILS=1` environment variable may show more debugging information
    1: wasm trap: cannot leave component instance
make: *** [Makefile:13: run] Error 1

Really appreciate if someone could help me out~

References

view this post on Zulip Wasmtime GitHub notifications bot (Dec 03 2025 at 06:53):

cfallin closed issue #12112:

It's broken to run wasm component model using wrapped realloc with host call.

The full reproduction repository goes as https://github.com/sammyne/broken-memory-inspector-with-host-call

Environment

Project layout

folder description
guest C++-written wasm component
host-rs Rust-written embedded wasmtime host, exposing a inspect api to called by guest's __wrap_realloc

Core logic

Guest: __wrap_realloc will call the real realloc and then call inspect to recorded the allocated ptr

void inspect(void *p, bool alloc_or_free) {
  sammyne_hellohost_api_inspect(uint64_t(p), alloc_or_free);
}

void *__wrap_realloc(void *ptr, size_t size) {
  // 不能直接调用 realloc,否则递归导致栈溢出。
  auto p = __real_realloc(ptr, size);

  // TODO: 排查清楚加这行代码会导致失败的原因
  // printf("realloc(%zu)=%p\n", size, p);
  inspect(p, true);

  return p;
}

Host: MyHost will be injected into wasmtime's Store

impl Host for MyHost {
    fn inspect(&mut self, addr: u64, alloc_or_free: bool) {
        println!("addr={addr},alloc-or-free={alloc_or_free}");
    }
}

Reproduction

Using the host to load the guest wasm to tiggering call of realloc as

make run

Error output as

Error: call

Caused by:
    0: error while executing at wasm backtrace:
           0:   0x1e58 - greeter.wasm!sammyne_hellohost_api_inspect
           1:    0xe28 - greeter.wasm!inspect(void*, bool)
           2:    0xf49 - greeter.wasm!__wrap_realloc
           3:   0x1d40 - greeter.wasm!cabi_realloc
       note: using the `WASMTIME_BACKTRACE_DETAILS=1` environment variable may show more debugging information
    1: wasm trap: cannot leave component instance
make: *** [Makefile:13: run] Error 1

Really appreciate if someone could help me out~

References

view this post on Zulip Wasmtime GitHub notifications bot (Dec 03 2025 at 06:53):

cfallin commented on issue #12112:

Unfortunately, the spec says that realloc cannot call out of the component; so this behavior in Wasmtime is correct. Look for may_leave in that document for details.

If you need to debug memory allocations, one workaround might be to record some log internally (e.g. in some fixed buffer) and then dump the information via another call into the component.

I'll close this since there is no Wasmtime bug, but best of luck and feel free to ask on our Zulip if you need more tips on debugging your system.

view this post on Zulip Wasmtime GitHub notifications bot (Dec 03 2025 at 12:40):

tschneidereit commented on issue #12112:

One thing to look forward here is a planned spec change that will make the realloc calls go away. At a very high level, the idea is that instead of the host calling realloc for a lowered value, it'll provide the required information to the guest on values that can be lowered, and the guest can then decide where to place them (and whether to do the lowering at all).


Last updated: Dec 06 2025 at 06:05 UTC