blob: a5de579a6274774e877a9516fbb88733085cc2b6 [file] [log] [blame]
Pavel Labath581d79a2019-03-21 09:18:59 +00001//===- Minidump.cpp - Minidump object file implementation -----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Object/Minidump.h"
10#include "llvm/Object/Error.h"
Pavel Labath51d9fa02019-04-05 08:06:26 +000011#include "llvm/Support/ConvertUTF.h"
Pavel Labath581d79a2019-03-21 09:18:59 +000012
13using namespace llvm;
14using namespace llvm::object;
15using namespace llvm::minidump;
16
17Optional<ArrayRef<uint8_t>>
18MinidumpFile::getRawStream(minidump::StreamType Type) const {
19 auto It = StreamMap.find(Type);
20 if (It != StreamMap.end())
21 return getRawStream(Streams[It->second]);
22 return None;
23}
24
Pavel Labath51d9fa02019-04-05 08:06:26 +000025Expected<std::string> MinidumpFile::getString(size_t Offset) const {
26 // Minidump strings consist of a 32-bit length field, which gives the size of
27 // the string in *bytes*. This is followed by the actual string encoded in
28 // UTF16.
29 auto ExpectedSize =
30 getDataSliceAs<support::ulittle32_t>(getData(), Offset, 1);
31 if (!ExpectedSize)
32 return ExpectedSize.takeError();
33 size_t Size = (*ExpectedSize)[0];
34 if (Size % 2 != 0)
35 return createError("String size not even");
36 Size /= 2;
37 if (Size == 0)
38 return "";
39
40 Offset += sizeof(support::ulittle32_t);
Pavel Labathebdc6982019-04-05 08:43:54 +000041 auto ExpectedData =
42 getDataSliceAs<support::ulittle16_t>(getData(), Offset, Size);
Pavel Labath51d9fa02019-04-05 08:06:26 +000043 if (!ExpectedData)
44 return ExpectedData.takeError();
45
Pavel Labathebdc6982019-04-05 08:43:54 +000046 SmallVector<UTF16, 32> WStr(Size);
47 copy(*ExpectedData, WStr.begin());
48
Pavel Labath51d9fa02019-04-05 08:06:26 +000049 std::string Result;
Pavel Labathebdc6982019-04-05 08:43:54 +000050 if (!convertUTF16ToUTF8String(WStr, Result))
Pavel Labath51d9fa02019-04-05 08:06:26 +000051 return createError("String decoding failed");
52
53 return Result;
54}
55
Pavel Labathcfc45192019-05-02 07:45:42 +000056template <typename T>
57Expected<ArrayRef<T>> MinidumpFile::getListStream(StreamType Stream) const {
58 auto OptionalStream = getRawStream(Stream);
Pavel Labathaaff4802019-04-08 09:57:29 +000059 if (!OptionalStream)
60 return createError("No such stream");
61 auto ExpectedSize =
62 getDataSliceAs<support::ulittle32_t>(*OptionalStream, 0, 1);
63 if (!ExpectedSize)
64 return ExpectedSize.takeError();
65
66 size_t ListSize = ExpectedSize.get()[0];
67
68 size_t ListOffset = 4;
Pavel Labathcfc45192019-05-02 07:45:42 +000069 // Some producers insert additional padding bytes to align the list to an
70 // 8-byte boundary. Check for that by comparing the list size with the overall
71 // stream size.
72 if (ListOffset + sizeof(T) * ListSize < OptionalStream->size())
Pavel Labathaaff4802019-04-08 09:57:29 +000073 ListOffset = 8;
74
Pavel Labathcfc45192019-05-02 07:45:42 +000075 return getDataSliceAs<T>(*OptionalStream, ListOffset, ListSize);
Pavel Labathaaff4802019-04-08 09:57:29 +000076}
Pavel Labathcfc45192019-05-02 07:45:42 +000077template Expected<ArrayRef<Module>>
78 MinidumpFile::getListStream(StreamType) const;
79template Expected<ArrayRef<Thread>>
80 MinidumpFile::getListStream(StreamType) const;
Pavel Labathaaff4802019-04-08 09:57:29 +000081
Pavel Labath581d79a2019-03-21 09:18:59 +000082Expected<ArrayRef<uint8_t>>
83MinidumpFile::getDataSlice(ArrayRef<uint8_t> Data, size_t Offset, size_t Size) {
84 // Check for overflow.
85 if (Offset + Size < Offset || Offset + Size < Size ||
86 Offset + Size > Data.size())
87 return createEOFError();
88 return Data.slice(Offset, Size);
89}
90
91Expected<std::unique_ptr<MinidumpFile>>
92MinidumpFile::create(MemoryBufferRef Source) {
93 ArrayRef<uint8_t> Data = arrayRefFromStringRef(Source.getBuffer());
94 auto ExpectedHeader = getDataSliceAs<minidump::Header>(Data, 0, 1);
95 if (!ExpectedHeader)
96 return ExpectedHeader.takeError();
97
98 const minidump::Header &Hdr = (*ExpectedHeader)[0];
99 if (Hdr.Signature != Header::MagicSignature)
100 return createError("Invalid signature");
101 if ((Hdr.Version & 0xffff) != Header::MagicVersion)
102 return createError("Invalid version");
103
104 auto ExpectedStreams = getDataSliceAs<Directory>(Data, Hdr.StreamDirectoryRVA,
105 Hdr.NumberOfStreams);
106 if (!ExpectedStreams)
107 return ExpectedStreams.takeError();
108
109 DenseMap<StreamType, std::size_t> StreamMap;
110 for (const auto &Stream : llvm::enumerate(*ExpectedStreams)) {
111 StreamType Type = Stream.value().Type;
112 const LocationDescriptor &Loc = Stream.value().Location;
113
114 auto ExpectedStream = getDataSlice(Data, Loc.RVA, Loc.DataSize);
115 if (!ExpectedStream)
116 return ExpectedStream.takeError();
117
118 if (Type == StreamType::Unused && Loc.DataSize == 0) {
119 // Ignore dummy streams. This is technically ill-formed, but a number of
120 // existing minidumps seem to contain such streams.
121 continue;
122 }
123
124 if (Type == DenseMapInfo<StreamType>::getEmptyKey() ||
125 Type == DenseMapInfo<StreamType>::getTombstoneKey())
126 return createError("Cannot handle one of the minidump streams");
127
128 // Update the directory map, checking for duplicate stream types.
129 if (!StreamMap.try_emplace(Type, Stream.index()).second)
130 return createError("Duplicate stream type");
131 }
132
133 return std::unique_ptr<MinidumpFile>(
134 new MinidumpFile(Source, Hdr, *ExpectedStreams, std::move(StreamMap)));
135}