Last active
July 16, 2024 15:36
-
-
Save kay/9715e05168381f3f3a0ff5abf49df578 to your computer and use it in GitHub Desktop.
Wasmer 4.3.4 panics when using static typed host functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
[dependencies] | |
wasmer = { version = "4.3.4" } | |
*/ | |
use wasmer::{Cranelift, Function, Imports, Instance, Module, RuntimeError, Store, Value}; | |
fn static_return_32_then_64_then_32(lhs: i32, rhs: i32) -> (i32, i64, i32) { | |
(lhs, lhs as i64 + rhs as i64, rhs) | |
} | |
fn dynamic_return_32_then_64_then_32(args: &[Value]) -> Result<Vec<Value>, RuntimeError> { | |
let lhs = args[0].unwrap_i32(); | |
let rhs = args[1].unwrap_i32(); | |
Ok(vec![ | |
Value::I32(lhs), | |
Value::I64(lhs as i64 + rhs as i64), | |
Value::I32(rhs), | |
]) | |
} | |
/** | |
Rust lang version is 1.79.0 | |
Wasmer version is 4.3.4 | |
Platform | |
$ uname -a | |
Darwin XXX 23.5.0 Darwin Kernel Version 23.5.0: Wed May 1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000 arm64 | |
Panics with | |
thread 'main' panicked at src/main.rs:120:46: | |
called `Result::unwrap()` on an `Err` value: RuntimeError { source: Wasm { pc: 4341616908, backtrace: 0: <unknown> | |
1: <unknown> | |
2: <unknown> | |
3: <unknown> | |
4: <unknown> | |
5: <unknown> | |
6: <unknown> | |
7: <unknown> | |
8: <unknown> | |
9: <unknown> | |
10: <unknown> | |
, signal_trap: Some(HeapAccessOutOfBounds) }, wasm_trace: [] } | |
stack backtrace: | |
0: rust_begin_unwind | |
at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/panicking.rs:652:5 | |
1: core::panicking::panic_fmt | |
at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/panicking.rs:72:14 | |
2: core::result::unwrap_failed | |
at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/result.rs:1654:5 | |
3: core::result::Result<T,E>::unwrap | |
at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/result.rs:1077:23 | |
4: wasm_cranelift_bug::main | |
at ./src/main.rs:120:9 | |
5: core::ops::function::FnOnce::call_once | |
at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/ops/function.rs:250:5 | |
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. | |
*/ | |
fn main() { | |
let wat_str = r#" | |
(module | |
(import "builtins" "return_32_then_64_then_32" (func $return_32_then_64_then_32 (param i32 i32) (result i32 i64 i32))) | |
(func $test (param $lhs i32) (param $rhs i32) (result i64) | |
(local $x i64) | |
local.get $lhs | |
local.get $rhs | |
call $return_32_then_64_then_32 | |
drop | |
local.set $x | |
drop | |
local.get $x | |
return | |
) | |
(export "test" (func $test)) | |
) | |
"#; | |
let engine = Cranelift::new(); | |
let mut store = Store::new(engine); | |
let module = Module::new(&store, &wat_str).unwrap(); | |
let mut dynamic_imports = Imports::new(); | |
dynamic_imports.define( | |
"builtins", | |
"return_32_then_64_then_32", | |
Function::new( | |
&mut store, | |
( | |
[wasmer::Type::I32, wasmer::Type::I32], | |
[wasmer::Type::I32, wasmer::Type::I64, wasmer::Type::I32], | |
), | |
dynamic_return_32_then_64_then_32, | |
), | |
); | |
let dynamic_instance = Instance::new(&mut store, &module, &dynamic_imports).unwrap(); | |
let mut static_imports = Imports::new(); | |
static_imports.define( | |
"builtins", | |
"return_32_then_64_then_32", | |
Function::new_typed(&mut store, static_return_32_then_64_then_32), | |
); | |
let static_instance = Instance::new(&mut store, &module, &static_imports).unwrap(); | |
let lhs = 2i32; | |
let rhs = 3i32; | |
let sum = 5i64; | |
let arguments = &[Value::I32(lhs), Value::I32(rhs)]; | |
let function = dynamic_instance.exports.get_function("test").unwrap(); | |
assert_eq!( | |
sum, | |
function.call(&mut store, arguments).unwrap()[0].unwrap_i64() | |
); | |
let function = static_instance.exports.get_function("test").unwrap(); | |
assert_eq!( | |
sum, | |
function.call(&mut store, arguments).unwrap()[0].unwrap_i64() | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment