Fix a use-after-free bug when passing ExternRefs to Wasm
We _must not_ trigger a GC when moving refs from host code into Wasm (e.g. returned from a host function or passed as arguments to a Wasm function). After insertion into the table, this reference is no longer rooted. If multiple references are being sent from the host into Wasm and we allowed GCs during insertion, then the following events could happen: * Reference A is inserted into the activations table. This does not trigger a GC, but does fill the table to capacity. * The caller's reference to A is removed. Now the only reference to A is from the activations table. * Reference B is inserted into the activations table. Because the table is at capacity, a GC is triggered. * A is reclaimed because the only reference keeping it alive was the activation table's reference (it isn't inside any Wasm frames on the stack yet, so stack scanning and stack maps don't increment its reference count). * We transfer control to Wasm, giving it A and B. Wasm uses A. That's a use after free. To prevent uses after free, we cannot GC when moving refs into the `VMExternRefActivationsTable` because we are passing them from the host to Wasm. On the other hand, when we are *cloning* -- as opposed to moving -- refs from the host to Wasm, then it is fine to GC while inserting into the activations table, because the original referent that we are cloning from is still alive and rooting the ref.
This commit is contained in:
@@ -608,7 +608,7 @@ impl<'data> ModuleEnvironment<'data> {
|
||||
.funcs
|
||||
.push(FunctionMetadata {
|
||||
locals: locals.into_boxed_slice(),
|
||||
params: sig.params.iter().cloned().map(|i| i.into()).collect(),
|
||||
params: sig.params().iter().cloned().map(|i| i.into()).collect(),
|
||||
});
|
||||
}
|
||||
body.allow_memarg64(self.features.memory64);
|
||||
|
||||
Reference in New Issue
Block a user