[llvm] [MC] Diagnose applying a specifier to an already-specified expression (PR #219710)

Guy David via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 29 11:06:53 PDT 2026


https://github.com/guy-david created https://github.com/llvm/llvm-project/pull/219710

MCAsmParser::applySpecifier() hit llvm_unreachable() when asked to apply a relocation specifier to an expression that already carries one, which caused a crash.

Assisted-by: Opus 5.0

>From 11b40521d5967482d27b7767a58504dfe9ec0dfd Mon Sep 17 00:00:00 2001
From: Guy David <guyda96 at gmail.com>
Date: Sat, 29 Aug 2026 16:54:15 +0300
Subject: [PATCH 1/2] [MC] Add test reproducing assembler crash on nested
 specifier (NFC)

---
 llvm/test/MC/AArch64/nested-specifier-error.s | 7 +++++++
 1 file changed, 7 insertions(+)
 create mode 100644 llvm/test/MC/AArch64/nested-specifier-error.s

diff --git a/llvm/test/MC/AArch64/nested-specifier-error.s b/llvm/test/MC/AArch64/nested-specifier-error.s
new file mode 100644
index 0000000000000..c0e4ee4ea4946
--- /dev/null
+++ b/llvm/test/MC/AArch64/nested-specifier-error.s
@@ -0,0 +1,7 @@
+// REQUIRES: asserts
+// RUN: not --crash llvm-mc -triple arm64e-apple-macosx -filetype obj %s -o /dev/null 2>&1 | FileCheck %s
+
+// CHECK: cannot apply another specifier to MCSpecifierExpr
+  ldr q4, [x8, :lo12:sym at PAGEOFF]
+  add x8, x8, :lo12:sym at PAGEOFF
+  add x8, x8, :lo12:sym at PAGEOFF + 4

>From 2a88c33baefd1428917d5dd83674c61d264dd3eb Mon Sep 17 00:00:00 2001
From: Guy David <guyda96 at gmail.com>
Date: Sat, 29 Aug 2026 16:54:46 +0300
Subject: [PATCH 2/2] [MC] Diagnose applying a specifier to an
 already-specified expression

MCAsmParser::applySpecifier() hit llvm_unreachable() when asked to apply
a relocation specifier to an expression that already carries one, which
caused a crash.

Assisted-by: Opus 5.0
---
 llvm/lib/MC/MCParser/AsmParser.cpp            | 4 +++-
 llvm/test/MC/AArch64/nested-specifier-error.s | 9 ++++++---
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index c4c5b1da9aa91..94082684e13ea 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -1360,7 +1360,9 @@ const MCExpr *MCAsmParser::applySpecifier(const MCExpr *E, uint32_t Spec) {
   // if there is exactly one symbol.
   switch (E->getKind()) {
   case MCExpr::Specifier:
-    llvm_unreachable("cannot apply another specifier to MCSpecifierExpr");
+    TokError("invalid variant on expression '" + getTok().getIdentifier() +
+             "' (already modified)");
+    return E;
   case MCExpr::Target:
   case MCExpr::Constant:
     return nullptr;
diff --git a/llvm/test/MC/AArch64/nested-specifier-error.s b/llvm/test/MC/AArch64/nested-specifier-error.s
index c0e4ee4ea4946..0eea84989772a 100644
--- a/llvm/test/MC/AArch64/nested-specifier-error.s
+++ b/llvm/test/MC/AArch64/nested-specifier-error.s
@@ -1,7 +1,10 @@
-// REQUIRES: asserts
-// RUN: not --crash llvm-mc -triple arm64e-apple-macosx -filetype obj %s -o /dev/null 2>&1 | FileCheck %s
+// RUN: not llvm-mc -triple arm64e-apple-macosx -filetype obj %s -o /dev/null 2>&1 | FileCheck %s
 
-// CHECK: cannot apply another specifier to MCSpecifierExpr
+// CHECK: [[@LINE+1]]:26: error: invalid variant on expression 'PAGEOFF' (already modified)
   ldr q4, [x8, :lo12:sym at PAGEOFF]
+
+// CHECK: [[@LINE+1]]:25: error: invalid variant on expression 'PAGEOFF' (already modified)
   add x8, x8, :lo12:sym at PAGEOFF
+
+// CHECK: [[@LINE+1]]:25: error: invalid variant on expression 'PAGEOFF' (already modified)
   add x8, x8, :lo12:sym at PAGEOFF + 4



More information about the llvm-commits mailing list