Skip to content

Commit 8f973d5

Browse files
committed
[DebugInfo] Fix crash when printing malformed DBG machine instructions
MachineVerifier does not check that DBG_VALUE, DBG_VALUE_LIST and DBG_INSTR_REF have the expected number of operands, so printing them (e.g. with -print-after-all) should not crash. Differential Revision: https://reviews.llvm.org/D156226
1 parent 620e2bb commit 8f973d5

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

llvm/lib/CodeGen/MachineInstr.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,16 +1883,20 @@ void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST,
18831883
DL.print(OS);
18841884
}
18851885

1886-
// Print extra comments for DEBUG_VALUE.
1887-
if (isDebugValueLike() && getDebugVariableOp().isMetadata()) {
1888-
if (!HaveSemi) {
1889-
OS << ";";
1890-
HaveSemi = true;
1886+
// Print extra comments for DEBUG_VALUE and friends if they are well-formed.
1887+
if ((isNonListDebugValue() && getNumOperands() >= 4) ||
1888+
(isDebugValueList() && getNumOperands() >= 2) ||
1889+
(isDebugRef() && getNumOperands() >= 3)) {
1890+
if (getDebugVariableOp().isMetadata()) {
1891+
if (!HaveSemi) {
1892+
OS << ";";
1893+
HaveSemi = true;
1894+
}
1895+
auto *DV = getDebugVariable();
1896+
OS << " line no:" << DV->getLine();
1897+
if (isIndirectDebugValue())
1898+
OS << " indirect";
18911899
}
1892-
auto *DV = getDebugVariable();
1893-
OS << " line no:" << DV->getLine();
1894-
if (isIndirectDebugValue())
1895-
OS << " indirect";
18961900
}
18971901
// TODO: DBG_LABEL
18981902

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# RUN: llc -run-pass=machineinstr-printer -o - %s | FileCheck %s
2+
3+
---
4+
name: test
5+
body: |
6+
bb.0:
7+
; CHECK-LABEL: name: test
8+
; CHECK: DBG_VALUE
9+
; CHECK-NEXT: DBG_VALUE_LIST
10+
; CHECK-NEXT: DBG_INSTR_REF
11+
DBG_VALUE
12+
DBG_VALUE_LIST
13+
DBG_INSTR_REF
14+
...

0 commit comments

Comments
 (0)