-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Runtime error using f64::sin in wasm32
#128105
Comments
wasm32-wasi
wasm32-wasi
wasm32
Your reproducer is incomplete because it does not include the build command you used with wasm-pack. It is possible to compile wasm code without wasm pack. |
You can build the reproduction using |
It is fine if you didn't, but e.g. every time I have touched wasm-pack, nothing flowed through npm, so I am actually surprised to hear you are using it. |
I've significantly reduced the reproduction and identified the problem as a symbol conflict. Here's the reproduction: #![no_main]
#[no_mangle]
pub extern "C" fn sin_(ptr: i64) -> i64 {
ptr
}
#[no_mangle]
pub extern "C" fn cos(ptr: i64) -> i64 {
ptr
}
#[no_mangle]
pub extern "C" fn _start() {
println!("{}", 5.0f64.sin()); // runs fine
println!("{}", 5.0f64.cos()); // errors
} Build with When compiling (func $cos (type 4) (param f64) (result f64)
local.get 0
call $_ZN17compiler_builtins4math3cos17h1c9c941a8073602cE) This generated function conflicts with the exported Using ~$ wasm-validate target/wasm32-unknown-unknown/debug/wasm-array-bug.wasm
target/wasm32-unknown-unknown/debug/wasm-array-bug.wasm:00001e5: error: type mismatch in call, expected [i64] but got [f64]
target/wasm32-unknown-unknown/debug/wasm-array-bug.wasm:00001e7: error: type mismatch in local.set, expected [f64] but got [i64] Remaining questions:
|
oh neat! |
f32 sin should also be sinf??? |
Yeah, these are the functions generated by f32::sin, f32::cos, f64::sin, f64::cos respectively: (func $sinf (type 5) (param f32) (result f32)
local.get 0
call $_ZN17compiler_builtins4math4sinf17ha493429580d1ed0cE)
(func $cosf (type 5) (param f32) (result f32)
local.get 0
call $_ZN17compiler_builtins4math4cosf17h2f95fcc8c4a732edE)
(func $sin (type 7) (param f64) (result f64)
local.get 0
call $_ZN17compiler_builtins4math3sin17h958cb507a9dbd375E)
(func $cos (type 7) (param f64) (result f64)
local.get 0
call $_ZN17compiler_builtins4math3cos17h1c9c941a8073602cE) I believe these symbol names correspond to those exported by |
I see. Hm, so, shadowing the libm names causing problems sounds like an unfortunate and expected hazard of using I think the rest is a wasm_bindgen bug then...? |
I'm out of my depth as to whether this is expected behavior or not. Naively, I'd assume this would generate a linker error rather than generating code broken at runtime, though I'm not sure how easy it'd be to fix. It does look like something similar happens with the same code compiled to I don't think there's any bug in wasm_bindgen, as I get the same behavior with and without. However, in theory it should be possible to change a function name in the wasm module without changing the name exported in the JS shim. related: #28179 |
It is easy to cause UB with #[no_mangle]
extern "C" fn malloc() {
panic!(
"malloc is a good name for a function that finds \
Original Content at the mall, right?"
);
}
// doesn't even do anything but the program crashes
fn main() {} For that reason, starting in edition 2024 it will be Symbols can be either strong (always gets picked, duplicates raise errors) or weak (yield to a strong symbol if available, if there are >1 weak symbols it just picks one). Symbols exported by Rust are always strong (the way to configure this is unstable), and I think Your best bet here might be to change the names you export to JS, then just wrap them on the JS side with the correct name. Assuming this is coming from wasm-bindgen and there aren't knobs to twiddle. |
adding |
Yeah, it looks like this is a dupe of #28179, and of rustwasm/wasm-bindgen#2338 Thanks for the help debugging. |
I'm working on a WASM library using
wasm_bindgen
which wrapsndarray
. I have the following basic structure:i.e. sin() is implemented by first dispatching on type and then mapping.
However, calling
sin
on aFloat64
array results in the following runtime error:This error originates in the implementation of
f64::sin()
.Interestingly, I don't get the same error for f32. I also don't get the same error if I remove the enum & match:
This makes me think there's likely some strange miscompilation. For reference, here's the
f64::sin()
function compiled in the broken case (with enum/match):This appears to push
$var0
to the stack and then immediately calls$signature_mismatch:sin
, which halts.The working case (without enum/match) compiles to the following:
I've uploaded the complete code to reproduce the issue here: https://github.com/hexane360/wasm-array-bug
I'm filing this issue here because it appears to be a miscompilation. It's possible this is actually a problem in
wasm-bindgen
orwasm-pack
. Please let me know and I'll open an issue there instead.Meta
rustc --version --verbose
:wasm-pack
version0.12.1
wasm-bindgen
version0.2.92
The text was updated successfully, but these errors were encountered: