Skip to content

release/22.x: [WebAssembly] Avoid crash in LateEHPrepare with empty cleanup pads (#200322)#200482

Merged
c-rhodes merged 1 commit into
llvm:release/22.xfrom
llvmbot:issue200322
Jun 1, 2026
Merged

release/22.x: [WebAssembly] Avoid crash in LateEHPrepare with empty cleanup pads (#200322)#200482
c-rhodes merged 1 commit into
llvm:release/22.xfrom
llvmbot:issue200322

Conversation

@llvmbot

@llvmbot llvmbot commented May 29, 2026

Copy link
Copy Markdown
Member

Backport dc40fcc

Requested by: @aheejin

@llvmbot

llvmbot commented May 29, 2026

Copy link
Copy Markdown
Member Author

@aheejin What do you think about merging this PR to the release branch?

@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-backend-webassembly

Author: llvmbot

Changes

Backport dc40fcc

Requested by: @aheejin


Full diff: https://github.com/llvm/llvm-project/pull/200482.diff

2 Files Affected:

  • (modified) llvm/lib/Target/WebAssembly/WebAssemblyLateEHPrepare.cpp (+2-1)
  • (modified) llvm/test/CodeGen/WebAssembly/exception.ll (+27)
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyLateEHPrepare.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyLateEHPrepare.cpp
index 8ac32f939c5f2..3189ad37d2c49 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyLateEHPrepare.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyLateEHPrepare.cpp
@@ -312,7 +312,8 @@ bool WebAssemblyLateEHPrepare::addCatchRefsAndThrowRefs(MachineFunction &MF) {
   // caught exception is rethrown. And convert RETHROWs to THROW_REFs.
   for (auto &[EHPad, Rethrows] : EHPadToRethrows) {
     auto *Catch = WebAssembly::findCatch(EHPad);
-    auto *InsertPos = Catch->getIterator()->getNextNode();
+    assert(Catch && "CATCH not found in EHPad");
+    auto InsertPos = std::next(Catch->getIterator());
     auto ExnReg = MRI.createVirtualRegister(&WebAssembly::EXNREFRegClass);
     if (Catch->getOpcode() == WebAssembly::CATCH) {
       MachineInstrBuilder MIB = BuildMI(*EHPad, InsertPos, Catch->getDebugLoc(),
diff --git a/llvm/test/CodeGen/WebAssembly/exception.ll b/llvm/test/CodeGen/WebAssembly/exception.ll
index 11e5be83d11cd..f738216d087ec 100644
--- a/llvm/test/CodeGen/WebAssembly/exception.ll
+++ b/llvm/test/CodeGen/WebAssembly/exception.ll
@@ -672,5 +672,32 @@ attributes #0 = { nounwind }
 attributes #1 = { noreturn }
 attributes #2 = { noreturn nounwind }
 
+; CHECK-LABEL: empty_cleanup_pad:
+; CHECK:     try_table    (catch_all_ref 0)
+; CHECK:     throw_ref
+define void @empty_cleanup_pad(i32 %arg) personality ptr @__gxx_wasm_personality_v0 {
+entry:
+  br label %loop
+
+loop:
+  invoke void @foo()
+          to label %loop unwind label %cleanup
+
+cleanup:
+  %exn = cleanuppad within none []
+  br label %dispatch
+
+dispatch:                                         ; preds = %cleanup, %dispatch
+  %cond = icmp eq i32 %arg, 0
+  br i1 %cond, label %ret, label %dispatch
+
+ret:                                              ; preds = %dispatch
+  cleanupret from %exn unwind label %cleanup2
+
+cleanup2:                                         ; preds = %ret
+  %exn2 = cleanuppad within none []
+  ret void
+}
+
 ;; The exception tag should not be defined locally
 ; CHECK-NOT: __cpp_exception:

@github-project-automation github-project-automation Bot moved this from Needs Triage to Needs Merge in LLVM Release Status May 29, 2026
…lvm#200322)

WebAssemblyLateEHPrepare::addCatchRefsAndThrowRefs was using
Catch->getIterator()->getNextNode() to find the insertion position
after the CATCH (or CATCH_ALL) instruction in an EH pad.
If the CATCH/CATCH_ALL instruction is the last instruction in the basic
block, getNextNode() returns nullptr, which causees a crash when passed
to BuildMI. This patch fixes it by using std::next(Catch->getIterator())
which returns MBB.end() if the catch is the last instruction, and the
overload of BuildMI that takes an iterator correctly handles BB.end().

Fixes llvm#197077

Assisted-By: Gemini
(cherry picked from commit dc40fcc)
@c-rhodes
c-rhodes merged commit a255c1e into llvm:release/22.x Jun 1, 2026
1 check was pending
@github-project-automation github-project-automation Bot moved this from Needs Merge to Done in LLVM Release Status Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

4 participants