Skip to content

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

Rename our OOM-handling `PrimaryMap` to `TryPrimaryMap` (#12726)

Nick Fitzgerald44224d5686bc
diff --git a/crates/environ/src/collections.rs b/crates/environ/src/collections.rs
index 5a3e9a8384..f61d970d97 100644
--- a/crates/environ/src/collections.rs
+++ b/crates/environ/src/collections.rs
@@ -13,7 +13,7 @@ pub use entity_set::TryEntitySet;
pub use hash_map::TryHashMap;
pub use hash_set::TryHashSet;
pub use index_map::TryIndexMap;
-pub use primary_map::PrimaryMap;
+pub use primary_map::TryPrimaryMap;
pub use secondary_map::SecondaryMap;
pub use wasmtime_core::{
alloc::{
diff --git a/crates/environ/src/collections/primary_map.rs b/crates/environ/src/collections/primary_map.rs
index 27b7ac666c..a44dd4b8b7 100644
--- a/crates/environ/src/collections/primary_map.rs
+++ b/crates/environ/src/collections/primary_map.rs
@@ -10,14 +10,14 @@ use serde::{Serialize, ser::SerializeSeq};
/// Like [`cranelift_entity::PrimaryMap`] but enforces fallible allocation for
/// all methods that allocate.
#[derive(Hash, PartialEq, Eq)]
-pub struct PrimaryMap<K, V>
+pub struct TryPrimaryMap<K, V>
where
K: EntityRef,
{
inner: cranelift_entity::PrimaryMap<K, V>,
}
-impl<K, V> Default for PrimaryMap<K, V>
+impl<K, V> Default for TryPrimaryMap<K, V>
where
K: EntityRef,
{
@@ -28,7 +28,7 @@ where
}
}
-impl<K, V> fmt::Debug for PrimaryMap<K, V>
+impl<K, V> fmt::Debug for TryPrimaryMap<K, V>
where
K: EntityRef + fmt::Debug,
V: fmt::Debug,
@@ -38,7 +38,7 @@ where
}
}
-impl<K, V> From<TryVec<V>> for PrimaryMap<K, V>
+impl<K, V> From<TryVec<V>> for TryPrimaryMap<K, V>
where
K: EntityRef,
{
@@ -49,7 +49,7 @@ where
}
}
-impl<K, V> serde::ser::Serialize for PrimaryMap<K, V>
+impl<K, V> serde::ser::Serialize for TryPrimaryMap<K, V>
where
K: EntityRef,
V: Serialize,
@@ -66,7 +66,7 @@ where
}
}
-impl<'de, K, V> serde::de::Deserialize<'de> for PrimaryMap<K, V>
+impl<'de, K, V> serde::de::Deserialize<'de> for TryPrimaryMap<K, V>
where
K: EntityRef,
V: serde::de::Deserialize<'de>,
@@ -80,7 +80,7 @@ where
}
}
-impl<K, V> PrimaryMap<K, V>
+impl<K, V> TryPrimaryMap<K, V>
where
K: EntityRef,
{
@@ -218,7 +218,7 @@ where
}
}
-impl<K, V> TryExtend<V> for PrimaryMap<K, V>
+impl<K, V> TryExtend<V> for TryPrimaryMap<K, V>
where
K: EntityRef,
{
@@ -237,7 +237,7 @@ where
}
}
-impl<K, V> Index<K> for PrimaryMap<K, V>
+impl<K, V> Index<K> for TryPrimaryMap<K, V>
where
K: EntityRef,
{
@@ -248,7 +248,7 @@ where
}
}
-impl<K, V> IndexMut<K> for PrimaryMap<K, V>
+impl<K, V> IndexMut<K> for TryPrimaryMap<K, V>
where
K: EntityRef,
{
@@ -257,7 +257,7 @@ where
}
}
-impl<K, V> IntoIterator for PrimaryMap<K, V>
+impl<K, V> IntoIterator for TryPrimaryMap<K, V>
where
K: EntityRef,
{
@@ -268,7 +268,7 @@ where
}
}
-impl<'a, K, V> IntoIterator for &'a PrimaryMap<K, V>
+impl<'a, K, V> IntoIterator for &'a TryPrimaryMap<K, V>
where
K: EntityRef,
{
@@ -279,7 +279,7 @@ where
}
}
-impl<'a, K, V> IntoIterator for &'a mut PrimaryMap<K, V>
+impl<'a, K, V> IntoIterator for &'a mut TryPrimaryMap<K, V>
where
K: EntityRef,
{
diff --git a/crates/environ/src/compile/module_environ.rs b/crates/environ/src/compile/module_environ.rs
index b328275206..2ecacbb21d 100644
--- a/crates/environ/src/compile/module_environ.rs
+++ b/crates/environ/src/compile/module_environ.rs
@@ -10,7 +10,7 @@ use crate::{
ModuleInternedTypeIndex, ModuleTypesBuilder, PanicOnOom as _, PrimaryMap, SizeOverflow,
StaticMemoryInitializer, StaticModuleIndex, TableIndex, TableInitialValue, Tag, TagIndex,
Tunables, TypeConvert, TypeIndex, WasmError, WasmHeapTopType, WasmHeapType, WasmResult,
- WasmValType, WasmparserTypeConverter, collections,
+ WasmValType, WasmparserTypeConverter,
};
use cranelift_entity::SecondaryMap;
use cranelift_entity::packed_option::ReservedValue;
@@ -1107,7 +1107,7 @@ impl ModuleTranslation<'_> {
// memory initialization image is built here from the page data and then
// it's converted to a single initializer.
let data = mem::replace(&mut self.data, Vec::new());
- let mut map = collections::PrimaryMap::with_capacity(info.len()).panic_on_oom();
+ let mut map = TryPrimaryMap::with_capacity(info.len()).panic_on_oom();
let mut module_data_size = 0u32;
for (memory, info) in info.iter() {
// Create the in-memory `image` which is the initialized contents of
diff --git a/crates/environ/src/module.rs b/crates/environ/src/module.rs
index f983aa304d..c2f52c2f60 100644
--- a/crates/environ/src/module.rs
+++ b/crates/environ/src/module.rs
@@ -82,7 +82,7 @@ pub enum MemoryInitialization {
///
/// The offset, range base, and range end are all guaranteed to be page
/// aligned to the page size passed in to `try_static_init`.
- map: collections::PrimaryMap<MemoryIndex, Option<StaticMemoryInitializer>>,
+ map: TryPrimaryMap<MemoryIndex, Option<StaticMemoryInitializer>>,
},
}
@@ -226,7 +226,7 @@ pub struct TableInitialization {
/// initialization. For example table initializers to a table that are all
/// in-bounds will get removed from `segment` and moved into
/// `initial_values` here.
- pub initial_values: collections::PrimaryMap<DefinedTableIndex, TableInitialValue>,
+ pub initial_values: TryPrimaryMap<DefinedTableIndex, TableInitialValue>,
/// Element segments present in the initial wasm module which are executed
/// at instantiation time.
@@ -325,7 +325,7 @@ pub struct Module {
pub passive_data_map: BTreeMap<DataIndex, Range<u32>>,
/// Types declared in the wasm module.
- pub types: collections::PrimaryMap<TypeIndex, EngineOrModuleTypeIndex>,
+ pub types: TryPrimaryMap<TypeIndex, EngineOrModuleTypeIndex>,
/// Number of imported or aliased functions in the module.
pub num_imported_funcs: usize,
@@ -353,22 +353,22 @@ pub struct Module {
pub num_escaped_funcs: usize,
/// Types of functions, imported and local.
- pub functions: collections::PrimaryMap<FuncIndex, FunctionType>,
+ pub functions: TryPrimaryMap<FuncIndex, FunctionType>,
/// WebAssembly tables.
- pub tables: collections::PrimaryMap<TableIndex, Table>,
+ pub tables: TryPrimaryMap<TableIndex, Table>,
/// WebAssembly linear memory plans.
- pub memories: collections::PrimaryMap<MemoryIndex, Memory>,
+ pub memories: TryPrimaryMap<MemoryIndex, Memory>,
/// WebAssembly global variables.
- pub globals: collections::PrimaryMap<GlobalIndex, Global>,
+ pub globals: TryPrimaryMap<GlobalIndex, Global>,
/// WebAssembly global initializers for locally-defined globals.
- pub global_initializers: collections::PrimaryMap<DefinedGlobalIndex, ConstExpr>,
+ pub global_initializers: TryPrimaryMap<DefinedGlobalIndex, ConstExpr>,
/// WebAssembly exception and control tags.
- pub tags: collections::PrimaryMap<TagIndex, Tag>,
+ pub tags: TryPrimaryMap<TagIndex, Tag>,
}
/// Initialization routines for creating an instance, encompassing imports,
diff --git a/crates/environ/src/module_artifacts.rs b/crates/environ/src/module_artifacts.rs
index 1632defc3d..6d6960b1ea 100644
--- a/crates/environ/src/module_artifacts.rs
+++ b/crates/environ/src/module_artifacts.rs
@@ -4,7 +4,7 @@
use crate::prelude::*;
use crate::{
EntityRef, FilePos, FuncIndex, FuncKey, FuncKeyIndex, FuncKeyKind, FuncKeyNamespace, Module,
- PanicOnOom as _, collections::PrimaryMap,
+ PanicOnOom as _,
};
use core::ops::Range;
use core::{fmt, u32};
@@ -78,13 +78,13 @@ impl CompiledFunctionsTableBuilder {
pub fn new() -> Self {
Self {
inner: CompiledFunctionsTable {
- namespaces: PrimaryMap::new(),
- func_loc_starts: PrimaryMap::new(),
- sparse_starts: PrimaryMap::new(),
- src_loc_starts: PrimaryMap::new(),
- sparse_indices: PrimaryMap::new(),
- func_locs: PrimaryMap::new(),
- src_locs: PrimaryMap::new(),
+ namespaces: TryPrimaryMap::new(),
+ func_loc_starts: TryPrimaryMap::new(),
+ sparse_starts: TryPrimaryMap::new(),
+ src_loc_starts: TryPrimaryMap::new(),
+ sparse_indices: TryPrimaryMap::new(),
+ func_locs: TryPrimaryMap::new(),
+ src_locs: TryPrimaryMap::new(),
},
}
}
@@ -289,7 +289,7 @@ pub struct CompiledFunctionsTable {
/// their associated `NamespaceIndex`. That `NamespaceIndex` can then be
/// used to find the range of other entity indices that are specific to that
/// namespace.
- namespaces: PrimaryMap<NamespaceIndex, FuncKeyNamespace>,
+ namespaces: TryPrimaryMap<NamespaceIndex, FuncKeyNamespace>,
/// `self.func_loc_starts[i]..self.func_loc_starts[i+1]` describes the range
/// within `self.func_locs` whose entries are associated with the namespace
@@ -297,7 +297,7 @@ pub struct CompiledFunctionsTable {
///
/// When `self.func_loc_starts[i+1]` is out of bounds, then the range is to
/// the end of `self.func_locs`.
- func_loc_starts: PrimaryMap<NamespaceIndex, FuncLocIndex>,
+ func_loc_starts: TryPrimaryMap<NamespaceIndex, FuncLocIndex>,
/// `self.sparse_starts[i]..self.sparse_starts[i+1]` describes the range
/// within `self.sparse_indices` whose entries are associated with the
@@ -307,7 +307,7 @@ pub struct CompiledFunctionsTable {
/// the end of `self.sparse_indices`.
///
/// Entries are only valid for sparse, non-dense namespaces.
- sparse_starts: PrimaryMap<NamespaceIndex, SparseIndex>,
+ sparse_starts: TryPrimaryMap<NamespaceIndex, SparseIndex>,
/// `self.src_loc_starts[i]..self.src_loc_starts[i+1]` describes the range
/// within `self.src_loc_indices` whose entries are associated with the
@@ -318,7 +318,7 @@ pub struct CompiledFunctionsTable {
///
/// Entries are only valid for namespaces whose functions have source
/// locations.
- src_loc_starts: PrimaryMap<NamespaceIndex, SrcLocIndex>,
+ src_loc_starts: TryPrimaryMap<NamespaceIndex, SrcLocIndex>,
/// `self.sparse_indices[i]` contains the index part of
/// `FuncKey::from_parts(ns, index)` where `ns` is determined by
@@ -327,7 +327,7 @@ pub struct CompiledFunctionsTable {
/// the namespace's start index.)
///
/// This is sorted to allow for binary searches.
- sparse_indices: PrimaryMap<SparseIndex, FuncKeyIndex>,
+ sparse_indices: TryPrimaryMap<SparseIndex, FuncKeyIndex>,
/// `self.func_locs[i]` contains the location within the text section of
/// `FuncKey::from_parts(self.namespaces[ns], i - start)`'s function, where
@@ -338,14 +338,14 @@ pub struct CompiledFunctionsTable {
///
/// The absence of a function location (for gaps in dense namespaces) is
/// represented with `FunctionLoc::none()`.
- func_locs: PrimaryMap<FuncLocIndex, FunctionLoc>,
+ func_locs: TryPrimaryMap<FuncLocIndex, FunctionLoc>,
/// `self.src_locs[i]` contains the initial source location of
/// `FuncKey::from_parts(self.namespaces[ns], i - start)`'s function, where
/// `ns` and `start` are determined by `self.src_loc_starts`.
///
/// The absence of a source location is represented by `FilePos::none()`.
- src_locs: PrimaryMap<SrcLocIndex, FilePos>,
+ src_locs: TryPrimaryMap<SrcLocIndex, FilePos>,
}
impl CompiledFunctionsTable {
diff --git a/crates/environ/src/module_types.rs b/crates/environ/src/module_types.rs
index 0da4fb721d..e99a5cd45e 100644
--- a/crates/environ/src/module_types.rs
+++ b/crates/environ/src/module_types.rs
@@ -1,7 +1,6 @@
use crate::{
ModuleInternedRecGroupIndex, ModuleInternedTypeIndex, PanicOnOom as _, TypeTrace, WasmSubType,
- collections::{PrimaryMap, SecondaryMap},
- packed_option::PackedOption,
+ collections::SecondaryMap, packed_option::PackedOption, prelude::*,
};
use core::ops::{Index, Range};
use serde_derive::{Deserialize, Serialize};
@@ -12,8 +11,8 @@ use serde_derive::{Deserialize, Serialize};
/// implementations for this type.
#[derive(Default, Serialize, Deserialize)]
pub struct ModuleTypes {
- rec_groups: PrimaryMap<ModuleInternedRecGroupIndex, Range<ModuleInternedTypeIndex>>,
- wasm_types: PrimaryMap<ModuleInternedTypeIndex, WasmSubType>,
+ rec_groups: TryPrimaryMap<ModuleInternedRecGroupIndex, Range<ModuleInternedTypeIndex>>,
+ wasm_types: TryPrimaryMap<ModuleInternedTypeIndex, WasmSubType>,
trampoline_types: SecondaryMap<ModuleInternedTypeIndex, PackedOption<ModuleInternedTypeIndex>>,
}
diff --git a/crates/environ/src/prelude.rs b/crates/environ/src/prelude.rs
index 8c5a3bfb23..51348e7e29 100644
--- a/crates/environ/src/prelude.rs
+++ b/crates/environ/src/prelude.rs
@@ -21,7 +21,7 @@
pub use crate::collections::{
TryClone, TryCollect, TryEntitySet, TryExtend, TryFromIterator, TryHashMap, TryHashSet,
- TryIndexMap, TryNew, TryString, TryToOwned, TryVec, try_new, try_vec,
+ TryIndexMap, TryNew, TryPrimaryMap, TryString, TryToOwned, TryVec, try_new, try_vec,
};
pub use crate::error::{Context, Error, Result, bail, ensure, format_err};
pub use alloc::borrow::ToOwned;
diff --git a/crates/wasmtime/src/runtime/store.rs b/crates/wasmtime/src/runtime/store.rs
index 97e2fd5ba2..af2bf238b1 100644
--- a/crates/wasmtime/src/runtime/store.rs
+++ b/crates/wasmtime/src/runtime/store.rs
@@ -473,7 +473,7 @@ pub struct StoreOpaque {
#[cfg(feature = "stack-switching")]
continuations: Vec<Box<VMContRef>>,
- instances: wasmtime_environ::collections::PrimaryMap<InstanceId, StoreInstance>,
+ instances: TryPrimaryMap<InstanceId, StoreInstance>,
signal_handler: Option<SignalHandler>,
modules: ModuleRegistry,
@@ -744,7 +744,7 @@ impl<T> Store<T> {
vm_store_context: Default::default(),
#[cfg(feature = "stack-switching")]
continuations: Vec::new(),
- instances: wasmtime_environ::collections::PrimaryMap::new(),
+ instances: TryPrimaryMap::new(),
signal_handler: None,
gc_store: None,
gc_roots: RootSet::default(),
diff --git a/crates/wasmtime/src/runtime/type_registry.rs b/crates/wasmtime/src/runtime/type_registry.rs
index 50773fe86a..b2a3737595 100644
--- a/crates/wasmtime/src/runtime/type_registry.rs
+++ b/crates/wasmtime/src/runtime/type_registry.rs
@@ -25,7 +25,7 @@ use wasmtime_core::slab::{Id as SlabId, Slab};
use wasmtime_environ::{
EngineOrModuleTypeIndex, EntityRef, GcLayout, ModuleInternedTypeIndex, ModuleTypes, TypeTrace,
Undo, VMSharedTypeIndex, WasmRecGroup, WasmSubType,
- collections::{PrimaryMap, SecondaryMap, TryCow},
+ collections::{SecondaryMap, TryCow},
iter_entity_range,
packed_option::{PackedOption, ReservedValue},
};
@@ -89,7 +89,7 @@ use wasmtime_environ::{
pub struct TypeCollection {
engine: Engine,
rec_groups: TryVec<RecGroupEntry>,
- types: PrimaryMap<ModuleInternedTypeIndex, VMSharedTypeIndex>,
+ types: TryPrimaryMap<ModuleInternedTypeIndex, VMSharedTypeIndex>,
trampolines: SecondaryMap<VMSharedTypeIndex, PackedOption<ModuleInternedTypeIndex>>,
}
@@ -184,7 +184,7 @@ impl TypeCollection {
///
/// This is used for looking up module shared type indexes during module
/// instantiation.
- pub fn as_module_map(&self) -> &PrimaryMap<ModuleInternedTypeIndex, VMSharedTypeIndex> {
+ pub fn as_module_map(&self) -> &TryPrimaryMap<ModuleInternedTypeIndex, VMSharedTypeIndex> {
&self.types
}
@@ -682,7 +682,7 @@ impl TypeRegistryInner {
) -> Result<
(
TryVec<RecGroupEntry>,
- PrimaryMap<ModuleInternedTypeIndex, VMSharedTypeIndex>,
+ TryPrimaryMap<ModuleInternedTypeIndex, VMSharedTypeIndex>,
),
OutOfMemory,
> {
@@ -693,7 +693,7 @@ impl TypeRegistryInner {
// The map from a module type index to an engine type index for these
// module types.
- let mut map = PrimaryMap::<ModuleInternedTypeIndex, VMSharedTypeIndex>::with_capacity(
+ let mut map = TryPrimaryMap::<ModuleInternedTypeIndex, VMSharedTypeIndex>::with_capacity(
types.wasm_types().len(),
)?;
@@ -751,7 +751,7 @@ impl TypeRegistryInner {
fn register_rec_group(
&mut self,
gc_runtime: Option<&dyn GcRuntime>,
- map: &PrimaryMap<ModuleInternedTypeIndex, VMSharedTypeIndex>,
+ map: &TryPrimaryMap<ModuleInternedTypeIndex, VMSharedTypeIndex>,
range: Range<ModuleInternedTypeIndex>,
types: impl ExactSizeIterator<Item = Result<WasmSubType, OutOfMemory>>,
) -> Result<RecGroupEntry, OutOfMemory> {
@@ -809,7 +809,7 @@ impl TypeRegistryInner {
fn register_new_rec_group(
&mut self,
gc_runtime: Option<&(dyn GcRuntime + 'static)>,
- map: &PrimaryMap<ModuleInternedTypeIndex, VMSharedTypeIndex>,
+ map: &TryPrimaryMap<ModuleInternedTypeIndex, VMSharedTypeIndex>,
range: Range<ModuleInternedTypeIndex>,
hash_consing_key: WasmRecGroup,
mut non_canon_types: TryVec<(ModuleInternedTypeIndex, WasmSubType)>,
@@ -1258,7 +1258,7 @@ impl TypeRegistryInner {
/// this registry.
fn canonicalize_entry_types_for_runtime_usage<'a>(
&self,
- map: &PrimaryMap<ModuleInternedTypeIndex, VMSharedTypeIndex>,
+ map: &TryPrimaryMap<ModuleInternedTypeIndex, VMSharedTypeIndex>,
entry: &RecGroupEntry,
sub_tys: impl ExactSizeIterator<Item = &'a mut WasmSubType>,
range: Range<ModuleInternedTypeIndex>,
@@ -1272,7 +1272,7 @@ impl TypeRegistryInner {
/// Canonicalize one type for runtime usage within this registry.
fn canonicalize_type_for_runtime_usage(
&self,
- map: &PrimaryMap<ModuleInternedTypeIndex, VMSharedTypeIndex>,
+ map: &TryPrimaryMap<ModuleInternedTypeIndex, VMSharedTypeIndex>,
entry: &RecGroupEntry,
engine_index: VMSharedTypeIndex,
ty: &mut WasmSubType,
@@ -1346,7 +1346,7 @@ impl TypeRegistryInner {
// This type doesn't have any module-level type references, since it is
// already canonicalized for runtime usage in this registry, so an empty
// map suffices.
- let map = PrimaryMap::default();
+ let map = TryPrimaryMap::default();
// This must have `range.len() == 1`, even though we know this type
// doesn't have any intra-group type references, to satisfy