[llvm] 8f973d5 - [DebugInfo] Fix crash when printing malformed DBG machine instructions

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 2 00:28:25 PDT 2023


Author: Jay Foad
Date: 2023-08-02T08:28:20+01:00
New Revision: 8f973d5c455cecfbee7766e23c3fcc3eb1048f1e

URL: https://github.com/llvm/llvm-project/commit/8f973d5c455cecfbee7766e23c3fcc3eb1048f1e
DIFF: https://github.com/llvm/llvm-project/commit/8f973d5c455cecfbee7766e23c3fcc3eb1048f1e.diff

LOG: [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

Added: 
    llvm/test/CodeGen/MIR/Generic/dbg-value.mir

Modified: 
    llvm/lib/CodeGen/MachineInstr.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/MachineInstr.cpp b/llvm/lib/CodeGen/MachineInstr.cpp
index a9309487a7a7ff..0c4353fbc3404a 100644
--- a/llvm/lib/CodeGen/MachineInstr.cpp
+++ b/llvm/lib/CodeGen/MachineInstr.cpp
@@ -1883,16 +1883,20 @@ void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST,
     DL.print(OS);
   }
 
-  // Print extra comments for DEBUG_VALUE.
-  if (isDebugValueLike() && getDebugVariableOp().isMetadata()) {
-    if (!HaveSemi) {
-      OS << ";";
-      HaveSemi = true;
+  // Print extra comments for DEBUG_VALUE and friends if they are well-formed.
+  if ((isNonListDebugValue() && getNumOperands() >= 4) ||
+      (isDebugValueList() && getNumOperands() >= 2) ||
+      (isDebugRef() && getNumOperands() >= 3)) {
+    if (getDebugVariableOp().isMetadata()) {
+      if (!HaveSemi) {
+        OS << ";";
+        HaveSemi = true;
+      }
+      auto *DV = getDebugVariable();
+      OS << " line no:" << DV->getLine();
+      if (isIndirectDebugValue())
+        OS << " indirect";
     }
-    auto *DV = getDebugVariable();
-    OS << " line no:" <<  DV->getLine();
-    if (isIndirectDebugValue())
-      OS << " indirect";
   }
   // TODO: DBG_LABEL
 

diff  --git a/llvm/test/CodeGen/MIR/Generic/dbg-value.mir b/llvm/test/CodeGen/MIR/Generic/dbg-value.mir
new file mode 100644
index 00000000000000..f2f505f1dfdbe6
--- /dev/null
+++ b/llvm/test/CodeGen/MIR/Generic/dbg-value.mir
@@ -0,0 +1,14 @@
+# RUN: llc -run-pass=machineinstr-printer -o - %s | FileCheck %s
+
+---
+name: test
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: test
+    ; CHECK: DBG_VALUE
+    ; CHECK-NEXT: DBG_VALUE_LIST
+    ; CHECK-NEXT: DBG_INSTR_REF
+    DBG_VALUE
+    DBG_VALUE_LIST
+    DBG_INSTR_REF
+...


        


More information about the llvm-commits mailing list