bytecodealliance / bytecodealliance/wasmtime

Importing host memory as `Memory` in wasmtime

Open
#7,193 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
18.6k
Forks
1.8k
Avg merge
1d 19h
Merged PRs (30d)
121

Description

#### Feature

Allow wasmtime to create memory from a host pointer.

#### Benefit

Mainly to avoid copies where the store cannot own the memory.

In my use case I'd like to avoid an additional copy when writing into a wgpu buffer that is exposed inside of wasm.

In this case I can't assume any old host allocated buffer will work because the driver may need to allocate host memory in a specific way to allow the memory to be uploaded to the GPU. For OpenGL I have no control over how the GPU upload buffer memory is allocated and as such would always need a copy. For Vulkan there is an exception, but can't blindly assume it's possible to get memory from wasm and use it right in vulkan for buffer upload.[^vk_exception]

As a result of these limitations I would need to do a copy anyways.

#### Implementation

The proposed API could look something like this (in Rust)
```rust
impl Memory {
pub unsafe fn import_memory(store: impl AsContextMut, ty: MemoryType, pointer: *mut u8) -> Result { ... }
}
```

This would be an unsafe API since the wasm runtime would need exclusive access of the memory (which can't really be guaranteed on every platform). Also the caller must ensure the pointer is valid as long as wasm is able to access that memory.

Because the memory was allocated by the host, wasmtime can't resize the memory itself. This means that `Memory::grow` would need to unconditionally fail. What I'm not sure about is freeing a `Memory` instance from the host. This would probably also be unsafe.

There is also the question of whether wasmtime can architecturally handle this type of API without some internal reworks.

#### Alternatives

I can't think up any other alternatives to this problem except for additional copies. The fact that the graphics API must allocate memory

[^vk_exception]: For Vulkan it is possible to use `VK_EXT_external_memory_host`. This however comes with some restrictions: alignment being the main one (which is probably fine given wasm needs 64k blocks of memory). However not every hardware vendor can implement that extension. Intel, Broadcom and Qualcomm seem to be incapable of supporting it while some older AMD and Nvidia hardware can't implement it.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.