Add support for parsing Breakpad minidump files that can have extra padding in the...
authorGreg Clayton <gclayton@apple.com>
Mon, 23 Jul 2018 14:16:08 +0000 (14:16 +0000)
committerGreg Clayton <gclayton@apple.com>
Mon, 23 Jul 2018 14:16:08 +0000 (14:16 +0000)
Differential Revision: https://reviews.llvm.org/D49579

llvm-svn: 337694

lldb/source/Plugins/Process/minidump/MinidumpTypes.cpp
lldb/unittests/Process/minidump/CMakeLists.txt
lldb/unittests/Process/minidump/Inputs/memory-list-not-padded.dmp [new file with mode: 0644]
lldb/unittests/Process/minidump/Inputs/memory-list-padded.dmp [new file with mode: 0644]
lldb/unittests/Process/minidump/Inputs/module-list-not-padded.dmp [new file with mode: 0644]
lldb/unittests/Process/minidump/Inputs/module-list-padded.dmp [new file with mode: 0644]
lldb/unittests/Process/minidump/Inputs/thread-list-not-padded.dmp [new file with mode: 0644]
lldb/unittests/Process/minidump/Inputs/thread-list-padded.dmp [new file with mode: 0644]
lldb/unittests/Process/minidump/MinidumpParserTest.cpp

index b095aeb..049704b 100644 (file)
@@ -81,11 +81,17 @@ const MinidumpThread *MinidumpThread::Parse(llvm::ArrayRef<uint8_t> &data) {
 
 llvm::ArrayRef<MinidumpThread>
 MinidumpThread::ParseThreadList(llvm::ArrayRef<uint8_t> &data) {
+  const auto orig_size = data.size();
   const llvm::support::ulittle32_t *thread_count;
   Status error = consumeObject(data, thread_count);
   if (error.Fail() || *thread_count * sizeof(MinidumpThread) > data.size())
     return {};
 
+  // Compilers might end up padding an extra 4 bytes depending on how the
+  // structure is padded by the compiler and the #pragma pack settings.
+  if (4 + *thread_count * sizeof(MinidumpThread) < orig_size)
+    data = data.drop_front(4);
+
   return llvm::ArrayRef<MinidumpThread>(
       reinterpret_cast<const MinidumpThread *>(data.data()), *thread_count);
 }
@@ -157,12 +163,17 @@ const MinidumpModule *MinidumpModule::Parse(llvm::ArrayRef<uint8_t> &data) {
 
 llvm::ArrayRef<MinidumpModule>
 MinidumpModule::ParseModuleList(llvm::ArrayRef<uint8_t> &data) {
-
+  const auto orig_size = data.size();
   const llvm::support::ulittle32_t *modules_count;
   Status error = consumeObject(data, modules_count);
   if (error.Fail() || *modules_count * sizeof(MinidumpModule) > data.size())
     return {};
-
+  
+  // Compilers might end up padding an extra 4 bytes depending on how the
+  // structure is padded by the compiler and the #pragma pack settings.
+  if (4 + *modules_count * sizeof(MinidumpModule) < orig_size)
+    data = data.drop_front(4);
+  
   return llvm::ArrayRef<MinidumpModule>(
       reinterpret_cast<const MinidumpModule *>(data.data()), *modules_count);
 }
@@ -180,11 +191,17 @@ MinidumpExceptionStream::Parse(llvm::ArrayRef<uint8_t> &data) {
 
 llvm::ArrayRef<MinidumpMemoryDescriptor>
 MinidumpMemoryDescriptor::ParseMemoryList(llvm::ArrayRef<uint8_t> &data) {
+  const auto orig_size = data.size();
   const llvm::support::ulittle32_t *mem_ranges_count;
   Status error = consumeObject(data, mem_ranges_count);
   if (error.Fail() ||
       *mem_ranges_count * sizeof(MinidumpMemoryDescriptor) > data.size())
     return {};
+  
+  // Compilers might end up padding an extra 4 bytes depending on how the
+  // structure is padded by the compiler and the #pragma pack settings.
+  if (4 + *mem_ranges_count * sizeof(MinidumpMemoryDescriptor) < orig_size)
+    data = data.drop_front(4);
 
   return llvm::makeArrayRef(
       reinterpret_cast<const MinidumpMemoryDescriptor *>(data.data()),
index 308b77a..a4ebb07 100644 (file)
@@ -19,6 +19,12 @@ set(test_inputs
    fizzbuzz_no_heap.dmp
    fizzbuzz_wow64.dmp
    bad_duplicate_streams.dmp
-   bad_overlapping_streams.dmp)
+   bad_overlapping_streams.dmp
+   thread-list-padded.dmp
+   thread-list-not-padded.dmp
+   module-list-padded.dmp
+   module-list-not-padded.dmp
+   memory-list-padded.dmp
+   memory-list-not-padded.dmp)
 
 add_unittest_inputs(LLDBMinidumpTests "${test_inputs}")
diff --git a/lldb/unittests/Process/minidump/Inputs/memory-list-not-padded.dmp b/lldb/unittests/Process/minidump/Inputs/memory-list-not-padded.dmp
new file mode 100644 (file)
index 0000000..cb7b5fb
Binary files /dev/null and b/lldb/unittests/Process/minidump/Inputs/memory-list-not-padded.dmp differ
diff --git a/lldb/unittests/Process/minidump/Inputs/memory-list-padded.dmp b/lldb/unittests/Process/minidump/Inputs/memory-list-padded.dmp
new file mode 100644 (file)
index 0000000..8b7e837
Binary files /dev/null and b/lldb/unittests/Process/minidump/Inputs/memory-list-padded.dmp differ
diff --git a/lldb/unittests/Process/minidump/Inputs/module-list-not-padded.dmp b/lldb/unittests/Process/minidump/Inputs/module-list-not-padded.dmp
new file mode 100644 (file)
index 0000000..b37e251
Binary files /dev/null and b/lldb/unittests/Process/minidump/Inputs/module-list-not-padded.dmp differ
diff --git a/lldb/unittests/Process/minidump/Inputs/module-list-padded.dmp b/lldb/unittests/Process/minidump/Inputs/module-list-padded.dmp
new file mode 100644 (file)
index 0000000..431c7c0
Binary files /dev/null and b/lldb/unittests/Process/minidump/Inputs/module-list-padded.dmp differ
diff --git a/lldb/unittests/Process/minidump/Inputs/thread-list-not-padded.dmp b/lldb/unittests/Process/minidump/Inputs/thread-list-not-padded.dmp
new file mode 100644 (file)
index 0000000..34ba5cc
Binary files /dev/null and b/lldb/unittests/Process/minidump/Inputs/thread-list-not-padded.dmp differ
diff --git a/lldb/unittests/Process/minidump/Inputs/thread-list-padded.dmp b/lldb/unittests/Process/minidump/Inputs/thread-list-padded.dmp
new file mode 100644 (file)
index 0000000..838a4df
Binary files /dev/null and b/lldb/unittests/Process/minidump/Inputs/thread-list-padded.dmp differ
index 9a99f6b..cf4873f 100644 (file)
@@ -84,6 +84,76 @@ TEST_F(MinidumpParserTest, GetThreadsAndGetThreadContext) {
   EXPECT_EQ(1232UL, context.size());
 }
 
+TEST_F(MinidumpParserTest, GetThreadListNotPadded) {
+  // Verify that we can load a thread list that doesn't have 4 bytes of padding
+  // after the thread count.
+  SetUpData("thread-list-not-padded.dmp");
+  llvm::ArrayRef<MinidumpThread> thread_list;
+  
+  thread_list = parser->GetThreads();
+  ASSERT_EQ(2UL, thread_list.size());
+  EXPECT_EQ(0x11223344UL, thread_list[0].thread_id);
+  EXPECT_EQ(0x55667788UL, thread_list[1].thread_id);
+}
+
+TEST_F(MinidumpParserTest, GetThreadListPadded) {
+  // Verify that we can load a thread list that has 4 bytes of padding
+  // after the thread count as found in breakpad minidump files.
+  SetUpData("thread-list-padded.dmp");
+  auto thread_list = parser->GetThreads();
+  ASSERT_EQ(2UL, thread_list.size());
+  EXPECT_EQ(0x11223344UL, thread_list[0].thread_id);
+  EXPECT_EQ(0x55667788UL, thread_list[1].thread_id);
+}
+
+TEST_F(MinidumpParserTest, GetModuleListNotPadded) {
+  // Verify that we can load a module list that doesn't have 4 bytes of padding
+  // after the module count.
+  SetUpData("module-list-not-padded.dmp");
+  auto module_list = parser->GetModuleList();
+  ASSERT_EQ(2UL, module_list.size());
+  EXPECT_EQ(0x1000UL, module_list[0].base_of_image);
+  EXPECT_EQ(0x2000UL, module_list[0].size_of_image);
+  EXPECT_EQ(0x5000UL, module_list[1].base_of_image);
+  EXPECT_EQ(0x3000UL, module_list[1].size_of_image);
+}
+
+TEST_F(MinidumpParserTest, GetModuleListPadded) {
+  // Verify that we can load a module list that has 4 bytes of padding
+  // after the module count as found in breakpad minidump files.
+  SetUpData("module-list-padded.dmp");
+  auto module_list = parser->GetModuleList();
+  ASSERT_EQ(2UL, module_list.size());
+  EXPECT_EQ(0x1000UL, module_list[0].base_of_image);
+  EXPECT_EQ(0x2000UL, module_list[0].size_of_image);
+  EXPECT_EQ(0x5000UL, module_list[1].base_of_image);
+  EXPECT_EQ(0x3000UL, module_list[1].size_of_image);
+}
+
+TEST_F(MinidumpParserTest, GetMemoryListNotPadded) {
+  // Verify that we can load a memory list that doesn't have 4 bytes of padding
+  // after the memory range count.
+  SetUpData("memory-list-not-padded.dmp");
+  auto mem = parser->FindMemoryRange(0x8000);
+  ASSERT_TRUE(mem.hasValue());
+  EXPECT_EQ((lldb::addr_t)0x8000, mem->start);
+  mem = parser->FindMemoryRange(0x8010);
+  ASSERT_TRUE(mem.hasValue());
+  EXPECT_EQ((lldb::addr_t)0x8010, mem->start);
+}
+
+TEST_F(MinidumpParserTest, GetMemoryListPadded) {
+  // Verify that we can load a memory list that has 4 bytes of padding
+  // after the memory range count as found in breakpad minidump files.
+  SetUpData("memory-list-padded.dmp");
+  auto mem = parser->FindMemoryRange(0x8000);
+  ASSERT_TRUE(mem.hasValue());
+  EXPECT_EQ((lldb::addr_t)0x8000, mem->start);
+  mem = parser->FindMemoryRange(0x8010);
+  ASSERT_TRUE(mem.hasValue());
+  EXPECT_EQ((lldb::addr_t)0x8010, mem->start);
+}
+
 TEST_F(MinidumpParserTest, TruncatedMinidumps) {
   InvalidMinidump("linux-x86_64.dmp", 32);
   InvalidMinidump("linux-x86_64.dmp", 100);