Skip to content

Commit 0158ca2

Browse files
authored
Prevent a crash when a global variable has debug metadata (#145918)
This patch fixes a crash I found when trying to compile some codebases with -fsanitize=type and -g
1 parent 7936670 commit 0158ca2

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

llvm/lib/IR/DebugInfo.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ TinyPtrVector<DbgDeclareInst *> llvm::findDbgDeclares(Value *V) {
5050
// DenseMap lookup. This check is a bitfield datamember lookup.
5151
if (!V->isUsedByMetadata())
5252
return {};
53-
auto *L = LocalAsMetadata::getIfExists(V);
53+
auto *L = ValueAsMetadata::getIfExists(V);
5454
if (!L)
5555
return {};
5656
auto *MDV = MetadataAsValue::getIfExists(V->getContext(), L);
@@ -69,7 +69,7 @@ TinyPtrVector<DbgVariableRecord *> llvm::findDVRDeclares(Value *V) {
6969
// DenseMap lookup. This check is a bitfield datamember lookup.
7070
if (!V->isUsedByMetadata())
7171
return {};
72-
auto *L = LocalAsMetadata::getIfExists(V);
72+
auto *L = ValueAsMetadata::getIfExists(V);
7373
if (!L)
7474
return {};
7575

@@ -86,7 +86,7 @@ TinyPtrVector<DbgVariableRecord *> llvm::findDVRValues(Value *V) {
8686
// DenseMap lookup. This check is a bitfield datamember lookup.
8787
if (!V->isUsedByMetadata())
8888
return {};
89-
auto *L = LocalAsMetadata::getIfExists(V);
89+
auto *L = ValueAsMetadata::getIfExists(V);
9090
if (!L)
9191
return {};
9292

llvm/unittests/IR/DebugInfoTest.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,49 @@ TEST(MetadataTest, DeleteInstUsedByDbgRecord) {
195195
EXPECT_TRUE(isa<UndefValue>(DVRs[0]->getValue(0)));
196196
}
197197

198+
TEST(MetadataTest, GlobalConstantMetadataUsedByDbgRecord) {
199+
LLVMContext C;
200+
std::unique_ptr<Module> M = parseIR(C, R"(
201+
@x = dso_local global i32 0, align 4
202+
declare void @llvm.dbg.value(metadata, metadata, metadata) #0
203+
204+
define i16 @f(i16 %a) !dbg !6 {
205+
%b = add i16 %a, 1, !dbg !11
206+
call void @llvm.dbg.declare(metadata ptr @x, metadata !9, metadata !DIExpression()), !dbg !11
207+
call void @llvm.dbg.value(metadata ptr @x, metadata !9, metadata !DIExpression()), !dbg !11
208+
ret i16 0, !dbg !11
209+
}
210+
211+
attributes #0 = { nounwind readnone speculatable willreturn }
212+
213+
!llvm.dbg.cu = !{!0}
214+
!llvm.module.flags = !{!5}
215+
216+
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
217+
!1 = !DIFile(filename: "t.ll", directory: "/")
218+
!2 = !{}
219+
!5 = !{i32 2, !"Debug Info Version", i32 3}
220+
!6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
221+
!7 = !DISubroutineType(types: !2)
222+
!8 = !{!9}
223+
!9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
224+
!10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
225+
!11 = !DILocation(line: 1, column: 1, scope: !6)
226+
)");
227+
228+
// Find the global @x
229+
Value *V = M->getNamedValue("x");
230+
231+
// Find the dbg.value
232+
auto DVIs = findDbgDeclares(V);
233+
auto DVRs = findDVRDeclares(V);
234+
auto DVRVs = findDVRValues(V);
235+
236+
EXPECT_EQ(DVRs[0]->getNumVariableLocationOps(), 1u);
237+
EXPECT_TRUE(DVRVs.size() == 1);
238+
EXPECT_FALSE(isa<UndefValue>(DVRs[0]->getValue(0)));
239+
}
240+
198241
TEST(DbgVariableIntrinsic, EmptyMDIsKillLocation) {
199242
LLVMContext Ctx;
200243
std::unique_ptr<Module> M = parseIR(Ctx, R"(

0 commit comments

Comments
 (0)