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:
@@ -514,16 +514,17 @@ pub fn table_ops(
|
||||
) {
|
||||
let _ = env_logger::try_init();
|
||||
|
||||
let expected_drops = Arc::new(AtomicUsize::new(ops.num_params() as usize));
|
||||
let num_dropped = Arc::new(AtomicUsize::new(0));
|
||||
|
||||
{
|
||||
let mut config = fuzz_config.to_wasmtime();
|
||||
config.wasm_reference_types(true);
|
||||
config.consume_fuel(true);
|
||||
|
||||
let engine = Engine::new(&config).unwrap();
|
||||
let mut store = create_store(&engine);
|
||||
if fuzz_config.consume_fuel {
|
||||
store.add_fuel(u64::max_value()).unwrap();
|
||||
}
|
||||
store.add_fuel(100).unwrap();
|
||||
|
||||
let wasm = ops.to_wasm_binary();
|
||||
log_wasm(&wasm);
|
||||
@@ -532,18 +533,104 @@ pub fn table_ops(
|
||||
Err(_) => return,
|
||||
};
|
||||
|
||||
let mut linker = Linker::new(&engine);
|
||||
|
||||
// To avoid timeouts, limit the number of explicit GCs we perform per
|
||||
// test case.
|
||||
const MAX_GCS: usize = 5;
|
||||
|
||||
let num_gcs = AtomicUsize::new(0);
|
||||
let gc = Func::wrap(&mut store, move |mut caller: Caller<'_, StoreLimits>| {
|
||||
if num_gcs.fetch_add(1, SeqCst) < MAX_GCS {
|
||||
caller.gc();
|
||||
}
|
||||
});
|
||||
linker
|
||||
.define(
|
||||
"",
|
||||
"gc",
|
||||
// NB: use `Func::new` so that this can still compile on the old x86
|
||||
// backend, where `IntoFunc` isn't implemented for multi-value
|
||||
// returns.
|
||||
Func::new(
|
||||
&mut store,
|
||||
FuncType::new(
|
||||
vec![],
|
||||
vec![ValType::ExternRef, ValType::ExternRef, ValType::ExternRef],
|
||||
),
|
||||
{
|
||||
let num_dropped = num_dropped.clone();
|
||||
let expected_drops = expected_drops.clone();
|
||||
move |mut caller: Caller<'_, StoreLimits>, _params, results| {
|
||||
if num_gcs.fetch_add(1, SeqCst) < MAX_GCS {
|
||||
caller.gc();
|
||||
}
|
||||
|
||||
let instance = Instance::new(&mut store, &module, &[gc.into()]).unwrap();
|
||||
expected_drops.fetch_add(3, SeqCst);
|
||||
results[0] =
|
||||
Some(ExternRef::new(CountDrops(num_dropped.clone()))).into();
|
||||
results[1] =
|
||||
Some(ExternRef::new(CountDrops(num_dropped.clone()))).into();
|
||||
results[2] =
|
||||
Some(ExternRef::new(CountDrops(num_dropped.clone()))).into();
|
||||
Ok(())
|
||||
}
|
||||
},
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
linker
|
||||
.func_wrap("", "take_refs", {
|
||||
let expected_drops = expected_drops.clone();
|
||||
move |a: Option<ExternRef>, b: Option<ExternRef>, c: Option<ExternRef>| {
|
||||
// Do the assertion on each ref's inner data, even though it
|
||||
// all points to the same atomic, so that if we happen to
|
||||
// run into a use-after-free bug with one of these refs we
|
||||
// are more likely to trigger a segfault.
|
||||
if let Some(a) = a {
|
||||
let a = a.data().downcast_ref::<CountDrops>().unwrap();
|
||||
assert!(a.0.load(SeqCst) <= expected_drops.load(SeqCst));
|
||||
}
|
||||
if let Some(b) = b {
|
||||
let b = b.data().downcast_ref::<CountDrops>().unwrap();
|
||||
assert!(b.0.load(SeqCst) <= expected_drops.load(SeqCst));
|
||||
}
|
||||
if let Some(c) = c {
|
||||
let c = c.data().downcast_ref::<CountDrops>().unwrap();
|
||||
assert!(c.0.load(SeqCst) <= expected_drops.load(SeqCst));
|
||||
}
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
linker
|
||||
.define(
|
||||
"",
|
||||
"make_refs",
|
||||
// NB: use `Func::new` so that this can still compile on the old
|
||||
// x86 backend, where `IntoFunc` isn't implemented for
|
||||
// multi-value returns.
|
||||
Func::new(
|
||||
&mut store,
|
||||
FuncType::new(
|
||||
vec![],
|
||||
vec![ValType::ExternRef, ValType::ExternRef, ValType::ExternRef],
|
||||
),
|
||||
{
|
||||
let num_dropped = num_dropped.clone();
|
||||
let expected_drops = expected_drops.clone();
|
||||
move |_caller, _params, results| {
|
||||
expected_drops.fetch_add(3, SeqCst);
|
||||
results[0] =
|
||||
Some(ExternRef::new(CountDrops(num_dropped.clone()))).into();
|
||||
results[1] =
|
||||
Some(ExternRef::new(CountDrops(num_dropped.clone()))).into();
|
||||
results[2] =
|
||||
Some(ExternRef::new(CountDrops(num_dropped.clone()))).into();
|
||||
Ok(())
|
||||
}
|
||||
},
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let instance = linker.instantiate(&mut store, &module).unwrap();
|
||||
let run = instance.get_func(&mut store, "run").unwrap();
|
||||
|
||||
let args: Vec<_> = (0..ops.num_params())
|
||||
@@ -552,7 +639,7 @@ pub fn table_ops(
|
||||
let _ = run.call(&mut store, &args);
|
||||
}
|
||||
|
||||
assert_eq!(num_dropped.load(SeqCst), ops.num_params() as usize);
|
||||
assert_eq!(num_dropped.load(SeqCst), expected_drops.load(SeqCst));
|
||||
return;
|
||||
|
||||
struct CountDrops(Arc<AtomicUsize>);
|
||||
|
||||
Reference in New Issue
Block a user