Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Optimize poison check for GCs while fuzzing (#15021)

Alex Crichton48c6579c9a1e
diff --git a/crates/wasmtime/src/runtime/vm/gc/enabled.rs b/crates/wasmtime/src/runtime/vm/gc/enabled.rs
index c5800046da..9d228521be 100644
--- a/crates/wasmtime/src/runtime/vm/gc/enabled.rs
+++ b/crates/wasmtime/src/runtime/vm/gc/enabled.rs
@@ -29,6 +29,18 @@ mod copying;
#[cfg(feature = "gc-copying")]
pub use copying::*;
+/// Returns whether every byte in `bytes` is [`wasmtime_environ::POISON`].
+#[cfg(any(feature = "gc-drc", feature = "gc-copying"))]
+fn is_poisoned(bytes: &[u8]) -> bool {
+ // Check `bytes` in large-ish chunks. This forces compiling down to `memcmp`
+ // in both debug mode and with sanitizers enabled (fuzzing).
+ const CHUNK: usize = 4096;
+ static POISONED: [u8; CHUNK] = [wasmtime_environ::POISON; CHUNK];
+ bytes
+ .chunks(CHUNK)
+ .all(|chunk| chunk == &POISONED[..chunk.len()])
+}
+
/// A hasher that doesn't hash, for use in the trace-info hash map, where we are
/// just using scalar keys and aren't overly concerned with collision-based DoS.
#[derive(Default)]
diff --git a/crates/wasmtime/src/runtime/vm/gc/enabled/copying.rs b/crates/wasmtime/src/runtime/vm/gc/enabled/copying.rs
index 41e67b6beb..40636a4ac1 100644
--- a/crates/wasmtime/src/runtime/vm/gc/enabled/copying.rs
+++ b/crates/wasmtime/src/runtime/vm/gc/enabled/copying.rs
@@ -11,6 +11,7 @@
//! This collector does not require any read or write barriers.
use super::VMArrayRef;
+use super::is_poisoned;
use super::trace_infos::TraceInfos;
use crate::runtime::vm::{
ExternRefHostDataId, GarbageCollection, GcHeap, GcHeapObject, GcProgress, GcRootsIter,
@@ -863,7 +864,7 @@ unsafe impl GcHeap for CopyingHeap {
let start = usize::try_from(gc_ref.heap_index()?.get()).unwrap();
let slice = &self.heap_slice()[start..][..layout.size()];
gc_assert!(
- slice.iter().all(|&b| b == POISON),
+ is_poisoned(slice),
"newly allocated GC object at index {start} is not fully poisoned; \
freed memory was corrupted",
);
diff --git a/crates/wasmtime/src/runtime/vm/gc/enabled/drc.rs b/crates/wasmtime/src/runtime/vm/gc/enabled/drc.rs
index e841fe93bf..a0d2ab1bd0 100644
--- a/crates/wasmtime/src/runtime/vm/gc/enabled/drc.rs
+++ b/crates/wasmtime/src/runtime/vm/gc/enabled/drc.rs
@@ -46,6 +46,7 @@
use super::VMArrayRef;
use super::free_list::FreeList;
+use super::is_poisoned;
use super::trace_infos::TraceInfos;
use crate::hash_set::HashSet;
use crate::runtime::vm::{
@@ -529,7 +530,7 @@ impl DrcHeap {
let size = usize::try_from(len).unwrap();
let slice = &self.heap_slice()[start..][..size];
assert!(
- slice.iter().all(|&b| b == POISON),
+ is_poisoned(slice),
"free block at heap index {start} (size {size}) is not fully poisoned",
);
}
@@ -1090,7 +1091,7 @@ unsafe impl GcHeap for DrcHeap {
let start = usize::try_from(gc_ref.heap_index()?.get())?;
let slice = &self.heap_slice()[start..][..layout.size()];
gc_assert!(
- slice.iter().all(|&b| b == POISON),
+ is_poisoned(slice),
"newly allocated GC object at index {start} is not fully poisoned; \
freed memory was corrupted",
);