A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Use `TryBTreeMap` in the store's `ModuleRegistry` (#12846)
Nick Fitzgerald6e0ded7ceead
diff --git a/crates/wasmtime/src/runtime/code.rs b/crates/wasmtime/src/runtime/code.rsindex 9b98a19597..484fa9373e 100644--- a/crates/wasmtime/src/runtime/code.rs+++ b/crates/wasmtime/src/runtime/code.rs@@ -8,6 +8,7 @@ use crate::{code_memory::CodeMemory, type_registry::TypeCollection};use alloc::boxed::Box;use alloc::sync::Arc;use core::ops::{Add, Range, Sub};+use wasmtime_core::error::OutOfMemory;use wasmtime_environ::DefinedFuncIndex;use wasmtime_environ::ModuleTypes;use wasmtime_environ::StaticModuleIndex;@@ -126,16 +127,20 @@ pub struct EngineCode {}impl EngineCode {- pub fn new(mmap: Arc<CodeMemory>, signatures: TypeCollection, types: Types) -> EngineCode {+ pub fn new(+ mmap: Arc<CodeMemory>,+ signatures: TypeCollection,+ types: Types,+ ) -> Result<EngineCode, OutOfMemory> {// The corresponding unregister for this is below in `Drop for// EngineCode`.- crate::module::register_code(&mmap, mmap.raw_addr_range());+ crate::module::register_code(&mmap, mmap.raw_addr_range())?;- EngineCode {+ Ok(EngineCode {original_code: mmap,signatures,types,- }+ })}#[cfg(feature = "component-model")]@@ -334,7 +339,10 @@ impl StoreCode {// this clones the whole image.let mut private_copy = engine_code.original_code.deep_clone(engine)?;private_copy.publish()?;- crate::module::register_code(&engine_code.original_code, private_copy.raw_addr_range());+ crate::module::register_code(+ &engine_code.original_code,+ private_copy.raw_addr_range(),+ )?;StoreCodeStorage::Private(Box::new(private_copy))} else {StoreCodeStorage::Shared(engine_code.original_code.clone())diff --git a/crates/wasmtime/src/runtime/component/component.rs b/crates/wasmtime/src/runtime/component/component.rsindex 462455de20..0ec5065311 100644--- a/crates/wasmtime/src/runtime/component/component.rs+++ b/crates/wasmtime/src/runtime/component/component.rs@@ -437,7 +437,7 @@ impl Component {// Assemble the `EngineCode` artifact which is shared by all core wasm// modules as well as the final component.let types = Arc::new(types);- let code = Arc::new(EngineCode::new(code_memory, signatures, types.into()));+ let code = Arc::new(EngineCode::new(code_memory, signatures, types.into())?);// Convert all information about static core wasm modules into actual// `Module` instances by converting each `CompiledModuleInfo`, thediff --git a/crates/wasmtime/src/runtime/module.rs b/crates/wasmtime/src/runtime/module.rsindex ada8d58a06..beb5aa1f59 100644--- a/crates/wasmtime/src/runtime/module.rs+++ b/crates/wasmtime/src/runtime/module.rs@@ -523,7 +523,7 @@ impl Module {// Package up all our data into an `EngineCode` and delegate to the final// step of module compilation.- let code = try_new::<Arc<_>>(EngineCode::new(code_memory, signatures, types.into()))?;+ let code = try_new::<Arc<_>>(EngineCode::new(code_memory, signatures, types.into())?)?;let index = try_new::<Arc<_>>(index)?;Module::from_parts_raw(engine, code, info, index, true)}diff --git a/crates/wasmtime/src/runtime/module/registry.rs b/crates/wasmtime/src/runtime/module/registry.rsindex 3e0d329183..c16d12cb6c 100644--- a/crates/wasmtime/src/runtime/module/registry.rs+++ b/crates/wasmtime/src/runtime/module/registry.rs@@ -6,15 +6,13 @@ use crate::component::Component;use crate::runtime::vm::VMWasmCallFunction;use crate::sync::{OnceLock, RwLock};use crate::vm::CompiledModuleId;-use crate::{Engine, prelude::*};-use crate::{FrameInfo, Module, code_memory::CodeMemory};-use alloc::collections::btree_map::{BTreeMap, Entry};+use crate::{Engine, FrameInfo, Module, code_memory::CodeMemory, prelude::*};use alloc::sync::Arc;#[cfg(not(feature = "debug"))]use core::marker::PhantomData;use core::ops::Range;use core::ptr::NonNull;-use wasmtime_environ::VMSharedTypeIndex;+use wasmtime_environ::{VMSharedTypeIndex, collections::btree_map::Entry};/// Used for registering modules with a store.///@@ -63,18 +61,18 @@ pub struct ModuleRegistry {/// then take the last element of the range. That picks the/// highest start address <= the query, and we can check whether/// it contains the address.- loaded_code: BTreeMap<StoreCodePC, LoadedCode>,+ loaded_code: TryBTreeMap<StoreCodePC, LoadedCode>,/// Map from EngineCodePC start to StoreCodePC start. We use this/// to memoize the store-code creation process: each EngineCode is/// instantiated to a StoreCode only once per store.- store_code: BTreeMap<EngineCodePC, StoreCodePC>,+ store_code: TryBTreeMap<EngineCodePC, StoreCodePC>,/// Modules instantiated in this registry.////// Every module is placed in this map, but not every module will/// be in a LoadedCode entry, because the module may have no text.- modules: BTreeMap<RegisteredModuleId, Module>,+ modules: TryBTreeMap<RegisteredModuleId, Module>,}struct LoadedCode {@@ -82,7 +80,7 @@ struct LoadedCode {code: StoreCode,/// Map by starting text offset of Modules in this code region.- modules: BTreeMap<usize, RegisteredModuleId>,+ modules: TryBTreeMap<usize, RegisteredModuleId>,}/// An identifier of a module that has previously been inserted into a@@ -93,9 +91,12 @@ struct LoadedCode {#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]pub struct RegisteredModuleId(CompiledModuleId);-fn assert_no_overlap(loaded_code: &BTreeMap<StoreCodePC, LoadedCode>, range: Range<StoreCodePC>) {+fn assert_no_overlap(+ loaded_code: &TryBTreeMap<StoreCodePC, LoadedCode>,+ range: Range<StoreCodePC>,+) {if let Some((start, _)) = loaded_code.range(range.start..).next() {- assert!(*start >= range.end);+ assert!(start >= range.end);}if let Some((_, code)) = loaded_code.range(..range.end).next_back() {assert!(code.code.text_range().end <= range.start);@@ -121,12 +122,12 @@ impl<'a> RegisterBreakpointState<'a> {impl ModuleRegistry {/// Get a previously-registered module by id.pub fn module_by_id(&self, id: RegisteredModuleId) -> Option<&Module> {- self.modules.get(&id)+ self.modules.get(id)}/// Get a module by CompiledModuleId, if present.pub fn module_by_compiled_id(&self, id: CompiledModuleId) -> Option<&Module> {- self.modules.get(&RegisteredModuleId(id))+ self.modules.get(RegisteredModuleId(id))}/// Fetches a registered StoreCode and module and an offset within@@ -138,7 +139,7 @@ impl ModuleRegistry {.next_back()?;let offset = StoreCodePC::offset_of(code.code.text_range(), pc)?;let (_, module_id) = code.modules.range(..=offset).next_back()?;- let module = self.modules.get(&module_id)?;+ let module = self.modules.get(*module_id)?;Some((ModuleWithCode::from_raw(module, &code.code), offset))}@@ -151,9 +152,7 @@ impl ModuleRegistry {/// Fetches the base `StoreCodePC` for a given `EngineCode`.pub fn store_code_base(&self, engine_code: &EngineCode) -> Option<StoreCodePC> {- self.store_code- .get(&engine_code.text_range().start)- .cloned()+ self.store_code.get(engine_code.text_range().start).cloned()}/// Fetches the base `StoreCodePC` for a given `EngineCode` with@@ -164,11 +163,11 @@ impl ModuleRegistry {breakpoint_state: RegisterBreakpointState,) -> Result<StoreCodePC> {let key = module.engine_code().text_range().start;- if !self.store_code.contains_key(&key) {+ if !self.store_code.contains_key(key) {let engine = module.engine().clone();self.register_module(module, &engine, breakpoint_state)?;}- Ok(*self.store_code.get(&key).unwrap())+ Ok(*self.store_code.get(key).unwrap())}/// Fetches a mutable `StoreCode` for a given base `StoreCodePC`.@@ -228,11 +227,13 @@ impl ModuleRegistry {breakpoint_state: RegisterBreakpointState,) -> Result<Option<RegisteredModuleId>> {// Register the module, if any.- let id = module.map(|module| {+ let id = if let Some(module) = module {let id = RegisteredModuleId(compiled_id);- self.modules.entry(id).or_insert_with(|| module.clone());- id- });+ self.modules.entry(id).or_insert_with(|| module.clone())?;+ Some(id)+ } else {+ None+ };// Create a StoreCode if one does not already exist.let store_code_pc = match self.store_code.entry(code.text_range().start) {@@ -244,10 +245,10 @@ impl ModuleRegistry {store_code_pc,LoadedCode {code: store_code,- modules: BTreeMap::default(),+ modules: TryBTreeMap::default(),},- );- *v.insert(store_code_pc)+ )?;+ *v.insert(store_code_pc)?}Entry::Occupied(o) => *o.get(),};@@ -257,9 +258,9 @@ impl ModuleRegistry {if let Some((_, range)) = module.compiled_module().finished_function_ranges().next() {let loaded_code = self.loaded_code- .get_mut(&store_code_pc)+ .get_mut(store_code_pc).expect("loaded_code must have entry for StoreCodePC");- loaded_code.modules.insert(range.start, id);+ loaded_code.modules.insert(range.start, id)?;breakpoint_state.update(&mut loaded_code.code, module)?;}}@@ -287,7 +288,7 @@ impl ModuleRegistry {let (_, module_id) = code.modules.range(..=text_offset).next_back()?;let module = self.modules- .get(&module_id)+ .get(*module_id).expect("referenced module ID not found");let info = FrameInfo::new(module.clone(), text_offset)?;let module_with_code = ModuleWithCode::from_raw(module, &code.code);@@ -332,7 +333,7 @@ fn global_code() -> &'static RwLock<GlobalRegistry> {GLOBAL_CODE.get_or_init(Default::default)}-type GlobalRegistry = BTreeMap<usize, (usize, Arc<CodeMemory>)>;+type GlobalRegistry = TryBTreeMap<usize, (usize, Arc<CodeMemory>)>;/// Find which registered region of code contains the given program counter, and/// what offset that PC is within that module's code.@@ -351,14 +352,15 @@ pub fn lookup_code(pc: usize) -> Option<(Arc<CodeMemory>, usize)> {/// This is required to enable traps to work correctly since the signal handler/// will lookup in the `GLOBAL_CODE` list to determine which a particular pc/// is a trap or not.-pub fn register_code(image: &Arc<CodeMemory>, address: Range<usize>) {+pub fn register_code(image: &Arc<CodeMemory>, address: Range<usize>) -> Result<(), OutOfMemory> {if address.is_empty() {- return;+ return Ok(());}let start = address.start;let end = address.end - 1;- let prev = global_code().write().insert(end, (start, image.clone()));+ let prev = global_code().write().insert(end, (start, image.clone()))?;assert!(prev.is_none());+ Ok(())}/// Unregisters a code mmap from the global map.@@ -369,7 +371,7 @@ pub fn unregister_code(address: Range<usize>) {return;}let end = address.end - 1;- let code = global_code().write().remove(&end);+ let code = global_code().write().remove(end);assert!(code.is_some());}