alexcrichton opened PR #10880 from alexcrichton:refactor-vmctx-plus-offset to bytecodealliance:main:
This commit merges two helper methods to calculate an internal pointer with a
VMContext. Historically these two methods mapped different levels of mutability but nowadays there's no need for this. The provenance/mutability ofVMContextmemory is independent of&selfmeaning that it's safe to create/mutate through aNonNullin the vmctx area through a&self-derived pointer.The purpose of this commit is to avoid needlessly requiring
&mut selfthroughout methods onInstance. Currently many of them require mutability to only create a pointer, and this mutability is a burden that need not be required.<!--
Please make sure you include the following information:
If this work has been discussed elsewhere, please include a link to that
conversation. If it was discussed in an issue, just mention "issue #...".Explain why this change is needed. If the details are in an issue already,
this can be brief.Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.htmlPlease ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->
alexcrichton requested fitzgen for a review on PR #10880.
alexcrichton requested wasmtime-core-reviewers for a review on PR #10880.
alexcrichton commented on PR #10880:
Hm ok thinking more about this is mainly means that
Syncis no longer correctly inferred, asInstanceshould no longer beSync. I feel like we probably do want to keep itSync, though, so now I'm souring a bit on this approach.I think I have an idea of how to tackle this, improve safety, and keep
Sync, so converting this to a draft.
alexcrichton updated PR #10880.
alexcrichton edited PR #10880:
Currently acquiring a mutable pointer to
VMContextmemory requires
&mut Instancewhich for many methods is a tall ask. Some methods are
basically just reading data structures and shuffling bits around, but
requiring a mutable instance prohibits their use in some contexts. The
general goal of this commit is to make it possible to read, but not
necessarily manipulate data structures with&selfinstead of requiring
&mut.Specifically this commit redefines the preexisting
vmctx_plus_offset{,_mut}methods as returning a safe reference, not a
raw reference. This ensures that the lifetime of the returned reference
is scoped properly and it inherits the mutability ofselfto retain
beingSendandSyncby default. To make up for lost ground though
this commit additionally adds a newvmctx_plus_offset_rawmethod which
these prior two methods delegate to. This new method returns a
NonNull<T>and is the primary way of acquiring a raw pointer to
VMContextfields. This new method notably takes&selfas an
argument.The end result is that small/quick mutations of a
VMContextare able
to keep usingvmctx_plus_offset{,_mut}with a little extra safety, but
many callers have migrated to the*_rawvariant which no longer
requires&mut self. This will end up being used in further commits and
methods aren't switched over just yet.It's worth noting the relevance to https://github.com/advisories/GHSA-ch89-5g45-qwc7 in this commit as
well. Historically the acquisition of a mutable pointer from&selfled
to true undefined behavior at runtime resulting in incorrect execution.
This is naively moving right back to such a model where a&self-based
method is returning a pointer through which a mutation is happening, but
this is crucially different in how the basevmctxpointer is created.
Previously it was calculated as an offset from&self, but now the
address is calculated from&selfbut the provenance comes from a
known-mutable pointer. This construction means that annotations on
&selfare not applicable to thevmctxpointer itself and thus not
applicable to the returned pointer. In the end this means that it should
be safe to acquire mutable pointers to vmctx memory from&selfon an
instance.
Last updated: Dec 06 2025 at 06:05 UTC