[llvm] Prevent a crash when a global variable has debug metadata (PR #145918)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 27 03:20:10 PDT 2025


https://github.com/gbMattN updated https://github.com/llvm/llvm-project/pull/145918

>From db2b92a9df550c7475798bdd4cf1d6fc00dfe626 Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Thu, 26 Jun 2025 16:35:12 +0100
Subject: [PATCH 1/3] Prevent a crash when a global variable has debug metadata

---
 llvm/lib/IR/DebugInfo.cpp           |  4 +--
 llvm/unittests/IR/DebugInfoTest.cpp | 38 +++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/IR/DebugInfo.cpp b/llvm/lib/IR/DebugInfo.cpp
index 5c645ffe3f3f7..c0ca896247ff3 100644
--- a/llvm/lib/IR/DebugInfo.cpp
+++ b/llvm/lib/IR/DebugInfo.cpp
@@ -50,7 +50,7 @@ TinyPtrVector<DbgDeclareInst *> llvm::findDbgDeclares(Value *V) {
   // DenseMap lookup. This check is a bitfield datamember lookup.
   if (!V->isUsedByMetadata())
     return {};
-  auto *L = LocalAsMetadata::getIfExists(V);
+  auto *L = ValueAsMetadata::getIfExists(V);
   if (!L)
     return {};
   auto *MDV = MetadataAsValue::getIfExists(V->getContext(), L);
@@ -69,7 +69,7 @@ TinyPtrVector<DbgVariableRecord *> llvm::findDVRDeclares(Value *V) {
   // DenseMap lookup. This check is a bitfield datamember lookup.
   if (!V->isUsedByMetadata())
     return {};
-  auto *L = LocalAsMetadata::getIfExists(V);
+  auto *L = ValueAsMetadata::getIfExists(V);
   if (!L)
     return {};
 
diff --git a/llvm/unittests/IR/DebugInfoTest.cpp b/llvm/unittests/IR/DebugInfoTest.cpp
index 35bdbf8cc8321..1f88f249789f8 100644
--- a/llvm/unittests/IR/DebugInfoTest.cpp
+++ b/llvm/unittests/IR/DebugInfoTest.cpp
@@ -195,6 +195,44 @@ TEST(MetadataTest, DeleteInstUsedByDbgRecord) {
   EXPECT_TRUE(isa<UndefValue>(DVRs[0]->getValue(0)));
 }
 
+TEST(MetadataTest, GlobalConstantMetadataUsedByDbgRecord) {
+    LLVMContext C;
+  std::unique_ptr<Module> M = parseIR(C, R"(
+    @x = dso_local global i32 0, align 4
+    define i16 @f(i16 %a) !dbg !6 {
+      %b = add i16 %a, 1, !dbg !11
+      call void @llvm.dbg.declare(metadata ptr @x, metadata !9, metadata !DIExpression()), !dbg !11
+      ret i16 0, !dbg !11
+    }
+    declare void @llvm.dbg.value(metadata, metadata, metadata) #0
+    attributes #0 = { nounwind readnone speculatable willreturn }
+
+    !llvm.dbg.cu = !{!0}
+    !llvm.module.flags = !{!5}
+
+    !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
+    !1 = !DIFile(filename: "t.ll", directory: "/")
+    !2 = !{}
+    !5 = !{i32 2, !"Debug Info Version", i32 3}
+    !6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
+    !7 = !DISubroutineType(types: !2)
+    !8 = !{!9}
+    !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
+    !10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
+    !11 = !DILocation(line: 1, column: 1, scope: !6)
+)");
+
+  // Find the global @x
+  Value* V = M->getNamedValue("x");
+
+  // Find the dbg.value
+  auto DVIs = findDbgDeclares(V);
+  auto DVRs = findDVRDeclares(V);
+
+  EXPECT_EQ(DVRs[0]->getNumVariableLocationOps(), 1u);
+  EXPECT_FALSE(isa<UndefValue>(DVRs[0]->getValue(0)));
+}
+
 TEST(DbgVariableIntrinsic, EmptyMDIsKillLocation) {
   LLVMContext Ctx;
   std::unique_ptr<Module> M = parseIR(Ctx, R"(

>From 0ec2f1772b50af34351c70179115d1026a1a9a93 Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Thu, 26 Jun 2025 17:15:38 +0100
Subject: [PATCH 2/3] style

---
 llvm/unittests/IR/DebugInfoTest.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/unittests/IR/DebugInfoTest.cpp b/llvm/unittests/IR/DebugInfoTest.cpp
index 1f88f249789f8..176aa94754fa2 100644
--- a/llvm/unittests/IR/DebugInfoTest.cpp
+++ b/llvm/unittests/IR/DebugInfoTest.cpp
@@ -196,7 +196,7 @@ TEST(MetadataTest, DeleteInstUsedByDbgRecord) {
 }
 
 TEST(MetadataTest, GlobalConstantMetadataUsedByDbgRecord) {
-    LLVMContext C;
+  LLVMContext C;
   std::unique_ptr<Module> M = parseIR(C, R"(
     @x = dso_local global i32 0, align 4
     define i16 @f(i16 %a) !dbg !6 {
@@ -223,7 +223,7 @@ TEST(MetadataTest, GlobalConstantMetadataUsedByDbgRecord) {
 )");
 
   // Find the global @x
-  Value* V = M->getNamedValue("x");
+  Value *V = M->getNamedValue("x");
 
   // Find the dbg.value
   auto DVIs = findDbgDeclares(V);

>From 4e75e081ad05dbac51fefbfd7efcd1c341711d87 Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Fri, 27 Jun 2025 11:20:00 +0100
Subject: [PATCH 3/3] Add fix and test for findDVRValues too

---
 llvm/lib/IR/DebugInfo.cpp           | 2 +-
 llvm/unittests/IR/DebugInfoTest.cpp | 7 ++++++-
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/IR/DebugInfo.cpp b/llvm/lib/IR/DebugInfo.cpp
index c0ca896247ff3..ecb19fd3c82db 100644
--- a/llvm/lib/IR/DebugInfo.cpp
+++ b/llvm/lib/IR/DebugInfo.cpp
@@ -86,7 +86,7 @@ TinyPtrVector<DbgVariableRecord *> llvm::findDVRValues(Value *V) {
   // DenseMap lookup. This check is a bitfield datamember lookup.
   if (!V->isUsedByMetadata())
     return {};
-  auto *L = LocalAsMetadata::getIfExists(V);
+  auto *L = ValueAsMetadata::getIfExists(V);
   if (!L)
     return {};
 
diff --git a/llvm/unittests/IR/DebugInfoTest.cpp b/llvm/unittests/IR/DebugInfoTest.cpp
index 176aa94754fa2..41bf863420304 100644
--- a/llvm/unittests/IR/DebugInfoTest.cpp
+++ b/llvm/unittests/IR/DebugInfoTest.cpp
@@ -199,12 +199,15 @@ TEST(MetadataTest, GlobalConstantMetadataUsedByDbgRecord) {
   LLVMContext C;
   std::unique_ptr<Module> M = parseIR(C, R"(
     @x = dso_local global i32 0, align 4
+    declare void @llvm.dbg.value(metadata, metadata, metadata) #0
+
     define i16 @f(i16 %a) !dbg !6 {
       %b = add i16 %a, 1, !dbg !11
       call void @llvm.dbg.declare(metadata ptr @x, metadata !9, metadata !DIExpression()), !dbg !11
+      call void @llvm.dbg.value(metadata ptr @x, metadata !9, metadata !DIExpression()), !dbg !11
       ret i16 0, !dbg !11
     }
-    declare void @llvm.dbg.value(metadata, metadata, metadata) #0
+
     attributes #0 = { nounwind readnone speculatable willreturn }
 
     !llvm.dbg.cu = !{!0}
@@ -228,8 +231,10 @@ TEST(MetadataTest, GlobalConstantMetadataUsedByDbgRecord) {
   // Find the dbg.value
   auto DVIs = findDbgDeclares(V);
   auto DVRs = findDVRDeclares(V);
+  auto DVRVs = findDVRValues(V);
 
   EXPECT_EQ(DVRs[0]->getNumVariableLocationOps(), 1u);
+  EXPECT_TRUE(DVRVs.size() == 1);
   EXPECT_FALSE(isa<UndefValue>(DVRs[0]->getValue(0)));
 }
 



More information about the llvm-commits mailing list