Stream: wasmtime

Topic: unknown import: `wasi_snapshot_preview1::fd_write`


view this post on Zulip Creponne Karim (Feb 15 2024 at 13:14):

Hello, so basically i loaded a precompiled module using Module::deserialize which worked wonderfully, but then i receive this error "unknown import: wasi_snapshot_preview1::fd_write has not been defined"; comming from the go implementation, i saw that i was missing the equivalent of "linker.DefineWasi()"; but i have been searching for a method to link a wasi instance to the linker for hours now without finding any result. So my question would be, first how can i remedy to this ? and what am i doing wrong ?
Also please pardon my ignorance since i picked up rust 2 days ago and i wanted to toy around with wasmtime a bit.
Thanks !

view this post on Zulip Creponne Karim (Feb 15 2024 at 13:16):

The code in question :

pub async unsafe fn execute (
    code: Vec<u8>,
    entry_point: &str,
    params: &[Val],
    engine: wasmtime::Engine,
    linker: wasmtime::Linker<WasiCtx>,
    work_dir_path:&str,
    // mut store: wasmtime::Store<WasiCtx>,
) {
    let mut std_err = wasmtime_wasi::sync::stdio::stderr();
    let mut std_out = wasmtime_wasi::sync::stdio::stdout();

    let wasi = WasiCtxBuilder::new()
        .stderr(Box::new(std_err))
        .stdout(Box::new(std_out))
        .build();

    let mut store = wasmtime::Store::new(linker.engine(),wasi);

    let module = Module::deserialize(&engine, code).unwrap();
    let mut results:[Val;0] = [];

    let instance = linker.instantiate(&mut store,&module).unwrap();
    let entrypoint_function = instance.get_func(&mut store, entry_point).unwrap();

    entrypoint_function.call(&mut store, params, &mut results).unwrap();

    //  read the stdout and err

    let mut bufs: [u8;80] = [0;80];
    // std_out.seek(0);
    let out_text = store.data().table().get::<wasmtime_wasi::sync::stdio::Stderr>(2).unwrap().read_vectored(&mut [std::io::IoSliceMut::new(&mut bufs)]).await.unwrap();
    println!("my loaver {}",out_text);
}

view this post on Zulip Peter Huene (Feb 15 2024 at 13:18):

You're probably missing a call to add_to_linker.

view this post on Zulip Peter Huene (Feb 15 2024 at 13:19):

an example: https://github.com/bytecodealliance/wasmtime/blob/120e6b23950292c60b9e2d70204953e8decd6f4a/examples/linking.rs#L15

view this post on Zulip Creponne Karim (Feb 15 2024 at 13:40):

That worked, thank you !


Last updated: Oct 23 2024 at 20:03 UTC