V0ldek requested wasmtime-core-reviewers for a review on PR #15144.
V0ldek removed the review request for wasmtime-core-reviewers from PR #15144.
V0ldek opened PR #15144 from V0ldek:v0ldek/expose-linear-memory to bytecodealliance:main:
Currently, when a custom
MemoryCreatoris supplied, it creates memory instances when a module is instantiated, but there is no way for user code to access that instance. One can work around this issue with rather clumsy code around a creator that inspects what it returns during aLinker::instantiatecall, but it is very brittle.This is useful for extracting some properties of the created memory. As an example, in AnyBlox, we need to know the length of the external data that we attach via a map into the linear memory segment. With this the creator can also assign unique IDs to created memory that can then be inspected from the outside for things like caching and custom pooling.
The proposal is to simply expose the instance from the store. After #14885,
LinearMemoryextendsAny, so we can expose alinear_memoryfunction that extracts the inner box and upcasts the reference to a concreteMtype. This makes it so that only custom implementors providing their ownLinearMemoryimpl can access it this way and aware of the safety implications. Internal wasmtime implementations remain inaccessible from the public. The alternative was to expose a&dyn LinearMemorydirectly, but I feel like a) this increases the public API surface significantly, since anyone would be able to access the internal wasmtime memory instances, and b) would be pretty useless if you didn't know and downcast to the concrete type anyway.Safety implications are that accessing a reference to the memory while it is attached to a module could result in unsoundness if the user implements interior mutability on the memory and uses it incorrectly. However, the public contract remains tight: obtaining a reference to the memory borrows the store, so the ref cannot be held across actual runtime of the wasm module.
V0ldek requested cfallin for a review on PR #15144.
:thumbs_up: alexcrichton submitted PR review:
Nice idea :+1:
alexcrichton added PR #15144 Expose the LinearMemory instance from the store. to the merge queue
:check: alexcrichton merged PR #15144.
alexcrichton removed PR #15144 Expose the LinearMemory instance from the store. from the merge queue
Last updated: Nov 19 2026 at 20:05 UTC