Skip to content

Commit 0988bf8

Browse files
authored
[LLVM-Reduce] - Null pointer handling during distinct metadata reduction (#117570)
Some distinct metadata nodes, e.g DICompileUnit, have implicit nullptrs inside them. Iterating over them with dyn_cast leads to a crash, change the behavior so that the nullptr operands are skipped. Add the test distinct-metadata-nullptr.ll which will crash if null pointers are not handled correctly.
1 parent 1df34f1 commit 0988bf8

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; Test checking that distinct metadata reduction pass handles null pointers properly.
2+
; This test will lead to a crash if nullptrs inside distinct metadata are not handled correctly, in this case inside DICompileUnit
3+
4+
; RUN: llvm-reduce --delta-passes=distinct-metadata --aggressive-named-md-reduction --test FileCheck --test-arg %s --test-arg --input-file %s -o %t
5+
; CHECK: {{.*}}distinct !DICompileUnit{{.*}}
6+
7+
8+
!llvm.module.flags = !{!0, !1, !6}
9+
!llvm.dbg.cu = !{!4}
10+
11+
!0 = !{i32 7, !"Dwarf Version", i32 4}
12+
!1 = !{i32 2, !"Source Lang Literal", !2}
13+
!2 = !{!3}
14+
!3 = !{!4, i32 33}
15+
!4 = distinct !DICompileUnit(language: DW_LANG_OpenCL, file: !5, producer: "foobar", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
16+
!5 = !DIFile(filename: "main.cpp", directory: "foodir")
17+
!6 = !{i32 2, !"Debug Info Version", i32 3}

llvm/tools/llvm-reduce/deltas/ReduceDistinctMetadata.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ reduceNodes(MDNode *Root,
3939
// Mark the nodes for removal
4040
for (unsigned int I = 0; I < CurrentNode->getNumOperands(); ++I) {
4141
if (MDNode *Operand =
42-
dyn_cast<MDNode>(CurrentNode->getOperand(I).get())) {
42+
dyn_cast_or_null<MDNode>(CurrentNode->getOperand(I).get())) {
4343
// Check whether node has been visited
4444
if (VisitedNodes.insert(Operand))
4545
NodesToTraverse.push(Operand);
@@ -71,7 +71,7 @@ static void cleanUpTemporaries(NamedMDNode &NamedNode, MDTuple *TemporaryTuple,
7171
for (auto I = NamedNode.op_begin(); I != NamedNode.op_end(); ++I) {
7272
// If the node hasn't been traversed yet, add it to the queue of nodes to
7373
// traverse.
74-
if (MDTuple *TupleI = dyn_cast<MDTuple>((*I))) {
74+
if (MDTuple *TupleI = dyn_cast_or_null<MDTuple>((*I))) {
7575
if (VisitedNodes.insert(TupleI))
7676
NodesToTraverse.push(TupleI);
7777
}
@@ -108,7 +108,8 @@ static void cleanUpTemporaries(NamedMDNode &NamedNode, MDTuple *TemporaryTuple,
108108

109109
// Push the remaining nodes into the queue
110110
for (unsigned int I = 0; I < CurrentTuple->getNumOperands(); ++I) {
111-
MDTuple *Operand = dyn_cast<MDTuple>(CurrentTuple->getOperand(I).get());
111+
MDTuple *Operand =
112+
dyn_cast_or_null<MDTuple>(CurrentTuple->getOperand(I).get());
112113
if (Operand && VisitedNodes.insert(Operand))
113114
// If the node hasn't been traversed yet, add it to the queue of nodes
114115
// to traverse.
@@ -127,7 +128,7 @@ static void extractDistinctMetadataFromModule(Oracle &O,
127128
Program.named_metadata()) { // Iterate over the named nodes
128129
for (unsigned int I = 0; I < NamedNode.getNumOperands();
129130
++I) { // Iterate over first level unnamed nodes..
130-
if (MDTuple *Operand = dyn_cast<MDTuple>(NamedNode.getOperand(I)))
131+
if (MDTuple *Operand = dyn_cast_or_null<MDTuple>(NamedNode.getOperand(I)))
131132
reduceNodes(Operand, NodesToDelete, TemporaryTuple, O, Program);
132133
}
133134
}

0 commit comments

Comments
 (0)