Skip to content

Commit 4c16e95

Browse files
authored
wasmparser: provide a better error if multiple modules/components concatenated (#2293)
1 parent 7308476 commit 4c16e95

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

crates/wasmparser/src/binary_reader.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,11 @@ impl<'a> BinaryReader<'a> {
736736
Ok(self.buffer[self.position])
737737
}
738738

739+
pub(crate) fn peek_bytes(&self, len: usize) -> Result<&[u8]> {
740+
self.ensure_has_bytes(len)?;
741+
Ok(&self.buffer[self.position..(self.position + len)])
742+
}
743+
739744
pub(crate) fn read_block_type(&mut self) -> Result<BlockType> {
740745
let b = self.peek()?;
741746

crates/wasmparser/src/parser.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,22 @@ impl Parser {
714714
return Ok(Payload::End(reader.original_position()));
715715
}
716716

717+
// Corrupted binaries containing multiple modules or
718+
// components will fail because a section can never start with
719+
// the magic number: 0 is custom section, 'a' is section len
720+
// of 97, `s` is section name string len of 115, at which
721+
// point validation will fail because name string is bigger
722+
// than section. Report a better error instead:
723+
match reader.peek_bytes(4) {
724+
Ok(peek) if peek == WASM_MAGIC_NUMBER => {
725+
return Err(BinaryReaderError::new(
726+
"expected section, got wasm magic number",
727+
reader.original_position(),
728+
));
729+
}
730+
_ => {}
731+
}
732+
717733
let id_pos = reader.original_position();
718734
let id = reader.read_u8()?;
719735
if id & 0x80 != 0 {
@@ -738,7 +754,7 @@ impl Parser {
738754
}
739755

740756
match (self.encoding, id) {
741-
// Sections for both modules and components.
757+
// Custom sections for both modules and components.
742758
(_, 0) => section(reader, len, CustomSectionReader::new, CustomSection),
743759

744760
// Module sections

tests/cli/invalid/concat.wast

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
;; RUN: wast --assert default --snapshot tests/snapshots %
2+
3+
;; corrupt binary: multiple modules concatenated
4+
(assert_malformed
5+
(module binary
6+
"\00asm" "\01\00\00\00" ;; magic header
7+
"\01\04" ;; type section
8+
"\01" ;; 1 count
9+
"\60\00\00" ;; no params or results
10+
"\00asm" "\01\00\00\00" ;; magic header
11+
)
12+
"expected section, got wasm magic number")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"source_filename": "tests/cli/invalid/concat.wast",
3+
"commands": [
4+
{
5+
"type": "assert_malformed",
6+
"line": 5,
7+
"filename": "concat.0.wasm",
8+
"module_type": "binary",
9+
"text": "expected section, got wasm magic number"
10+
}
11+
]
12+
}

0 commit comments

Comments
 (0)