release/22.x: [WebAssembly] Avoid crash in LateEHPrepare with empty cleanup pads (#200322)#200482
Merged
Merged
Conversation
Member
Author
|
@aheejin What do you think about merging this PR to the release branch? |
|
@llvm/pr-subscribers-backend-webassembly Author: llvmbot ChangesBackport dc40fcc Requested by: @aheejin Full diff: https://github.com/llvm/llvm-project/pull/200482.diff 2 Files Affected:
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:
|
aheejin
approved these changes
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backport dc40fcc
Requested by: @aheejin