Stream: git-wasmtime

Topic: wasmtime / issue #7786 How to access the instance's memor...


view this post on Zulip Wasmtime GitHub notifications bot (Jan 17 2024 at 23:34):

eigenein opened issue #7786:

:wave: Hi team,

I'm instantiating a long-lived Engine and Linker, and then I'm adding some host functions to the linker (Rust API). Some of these functions should receive a string as a parameter, so an offset and a length in a guest's memory, for example:

let mut linker = wasmtime::Linker::new(&engine);
linker.func_wrap("logging", "info",
    |mut caller: Caller<'_, String>, offset: u32, size: u32| {
        // How do I access instance's memory here and read from there?
    },
)?;

I could use a closure, but I'm going to support a plugin system, and a shared host function may be called from any of them. Also, Store is short-lived, and I wouldn't like to re-instantiate a Linker together with the closures for every host→guest call

Should I put a reference to instance's Memory to the instance's Store? That smells a little, since Caller already contains Store and all the memories are already owned by Store. Am I missing something?

view this post on Zulip Wasmtime GitHub notifications bot (Jan 17 2024 at 23:34):

eigenein edited issue #7786:

:wave: Hi team,

I'm instantiating a long-lived Engine and Linker, and then I'm adding some host functions to the linker (Rust API). Some of these functions should receive a string as a parameter, so an offset and a length in a guest's memory, for example:

let mut linker = wasmtime::Linker::new(&engine);
linker.func_wrap("logging", "info",
    |mut caller: Caller<'_, ()>, offset: u32, size: u32| {
        // How do I access instance's memory here and read from there?
    },
)?;

I could use a closure, but I'm going to support a plugin system, and a shared host function may be called from any of them. Also, Store is short-lived, and I wouldn't like to re-instantiate a Linker together with the closures for every host→guest call

Should I put a reference to instance's Memory to the instance's Store? That smells a little, since Caller already contains Store and all the memories are already owned by Store. Am I missing something?

view this post on Zulip Wasmtime GitHub notifications bot (Jan 17 2024 at 23:34):

eigenein edited issue #7786:

:wave: Hi team,

I'm instantiating a long-lived Engine and Linker, and then I'm adding some host functions to the linker (Rust API). Some of these functions should receive a string as a parameter, so an offset and a length in a guest's memory, for example:

let mut linker = wasmtime::Linker::new(&engine);
linker.func_wrap("logging", "info",
    |mut caller: Caller<'_, ()>, offset: u32, size: u32| {
        // How do I access instance's memory here and read from there?
    },
)?;

I could use a closure, but I'm going to support a plugin system, and a shared host function may be called from any plugin. Also, Store is short-lived, and I wouldn't like to re-instantiate a Linker together with the closures for every host→guest call

Should I put a reference to instance's Memory to the instance's Store? That smells a little, since Caller already contains Store and all the memories are already owned by Store. Am I missing something?

view this post on Zulip Wasmtime GitHub notifications bot (Jan 17 2024 at 23:35):

eigenein edited issue #7786:

:wave: Hi team,

I'm instantiating a long-lived Engine and Linker, and then I'm adding some host functions to the linker (Rust API). Some of these functions should receive a string as a parameter, so an offset and a length in a guest's memory, for example:

let mut linker = wasmtime::Linker::new(&engine);
linker.func_wrap("logging", "info",
    |mut caller: Caller<'_, ()>, offset: u32, size: u32| {
        // How do I access instance's memory here and read from there?
    },
)?;

I could use a closure, but I'm going to support a plugin system, and a shared host function may be called from any plugin. Also, Store is short-lived, and I wouldn't like to re-instantiate a Linker together with the closures for every host-to-guest call

Should I put a reference to instance's Memory to the instance's Store? That smells a little, since Caller already contains Store and all the memories are already owned by Store. Am I missing something?

view this post on Zulip Wasmtime GitHub notifications bot (Jan 17 2024 at 23:46):

eigenein edited issue #7786:

:wave: Hi team,

I'm instantiating a long-lived Engine and Linker, and then I'm adding some host functions to the linker (Rust API). Some of these functions should receive a string as a parameter, so an offset and a length in a guest's memory, for example:

let mut linker = wasmtime::Linker::new(&engine);
linker.func_wrap("logging", "info",
    |mut caller: Caller<'_, ()>, (offset, size): (u32, u32)| {
        // How do I access instance's memory here and read from there?
    },
)?;

I could use a closure, but I'm going to support a plugin system, and a shared host function may be called from any plugin. Also, Store is short-lived, and I wouldn't like to re-instantiate a Linker together with the closures for every host-to-guest call

