Skip to content

Commit 9c17fed

Browse files
authored
Use OOM-handling PrimaryMap in wasmtime_environ::Module (#12606)
1 parent ba8f7d7 commit 9c17fed

5 files changed

Lines changed: 50 additions & 38 deletions

File tree

crates/environ/src/compile/module_environ.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> {
281281
log::trace!("interning {count} Wasm types");
282282

283283
let capacity = usize::try_from(count).unwrap();
284-
self.result.module.types.reserve(capacity);
284+
self.result.module.types.reserve(capacity)?;
285285
self.types.reserve_wasm_signatures(capacity);
286286

287287
// Iterate over each *rec group* -- not type -- defined in the
@@ -318,9 +318,9 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> {
318318
let interned = self.types.intern_rec_group(validator_types, rec_group_id)?;
319319
let elems = self.types.rec_group_elements(interned);
320320
let len = elems.len();
321-
self.result.module.types.reserve(len);
321+
self.result.module.types.reserve(len)?;
322322
for ty in elems {
323-
self.result.module.types.push(ty.into());
323+
self.result.module.types.push(ty.into())?;
324324
}
325325

326326
// Advance `type_index` to the start of the next rec group.
@@ -381,7 +381,7 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> {
381381
self.validator.function_section(&functions)?;
382382

383383
let cnt = usize::try_from(functions.count()).unwrap();
384-
self.result.module.functions.reserve_exact(cnt);
384+
self.result.module.functions.reserve_exact(cnt)?;
385385

386386
for entry in functions {
387387
let sigindex = entry?;
@@ -394,13 +394,13 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> {
394394
Payload::TableSection(tables) => {
395395
self.validator.table_section(&tables)?;
396396
let cnt = usize::try_from(tables.count()).unwrap();
397-
self.result.module.tables.reserve_exact(cnt);
397+
self.result.module.tables.reserve_exact(cnt)?;
398398

399399
for entry in tables {
400400
let wasmparser::Table { ty, init } = entry?;
401401
let table = self.convert_table_type(&ty)?;
402402
self.result.module.needs_gc_heap |= table.ref_type.is_vmgcref_type();
403-
self.result.module.tables.push(table);
403+
self.result.module.tables.push(table)?;
404404
let init = match init {
405405
wasmparser::TableInit::RefNull => TableInitialValue::Null {
406406
precomputed: collections::Vec::new(),
@@ -417,19 +417,19 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> {
417417
.module
418418
.table_initialization
419419
.initial_values
420-
.push(init);
420+
.push(init)?;
421421
}
422422
}
423423

424424
Payload::MemorySection(memories) => {
425425
self.validator.memory_section(&memories)?;
426426

427427
let cnt = usize::try_from(memories.count()).unwrap();
428-
self.result.module.memories.reserve_exact(cnt);
428+
self.result.module.memories.reserve_exact(cnt)?;
429429

430430
for entry in memories {
431431
let memory = entry?;
432-
self.result.module.memories.push(memory.into());
432+
self.result.module.memories.push(memory.into())?;
433433
}
434434
}
435435

@@ -451,7 +451,7 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> {
451451
self.validator.global_section(&globals)?;
452452

453453
let cnt = usize::try_from(globals.count()).unwrap();
454-
self.result.module.globals.reserve_exact(cnt);
454+
self.result.module.globals.reserve_exact(cnt)?;
455455

456456
for entry in globals {
457457
let wasmparser::Global { ty, init_expr } = entry?;
@@ -460,8 +460,8 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> {
460460
self.flag_func_escaped(f);
461461
}
462462
let ty = self.convert_global_type(&ty)?;
463-
self.result.module.globals.push(ty);
464-
self.result.module.global_initializers.push(initializer);
463+
self.result.module.globals.push(ty)?;
464+
self.result.module.global_initializers.push(initializer)?;
465465
}
466466
}
467467

@@ -835,10 +835,18 @@ and for re-adding support for interface types you can see this issue:
835835
self.flag_func_escaped(func_index);
836836
func_index
837837
}),
838-
EntityType::Table(ty) => EntityIndex::Table(self.result.module.tables.push(ty)),
839-
EntityType::Memory(ty) => EntityIndex::Memory(self.result.module.memories.push(ty)),
840-
EntityType::Global(ty) => EntityIndex::Global(self.result.module.globals.push(ty)),
841-
EntityType::Tag(ty) => EntityIndex::Tag(self.result.module.tags.push(ty)),
838+
EntityType::Table(ty) => {
839+
EntityIndex::Table(self.result.module.tables.push(ty).panic_on_oom())
840+
}
841+
EntityType::Memory(ty) => {
842+
EntityIndex::Memory(self.result.module.memories.push(ty).panic_on_oom())
843+
}
844+
EntityType::Global(ty) => {
845+
EntityIndex::Global(self.result.module.globals.push(ty).panic_on_oom())
846+
}
847+
EntityType::Tag(ty) => {
848+
EntityIndex::Tag(self.result.module.tags.push(ty).panic_on_oom())
849+
}
842850
}
843851
}
844852

@@ -1099,7 +1107,7 @@ impl ModuleTranslation<'_> {
10991107
// memory initialization image is built here from the page data and then
11001108
// it's converted to a single initializer.
11011109
let data = mem::replace(&mut self.data, Vec::new());
1102-
let mut map = PrimaryMap::with_capacity(info.len());
1110+
let mut map = collections::PrimaryMap::with_capacity(info.len()).panic_on_oom();
11031111
let mut module_data_size = 0u32;
11041112
for (memory, info) in info.iter() {
11051113
// Create the in-memory `image` which is the initialized contents of
@@ -1183,7 +1191,7 @@ impl ModuleTranslation<'_> {
11831191
} else {
11841192
None
11851193
};
1186-
let idx = map.push(init);
1194+
let idx = map.push(init).panic_on_oom();
11871195
assert_eq!(idx, memory);
11881196
module_data_size += len;
11891197
}

crates/environ/src/module.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub enum MemoryInitialization {
8282
///
8383
/// The offset, range base, and range end are all guaranteed to be page
8484
/// aligned to the page size passed in to `try_static_init`.
85-
map: PrimaryMap<MemoryIndex, Option<StaticMemoryInitializer>>,
85+
map: collections::PrimaryMap<MemoryIndex, Option<StaticMemoryInitializer>>,
8686
},
8787
}
8888

@@ -226,7 +226,7 @@ pub struct TableInitialization {
226226
/// initialization. For example table initializers to a table that are all
227227
/// in-bounds will get removed from `segment` and moved into
228228
/// `initial_values` here.
229-
pub initial_values: PrimaryMap<DefinedTableIndex, TableInitialValue>,
229+
pub initial_values: collections::PrimaryMap<DefinedTableIndex, TableInitialValue>,
230230

231231
/// Element segments present in the initial wasm module which are executed
232232
/// at instantiation time.
@@ -325,7 +325,7 @@ pub struct Module {
325325
pub passive_data_map: BTreeMap<DataIndex, Range<u32>>,
326326

327327
/// Types declared in the wasm module.
328-
pub types: PrimaryMap<TypeIndex, EngineOrModuleTypeIndex>,
328+
pub types: collections::PrimaryMap<TypeIndex, EngineOrModuleTypeIndex>,
329329

330330
/// Number of imported or aliased functions in the module.
331331
pub num_imported_funcs: usize,
@@ -353,22 +353,22 @@ pub struct Module {
353353
pub num_escaped_funcs: usize,
354354

355355
/// Types of functions, imported and local.
356-
pub functions: PrimaryMap<FuncIndex, FunctionType>,
356+
pub functions: collections::PrimaryMap<FuncIndex, FunctionType>,
357357

358358
/// WebAssembly tables.
359-
pub tables: PrimaryMap<TableIndex, Table>,
359+
pub tables: collections::PrimaryMap<TableIndex, Table>,
360360

361361
/// WebAssembly linear memory plans.
362-
pub memories: PrimaryMap<MemoryIndex, Memory>,
362+
pub memories: collections::PrimaryMap<MemoryIndex, Memory>,
363363

364364
/// WebAssembly global variables.
365-
pub globals: PrimaryMap<GlobalIndex, Global>,
365+
pub globals: collections::PrimaryMap<GlobalIndex, Global>,
366366

367367
/// WebAssembly global initializers for locally-defined globals.
368-
pub global_initializers: PrimaryMap<DefinedGlobalIndex, ConstExpr>,
368+
pub global_initializers: collections::PrimaryMap<DefinedGlobalIndex, ConstExpr>,
369369

370370
/// WebAssembly exception and control tags.
371-
pub tags: PrimaryMap<TagIndex, Tag>,
371+
pub tags: collections::PrimaryMap<TagIndex, Tag>,
372372
}
373373

374374
/// Initialization routines for creating an instance, encompassing imports,
@@ -605,21 +605,25 @@ impl Module {
605605
) -> TagIndex {
606606
let signature = signature.into();
607607
let exception = exception.into();
608-
self.tags.push(Tag {
609-
signature,
610-
exception,
611-
})
608+
self.tags
609+
.push(Tag {
610+
signature,
611+
exception,
612+
})
613+
.panic_on_oom()
612614
}
613615

614616
/// Appends a new function to this module with the given type information,
615617
/// used for functions that either don't escape or aren't certain whether
616618
/// they escape yet.
617619
pub fn push_function(&mut self, signature: impl Into<EngineOrModuleTypeIndex>) -> FuncIndex {
618620
let signature = signature.into();
619-
self.functions.push(FunctionType {
620-
signature,
621-
func_ref: FuncRefIndex::reserved_value(),
622-
})
621+
self.functions
622+
.push(FunctionType {
623+
signature,
624+
func_ref: FuncRefIndex::reserved_value(),
625+
})
626+
.panic_on_oom()
623627
}
624628

625629
/// Returns an iterator over all of the defined function indices in this

crates/wasmtime/src/runtime/trampoline/memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub async fn create_memory(
3333
// Create a memory, though it will never be used for constructing a memory
3434
// with an allocator: instead the memories are either preallocated (i.e.,
3535
// shared memory) or allocated manually below.
36-
let memory_id = module.memories.push(*memory_ty.wasmtime_memory());
36+
let memory_id = module.memories.push(*memory_ty.wasmtime_memory())?;
3737

3838
// Since we have only associated a single memory with the "frankenstein"
3939
// instance, it will be exported at index 0.

crates/wasmtime/src/runtime/trampoline/table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub async fn create_table(
2121
wasmtime_table.ref_type
2222
);
2323

24-
let table_id = module.tables.push(wasmtime_table);
24+
let table_id = module.tables.push(wasmtime_table)?;
2525

2626
// TODO: can this `exports.insert` get removed?
2727
let name = module.strings.insert("")?;

crates/wasmtime/src/runtime/trampoline/tag.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub fn create_tag(store: &mut StoreOpaque, ty: &TagType) -> Result<InstanceId> {
2626
let tag_id = module.tags.push(Tag {
2727
signature: EngineOrModuleTypeIndex::Engine(func_ty.index()),
2828
exception: EngineOrModuleTypeIndex::Engine(exn_ty.index()),
29-
});
29+
})?;
3030

3131
let name = module.strings.insert("")?;
3232
module.exports.insert(name, EntityIndex::Tag(tag_id))?;

0 commit comments

Comments
 (0)