Stream: wasi

Topic: How to have direct access to the guest memory's base addres


view this post on Zulip Coulson Liang (Jun 14 2024 at 20:48):

Hi, say I have a wiggle::GuestMemory, how to access the address of the starting address of the linear memory instance?

view this post on Zulip Alex Crichton (Jun 14 2024 at 20:49):

For that you'll want GuestMemory::base

view this post on Zulip Coulson Liang (Jun 14 2024 at 21:13):

Yeah I think I need that, but I didn't see the the base() function defined here https://github.com/bytecodealliance/wasmtime/blob/main/crates/wiggle/src/lib.rs

A fast and secure runtime for WebAssembly. Contribute to bytecodealliance/wasmtime development by creating an account on GitHub.

view this post on Zulip Coulson Liang (Jun 14 2024 at 21:34):

Basically I just want to know the start address of the linear memory, when I'm in a host function exposed the wasm module, like fd_write

view this post on Zulip Alex Crichton (Jun 14 2024 at 21:34):

Ah yes wiggle changed on the main branch of wasmtime, for the base address you can look at the payloads of GuestMemory

view this post on Zulip Alex Crichton (Jun 14 2024 at 21:35):

this is the documentation for main and you can do a match on a GuestMemory. The Shared and Unshared variants both hold the entire contents of linear memory, so the base address of those arrays is the base address of linear memory

view this post on Zulip Alex Crichton (Jun 14 2024 at 21:36):

If you'd like I think it would also be reasonable to re-add the base method as fn(&GuestMemory) -> &[UnsafeCell<u8>] too

view this post on Zulip Coulson Liang (Jun 14 2024 at 21:46):

Thank you! This makes a lot of sense. I added a simple implementation, works fine

pub fn base(&self) -> *const u8 {
        match self {
            GuestMemory::Unshared(slice) => slice.as_ptr(),
            GuestMemory::Shared(slice) => slice.as_ptr() as *const u8,
        }
    }

Last updated: Oct 23 2024 at 20:03 UTC