Should I put a reference to instance's Memory to the instance's Store? That smells a little, since Caller already contains Store and all the memories are already owned by Store. Am I missing something?

view this post on Zulip Wasmtime GitHub notifications bot (Jan 17 2024 at 23:48):

eigenein edited issue #7786:

:wave: Hi team,

I'm instantiating a long-lived Engine and Linker, and then I'm adding some host functions to the linker (Rust API). Some of these functions should receive a string as a parameter, so an offset and a length in a guest's memory, for example:

let mut linker = wasmtime::Linker::new(&engine);
linker.func_wrap("logging", "info",
    |mut caller: Caller<'_, ()>, (offset, size): (u32, u32)| {
        // How do I access instance's memory here and read from there?
    },
)?;

I could use a closure, but I'm going to support a plugin system, and a shared host function may be called from any plugin. Also, Store is short-lived, and I wouldn't like to re-instantiate a Linker together with the closures for every host-to-guest call

Should I put a reference to Instance or to the instance's Memory to the instance's Store? That smells a little, since Caller already contains Store and all the memories are already owned by Store. Am I missing something?

view this post on Zulip Wasmtime GitHub notifications bot (Jan 17 2024 at 23:49):

eigenein edited issue #7786:

:wave: Hi team,

I'm instantiating a long-lived Engine and Linker, and then I'm adding some host functions to the linker (Rust API). Some of these functions should receive a string as a parameter, so an offset and a length in a guest's memory, for example:

let mut linker = wasmtime::Linker::new(&engine);
linker.func_wrap("logging", "info",
    |mut caller: Caller<'_, ()>, (offset, size): (u32, u32)| {
        // How do I access instance's memory here and read from there?
    },
)?;

I could use a closure, but I'm going to support a plugin system, and a shared host function may be called from any plugin. Also, Store is short-lived, and I wouldn't like to re-instantiate a Linker together with the closures for every host-to-guest call

Should I put a reference to Instance or to the instance's Memory to the instance's Store? That smells a little, since Caller already contains Store and all the memories are already owned by Store, that is effeectively making a loop of references. Am I missing something?

view this post on Zulip Wasmtime GitHub notifications bot (Jan 17 2024 at 23:49):

eigenein edited issue #7786:

:wave: Hi team,

I'm instantiating a long-lived Engine and Linker, and then I'm adding some host functions to the linker (Rust API). Some of these functions should receive a string as a parameter, so an offset and a length in a guest's memory, for example:

let mut linker = wasmtime::Linker::new(&engine);
linker.func_wrap("logging", "info",
    |mut caller: Caller<'_, ()>, (offset, size): (u32, u32)| {
        // How do I access instance's memory here and read from there?
    },
)?;

I could use a closure, but I'm going to support a plugin system, and a shared host function may be called from any plugin. Also, Store is short-lived, and I wouldn't like to re-instantiate a Linker together with the closures for every host-to-guest call

Should I put a reference to Instance or to the instance's Memory to the instance's Store? That smells a little, since Caller already contains Store and all the memories are already owned by Store, that is effectively making a loop of references. Am I missing something?

view this post on Zulip Wasmtime GitHub notifications bot (Jan 17 2024 at 23:49):

eigenein edited issue #7786:

:wave: Hi team,

I'm instantiating a long-lived Engine and Linker, and then I'm adding some host functions to the linker (Rust API). Some of these functions should receive a string as a parameter, so an offset and a length in a guest's memory, for example:

let mut linker = wasmtime::Linker::new(&engine);
linker.func_wrap("logging", "info",
    |mut caller: Caller<'_, ()>, (offset, size): (u32, u32)| {
        // How do I access instance's memory here and read from there?
    },
)?;

I could use a closure, but I'm going to support a plugin system, and a shared host function may be called from any plugin. Also, Store is short-lived, and I wouldn't like to re-instantiate a Linker together with the closures for every host-to-guest call

Should I put a reference to Instance or to the instance's Memory to the instance's Store? That smells a little, since Caller already contains Store and all the memories are already owned by Store – so that would effectively be making a loop of references. Am I missing something?

view this post on Zulip Wasmtime GitHub notifications bot (Jan 17 2024 at 23:57):

eigenein edited issue #7786:

:wave: Hi team,

I'm instantiating a long-lived Engine and Linker, and then I'm adding some host functions to the linker (Rust API). Some of these functions should receive a string as a parameter, so an offset and a length in a guest's memory, for example:

