use std::fs::File;
use wadec::core::SectionKind;
use wadec::decode_errors::*;
use wadec::decode_module;
#[test]
fn type_idx_error_for_overlong_function_index() {
let wasm =
File::open("tests/fixtures/malformed/function_section_typeidx_overlong.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong type index should fail to decode");
match err {
DecodeModuleError::DecodeFunctionSection(DecodeFunctionSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source: DecodeTypeIdxError(DecodeU32Error::RepresentationTooLong),
},
)) => {
assert_eq!(position, 0);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_u32_error_io_for_section_size() {
let wasm = File::open("tests/fixtures/malformed/section_header_size_truncated.wasm").unwrap();
let err = decode_module(wasm).expect_err("truncated section size should fail to decode");
match err {
DecodeModuleError::DecodeSectionHeader(DecodeSectionHeaderError::DecodeSectionSize(
u32_err,
)) => match u32_err {
DecodeU32Error::Io(io_err) => {
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("expected Io error, got {other:?}"),
},
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_i32_error_too_large() {
let wasm = File::open("tests/fixtures/malformed/i32_const_too_large.wasm").unwrap();
let err = decode_module(wasm).expect_err("too-large i32 const should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Numeric(NumericError::DecodeI32(i32_err)),
)),
},
)) => {
assert_eq!(position, 0);
assert!(matches!(i32_err, DecodeI32Error::TooLarge));
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_i32_error_representation_too_long() {
let wasm =
File::open("tests/fixtures/malformed/i32_const_representation_too_long.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong i32 const should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Numeric(NumericError::DecodeI32(i32_err)),
)),
},
)) => {
assert_eq!(position, 0);
assert!(matches!(i32_err, DecodeI32Error::RepresentationTooLong));
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_i32_error_io() {
let wasm = File::open("tests/fixtures/malformed/i32_const_truncated.wasm").unwrap();
let err = decode_module(wasm).expect_err("truncated i32 const should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Numeric(NumericError::DecodeI32(i32_err)),
)),
},
)) => {
assert_eq!(position, 0);
match i32_err {
DecodeI32Error::Io(io_err) => {
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("expected Io error, got {other:?}"),
}
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_i64_error_representation_too_long() {
let wasm =
File::open("tests/fixtures/malformed/i64_const_representation_too_long.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong i64 const should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Numeric(NumericError::DecodeI64(i64_err)),
)),
},
)) => {
assert_eq!(position, 0);
assert!(matches!(i64_err, DecodeI64Error::RepresentationTooLong));
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_i64_error_incorrect_sign_extension() {
let wasm =
File::open("tests/fixtures/malformed/i64_const_incorrect_sign_extension.wasm").unwrap();
let err = decode_module(wasm).expect_err("bad i64 sign extension should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Numeric(NumericError::DecodeI64(i64_err)),
)),
},
)) => {
assert_eq!(position, 0);
assert!(matches!(i64_err, DecodeI64Error::IncorrectSignExtension));
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_i64_error_io() {
let wasm = File::open("tests/fixtures/malformed/i64_const_truncated.wasm").unwrap();
let err = decode_module(wasm).expect_err("truncated i64 const should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Numeric(NumericError::DecodeI64(i64_err)),
)),
},
)) => {
assert_eq!(position, 0);
match i64_err {
DecodeI64Error::Io(io_err) => {
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("expected Io error, got {other:?}"),
}
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn parse_error_read_opcode() {
let wasm = File::open("tests/fixtures/malformed/code_body_missing_opcode.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing opcode should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::ReadOpcode(io_err),
)),
..
},
)) => {
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn parse_error_control() {
let wasm = File::open("tests/fixtures/malformed/br_labelidx_overlong.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid label index should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Control(ControlError::LabelIdx(DecodeLabelIdxError(
DecodeU32Error::RepresentationTooLong,
))),
)),
..
},
)) => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn parse_error_reference() {
let wasm = File::open("tests/fixtures/malformed/ref_null_invalid_marker.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid ref type marker should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Reference(ReferenceError::RefType(
DecodeRefTypeError::InvalidMarkerByte(err),
)),
)),
..
},
)) => assert_eq!(err.0, 0x00),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn parse_error_parametric() {
let wasm = File::open("tests/fixtures/malformed/select_invalid_valtype.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid valtype in select should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Parametric(ParametricError::DecodeVector(
DecodeVectorError::ParseElement {
position: vec_pos,
source: DecodeValTypeError::InvalidMarkerByte(err),
},
)),
)),
..
},
)) => {
assert_eq!(vec_pos, 0);
assert_eq!(err.0, 0xFF);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn parse_error_variable() {
let wasm = File::open("tests/fixtures/malformed/global_set_truncated.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing global index should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Variable(VariableError::GlobalIdx(DecodeGlobalIdxError(
DecodeU32Error::Io(io_err),
))),
)),
..
},
)) => assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn parse_error_table() {
let wasm = File::open("tests/fixtures/malformed/table_get_tableidx_overlong.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid table index should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Table(TableError::TableIdx(DecodeTableIdxError(
DecodeU32Error::RepresentationTooLong,
))),
)),
..
},
)) => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn parse_error_memory() {
let wasm = File::open("tests/fixtures/malformed/memory_size_reserved_nonzero.wasm").unwrap();
let err = decode_module(wasm).expect_err("non-zero reserved byte should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Memory(MemoryError::InvalidMemorySizeByte(b)),
)),
..
},
)) => assert_eq!(b, 0x01),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn parse_error_numeric() {
let wasm = File::open("tests/fixtures/malformed/f32_const_truncated.wasm").unwrap();
let err = decode_module(wasm).expect_err("truncated f32 const should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Numeric(NumericError::DecodeF32(
DecodeFloat32Error::ReadPayload(io_err),
)),
)),
..
},
)) => assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn parse_error_vector() {
let wasm = File::open("tests/fixtures/malformed/vector_invalid_opcode.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid vector opcode should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Vector(VectorError::InvalidOpcode(op)),
)),
..
},
)) => assert_eq!(op, 65535),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn parse_error_invalid_opcode() {
let wasm = File::open("tests/fixtures/malformed/invalid_opcode_ff.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid opcode should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::InvalidOpcode(0xFF),
)),
..
},
)) => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn parse_error_invalid_marker_after_fc() {
let wasm = File::open("tests/fixtures/malformed/fc_invalid_marker.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid FC marker should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::InvalidMarkerByteAfterFC(op),
)),
..
},
)) => {
assert_eq!(op, 0xFF);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn control_error_decode_label_idx_vector() {
let wasm =
File::open("tests/fixtures/malformed/br_table_labelidxvector_overlong.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong label index should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Control(ControlError::DecodeLabelIdxVector(
DecodeVectorError::ParseElement {
position: vec_pos,
source: DecodeLabelIdxError(DecodeU32Error::RepresentationTooLong),
},
)),
)),
..
},
)) => {
assert_eq!(vec_pos, 0);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn control_error_table_idx() {
let wasm = File::open("tests/fixtures/malformed/call_indirect_tableidx_overlong.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong table index should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Control(ControlError::TableIdx(DecodeTableIdxError(
DecodeU32Error::RepresentationTooLong,
))),
)),
..
},
)) => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn control_error_type_idx() {
let wasm = File::open("tests/fixtures/malformed/call_indirect_typeidx_overlong.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong type index should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Control(ControlError::TypeIdx(DecodeTypeIdxError(
DecodeU32Error::RepresentationTooLong,
))),
)),
..
},
)) => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn control_error_block_type() {
let wasm = File::open("tests/fixtures/malformed/block_missing_blocktype.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing block type should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Control(ControlError::BlockType(
BlockTypeError::ReadMarkerByte(io_err),
)),
)),
..
},
)) => assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn control_error_unexpected_else() {
let wasm = File::open("tests/fixtures/malformed/if_unexpected_else.wasm").unwrap();
let err = decode_module(wasm).expect_err("double else should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Control(ControlError::UnexpectedElse),
)),
..
},
)) => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn reference_error_func_idx() {
let wasm = File::open("tests/fixtures/malformed/ref_func_overlong.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong ref.func index should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Reference(ReferenceError::FuncIdx(DecodeFuncIdxError(
DecodeU32Error::RepresentationTooLong,
))),
)),
..
},
)) => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn func_idx_error_for_overlong_call_instruction() {
let wasm = File::open("tests/fixtures/malformed/function_call_funcidx_overlong.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong func idx should fail to decode");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Control(ControlError::FuncIdx(DecodeFuncIdxError(
DecodeU32Error::RepresentationTooLong,
))),
)),
},
)) => {
assert_eq!(position, 0);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn table_idx_error_for_overlong_table_get_instruction() {
let wasm = File::open("tests/fixtures/malformed/table_get_tableidx_overlong.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong table idx should fail to decode");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Table(TableError::TableIdx(DecodeTableIdxError(
DecodeU32Error::RepresentationTooLong,
))),
)),
},
)) => {
assert_eq!(position, 0);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn mem_idx_error_for_overlong_data_segment_memory_index() {
let wasm = File::open("tests/fixtures/malformed/data_section_memidx_overlong.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong mem idx should fail to decode");
match err {
DecodeModuleError::DecodeDataSection(DecodeDataSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeDataSegmentError::DecodeMemIdx(DecodeMemIdxError(
DecodeU32Error::RepresentationTooLong,
)),
},
)) => {
assert_eq!(position, 0);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn global_idx_error_for_overlong_global_get_instruction() {
let wasm = File::open("tests/fixtures/malformed/global_get_globalidx_overlong.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong global idx should fail to decode");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Variable(VariableError::GlobalIdx(DecodeGlobalIdxError(
DecodeU32Error::RepresentationTooLong,
))),
)),
},
)) => {
assert_eq!(position, 0);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn elem_idx_error_for_overlong_elem_drop_instruction() {
let wasm = File::open("tests/fixtures/malformed/elem_drop_elemidx_overlong.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong elem idx should fail to decode");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Table(TableError::ElemIdx(DecodeElemIdxError(
DecodeU32Error::RepresentationTooLong,
))),
)),
},
)) => {
assert_eq!(position, 0);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn data_idx_error_for_overlong_data_drop_instruction() {
let wasm = File::open("tests/fixtures/malformed/data_drop_dataidx_overlong.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong data idx should fail to decode");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Memory(MemoryError::DecodeDataIdx(DecodeDataIdxError(
DecodeU32Error::RepresentationTooLong,
))),
)),
},
)) => {
assert_eq!(position, 0);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn local_idx_error_for_overlong_local_get_instruction() {
let wasm = File::open("tests/fixtures/malformed/local_get_localidx_overlong.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong local idx should fail to decode");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Variable(VariableError::LocalIdx(DecodeLocalIdxError(
DecodeU32Error::RepresentationTooLong,
))),
)),
},
)) => {
assert_eq!(position, 0);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn label_idx_error_for_overlong_br_instruction() {
let wasm = File::open("tests/fixtures/malformed/br_labelidx_overlong.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong label idx should fail to decode");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Control(ControlError::LabelIdx(DecodeLabelIdxError(
DecodeU32Error::RepresentationTooLong,
))),
)),
},
)) => {
assert_eq!(position, 0);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_u32_error_too_large_for_type_section_length() {
let wasm = File::open("tests/fixtures/malformed/type_section_length_too_large.wasm").unwrap();
let err = decode_module(wasm).expect_err("too-large vector length should fail to decode");
match err {
DecodeModuleError::DecodeTypeSection(DecodeTypeSectionError::DecodeVector(
DecodeVectorError::DecodeLength(u32_err),
)) => {
assert!(
matches!(u32_err, DecodeU32Error::TooLarge),
"expected TooLarge variant, got {u32_err:?}"
);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_u32_error_representation_too_long_for_section_size() {
let wasm =
File::open("tests/fixtures/malformed/section_size_representation_too_long.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong section size should fail to decode");
match err {
DecodeModuleError::DecodeSectionHeader(DecodeSectionHeaderError::DecodeSectionSize(
u32_err,
)) => assert!(
matches!(u32_err, DecodeU32Error::RepresentationTooLong),
"expected RepresentationTooLong variant, got {u32_err:?}"
),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn parametric_error_decode_vector_invalid_valtype() {
let wasm =
File::open("tests/fixtures/malformed/select_type_vector_invalid_valtype.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid type vector in select should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Parametric(ParametricError::DecodeVector(
DecodeVectorError::ParseElement {
position: vec_pos,
source: DecodeValTypeError::InvalidMarkerByte(err),
},
)),
)),
},
)) => {
assert_eq!(position, 0);
assert_eq!(vec_pos, 0);
assert_eq!(err.0, 0xFF);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn memory_error_decode_memarg_missing_offset() {
let wasm =
File::open("tests/fixtures/malformed/i32_load_memarg_truncated_offset.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing memarg offset should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Memory(MemoryError::DecodeMemarg(memarg_err)),
)),
},
)) => {
assert_eq!(position, 0);
match memarg_err {
MemargError::Offset(DecodeU32Error::Io(io_err)) => {
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("expected offset decoding failure, got {other:?}"),
}
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn memory_error_read_reserved_byte() {
let wasm =
File::open("tests/fixtures/malformed/memory_size_missing_reserved_byte.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing reserved byte should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Memory(MemoryError::ReadReservedByte(io_err)),
)),
},
)) => {
assert_eq!(position, 0);
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn memory_error_read_reserved_bytes() {
let wasm =
File::open("tests/fixtures/malformed/memory_copy_missing_reserved_bytes.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing reserved bytes should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Memory(MemoryError::ReadReservedBytes(io_err)),
)),
},
)) => {
assert_eq!(position, 0);
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn memory_error_invalid_memory_size_byte() {
let wasm = File::open("tests/fixtures/malformed/memory_size_reserved_nonzero.wasm").unwrap();
let err = decode_module(wasm).expect_err("non-zero reserved byte should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Memory(MemoryError::InvalidMemorySizeByte(b)),
)),
},
)) => {
assert_eq!(position, 0);
assert_eq!(b, 0x01);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn memory_error_invalid_memory_grow_byte() {
let wasm = File::open("tests/fixtures/malformed/memory_grow_reserved_nonzero.wasm").unwrap();
let err = decode_module(wasm).expect_err("non-zero reserved grow byte should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Memory(MemoryError::InvalidMemoryGrowByte(b)),
)),
},
)) => {
assert_eq!(position, 0);
assert_eq!(b, 0x01);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn memory_error_invalid_memory_init_byte() {
let wasm = File::open("tests/fixtures/malformed/memory_init_reserved_nonzero.wasm").unwrap();
let err = decode_module(wasm).expect_err("non-zero reserved init byte should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Memory(MemoryError::InvalidMemoryInitByte(b)),
)),
},
)) => {
assert_eq!(position, 0);
assert_eq!(b, 0x01);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn memory_error_invalid_memory_copy_bytes() {
let wasm = File::open("tests/fixtures/malformed/memory_copy_reserved_wrong.wasm").unwrap();
let err = decode_module(wasm).expect_err("non-zero reserved bytes should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Memory(MemoryError::InvalidMemoryCopyBytes(bytes)),
)),
},
)) => {
assert_eq!(position, 0);
assert_eq!(bytes, [0x00, 0x01]);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn memory_error_invalid_memory_fill_byte() {
let wasm = File::open("tests/fixtures/malformed/memory_fill_reserved_nonzero.wasm").unwrap();
let err = decode_module(wasm).expect_err("non-zero reserved fill byte should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Memory(MemoryError::InvalidMemoryFillByte(b)),
)),
},
)) => {
assert_eq!(position, 0);
assert_eq!(b, 0x01);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn numeric_error_read_opcode_truncated_fc_prefix() {
let wasm = File::open("tests/fixtures/malformed/numeric_fc_opcode_truncated.wasm").unwrap();
let err = decode_module(wasm).expect_err("truncated 0xFC opcode should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Numeric(NumericError::ReadOpcode(u32_err)),
)),
..
},
)) => match u32_err {
DecodeU32Error::Io(io_err) => {
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("expected Io error, got {other:?}"),
},
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn numeric_error_decode_f32_truncated_payload() {
let wasm = File::open("tests/fixtures/malformed/f32_const_truncated.wasm").unwrap();
let err = decode_module(wasm).expect_err("truncated f32 const should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Numeric(NumericError::DecodeF32(f32_err)),
)),
..
},
)) => match f32_err {
DecodeFloat32Error::ReadPayload(io_err) => {
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
},
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn numeric_error_decode_f64_truncated_payload() {
let wasm = File::open("tests/fixtures/malformed/numeric_f64_const_truncated.wasm").unwrap();
let err = decode_module(wasm).expect_err("truncated f64 const should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Numeric(NumericError::DecodeF64(f64_err)),
)),
..
},
)) => match f64_err {
DecodeFloat64Error::ReadPayload(io_err) => {
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
},
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn vector_error_read_opcode_truncated() {
let wasm = File::open("tests/fixtures/malformed/vector_opcode_truncated.wasm").unwrap();
let err = decode_module(wasm).expect_err("truncated vector opcode should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Vector(VectorError::ReadOpcode(u32_err)),
)),
..
},
)) => match u32_err {
DecodeU32Error::Io(io_err) => {
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("expected Io error, got {other:?}"),
},
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn vector_error_memarg_offset_missing() {
let wasm = File::open("tests/fixtures/malformed/vector_memarg_offset_truncated.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing memarg offset should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Vector(VectorError::Memarg(MemargError::Offset(
DecodeU32Error::Io(io_err),
))),
)),
..
},
)) => assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn vector_error_laneidx_missing() {
let wasm = File::open("tests/fixtures/malformed/vector_laneidx_missing.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing lane idx should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Vector(VectorError::LaneIdx(LaneIdxError(io_err))),
)),
..
},
)) => {
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn vector_error_read_immediate_bytes() {
let wasm = File::open("tests/fixtures/malformed/vector_immediates_truncated.wasm").unwrap();
let err = decode_module(wasm).expect_err("truncated vector immediate should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Vector(VectorError::ReadImmediateBytes(io_err)),
)),
..
},
)) => assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn vector_error_invalid_opcode() {
let wasm = File::open("tests/fixtures/malformed/vector_invalid_opcode.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid vector opcode should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Vector(VectorError::InvalidOpcode(op)),
)),
..
},
)) => assert_eq!(op, 65535),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn memarg_error_align_missing() {
let wasm = File::open("tests/fixtures/malformed/memarg_align_missing.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing memarg align should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Memory(MemoryError::DecodeMemarg(MemargError::Align(
DecodeU32Error::Io(io_err),
))),
)),
..
},
)) => assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn lane_idx_error_missing_byte() {
let wasm = File::open("tests/fixtures/malformed/vector_laneidx_missing.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing lane idx should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Vector(VectorError::LaneIdx(LaneIdxError(io_err))),
)),
..
},
)) => {
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn block_type_error_read_marker_byte() {
let wasm = File::open("tests/fixtures/malformed/block_missing_blocktype.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing block type should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Control(ControlError::BlockType(
BlockTypeError::ReadMarkerByte(io_err),
)),
)),
..
},
)) => assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn block_type_error_decode_index_truncated() {
let wasm = File::open("tests/fixtures/malformed/blocktype_index_truncated.wasm").unwrap();
let err = decode_module(wasm).expect_err("truncated block type index should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Control(ControlError::BlockType(BlockTypeError::DecodeIndex(
DecodeI64Error::Io(io_err),
))),
)),
..
},
)) => assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn block_type_error_negative_type_index() {
let wasm = File::open("tests/fixtures/malformed/blocktype_negative_typeidx.wasm").unwrap();
let err = decode_module(wasm).expect_err("negative block type index should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Control(ControlError::BlockType(
BlockTypeError::NegativeTypeIndex(n),
)),
)),
..
},
)) => assert!(n < 0),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn block_type_error_type_index_too_large() {
let wasm = File::open("tests/fixtures/malformed/blocktype_index_too_large.wasm").unwrap();
let err = decode_module(wasm).expect_err("too-large block type index should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeFunctionBody(ParseExpressionError::ParseInstruction(
ParseError::Control(ControlError::BlockType(
BlockTypeError::TypeIndexTooLarge(n),
)),
)),
..
},
)) => assert!(n > i64::from(u32::MAX)),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_func_type_error_read_marker_byte() {
let wasm =
File::open("tests/fixtures/malformed/type_section_functype_marker_missing.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing functype marker should fail");
match err {
DecodeModuleError::DecodeTypeSection(DecodeTypeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source: DecodeFuncTypeError::ReadMarkerByte(io_err),
},
)) => {
assert_eq!(position, 0);
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_func_type_error_invalid_marker_byte() {
let wasm =
File::open("tests/fixtures/malformed/type_section_functype_invalid_marker.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid functype marker should fail");
match err {
DecodeModuleError::DecodeTypeSection(DecodeTypeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source: DecodeFuncTypeError::InvalidMarkerByte(b),
},
)) => {
assert_eq!(position, 0);
assert_eq!(b, 0x00);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_func_type_error_parameter_types_invalid_valtype() {
let wasm =
File::open("tests/fixtures/malformed/type_section_param_invalid_valtype.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid parameter valtype should fail");
match err {
DecodeModuleError::DecodeTypeSection(DecodeTypeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeFuncTypeError::DecodeParameterTypes(DecodeResultTypeError::DecodeVector(
DecodeVectorError::ParseElement {
position: inner_pos,
source: DecodeValTypeError::InvalidMarkerByte(err),
},
)),
},
)) => {
assert_eq!(position, 0);
assert_eq!(inner_pos, 0);
assert_eq!(err.0, 0xFF);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_func_type_error_result_types_truncated() {
let wasm =
File::open("tests/fixtures/malformed/type_section_result_valtype_truncated.wasm").unwrap();
let err = decode_module(wasm).expect_err("truncated result valtype should fail");
match err {
DecodeModuleError::DecodeTypeSection(DecodeTypeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeFuncTypeError::DecodeResultTypes(DecodeResultTypeError::DecodeVector(
DecodeVectorError::ParseElement {
position: inner_pos,
source: DecodeValTypeError::Io(io_err),
},
)),
},
)) => {
assert_eq!(position, 0);
assert_eq!(inner_pos, 0);
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_table_error_invalid_reftype_marker() {
let wasm = File::open("tests/fixtures/malformed/table_section_invalid_reftype.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid table reftype should fail");
match err {
DecodeModuleError::DecodeTableSection(DecodeTableSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source: DecodeTableError::DecodeRefType(DecodeRefTypeError::InvalidMarkerByte(err)),
},
)) => {
assert_eq!(position, 0);
assert_eq!(err.0, 0x00);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_table_error_reftype_truncated() {
let wasm = File::open("tests/fixtures/malformed/table_section_reftype_truncated.wasm").unwrap();
let err = decode_module(wasm).expect_err("truncated table reftype should fail");
match err {
DecodeModuleError::DecodeTableSection(DecodeTableSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source: DecodeTableError::DecodeRefType(DecodeRefTypeError::Io(io_err)),
},
)) => {
assert_eq!(position, 0);
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_table_error_limits_invalid_flag() {
let wasm =
File::open("tests/fixtures/malformed/table_section_limits_invalid_flag.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid table limits flag should fail");
match err {
DecodeModuleError::DecodeTableSection(DecodeTableSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeTableError::DecodeLimits(ParseLimitsError::UnexpectedMaxLimitByte(0x02)),
},
)) => assert_eq!(position, 0),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_memory_type_error_missing_limits_byte() {
let wasm = File::open("tests/fixtures/malformed/memory_section_missing_limits.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing limits should fail");
match err {
DecodeModuleError::DecodeMemorySection(DecodeMemorySectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source: DecodeMemoryTypeError(ParseLimitsError::DetermineMaxLimitPresence(io_err)),
},
)) => {
assert_eq!(position, 0);
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_memory_type_error_unexpected_max_limit_byte() {
let wasm =
File::open("tests/fixtures/malformed/memory_section_invalid_limits_flag.wasm").unwrap();
let err = decode_module(wasm).expect_err("unexpected limits flag should fail");
match err {
DecodeModuleError::DecodeMemorySection(DecodeMemorySectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source: DecodeMemoryTypeError(ParseLimitsError::UnexpectedMaxLimitByte(0x02)),
},
)) => {
assert_eq!(position, 0);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_memory_type_error_missing_min_limit() {
let wasm = File::open("tests/fixtures/malformed/memory_section_missing_min.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing min limit should fail");
match err {
DecodeModuleError::DecodeMemorySection(DecodeMemorySectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeMemoryTypeError(ParseLimitsError::ReadMinLimit(DecodeU32Error::Io(io_err))),
},
)) => {
assert_eq!(position, 0);
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_memory_type_error_missing_max_limit() {
let wasm = File::open("tests/fixtures/malformed/memory_section_missing_max.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing max limit should fail");
match err {
DecodeModuleError::DecodeMemorySection(DecodeMemorySectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeMemoryTypeError(ParseLimitsError::ReadMaxLimit(DecodeU32Error::Io(io_err))),
},
)) => {
assert_eq!(position, 0);
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_global_type_error_invalid_valtype() {
let wasm = File::open("tests/fixtures/malformed/global_section_invalid_valtype.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid global valtype should fail");
match err {
DecodeModuleError::DecodeGlobalSection(DecodeGlobalSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeGlobalError::DecodeGlobalType(DecodeGlobalTypeError::DecodeValueType(
DecodeValTypeError::InvalidMarkerByte(err),
)),
},
)) => {
assert_eq!(position, 0);
assert_eq!(err.0, 0xFF);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_global_type_error_missing_mutability() {
let wasm =
File::open("tests/fixtures/malformed/global_section_missing_mutability.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing mutability should fail");
match err {
DecodeModuleError::DecodeGlobalSection(DecodeGlobalSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeGlobalError::DecodeGlobalType(DecodeGlobalTypeError::DecodeMutability(io_err)),
},
)) => {
assert_eq!(position, 0);
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_global_type_error_invalid_mutability() {
let wasm =
File::open("tests/fixtures/malformed/global_section_invalid_mutability.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid mutability should fail");
match err {
DecodeModuleError::DecodeGlobalSection(DecodeGlobalSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeGlobalError::DecodeGlobalType(DecodeGlobalTypeError::InvalidMutability(err)),
},
)) => {
assert_eq!(position, 0);
assert_eq!(err.0, 0x02);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn parse_preamble_error_io() {
let wasm = File::open("tests/fixtures/malformed/preamble_truncated.wasm").unwrap();
let err = decode_module(wasm).expect_err("truncated preamble should fail");
match err {
DecodeModuleError::ParsePreamble(ParsePreambleError::Io(io_err)) => {
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn parse_preamble_error_unexpected() {
let wasm = File::open("tests/fixtures/malformed/preamble_unexpected.wasm").unwrap();
let err = decode_module(wasm).expect_err("wrong preamble should fail");
match err {
DecodeModuleError::ParsePreamble(ParsePreambleError::Unexpected(bytes)) => {
assert_ne!(bytes, *b"\0asm\x01\0\0\0");
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_section_header_error_invalid_section_id() {
let wasm = File::open("tests/fixtures/malformed/invalid_section_id.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid section id should fail");
match err {
DecodeModuleError::DecodeSectionHeader(DecodeSectionHeaderError::InvalidSectionId(err)) => {
assert_eq!(err.0, 0x0D);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_module_section_out_of_order() {
let wasm = File::open("tests/fixtures/malformed/section_out_of_order.wasm").unwrap();
let err = decode_module(wasm).expect_err("out-of-order sections should fail");
match err {
DecodeModuleError::SectionOutOfOrder { current, previous } => {
assert_eq!(current, SectionKind::Type);
assert_eq!(previous, SectionKind::Memory);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_module_duplicate_section() {
let wasm = File::open("tests/fixtures/malformed/duplicate_type_section.wasm").unwrap();
let err = decode_module(wasm).expect_err("duplicate type section should fail");
match err {
DecodeModuleError::DuplicateSection(kind) => assert_eq!(kind, SectionKind::Type),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_module_section_size_mismatch() {
let wasm = File::open("tests/fixtures/malformed/type_section_size_mismatch.wasm").unwrap();
let err = decode_module(wasm).expect_err("section size mismatch should fail");
match err {
DecodeModuleError::SectionSizeMismatch {
section_kind,
declared,
got,
} => {
assert_eq!(section_kind, SectionKind::Type);
assert!(u64::from(declared) > got);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_module_code_func_entries_len_mismatch() {
let wasm = File::open("tests/fixtures/malformed/code_func_len_mismatch.wasm").unwrap();
let err = decode_module(wasm).expect_err("function/code length mismatch should fail");
match err {
DecodeModuleError::CodeFuncEntriesLenMismatch {
codes_len,
funcs_len,
} => {
assert_eq!(codes_len, 0);
assert_eq!(funcs_len, 1);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_module_data_count_mismatch() {
let wasm = File::open("tests/fixtures/malformed/data_count_mismatch.wasm").unwrap();
let err = decode_module(wasm).expect_err("data count mismatch should fail");
match err {
DecodeModuleError::DataCountMismatch {
datas_len,
data_count,
} => {
assert_eq!(datas_len, 0);
assert_eq!(data_count, 1);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_module_data_count_datas_len_mismatch() {
let wasm = File::open("tests/fixtures/malformed/data_count_datas_len_mismatch.wasm").unwrap();
let err = decode_module(wasm).expect_err("data count datas len mismatch should fail");
match err {
DecodeModuleError::DataCountDatasLenMismatch {
datas_len,
data_count,
} => {
assert_eq!(datas_len, 0);
assert_eq!(data_count, 1);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_module_data_index_without_data_count() {
let wasm = File::open("tests/fixtures/malformed/data_index_without_data_count.wasm").unwrap();
let err = decode_module(wasm).expect_err("data index without data count should fail");
match err {
DecodeModuleError::DataIndexWithoutDataCount => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_custom_section_error_decode_name_length() {
let wasm = File::open("tests/fixtures/malformed/custom_name_length_overlong.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong custom name length should fail");
match err {
DecodeModuleError::DecodeCustomSection(DecodeCustomSectionError::DecodeName(
DecodeNameError::DecodeByteVector(DecodeByteVectorError::DecodeLength(
DecodeU32Error::RepresentationTooLong,
)),
)) => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_custom_section_error_decode_name_truncated() {
let wasm = File::open("tests/fixtures/malformed/custom_name_truncated_bytes.wasm").unwrap();
let err = decode_module(wasm).expect_err("truncated custom name should fail");
match err {
DecodeModuleError::DecodeCustomSection(DecodeCustomSectionError::DecodeName(
DecodeNameError::DecodeByteVector(DecodeByteVectorError::ReadElements(io_err)),
)) => assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_custom_section_error_invalid_utf8_name() {
let wasm = File::open("tests/fixtures/malformed/custom_name_invalid_utf8.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid utf8 in custom name should fail");
match err {
DecodeModuleError::DecodeCustomSection(DecodeCustomSectionError::DecodeName(
DecodeNameError::Utf8(err),
)) => {
let utf8_err = err.utf8_error();
assert_eq!(utf8_err.valid_up_to(), 0);
assert_eq!(utf8_err.error_len(), Some(1));
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_import_section_error_module_name() {
let wasm = File::open("tests/fixtures/malformed/import_module_name_decode_error.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid import module name should fail");
match err {
DecodeModuleError::DecodeImportSection(DecodeImportSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeImportError::DecodeModuleName(DecodeNameError::DecodeByteVector(
DecodeByteVectorError::DecodeLength(DecodeU32Error::Io(io_err)),
)),
},
)) => {
assert_eq!(position, 0);
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_import_section_error_entity_name_utf8() {
let wasm = File::open("tests/fixtures/malformed/import_entity_name_utf8_error.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid import entity name should fail");
match err {
DecodeModuleError::DecodeImportSection(DecodeImportSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source: DecodeImportError::DecodeName(DecodeNameError::Utf8(err)),
..
},
)) => {
let utf8_err = err.utf8_error();
assert_eq!(utf8_err.valid_up_to(), 0);
assert_eq!(utf8_err.error_len(), Some(1));
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_import_error_descriptor_missing_byte() {
let wasm = File::open("tests/fixtures/malformed/import_descriptor_missing_byte.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing import descriptor should fail");
match err {
DecodeModuleError::DecodeImportSection(DecodeImportSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source: DecodeImportError::ReadDescriptorMarkerByte(io_err),
..
},
)) => assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_import_error_invalid_descriptor() {
let wasm = File::open("tests/fixtures/malformed/import_invalid_descriptor.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid import descriptor should fail");
match err {
DecodeModuleError::DecodeImportSection(DecodeImportSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source: DecodeImportError::InvalidDescriptorMarkerByte(b),
..
},
)) => assert_eq!(b, 0xFF),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_import_error_typeidx_overlong() {
let wasm = File::open("tests/fixtures/malformed/import_typeidx_overlong.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong import type idx should fail");
match err {
DecodeModuleError::DecodeImportSection(DecodeImportSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeImportError::DecodeTypeIdx(DecodeTypeIdxError(DecodeU32Error::TooLarge)),
..
},
)) => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_import_error_table_invalid_reftype() {
let wasm = File::open("tests/fixtures/malformed/import_table_invalid_reftype.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid import table reftype should fail");
match err {
DecodeModuleError::DecodeImportSection(DecodeImportSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeImportError::DecodeTable(DecodeTableError::DecodeRefType(
DecodeRefTypeError::InvalidMarkerByte(err),
)),
..
},
)) => {
assert_eq!(err.0, 0x00);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_import_error_memory_invalid_limits() {
let wasm = File::open("tests/fixtures/malformed/import_memory_invalid_limits.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid import memory limits should fail");
match err {
DecodeModuleError::DecodeImportSection(DecodeImportSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeImportError::DecodeMemType(DecodeMemoryTypeError(
ParseLimitsError::UnexpectedMaxLimitByte(0x02),
)),
..
},
)) => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_import_error_global_invalid_mutability() {
let wasm =
File::open("tests/fixtures/malformed/import_global_invalid_mutability.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid import global mutability should fail");
match err {
DecodeModuleError::DecodeImportSection(DecodeImportSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeImportError::DecodeGlobalType(DecodeGlobalTypeError::InvalidMutability(err)),
..
},
)) => {
assert_eq!(err.0, 0x02);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_export_section_error_name_decode() {
let wasm = File::open("tests/fixtures/malformed/export_name_decode_error.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid export name should fail");
match err {
DecodeModuleError::DecodeExportSection(DecodeExportSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeExportError::DecodeName(DecodeNameError::DecodeByteVector(
DecodeByteVectorError::DecodeLength(DecodeU32Error::RepresentationTooLong),
)),
..
},
)) => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_export_error_descriptor_missing_byte() {
let wasm = File::open("tests/fixtures/malformed/export_descriptor_missing_byte.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing export descriptor should fail");
match err {
DecodeModuleError::DecodeExportSection(DecodeExportSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source: DecodeExportError::ReadDescriptorMarkerByte(io_err),
..
},
)) => assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_export_error_index_decode() {
let wasm = File::open("tests/fixtures/malformed/export_index_decode_error.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong export index should fail");
match err {
DecodeModuleError::DecodeExportSection(DecodeExportSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source: DecodeExportError::DecodeIndex(u32_err),
..
},
)) => assert!(
matches!(u32_err, DecodeU32Error::TooLarge),
"unexpected export index error: {u32_err:?}"
),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_export_error_invalid_descriptor() {
let wasm = File::open("tests/fixtures/malformed/export_invalid_descriptor.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid export descriptor should fail");
match err {
DecodeModuleError::DecodeExportSection(DecodeExportSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source: DecodeExportError::InvalidDescriptorMarkerByte(err),
..
},
)) => {
assert_eq!(err.0, 0xFF);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_start_section_error_func_idx() {
let wasm = File::open("tests/fixtures/malformed/start_section_funcidx_overlong.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong start func idx should fail");
match err {
DecodeModuleError::DecodeStartSection(DecodeStartSectionError(DecodeFuncIdxError(
DecodeU32Error::TooLarge,
))) => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_code_error_function_size_truncated() {
let wasm =
File::open("tests/fixtures/malformed/code_section_function_size_truncated.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing code size should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source: DecodeCodeError::DecodeFunctionSize(u32_err),
},
)) => {
assert_eq!(position, 0);
match u32_err {
DecodeU32Error::Io(io_err) => {
assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof);
}
other => panic!("expected Io error, got {other:?}"),
}
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_code_error_locals_vector_length_overlong() {
let wasm =
File::open("tests/fixtures/malformed/code_section_locals_vector_length_overlong.wasm")
.unwrap();
let err = decode_module(wasm).expect_err("overlong locals vector length should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source:
DecodeCodeError::DecodeLocalsVector(DecodeVectorError::DecodeLength(
DecodeU32Error::RepresentationTooLong,
)),
},
)) => assert_eq!(position, 0),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_code_locals_error_count_out_of_bounds() {
let wasm = File::open("tests/fixtures/malformed/code_section_locals_count_out_of_bounds.wasm")
.unwrap();
let err = decode_module(wasm).expect_err("too many locals should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeLocalsVector(DecodeVectorError::ParseElement {
source: DecodeCodeLocalsError::LocalsCountOutOfBound { .. },
..
}),
..
},
)) => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_code_locals_error_decode_count() {
let wasm =
File::open("tests/fixtures/malformed/code_section_locals_count_decode_error.wasm").unwrap();
let err = decode_module(wasm).expect_err("truncated locals count should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeLocalsVector(DecodeVectorError::ParseElement {
source: DecodeCodeLocalsError::DecodeLocalsCount(DecodeU32Error::Io(io_err)),
..
}),
..
},
)) => assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_code_error_local_valtype_invalid() {
let wasm =
File::open("tests/fixtures/malformed/code_section_local_valtype_invalid.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid local valtype should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::DecodeLocalsVector(DecodeVectorError::ParseElement {
source:
DecodeCodeLocalsError::DecodeLocalValType(
DecodeValTypeError::InvalidMarkerByte(err),
),
..
}),
..
},
)) => {
assert_eq!(err.0, 0xFF);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_code_error_entry_size_mismatch() {
let wasm =
File::open("tests/fixtures/malformed/code_section_entry_size_mismatch.wasm").unwrap();
let err = decode_module(wasm).expect_err("entry size mismatch should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeCodeError::EntrySizeMismatch {
declared_bytes,
leftover_bytes,
..
},
..
},
)) => {
assert!(leftover_bytes > 0);
assert!(declared_bytes > 0);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn parse_expression_error_unexpected_else() {
let wasm = File::open("tests/fixtures/malformed/expression_unexpected_else.wasm").unwrap();
let err = decode_module(wasm).expect_err("unexpected else should fail");
match err {
DecodeModuleError::DecodeCodeSection(DecodeCodeSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source: DecodeCodeError::DecodeFunctionBody(ParseExpressionError::UnexpectedElse),
..
},
)) => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_element_section_error_bitfield_decode() {
let wasm = File::open("tests/fixtures/malformed/element_bitfield_decode_error.wasm").unwrap();
let err = decode_module(wasm).expect_err("element bitfield decode should fail");
match err {
DecodeModuleError::DecodeElementSection(DecodeElementSectionError::DecodeVector(
DecodeVectorError::ParseElement {
position,
source: DecodeElementError::DecodeBitfield(DecodeU32Error::RepresentationTooLong),
},
)) => assert_eq!(position, 0),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_element_error_invalid_bitfield() {
let wasm = File::open("tests/fixtures/malformed/element_invalid_bitfield.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid element bitfield should fail");
match err {
DecodeModuleError::DecodeElementSection(DecodeElementSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source: DecodeElementError::InvalidBitfield(n),
..
},
)) => assert_eq!(n, 9),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_element_error_offset_expression() {
let wasm = File::open("tests/fixtures/malformed/element_offset_expr_missing.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing offset expr should fail");
match err {
DecodeModuleError::DecodeElementSection(DecodeElementSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeElementError::DecodeOffsetExpression(ParseExpressionError::ParseInstruction(
ParseError::ReadOpcode(io_err),
)),
..
},
)) => assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_element_error_elemkind_invalid() {
let wasm = File::open("tests/fixtures/malformed/element_elemkind_invalid.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid element kind should fail");
match err {
DecodeModuleError::DecodeElementSection(DecodeElementSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeElementError::DecodeElementKind(DecodeElementKindError::InvalidElemKind(0x01)),
..
},
)) => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_element_error_elemkind_io() {
let wasm = File::open("tests/fixtures/malformed/element_elemkind_io.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing elemkind byte should fail");
match err {
DecodeModuleError::DecodeElementSection(DecodeElementSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source: DecodeElementError::DecodeElementKind(DecodeElementKindError::Io(io_err)),
..
},
)) => assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_element_error_reference_type_invalid() {
let wasm = File::open("tests/fixtures/malformed/element_reftype_invalid.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid element ref type should fail");
match err {
DecodeModuleError::DecodeElementSection(DecodeElementSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeElementError::DecodeReferenceType(DecodeRefTypeError::InvalidMarkerByte(err)),
..
},
)) => {
assert_eq!(err.0, 0x00);
}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_element_error_init_decode_length() {
let wasm = File::open("tests/fixtures/malformed/element_init_decode_length.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid element init vector should fail");
match err {
DecodeModuleError::DecodeElementSection(DecodeElementSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeElementError::DecodeInit(DecodeVectorError::DecodeLength(
DecodeU32Error::RepresentationTooLong,
)),
..
},
)) => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_element_error_funcidx_vector_overlong() {
let wasm = File::open("tests/fixtures/malformed/element_funcidx_vector_overlong.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong funcidx vector should fail");
match err {
DecodeModuleError::DecodeElementSection(DecodeElementSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeElementError::DecodeFuncIdxVector(DecodeVectorError::ParseElement {
source: DecodeFuncIdxError(DecodeU32Error::TooLarge),
..
}),
..
},
)) => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_element_error_table_idx_overlong() {
let wasm = File::open("tests/fixtures/malformed/element_tableidx_overlong.wasm").unwrap();
let err = decode_module(wasm).expect_err("overlong table idx should fail");
match err {
DecodeModuleError::DecodeElementSection(DecodeElementSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeElementError::DecodeTableIdx(DecodeTableIdxError(DecodeU32Error::TooLarge)),
..
},
)) => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_element_error_expression_missing_opcode() {
let wasm =
File::open("tests/fixtures/malformed/element_expression_missing_opcode.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing element expression should fail");
match err {
DecodeModuleError::DecodeElementSection(DecodeElementSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeElementError::DecodeElementExpression(ParseExpressionError::ParseInstruction(
ParseError::ReadOpcode(io_err),
)),
..
},
)) => assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_data_section_error_bitfield_decode() {
let wasm = File::open("tests/fixtures/malformed/data_bitfield_decode_error.wasm").unwrap();
let err = decode_module(wasm).expect_err("data bitfield decode should fail");
match err {
DecodeModuleError::DecodeDataSection(DecodeDataSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeDataSegmentError::DecodeBitfield(DecodeU32Error::RepresentationTooLong),
..
},
)) => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_data_section_error_invalid_bitfield() {
let wasm = File::open("tests/fixtures/malformed/data_invalid_bitfield.wasm").unwrap();
let err = decode_module(wasm).expect_err("invalid data bitfield should fail");
match err {
DecodeModuleError::DecodeDataSection(DecodeDataSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source: DecodeDataSegmentError::InvalidBitfield(n),
..
},
)) => assert_eq!(n, 3),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_data_section_error_offset_expr_missing() {
let wasm = File::open("tests/fixtures/malformed/data_offset_expr_missing.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing data offset expr should fail");
match err {
DecodeModuleError::DecodeDataSection(DecodeDataSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeDataSegmentError::DecodeOffsetExpr(ParseExpressionError::ParseInstruction(
ParseError::ReadOpcode(io_err),
)),
..
},
)) => assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_data_section_error_init_vector_truncated() {
let wasm = File::open("tests/fixtures/malformed/data_init_vector_truncated.wasm").unwrap();
let err = decode_module(wasm).expect_err("truncated data init vector should fail");
match err {
DecodeModuleError::DecodeDataSection(DecodeDataSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeDataSegmentError::DecodeInitVector(DecodeByteVectorError::ReadElements(
io_err,
)),
..
},
)) => assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_datacount_section_error_decode_segment_count() {
let wasm = File::open("tests/fixtures/malformed/data_count_decode_error.wasm").unwrap();
let err = decode_module(wasm).expect_err("data count decode should fail");
match err {
DecodeModuleError::DecodeDatacountSection(
DecodeDataCountSectionError::DecodeDataSegmentCount(
DecodeU32Error::RepresentationTooLong,
),
) => {}
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn decode_global_error_init_missing_expr() {
let wasm =
File::open("tests/fixtures/malformed/global_section_init_missing_expr.wasm").unwrap();
let err = decode_module(wasm).expect_err("missing global init should fail");
match err {
DecodeModuleError::DecodeGlobalSection(DecodeGlobalSectionError::DecodeVector(
DecodeVectorError::ParseElement {
source:
DecodeGlobalError::DecodeInit(ParseExpressionError::ParseInstruction(
ParseError::ReadOpcode(io_err),
)),
..
},
)) => assert_eq!(io_err.kind(), std::io::ErrorKind::UnexpectedEof),
other => panic!("unexpected error: {other:?}"),
}
}