[llvm] d506aa4 - Reland "[MC][AsmParser] Diagnose improperly nested .cfi frames"

Jon Roelofs via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 21 10:33:30 PST 2023


Author: Jon Roelofs
Date: 2023-11-21T10:33:11-08:00
New Revision: d506aa4edfa66074db3dc1fa84da9d9c80d71500

URL: https://github.com/llvm/llvm-project/commit/d506aa4edfa66074db3dc1fa84da9d9c80d71500
DIFF: https://github.com/llvm/llvm-project/commit/d506aa4edfa66074db3dc1fa84da9d9c80d71500.diff

LOG: Reland "[MC][AsmParser] Diagnose improperly nested .cfi frames"

This showed up when simplifying some large testcase, where the cfi directives
became out of sync with the proc's they enclose.

Now restricted to platforms that support .subsections_via_symbols.

This reverts commit 797b68c0ba699994e1038ac33d3083541482bf19.

Fixes: #72802

Differential revision: https://reviews.llvm.org/D153167

rdar://111459507

Added: 
    llvm/test/MC/AArch64/cfi-bad-nesting-darwin.s
    llvm/test/MC/AArch64/cfi-bad-nesting-elf.s

Modified: 
    lld/test/COFF/gc-dwarf-eh.s
    llvm/lib/MC/MCParser/AsmParser.cpp

Removed: 
    


################################################################################
diff  --git a/lld/test/COFF/gc-dwarf-eh.s b/lld/test/COFF/gc-dwarf-eh.s
index 757aa671c76933a..efe92d77fcb717c 100644
--- a/lld/test/COFF/gc-dwarf-eh.s
+++ b/lld/test/COFF/gc-dwarf-eh.s
@@ -13,9 +13,9 @@
 	.def	_main; .scl	2; .type	32; .endef
 	.section	.text,"xr",one_only,_main
 	.globl	_main
+_main:
 	.cfi_startproc
 	.cfi_personality 0, ___gxx_personality_v0
-_main:
 	xorl	%eax, %eax
 	ret
 	.cfi_endproc
@@ -29,8 +29,8 @@ ___gxx_personality_v0:
 	.def	_unused; .scl	2; .type	32; .endef
 	.section	.text,"xr",one_only,_unused
 	.globl	_unused
+_unused:
 	.cfi_startproc
 	.cfi_personality 0, ___gxx_personality_v0
-_unused:
 	ret
 	.cfi_endproc

diff  --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index b36c5f067a95392..825b12e037d3080 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -125,6 +125,7 @@ class AsmParser : public MCAsmParser {
   void *SavedDiagContext;
   std::unique_ptr<MCAsmParserExtension> PlatformParser;
   SMLoc StartTokLoc;
+  std::optional<SMLoc> CFIStartProcLoc;
 
   /// This is the current buffer index we're lexing from as managed by the
   /// SourceMgr object.
@@ -1949,6 +1950,11 @@ bool AsmParser::parseStatement(ParseStatementInfo &Info,
       Lex();
     }
 
+    if (MAI.hasSubsectionsViaSymbols() && CFIStartProcLoc && Sym->isExternal())
+      return Error(StartTokLoc, "non-private labels cannot appear between "
+                                ".cfi_startproc / .cfi_endproc pairs") &&
+             Error(*CFIStartProcLoc, "previous .cfi_startproc was here");
+
     if (discardLTOSymbol(IDVal))
       return false;
 
@@ -4193,6 +4199,8 @@ bool AsmParser::parseDirectiveCFISections() {
 /// parseDirectiveCFIStartProc
 /// ::= .cfi_startproc [simple]
 bool AsmParser::parseDirectiveCFIStartProc() {
+  CFIStartProcLoc = StartTokLoc;
+
   StringRef Simple;
   if (!parseOptionalToken(AsmToken::EndOfStatement)) {
     if (check(parseIdentifier(Simple) || Simple != "simple",
@@ -4213,8 +4221,11 @@ bool AsmParser::parseDirectiveCFIStartProc() {
 /// parseDirectiveCFIEndProc
 /// ::= .cfi_endproc
 bool AsmParser::parseDirectiveCFIEndProc() {
+  CFIStartProcLoc = std::nullopt;
+
   if (parseEOL())
     return true;
+
   getStreamer().emitCFIEndProc();
   return false;
 }

diff  --git a/llvm/test/MC/AArch64/cfi-bad-nesting-darwin.s b/llvm/test/MC/AArch64/cfi-bad-nesting-darwin.s
new file mode 100644
index 000000000000000..235b7d448099298
--- /dev/null
+++ b/llvm/test/MC/AArch64/cfi-bad-nesting-darwin.s
@@ -0,0 +1,24 @@
+; RUN: not llvm-mc -triple arm64-apple-darwin %s -filetype=obj -o /dev/null 2>&1 | FileCheck %s --check-prefix=DARWIN
+
+; REQUIRES: aarch64-registered-target
+
+	.section	__TEXT,locomotive,regular,pure_instructions
+
+	.globl	_locomotive
+	.p2align	2
+_locomotive:
+	.cfi_startproc
+	ret
+
+	; It is invalid to have a non-private label between .cfi_startproc and
+	; .cfi_endproc on MachO platforms.
+	.section	__TEXT,__text,regular,pure_instructions
+	.globl	_caboose
+	.p2align	2
+_caboose:
+; DARWIN: [[#@LINE-1]]:1: error: non-private labels cannot appear between .cfi_startproc / .cfi_endproc pairs
+; DARWIN: [[#@LINE-10]]:2: error: previous .cfi_startproc was here
+	ret
+	.cfi_endproc
+
+.subsections_via_symbols
\ No newline at end of file

diff  --git a/llvm/test/MC/AArch64/cfi-bad-nesting-elf.s b/llvm/test/MC/AArch64/cfi-bad-nesting-elf.s
new file mode 100644
index 000000000000000..98734944dbef18f
--- /dev/null
+++ b/llvm/test/MC/AArch64/cfi-bad-nesting-elf.s
@@ -0,0 +1,20 @@
+# RUN: llvm-mc -triple arm64-pc-linux-gnu %s -filetype=obj -o /dev/null 2>&1 | FileCheck %s --allow-empty --check-prefix=ELF
+# RUN: llvm-mc -triple arm64-windows-gnu %s -filetype=obj -o /dev/null 2>&1 | FileCheck %s --allow-empty --check-prefix=ELF
+
+# REQUIRES: aarch64-registered-target
+
+	.globl	_locomotive
+	.p2align	2
+_locomotive:
+	.cfi_startproc
+	ret
+
+	.globl	_caboose
+	.p2align	2
+_caboose:
+	ret
+	.cfi_endproc
+
+# Check that the diagnostic does not fire on ELF, nor COFF platforms, which do
+# not support subsections_via_symbols. See also: cfi-bad-nesting-darwin.s
+# ELF-NOT: error:
\ No newline at end of file


        


More information about the llvm-commits mailing list