[llvm-branch-commits] [llvm] ce7f30b - [llvm-pdbutil] Don't crash when printing unknown CodeView type records

Alexandre Ganea via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Jan 7 12:49:42 PST 2021


Author: Alexandre Ganea
Date: 2021-01-07T15:44:55-05:00
New Revision: ce7f30b2a874386a0ce089c98327acb65e87b04d

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

LOG: [llvm-pdbutil] Don't crash when printing unknown CodeView type records

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

Added: 
    llvm/test/tools/llvm-pdbutil/Inputs/unknown-record.obj
    llvm/test/tools/llvm-pdbutil/unknown-records.test

Modified: 
    llvm/tools/llvm-pdbutil/DumpOutputStyle.cpp
    llvm/tools/llvm-pdbutil/FormatUtil.cpp
    llvm/tools/llvm-pdbutil/FormatUtil.h

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-pdbutil/Inputs/unknown-record.obj b/llvm/test/tools/llvm-pdbutil/Inputs/unknown-record.obj
new file mode 100644
index 000000000000..0be00ffdd922
Binary files /dev/null and b/llvm/test/tools/llvm-pdbutil/Inputs/unknown-record.obj 
diff er

diff  --git a/llvm/test/tools/llvm-pdbutil/unknown-records.test b/llvm/test/tools/llvm-pdbutil/unknown-records.test
new file mode 100644
index 000000000000..9773b85a0cd7
--- /dev/null
+++ b/llvm/test/tools/llvm-pdbutil/unknown-records.test
@@ -0,0 +1,3 @@
+; REQUIRES: diasdk
+; RUN: llvm-pdbutil dump -all %p/Inputs/unknown-record.obj | FileCheck %s
+; CHECK: UNKNOWN RECORD (0x1609)

diff  --git a/llvm/tools/llvm-pdbutil/DumpOutputStyle.cpp b/llvm/tools/llvm-pdbutil/DumpOutputStyle.cpp
index aa185e8a2f22..3d8a86f34922 100644
--- a/llvm/tools/llvm-pdbutil/DumpOutputStyle.cpp
+++ b/llvm/tools/llvm-pdbutil/DumpOutputStyle.cpp
@@ -738,21 +738,17 @@ namespace {
 constexpr uint32_t kNoneUdtKind = 0;
 constexpr uint32_t kSimpleUdtKind = 1;
 constexpr uint32_t kUnknownUdtKind = 2;
-const StringRef NoneLabel("<none type>");
-const StringRef SimpleLabel("<simple type>");
-const StringRef UnknownLabel("<unknown type>");
-
 } // namespace
 
-static StringRef getUdtStatLabel(uint32_t Kind) {
+static std::string getUdtStatLabel(uint32_t Kind) {
   if (Kind == kNoneUdtKind)
-    return NoneLabel;
+    return "<none type>";
 
   if (Kind == kSimpleUdtKind)
-    return SimpleLabel;
+    return "<simple type>";
 
   if (Kind == kUnknownUdtKind)
-    return UnknownLabel;
+    return "<unknown type>";
 
   return formatTypeLeafKind(static_cast<TypeLeafKind>(Kind));
 }
@@ -760,7 +756,7 @@ static StringRef getUdtStatLabel(uint32_t Kind) {
 static uint32_t getLongestTypeLeafName(const StatCollection &Stats) {
   size_t L = 0;
   for (const auto &Stat : Stats.Individual) {
-    StringRef Label = getUdtStatLabel(Stat.first);
+    std::string Label = getUdtStatLabel(Stat.first);
     L = std::max(L, Label.size());
   }
   return static_cast<uint32_t>(L);
@@ -869,7 +865,7 @@ Error DumpOutputStyle::dumpUdtStats() {
 
   P.formatLine("{0}", fmt_repeat('-', TableWidth));
   for (const auto &Stat : UdtTargetStats.getStatsSortedBySize()) {
-    StringRef Label = getUdtStatLabel(Stat.first);
+    std::string Label = getUdtStatLabel(Stat.first);
     P.formatLine("{0} | {1:N}  {2:N}",
                  fmt_align(Label, AlignStyle::Right, FieldWidth),
                  fmt_align(Stat.second.Count, AlignStyle::Right, CD),

diff  --git a/llvm/tools/llvm-pdbutil/FormatUtil.cpp b/llvm/tools/llvm-pdbutil/FormatUtil.cpp
index c9ef19609496..b4837398f1d0 100644
--- a/llvm/tools/llvm-pdbutil/FormatUtil.cpp
+++ b/llvm/tools/llvm-pdbutil/FormatUtil.cpp
@@ -156,16 +156,17 @@ std::string llvm::pdb::formatSymbolKind(SymbolKind K) {
   return formatUnknownEnum(K);
 }
 
-StringRef llvm::pdb::formatTypeLeafKind(TypeLeafKind K) {
+std::string llvm::pdb::formatTypeLeafKind(TypeLeafKind K) {
   switch (K) {
 #define TYPE_RECORD(EnumName, value, name)                                     \
   case EnumName:                                                               \
     return #EnumName;
 #include "llvm/DebugInfo/CodeView/CodeViewTypes.def"
   default:
-    llvm_unreachable("Unknown type leaf kind!");
+    return formatv("UNKNOWN RECORD ({0:X})",
+                   static_cast<std::underlying_type_t<TypeLeafKind>>(K))
+        .str();
   }
-  return "";
 }
 
 std::string llvm::pdb::formatSegmentOffset(uint16_t Segment, uint32_t Offset) {

diff  --git a/llvm/tools/llvm-pdbutil/FormatUtil.h b/llvm/tools/llvm-pdbutil/FormatUtil.h
index 133a0eb40e12..b99ccec215b5 100644
--- a/llvm/tools/llvm-pdbutil/FormatUtil.h
+++ b/llvm/tools/llvm-pdbutil/FormatUtil.h
@@ -66,7 +66,7 @@ std::string typesetStringList(uint32_t IndentLevel,
 std::string formatChunkKind(codeview::DebugSubsectionKind Kind,
                             bool Friendly = true);
 std::string formatSymbolKind(codeview::SymbolKind K);
-StringRef formatTypeLeafKind(codeview::TypeLeafKind K);
+std::string formatTypeLeafKind(codeview::TypeLeafKind K);
 
 /// Returns the number of digits in the given integer.
 inline int NumDigits(uint64_t N) {


        


More information about the llvm-branch-commits mailing list