Skip to content

Commit 4c503f3

Browse files
committed
Use TryPrimaryMap in an instance's OwnedImports
1 parent 9eed075 commit 4c503f3

File tree

2 files changed

+51
-45
lines changed

2 files changed

+51
-45
lines changed

crates/wasmtime/src/runtime/component/instance.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ impl<'a> Instantiator<'a> {
823823
// `args` list is already in the right order.
824824
InstantiateModule::Static(idx, args) => {
825825
module = self.component.static_module(*idx);
826-
self.build_imports(store.0, module, args.iter())
826+
self.build_imports(store.0, module, args.iter())?
827827
}
828828

829829
// With imports, unlike upvars, we need to do runtime
@@ -842,7 +842,7 @@ impl<'a> Instantiator<'a> {
842842
let args = module
843843
.imports()
844844
.map(|import| &args[import.module()][import.name()]);
845-
self.build_imports(store.0, module, args)
845+
self.build_imports(store.0, module, args)?
846846
}
847847
};
848848

@@ -985,9 +985,9 @@ impl<'a> Instantiator<'a> {
985985
store: &mut StoreOpaque,
986986
module: &Module,
987987
args: impl Iterator<Item = &'b CoreDef>,
988-
) -> &OwnedImports {
988+
) -> Result<&OwnedImports, OutOfMemory> {
989989
self.core_imports.clear();
990-
self.core_imports.reserve(module);
990+
self.core_imports.reserve(module)?;
991991
let mut imports = module.compiled_module().module().imports();
992992

993993
for arg in args {
@@ -1005,11 +1005,11 @@ impl<'a> Instantiator<'a> {
10051005
// directly from an instance which should only give us valid export
10061006
// items.
10071007
let export = lookup_vmdef(store, self.id, arg);
1008-
self.core_imports.push_export(store, &export);
1008+
self.core_imports.push_export(store, &export)?;
10091009
}
10101010
debug_assert!(imports.next().is_none());
10111011

1012-
&self.core_imports
1012+
Ok(&self.core_imports)
10131013
}
10141014

10151015
fn assert_type_matches(

crates/wasmtime/src/runtime/instance.rs

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ use alloc::sync::Arc;
1616
use core::ptr::NonNull;
1717
use wasmparser::WasmFeatures;
1818
use wasmtime_environ::{
19-
EntityIndex, EntityType, FuncIndex, GlobalIndex, MemoryIndex, PrimaryMap, TableIndex, TagIndex,
20-
TypeTrace,
19+
EntityIndex, EntityType, FuncIndex, GlobalIndex, MemoryIndex, TableIndex, TagIndex, TypeTrace,
2120
};
2221

2322
/// An instantiated WebAssembly module.
@@ -230,9 +229,9 @@ impl Instance {
230229
let (funcrefs, modules) = store.func_refs_and_modules();
231230
funcrefs.fill(modules);
232231

233-
let mut owned_imports = OwnedImports::new(module);
232+
let mut owned_imports = OwnedImports::new(module)?;
234233
for import in imports {
235-
owned_imports.push(import, store);
234+
owned_imports.push(import, store)?;
236235
}
237236
Ok(owned_imports)
238237
}
@@ -657,37 +656,38 @@ impl Instance {
657656
}
658657

659658
pub(crate) struct OwnedImports {
660-
functions: PrimaryMap<FuncIndex, VMFunctionImport>,
661-
tables: PrimaryMap<TableIndex, VMTableImport>,
662-
memories: PrimaryMap<MemoryIndex, VMMemoryImport>,
663-
globals: PrimaryMap<GlobalIndex, VMGlobalImport>,
664-
tags: PrimaryMap<TagIndex, VMTagImport>,
659+
functions: TryPrimaryMap<FuncIndex, VMFunctionImport>,
660+
tables: TryPrimaryMap<TableIndex, VMTableImport>,
661+
memories: TryPrimaryMap<MemoryIndex, VMMemoryImport>,
662+
globals: TryPrimaryMap<GlobalIndex, VMGlobalImport>,
663+
tags: TryPrimaryMap<TagIndex, VMTagImport>,
665664
}
666665

667666
impl OwnedImports {
668-
fn new(module: &Module) -> OwnedImports {
667+
fn new(module: &Module) -> Result<OwnedImports, OutOfMemory> {
669668
let mut ret = OwnedImports::empty();
670-
ret.reserve(module);
671-
return ret;
669+
ret.reserve(module)?;
670+
Ok(ret)
672671
}
673672

674673
pub(crate) fn empty() -> OwnedImports {
675674
OwnedImports {
676-
functions: PrimaryMap::new(),
677-
tables: PrimaryMap::new(),
678-
memories: PrimaryMap::new(),
679-
globals: PrimaryMap::new(),
680-
tags: PrimaryMap::new(),
675+
functions: TryPrimaryMap::new(),
676+
tables: TryPrimaryMap::new(),
677+
memories: TryPrimaryMap::new(),
678+
globals: TryPrimaryMap::new(),
679+
tags: TryPrimaryMap::new(),
681680
}
682681
}
683682

684-
pub(crate) fn reserve(&mut self, module: &Module) {
683+
pub(crate) fn reserve(&mut self, module: &Module) -> Result<(), OutOfMemory> {
685684
let raw = module.compiled_module().module();
686-
self.functions.reserve(raw.num_imported_funcs);
687-
self.tables.reserve(raw.num_imported_tables);
688-
self.memories.reserve(raw.num_imported_memories);
689-
self.globals.reserve(raw.num_imported_globals);
690-
self.tags.reserve(raw.num_imported_tags);
685+
self.functions.reserve(raw.num_imported_funcs)?;
686+
self.tables.reserve(raw.num_imported_tables)?;
687+
self.memories.reserve(raw.num_imported_memories)?;
688+
self.globals.reserve(raw.num_imported_globals)?;
689+
self.tags.reserve(raw.num_imported_tags)?;
690+
Ok(())
691691
}
692692

693693
#[cfg(feature = "component-model")]
@@ -699,33 +699,38 @@ impl OwnedImports {
699699
self.tags.clear();
700700
}
701701

702-
fn push(&mut self, item: &Extern, store: &mut StoreOpaque) {
702+
fn push(&mut self, item: &Extern, store: &mut StoreOpaque) -> Result<(), OutOfMemory> {
703703
match item {
704704
Extern::Func(i) => {
705-
self.functions.push(i.vmimport(store));
705+
self.functions.push(i.vmimport(store))?;
706706
}
707707
Extern::Global(i) => {
708-
self.globals.push(i.vmimport(store));
708+
self.globals.push(i.vmimport(store))?;
709709
}
710710
Extern::Table(i) => {
711-
self.tables.push(i.vmimport(store));
711+
self.tables.push(i.vmimport(store))?;
712712
}
713713
Extern::Memory(i) => {
714-
self.memories.push(i.vmimport(store));
714+
self.memories.push(i.vmimport(store))?;
715715
}
716716
Extern::SharedMemory(i) => {
717-
self.memories.push(i.vmimport(store));
717+
self.memories.push(i.vmimport(store))?;
718718
}
719719
Extern::Tag(i) => {
720-
self.tags.push(i.vmimport(store));
720+
self.tags.push(i.vmimport(store))?;
721721
}
722722
}
723+
Ok(())
723724
}
724725

725726
/// Note that this is unsafe as the validity of `item` is not verified and
726727
/// it contains a bunch of raw pointers.
727728
#[cfg(feature = "component-model")]
728-
pub(crate) fn push_export(&mut self, store: &StoreOpaque, item: &crate::runtime::vm::Export) {
729+
pub(crate) fn push_export(
730+
&mut self,
731+
store: &StoreOpaque,
732+
item: &crate::runtime::vm::Export,
733+
) -> Result<(), OutOfMemory> {
729734
match item {
730735
crate::runtime::vm::Export::Function(f) => {
731736
// SAFETY: the funcref associated with a `Func` is valid to use
@@ -735,24 +740,25 @@ impl OwnedImports {
735740
wasm_call: f.wasm_call.unwrap(),
736741
array_call: f.array_call,
737742
vmctx: f.vmctx,
738-
});
743+
})?;
739744
}
740745
crate::runtime::vm::Export::Global(g) => {
741-
self.globals.push(g.vmimport(store));
746+
self.globals.push(g.vmimport(store))?;
742747
}
743748
crate::runtime::vm::Export::Table(t) => {
744-
self.tables.push(t.vmimport(store));
749+
self.tables.push(t.vmimport(store))?;
745750
}
746751
crate::runtime::vm::Export::Memory(m) => {
747-
self.memories.push(m.vmimport(store));
752+
self.memories.push(m.vmimport(store))?;
748753
}
749754
crate::runtime::vm::Export::SharedMemory(_, vmimport) => {
750-
self.memories.push(*vmimport);
755+
self.memories.push(*vmimport)?;
751756
}
752757
crate::runtime::vm::Export::Tag(t) => {
753-
self.tags.push(t.vmimport(store));
758+
self.tags.push(t.vmimport(store))?;
754759
}
755760
}
761+
Ok(())
756762
}
757763

758764
pub(crate) fn as_ref(&self) -> Imports<'_> {
@@ -989,7 +995,7 @@ fn pre_instantiate_raw(
989995
store.set_async_required(asyncness);
990996

991997
let mut func_refs = func_refs.iter().map(|f| NonNull::from(f));
992-
let mut imports = OwnedImports::new(module);
998+
let mut imports = OwnedImports::new(module)?;
993999
for import in items.iter() {
9941000
if !import.comes_from_same_store(store) {
9951001
bail!("cross-`Store` instantiation is not currently supported");
@@ -1012,7 +1018,7 @@ fn pre_instantiate_raw(
10121018
.into()
10131019
},
10141020
};
1015-
imports.push(&item, store);
1021+
imports.push(&item, store)?;
10161022
}
10171023

10181024
Ok(imports)

0 commit comments

Comments
 (0)