Skip to content

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

Small clean ups after evaluating element segments only once (#13018)

Nick Fitzgerald47dcd1c8a0e6
diff --git a/crates/wasmtime/src/runtime/store.rs b/crates/wasmtime/src/runtime/store.rs
index 67365fc3c5..cd11fd2a73 100644
--- a/crates/wasmtime/src/runtime/store.rs
+++ b/crates/wasmtime/src/runtime/store.rs
@@ -2269,7 +2269,10 @@ impl StoreOpaque {
// SAFETY: the instance's GC roots will remain valid for the
// duration of this GC cycle.
unsafe {
- instance.handle.get_mut().trace_roots(gc_roots_list);
+ instance
+ .handle
+ .get_mut()
+ .trace_element_segment_roots(gc_roots_list);
}
}
log::trace!("End trace GC roots :: instance");
diff --git a/crates/wasmtime/src/runtime/vm/instance.rs b/crates/wasmtime/src/runtime/vm/instance.rs
index 2125cf8b6b..11f857ef4d 100644
--- a/crates/wasmtime/src/runtime/vm/instance.rs
+++ b/crates/wasmtime/src/runtime/vm/instance.rs
@@ -214,24 +214,23 @@ impl Instance {
Ok(ret)
}
- /// Trace GC roots inside this `Instance`.
- ///
- /// NB: This instance's `vmctx` roots are traced separately in
- /// `Store::trace_vmctx_roots`.
+ /// Trace element segment GC roots inside this `Instance`.
///
/// # Safety
///
/// This instance must live for the duration of the associated GC cycle.
#[cfg(feature = "gc")]
- pub(crate) unsafe fn trace_roots(self: Pin<&mut Self>, gc_roots: &mut crate::vm::GcRootsList) {
- // SAFETY: not moving data out of `self`.
- let passive_elements = &mut unsafe { self.get_unchecked_mut() }.passive_elements;
-
- for segment in passive_elements {
+ pub(crate) unsafe fn trace_element_segment_roots(
+ self: Pin<&mut Self>,
+ gc_roots: &mut crate::vm::GcRootsList,
+ ) {
+ for segment in self.passive_elements_mut().iter_mut() {
if let Some((wasmtime_environ::NeedsGcRooting::Yes, elems)) = segment {
for e in elems {
- let root: SendSyncPtr<ValRaw> = SendSyncPtr::from(e);
- let root: SendSyncPtr<super::VMGcRef> = root.cast();
+ let Some(root) = e.as_vmgc_ref_ptr() else {
+ continue;
+ };
+ let root: SendSyncPtr<super::VMGcRef> = root.into();
// Safety: We know this is a type that needs GC rooting and
// the lifetime is implied by our safety contract.
diff --git a/crates/wasmtime/src/runtime/vm/vmcontext.rs b/crates/wasmtime/src/runtime/vm/vmcontext.rs
index aac495af2a..0125218684 100644
--- a/crates/wasmtime/src/runtime/vm/vmcontext.rs
+++ b/crates/wasmtime/src/runtime/vm/vmcontext.rs
@@ -1738,6 +1738,17 @@ impl ValRaw {
assert!(cfg!(feature = "gc") || exnref == 0);
exnref
}
+
+ /// Convert this `&ValRaw` into a pointer to its inner `VMGcRef`.
+ #[cfg(feature = "gc")]
+ pub(crate) fn as_vmgc_ref_ptr(&self) -> Option<NonNull<crate::vm::VMGcRef>> {
+ if self.get_anyref() == 0 {
+ return None;
+ }
+ let ptr = &raw const self.anyref;
+ let ptr = NonNull::new(ptr.cast_mut()).unwrap();
+ Some(ptr.cast())
+ }
}
/// An "opaque" version of `VMContext` which must be explicitly casted to a