bytecodealliance / bytecodealliance/wasmtime
Make the imported memory available in functions
- Dominant language
- Rust
- Stars
- 18.6k
- Forks
- 1.8k
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 135
Description
#### Feature
Hello, I need to access the imported memory inside a functions, then I realized the [Caller](https://docs.rs/wasmtime/38.0.3/wasmtime/struct.Caller.html) only show exports, not imports, so I had a lot of trouble to get Wasmtime store working with [MaybeUnit](https://doc.rust-lang.org/beta/std/mem/union.MaybeUninit.html), this simple solution segfaults:
```rust
pub struct State {
pub memory: Memory,
}
let state = MaybeUninit::::uninit();
let mut store = Store::new(engine, state);
let memory_type = MemoryType::new(16, None);
store.data_mut().memory.write(Memory::new(&mut store, memory_type)?);
let store = unsafe {
std::mem::transmute::>, Store>(store)
};
// ...
let imports = [memory.into()];
// segfaults below
let instance = Instance::new(&mut store, &module, &imports)?;
```
Then I realized the issue is that there's no way for me transmute only the State, I need to transmute the Store which is not recommended, once rust [doesn't guarantee the same memory layout](https://doc.rust-lang.org/beta/std/mem/union.MaybeUninit.html#layout-1):
```rust
assert_eq!(size_of::>(), 1);
assert_eq!(size_of::>>(), 2);
```
Actually I haven't find any way to use MaybeUnit that doesn't look hacky, and I want to avoid the usage of Option and unwraps in the code, once it bloats the binary with panic data.
#### Alternatives
1. Make the imported memory easily available inside Functions, ex: expose it in the Caller.
2. Use `#[repr(C)]` on `Store`, so we can safely transmute it.
Contributor guide
Research direction
Start by reading the Caller, Store, Memory, and Instance::new APIs referenced in the issue, then trace how imported memories are passed into functions. Compare the proposed Caller exposure with the unsafe MaybeUninit and Store transmute approach. Done means there is a supported way to access imported memory from functions without relying on unsafe Store layout assumptions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust, wasm
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100