Skip to content

Commit e1fce94

Browse files
authored
Add negative tests for compact imports (#2408)
Assert that when the feature is disabled the new binary forms are rejected.
1 parent ea6041e commit e1fce94

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

crates/wasmparser/src/readers/core/imports.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,15 @@ impl<'a> FromReader<'a> for Imports<'a> {
9393
let module = reader.read_string()?;
9494
let single_item_name = reader.read_string()?;
9595
let discriminator = reader.peek_bytes(1)?[0];
96-
match (single_item_name, discriminator, reader.compact_imports()) {
97-
("", 0x7F, true) => {
96+
match (single_item_name, discriminator) {
97+
("", 0x7F) => {
98+
if !reader.compact_imports() {
99+
bail!(
100+
reader.original_position(),
101+
"invalid leading byte 0x7F with compact imports \
102+
proposal disabled"
103+
);
104+
}
98105
// Compact encoding 1: one module name, many item names / types
99106
reader.read_bytes(1)?;
100107
// FIXME(#188) shouldn't need to skip here
@@ -111,7 +118,14 @@ impl<'a> FromReader<'a> for Imports<'a> {
111118
items: SectionLimited::new(items)?,
112119
})
113120
}
114-
("", 0x7E, true) => {
121+
("", 0x7E) => {
122+
if !reader.compact_imports() {
123+
bail!(
124+
reader.original_position(),
125+
"invalid leading byte 0x7E with compact imports \
126+
proposal disabled"
127+
);
128+
}
115129
// Compact encoding 2: one module name / type, many item names
116130
reader.read_bytes(1)?;
117131
let ty: TypeRef = reader.read()?;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
;; RUN: wast --assert default --snapshot tests/snapshots % -f=-compact-imports
2+
3+
(assert_malformed
4+
(module
5+
(import "test"
6+
(item "fi32" (func (result i32)))
7+
(item "fi64" (func (result i64)))
8+
)
9+
)
10+
"invalid leading byte 0x7F with compact imports proposal disabled")
11+
12+
(assert_malformed
13+
(module
14+
(import "test"
15+
(item "g1")
16+
(item "g20")
17+
(item "g300")
18+
(item "g4000")
19+
(global i32)
20+
)
21+
)
22+
"invalid leading byte 0x7E with compact imports proposal disabled")
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"source_filename": "tests/cli/compact-imports/compact-imports-rejected.wast",
3+
"commands": [
4+
{
5+
"type": "assert_malformed",
6+
"line": 4,
7+
"filename": "compact-imports-rejected.0.wasm",
8+
"module_type": "binary",
9+
"text": "invalid leading byte 0x7F with compact imports proposal disabled"
10+
},
11+
{
12+
"type": "assert_malformed",
13+
"line": 13,
14+
"filename": "compact-imports-rejected.1.wasm",
15+
"module_type": "binary",
16+
"text": "invalid leading byte 0x7E with compact imports proposal disabled"
17+
}
18+
]
19+
}

0 commit comments

Comments
 (0)