sammyne opened issue #12112:
It's broken to run wasm component model using wrapped
reallocwith host call.The full reproduction repository goes as https://github.com/sammyne/broken-memory-inspector-with-host-call
Environment
- wasmtime v39.0.1
- wasi-sdk v29.0
- wasm-tools v1.243.0
- wit-bindgen v0.48.1
Project layout
folder description guest C++-written wasm component host-rs Rust-written embedded wasmtime host, exposing a inspectapi to called by guest's__wrap_reallocCore logic
Guest:
__wrap_reallocwill call the realreallocand then callinspectto recorded the allocated ptrvoid 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:
MyHostwill be injected into wasmtime'sStoreimpl 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
reallocasmake runError 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 1Really appreciate if someone could help me out~
References
cfallin closed issue #12112:
It's broken to run wasm component model using wrapped
reallocwith host call.The full reproduction repository goes as https://github.com/sammyne/broken-memory-inspector-with-host-call
Environment
- wasmtime v39.0.1
- wasi-sdk v29.0
- wasm-tools v1.243.0
- wit-bindgen v0.48.1
Project layout
folder description guest C++-written wasm component host-rs Rust-written embedded wasmtime host, exposing a inspectapi to called by guest's__wrap_reallocCore logic
Guest:
__wrap_reallocwill call the realreallocand then callinspectto recorded the allocated ptrvoid 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:
MyHostwill be injected into wasmtime'sStoreimpl 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
reallocasmake runError 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 1Really appreciate if someone could help me out~
References
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_leavein 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.
tschneidereit commented on issue #12112:
One thing to look forward here is a planned spec change that will make the
realloccalls go away. At a very high level, the idea is that instead of the host callingreallocfor 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