Stream: wasm

Topic: Export the size and offset of a pre-allocated memory segment


view this post on Zulip Geoff Goodman (Mar 22 2024 at 01:47):

I'm looking to try an experiment where the host and guest leverage a shared region of memory for message communication. It leverages an assumption that imported and exported functions will used this region of memory to pass in a single argument and will use the same region to return an outcome.

I'm struggling to even get the groundwork in place. Here's the idea I have:

const SHARED_BUFFER_SIZE_MAX: usize = 1024;
static mut SHARED_BUFFER: [u8; SHARED_BUFFER_SIZE_MAX] = [0; SHARED_BUFFER_SIZE_MAX];

#[export_name = "SHARED_BUFFER_OFFSET"]
pub static mut SHARED_BUFFER_OFFSET: *const u8 = unsafe { SHARED_BUFFER }.as_ptr();

#[export_name = "SHARED_BUFFER_SIZE"]
pub static SHARED_BUFFER_SIZE: u32 = SHARED_BUFFER_SIZE_MAX as u32;

The resulting .wat has this though:

  (global (;5;) i32 i32.const 1091744)
  (global (;6;) i32 i32.const 1049600)
  (export "memory" (memory 0))
  (export "SHARED_BUFFER_OFFSET" (global 5))
  (export "SHARED_BUFFER_SIZE" (global 6))

view this post on Zulip Alex Crichton (Mar 22 2024 at 03:41):

That will work, yeah, but those values are stored in linear memory so the globals are pointers into linear memory you'd have to read. That's basically how globals work in LLVM, everything lives in linear memory and exports are pointers to the address in linear memory

view this post on Zulip Geoff Goodman (Mar 22 2024 at 12:43):

Are there alternative approaches to accomplish what I'm trying to do?

view this post on Zulip Geoff Goodman (Mar 22 2024 at 13:14):

Probably not a big deal doing the extra read since I control the host.

view this post on Zulip Alex Crichton (Mar 22 2024 at 14:30):

I'm not aware of any myself, AFAIK there's no way to export custom globals with custom values without liberal use of "inline assembly" which is not easy to do I believe


Last updated: Dec 23 2024 at 12:05 UTC