[llvm] [MC] Diagnose unfinished CFI frame from an earlier section (PR #196775)

via llvm-commits llvm-commits at lists.llvm.org
Sat May 9 21:00:23 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-mc

Author: somi (1wos)

<details>
<summary>Changes</summary>

Fixes #<!-- -->177852.

**Problem.** The reproducer has two `.cfi_startproc` directives separated by a `.popsection`. The first is never closed; the second is properly paired with `.cfi_endproc`. `MCStreamer::finish()` only inspects the last entry of `DwarfFrameInfos`, so the unfinished earlier frame slips through and crashes `finishImpl()` when it emits frame data with a null End label.

**Fix.** Use `hasUnfinishedDwarfFrameInfo()` instead, which walks the full `FrameInfoStack` and catches every unfinished frame.

**Test.** Added `llvm/test/MC/ELF/cfi-startproc-no-endproc.s`. The reproducer now emits `error: Unfinished frame!` instead of crashing, and `check-llvm-mc` still passes.


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


2 Files Affected:

- (modified) llvm/lib/MC/MCStreamer.cpp (+1-1) 
- (added) llvm/test/MC/ELF/cfi-startproc-no-endproc.s (+20) 


``````````diff
diff --git a/llvm/lib/MC/MCStreamer.cpp b/llvm/lib/MC/MCStreamer.cpp
index 6325eee91c57e..f9f4138916cfe 100644
--- a/llvm/lib/MC/MCStreamer.cpp
+++ b/llvm/lib/MC/MCStreamer.cpp
@@ -1117,7 +1117,7 @@ void MCStreamer::emitWindowsUnwindTables() {}
 void MCStreamer::emitWindowsUnwindTables(WinEH::FrameInfo *Frame) {}
 
 void MCStreamer::finish(SMLoc EndLoc) {
-  if ((!DwarfFrameInfos.empty() && !DwarfFrameInfos.back().End) ||
+  if (hasUnfinishedDwarfFrameInfo() ||
       (!WinFrameInfos.empty() && !WinFrameInfos.back()->End)) {
     getContext().reportError(EndLoc, "Unfinished frame!");
     return;
diff --git a/llvm/test/MC/ELF/cfi-startproc-no-endproc.s b/llvm/test/MC/ELF/cfi-startproc-no-endproc.s
new file mode 100644
index 0000000000000..76682bfc78f46
--- /dev/null
+++ b/llvm/test/MC/ELF/cfi-startproc-no-endproc.s
@@ -0,0 +1,20 @@
+# RUN: not llvm-mc %s -triple x86_64-linux -o /dev/null 2>&1 | FileCheck %s
+# RUN: not llvm-mc %s -triple x86_64-linux -filetype=obj -o /dev/null 2>&1 | FileCheck %s
+
+## https://github.com/llvm/llvm-project/issues/177852
+## Check we don't crash when an inner .cfi_startproc in one section
+## is left unfinished while a later frame in another section is
+## properly closed (so the last DwarfFrameInfo entry has a valid End).
+
+.pushsection .text.qux, "ax", @progbits
+.type qux, @function
+qux:
+.cfi_startproc
+.popsection
+
+.type quux, @function
+quux:
+.cfi_startproc
+.cfi_endproc
+
+# CHECK: error: Unfinished frame!

``````````

</details>


https://github.com/llvm/llvm-project/pull/196775


More information about the llvm-commits mailing list