Skip to content
Merged
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
7 changes: 6 additions & 1 deletion crates/wasmparser/src/module_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* limitations under the License.
*/

use crate::{FuncType, GlobalType, MemoryType, TableType, Type};
use crate::{EventType, FuncType, GlobalType, MemoryType, TableType, Type};
use std::ops::Range;

/// Types that qualify as Wasm function types for validation purposes.
Expand Down Expand Up @@ -202,6 +202,8 @@ pub trait WasmModuleResources {
fn table_at(&self, at: u32) -> Option<TableType>;
/// Returns the linear memory at given index.
fn memory_at(&self, at: u32) -> Option<MemoryType>;
/// Returns the event at given index.
fn event_at(&self, at: u32) -> Option<EventType>;
/// Returns the global variable at given index.
fn global_at(&self, at: u32) -> Option<GlobalType>;
/// Returns the `FuncType` associated with the given type index.
Expand Down Expand Up @@ -232,6 +234,9 @@ where
fn memory_at(&self, at: u32) -> Option<MemoryType> {
T::memory_at(self, at)
}
fn event_at(&self, at: u32) -> Option<EventType> {
T::event_at(self, at)
}
fn global_at(&self, at: u32) -> Option<GlobalType> {
T::global_at(self, at)
}
Expand Down
16 changes: 13 additions & 3 deletions crates/wasmparser/src/operators_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
// the various methods here.

use crate::limits::MAX_WASM_FUNCTION_LOCALS;
use crate::primitives::{MemoryImmediate, Operator, SIMDLaneIndex, Type, TypeOrFuncType};
use crate::primitives::{
EventType, MemoryImmediate, Operator, SIMDLaneIndex, Type, TypeOrFuncType,
};
use crate::{BinaryReaderError, Result, WasmFeatures, WasmFuncType, WasmModuleResources};

/// A wrapper around a `BinaryReaderError` where the inner error's offset is a
Expand Down Expand Up @@ -598,7 +600,8 @@ impl OperatorValidator {
Operator::Throw { index } => {
self.check_exceptions_enabled()?;
// Check values associated with the exception.
let ty = func_type_at(&resources, index)?;
let event_ty = event_at(&resources, index)?;
let ty = func_type_at(&resources, event_ty.type_index)?;
for ty in ty.inputs().rev() {
self.pop_operand(Some(ty))?;
}
Expand All @@ -620,7 +623,8 @@ impl OperatorValidator {
let (ty, kind) = self.jump(relative_depth)?;
self.pop_operand(Some(Type::ExnRef))?;
// Check the exception's argument values with target block's.
let exn_args = func_type_at(&resources, index)?;
let event_ty = event_at(&resources, index)?;
let exn_args = func_type_at(&resources, event_ty.type_index)?;
if Iterator::ne(exn_args.inputs(), label_types(ty, resources, kind)?) {
bail_op_err!("target block types do not match");
}
Expand Down Expand Up @@ -1842,6 +1846,12 @@ fn func_type_at<T: WasmModuleResources>(
.ok_or_else(|| OperatorValidatorError::new("unknown type: type index out of bounds"))
}

fn event_at<T: WasmModuleResources>(resources: &T, at: u32) -> OperatorValidatorResult<EventType> {
resources
.event_at(at)
.ok_or_else(|| OperatorValidatorError::new("unknown event: event index out of bounds"))
}

enum Either<A, B> {
A(A),
B(B),
Expand Down
4 changes: 4 additions & 0 deletions crates/wasmparser/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1838,6 +1838,10 @@ impl WasmModuleResources for ValidatorResources {
self.0.get_memory(self.0.def(at)).copied()
}

fn event_at(&self, at: u32) -> Option<EventType> {
self.0.get_event(self.0.def(at)).copied()
}

fn global_at(&self, at: u32) -> Option<GlobalType> {
self.0.get_global(self.0.def(at)).map(|t| t.item)
}
Expand Down
3 changes: 3 additions & 0 deletions crates/wasmparser/src/validator/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ mod tests {
fn memory_at(&self, _at: u32) -> Option<crate::MemoryType> {
todo!()
}
fn event_at(&self, _at: u32) -> Option<crate::EventType> {
todo!()
}
fn global_at(&self, _at: u32) -> Option<crate::GlobalType> {
todo!()
}
Expand Down
14 changes: 14 additions & 0 deletions tests/local/exception-handling.wast
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,17 @@
end
)
)

(assert_invalid
(module
(type (func))
(func throw 0))
"unknown event: event index out of bounds")

(assert_invalid
(module
(type (func))
(func (param exnref)
local.get 0
br_on_exn 0 0))
"unknown event: event index out of bounds")