[llvm] 63b428e - DWARFDebugLine.cpp: Format unknown line number standard opcodes

Hubert Tong via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 15 07:45:58 PST 2020


Author: Hubert Tong
Date: 2020-01-15T10:45:50-05:00
New Revision: 63b428e3861bed666525b3af56cd50e14ab30495

URL: https://github.com/llvm/llvm-project/commit/63b428e3861bed666525b3af56cd50e14ab30495
DIFF: https://github.com/llvm/llvm-project/commit/63b428e3861bed666525b3af56cd50e14ab30495.diff

LOG: DWARFDebugLine.cpp: Format unknown line number standard opcodes

Summary:
This patch implements `formatv()` formatting for `dwarf::LineNumberOps`
and makes use of it for the `llvm-dwarfdump --debug-line` dump.

Previously, unknown line number standard opcodes would lead to undefined
behaviour. The code would attempt to format the data pointer of an empty
`StringRef` (a null pointer) using `%s`. According to the description
for `format()`, use of that interface carries the "risk of `printf`".
Passing a null pointer in place of an array to a C library function
results in undefined behaviour.

Reviewers: jhenderson, daltenty, stevewan

Reviewed By: jhenderson

Subscribers: aprantl, hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D72369

Added: 
    

Modified: 
    llvm/include/llvm/BinaryFormat/Dwarf.h
    llvm/lib/BinaryFormat/Dwarf.cpp
    llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
    llvm/test/tools/llvm-dwarfdump/X86/debug-line.s

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/BinaryFormat/Dwarf.h b/llvm/include/llvm/BinaryFormat/Dwarf.h
index 2ad201831d2b..613cde9a0cf6 100644
--- a/llvm/include/llvm/BinaryFormat/Dwarf.h
+++ b/llvm/include/llvm/BinaryFormat/Dwarf.h
@@ -654,6 +654,11 @@ template <> struct EnumTraits<Tag> : public std::true_type {
   static constexpr char Type[4] = "TAG";
   static constexpr StringRef (*StringFn)(unsigned) = &TagString;
 };
+
+template <> struct EnumTraits<LineNumberOps> : public std::true_type {
+  static constexpr char Type[4] = "LNS";
+  static constexpr StringRef (*StringFn)(unsigned) = &LNStandardString;
+};
 } // End of namespace dwarf
 
 /// Dwarf constants format_provider

diff  --git a/llvm/lib/BinaryFormat/Dwarf.cpp b/llvm/lib/BinaryFormat/Dwarf.cpp
index 9ca3317418ce..c3da0e2c4312 100644
--- a/llvm/lib/BinaryFormat/Dwarf.cpp
+++ b/llvm/lib/BinaryFormat/Dwarf.cpp
@@ -757,3 +757,4 @@ constexpr char llvm::dwarf::EnumTraits<Attribute>::Type[];
 constexpr char llvm::dwarf::EnumTraits<Form>::Type[];
 constexpr char llvm::dwarf::EnumTraits<Index>::Type[];
 constexpr char llvm::dwarf::EnumTraits<Tag>::Type[];
+constexpr char llvm::dwarf::EnumTraits<LineNumberOps>::Type[];

diff  --git a/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
index 11adb1e47640..2eceadb65520 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
@@ -16,6 +16,7 @@
 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
 #include "llvm/Support/Errc.h"
 #include "llvm/Support/Format.h"
+#include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/WithColor.h"
 #include "llvm/Support/raw_ostream.h"
 #include <algorithm>
@@ -114,8 +115,9 @@ void DWARFDebugLine::Prologue::dump(raw_ostream &OS,
      << format("     opcode_base: %u\n", OpcodeBase);
 
   for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
-    OS << format("standard_opcode_lengths[%s] = %u\n",
-                 LNStandardString(I + 1).data(), StandardOpcodeLengths[I]);
+    OS << formatv("standard_opcode_lengths[{0}] = {1}\n",
+                  static_cast<dwarf::LineNumberOps>(I + 1),
+                  StandardOpcodeLengths[I]);
 
   if (!IncludeDirectories.empty()) {
     // DWARF v5 starts directory indexes at 0.

diff  --git a/llvm/test/tools/llvm-dwarfdump/X86/debug-line.s b/llvm/test/tools/llvm-dwarfdump/X86/debug-line.s
index c83d49f06775..7793d1a0ad69 100644
--- a/llvm/test/tools/llvm-dwarfdump/X86/debug-line.s
+++ b/llvm/test/tools/llvm-dwarfdump/X86/debug-line.s
@@ -30,7 +30,7 @@
 # CHECK-NEXT: standard_opcode_lengths[DW_LNS_set_prologue_end] = 0
 # CHECK-NEXT: standard_opcode_lengths[DW_LNS_set_epilogue_begin] = 0
 # CHECK-NEXT: standard_opcode_lengths[DW_LNS_set_isa] = 1
-# CHECK-NEXT: standard_opcode_lengths[(null)] = 0
+# CHECK-NEXT: standard_opcode_lengths[DW_LNS_unknown_d] = 0
 # CHECK-NEXT: include_directories[  0] = "dir1/dir2"
 # CHECK-NEXT: file_names[  0]:
 # CHECK-NEXT:            name: "file1.c"


        


More information about the llvm-commits mailing list