Skip to content

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

Handle OOM in `Component::deserialize` (#14922)

Alex Crichton2f1a81b676c4
diff --git a/crates/environ/src/collections.rs b/crates/environ/src/collections.rs
index be7e4cc369..d40147c025 100644
--- a/crates/environ/src/collections.rs
+++ b/crates/environ/src/collections.rs
@@ -5,6 +5,7 @@ mod entity_set;
mod hash_map;
mod hash_set;
mod index_map;
+mod index_set;
mod primary_map;
mod secondary_map;
@@ -13,6 +14,7 @@ pub use entity_set::TryEntitySet;
pub use hash_map::TryHashMap;
pub use hash_set::TryHashSet;
pub use index_map::TryIndexMap;
+pub use index_set::TryIndexSet;
pub use primary_map::TryPrimaryMap;
pub use secondary_map::TrySecondaryMap;
pub use wasmtime_core::{
diff --git a/crates/environ/src/collections/index_map.rs b/crates/environ/src/collections/index_map.rs
index 55629f871d..4f3597e776 100644
--- a/crates/environ/src/collections/index_map.rs
+++ b/crates/environ/src/collections/index_map.rs
@@ -1,4 +1,7 @@
-use crate::{collections::TryClone, error::OutOfMemory};
+use crate::{
+ collections::{TryClone, TryFromIterator},
+ error::OutOfMemory,
+};
use core::{
borrow::Borrow,
cmp::Ordering,
@@ -124,6 +127,40 @@ where
}
}
+impl<K, V> TryFromIterator<(K, V), OutOfMemory> for TryIndexMap<K, V>
+where
+ K: Hash + Eq,
+{
+ fn try_from_iter<I>(iter: I) -> Result<Self, OutOfMemory>
+ where
+ I: Iterator<Item = (K, V)>,
+ {
+ let mut map = Self::with_capacity(iter.size_hint().0)?;
+ for (k, v) in iter {
+ map.insert(k, v)?;
+ }
+ Ok(map)
+ }
+}
+
+impl<K, V, E> TryFromIterator<Result<(K, V), E>, E> for TryIndexMap<K, V>
+where
+ K: Hash + Eq,
+ E: From<OutOfMemory>,
+{
+ fn try_from_iter<I>(iter: I) -> Result<Self, E>
+ where
+ I: Iterator<Item = Result<(K, V), E>>,
+ {
+ let mut map = Self::with_capacity(iter.size_hint().0)?;
+ for item in iter {
+ let (k, v) = item?;
+ map.insert(k, v)?;
+ }
+ Ok(map)
+ }
+}
+
impl<K, V> TryIndexMap<K, V> {
/// Same as [`indexmap::IndexMap::new`].
pub fn new() -> Self {
diff --git a/crates/environ/src/collections/index_set.rs b/crates/environ/src/collections/index_set.rs
new file mode 100644
index 0000000000..6eae2244a2
--- /dev/null
+++ b/crates/environ/src/collections/index_set.rs
@@ -0,0 +1,289 @@
+use crate::{
+ collections::{TryClone, TryFromIterator},
+ error::OutOfMemory,
+};
+use core::{
+ borrow::Borrow,
+ fmt,
+ hash::{BuildHasher, Hash},
+ marker::PhantomData,
+ mem,
+ ops::Index,
+};
+use indexmap::set as inner;
+
+#[cfg(feature = "std")]
+use std::hash::RandomState as DefaultHashBuilder;
+
+#[cfg(not(feature = "std"))]
+use hashbrown::DefaultHashBuilder;
+
+/// A wrapper around [`indexmap::IndexSet`] that only provides fallible
+/// allocation.
+pub struct TryIndexSet<T, S = DefaultHashBuilder> {
+ inner: indexmap::IndexSet<T, S>,
+}
+
+impl<T, S> Default for TryIndexSet<T, S>
+where
+ S: Default,
+{
+ fn default() -> Self {
+ Self {
+ inner: indexmap::IndexSet::default(),
+ }
+ }
+}
+
+impl<T, S> PartialEq for TryIndexSet<T, S>
+where
+ T: Eq + Hash,
+ S: BuildHasher,
+{
+ fn eq(&self, other: &Self) -> bool {
+ self.inner.eq(&other.inner)
+ }
+}
+
+impl<T, S> Eq for TryIndexSet<T, S>
+where
+ T: Eq + Hash,
+ S: BuildHasher,
+{
+}
+
+impl<T, S> TryClone for TryIndexSet<T, S>
+where
+ T: Eq + Hash + TryClone,
+ S: BuildHasher + TryClone,
+{
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ let mut set = Self::with_hasher(self.inner.hasher().try_clone()?);
+ set.reserve(self.len())?;
+ for value in self.iter() {
+ set.insert(value.try_clone()?)?;
+ }
+ Ok(set)
+ }
+}
+
+impl<T, S> fmt::Debug for TryIndexSet<T, S>
+where
+ T: fmt::Debug,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Debug::fmt(&self.inner, f)
+ }
+}
+
+impl<T, S> From<TryIndexSet<T, S>> for indexmap::IndexSet<T, S> {
+ fn from(set: TryIndexSet<T, S>) -> Self {
+ set.inner
+ }
+}
+
+impl<T, S> From<indexmap::IndexSet<T, S>> for TryIndexSet<T, S> {
+ fn from(inner: indexmap::IndexSet<T, S>) -> Self {
+ Self { inner }
+ }
+}
+
+impl<T, S> serde::ser::Serialize for TryIndexSet<T, S>
+where
+ T: serde::ser::Serialize,
+{
+ fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
+ where
+ Ser: serde::Serializer,
+ {
+ use serde::ser::SerializeSeq as _;
+ let mut seq = serializer.serialize_seq(Some(self.len()))?;
+ for value in self {
+ seq.serialize_element(value)?;
+ }
+ seq.end()
+ }
+}
+
+impl<'de, T> serde::de::Deserialize<'de> for TryIndexSet<T>
+where
+ T: serde::de::Deserialize<'de> + Hash + Eq,
+{
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: serde::Deserializer<'de>,
+ {
+ struct Visitor<T>(PhantomData<fn() -> TryIndexSet<T>>);
+
+ impl<'de, T> serde::de::Visitor<'de> for Visitor<T>
+ where
+ T: serde::de::Deserialize<'de> + Hash + Eq,
+ {
+ type Value = TryIndexSet<T>;
+
+ fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.write_str("an `IndexSet`")
+ }
+
+ fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
+ where
+ A: serde::de::SeqAccess<'de>,
+ {
+ use serde::de::Error as _;
+
+ let mut result = TryIndexSet::<T>::new();
+
+ if let Some(len) = seq.size_hint() {
+ result.reserve(len).map_err(|oom| A::Error::custom(oom))?;
+ }
+
+ while let Some(value) = seq.next_element::<T>()? {
+ result.insert(value).map_err(|oom| A::Error::custom(oom))?;
+ }
+
+ Ok(result)
+ }
+ }
+
+ deserializer.deserialize_seq(Visitor(PhantomData))
+ }
+}
+
+impl<T> TryFromIterator<T, OutOfMemory> for TryIndexSet<T>
+where
+ T: Hash + Eq,
+{
+ fn try_from_iter<I>(iter: I) -> Result<Self, OutOfMemory>
+ where
+ I: Iterator<Item = T>,
+ {
+ let mut set = Self::new();
+ set.reserve(iter.size_hint().0)?;
+ for value in iter {
+ set.insert(value)?;
+ }
+ Ok(set)
+ }
+}
+
+impl<T, E> TryFromIterator<Result<T, E>, E> for TryIndexSet<T>
+where
+ T: Hash + Eq,
+ E: From<OutOfMemory>,
+{
+ fn try_from_iter<I>(iter: I) -> Result<Self, E>
+ where
+ I: Iterator<Item = Result<T, E>>,
+ {
+ let mut set = Self::new();
+ set.reserve(iter.size_hint().0)?;
+ for value in iter {
+ set.insert(value?)?;
+ }
+ Ok(set)
+ }
+}
+
+impl<T> TryIndexSet<T> {
+ /// Same as [`indexmap::IndexSet::new`].
+ pub fn new() -> Self {
+ Self {
+ inner: indexmap::IndexSet::with_hasher(Default::default()),
+ }
+ }
+}
+
+impl<T, S> TryIndexSet<T, S> {
+ /// Same as [`indexmap::IndexSet::with_hasher`].
+ pub const fn with_hasher(hash_builder: S) -> Self {
+ TryIndexSet {
+ inner: indexmap::IndexSet::with_hasher(hash_builder),
+ }
+ }
+
+ /// Same as [`indexmap::IndexSet::len`].
+ pub fn len(&self) -> usize {
+ self.inner.len()
+ }
+
+ /// Same as [`indexmap::IndexSet::is_empty`].
+ pub fn is_empty(&self) -> bool {
+ self.inner.is_empty()
+ }
+
+ /// Same as [`indexmap::IndexSet::iter`].
+ pub fn iter(&self) -> inner::Iter<'_, T> {
+ self.inner.iter()
+ }
+
+ /// Same as [`indexmap::IndexSet::get_index`].
+ pub fn get_index(&self, index: usize) -> Option<&T> {
+ self.inner.get_index(index)
+ }
+
+ /// Same as [`indexmap::IndexSet::reserve`] but returns an error on
+ /// allocation failure.
+ pub fn reserve(&mut self, additional: usize) -> Result<(), OutOfMemory> {
+ self.inner.try_reserve(additional).map_err(|_| {
+ let new_len = self.len().saturating_add(additional);
+ OutOfMemory::new(new_len.saturating_mul(mem::size_of::<T>()))
+ })
+ }
+}
+
+impl<T, S> TryIndexSet<T, S>
+where
+ T: Hash + Eq,
+ S: BuildHasher,
+{
+ /// Same as [`indexmap::IndexSet::insert`] but returns an error on
+ /// allocation failure.
+ pub fn insert(&mut self, value: T) -> Result<bool, OutOfMemory> {
+ self.reserve(1)?;
+ Ok(self.inner.insert(value))
+ }
+
+ /// Same as [`indexmap::IndexSet::contains`].
+ pub fn contains<Q>(&self, value: &Q) -> bool
+ where
+ Q: ?Sized + Hash + Eq,
+ T: Borrow<Q>,
+ {
+ self.inner.contains(value)
+ }
+
+ /// Same as [`indexmap::IndexSet::get_index_of`].
+ pub fn get_index_of<Q>(&self, value: &Q) -> Option<usize>
+ where
+ Q: ?Sized + Hash + Eq,
+ T: Borrow<Q>,
+ {
+ self.inner.get_index_of(value)
+ }
+}
+
+impl<T, S> Index<usize> for TryIndexSet<T, S> {
+ type Output = T;
+
+ fn index(&self, index: usize) -> &T {
+ &self.inner[index]
+ }
+}
+
+impl<'a, T, S> IntoIterator for &'a TryIndexSet<T, S> {
+ type Item = &'a T;
+ type IntoIter = inner::Iter<'a, T>;
+
+ fn into_iter(self) -> Self::IntoIter {
+ self.iter()
+ }
+}
+
+impl<T, S> IntoIterator for TryIndexSet<T, S> {
+ type Item = T;
+ type IntoIter = inner::IntoIter<T>;
+
+ fn into_iter(self) -> Self::IntoIter {
+ self.inner.into_iter()
+ }
+}
diff --git a/crates/environ/src/collections/primary_map.rs b/crates/environ/src/collections/primary_map.rs
index 6ed542091f..3467fe5667 100644
--- a/crates/environ/src/collections/primary_map.rs
+++ b/crates/environ/src/collections/primary_map.rs
@@ -237,6 +237,31 @@ where
}
}
+impl<K, V> TryFromIterator<V, OutOfMemory> for TryPrimaryMap<K, V>
+where
+ K: EntityRef,
+{
+ fn try_from_iter<I>(iter: I) -> Result<Self, OutOfMemory>
+ where
+ I: Iterator<Item = V>,
+ {
+ Ok(iter.try_collect::<TryVec<_>, _>()?.into())
+ }
+}
+
+impl<K, V, E> TryFromIterator<Result<V, E>, E> for TryPrimaryMap<K, V>
+where
+ K: EntityRef,
+ E: From<OutOfMemory>,
+{
+ fn try_from_iter<I>(iter: I) -> Result<Self, E>
+ where
+ I: Iterator<Item = Result<V, E>>,
+ {
+ Ok(iter.try_collect::<TryVec<_>, E>()?.into())
+ }
+}
+
impl<K, V> Index<K> for TryPrimaryMap<K, V>
where
K: EntityRef,
diff --git a/crates/environ/src/component/artifacts.rs b/crates/environ/src/component/artifacts.rs
index 725c2bd4c9..0c7e96e94f 100644
--- a/crates/environ/src/component/artifacts.rs
+++ b/crates/environ/src/component/artifacts.rs
@@ -2,7 +2,8 @@
//! which are serialized with `bincode` into output ELF files.
use crate::{
- CompiledFunctionsTable, CompiledModuleInfo, PrimaryMap, StaticModuleIndex, WasmChecksum,
+ CompiledFunctionsTable, CompiledModuleInfo, StaticModuleIndex, WasmChecksum,
+ collections::TryPrimaryMap,
component::{Component, ComponentTypes, TypeComponentIndex},
};
use serde_derive::{Deserialize, Serialize};
@@ -19,7 +20,7 @@ pub struct ComponentArtifacts {
/// Type information for this component and all contained modules.
pub types: ComponentTypes,
/// Serialized metadata about all included core wasm modules.
- pub static_modules: PrimaryMap<StaticModuleIndex, CompiledModuleInfo>,
+ pub static_modules: TryPrimaryMap<StaticModuleIndex, CompiledModuleInfo>,
/// A checksum of the source Wasm binary from which the component was compiled.
pub checksum: WasmChecksum,
}
diff --git a/crates/environ/src/component/dfg.rs b/crates/environ/src/component/dfg.rs
index 83b4a39326..60ece44cc3 100644
--- a/crates/environ/src/component/dfg.rs
+++ b/crates/environ/src/component/dfg.rs
@@ -30,7 +30,9 @@
use crate::component::*;
use crate::error::Result;
use crate::prelude::*;
-use crate::{EntityIndex, EntityRef, ModuleInternedTypeIndex, PrimaryMap, Trap, WasmValType};
+use crate::{
+ EntityIndex, EntityRef, ModuleInternedTypeIndex, PanicOnOom as _, PrimaryMap, Trap, WasmValType,
+};
use cranelift_entity::EntitySet;
use cranelift_entity::packed_option::PackedOption;
use indexmap::IndexMap;
@@ -301,6 +303,14 @@ pub struct CoreExport<T> {
pub item: ExportItem<T>,
}
+/// Same as `info::ExportItem`
+#[derive(Debug, Clone, Hash, Eq, PartialEq)]
+#[expect(missing_docs, reason = "self-describing variants")]
+pub enum ExportItem<T> {
+ Index(T),
+ Name(String),
+}
+
impl<T> CoreExport<T> {
#[expect(missing_docs, reason = "self-describing function")]
pub fn map_index<U>(self, f: impl FnOnce(T) -> U) -> CoreExport<U> {
@@ -642,7 +652,12 @@ impl ComponentDfg {
for (name, (export, data)) in self.exports.iter() {
let export =
linearize.export(export, &mut export_items, wasmtime_types, wasmparser_types)?;
- exports.insert(name, &mut NameMapNoIntern, false, (export, data.clone()))?;
+ exports.insert(
+ name,
+ &mut NameMapNoIntern,
+ false,
+ (export, data.try_clone()?),
+ )?;
}
// With all those pieces done the results of the dataflow-based
@@ -653,12 +668,12 @@ impl ComponentDfg {
trampolines: linearize.trampoline_defs,
component: Component {
exports,
- export_items,
- initializers: linearize.initializers,
+ export_items: export_items.into(),
+ initializers: linearize.initializers.into(),
unsafe_intrinsics: linearize.unsafe_intrinsics,
- trampolines: linearize.trampolines,
+ trampolines: linearize.trampolines.into(),
num_lowerings: linearize.num_lowerings,
- options: linearize.options,
+ options: linearize.options.into(),
num_runtime_memories: linearize.runtime_memories.len() as u32,
num_runtime_tables: linearize.runtime_tables.len() as u32,
@@ -666,19 +681,30 @@ impl ComponentDfg {
num_runtime_reallocs: linearize.runtime_reallocs.len() as u32,
num_runtime_callbacks: linearize.runtime_callbacks.len() as u32,
num_runtime_instances: linearize.runtime_instances.len() as u32,
- imports: self.imports,
- import_types: self.import_types,
+ imports: self
+ .imports
+ .into_iter()
+ .map(|(_, (import, path))| {
+ let path = path.into_iter().map(TryString::from).try_collect()?;
+ Ok((import, path))
+ })
+ .try_collect::<_, OutOfMemory>()?,
+ import_types: self
+ .import_types
+ .into_iter()
+ .map(|(_, (name, ty))| (TryString::from(name), ty))
+ .try_collect()?,
num_runtime_component_instances: self.num_runtime_component_instances,
num_future_tables: self.num_future_tables,
num_stream_tables: self.num_stream_tables,
num_error_context_tables: self.num_error_context_tables,
num_resources: (self.resources.len() + self.imported_resources.len()) as u32,
- imported_resources: self.imported_resources,
+ imported_resources: self.imported_resources.into(),
defined_resource_instances: self
.resources
.iter()
.map(|(_, r)| r.instance)
- .collect(),
+ .try_collect()?,
},
})
}
@@ -736,7 +762,10 @@ impl LinearizeDfg<'_> {
let instantiation = match args {
Instance::Static(index, args) => InstantiateModule::Static(
*index,
- args.iter().map(|def| self.core_def(def)).collect(),
+ args.iter()
+ .map(|def| self.core_def(def))
+ .try_collect()
+ .panic_on_oom(),
),
Instance::Import(index, args) => InstantiateModule::Import(
*index,
@@ -744,11 +773,14 @@ impl LinearizeDfg<'_> {
.map(|(module, values)| {
let values = values
.iter()
- .map(|(name, def)| (name.clone(), self.core_def(def)))
- .collect();
- (module.clone(), values)
+ .map(|(name, def)| {
+ Ok((TryString::try_from(name.as_str())?, self.core_def(def)))
+ })
+ .try_collect::<_, OutOfMemory>()?;
+ Ok((TryString::try_from(module.as_str())?, values))
})
- .collect(),
+ .try_collect::<_, OutOfMemory>()
+ .panic_on_oom(),
),
};
let index = RuntimeInstanceIndex::new(self.runtime_instances.len());
@@ -805,7 +837,12 @@ impl LinearizeDfg<'_> {
for (name, (export, data)) in exports {
let export =
self.export(export, items, wasmtime_types, wasmparser_types)?;
- map.insert(name, &mut NameMapNoIntern, false, (export, data.clone()))?;
+ map.insert(
+ name,
+ &mut NameMapNoIntern,
+ false,
+ (export, data.try_clone()?),
+ )?;
}
map
},
@@ -1204,7 +1241,10 @@ impl LinearizeDfg<'_> {
log::trace!("referencing export of {instance:?}");
info::CoreExport {
instance: self.runtime_instances[&RuntimeInstance::Normal(instance)],
- item: export.item.clone(),
+ item: match &export.item {
+ ExportItem::Index(i) => info::ExportItem::Index(i.clone()),
+ ExportItem::Name(name) => info::ExportItem::Name(name.clone().into()),
+ },
}
}
@@ -1219,7 +1259,7 @@ impl LinearizeDfg<'_> {
// This adapter is always an export of the instance.
info::CoreExport {
instance,
- item: ExportItem::Index(entity_index),
+ item: info::ExportItem::Index(entity_index),
}
}
@@ -1230,7 +1270,11 @@ impl LinearizeDfg<'_> {
|me, _| {
log::debug!("instantiating {adapter_module:?}");
let (module_index, args) = &me.dfg.adapter_modules[adapter_module];
- let args = args.iter().map(|arg| me.core_def(arg)).collect();
+ let args = args
+ .iter()
+ .map(|arg| me.core_def(arg))
+ .try_collect()
+ .panic_on_oom();
let instantiate = InstantiateModule::Static(*module_index, args);
GlobalInitializer::InstantiateModule(instantiate, None)
},
diff --git a/crates/environ/src/component/info.rs b/crates/environ/src/component/info.rs
index 0e5de10953..1a602e4f17 100644
--- a/crates/environ/src/component/info.rs
+++ b/crates/environ/src/component/info.rs
@@ -76,7 +76,7 @@ pub struct Component {
///
/// Note that each name is given an `ImportIndex` here for the next map to
/// refer back to.
- pub import_types: PrimaryMap<ImportIndex, (String, ComponentExtern)>,
+ pub import_types: TryPrimaryMap<ImportIndex, (TryString, ComponentExtern)>,
/// A list of "flattened" imports that are used by this instance.
///
@@ -104,7 +104,7 @@ pub struct Component {
// strings could otherwise be globally intern'd via some other mechanism to
// avoid `Linker`-specific intern-ing plus intern-ing here. Unsure what the
// best route is or whether such an optimization is even necessary here.
- pub imports: PrimaryMap<RuntimeImportIndex, (ImportIndex, Vec<String>)>,
+ pub imports: TryPrimaryMap<RuntimeImportIndex, (ImportIndex, TryVec<TryString>)>,
/// This component's own root exports from the component itself.
pub exports: NameMap<TryString, (ExportIndex, ComponentExternData)>,
@@ -113,7 +113,7 @@ pub struct Component {
///
/// This is indexed by `ExportIndex` for fast lookup and `Export::Instance`
/// will refer back into this list.
- pub export_items: PrimaryMap<ExportIndex, Export>,
+ pub export_items: TryPrimaryMap<ExportIndex, Export>,
/// Initializers that must be processed when instantiating this component.
///
@@ -124,7 +124,7 @@ pub struct Component {
/// have instantiations, for example, in addition to entries which
/// initialize `VMComponentContext` fields with previously instantiated
/// instances.
- pub initializers: Vec<GlobalInitializer>,
+ pub initializers: TryVec<GlobalInitializer>,
/// The number of runtime instances (maximum `RuntimeInstanceIndex`) created
/// when instantiating this component.
@@ -161,7 +161,7 @@ pub struct Component {
pub num_runtime_post_returns: u32,
/// WebAssembly type signature of all trampolines.
- pub trampolines: PrimaryMap<TrampolineIndex, ModuleInternedTypeIndex>,
+ pub trampolines: TryPrimaryMap<TrampolineIndex, ModuleInternedTypeIndex>,
/// A map from a `UnsafeIntrinsic::index()` to that intrinsic's
/// module-interned type.
@@ -191,18 +191,19 @@ pub struct Component {
/// imports array.
///
/// This map is only as large as the number of imported resources.
- pub imported_resources: PrimaryMap<ResourceIndex, RuntimeImportIndex>,
+ pub imported_resources: TryPrimaryMap<ResourceIndex, RuntimeImportIndex>,
/// Metadata about which component instances defined each resource within
/// this component.
///
/// This is used to determine which set of instance flags are inspected when
/// testing reentrance.
- pub defined_resource_instances: PrimaryMap<DefinedResourceIndex, RuntimeComponentInstanceIndex>,
+ pub defined_resource_instances:
+ TryPrimaryMap<DefinedResourceIndex, RuntimeComponentInstanceIndex>,
/// All canonical options used by this component. Stored as a table here
/// from index-to-options so the options can be consulted at runtime.
- pub options: PrimaryMap<OptionsIndex, CanonicalOptions>,
+ pub options: TryPrimaryMap<OptionsIndex, CanonicalOptions>,
}
impl Component {
@@ -359,7 +360,7 @@ pub enum InstantiateModule {
/// order of imports required is statically known and can be pre-calculated
/// to avoid string lookups related to names at runtime, represented by the
/// flat list of arguments here.
- Static(StaticModuleIndex, Box<[CoreDef]>),
+ Static(StaticModuleIndex, TryVec<CoreDef>),
/// An imported module is being instantiated.
///
@@ -368,7 +369,7 @@ pub enum InstantiateModule {
/// runtime.
Import(
RuntimeImportIndex,
- IndexMap<String, IndexMap<String, CoreDef>>,
+ TryIndexMap<TryString, TryIndexMap<TryString, CoreDef>>,
),
}
@@ -379,7 +380,7 @@ pub enum InstantiateModule {
/// during compile time of the component itself. References to core wasm items
/// are "compiled" to either referring to a previous instance or to some sort of
/// lowered host import.
-#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
+#[derive(Debug, Serialize, Deserialize, Hash, Eq, PartialEq)]
pub enum CoreDef {
/// This item refers to an export of a previously instantiated core wasm
/// instance.
@@ -394,6 +395,17 @@ pub enum CoreDef {
UnsafeIntrinsic(UnsafeIntrinsic),
}
+impl TryClone for CoreDef {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(match self {
+ CoreDef::Export(e) => CoreDef::Export(e.try_clone()?),
+ CoreDef::InstanceFlags(i) => CoreDef::InstanceFlags(*i),
+ CoreDef::Trampoline(i) => CoreDef::Trampoline(*i),
+ CoreDef::UnsafeIntrinsic(i) => CoreDef::UnsafeIntrinsic(*i),
+ })
+ }
+}
+
impl<T> From<CoreExport<T>> for CoreDef
where
EntityIndex: From<T>,
@@ -408,7 +420,7 @@ where
/// Note that the `T` here is the index type for exports which can be
/// identified by index. The `T` is monomorphized with types like
/// [`EntityIndex`] or [`FuncIndex`].
-#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
+#[derive(Debug, Serialize, Deserialize, Hash, Eq, PartialEq)]
pub struct CoreExport<T> {
/// The instance that this item is located within.
///
@@ -421,6 +433,15 @@ pub struct CoreExport<T> {
pub item: ExportItem<T>,
}
+impl<T: TryClone> TryClone for CoreExport<T> {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(CoreExport {
+ instance: self.instance,
+ item: self.item.try_clone()?,
+ })
+ }
+}
+
impl<T> CoreExport<T> {
/// Maps the index type `T` to another type `U` if this export item indeed
/// refers to an index `T`.
@@ -436,7 +457,7 @@ impl<T> CoreExport<T> {
}
/// An index at which to find an item within a runtime instance.
-#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
+#[derive(Debug, Serialize, Deserialize, Hash, Eq, PartialEq)]
pub enum ExportItem<T> {
/// An exact index that the target can be found at.
///
@@ -451,7 +472,16 @@ pub enum ExportItem<T> {
///
/// This is used for instantiations of imported modules, for example, since
/// the precise shape of the module is not known.
- Name(String),
+ Name(TryString),
+}
+
+impl<T: TryClone> TryClone for ExportItem<T> {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(match self {
+ ExportItem::Index(i) => ExportItem::Index(i.try_clone()?),
+ ExportItem::Name(name) => ExportItem::Name(name.try_clone()?),
+ })
+ }
}
/// Possible exports from a component.
diff --git a/crates/environ/src/component/same_vmctx.rs b/crates/environ/src/component/same_vmctx.rs
index 9ff7e27935..8b419e4ad7 100644
--- a/crates/environ/src/component/same_vmctx.rs
+++ b/crates/environ/src/component/same_vmctx.rs
@@ -25,9 +25,9 @@
//! it, so the DFG is a DAG and one pass reaches the fixpoint.
use crate::compile::ModuleTranslation;
-use crate::component::ExportItem;
use crate::component::dfg::{
- AdapterId, AdapterModuleId, ComponentDfg, CoreDef, Export, Instance, InstanceId, SideEffect,
+ AdapterId, AdapterModuleId, ComponentDfg, CoreDef, Export, ExportItem, Instance, InstanceId,
+ SideEffect,
};
use crate::prelude::*;
use crate::union_find::UnionFind;
diff --git a/crates/environ/src/component/translate/inline.rs b/crates/environ/src/component/translate/inline.rs
index 1b611797b3..0d24524c9c 100644
--- a/crates/environ/src/component/translate/inline.rs
+++ b/crates/environ/src/component/translate/inline.rs
@@ -45,6 +45,7 @@
//! side-effectful initializers are emitted to the `GlobalInitializer` list in the
//! final `Component`.
+use crate::component::dfg::ExportItem;
use crate::component::translate::*;
use crate::{EntityType, Memory};
use core::str::FromStr;
@@ -1570,7 +1571,7 @@ impl<'a> Inliner<'a> {
ExportItem::Name(_) => unreachable!(),
},
InstanceModule::Import(ty) => match &memory.item {
- ExportItem::Name(name) => match types[*ty].exports[name] {
+ ExportItem::Name(name) => match types[*ty].exports[name.as_str()] {
EntityType::Memory(m) => m,
_ => unreachable!(),
},
@@ -1704,9 +1705,15 @@ impl<'a> Inliner<'a> {
// module-exporting instances.
ComponentInstanceDef::Import(path, ty) => {
for (name, ty) in types[ty].exports.iter() {
- let path = path.push(name);
+ let path = path.push(name.as_str());
let def = ComponentItemDef::from_import(path, ty.ty)?;
- self.record_export(name, def, ty.data.clone(), types, &mut exports)?;
+ self.record_export(
+ name,
+ def,
+ ty.data.try_clone()?,
+ types,
+ &mut exports,
+ )?;
}
dfg::Export::Instance { ty, exports }
}
@@ -1725,7 +1732,7 @@ impl<'a> Inliner<'a> {
if let TypeDef::ComponentInstance(instance_ty) = export.ty {
def = def.with_instance_type(instance_ty);
}
- let data = export.data.clone();
+ let data = export.data.try_clone()?;
self.record_export(name, def, data, types, &mut exports)?;
}
dfg::Export::Instance { ty, exports }
@@ -2010,8 +2017,8 @@ enum InstanceModule {
impl ComponentExternData {
fn new(data: wasmparser::ComponentExternName<'_>) -> Self {
ComponentExternData {
- implements: data.implements.map(|s| s.to_string()),
- external_id: data.external_id.map(|s| s.to_string()),
+ implements: data.implements.map(|s| s.to_string().into()),
+ external_id: data.external_id.map(|s| s.to_string().into()),
}
}
}
diff --git a/crates/environ/src/component/types.rs b/crates/environ/src/component/types.rs
index 18d108092b..6c8eefe054 100644
--- a/crates/environ/src/component/types.rs
+++ b/crates/environ/src/component/types.rs
@@ -1,5 +1,5 @@
use crate::component::{MAX_FLAT_PARAMS, MAX_FLAT_RESULTS};
-use crate::{EntityType, ModuleInternedTypeIndex, ModuleTypes, PrimaryMap};
+use crate::{EntityType, ModuleInternedTypeIndex, ModuleTypes, PanicOnOom as _};
use crate::{TypeTrace, prelude::*};
use core::hash::{Hash, Hasher};
use core::ops::Index;
@@ -286,28 +286,29 @@ pub enum ComponentItem {
/// will have a pointer to this value as well.
#[derive(Default, Serialize, Deserialize)]
pub struct ComponentTypes {
- pub(super) modules: PrimaryMap<TypeModuleIndex, TypeModule>,
- pub(super) components: PrimaryMap<TypeComponentIndex, TypeComponent>,
- pub(super) component_instances: PrimaryMap<TypeComponentInstanceIndex, TypeComponentInstance>,
- pub(super) functions: PrimaryMap<TypeFuncIndex, TypeFunc>,
- pub(super) lists: PrimaryMap<TypeListIndex, TypeList>,
- pub(super) maps: PrimaryMap<TypeMapIndex, TypeMap>,
- pub(super) records: PrimaryMap<TypeRecordIndex, TypeRecord>,
- pub(super) variants: PrimaryMap<TypeVariantIndex, TypeVariant>,
- pub(super) tuples: PrimaryMap<TypeTupleIndex, TypeTuple>,
- pub(super) enums: PrimaryMap<TypeEnumIndex, TypeEnum>,
- pub(super) flags: PrimaryMap<TypeFlagsIndex, TypeFlags>,
- pub(super) options: PrimaryMap<TypeOptionIndex, TypeOption>,
- pub(super) results: PrimaryMap<TypeResultIndex, TypeResult>,
- pub(super) resource_tables: PrimaryMap<TypeResourceTableIndex, TypeResourceTable>,
+ pub(super) modules: TryPrimaryMap<TypeModuleIndex, TypeModule>,
+ pub(super) components: TryPrimaryMap<TypeComponentIndex, TypeComponent>,
+ pub(super) component_instances:
+ TryPrimaryMap<TypeComponentInstanceIndex, TypeComponentInstance>,
+ pub(super) functions: TryPrimaryMap<TypeFuncIndex, TypeFunc>,
+ pub(super) lists: TryPrimaryMap<TypeListIndex, TypeList>,
+ pub(super) maps: TryPrimaryMap<TypeMapIndex, TypeMap>,
+ pub(super) records: TryPrimaryMap<TypeRecordIndex, TypeRecord>,
+ pub(super) variants: TryPrimaryMap<TypeVariantIndex, TypeVariant>,
+ pub(super) tuples: TryPrimaryMap<TypeTupleIndex, TypeTuple>,
+ pub(super) enums: TryPrimaryMap<TypeEnumIndex, TypeEnum>,
+ pub(super) flags: TryPrimaryMap<TypeFlagsIndex, TypeFlags>,
+ pub(super) options: TryPrimaryMap<TypeOptionIndex, TypeOption>,
+ pub(super) results: TryPrimaryMap<TypeResultIndex, TypeResult>,
+ pub(super) resource_tables: TryPrimaryMap<TypeResourceTableIndex, TypeResourceTable>,
pub(super) module_types: Option<ModuleTypes>,
- pub(super) futures: PrimaryMap<TypeFutureIndex, TypeFuture>,
- pub(super) future_tables: PrimaryMap<TypeFutureTableIndex, TypeFutureTable>,
- pub(super) streams: PrimaryMap<TypeStreamIndex, TypeStream>,
- pub(super) stream_tables: PrimaryMap<TypeStreamTableIndex, TypeStreamTable>,
+ pub(super) futures: TryPrimaryMap<TypeFutureIndex, TypeFuture>,
+ pub(super) future_tables: TryPrimaryMap<TypeFutureTableIndex, TypeFutureTable>,
+ pub(super) streams: TryPrimaryMap<TypeStreamIndex, TypeStream>,
+ pub(super) stream_tables: TryPrimaryMap<TypeStreamTableIndex, TypeStreamTable>,
pub(super) error_context_tables:
- PrimaryMap<TypeComponentLocalErrorContextTableIndex, TypeErrorContextTable>,
- pub(super) fixed_length_lists: PrimaryMap<TypeFixedLengthListIndex, TypeFixedLengthList>,
+ TryPrimaryMap<TypeComponentLocalErrorContextTableIndex, TypeErrorContextTable>,
+ pub(super) fixed_length_lists: TryPrimaryMap<TypeFixedLengthListIndex, TypeFixedLengthList>,
}
impl TypeTrace for ComponentTypes {
@@ -389,7 +390,7 @@ impl ComponentTypes {
/// Adds a new `table` to the list of resource tables for this component.
pub fn push_resource_table(&mut self, table: TypeResourceTable) -> TypeResourceTableIndex {
- self.resource_tables.push(table)
+ self.resource_tables.push(table).panic_on_oom()
}
}
@@ -453,7 +454,7 @@ where
///
/// This records the type of the item that is being imported or exported along
/// with any metadata associated with the item.
-#[derive(Clone, Debug, Serialize, Deserialize)]
+#[derive(Debug, Serialize, Deserialize)]
pub struct ComponentExtern {
/// Metadata associated with this item's name, such as
/// `(implements "...")`.
@@ -462,8 +463,17 @@ pub struct ComponentExtern {
pub ty: TypeDef,
}
+impl TryClone for ComponentExtern {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(ComponentExtern {
+ data: self.data.try_clone()?,
+ ty: self.ty,
+ })
+ }
+}
+
/// Metadata associated with the name of a component import or export.
-#[derive(Clone, Debug, Serialize, Deserialize)]
+#[derive(Debug, Serialize, Deserialize)]
pub struct ComponentExternData {
/// The `(implements "...")` annotation, if present: the name of the
/// interface that this item implements.
@@ -471,10 +481,19 @@ pub struct ComponentExternData {
/// This is metadata only: imports are resolved by their own name (e.g.
/// `my-label`), not by this interface name. Embedders can use it to find
/// out which interface a labelled item implements.
- pub implements: Option<String>,
+ pub implements: Option<TryString>,
/// The `(external-id "...")` annotation, if present: a free-form
/// host-defined identifier which is ignored by type checking.
- pub external_id: Option<String>,
+ pub external_id: Option<TryString>,
+}
+
+impl TryClone for ComponentExternData {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(ComponentExternData {
+ implements: self.implements.try_clone()?,
+ external_id: self.external_id.try_clone()?,
+ })
+ }
}
/// Types of imports and exports in the component model.
@@ -536,13 +555,13 @@ pub struct TypeModule {
/// two-level namespace of core WebAssembly, but unlike core wasm all import
/// names are required to be unique to describe a module in the component
/// model.
- pub imports: IndexMap<(String, String), EntityType>,
+ pub imports: TryIndexMap<(TryString, TryString), EntityType>,
/// The values that this module exports.
///
/// Note that the value of this map is the core wasm `EntityType` to
/// represent that core wasm items are being exported.
- pub exports: IndexMap<String, EntityType>,
+ pub exports: TryIndexMap<TryString, EntityType>,
}
impl TypeTrace for TypeModule {
@@ -577,9 +596,9 @@ impl TypeTrace for TypeModule {
#[derive(Serialize, Deserialize, Default)]
pub struct TypeComponent {
/// The named values that this component imports.
- pub imports: IndexMap<String, ComponentExtern>,
+ pub imports: TryIndexMap<TryString, ComponentExtern>,
/// The named values that this component exports.
- pub exports: IndexMap<String, ComponentExtern>,
+ pub exports: TryIndexMap<TryString, ComponentExtern>,
}
/// The type of a component instance in the component model, or an instantiated
@@ -589,22 +608,33 @@ pub struct TypeComponent {
#[derive(Serialize, Deserialize, Default)]
pub struct TypeComponentInstance {
/// The list of exports that this component has along with their types.
- pub exports: IndexMap<String, ComponentExtern>,
+ pub exports: TryIndexMap<TryString, ComponentExtern>,
}
/// A component function type in the component model.
-#[derive(Serialize, Deserialize, Clone, Hash, Eq, PartialEq, Debug)]
+#[derive(Serialize, Deserialize, Hash, Eq, PartialEq, Debug)]
pub struct TypeFunc {
/// Whether or not this is an async function.
pub async_: bool,
/// Names of parameters.
- pub param_names: Vec<String>,
+ pub param_names: TryVec<TryString>,
/// Parameters to the function represented as a tuple.
pub params: TypeTupleIndex,
/// Results of the function represented as a tuple.
pub results: TypeTupleIndex,
}
+impl TryClone for TypeFunc {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(TypeFunc {
+ async_: self.async_,
+ param_names: self.param_names.try_clone()?,
+ params: self.params,
+ results: self.results,
+ })
+ }
+}
+
/// All possible interface types that values can have.
///
/// This list represents an exhaustive listing of interface types and the
@@ -644,6 +674,12 @@ pub enum InterfaceType {
FixedLengthList(TypeFixedLengthListIndex),
}
+impl TryClone for InterfaceType {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(*self)
+ }
+}
+
/// Bye information about a type in the canonical ABI, with metadata for both
/// memory32 and memory64-based types.
#[derive(Serialize, Deserialize, Clone, Hash, Eq, PartialEq, Debug)]
@@ -1034,32 +1070,50 @@ mod serde_discrim_size {
/// Shape of a "record" type in interface types.
///
/// This is equivalent to a `struct` in Rust.
-#[derive(Serialize, Deserialize, Clone, Hash, Eq, PartialEq, Debug)]
+#[derive(Serialize, Deserialize, Hash, Eq, PartialEq, Debug)]
pub struct TypeRecord {
/// The fields that are contained within this struct type.
- pub fields: Box<[RecordField]>,
+ pub fields: TryVec<RecordField>,
/// Byte information about this type in the canonical ABI.
pub abi: CanonicalAbiInfo,
}
+impl TryClone for TypeRecord {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(TypeRecord {
+ fields: self.fields.try_clone()?,
+ abi: self.abi.clone(),
+ })
+ }
+}
+
/// One field within a record.
-#[derive(Serialize, Deserialize, Clone, Hash, Eq, PartialEq, Debug)]
+#[derive(Serialize, Deserialize, Hash, Eq, PartialEq, Debug)]
pub struct RecordField {
/// The name of the field, unique amongst all fields in a record.
- pub name: String,
+ pub name: TryString,
/// The type that this field contains.
pub ty: InterfaceType,
}
+impl TryClone for RecordField {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(RecordField {
+ name: self.name.try_clone()?,
+ ty: self.ty,
+ })
+ }
+}
+
/// Shape of a "variant" type in interface types.
///
/// Variants are close to Rust `enum` declarations where a value is one of many
/// cases and each case has a unique name and an optional payload associated
/// with it.
-#[derive(Serialize, Deserialize, Clone, Debug)]
+#[derive(Serialize, Deserialize, Debug)]
pub struct TypeVariant {
/// The list of cases that this variant can take.
- pub cases: IndexMap<String, Option<InterfaceType>>,
+ pub cases: TryIndexMap<TryString, Option<InterfaceType>>,
/// Byte information about this type in the canonical ABI.
pub abi: CanonicalAbiInfo,
/// Byte information about this variant type.
@@ -1080,6 +1134,16 @@ impl PartialEq for TypeVariant {
impl Eq for TypeVariant {}
+impl TryClone for TypeVariant {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(TypeVariant {
+ cases: self.cases.try_clone()?,
+ abi: self.abi.clone(),
+ info: self.info.clone(),
+ })
+ }
+}
+
impl Hash for TypeVariant {
fn hash<H: Hasher>(&self, h: &mut H) {
let TypeVariant { cases, abi, info } = self;
@@ -1096,22 +1160,31 @@ impl Hash for TypeVariant {
///
/// This is largely the same as a tuple in Rust, basically a record with
/// unnamed fields.
-#[derive(Serialize, Deserialize, Clone, Hash, Eq, PartialEq, Debug)]
+#[derive(Serialize, Deserialize, Hash, Eq, PartialEq, Debug)]
pub struct TypeTuple {
/// The types that are contained within this tuple.
- pub types: Box<[InterfaceType]>,
+ pub types: TryVec<InterfaceType>,
/// Byte information about this type in the canonical ABI.
pub abi: CanonicalAbiInfo,
}
+impl TryClone for TypeTuple {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(TypeTuple {
+ types: self.types.try_clone()?,
+ abi: self.abi.clone(),
+ })
+ }
+}
+
/// Shape of a "flags" type in interface types.
///
/// This can be thought of as a record-of-bools, although the representation is
/// more efficient as bitflags.
-#[derive(Serialize, Deserialize, Clone, Debug)]
+#[derive(Serialize, Deserialize, Debug)]
pub struct TypeFlags {
/// The names of all flags, all of which are unique.
- pub names: IndexSet<String>,
+ pub names: TryIndexSet<TryString>,
/// Byte information about this type in the canonical ABI.
pub abi: CanonicalAbiInfo,
}
@@ -1127,6 +1200,15 @@ impl PartialEq for TypeFlags {
impl Eq for TypeFlags {}
+impl TryClone for TypeFlags {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(TypeFlags {
+ names: self.names.try_clone()?,
+ abi: self.abi.clone(),
+ })
+ }
+}
+
impl Hash for TypeFlags {
fn hash<H: Hasher>(&self, h: &mut H) {
let TypeFlags { names, abi } = self;
@@ -1143,10 +1225,10 @@ impl Hash for TypeFlags {
///
/// In interface types enums are simply a bag of names, and can be seen as a
/// variant where all payloads are `Unit`.
-#[derive(Serialize, Deserialize, Clone, Debug)]
+#[derive(Serialize, Deserialize, Debug)]
pub struct TypeEnum {
/// The names of this enum, all of which are unique.
- pub names: IndexSet<String>,
+ pub names: TryIndexSet<TryString>,
/// Byte information about this type in the canonical ABI.
pub abi: CanonicalAbiInfo,
/// Byte information about this variant type.
@@ -1167,6 +1249,16 @@ impl PartialEq for TypeEnum {
impl Eq for TypeEnum {}
+impl TryClone for TypeEnum {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(TypeEnum {
+ names: self.names.try_clone()?,
+ abi: self.abi.clone(),
+ info: self.info.clone(),
+ })
+ }
+}
+
impl Hash for TypeEnum {
fn hash<H: Hasher>(&self, h: &mut H) {
let TypeEnum { names, abi, info } = self;
@@ -1331,6 +1423,31 @@ pub struct TypeFixedLengthList {
pub abi: CanonicalAbiInfo,
}
+macro_rules! impl_try_clone_via_clone {
+ ($($ty:ty),* $(,)?) => {$(
+ impl TryClone for $ty {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(self.clone())
+ }
+ }
+ )*};
+}
+
+// These types don't contain any heap allocations, so cloning them can't fail.
+impl_try_clone_via_clone! {
+ TypeOption,
+ TypeResult,
+ TypeFuture,
+ TypeFutureTable,
+ TypeStream,
+ TypeStreamTable,
+ TypeErrorContextTable,
+ TypeResourceTable,
+ TypeList,
+ TypeMap,
+ TypeFixedLengthList,
+}
+
/// Maximum number of flat types, for either params or results.
pub const MAX_FLAT_TYPES: usize = if MAX_FLAT_PARAMS > MAX_FLAT_RESULTS {
MAX_FLAT_PARAMS
@@ -1401,8 +1518,9 @@ mod tests {
TypeVariant {
cases: cases
.iter()
- .map(|(name, ty)| (name.to_string(), Some(*ty)))
- .collect(),
+ .map(|(name, ty)| Ok((TryString::try_from(*name)?, Some(*ty))))
+ .try_collect::<_, OutOfMemory>()
+ .panic_on_oom(),
abi: CanonicalAbiInfo::default(),
info: VariantInfo {
size: DiscriminantSize::Size1,
@@ -1414,14 +1532,22 @@ mod tests {
fn flags(names: &[&str]) -> TypeFlags {
TypeFlags {
- names: names.iter().map(|n| n.to_string()).collect(),
+ names: names
+ .iter()
+ .map(|n| TryString::try_from(*n))
+ .try_collect::<_, OutOfMemory>()
+ .panic_on_oom(),
abi: CanonicalAbiInfo::default(),
}
}
fn enum_(names: &[&str]) -> TypeEnum {
TypeEnum {
- names: names.iter().map(|n| n.to_string()).collect(),
+ names: names
+ .iter()
+ .map(|n| TryString::try_from(*n))
+ .try_collect::<_, OutOfMemory>()
+ .panic_on_oom(),
abi: CanonicalAbiInfo::default(),
info: VariantInfo {
size: DiscriminantSize::Size1,
@@ -1436,7 +1562,7 @@ mod tests {
let a = variant(&[("n", InterfaceType::U32), ("s", InterfaceType::String)]);
let b = variant(&[("s", InterfaceType::String), ("n", InterfaceType::U32)]);
assert_ne!(a, b);
- assert_eq!(a, a.clone());
+ assert_eq!(a, a.clone_panic_on_oom());
}
#[test]
@@ -1445,7 +1571,7 @@ mod tests {
let b = flags(&["c", "b", "a"]);
assert_ne!(a, b);
- assert_eq!(a, a.clone());
+ assert_eq!(a, a.clone_panic_on_oom());
}
#[test]
@@ -1454,6 +1580,6 @@ mod tests {
let b = enum_(&["blue", "green", "red"]);
assert_ne!(a, b);
- assert_eq!(a, a.clone());
+ assert_eq!(a, a.clone_panic_on_oom());
}
}
diff --git a/crates/environ/src/component/types_builder.rs b/crates/environ/src/component/types_builder.rs
index 97f5ff6110..9e453f9ac2 100644
--- a/crates/environ/src/component/types_builder.rs
+++ b/crates/environ/src/component/types_builder.rs
@@ -1,3 +1,4 @@
+use crate::PanicOnOom as _;
use crate::component::*;
use crate::error::{Result, bail};
use crate::prelude::*;
@@ -76,7 +77,11 @@ macro_rules! intern_and_fill_flat_types {
if let Some(idx) = $me.$name.get(&$val) {
*idx
} else {
- let idx = $me.component_types.$name.push($val.clone());
+ let idx = $me
+ .component_types
+ .$name
+ .push($val.clone_panic_on_oom())
+ .panic_on_oom();
let mut info = TypeInformation::new();
info.$name($me, &$val);
let idx2 = $me.type_info.$name.push(info);
@@ -118,7 +123,7 @@ impl ComponentTypesBuilder {
fn export_type_def(
&mut self,
- export_items: &PrimaryMap<ExportIndex, Export>,
+ export_items: &TryPrimaryMap<ExportIndex, Export>,
idx: ExportIndex,
) -> TypeDef {
match &export_items[idx] {
@@ -137,18 +142,28 @@ impl ComponentTypesBuilder {
pub fn finish(mut self, component: &Component) -> (ComponentTypes, TypeComponentIndex) {
let mut component_ty = TypeComponent::default();
for (_, (name, ty)) in component.import_types.iter() {
- component_ty.imports.insert(name.clone(), ty.clone());
+ component_ty
+ .imports
+ .insert(name.clone_panic_on_oom(), ty.clone_panic_on_oom())
+ .panic_on_oom();
}
for (name, (ty, data)) in component.exports.raw_iter() {
- component_ty.exports.insert(
- name.clone_panic_on_oom().into(),
- ComponentExtern {
- data: data.clone(),
- ty: self.export_type_def(&component.export_items, *ty),
- },
- );
+ component_ty
+ .exports
+ .insert(
+ name.clone_panic_on_oom(),
+ ComponentExtern {
+ data: data.clone_panic_on_oom(),
+ ty: self.export_type_def(&component.export_items, *ty),
+ },
+ )
+ .panic_on_oom();
}
- let ty = self.component_types.components.push(component_ty);
+ let ty = self
+ .component_types
+ .components
+ .push(component_ty)
+ .panic_on_oom();
self.component_types.module_types = Some(self.module_types.finish());
(self.component_types, ty)
@@ -216,7 +231,11 @@ impl ComponentTypesBuilder {
) -> Result<TypeFuncIndex> {
assert_eq!(types.id(), self.module_types.validator_id());
let ty = &types[id];
- let param_names = ty.params.iter().map(|(name, _)| name.to_string()).collect();
+ let param_names = ty
+ .params
+ .iter()
+ .map(|(name, _)| TryString::try_from(name.as_str()))
+ .try_collect::<TryVec<_>, _>()?;
let params = ty
.params
.iter()
@@ -248,8 +267,8 @@ impl ComponentTypesBuilder {
Ok(ComponentExtern {
ty: self.convert_component_entity_type(types, ty.ty)?,
data: ComponentExternData {
- implements: ty.implements.clone(),
- external_id: ty.external_id.clone(),
+ implements: ty.implements.clone().map(Into::into),
+ external_id: ty.external_id.clone().map(Into::into),
},
})
}
@@ -316,15 +335,15 @@ impl ComponentTypesBuilder {
self.register_abstract_component_entity_type(types, ty.ty);
result
.imports
- .insert(name.clone(), self.convert_component_item(types, ty)?);
+ .insert(name.clone().into(), self.convert_component_item(types, ty)?)?;
}
for (name, ty) in ty.exports.iter() {
self.register_abstract_component_entity_type(types, ty.ty);
result
.exports
- .insert(name.clone(), self.convert_component_item(types, ty)?);
+ .insert(name.clone().into(), self.convert_component_item(types, ty)?)?;
}
- Ok(self.component_types.components.push(result))
+ Ok(self.component_types.components.push(result)?)
}
pub(crate) fn convert_instance(
@@ -339,9 +358,9 @@ impl ComponentTypesBuilder {
self.register_abstract_component_entity_type(types, ty.ty);
result
.exports
- .insert(name.clone(), self.convert_component_item(types, ty)?);
+ .insert(name.clone().into(), self.convert_component_item(types, ty)?)?;
}
- Ok(self.component_types.component_instances.push(result))
+ Ok(self.component_types.component_instances.push(result)?)
}
fn register_abstract_component_entity_type(
@@ -371,16 +390,16 @@ impl ComponentTypesBuilder {
let mut result = TypeModule::default();
for ((module, field), ty) in ty.imports.iter() {
result.imports.insert(
- (module.clone(), field.clone()),
+ (module.clone().into(), field.clone().into()),
self.entity_type(types, ty)?,
- );
+ )?;
}
for (name, ty) in ty.exports.iter() {
result
.exports
- .insert(name.clone(), self.entity_type(types, ty)?);
+ .insert(name.clone().into(), self.entity_type(types, ty)?)?;
}
- Ok(self.component_types.modules.push(result))
+ Ok(self.component_types.modules.push(result)?)
}
fn entity_type(
@@ -522,11 +541,11 @@ impl ComponentTypesBuilder {
.iter()
.map(|(name, ty)| {
Ok(RecordField {
- name: name.to_string(),
+ name: TryString::try_from(name.as_str())?,
ty: self.valtype(types, ty)?,
})
})
- .collect::<Result<Box<[_]>>>()?;
+ .try_collect::<TryVec<_>, Error>()?;
let abi = CanonicalAbiInfo::record(
fields
.iter()
@@ -542,14 +561,14 @@ impl ComponentTypesBuilder {
.iter()
.map(|(name, case)| {
Ok((
- name.to_string(),
+ TryString::try_from(name.as_str())?,
match &case.ty.as_ref() {
Some(ty) => Some(self.valtype(types, ty)?),
None => None,
},
))
})
- .collect::<Result<IndexMap<_, _>>>()?;
+ .try_collect::<TryIndexMap<_, _>, Error>()?;
let (info, abi) = VariantInfo::new(
cases
.iter()
@@ -574,7 +593,10 @@ impl ComponentTypesBuilder {
.iter()
.map(|ty| self.component_types.canonical_abi(ty)),
);
- self.add_tuple_type(TypeTuple { types, abi })
+ self.add_tuple_type(TypeTuple {
+ types: types.into(),
+ abi,
+ })
}
fn fixed_length_list_type(
@@ -603,7 +625,11 @@ impl ComponentTypesBuilder {
fn flags_type(&mut self, flags: &IndexSet<KebabString>) -> TypeFlagsIndex {
let flags = TypeFlags {
- names: flags.iter().map(|s| s.to_string()).collect(),
+ names: flags
+ .iter()
+ .map(|s| TryString::try_from(s.as_str()))
+ .try_collect::<TryIndexSet<_>, _>()
+ .panic_on_oom(),
abi: CanonicalAbiInfo::flags(flags.len()),
};
self.add_flags_type(flags)
@@ -612,8 +638,9 @@ impl ComponentTypesBuilder {
fn enum_type(&mut self, variants: &IndexSet<KebabString>) -> TypeEnumIndex {
let names = variants
.iter()
- .map(|s| s.to_string())
- .collect::<IndexSet<_>>();
+ .map(|s| TryString::try_from(s.as_str()))
+ .try_collect::<TryIndexSet<_>, _>()
+ .panic_on_oom();
let (info, abi) = VariantInfo::new(names.iter().map(|_| None));
self.add_enum_type(TypeEnum { names, abi, info })
}
@@ -948,15 +975,15 @@ impl ComponentTypesBuilder {
}
}
-fn intern<T, U>(map: &mut HashMap<T, U>, list: &mut PrimaryMap<U, T>, item: T) -> U
+fn intern<T, U>(map: &mut HashMap<T, U>, list: &mut TryPrimaryMap<U, T>, item: T) -> U
where
- T: Hash + Clone + Eq,
+ T: Hash + TryClone + Eq,
U: Copy + EntityRef,
{
if let Some(idx) = map.get(&item) {
return *idx;
}
- let idx = list.push(item.clone());
+ let idx = list.push(item.clone_panic_on_oom()).panic_on_oom();
map.insert(item, idx);
return idx;
}
diff --git a/crates/environ/src/prelude.rs b/crates/environ/src/prelude.rs
index 70eba79316..5e21e38599 100644
--- a/crates/environ/src/prelude.rs
+++ b/crates/environ/src/prelude.rs
@@ -21,8 +21,8 @@
pub use crate::collections::{
TryBTreeMap, TryClone, TryCollect, TryEntitySet, TryExtend, TryFromIterator, TryHashMap,
- TryHashSet, TryIndexMap, TryNew, TryPrimaryMap, TrySecondaryMap, TryString, TryToOwned, TryVec,
- try_new, try_vec,
+ TryHashSet, TryIndexMap, TryIndexSet, TryNew, TryPrimaryMap, TrySecondaryMap, TryString,
+ TryToOwned, TryVec, try_new, try_vec,
};
pub use crate::error::{Context, Error, OutOfMemory, Result, bail, ensure, format_err};
pub use alloc::borrow::ToOwned;
diff --git a/crates/environ/src/types.rs b/crates/environ/src/types.rs
index 5b78cbcad3..5e13453544 100644
--- a/crates/environ/src/types.rs
+++ b/crates/environ/src/types.rs
@@ -1791,6 +1791,12 @@ pub enum EntityIndex {
Tag(TagIndex),
}
+impl TryClone for EntityIndex {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(*self)
+ }
+}
+
impl From<FuncIndex> for EntityIndex {
fn from(idx: FuncIndex) -> EntityIndex {
EntityIndex::Function(idx)
diff --git a/crates/fuzzing/tests/oom/component_component.rs b/crates/fuzzing/tests/oom/component_component.rs
index 19d33685c3..6ddbbc6948 100644
--- a/crates/fuzzing/tests/oom/component_component.rs
+++ b/crates/fuzzing/tests/oom/component_component.rs
@@ -30,3 +30,68 @@ fn component_serialize() -> Result<()> {
Ok(())
})
}
+
+#[test]
+fn component_deserialize() -> Result<()> {
+ let component_bytes = {
+ let mut config = Config::new();
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ Component::new(
+ &engine,
+ r#"
+ (component
+ (type $r (record (field "a" u32) (field "b" string)))
+ (export $r' "r" (type $r))
+ (type $e (enum "a" "b"))
+ (export $e' "e" (type $e))
+ (type $f (flags "a" "b"))
+ (export $f' "f" (type $f))
+ (type $v (variant (case "a") (case "b" (list $r'))))
+ (export $v' "v" (type $v))
+ (type $t (tuple (option $f') (result string (error $e'))))
+ (import "host" (func $host (param "x" string) (result u32)))
+
+ (core module $libc
+ (memory (export "memory") 1)
+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)
+ unreachable)
+ )
+ (core instance $libc (instantiate $libc))
+ (core func $host (canon lower (func $host)
+ (memory (core memory $libc "memory")) (realloc (core func $libc "realloc"))))
+
+ (core module $m
+ (import "" "host" (func (param i32 i32) (result i32)))
+ (func (export "id") (param i32) (result i32) (local.get 0))
+ (func (export "tuple") (param i32 i32 i32 i32 i32) (result i32)
+ unreachable)
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance (export "host" (func $host))))
+ ))
+ (func (export "id") (param "x" s32) (result s32)
+ (canon lift (core func $i "id")))
+ (func (export "tuple") (param "x" $t) (result $v')
+ (canon lift (core func $i "tuple")
+ (memory (core memory $libc "memory")) (realloc (core func $libc "realloc"))))
+ )
+ "#,
+ )?
+ .serialize()?
+ };
+
+ let mut config = Config::new();
+ config.enable_compiler(false);
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+
+ OomTest::new()
+ // NB: see `module_deserialize` for why allocation after OOM is
+ // allowed.
+ .allow_alloc_after_oom(true)
+ .test(|| unsafe {
+ let _ = Component::deserialize(&engine, &component_bytes)?;
+ Ok(())
+ })
+}
diff --git a/crates/wasmtime/src/compile.rs b/crates/wasmtime/src/compile.rs
index deb540deee..6c9c68424d 100644
--- a/crates/wasmtime/src/compile.rs
+++ b/crates/wasmtime/src/compile.rs
@@ -233,7 +233,7 @@ pub(crate) fn build_component_artifacts<T: FinishedObject>(
table: compilation_artifacts.table,
ty,
types,
- static_modules: compilation_artifacts.modules,
+ static_modules: compilation_artifacts.modules.into(),
checksum: WasmChecksum::from_binary(binary, tunables.recording),
};
object.serialize_info(&artifacts);
diff --git a/crates/wasmtime/src/runtime/component/component.rs b/crates/wasmtime/src/runtime/component/component.rs
index 8efb57c051..94f407e0e1 100644
--- a/crates/wasmtime/src/runtime/component/component.rs
+++ b/crates/wasmtime/src/runtime/component/component.rs
@@ -23,7 +23,7 @@ use wasmtime_environ::component::{
TrampolineIndex, TypeComponentIndex, TypeFuncIndex, UnsafeIntrinsic, VMComponentOffsets,
};
use wasmtime_environ::{Abi, CompiledFunctionsTable, FuncKey, TypeTrace, WasmChecksum};
-use wasmtime_environ::{FunctionLoc, HostPtr, ObjectKind, PrimaryMap};
+use wasmtime_environ::{FunctionLoc, HostPtr, ObjectKind};
/// A compiled WebAssembly Component.
///
@@ -89,7 +89,7 @@ struct ComponentInner {
/// Core wasm modules that the component defined internally, indexed by the
/// compile-time-assigned `ModuleUpvarIndex`.
- static_modules: PrimaryMap<StaticModuleIndex, Module>,
+ static_modules: TryPrimaryMap<StaticModuleIndex, Module>,
/// Code-related information such as the compiled artifact, type
/// information, etc.
@@ -444,7 +444,7 @@ impl Component {
Some(artifacts) => artifacts,
None => postcard::from_bytes(code_memory.wasmtime_info())?,
};
- let index = Arc::new(index);
+ let index = try_new::<Arc<_>>(index)?;
// Validate that the component can be used with the current instance
// allocator.
@@ -465,8 +465,8 @@ 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 types = try_new::<Arc<_>>(types)?;
+ let code = try_new::<Arc<_>>(EngineCode::new(code_memory, signatures, types.into())?)?;
// Convert all information about static core wasm modules into actual
// `Module` instances by converting each `CompiledModuleInfo`, the
@@ -476,16 +476,16 @@ impl Component {
.map(|(_, info)| {
Module::from_parts_raw(engine, code.clone(), info, index.clone(), false)
})
- .collect::<Result<_>>()?;
+ .try_collect::<_, Error>()?;
- let realloc_func_type = Arc::new(FuncType::new(
+ let realloc_func_type = try_new::<Arc<_>>(FuncType::try_new(
engine,
[ValType::I32, ValType::I32, ValType::I32, ValType::I32],
[ValType::I32],
- ));
+ )?)?;
Ok(Component {
- inner: Arc::new(ComponentInner {
+ inner: try_new::<Arc<_>>(ComponentInner {
id: CompiledModuleId::new(),
engine: engine.clone(),
ty,
@@ -495,7 +495,7 @@ impl Component {
index,
realloc_func_type,
checksum,
- }),
+ })?,
})
}
diff --git a/crates/wasmtime/src/runtime/component/func.rs b/crates/wasmtime/src/runtime/component/func.rs
index 0e8053b3de..c22d37b00f 100644
--- a/crates/wasmtime/src/runtime/component/func.rs
+++ b/crates/wasmtime/src/runtime/component/func.rs
@@ -66,12 +66,9 @@ impl Func {
instance: Instance,
index: ExportIndex,
) -> Func {
- let def = {
- let vminstance = instance.id().get(store);
- let (_ty, def, _options) = vminstance.component().export_lifted_function(index);
- def.clone()
- };
- let unsafe_func_ref = match instance.lookup_vmdef(store, &def) {
+ let (component, store) = instance.component_and_store_mut(store);
+ let (_ty, def, _options) = component.export_lifted_function(index);
+ let unsafe_func_ref = match instance.lookup_vmdef(store, def) {
Export::Function(f) => f.vm_func_ref(store),
_ => unreachable!(),
}
diff --git a/crates/wasmtime/src/runtime/component/func/typed.rs b/crates/wasmtime/src/runtime/component/func/typed.rs
index abbdfc9dae..56c8b30184 100644
--- a/crates/wasmtime/src/runtime/component/func/typed.rs
+++ b/crates/wasmtime/src/runtime/component/func/typed.rs
@@ -2291,7 +2291,7 @@ pub fn typecheck_record(
check(&field.ty, types)
.with_context(|| format!("type mismatch for field {name}"))?;
- if field.name != name {
+ if *field.name != *name {
bail!("expected record field named {}, found {}", name, field.name);
}
}
@@ -2325,7 +2325,7 @@ pub fn typecheck_variant(
}
for ((case_name, case_ty), &(name, check)) in cases.iter().zip(expected) {
- if *case_name != name {
+ if **case_name != *name {
bail!("expected variant case named {name}, found {case_name}");
}
@@ -2368,7 +2368,7 @@ pub fn typecheck_enum(
}
for (name, expected) in names.iter().zip(expected) {
- if name != expected {
+ if **name != **expected {
bail!("expected enum case named {expected}, found {name}");
}
}
@@ -2399,7 +2399,7 @@ pub fn typecheck_flags(
}
for (name, expected) in names.iter().zip(expected) {
- if name != expected {
+ if **name != **expected {
bail!("expected flag named {expected}, found {name}");
}
}
diff --git a/crates/wasmtime/src/runtime/component/matching.rs b/crates/wasmtime/src/runtime/component/matching.rs
index 9c2dccf3ba..d33c73ba76 100644
--- a/crates/wasmtime/src/runtime/component/matching.rs
+++ b/crates/wasmtime/src/runtime/component/matching.rs
@@ -143,7 +143,7 @@ impl TypeChecker<'_> {
// TODO: shouldn't need a `.to_string()` here ideally
let expected = expected
.imports
- .get(&(module.to_string(), name.to_string()))
+ .get(&(module.to_string().into(), name.to_string().into()))
.ok_or_else(|| format_err!("module import `{module}::{name}` not defined"))?;
matching::entity_ty(self.engine, &actual, expected)
.with_context(|| format!("module import `{module}::{name}` has the wrong type"))?;
diff --git a/crates/wasmtime/src/runtime/component/values.rs b/crates/wasmtime/src/runtime/component/values.rs
index f01295eb63..16a624d890 100644
--- a/crates/wasmtime/src/runtime/component/values.rs
+++ b/crates/wasmtime/src/runtime/component/values.rs
@@ -184,7 +184,7 @@ impl Val {
let (k, _) = vty.cases.get_index(discriminant as usize).unwrap();
cx.consume_fuel(k.len())?;
- Val::Variant(k.clone(), value)
+ Val::Variant(k.to_string(), value)
}
InterfaceType::Enum(i) => {
let ety = &cx.types[i];
@@ -197,7 +197,7 @@ impl Val {
let name = &ety.names[discriminant as usize];
cx.consume_fuel(name.len())?;
- Val::Enum(name.clone())
+ Val::Enum(name.to_string())
}
InterfaceType::Option(i) => {
let (_discriminant, value) = lift_variant(
@@ -327,7 +327,7 @@ impl Val {
let (k, _) = ty.cases.get_index(discriminant as usize).unwrap();
cx.consume_fuel(k.len())?;
- Val::Variant(k.clone(), value)
+ Val::Variant(k.to_string(), value)
}
InterfaceType::Enum(i) => {
let ty = &cx.types[i];
@@ -336,7 +336,7 @@ impl Val {
let name = &ty.names[discriminant as usize];
cx.consume_fuel(name.len())?;
- Val::Enum(name.clone())
+ Val::Enum(name.to_string())
}
InterfaceType::Option(i) => {
let ty = &cx.types[i];
@@ -505,7 +505,7 @@ impl Val {
bail!("expected {} fields, got {}", ty.fields.len(), values.len());
}
for ((name, value), field) in values.iter().zip(ty.fields.iter()) {
- if *name != field.name {
+ if **name != *field.name {
bail!("expected field `{}`, got `{name}`", field.name);
}
value.lower(cx, field.ty, dst)?;
@@ -654,7 +654,7 @@ impl Val {
}
let mut offset = offset;
for ((name, value), field) in values.iter().zip(ty.fields.iter()) {
- if *name != field.name {
+ if **name != *field.name {
bail!("expected field `{}`, got `{name}`", field.name);
}
value.store(
@@ -1239,7 +1239,7 @@ fn push_flags(
if bits & 1 != 0 {
let name = &ty.names[offset as usize];
cx.consume_fuel(name.len())?;
- flags.push(name.clone());
+ flags.push(name.to_string());
}
bits >>= 1;
offset += 1;
@@ -1257,7 +1257,7 @@ fn flags_to_storage(ty: &TypeFlags, flags: &[String]) -> Result<Vec<u32>> {
for flag in flags {
let bit = ty
.names
- .get_index_of(flag)
+ .get_index_of(flag.as_str())
.ok_or_else(|| crate::format_err!("unknown flag: `{flag}`"))?;
storage[bit / 32] |= 1 << (bit % 32);
}