leonwanghui opened Issue #1730:
Summary
Hi, I was testing with some sample code (with some variants) of wasmtime, but found the error below:
Error: wrong number of imports provided, 0 != 1
I was totally confused of the errors because there is no more addtional debug info, could be appreciated if anyone could take a look, thanks!
Sample code
use anyhow::Result; use wasmtime::*; fn main() -> Result<()> { let store = Store::default(); let module = Module::from_file(&store, "/opt/ms_backend_wasm.wasi.wasm")?; let instance = Instance::new(&module, &[])?; let in_data = vec![1, 2, 3]; let in_data_ptr = &in_data[0] as *const i32; let in_addr = in_data_ptr as i32; let in_size: i32 = 3; let out_data = vec![0, 0, 0]; let out_data_ptr = &out_data[0] as *const i32; let out_addr = out_data_ptr as i32; let out_size: i32 = 3; // Invoke `run` export let add = instance .get_func("run") .ok_or(anyhow::format_err!("failed to find `add` function export"))? .get7::<i32, i32, i32, i32, i32, i32, i32, i32>()?; println!( "run({}) = {}", 0, add(0, in_addr, in_size, in_addr, in_size, out_addr, out_size)? ); Ok(()) }
leonwanghui edited Issue #1730:
Summary
Hi, I was testing with some sample code (with some variants) of wasmtime, but found the error below:
Error: wrong number of imports provided, 0 != 1
I was totally confused of the errors because there is no more addtional debug info, could be appreciated if anyone could take a look, thanks!
Sample code
use anyhow::Result; use wasmtime::*; fn main() -> Result<()> { let store = Store::default(); let module = Module::from_file(&store, "/opt/ms_backend_wasm.wasi.wasm")?; let instance = Instance::new(&module, &[])?; let in_data = vec![1, 2, 3]; let in_data_ptr = &in_data[0] as *const i32; let in_addr = in_data_ptr as i32; let in_size: i32 = 3; let out_data = vec![0, 0, 0]; let out_data_ptr = &out_data[0] as *const i32; let out_addr = out_data_ptr as i32; let out_size: i32 = 3; // Invoke `run` export let run = instance .get_func("run") .ok_or(anyhow::format_err!("failed to find `run` function export"))? .get7::<i32, i32, i32, i32, i32, i32, i32, i32>()?; println!( "run({}) = {}", 0, run(0, in_addr, in_size, in_addr, in_size, out_addr, out_size)? ); Ok(()) }
leonwanghui commented on Issue #1730:
Some follow-up info is as below:
# wasmtime /opt/ms_backend_wasm.wasi.wasm --invoke run 0 1 3 1 3 1 3 warning: using `--invoke` with a function that takes arguments is experimental and may break in the future AddOp init success! AddOp result is [0, 0, 0] AddOp run success! warning: using `--invoke` with a function that returns values is experimental and may break in the future 0
alexcrichton commented on Issue #1730:
Thanks for the report! There's a few issues with what's going on here:
First is that you're calling
Instance::new
with an empty list of imports. Your wasm file,/opt/ms_backend_wasm.wasi.wasm
, however, probably imports from WASI. When usingInstance::new
you'll need to specify imports manually. I might recommend instead following thelinker
example which uses aLinker
to link modules together.Next you're passing host pointers to the guest. This won't work because the host and the guest share different address spaces, so data allocated on the host is not accessible to the guest (by design!). You'll need to copy data into the guest's address space and pass it pointers into the guest address space, not the host address space.
leonwanghui commented on Issue #1730:
@alexcrichton Thanks, I will have a try, please keep this issue opened in case that something unexpected occurred.
leonwanghui commented on Issue #1730:
@alexcrichton Thanks, the issue has been addressed : )
leonwanghui closed Issue #1730:
Summary
Hi, I was testing with some sample code (with some variants) of wasmtime, but found the error below:
Error: wrong number of imports provided, 0 != 1
I was totally confused of the errors because there is no more addtional debug info, could be appreciated if anyone could take a look, thanks!
Sample code
use anyhow::Result; use wasmtime::*; fn main() -> Result<()> { let store = Store::default(); let module = Module::from_file(&store, "/opt/ms_backend_wasm.wasi.wasm")?; let instance = Instance::new(&module, &[])?; let in_data = vec![1, 2, 3]; let in_data_ptr = &in_data[0] as *const i32; let in_addr = in_data_ptr as i32; let in_size: i32 = 3; let out_data = vec![0, 0, 0]; let out_data_ptr = &out_data[0] as *const i32; let out_addr = out_data_ptr as i32; let out_size: i32 = 3; // Invoke `run` export let run = instance .get_func("run") .ok_or(anyhow::format_err!("failed to find `run` function export"))? .get7::<i32, i32, i32, i32, i32, i32, i32, i32>()?; println!( "run({}) = {}", 0, run(0, in_addr, in_size, in_addr, in_size, out_addr, out_size)? ); Ok(()) }
Last updated: Nov 22 2024 at 16:03 UTC