Skip to content

Commit bab2105

Browse files
authored
Do not emit recursion groups without GC enabled (#4738)
We emit nominal types as a single large recursion group, but this produces invalid modules when --nominal or --hybrid was used without GC enabled. Fix the bug by always emitting types as though they were structural (i.e. without recursion groups) when GC is not enabled. Fixes #4723.
1 parent 4652398 commit bab2105

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/wasm/wasm-binary.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,12 @@ void WasmBinaryWriter::writeTypes() {
223223
// the type section. With nominal typing there is always one group and with
224224
// equirecursive typing there is one group per type.
225225
size_t numGroups = 0;
226-
switch (getTypeSystem()) {
226+
// MVP types are structural and do not use recursion groups.
227+
TypeSystem typeSystem = getTypeSystem();
228+
if (!wasm->features.hasGC()) {
229+
typeSystem = TypeSystem::Equirecursive;
230+
}
231+
switch (typeSystem) {
227232
case TypeSystem::Equirecursive:
228233
numGroups = indexedTypes.types.size();
229234
break;
@@ -242,7 +247,7 @@ void WasmBinaryWriter::writeTypes() {
242247
BYN_TRACE("== writeTypes\n");
243248
auto start = startSection(BinaryConsts::Section::Type);
244249
o << U32LEB(numGroups);
245-
if (getTypeSystem() == TypeSystem::Nominal) {
250+
if (typeSystem == TypeSystem::Nominal) {
246251
// The nominal recursion group contains every type.
247252
o << S32LEB(BinaryConsts::EncodedType::Rec)
248253
<< U32LEB(indexedTypes.types.size());

test/lit/nominal-no-gc.wast

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
;; Write the module with --nominal but without GC
2+
;; RUN: wasm-opt %s --nominal --disable-gc -g -o %t.wasm
3+
4+
;; We should not get any recursion groups even though we used --nominal. We use
5+
;; --hybrid -all here to make sure that any rec groups from the binary will
6+
;; actually show up in the output and cause the test to fail.
7+
;; RUN: wasm-opt %t.wasm --hybrid -all -S -o - | filecheck %s
8+
9+
;; Also check that we don't get a failure with the default configuration.
10+
;; RUN: wasm-opt %t.wasm
11+
12+
;; CHECK-NOT: rec
13+
14+
(module
15+
(type $foo (func))
16+
(type $bar (func))
17+
18+
(func $f1 (type $foo))
19+
(func $f2 (type $bar))
20+
)

0 commit comments

Comments
 (0)