Stream: general

Topic: Scenarios for multiple memories


view this post on Zulip Firestar99 (Nov 06 2026 at 18:55):

I'm trying to wrap my head around wasm memory, specifically the ability to have multiple memories.

My primary interest is building a "wasm engine" but it's not so much an engine, but just using wasm as a convenient IR to take Rust (and maybe C++) as input and transpile it into other IRs / ISAs. In this use-case I can also trust the wasm to be safe. But the design of my target relies on something that can be described as address spaces. Naturally, you'd think different wasm memories could be mapped as different address spaces. However, rustc has no support for address spaces and assumes one global address space.

So I'm currently thinking of this: Wrap non-0 address spaces in a XAlloc<T> type that provides get() -> T and set(T) functions that are implemented using asm! to read and write to non-0 wasm memory. However, you'd never be able to create a pointer to that data, since rust would assume it's in the generic address space.

Is such a design viable, given the current ecosystem, or can you think of any potential problems I could run into?

view this post on Zulip Chris Fallin (Nov 06 2026 at 19:00):

I don't think support for multimemory is good (or existent at all?) in LLVM; the idea has usually been that it's useful for either composition scenarios (merge multiple programs into one Wasm module, each with their own memory) or instrumentation scenarios (shadow store / metadata alongside the main memory).

wasm-tools supports multimemory natively so is a good option whenever wasm2wat/wat2wasm don't have support for some beyond-v1 Wasm feature; wasm-tools includes printer/parser (equivalent to the above) + more.

view this post on Zulip Firestar99 (Nov 06 2026 at 19:10):

shadow store / metadata alongside the main memory

I also have another crazier design: Remap all u32s to (u32, u32) (and similar), with the first value describing the address space. All normal constants initialize the address space to 0, certain intrinsics may return a different address space, e.g. XAlloc<T>::deref(). Propagate this address space though all integer operations, with an address space of 0 serving as the "neutral element". So an addition would just add the address spaces, whereas a multiplication with an operand having the address space of 0 would multiply by 1. Then on memory access, match on the address space to select from a number of load/store instruction for all the different address space. As the address space is derived purely from constants and operations on them, I'd expect const propagation to elliminate most of the additional address space tracking overhead (given plenty of inlining, exhaustive inlining would be viable here).
However, this requires a notion of provenance, that wasm doesn't have. This technically makes this entire approach UB, but it may still work when you're compiling from Rust or C++ to wasm.

view this post on Zulip Adam Bratschi-Kaye (he) (Nov 06 2026 at 19:54):

Can you use inline asm within rustc to declare and use other memories?

Last I looked this wasn't possible (no asm! support for Wasm). But if you want the address space separation then you might also want to consider components:

So I'm currently thinking of this: Wrap non-0 address spaces in a XAlloc<T> type that provides get() -> T and set(T) functions that are implemented using asm! to read and write to non-0 wasm memory.

For example, you could do this by having component A import a WIT interface with get and set and component B export that same interface. Then each one can be implemented in rust or C++ and they can be combined with wac plug and you'd have the address space separation you're thinking of. But the WIT interface wouldn't be generic over T.

view this post on Zulip fitzgen (he/him) (Nov 06 2026 at 20:46):

you can write .s files in wasm-ld's pseudo WAT assembly text format and link them in. that's almost inline asm, but not quite

view this post on Zulip bjorn3 (Nov 07 2026 at 13:38):

And rustc does support inline asm for wasm on nightly. Though specifically LLVM's custom wasm assembly, not the standard wat format. It hasn't been decided if we want to change that to wat or not. LLVM doesn't support wat, but wat is much better specified that what LLVM does.


Last updated: Nov 19 2026 at 20:05 UTC