let mut linker = wasmtime::Linker::new(&engine);
linker.func_wrap("logging", "info",
    |mut caller: Caller<'_, ()>, (offset, size): (u32, u32)| {
        // How do I access instance's memory here and read from it?
    },
)?;

I could use a closure, but I'm going to support a plugin system, and a shared host function may be called from any plugin. Also, Store is short-lived, and I wouldn't like to re-instantiate a Linker together with the closures for every host-to-guest call

Should I put a reference to Instance or to the instance's Memory to the instance's Store? That smells a little, since Caller already contains Store and all the memories are already owned by Store – so that would effectively be making a loop of references. Am I missing something?

view this post on Zulip Wasmtime GitHub notifications bot (Jan 17 2024 at 23:59):

eigenein edited issue #7786:

:wave: Hi team,

I'm instantiating a long-lived Engine and Linker, and then I'm adding some host functions to the linker (Rust API). Some of these functions should receive a string as a parameter, so an offset and a length in a guest's memory, for example:

let mut linker = wasmtime::Linker::new(&engine);
linker.func_wrap("logging", "info",
    |mut caller: Caller<'_, ()>, (offset, size): (u32, u32)| {
        // How do I access instance's memory here and read from it?
    },
)?;

I could use a closure as suggested in https://github.com/bytecodealliance/wasmtime/issues/2491#issuecomment-742352722, but I'm going to support a plugin system, and a shared host function may be called from any plugin. Also, Store is short-lived, and I wouldn't like to re-instantiate a Linker together with the closures for every host-to-guest call

Should I put a reference to Instance or to the instance's Memory to the instance's Store? That smells a little, since Caller already contains Store and all the memories are already owned by Store – so that would effectively be making a loop of references. Am I missing something?

view this post on Zulip Wasmtime GitHub notifications bot (Jan 18 2024 at 00:00):

eigenein edited issue #7786:

:wave: Hi team,

I'm instantiating a long-lived Engine and Linker, and then I'm adding some host functions to the linker (Rust API). Some of these functions should receive a string as a parameter, so an offset and a length in a guest's memory, for example:

let mut linker = wasmtime::Linker::new(&engine);
linker.func_wrap("logging", "info",
    |mut caller: Caller<'_, ()>, (offset, size): (u32, u32)| {
        // How do I access instance's memory here and read from it?
    },
)?;

I could use a closure as suggested in https://github.com/bytecodealliance/wasmtime/issues/2491#issuecomment-742352722, but I'm going to support a plugin system, and a shared host function may be called from any plugin. Also, Store is short-lived, and I wouldn't like to re-instantiate a Linker together with the closures for every host-to-guest call

Should I put a reference to Instance or to the instance's Memory into the instance's Store? That smells a little, since Caller already contains Store and all the memories are already owned by Store – so that would effectively be making a loop of references. Am I missing something?

view this post on Zulip Wasmtime GitHub notifications bot (Jan 18 2024 at 00:36):

alexcrichton commented on issue #7786:

There's an example for this on the documentation of Func::wrap where the gist of it is:

    let mem = match caller.get_export("memory") {
        Some(Extern::Memory(mem)) => mem,
        _ => anyhow::bail!("failed to find host memory"),
    };

One option is to store a Memory in the T of Store<T> (i.e. replace the () you're using currently), but the easiest option is probably to call caller.get_export(...)

view this post on Zulip Wasmtime GitHub notifications bot (Jan 18 2024 at 00:43):

eigenein commented on issue #7786:

@alexcrichton Thank you, just exactly what I needed and completely overlooked!

view this post on Zulip Wasmtime GitHub notifications bot (Jan 18 2024 at 00:43):

eigenein closed issue #7786:

:wave: Hi team,

I'm instantiating a long-lived Engine and Linker, and then I'm adding some host functions to the linker (Rust API). Some of these functions should receive a string as a parameter, so an offset and a length in a guest's memory, for example:

let mut linker = wasmtime::Linker::new(&engine);
linker.func_wrap("logging", "info",
    |mut caller: Caller<'_, ()>, (offset, size): (u32, u32)| {
        // How do I access instance's memory here and read from it?
    },
)?;

I could use a closure as suggested in https://github.com/bytecodealliance/wasmtime/issues/2491#issuecomment-742352722, but I'm going to support a plugin system, and a shared host function may be called from any plugin. Also, Store is short-lived, and I wouldn't like to re-instantiate a Linker together with the closures for every host-to-guest call

Should I put a reference to Instance or to the instance's Memory into the instance's Store? That smells a little, since Caller already contains Store and all the memories are already owned by Store – so that would effectively be making a loop of references. Am I missing something?


Last updated: Nov 22 2024 at 16:03 UTC