-
Notifications
You must be signed in to change notification settings - Fork 824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Corruption of WasmerEnv when using call indirect to host fn from Wasm with Singlepass #2329
Comments
Thanks for the bug report! I did a deep dive on this and found out the cause. It seems to be a bug only affecting singlepass. The WAT that causes the failure: (module
(type (;0;) (func (param i32) (result i32)))
(type (;1;) (func))
(type (;2;) (func (param i32 i32) (result i32)))
(import "env" "__read_memory" (func $__read_memory (type 0)))
(func $read_memory (type 1)
(drop
(call $_ZN5other8dispatch17h053cb34ef5d0d7b0E
(i32.const 1)
(i32.const 2)))
(drop
(call $__read_memory
(i32.const 1))))
(func $_ZN5other8dispatch17h053cb34ef5d0d7b0E (type 2) (param i32 i32) (result i32)
(call_indirect (type 0)
(local.get 1)
(local.get 0)))
(table (;0;) 2 2 funcref)
(memory (;0;) 16)
(global (;0;) (mut i32) (i32.const 1048576))
(global (;1;) i32 (i32.const 1048576))
(global (;2;) i32 (i32.const 1048576))
(export "memory" (memory 0))
(export "read_memory" (func $read_memory))
(export "__data_end" (global 1))
(export "__heap_base" (global 2))
(elem (;0;) (i32.const 1) func $__read_memory)) The version with inlining that fixes this (module
(type (;0;) (func (param i32) (result i32)))
(type (;1;) (func))
(import "env" "__read_memory" (func $__read_memory (type 0)))
(func $read_memory (type 1)
(drop
(call $__read_memory
(i32.const 2)))
(drop
(call $__read_memory
(i32.const 1))))
(table (;0;) 1 1 funcref)
(memory (;0;) 16)
(global (;0;) (mut i32) (i32.const 1048576))
(global (;1;) i32 (i32.const 1048576))
(global (;2;) i32 (i32.const 1048576))
(export "memory" (memory 0))
(export "read_memory" (func $read_memory))
(export "__data_end" (global 1))
(export "__heap_base" (global 2))) Additionally, running the Wasm through wasm-opt will inline the call and side-step this bug (I imagine compiling with full LTO also avoids it). Just as a side note, it's always good to ensure your Wasm is optimized before giving it to Wasmer, though in this case I'm glad you didn't so we could find this bug 😆 . |
Can be fixed along those lines:
|
The fix looks correct. I've opened a PR to test it a bit more throughly |
This is now fixed in master: #2494. Closing the issue |
Describe the bug
If a host function is being called from different crates in the guest then on the host side the env points to garbage, which returns
None
for memory and can simply panic for other fields.Steps to reproduce
I made a repo - https://github.com/thedavidmeister/wasmer-env-memory
Expected behavior
Host consistently finds memory and other env fields.
Actual behavior
Host finds
None
and/or panics due to other env fields pointing to garbage.Additional context
There is a workaround/fix which is adding
#[inline(always)]
to any guest function that:https://github.com/thedavidmeister/wasmer-env-memory/blob/master/other/lib.rs#L1
The text was updated successfully, but these errors were encountered: