Skip to content

Commit c782855

Browse files
authored
Use our OOM-handling Vec in wasmtime_environ::Module (#12584)
1 parent b298f37 commit c782855

3 files changed

Lines changed: 55 additions & 43 deletions

File tree

crates/environ/src/compile/module_environ.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
ModuleInternedTypeIndex, ModuleTypesBuilder, PanicOnOom as _, PrimaryMap, SizeOverflow,
1111
StaticMemoryInitializer, StaticModuleIndex, TableIndex, TableInitialValue, Tag, TagIndex,
1212
Tunables, TypeConvert, TypeIndex, WasmError, WasmHeapTopType, WasmHeapType, WasmResult,
13-
WasmValType, WasmparserTypeConverter,
13+
WasmValType, WasmparserTypeConverter, collections,
1414
};
1515
use cranelift_entity::SecondaryMap;
1616
use cranelift_entity::packed_option::ReservedValue;
@@ -332,7 +332,7 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> {
332332
self.validator.import_section(&imports)?;
333333

334334
let cnt = usize::try_from(imports.count()).unwrap();
335-
self.result.module.initializers.reserve(cnt);
335+
self.result.module.initializers.reserve(cnt)?;
336336

337337
for entry in imports.into_imports() {
338338
let import = entry?;
@@ -403,7 +403,7 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> {
403403
self.result.module.tables.push(table);
404404
let init = match init {
405405
wasmparser::TableInit::RefNull => TableInitialValue::Null {
406-
precomputed: Vec::new(),
406+
precomputed: collections::Vec::new(),
407407
},
408408
wasmparser::TableInit::Expr(expr) => {
409409
let (init, escaped) = ConstExpr::from_wasmparser(self, expr)?;
@@ -547,21 +547,19 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> {
547547
let (offset, escaped) = ConstExpr::from_wasmparser(self, offset_expr)?;
548548
debug_assert!(escaped.is_empty());
549549

550-
self.result
551-
.module
552-
.table_initialization
553-
.segments
554-
.push(TableSegment {
550+
self.result.module.table_initialization.segments.push(
551+
TableSegment {
555552
table_index,
556553
offset,
557554
elements,
558-
});
555+
},
556+
)?;
559557
}
560558

561559
ElementKind::Passive => {
562560
let elem_index = ElemIndex::from_u32(index as u32);
563561
let index = self.result.module.passive_elements.len();
564-
self.result.module.passive_elements.push(elements);
562+
self.result.module.passive_elements.push(elements)?;
565563
self.result
566564
.module
567565
.passive_elements_map
@@ -627,7 +625,7 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> {
627625
};
628626

629627
let cnt = usize::try_from(data.count()).unwrap();
630-
initializers.reserve_exact(cnt);
628+
initializers.reserve_exact(cnt)?;
631629
self.result.data.reserve_exact(cnt);
632630

633631
for (index, entry) in data.into_iter().enumerate() {
@@ -670,7 +668,7 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> {
670668
memory_index,
671669
offset,
672670
data: range,
673-
});
671+
})?;
674672
self.result.data.push(data.into());
675673
}
676674
DataKind::Passive => {
@@ -821,7 +819,7 @@ and for re-adding support for interface types you can see this issue:
821819
name: self.result.module.strings.insert(module)?,
822820
field: self.result.module.strings.insert(field)?,
823821
index,
824-
});
822+
})?;
825823
Ok(())
826824
}
827825

@@ -1224,7 +1222,7 @@ impl ModuleTranslation<'_> {
12241222
if let TableInitialValue::Expr(expr) = init {
12251223
if let [ConstOp::RefFunc(f)] = expr.ops() {
12261224
*init = TableInitialValue::Null {
1227-
precomputed: vec![*f; table_size as usize],
1225+
precomputed: collections::vec![*f; table_size as usize].panic_on_oom(),
12281226
};
12291227
}
12301228
}
@@ -1318,14 +1316,16 @@ impl ModuleTranslation<'_> {
13181316
// it's large enough to hold the segment and then copying the
13191317
// segment into the precomputed list.
13201318
if precomputed.len() < top as usize {
1321-
precomputed.resize(top as usize, FuncIndex::reserved_value());
1319+
precomputed
1320+
.resize(top as usize, FuncIndex::reserved_value())
1321+
.panic_on_oom();
13221322
}
13231323
let dst = &mut precomputed[offset as usize..top as usize];
13241324
dst.copy_from_slice(&function_elements);
13251325

13261326
// advance the iterator to see the next segment
13271327
let _ = segments.next();
13281328
}
1329-
self.module.table_initialization.segments = segments.collect();
1329+
self.module.table_initialization.segments = segments.try_collect().panic_on_oom();
13301330
}
13311331
}

