Skip to content

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

Use `TryPrimaryMap` in `Instance` (#12848)

Nick Fitzgerald9c44a9b47210
diff --git a/crates/wasmtime/src/runtime/vm/instance.rs b/crates/wasmtime/src/runtime/vm/instance.rs
index ac37501506..eb459f0f17 100644
--- a/crates/wasmtime/src/runtime/vm/instance.rs
+++ b/crates/wasmtime/src/runtime/vm/instance.rs
@@ -37,8 +37,8 @@ use wasmtime_environ::ModuleInternedTypeIndex;
use wasmtime_environ::error::OutOfMemory;
use wasmtime_environ::{
DataIndex, DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex, DefinedTagIndex,
- ElemIndex, EntityIndex, EntityRef, FuncIndex, GlobalIndex, HostPtr, MemoryIndex, PrimaryMap,
- PtrSize, TableIndex, TableInitialValue, TableSegmentElements, TagIndex, Trap, VMCONTEXT_MAGIC,
+ ElemIndex, EntityIndex, EntityRef, FuncIndex, GlobalIndex, HostPtr, MemoryIndex, PtrSize,
+ TableIndex, TableInitialValue, TableSegmentElements, TagIndex, Trap, VMCONTEXT_MAGIC,
VMOffsets, VMSharedTypeIndex, packed_option::ReservedValue,
};
#[cfg(feature = "wmemcheck")]
@@ -116,7 +116,7 @@ pub struct Instance {
/// The `MemoryAllocationIndex` was given from our `InstanceAllocator` and
/// must be given back to the instance allocator when deallocating each
/// memory.
- memories: PrimaryMap<DefinedMemoryIndex, (MemoryAllocationIndex, Memory)>,
+ memories: TryPrimaryMap<DefinedMemoryIndex, (MemoryAllocationIndex, Memory)>,
/// WebAssembly table data.
///
@@ -126,7 +126,7 @@ pub struct Instance {
/// The `TableAllocationIndex` was given from our `InstanceAllocator` and
/// must be given back to the instance allocator when deallocating each
/// table.
- tables: PrimaryMap<DefinedTableIndex, (TableAllocationIndex, Table)>,
+ tables: TryPrimaryMap<DefinedTableIndex, (TableAllocationIndex, Table)>,
/// Stores the dropped passive element segments in this instantiation by index.
/// If the index is present in the set, the segment has been dropped.
@@ -166,8 +166,8 @@ impl Instance {
/// and `tables` must have been allocated for `req.store`.
unsafe fn new(
req: InstanceAllocationRequest,
- memories: PrimaryMap<DefinedMemoryIndex, (MemoryAllocationIndex, Memory)>,
- tables: PrimaryMap<DefinedTableIndex, (TableAllocationIndex, Table)>,
+ memories: TryPrimaryMap<DefinedMemoryIndex, (MemoryAllocationIndex, Memory)>,
+ tables: TryPrimaryMap<DefinedTableIndex, (TableAllocationIndex, Table)>,
) -> Result<InstanceHandle, OutOfMemory> {
let module = req.runtime_info.env_module();
let memory_tys = &module.memories;
@@ -1638,14 +1638,14 @@ impl Instance {
fn memories_mut(
self: Pin<&mut Self>,
- ) -> &mut PrimaryMap<DefinedMemoryIndex, (MemoryAllocationIndex, Memory)> {
+ ) -> &mut TryPrimaryMap<DefinedMemoryIndex, (MemoryAllocationIndex, Memory)> {
// SAFETY: see `store_mut` above.
unsafe { &mut self.get_unchecked_mut().memories }
}
pub(crate) fn tables_mut(
self: Pin<&mut Self>,
- ) -> &mut PrimaryMap<DefinedTableIndex, (TableAllocationIndex, Table)> {
+ ) -> &mut TryPrimaryMap<DefinedTableIndex, (TableAllocationIndex, Table)> {
// SAFETY: see `store_mut` above.
unsafe { &mut self.get_unchecked_mut().tables }
}
diff --git a/crates/wasmtime/src/runtime/vm/instance/allocator.rs b/crates/wasmtime/src/runtime/vm/instance/allocator.rs
index d7935919cb..fd2a52b26e 100644
--- a/crates/wasmtime/src/runtime/vm/instance/allocator.rs
+++ b/crates/wasmtime/src/runtime/vm/instance/allocator.rs
@@ -11,7 +11,7 @@ use crate::{OpaqueRootScope, Val};
use core::{mem, ptr};
use wasmtime_environ::{
DefinedMemoryIndex, DefinedTableIndex, HostPtr, InitMemory, MemoryInitialization,
- MemoryInitializer, Module, PrimaryMap, SizeOverflow, TableInitialValue, Trap, VMOffsets,
+ MemoryInitializer, Module, SizeOverflow, TableInitialValue, Trap, VMOffsets,
};
#[cfg(feature = "gc")]
@@ -326,8 +326,8 @@ impl dyn InstanceAllocator + '_ {
let mut guard = DeallocateOnDrop {
run_deallocate: true,
- memories: PrimaryMap::with_capacity(num_defined_memories),
- tables: PrimaryMap::with_capacity(num_defined_tables),
+ memories: TryPrimaryMap::with_capacity(num_defined_memories)?,
+ tables: TryPrimaryMap::with_capacity(num_defined_tables)?,
allocator: self,
};
@@ -349,8 +349,8 @@ impl dyn InstanceAllocator + '_ {
struct DeallocateOnDrop<'a> {
run_deallocate: bool,
- memories: PrimaryMap<DefinedMemoryIndex, (MemoryAllocationIndex, Memory)>,
- tables: PrimaryMap<DefinedTableIndex, (TableAllocationIndex, Table)>,
+ memories: TryPrimaryMap<DefinedMemoryIndex, (MemoryAllocationIndex, Memory)>,
+ tables: TryPrimaryMap<DefinedTableIndex, (TableAllocationIndex, Table)>,
allocator: &'a (dyn InstanceAllocator + 'a),
}
@@ -394,7 +394,7 @@ impl dyn InstanceAllocator + '_ {
async fn allocate_memories(
&self,
request: &mut InstanceAllocationRequest<'_, '_>,
- memories: &mut PrimaryMap<DefinedMemoryIndex, (MemoryAllocationIndex, Memory)>,
+ memories: &mut TryPrimaryMap<DefinedMemoryIndex, (MemoryAllocationIndex, Memory)>,
) -> Result<()> {
let module = request.runtime_info.env_module();
@@ -411,7 +411,7 @@ impl dyn InstanceAllocator + '_ {
let memory = self
.allocate_memory(request, ty, Some(memory_index))
.await?;
- memories.push(memory);
+ memories.push(memory)?;
}
Ok(())
@@ -425,7 +425,7 @@ impl dyn InstanceAllocator + '_ {
/// `Self::allocate_memories`.
unsafe fn deallocate_memories(
&self,
- memories: &mut PrimaryMap<DefinedMemoryIndex, (MemoryAllocationIndex, Memory)>,
+ memories: &mut TryPrimaryMap<DefinedMemoryIndex, (MemoryAllocationIndex, Memory)>,
) {
for (memory_index, (allocation_index, memory)) in mem::take(memories) {
// Because deallocating memory is infallible, we don't need to worry
@@ -447,7 +447,7 @@ impl dyn InstanceAllocator + '_ {
async fn allocate_tables(
&self,
request: &mut InstanceAllocationRequest<'_, '_>,
- tables: &mut PrimaryMap<DefinedTableIndex, (TableAllocationIndex, Table)>,
+ tables: &mut TryPrimaryMap<DefinedTableIndex, (TableAllocationIndex, Table)>,
) -> Result<()> {
let module = request.runtime_info.env_module();
@@ -462,7 +462,7 @@ impl dyn InstanceAllocator + '_ {
.expect("should be a defined table since we skipped imported ones");
let table = self.allocate_table(request, table, def_index).await?;
- tables.push(table);
+ tables.push(table)?;
}
Ok(())
@@ -476,7 +476,7 @@ impl dyn InstanceAllocator + '_ {
/// `Self::allocate_tables`.
unsafe fn deallocate_tables(
&self,
- tables: &mut PrimaryMap<DefinedTableIndex, (TableAllocationIndex, Table)>,
+ tables: &mut TryPrimaryMap<DefinedTableIndex, (TableAllocationIndex, Table)>,
) {
for (table_index, (allocation_index, table)) in mem::take(tables) {
// SAFETY: the tables here were allocated from this allocator per