Fix a use-after-free of trampoline code #2408
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This commit fixes an issue with wasmtime where it was possible for a
trampoline from one module to get used for another module after it was
freed. This issue arises because we register a module's native
trampolines before it's fully instantiated, which is a fallible
process. Some fallibility is predictable, such as import type
mismatches, but other fallibility is less predictable, such as failure
to allocate a linear memory.
The problem happened when a module was registered with a
Store
,retaining information about its trampolines, but then instantiation
failed and the module's code was never persisted within the
Store
.Unlike as documented in #2374 the
Module
inside anInstance
is notthe primary way to hold on to a module's code, but rather the
Arc<ModuleCode>
is persisted within the global frame information offon the side. This persistence only made its way into the store through
the
Box<Any>
field ofInstanceHandle
, but that's never made ifinstantiation fails during import matching.
The fix here is to build on the refactoring of #2407 to not store module
code in frame information but rather explicitly in the
Store
.Registration is now deferred until just-before an instance handle is
created, and during module registration we insert the
Arc<ModuleCode>
into a set stored within the
Store
.