A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Prevent using shared memories with `Memory` (#12022)
Alex Crichton1fcd09331444
diff --git a/crates/wasmtime/src/runtime/component/instance.rs b/crates/wasmtime/src/runtime/component/instance.rsindex 737b40a55c..fb1e99a09c 100644--- a/crates/wasmtime/src/runtime/component/instance.rs+++ b/crates/wasmtime/src/runtime/component/instance.rs@@ -928,11 +928,11 @@ impl<'a> Instantiator<'a> {}fn extract_memory(&mut self, store: &mut StoreOpaque, memory: &ExtractMemory) {- let mem = match lookup_vmexport(store, self.id, &memory.export) {- crate::runtime::vm::Export::Memory { memory, .. } => memory,+ let import = match lookup_vmexport(store, self.id, &memory.export) {+ crate::runtime::vm::Export::Memory(memory) => memory.vmimport(store),+ crate::runtime::vm::Export::SharedMemory(_, import) => import,_ => unreachable!(),};- let import = mem.vmimport(store);self.instance_mut(store).set_runtime_memory(memory.index, import.from.as_non_null());}@@ -1040,7 +1040,7 @@ impl<'a> Instantiator<'a> {return;}- let val = unsafe { crate::Extern::from_wasmtime_export(export, store) };+ let val = crate::Extern::from_wasmtime_export(export, store);let ty = DefinitionType::from(store, &val);crate::types::matching::MatchCx::new(module.engine()).definition(&expected, &ty)diff --git a/crates/wasmtime/src/runtime/coredump.rs b/crates/wasmtime/src/runtime/coredump.rsindex ac557b7f60..c62b316d9e 100644--- a/crates/wasmtime/src/runtime/coredump.rs+++ b/crates/wasmtime/src/runtime/coredump.rs@@ -41,7 +41,8 @@ impl WasmCoreDump {pub(crate) fn new(store: &mut StoreOpaque, backtrace: WasmBacktrace) -> WasmCoreDump {let modules: Vec<_> = store.modules().all_modules().cloned().collect();let instances: Vec<Instance> = store.all_instances().collect();- let store_memories: Vec<Memory> = store.all_memories().collect();+ let store_memories: Vec<Memory> =+ store.all_memories().filter_map(|m| m.unshared()).collect();let mut store_globals: Vec<Global> = vec![];store.for_each_global(|_store, global| store_globals.push(global));@@ -266,9 +267,13 @@ impl WasmCoreDump {let memories = instance.all_memories(store.0)- .collect::<Vec<_>>()- .into_iter()- .map(|(_i, memory)| memory_to_idx[&memory.hash_key(&store.0)])+ .filter_map(|(_, m)| m.unshared())+ .map(|memory| {+ memory_to_idx+ .get(&memory.hash_key(&store.0))+ .copied()+ .unwrap_or(u32::MAX)+ }).collect::<Vec<_>>();let globals = instancediff --git a/crates/wasmtime/src/runtime/externals.rs b/crates/wasmtime/src/runtime/externals.rsindex 2d6204d325..0873ad3c78 100644--- a/crates/wasmtime/src/runtime/externals.rs+++ b/crates/wasmtime/src/runtime/externals.rs@@ -125,18 +125,15 @@ impl Extern {}}- pub(crate) unsafe fn from_wasmtime_export(+ pub(crate) fn from_wasmtime_export(wasmtime_export: crate::runtime::vm::Export,store: &StoreOpaque,) -> Extern {match wasmtime_export {crate::runtime::vm::Export::Function(f) => Extern::Func(f),- crate::runtime::vm::Export::Memory { memory, shared } => {- if shared {- Extern::SharedMemory(SharedMemory::from_memory(memory, store))- } else {- Extern::Memory(memory)- }+ crate::runtime::vm::Export::Memory(m) => Extern::Memory(m),+ crate::runtime::vm::Export::SharedMemory(m, _) => {+ Extern::SharedMemory(crate::SharedMemory::from_raw(m, store.engine().clone()))}crate::runtime::vm::Export::Global(g) => Extern::Global(g),crate::runtime::vm::Export::Table(t) => Extern::Table(t),diff --git a/crates/wasmtime/src/runtime/instance.rs b/crates/wasmtime/src/runtime/instance.rsindex af3d13f850..3445a4a401 100644--- a/crates/wasmtime/src/runtime/instance.rs+++ b/crates/wasmtime/src/runtime/instance.rs@@ -480,7 +480,7 @@ impl Instance {// SAFETY: the store `id` owns this instance and all exports contained// within.let export = unsafe { self.id.get_mut(store).get_export_by_index_mut(id, entity) };- unsafe { Extern::from_wasmtime_export(export, store) }+ Extern::from_wasmtime_export(export, store)}/// Looks up an exported [`Func`] value by name.@@ -623,7 +623,7 @@ impl Instance {pub(crate) fn all_memories<'a>(&'a self,store: &'a StoreOpaque,- ) -> impl ExactSizeIterator<Item = (MemoryIndex, Memory)> + 'a {+ ) -> impl ExactSizeIterator<Item = (MemoryIndex, vm::ExportMemory)> + 'a {let store_id = store.id();store[self.id].all_memories(store_id)}@@ -716,8 +716,11 @@ impl OwnedImports {crate::runtime::vm::Export::Table(t) => {self.tables.push(t.vmimport(store));}- crate::runtime::vm::Export::Memory { memory, .. } => {- self.memories.push(memory.vmimport(store));+ crate::runtime::vm::Export::Memory(m) => {+ self.memories.push(m.vmimport(store));+ }+ crate::runtime::vm::Export::SharedMemory(_, vmimport) => {+ self.memories.push(*vmimport);}crate::runtime::vm::Export::Tag(t) => {self.tags.push(t.vmimport(store));diff --git a/crates/wasmtime/src/runtime/memory.rs b/crates/wasmtime/src/runtime/memory.rsindex bdf670eb1d..96404b2d33 100644--- a/crates/wasmtime/src/runtime/memory.rs+++ b/crates/wasmtime/src/runtime/memory.rs@@ -1,6 +1,6 @@use crate::Trap;use crate::prelude::*;-use crate::runtime::vm::{self, VMStore};+use crate::runtime::vm::{self, ExportMemory, VMStore};use crate::store::{StoreInstanceId, StoreOpaque, StoreResourceLimiter};use crate::trampoline::generate_memory_export;use crate::{AsContext, AsContextMut, Engine, MemoryType, StoreContext, StoreContextMut};@@ -285,7 +285,13 @@ impl Memory {limiter: Option<&mut StoreResourceLimiter<'_>>,ty: MemoryType,) -> Result<Memory> {- generate_memory_export(store, limiter, &ty, None).await+ if ty.is_shared() {+ bail!("shared memories must be created through `SharedMemory`")+ }+ Ok(generate_memory_export(store, limiter, &ty, None)+ .await?+ .unshared()+ .unwrap())}/// Returns the underlying type of this memory.@@ -638,7 +644,14 @@ impl Memory {}}- pub(crate) fn from_raw(instance: StoreInstanceId, index: DefinedMemoryIndex) -> Memory {+ /// Creates a new memory from its raw component parts.+ ///+ /// # Safety+ ///+ /// The caller must ensure that the memory pointed to by `instance` and+ /// `index` is not a shared memory. For that `SharedMemory` must be used+ /// instead.+ pub(crate) unsafe fn from_raw(instance: StoreInstanceId, index: DefinedMemoryIndex) -> Memory {Memory { instance, index }}@@ -649,12 +662,7 @@ impl Memory {}pub(crate) fn vmimport(&self, store: &StoreOpaque) -> crate::runtime::vm::VMMemoryImport {- let instance = &store[self.instance];- crate::runtime::vm::VMMemoryImport {- from: instance.memory_ptr(self.index).into(),- vmctx: instance.vmctx().into(),- index: self.index,- }+ store[self.instance].get_defined_memory_vmimport(self.index)}pub(crate) fn comes_from_same_store(&self, store: &StoreOpaque) -> bool {@@ -804,7 +812,6 @@ pub unsafe trait MemoryCreator: Send + Sync {pub struct SharedMemory {vm: crate::runtime::vm::SharedMemory,engine: Engine,- page_size_log2: u8,}impl SharedMemory {@@ -820,13 +827,11 @@ impl SharedMemory {let tunables = engine.tunables();let ty = ty.wasmtime_memory();- let page_size_log2 = ty.page_size_log2;let memory = crate::runtime::vm::SharedMemory::new(ty, tunables)?;Ok(Self {vm: memory,engine: engine.clone(),- page_size_log2,})}@@ -838,7 +843,7 @@ impl SharedMemory {/// Returns the size, in WebAssembly pages, of this wasm memory.pub fn size(&self) -> u64 {let byte_size = u64::try_from(self.data_size()).unwrap();- let page_size = u64::from(self.page_size());+ let page_size = self.page_size();byte_size / page_size}@@ -849,9 +854,8 @@ impl SharedMemory {/// `1`. Future extensions might allow any power of two as a page size.////// [the custom-page-sizes proposal]: https://github.com/WebAssembly/custom-page-sizes- pub fn page_size(&self) -> u32 {- debug_assert!(self.page_size_log2 == 0 || self.page_size_log2 == 16);- 1 << self.page_size_log2+ pub fn page_size(&self) -> u64 {+ self.ty().page_size()}/// Returns the byte length of this memory.@@ -912,7 +916,7 @@ impl SharedMemory {Some((old_size, _new_size)) => {// For shared memory, the `VMMemoryDefinition` is updated inside// the locked region.- Ok(u64::try_from(old_size).unwrap() / u64::from(self.page_size()))+ Ok(u64::try_from(old_size).unwrap() / self.page_size())}None => bail!("failed to grow memory by `{delta}`"),}@@ -1010,41 +1014,22 @@ impl SharedMemory {// Note `vm::assert_ready` shouldn't panic here because this isn't// actually allocating any new memory (also no limiter), so resource// limiting shouldn't kick in.- vm::assert_ready(generate_memory_export(+ let memory = vm::assert_ready(generate_memory_export(store,None,&self.ty(),Some(&self.vm),))- .unwrap()- .vmimport(store)+ .unwrap();+ match memory {+ ExportMemory::Unshared(_) => unreachable!(),+ ExportMemory::Shared(_shared, vmimport) => vmimport,+ }}- /// Create a [`SharedMemory`] from an [`ExportMemory`] definition. This- /// function is available to handle the case in which a Wasm module exports- /// shared memory and the user wants host-side access to it.- pub(crate) fn from_memory(mem: Memory, store: &StoreOpaque) -> Self {- #![cfg_attr(- not(feature = "threads"),- expect(- unused_variables,- unreachable_code,- reason = "definitions cfg'd to dummy",- )- )]-- let instance = mem.instance.get(store);- let memory = instance.get_defined_memory(mem.index);- let module = instance.env_module();- let page_size_log2 = module.memories[module.memory_index(mem.index)].page_size_log2;- match memory.as_shared_memory() {- Some(mem) => Self {- vm: mem.clone(),- engine: store.engine().clone(),- page_size_log2,- },- None => panic!("unable to convert from a shared memory"),- }+ /// Creates a [`SharedMemory`] from its constituent parts.+ pub(crate) fn from_raw(vm: crate::runtime::vm::SharedMemory, engine: Engine) -> Self {+ SharedMemory { vm, engine }}}diff --git a/crates/wasmtime/src/runtime/store.rs b/crates/wasmtime/src/runtime/store.rsindex b38e4f806d..710ff8ca68 100644--- a/crates/wasmtime/src/runtime/store.rs+++ b/crates/wasmtime/src/runtime/store.rs@@ -97,15 +97,16 @@ use crate::runtime::vm::GcRootsList;use crate::runtime::vm::VMContRef;use crate::runtime::vm::mpk::ProtectionKey;use crate::runtime::vm::{- self, GcStore, Imports, InstanceAllocationRequest, InstanceAllocator, InstanceHandle,- Interpreter, InterpreterRef, ModuleRuntimeInfo, OnDemandInstanceAllocator, SendSyncPtr,- SignalHandler, StoreBox, Unwind, VMContext, VMFuncRef, VMGcRef, VMStore, VMStoreContext,+ self, ExportMemory, GcStore, Imports, InstanceAllocationRequest, InstanceAllocator,+ InstanceHandle, Interpreter, InterpreterRef, ModuleRuntimeInfo, OnDemandInstanceAllocator,+ SendSyncPtr, SignalHandler, StoreBox, Unwind, VMContext, VMFuncRef, VMGcRef, VMStore,+ VMStoreContext,};use crate::trampoline::VMHostGlobalContext;use crate::{Engine, Module, Val, ValRaw, module::ModuleRegistry};#[cfg(feature = "gc")]use crate::{ExnRef, Rooted};-use crate::{Global, Instance, Memory, Table, Uninhabited};+use crate::{Global, Instance, Table, Uninhabited};use alloc::sync::Arc;use core::fmt;use core::marker;@@ -1728,7 +1729,7 @@ impl StoreOpaque {}/// Get all memories (host- or Wasm-defined) within this store.- pub fn all_memories<'a>(&'a self) -> impl Iterator<Item = Memory> + 'a {+ pub fn all_memories<'a>(&'a self) -> impl Iterator<Item = ExportMemory> + 'a {// NB: Host-created memories have dummy instances. Therefore, we can get// all memories in the store by iterating over all instances (including// dummy instances) and getting each of their defined memories.diff --git a/crates/wasmtime/src/runtime/trampoline.rs b/crates/wasmtime/src/runtime/trampoline.rsindex faa4321ff3..2bc6099322 100644--- a/crates/wasmtime/src/runtime/trampoline.rs+++ b/crates/wasmtime/src/runtime/trampoline.rs@@ -14,7 +14,7 @@ use self::memory::create_memory;use self::table::create_table;use self::tag::create_tag;use crate::prelude::*;-use crate::runtime::vm::SharedMemory;+use crate::runtime::vm::{ExportMemory, SharedMemory};use crate::store::{StoreOpaque, StoreResourceLimiter};use crate::{MemoryType, TableType, TagType};use wasmtime_environ::{MemoryIndex, TableIndex, TagIndex};@@ -24,7 +24,7 @@ pub async fn generate_memory_export(limiter: Option<&mut StoreResourceLimiter<'_>>,m: &MemoryType,preallocation: Option<&SharedMemory>,-) -> Result<crate::Memory> {+) -> Result<ExportMemory> {let id = store.id();let instance = create_memory(store, limiter, m, preallocation).await?;Ok(storediff --git a/crates/wasmtime/src/runtime/vm/export.rs b/crates/wasmtime/src/runtime/vm/export.rsindex 075dd4b1ad..4c11d9729e 100644--- a/crates/wasmtime/src/runtime/vm/export.rs+++ b/crates/wasmtime/src/runtime/vm/export.rs@@ -1,3 +1,5 @@+use crate::runtime::vm::{SharedMemory, VMMemoryImport};+/// The value of an export passed from one instance to another.pub enum Export {/// A function export value.@@ -6,8 +8,11 @@ pub enum Export {/// A table export value.Table(crate::Table),- /// A memory export value.- Memory { memory: crate::Memory, shared: bool },+ /// An unshared memory export value.+ Memory(crate::Memory),++ /// A shared memory export value.+ SharedMemory(SharedMemory, VMMemoryImport),/// A global export value.Global(crate::Global),@@ -15,3 +20,17 @@ pub enum Export {/// A tag export value.Tag(crate::Tag),}++pub enum ExportMemory {+ Unshared(crate::Memory),+ Shared(SharedMemory, VMMemoryImport),+}++impl ExportMemory {+ pub fn unshared(self) -> Option<crate::Memory> {+ match self {+ ExportMemory::Unshared(m) => Some(m),+ ExportMemory::Shared(..) => None,+ }+ }+}diff --git a/crates/wasmtime/src/runtime/vm/instance.rs b/crates/wasmtime/src/runtime/vm/instance.rsindex f59be04881..dedf27b79e 100644--- a/crates/wasmtime/src/runtime/vm/instance.rs+++ b/crates/wasmtime/src/runtime/vm/instance.rs@@ -5,7 +5,7 @@use crate::OpaqueRootScope;use crate::prelude::*;use crate::runtime::vm::const_expr::{ConstEvalContext, ConstExprEvaluator};-use crate::runtime::vm::export::Export;+use crate::runtime::vm::export::{Export, ExportMemory};use crate::runtime::vm::memory::{Memory, RuntimeMemoryCreator};use crate::runtime::vm::table::{Table, TableElementType};use crate::runtime::vm::vmcontext::{@@ -586,19 +586,48 @@ impl Instance {/// # Panics////// Panics if `index` is out-of-bounds for this instance.- pub fn get_exported_memory(&self, store: StoreId, index: MemoryIndex) -> crate::Memory {- let (id, def_index) = if let Some(def_index) = self.env_module().defined_memory_index(index)- {- (self.id, def_index)+ #[cfg_attr(+ not(feature = "threads"),+ expect(unused_variables, reason = "definitions cfg'd to dummy",)+ )]+ pub fn get_exported_memory(&self, store: StoreId, index: MemoryIndex) -> ExportMemory {+ let module = self.env_module();+ if module.memories[index].shared {+ let (memory, import) =+ if let Some(def_index) = self.env_module().defined_memory_index(index) {+ (+ self.get_defined_memory(def_index),+ self.get_defined_memory_vmimport(def_index),+ )+ } else {+ let import = self.imported_memory(index);+ // SAFETY: validity of this `Instance` guarantees validity of+ // the `vmctx` pointer being read here to find the transitive+ // `InstanceId` that the import is associated with.+ let instance = unsafe { self.sibling_vmctx(import.vmctx.as_non_null()) };+ (instance.get_defined_memory(import.index), *import)+ };++ let vm = memory.as_shared_memory().unwrap().clone();+ ExportMemory::Shared(vm, import)} else {- let import = self.imported_memory(index);- // SAFETY: validity of this `Instance` guarantees validity of the- // `vmctx` pointer being read here to find the transitive- // `InstanceId` that the import is associated with.- let id = unsafe { self.sibling_vmctx(import.vmctx.as_non_null()).id };- (id, import.index)- };- crate::Memory::from_raw(StoreInstanceId::new(store, id), def_index)+ let (id, def_index) =+ if let Some(def_index) = self.env_module().defined_memory_index(index) {+ (self.id, def_index)+ } else {+ let import = self.imported_memory(index);+ // SAFETY: validity of this `Instance` guarantees validity of the+ // `vmctx` pointer being read here to find the transitive+ // `InstanceId` that the import is associated with.+ let id = unsafe { self.sibling_vmctx(import.vmctx.as_non_null()).id };+ (id, import.index)+ };++ // SAFETY: `from_raw` requires that the memory is not shared, which+ // was tested above in this if/else.+ let store_id = StoreInstanceId::new(store, id);+ ExportMemory::Unshared(unsafe { crate::Memory::from_raw(store_id, def_index) })+ }}/// Lookup a global by index.@@ -998,6 +1027,14 @@ impl Instance {&self.memories[index].1}+ pub fn get_defined_memory_vmimport(&self, index: DefinedMemoryIndex) -> VMMemoryImport {+ crate::runtime::vm::VMMemoryImport {+ from: self.memory_ptr(index).into(),+ vmctx: self.vmctx().into(),+ index,+ }+ }+/// Do a `memory.copy`////// # Errors@@ -1462,7 +1499,7 @@ impl Instance {pub fn all_memories(&self,store: StoreId,- ) -> impl ExactSizeIterator<Item = (MemoryIndex, crate::Memory)> + '_ {+ ) -> impl ExactSizeIterator<Item = (MemoryIndex, ExportMemory)> + '_ {self.env_module().memories.iter()@@ -1473,7 +1510,7 @@ impl Instance {pub fn defined_memories<'a>(&'a self,store: StoreId,- ) -> impl ExactSizeIterator<Item = crate::Memory> + 'a {+ ) -> impl ExactSizeIterator<Item = ExportMemory> + 'a {let num_imported = self.env_module().num_imported_memories;self.all_memories(store).skip(num_imported)@@ -1503,9 +1540,9 @@ impl Instance {}EntityIndex::Global(i) => Export::Global(self.get_exported_global(store, i)),EntityIndex::Table(i) => Export::Table(self.get_exported_table(store, i)),- EntityIndex::Memory(i) => Export::Memory {- memory: self.get_exported_memory(store, i),- shared: self.env_module().memories[i].shared,+ EntityIndex::Memory(i) => match self.get_exported_memory(store, i) {+ ExportMemory::Unshared(m) => Export::Memory(m),+ ExportMemory::Shared(m, i) => Export::SharedMemory(m, i),},EntityIndex::Tag(i) => Export::Tag(self.get_exported_tag(store, i)),}diff --git a/crates/wast/Cargo.toml b/crates/wast/Cargo.tomlindex 8c87dd4afa..7d30b2069a 100644--- a/crates/wast/Cargo.toml+++ b/crates/wast/Cargo.toml@@ -15,7 +15,7 @@ workspace = true[dependencies]anyhow = { workspace = true }-wasmtime = { workspace = true, features = ['cranelift', 'wat', 'runtime', 'gc', 'async'] }+wasmtime = { workspace = true, features = ['cranelift', 'wat', 'runtime', 'gc', 'async', 'threads'] }wast = { workspace = true, features = ['dwarf'] }log = { workspace = true }tokio = { workspace = true, features = ['rt'] }diff --git a/crates/wast/src/spectest.rs b/crates/wast/src/spectest.rsindex f38356ac84..26c54c39af 100644--- a/crates/wast/src/spectest.rs+++ b/crates/wast/src/spectest.rs@@ -80,7 +80,7 @@ pub fn link_spectest<T>(if config.use_shared_memory {let ty = MemoryType::shared(1, 1);- let memory = Memory::new(&mut *store, ty)?;+ let memory = SharedMemory::new(store.engine(), ty)?;linker.define(&mut *store, "spectest", "shared_memory", memory)?;}diff --git a/tests/all/coredump.rs b/tests/all/coredump.rsindex 53351b7663..c2375e4549 100644--- a/tests/all/coredump.rs+++ b/tests/all/coredump.rs@@ -259,3 +259,36 @@ fn multiple_globals_memories_and_instances() -> Result<()> {Ok(())}++#[test]+#[cfg_attr(miri, ignore)]+fn core_dump_with_shared_memory() -> Result<()> {+ if super::threads::engine().is_none() {+ return Ok(());+ }+ let mut config = Config::new();+ config.coredump_on_trap(true);+ let engine = Engine::new(&config)?;+ let mut store = Store::new(&engine, ());+ let wat = r#"(module+ (memory 1 1 shared)+ (func (export "foo") unreachable)+ (data (i32.const 0) "a")+ )"#;+ let module = Module::new(&engine, wat)?;+ let instance = Instance::new(&mut store, &module, &[])?;+ let foo = instance.get_typed_func::<(), ()>(&mut store, "foo")?;+ let err = foo.call(&mut store, ()).unwrap_err();+ let coredump = err.downcast_ref::<WasmCoreDump>().unwrap();+ assert!(coredump.memories().is_empty());++ let bytes = coredump.serialize(&mut store, "howdy");+ for payload in wasmparser::Parser::new(0).parse_all(&bytes) {+ let payload = payload?;+ if let wasmparser::Payload::DataSection(s) = payload {+ assert_eq!(s.count(), 0);+ }+ }++ Ok(())+}diff --git a/tests/all/threads.rs b/tests/all/threads.rsindex fafc0c1aaa..ef320b9382 100644--- a/tests/all/threads.rs+++ b/tests/all/threads.rs@@ -295,3 +295,11 @@ fn test_memory_size_accessibility() -> Result<()> {Ok(())}++#[test]+fn create_shared_memory_through_memory() -> Result<()> {+ let engine = Engine::default();+ let mut store = Store::new(&engine, ());+ assert!(Memory::new(&mut store, MemoryType::shared(1, 1)).is_err());+ Ok(())+}