[Object] Add basic minidump support
Summary:
This patch adds basic support for reading minidump files. It contains
the definitions of various important minidump data structures (header,
stream directory), and of one minidump stream (SystemInfo). The ability
to read other streams will be added in follow-up patches. However, all
streams can be read even now as raw data, which means lldb's minidump
support (where this code is taken from) can be immediately rebased on
top of this patch as soon as it lands.
As we don't have any support for generating minidump files (yet), this
tests the code via unit tests with some small handcrafted binaries in
the form of c char arrays.
Reviewers: Bigcheese, jhenderson, zturner
Subscribers: srhines, dschuff, mgorny, fedor.sergeev, lemo, clayborg, JDevlieghere, aprantl, lldb-commits, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59291
llvm-svn: 356652
diff --git a/llvm/lib/BinaryFormat/CMakeLists.txt b/llvm/lib/BinaryFormat/CMakeLists.txt
index 98ed847..5d792a6 100644
--- a/llvm/lib/BinaryFormat/CMakeLists.txt
+++ b/llvm/lib/BinaryFormat/CMakeLists.txt
@@ -2,6 +2,7 @@
AMDGPUMetadataVerifier.cpp
Dwarf.cpp
Magic.cpp
+ Minidump.cpp
MsgPackDocument.cpp
MsgPackDocumentYAML.cpp
MsgPackReader.cpp
diff --git a/llvm/lib/BinaryFormat/Magic.cpp b/llvm/lib/BinaryFormat/Magic.cpp
index 5e9d6e7..85f944f 100644
--- a/llvm/lib/BinaryFormat/Magic.cpp
+++ b/llvm/lib/BinaryFormat/Magic.cpp
@@ -181,7 +181,8 @@
return file_magic::coff_object;
break;
- case 'M': // Possible MS-DOS stub on Windows PE file or MSF/PDB file.
+ case 'M': // Possible MS-DOS stub on Windows PE file, MSF/PDB file or a
+ // Minidump file.
if (startswith(Magic, "MZ") && Magic.size() >= 0x3c + 4) {
uint32_t off = read32le(Magic.data() + 0x3c);
// PE/COFF file, either EXE or DLL.
@@ -191,6 +192,8 @@
}
if (Magic.startswith("Microsoft C/C++ MSF 7.00\r\n"))
return file_magic::pdb;
+ if (startswith(Magic, "MDMP"))
+ return file_magic::minidump;
break;
case 0x64: // x86-64 or ARM64 Windows.
diff --git a/llvm/lib/BinaryFormat/Minidump.cpp b/llvm/lib/BinaryFormat/Minidump.cpp
new file mode 100644
index 0000000..b618fb1
--- /dev/null
+++ b/llvm/lib/BinaryFormat/Minidump.cpp
@@ -0,0 +1,14 @@
+//===-- Minidump.cpp - Minidump constants and structures ---------*- C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/BinaryFormat/Minidump.h"
+
+using namespace llvm::minidump;
+
+constexpr uint32_t Header::MagicSignature;
+constexpr uint16_t Header::MagicVersion;
diff --git a/llvm/lib/Object/Binary.cpp b/llvm/lib/Object/Binary.cpp
index 87b2c12..e1063a6 100644
--- a/llvm/lib/Object/Binary.cpp
+++ b/llvm/lib/Object/Binary.cpp
@@ -16,6 +16,7 @@
#include "llvm/Object/Archive.h"
#include "llvm/Object/Error.h"
#include "llvm/Object/MachOUniversal.h"
+#include "llvm/Object/Minidump.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Object/WindowsResource.h"
#include "llvm/Support/Error.h"
@@ -81,6 +82,8 @@
case file_magic::coff_cl_gl_object:
// Unrecognized object file format.
return errorCodeToError(object_error::invalid_file_type);
+ case file_magic::minidump:
+ return MinidumpFile::create(Buffer);
}
llvm_unreachable("Unexpected Binary File Type");
}
diff --git a/llvm/lib/Object/CMakeLists.txt b/llvm/lib/Object/CMakeLists.txt
index fd5e770..a0ac004 100644
--- a/llvm/lib/Object/CMakeLists.txt
+++ b/llvm/lib/Object/CMakeLists.txt
@@ -13,6 +13,7 @@
IRSymtab.cpp
MachOObjectFile.cpp
MachOUniversal.cpp
+ Minidump.cpp
ModuleSymbolTable.cpp
Object.cpp
ObjectFile.cpp
diff --git a/llvm/lib/Object/Minidump.cpp b/llvm/lib/Object/Minidump.cpp
new file mode 100644
index 0000000..d0d49b4
--- /dev/null
+++ b/llvm/lib/Object/Minidump.cpp
@@ -0,0 +1,77 @@
+//===- Minidump.cpp - Minidump object file implementation -----------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Object/Minidump.h"
+#include "llvm/Object/Error.h"
+
+using namespace llvm;
+using namespace llvm::object;
+using namespace llvm::minidump;
+
+Optional<ArrayRef<uint8_t>>
+MinidumpFile::getRawStream(minidump::StreamType Type) const {
+ auto It = StreamMap.find(Type);
+ if (It != StreamMap.end())
+ return getRawStream(Streams[It->second]);
+ return None;
+}
+
+Expected<ArrayRef<uint8_t>>
+MinidumpFile::getDataSlice(ArrayRef<uint8_t> Data, size_t Offset, size_t Size) {
+ // Check for overflow.
+ if (Offset + Size < Offset || Offset + Size < Size ||
+ Offset + Size > Data.size())
+ return createEOFError();
+ return Data.slice(Offset, Size);
+}
+
+Expected<std::unique_ptr<MinidumpFile>>
+MinidumpFile::create(MemoryBufferRef Source) {
+ ArrayRef<uint8_t> Data = arrayRefFromStringRef(Source.getBuffer());
+ auto ExpectedHeader = getDataSliceAs<minidump::Header>(Data, 0, 1);
+ if (!ExpectedHeader)
+ return ExpectedHeader.takeError();
+
+ const minidump::Header &Hdr = (*ExpectedHeader)[0];
+ if (Hdr.Signature != Header::MagicSignature)
+ return createError("Invalid signature");
+ if ((Hdr.Version & 0xffff) != Header::MagicVersion)
+ return createError("Invalid version");
+
+ auto ExpectedStreams = getDataSliceAs<Directory>(Data, Hdr.StreamDirectoryRVA,
+ Hdr.NumberOfStreams);
+ if (!ExpectedStreams)
+ return ExpectedStreams.takeError();
+
+ DenseMap<StreamType, std::size_t> StreamMap;
+ for (const auto &Stream : llvm::enumerate(*ExpectedStreams)) {
+ StreamType Type = Stream.value().Type;
+ const LocationDescriptor &Loc = Stream.value().Location;
+
+ auto ExpectedStream = getDataSlice(Data, Loc.RVA, Loc.DataSize);
+ if (!ExpectedStream)
+ return ExpectedStream.takeError();
+
+ if (Type == StreamType::Unused && Loc.DataSize == 0) {
+ // Ignore dummy streams. This is technically ill-formed, but a number of
+ // existing minidumps seem to contain such streams.
+ continue;
+ }
+
+ if (Type == DenseMapInfo<StreamType>::getEmptyKey() ||
+ Type == DenseMapInfo<StreamType>::getTombstoneKey())
+ return createError("Cannot handle one of the minidump streams");
+
+ // Update the directory map, checking for duplicate stream types.
+ if (!StreamMap.try_emplace(Type, Stream.index()).second)
+ return createError("Duplicate stream type");
+ }
+
+ return std::unique_ptr<MinidumpFile>(
+ new MinidumpFile(Source, Hdr, *ExpectedStreams, std::move(StreamMap)));
+}
diff --git a/llvm/lib/Object/ObjectFile.cpp b/llvm/lib/Object/ObjectFile.cpp
index 441ef54..cf095db 100644
--- a/llvm/lib/Object/ObjectFile.cpp
+++ b/llvm/lib/Object/ObjectFile.cpp
@@ -127,6 +127,7 @@
case file_magic::macho_universal_binary:
case file_magic::windows_resource:
case file_magic::pdb:
+ case file_magic::minidump:
return errorCodeToError(object_error::invalid_file_type);
case file_magic::elf:
case file_magic::elf_relocatable:
diff --git a/llvm/lib/Object/SymbolicFile.cpp b/llvm/lib/Object/SymbolicFile.cpp
index 8ee8256..675f209 100644
--- a/llvm/lib/Object/SymbolicFile.cpp
+++ b/llvm/lib/Object/SymbolicFile.cpp
@@ -52,6 +52,7 @@
case file_magic::macho_universal_binary:
case file_magic::windows_resource:
case file_magic::pdb:
+ case file_magic::minidump:
return errorCodeToError(object_error::invalid_file_type);
case file_magic::elf:
case file_magic::elf_executable: