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

Alexandre Ganea via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 22 11:22:42 PST 2020


aganea created this revision.
aganea added reviewers: rnk, epastor, MaskRay.
Herald added a subscriber: pengfei.
aganea requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

It seems Microsoft has added some new, undocumented, debug type records with the (latest?) VS 2019 16.8 release. For example:

  >cat a.cpp
  namespace NS {
    struct Foo {
      explicit Foo(int x) : X(x) {}
      int X;
    };
  }
  
  int main(int argc, char **argv) {
    NS::Foo f(argc);
    return 0;
  }
  
  >cl /c /Z7 /GS- a.cpp
  Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29335 for x86
  Copyright (C) Microsoft Corporation.  All rights reserved.
  
  a.cpp
  
  >cvdump.exe a.obj | grep "UNRECOGNIZED"
  0x1004 : Length = 46, Leaf = 0x1609 UNRECOGNIZED TYPE
  0x1009 : Length = 46, Leaf = 0x1609 UNRECOGNIZED TYPE

Before this patch, `llvm-pdbutil dump a.obj` used to crash. Now it displays "UNKOWN RECORD":

  >llvm-pdbutil dump -all a.obj | grep -C2 "0x1609"
                  Record Kind | Count  Size
      --------------------------------------
      UNKNOWN RECORD (0x1609) |     1    48
      --------------------------------------
                Total (S_UDT) |     1    16
  --
  0x1003 | LF_FUNC_ID [size = 20]
           name = main, type = 0x1002, parent scope = <no type>
  0x1004 | UNKNOWN RECORD (0x1609) [size = 48]
  0x1005 | LF_POINTER [size = 12]
           referent = 0x1004, mode = pointer, opts = const, kind = ptr64
  --
             type = 0x1007, vftable offset = -1, attrs = public
           - LF_MEMBER [name = `X`, Type = 0x0074 (int), offset = 0, attrs = public]
  0x1009 | UNKNOWN RECORD (0x1609) [size = 48]
  0x100A | LF_STRING_ID [size = 48] ID: <no type>, String: F:\aganea\llvm-project\__test\a.cpp
  0x100B | LF_UDT_SRC_LINE [size = 16]

There's also a crash in `obj2yaml` as well, I can send another review for that.

@gratianlup Do you think if it would be possible to to upgrade the list of records in https://github.com/microsoft/microsoft-pdb ? Thanks in advance!


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D93720

Files:
  llvm/test/tools/llvm-pdbutil/Inputs/unknown-record.obj
  llvm/test/tools/llvm-pdbutil/unknown-records.test
  llvm/tools/llvm-pdbutil/DumpOutputStyle.cpp
  llvm/tools/llvm-pdbutil/FormatUtil.cpp
  llvm/tools/llvm-pdbutil/FormatUtil.h


Index: llvm/tools/llvm-pdbutil/FormatUtil.h
===================================================================
--- llvm/tools/llvm-pdbutil/FormatUtil.h
+++ llvm/tools/llvm-pdbutil/FormatUtil.h
@@ -66,7 +66,7 @@
 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) {
Index: llvm/tools/llvm-pdbutil/FormatUtil.cpp
===================================================================
--- llvm/tools/llvm-pdbutil/FormatUtil.cpp
+++ llvm/tools/llvm-pdbutil/FormatUtil.cpp
@@ -156,16 +156,17 @@
   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) {
Index: llvm/tools/llvm-pdbutil/DumpOutputStyle.cpp
===================================================================
--- llvm/tools/llvm-pdbutil/DumpOutputStyle.cpp
+++ llvm/tools/llvm-pdbutil/DumpOutputStyle.cpp
@@ -738,21 +738,17 @@
 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 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 @@
 
   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),
Index: llvm/test/tools/llvm-pdbutil/unknown-records.test
===================================================================
--- /dev/null
+++ 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)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93720.313395.patch
Type: text/x-patch
Size: 3580 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201222/42a38e8e/attachment.bin>


More information about the llvm-commits mailing list