crates/environ/src/module.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub enum MemoryInitialization {
4848
/// data segments when the module is instantiated.
4949
///
5050
/// This is the default memory initialization type.
51-
Segmented(Vec<MemoryInitializer>),
51+
Segmented(collections::Vec<MemoryInitializer>),
5252

5353
/// Memory initialization is statically known and involves a single `memcpy`
5454
/// or otherwise simply making the defined data visible.
@@ -88,7 +88,7 @@ pub enum MemoryInitialization {
8888

8989
impl Default for MemoryInitialization {
9090
fn default() -> Self {
91-
Self::Segmented(Vec::new())
91+
Self::Segmented(collections::Vec::new())
9292
}
9393
}
9494

@@ -233,7 +233,7 @@ pub struct TableInitialization {
233233
///
234234
/// These element segments are iterated over during instantiation to apply
235235
/// any segments that weren't already moved into `initial_values` above.
236-
pub segments: Vec<TableSegment>,
236+
pub segments: collections::Vec<TableSegment>,
237237
}
238238

239239
/// Initial value for all elements in a table.
@@ -249,7 +249,7 @@ pub enum TableInitialValue {
249249
/// `FuncIndex::reserved_value()`. Note that this image is empty by
250250
/// default and may not encompass the entire span of the table in which
251251
/// case the elements are initialized to null.
252-
precomputed: Vec<FuncIndex>,
252+
precomputed: collections::Vec<FuncIndex>,
253253
},
254254
/// An arbitrary const expression.
255255
Expr(ConstExpr),
@@ -301,7 +301,7 @@ pub struct Module {
301301
pub name: Option<Atom>,
302302

303303
/// All import records, in the order they are declared in the module.
304-
pub initializers: Vec<Initializer>,
304+
pub initializers: collections::Vec<Initializer>,
305305

306306
/// Exported entities.
307307
pub exports: collections::IndexMap<Atom, EntityIndex>,
@@ -316,7 +316,7 @@ pub struct Module {
316316
pub memory_initialization: MemoryInitialization,
317317

318318
/// WebAssembly passive elements.
319-
pub passive_elements: Vec<TableSegmentElements>,
319+
pub passive_elements: collections::Vec<TableSegmentElements>,
320320

321321
/// The map from passive element index (element segment index space) to index in `passive_elements`.
322322
pub passive_elements_map: BTreeMap<ElemIndex, usize>,

crates/environ/src/types.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::{Tunables, WasmResult, error::OutOfMemory, wasm_unsupported};
22
use alloc::borrow::Cow;
33
use alloc::boxed::Box;
44
use core::{fmt, ops::Range};
5-
use cranelift_entity::entity_impl;
65
use serde_derive::{Deserialize, Serialize};
76
use smallvec::SmallVec;
87
use wasmtime_core::alloc::{TryClone, TryCollect as _};
@@ -1536,67 +1535,80 @@ impl TypeTrace for WasmRecGroup {
15361535
}
15371536
}
15381537

1538+
macro_rules! entity_impl_with_try_clone {
1539+
( $ty:ident ) => {
1540+
cranelift_entity::entity_impl!($ty);
1541+
1542+
impl TryClone for $ty {
1543+
#[inline]
1544+
fn try_clone(&self) -> Result<Self, $crate::error::OutOfMemory> {
1545+
Ok(*self)
1546+
}
1547+
}
1548+
};
1549+
}
1550+
15391551
/// Index type of a function (imported or defined) inside the WebAssembly module.
15401552
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
15411553
pub struct FuncIndex(u32);
1542-
entity_impl!(FuncIndex);
1554+
entity_impl_with_try_clone!(FuncIndex);
15431555

15441556
/// Index type of a defined function inside the WebAssembly module.
15451557
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
15461558
pub struct DefinedFuncIndex(u32);
1547-
entity_impl!(DefinedFuncIndex);
1559+
entity_impl_with_try_clone!(DefinedFuncIndex);
15481560

15491561
/// Index type of a defined table inside the WebAssembly module.
15501562
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
15511563
pub struct DefinedTableIndex(u32);
1552-
entity_impl!(DefinedTableIndex);
1564+
entity_impl_with_try_clone!(DefinedTableIndex);
15531565

15541566
/// Index type of a defined memory inside the WebAssembly module.
15551567
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
15561568
pub struct DefinedMemoryIndex(u32);
1557-
entity_impl!(DefinedMemoryIndex);
1569+
entity_impl_with_try_clone!(DefinedMemoryIndex);
15581570

15591571
/// Index type of a defined memory inside the WebAssembly module.
15601572
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
15611573
pub struct OwnedMemoryIndex(u32);
1562-
entity_impl!(OwnedMemoryIndex);
1574+
entity_impl_with_try_clone!(OwnedMemoryIndex);
15631575

15641576
/// Index type of a defined global inside the WebAssembly module.
15651577
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
15661578
pub struct DefinedGlobalIndex(u32);
1567-
entity_impl!(DefinedGlobalIndex);
1579+
entity_impl_with_try_clone!(DefinedGlobalIndex);
15681580

15691581
/// Index type of a table (imported or defined) inside the WebAssembly module.
15701582
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
15711583
pub struct TableIndex(u32);
1572-
entity_impl!(TableIndex);
1584+
entity_impl_with_try_clone!(TableIndex);
15731585

15741586
/// Index type of a global variable (imported or defined) inside the WebAssembly module.
15751587
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
15761588
pub struct GlobalIndex(u32);
1577-
entity_impl!(GlobalIndex);
1589+
entity_impl_with_try_clone!(GlobalIndex);
15781590

15791591
/// Index type of a linear memory (imported or defined) inside the WebAssembly module.
15801592
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
15811593
pub struct MemoryIndex(u32);
1582-
entity_impl!(MemoryIndex);
1594+
entity_impl_with_try_clone!(MemoryIndex);
15831595

15841596
/// Index type of a canonicalized recursive type group inside a WebAssembly
15851597
/// module (as opposed to canonicalized within the whole engine).
15861598
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
15871599
pub struct ModuleInternedRecGroupIndex(u32);
1588-
entity_impl!(ModuleInternedRecGroupIndex);
1600+
entity_impl_with_try_clone!(ModuleInternedRecGroupIndex);
15891601

15901602
/// Index type of a canonicalized recursive type group inside the whole engine
15911603
/// (as opposed to canonicalized within just a single Wasm module).
15921604
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
15931605
pub struct EngineInternedRecGroupIndex(u32);
1594-
entity_impl!(EngineInternedRecGroupIndex);
1606+
entity_impl_with_try_clone!(EngineInternedRecGroupIndex);
15951607

15961608
/// Index type of a type (imported or defined) inside the WebAssembly module.
15971609
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
15981610
pub struct TypeIndex(u32);
1599-
entity_impl!(TypeIndex);
1611+
entity_impl_with_try_clone!(TypeIndex);
16001612

16011613
/// A canonicalized type index referencing a type within a single recursion
16021614
/// group from another type within that same recursion group.
@@ -1605,7 +1617,7 @@ entity_impl!(TypeIndex);
16051617
/// groups.
16061618
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
16071619
pub struct RecGroupRelativeTypeIndex(u32);
1608-
entity_impl!(RecGroupRelativeTypeIndex);
1620+
entity_impl_with_try_clone!(RecGroupRelativeTypeIndex);
16091621

16101622
/// A canonicalized type index for a type within a single WebAssembly module.
16111623
///
@@ -1616,7 +1628,7 @@ entity_impl!(RecGroupRelativeTypeIndex);
16161628
/// involve entities defined in different modules).
16171629
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
16181630
pub struct ModuleInternedTypeIndex(u32);
1619-
entity_impl!(ModuleInternedTypeIndex);
1631+
entity_impl_with_try_clone!(ModuleInternedTypeIndex);
16201632

16211633
/// A canonicalized type index into an engine's shared type registry.
16221634
///
@@ -1628,7 +1640,7 @@ entity_impl!(ModuleInternedTypeIndex);
16281640
#[repr(transparent)] // Used directly by JIT code.
16291641
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
16301642
pub struct VMSharedTypeIndex(u32);
1631-
entity_impl!(VMSharedTypeIndex);
1643+
entity_impl_with_try_clone!(VMSharedTypeIndex);
16321644

16331645
impl VMSharedTypeIndex {
16341646
/// Create a new `VMSharedTypeIndex`.
@@ -1659,30 +1671,30 @@ impl Default for VMSharedTypeIndex {
16591671
/// Index type of a passive data segment inside the WebAssembly module.
16601672
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
16611673
pub struct DataIndex(u32);
1662-
entity_impl!(DataIndex);
1674+
entity_impl_with_try_clone!(DataIndex);
16631675

16641676
/// Index type of a passive element segment inside the WebAssembly module.
16651677
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
16661678
pub struct ElemIndex(u32);
1667-
entity_impl!(ElemIndex);
1679+
entity_impl_with_try_clone!(ElemIndex);
16681680

16691681
/// Index type of a defined tag inside the WebAssembly module.
16701682
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
16711683
pub struct DefinedTagIndex(u32);
1672-
entity_impl!(DefinedTagIndex);
1684+
entity_impl_with_try_clone!(DefinedTagIndex);
16731685

16741686
/// Index type of an event inside the WebAssembly module.
16751687
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
16761688
pub struct TagIndex(u32);
1677-
entity_impl!(TagIndex);
1689+
entity_impl_with_try_clone!(TagIndex);
16781690

16791691
/// Index into the global list of modules found within an entire component.
16801692
///
16811693
/// Module translations are saved on the side to get fully compiled after
16821694
/// the original component has finished being translated.
16831695
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
16841696
pub struct StaticModuleIndex(u32);
1685-
entity_impl!(StaticModuleIndex);
1697+
entity_impl_with_try_clone!(StaticModuleIndex);
16861698

16871699
/// An index of an entity.
16881700
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]

0 commit comments

Comments
 (0)