Skip to content

Commit 79d54a1

Browse files
authored
Fix crash in MinimizeRecGroups (#8685)
We previously did not store type indices for public types. This caused a crash when the logic for comparing rec groups came across references to public types and assumed there would be indices to compare them by. Fixes #8682.
1 parent 6c32125 commit 79d54a1

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

src/passes/MinimizeRecGroups.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,10 @@ struct MinimizeRecGroups : Pass {
311311
// generate new groups with the same shape.
312312
std::unordered_set<RecGroup> publicGroups;
313313
for (auto& [type, info] : typeInfo) {
314+
typeIndices.insert({type, typeIndices.size()});
314315
if (info.visibility == ModuleUtils::Visibility::Private) {
315316
// We can optimize private types.
316317
types.push_back(type);
317-
typeIndices.insert({type, typeIndices.size()});
318318
} else {
319319
publicGroups.insert(type.getRecGroup());
320320
}

test/lit/passes/minimize-rec-groups.wast

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,4 +563,44 @@
563563
;; CHECK: (global $privateB (ref null $privateB) (ref.null none))
564564
(global $privateB (ref null $privateB) (ref.null none))
565565
)
566+
567+
;; Regression test for a bug where we crashed when comparing rec groups with
568+
;; references to public types because we did not store type indices for public
569+
;; types.
566570
;; CHECK: (export "g" (global $public))
571+
(module
572+
(rec
573+
;; CHECK: (rec
574+
;; CHECK-NEXT: (type $public (sub (descriptor $desc) (struct)))
575+
(type $public (sub (descriptor $desc) (struct)))
576+
;; CHECK: (type $desc (sub (describes $public) (struct)))
577+
(type $desc (sub (describes $public) (struct)))
578+
)
579+
580+
;; We should not crash when comparing these identical connected components.
581+
(rec
582+
;; CHECK: (rec
583+
;; CHECK-NEXT: (type $private1 (sub $public (descriptor $desc1) (struct)))
584+
(type $private1 (sub $public (descriptor $desc1) (struct)))
585+
;; CHECK: (type $desc1 (sub $desc (describes $private1) (struct)))
586+
587+
;; CHECK: (rec
588+
;; CHECK-NEXT: (type $4 (struct))
589+
590+
;; CHECK: (type $private2 (sub $public (descriptor $desc2) (struct)))
591+
(type $private2 (sub $public (descriptor $desc2) (struct)))
592+
(type $desc1 (sub $desc (describes $private1) (struct)))
593+
;; CHECK: (type $desc2 (sub $desc (describes $private2) (struct)))
594+
(type $desc2 (sub $desc (describes $private2) (struct)))
595+
)
596+
597+
;; CHECK: (global $use1 (ref null $private1) (ref.null none))
598+
(global $use1 (ref null $private1) (ref.null none))
599+
;; CHECK: (global $use2 (ref null $private2) (ref.null none))
600+
(global $use2 (ref null $private2) (ref.null none))
601+
;; CHECK: (global $public (ref null $public) (ref.null none))
602+
(global $public (ref null $public) (ref.null none))
603+
;; CHECK: (export "public" (global $public))
604+
(export "public" (global $public))
605+
)
606+

0 commit comments

Comments
 (0)