A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Run `struct.wast` GC test under MIRI+Pulley in CI (#13411)
Nick Fitzgeraldc9cfaf25ebbe
diff --git a/.github/workflows/main.yml b/.github/workflows/main.ymlindex 5cc06e8ec0..9b0b67249b 100644--- a/.github/workflows/main.yml+++ b/.github/workflows/main.yml@@ -1295,6 +1295,9 @@ jobs:- crate: "wasmtime-internal-core --all-features"- script: ./ci/miri-provenance-test.sh- script: ./ci/miri-wast.sh ./tests/spec_testsuite/table.wast+ - script: "./ci/miri-wast.sh ./tests/spec_testsuite/struct.wast -Ccollector=null"+ - script: "./ci/miri-wast.sh ./tests/spec_testsuite/struct.wast -Ccollector=copying"+ - script: "./ci/miri-wast.sh ./tests/spec_testsuite/struct.wast -Ccollector=drc"needs: determineif: needs.determine.outputs.test-miri && github.repository == 'bytecodealliance/wasmtime'name: Miridiff --git a/crates/wasmtime/src/runtime/store.rs b/crates/wasmtime/src/runtime/store.rsindex 73adf8cd22..72af9ad1b5 100644--- a/crates/wasmtime/src/runtime/store.rs+++ b/crates/wasmtime/src/runtime/store.rs@@ -1824,13 +1824,13 @@ impl StoreOpaque {assert!(self.gc_store.is_none());assert_eq!(- self.vm_store_context.gc_heap.base.as_non_null(),+ self.vm_store_context.gc_heap.get_mut().base.as_non_null(),NonNull::dangling(),);- assert_eq!(self.vm_store_context.gc_heap.current_length(), 0);+ assert_eq!(self.vm_store_context.gc_heap.get_mut().current_length(), 0);let gc_store = allocate_gc_store(self, limiter).await?;- self.vm_store_context.gc_heap = gc_store.vmmemory_definition();+ *self.vm_store_context.gc_heap.get_mut() = gc_store.vmmemory_definition();return Ok(self.gc_store.insert(gc_store));#[cfg(feature = "gc")]diff --git a/crates/wasmtime/src/runtime/store/gc.rs b/crates/wasmtime/src/runtime/store/gc.rsindex 066169971a..9b8eec02b4 100644--- a/crates/wasmtime/src/runtime/store/gc.rs+++ b/crates/wasmtime/src/runtime/store/gc.rs@@ -276,7 +276,7 @@ impl StoreOpaque {.await?.ok_or_else(|| format_err!("failed to grow GC heap"))?;}- heap.store.vm_store_context.gc_heap = heap.memory.vmmemory();+ *heap.store.vm_store_context.gc_heap.get_mut() = heap.memory.vmmemory();let new_size_in_bytes = u64::try_from(heap.memory.byte_size()).unwrap();assert!(new_size_in_bytes > current_size_in_bytes);diff --git a/crates/wasmtime/src/runtime/vm.rs b/crates/wasmtime/src/runtime/vm.rsindex d3fe27fc5b..3a57c66547 100644--- a/crates/wasmtime/src/runtime/vm.rs+++ b/crates/wasmtime/src/runtime/vm.rs@@ -151,9 +151,9 @@ mod cow_disabled;#[cfg(has_virtual_memory)]mod mmap;-#[cfg(feature = "gc-null")]+#[allow(unused, reason = "hard to cfg on/off, weird feature interactions")]mod send_sync_unsafe_cell;-#[cfg(feature = "gc-null")]+#[allow(unused, reason = "hard to cfg on/off, weird feature interactions")]pub use send_sync_unsafe_cell::SendSyncUnsafeCell;cfg_if::cfg_if! {diff --git a/crates/wasmtime/src/runtime/vm/gc/enabled/copying.rs b/crates/wasmtime/src/runtime/vm/gc/enabled/copying.rsindex 9c21e2d726..cbd623c240 100644--- a/crates/wasmtime/src/runtime/vm/gc/enabled/copying.rs+++ b/crates/wasmtime/src/runtime/vm/gc/enabled/copying.rs@@ -14,12 +14,13 @@ use super::VMArrayRef;use super::trace_info::{TraceInfo, TraceInfos};use crate::runtime::vm::{ExternRefHostDataId, ExternRefHostDataTable, GarbageCollection, GcHeap, GcHeapObject,- GcProgress, GcRootsIter, GcRuntime, TypedGcRef, VMExternRef, VMGcHeader, VMGcRef,- VMMemoryDefinition,+ GcProgress, GcRootsIter, GcRuntime, SendSyncUnsafeCell, TypedGcRef, VMExternRef, VMGcHeader,+ VMGcRef, VMMemoryDefinition,};use crate::{Engine, prelude::*};-use core::sync::atomic::AtomicUsize;-use core::{alloc::Layout, any::Any, mem, num::NonZeroU32, ptr::NonNull};+use core::{+ alloc::Layout, any::Any, mem, num::NonZeroU32, ptr::NonNull, sync::atomic::AtomicUsize,+};use wasmtime_environ::copying::{ALIGN, ARRAY_LENGTH_OFFSET, CopyingTypeLayouts, HEADER_COPIED_BIT,};@@ -175,14 +176,41 @@ unsafe impl GcHeapObject for VMCopyingExternRef {////// NB: Layout is defined by constants in `wasmtime_environ::copying`. Keep in/// sync!+#[derive(Default)]#[repr(C)]-struct VMCopyingHeapData {+struct VMCopyingHeapDataInner {/// Current bump pointer (index into the GC heap).bump_ptr: u32,/// End of the active semi-space.active_space_end: u32,}+#[derive(Default)]+#[repr(transparent)]+struct VMCopyingHeapData {+ inner: SendSyncUnsafeCell<VMCopyingHeapDataInner>,+}++impl VMCopyingHeapData {+ fn bump_ptr(&self) -> u32 {+ // Safety: `inner` is valid to read from.+ unsafe { (*self.inner.get()).bump_ptr }+ }++ fn set_bump_ptr(&mut self, val: u32) {+ self.inner.get_mut().bump_ptr = val;+ }++ fn active_space_end(&self) -> u32 {+ // Safety: `inner` is valid to read from.+ unsafe { (*self.inner.get()).active_space_end }+ }++ fn set_active_space_end(&mut self, val: u32) {+ self.inner.get_mut().active_space_end = val;+ }+}+/// Get a typed reference to a copying-collector object from a raw `VMGcRef`.fn copying_ref(gc_ref: &VMGcRef) -> &TypedGcRef<VMCopyingHeader> {debug_assert!(!gc_ref.is_i31());@@ -264,10 +292,7 @@ impl CopyingHeap {no_gc_count: 0,memory: None,vmmemory: None,- vmctx_data: VMCopyingHeapData {- bump_ptr: 0,- active_space_end: 0,- },+ vmctx_data: VMCopyingHeapData::default(),active_space_start: 0,idle_space_start: 0,idle_space_end: 0,@@ -278,19 +303,19 @@ impl CopyingHeap {}fn bump_ptr(&self) -> u32 {- self.vmctx_data.bump_ptr+ self.vmctx_data.bump_ptr()}fn set_bump_ptr(&mut self, val: u32) {- self.vmctx_data.bump_ptr = val;+ self.vmctx_data.set_bump_ptr(val);}fn active_space_end(&self) -> u32 {- self.vmctx_data.active_space_end+ self.vmctx_data.active_space_end()}fn set_active_space_end(&mut self, val: u32) {- self.vmctx_data.active_space_end = val;+ self.vmctx_data.set_active_space_end(val);}fn capacity(&self) -> u32 {@@ -881,8 +906,7 @@ unsafe impl GcHeap for CopyingHeap {}unsafe fn vmctx_gc_heap_data(&self) -> NonNull<u8> {- let ptr: *const VMCopyingHeapData = &self.vmctx_data;- NonNull::new(ptr as *mut u8).unwrap()+ NonNull::from(&self.vmctx_data).cast::<u8>()}fn take_memory(&mut self) -> crate::vm::Memory {@@ -1121,7 +1145,7 @@ mod tests {fn vm_copying_heap_data_bump_ptr_offset() {assert_eq!(HostPtr.vmcopying_heap_data_bump_ptr() as usize,- core::mem::offset_of!(VMCopyingHeapData, bump_ptr),+ core::mem::offset_of!(VMCopyingHeapDataInner, bump_ptr),);}@@ -1129,7 +1153,7 @@ mod tests {fn vm_copying_heap_data_active_space_end_offset() {assert_eq!(HostPtr.vmcopying_heap_data_active_space_end() as usize,- core::mem::offset_of!(VMCopyingHeapData, active_space_end),+ core::mem::offset_of!(VMCopyingHeapDataInner, active_space_end),);}@@ -1139,6 +1163,10 @@ mod tests {HostPtr.size_of_vmcopying_heap_data() as usize,core::mem::size_of::<VMCopyingHeapData>(),);+ assert_eq!(+ HostPtr.size_of_vmcopying_heap_data() as usize,+ core::mem::size_of::<VMCopyingHeapDataInner>(),+ );}#[test]@@ -1147,5 +1175,9 @@ mod tests {HostPtr.align_of_vmcopying_heap_data() as usize,core::mem::align_of::<VMCopyingHeapData>(),);+ assert_eq!(+ HostPtr.align_of_vmcopying_heap_data() as usize,+ core::mem::align_of::<VMCopyingHeapDataInner>(),+ );}}diff --git a/crates/wasmtime/src/runtime/vm/send_sync_unsafe_cell.rs b/crates/wasmtime/src/runtime/vm/send_sync_unsafe_cell.rsindex 2e1fc47f6a..f1b16d1a94 100644--- a/crates/wasmtime/src/runtime/vm/send_sync_unsafe_cell.rs+++ b/crates/wasmtime/src/runtime/vm/send_sync_unsafe_cell.rs@@ -2,6 +2,7 @@ use core::cell::UnsafeCell;/// A wrapper around `UnsafeCell` that implements `Send` and `Sync` for types/// that are themselves `Send` and `Sync`.+#[derive(Default)]pub struct SendSyncUnsafeCell<T>(UnsafeCell<T>);// Safety: `T` is `Send` and users guarantee that any pointers derived from thediff --git a/crates/wasmtime/src/runtime/vm/vmcontext.rs b/crates/wasmtime/src/runtime/vm/vmcontext.rsindex d2791a1498..eedf3c7d9b 100644--- a/crates/wasmtime/src/runtime/vm/vmcontext.rs+++ b/crates/wasmtime/src/runtime/vm/vmcontext.rs@@ -1211,7 +1211,7 @@ pub struct VMStoreContext {pub stack_limit: UnsafeCell<usize>,/// The `VMMemoryDefinition` for this store's GC heap.- pub gc_heap: VMMemoryDefinition,+ pub gc_heap: UnsafeCell<VMMemoryDefinition>,/// The value of the frame pointer register in the trampoline used/// to call from Wasm to the host.@@ -1372,10 +1372,10 @@ impl Default for VMStoreContext {epoch_deadline: UnsafeCell::new(0),execution_version: 0,stack_limit: UnsafeCell::new(usize::max_value()),- gc_heap: VMMemoryDefinition {+ gc_heap: UnsafeCell::new(VMMemoryDefinition {base: NonNull::dangling().into(),current_length: AtomicUsize::new(0),- },+ }),last_wasm_exit_trampoline_fp: UnsafeCell::new(0),last_wasm_exit_pc: UnsafeCell::new(0),last_wasm_entry_fp: UnsafeCell::new(0),