[PATCH] D156226: [DebugInfo] Fix crash when printing malformed DBG machine instructions

Jay Foad via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 25 03:53:58 PDT 2023


foad created this revision.
foad added reviewers: jmorse, StephenTozer, Orlando, arsenm.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
foad requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156226

Files:
  llvm/lib/CodeGen/MachineInstr.cpp
  llvm/test/CodeGen/MIR/Generic/dbg-value.mir


Index: llvm/test/CodeGen/MIR/Generic/dbg-value.mir
===================================================================
--- /dev/null
+++ 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
+...
Index: llvm/lib/CodeGen/MachineInstr.cpp
===================================================================
--- llvm/lib/CodeGen/MachineInstr.cpp
+++ llvm/lib/CodeGen/MachineInstr.cpp
@@ -1883,16 +1883,20 @@
     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
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156226.543917.patch
Type: text/x-patch
Size: 1578 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230725/cfddf1a3/attachment.bin>


More information about the llvm-commits mailing list