Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ Released YYYY-MM-DD.
* With `generate_dwarf`, `DW_AT_macros`, `DW_AT_macro_info` and
`DW_AT_GNU_macros` are removed. walrus never emitted the macro sections
they point into, because gimli can't write them.
* With the `parallel` feature, parsing finds the block types of legacy `catch`
handlers in parallel. This scan decodes every function body. It runs for
any module with a tag that has params, such as an Emscripten module built
with C++ exceptions, or with a function type that has two or more results.

### Deprecated

Expand Down
51 changes: 33 additions & 18 deletions src/module/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,12 @@ impl Module {
// necessary and it's a bottleneck!
let mut bodies = Vec::with_capacity(functions.len());

// `register_legacy_catch_block_types` only registers a `Type` for a
// `catch` whose tag has params, or for a handler of a `try` with two
// or more results, which only a function type in the type section
// can give it. When the module has neither, or legacy exceptions are
// disabled so validation rejects any `try`, it would register
// nothing, so don't decode every body for it.
// `legacy_catch_block_types` only finds a `Type` for a `catch` whose
// tag has params, or for a handler of a `try` with two or more
// results, which needs a function type from the type section. When
// the module has neither, or legacy exceptions are disabled so
// validation rejects any `try`, it would find nothing, so don't
// decode every body for it.
let has_legacy_catch_block_types = !self.config.skip_legacy_exceptions
&& (self
.tags
Expand All @@ -369,6 +369,20 @@ impl Module {
.iter()
.filter_map(|ty| ty.as_function())
.any(|ty| ty.results().len() >= 2));
// Decoding every body is costly, so with the `parallel` feature find
// the types in parallel. The loop below adds each function's types
// after its entry block type, in function order.
let legacy_catch_block_types = if has_legacy_catch_block_types {
let indices = &*indices;
#[cfg(feature = "parallel")]
let iter = functions.par_iter();
#[cfg(not(feature = "parallel"))]
let iter = functions.iter();
iter.map(|(body, _)| self.legacy_catch_block_types(body, indices))
.collect::<Result<Vec<_>>>()?
} else {
Vec::new()
};

for (i, (body, mut validator)) in functions.into_iter().enumerate() {
let index = (num_imports + i) as u32;
Expand Down Expand Up @@ -400,8 +414,8 @@ impl Module {
self.types.add_entry_ty(&results);

// Likewise for legacy `catch` handler block types.
if has_legacy_catch_block_types {
self.register_legacy_catch_block_types(&body, indices)?;
for (params, results) in legacy_catch_block_types.get(i).into_iter().flatten() {
self.types.add(params, results);
}

// Next up comes all the locals of the function.
Expand Down Expand Up @@ -475,24 +489,26 @@ impl Module {
Ok(())
}

/// Pre-register the implicit `[tag params] -> [try results]` `Type` of
/// every non-`Simple` legacy `catch` / `catch_all` handler in `body`.
/// The implicit `[tag params] -> [try results]` `Type` of every
/// non-`Simple` legacy `catch` / `catch_all` handler in `body`.
///
/// Legacy `catch` handler signatures are implicit in the encoding and
/// so never in `ModuleTypes`. The IR builder runs in parallel over
/// `&Module` and cannot register them itself, so — like `add_entry_ty`
/// for the function entry block — they're pre-registered here.
fn register_legacy_catch_block_types(
&mut self,
/// for the function entry block — `parse_local_functions` registers
/// them before parsing the bodies.
fn legacy_catch_block_types(
&self,
body: &wasmparser::FunctionBody,
indices: &IndicesToIds,
) -> Result<()> {
) -> Result<Vec<(Vec<ValType>, Vec<ValType>)>> {
use wasmparser::{BlockType, Operator};

// One entry per open structured frame; `Some` holds a `try`'s
// result types. A `catch` pairs with the innermost `try`, i.e.
// the top frame.
let mut frames: Vec<Option<Vec<ValType>>> = Vec::new();
let mut types = Vec::new();
for op in body.get_operators_reader()? {
match op? {
Operator::Block { .. }
Expand All @@ -515,23 +531,22 @@ impl Module {
let params = self.types.params(tag_ty).to_vec();
// `Simple` handlers (0 params, <=1 result) need no `Type`.
if !matches!((params.len(), results.len()), (0, 0) | (0, 1)) {
self.types.add(&params, &results);
types.push((params, results));
}
}
}
Operator::CatchAll => {
if let Some(Some(results)) = frames.last() {
// Handler is `[] -> results`; multi-value iff >1 result.
if results.len() > 1 {
let results = results.clone();
self.types.add(&[], &results);
types.push((Vec::new(), results.clone()));
}
}
}
_ => {}
}
}
Ok(())
Ok(types)
}

/// Retrieve the ID for the first exported memory.
Expand Down
Loading