Skip to content

Commit 7869b2f

Browse files
committed
Validate heap types
Validate that GC is enabled if struct and array types are used by the module and validate that both GC and isorecursive types are enabled when nontrivial rec groups are used. This fixes a fuzz bug in #5239 where initial contents included a rec group but the fuzzer disabled GC. Since the resulting module passed validation, the rec groups made it into the binary output, making the type section malformed.
1 parent 66214bc commit 7869b2f

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/wasm/wasm-validator.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3461,6 +3461,25 @@ static void validateTags(Module& module, ValidationInfo& info) {
34613461
}
34623462
}
34633463

3464+
static void validateTypes(Module& module, ValidationInfo& info) {
3465+
for (auto type : ModuleUtils::collectHeapTypes(module)) {
3466+
if (type.getRecGroup().size() > 1) {
3467+
info.shouldBeTrue(module.features.hasGC() &&
3468+
getTypeSystem() == TypeSystem::Isorecursive,
3469+
type,
3470+
"Recursion groups require GC [--enable-gc] and "
3471+
"isorecursive types [--hybrid]");
3472+
}
3473+
3474+
if (!module.features.hasGC()) {
3475+
info.shouldBeTrue(
3476+
!type.isStruct(), type, "Struct types require GC [--enable-gc]");
3477+
info.shouldBeTrue(
3478+
!type.isArray(), type, "Array types require GC [--enable-gc]");
3479+
}
3480+
}
3481+
}
3482+
34643483
static void validateModule(Module& module, ValidationInfo& info) {
34653484
// start
34663485
if (module.start.is()) {
@@ -3505,6 +3524,7 @@ bool WasmValidator::validate(Module& module, Flags flags) {
35053524
validateDataSegments(module, info);
35063525
validateTables(module, info);
35073526
validateTags(module, info);
3527+
validateTypes(module, info);
35083528
validateModule(module, info);
35093529
validateFeatures(module, info);
35103530
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
;; Test that using GC types without GC is a validation error.
2+
3+
;; RUN: not wasm-opt --hybrid -all --disable-gc %s 2>&1 | filecheck %s
4+
5+
;; CHECK: Recursion groups require GC [--enable-gc] and isorecursive types [--hybrid]
6+
;; CHECK: Struct types require GC [--enable-gc]
7+
;; CHECK: Array types require GC [--enable-gc]
8+
9+
(module
10+
(rec
11+
(type $f1 (func))
12+
(type $f2 (func))
13+
)
14+
15+
(type $struct (struct))
16+
(type $array (array i32))
17+
18+
(func $test1 (type $f1)
19+
(unreachable)
20+
)
21+
22+
(func $test2 (param (ref $struct) (ref $array))
23+
(unreachable)
24+
)
25+
)

0 commit comments

Comments
 (0)