blob: 3e3255a2f17cb663a1ea83a63718a95b07dccc0c [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);
41 auto ExpectedData = getDataSliceAs<UTF16>(getData(), Offset, Size);
42 if (!ExpectedData)
43 return ExpectedData.takeError();
44
45 std::string Result;
46 if (!convertUTF16ToUTF8String(*ExpectedData, Result))
47 return createError("String decoding failed");
48
49 return Result;
50}
51
Pavel Labath581d79a2019-03-21 09:18:59 +000052Expected<ArrayRef<uint8_t>>
53MinidumpFile::getDataSlice(ArrayRef<uint8_t> Data, size_t Offset, size_t Size) {
54 // Check for overflow.
55 if (Offset + Size < Offset || Offset + Size < Size ||
56 Offset + Size > Data.size())
57 return createEOFError();
58 return Data.slice(Offset, Size);
59}
60
61Expected<std::unique_ptr<MinidumpFile>>
62MinidumpFile::create(MemoryBufferRef Source) {
63 ArrayRef<uint8_t> Data = arrayRefFromStringRef(Source.getBuffer());
64 auto ExpectedHeader = getDataSliceAs<minidump::Header>(Data, 0, 1);
65 if (!ExpectedHeader)
66 return ExpectedHeader.takeError();
67
68 const minidump::Header &Hdr = (*ExpectedHeader)[0];
69 if (Hdr.Signature != Header::MagicSignature)
70 return createError("Invalid signature");
71 if ((Hdr.Version & 0xffff) != Header::MagicVersion)
72 return createError("Invalid version");
73
74 auto ExpectedStreams = getDataSliceAs<Directory>(Data, Hdr.StreamDirectoryRVA,
75 Hdr.NumberOfStreams);
76 if (!ExpectedStreams)
77 return ExpectedStreams.takeError();
78
79 DenseMap<StreamType, std::size_t> StreamMap;
80 for (const auto &Stream : llvm::enumerate(*ExpectedStreams)) {
81 StreamType Type = Stream.value().Type;
82 const LocationDescriptor &Loc = Stream.value().Location;
83
84 auto ExpectedStream = getDataSlice(Data, Loc.RVA, Loc.DataSize);
85 if (!ExpectedStream)
86 return ExpectedStream.takeError();
87
88 if (Type == StreamType::Unused && Loc.DataSize == 0) {
89 // Ignore dummy streams. This is technically ill-formed, but a number of
90 // existing minidumps seem to contain such streams.
91 continue;
92 }
93
94 if (Type == DenseMapInfo<StreamType>::getEmptyKey() ||
95 Type == DenseMapInfo<StreamType>::getTombstoneKey())
96 return createError("Cannot handle one of the minidump streams");
97
98 // Update the directory map, checking for duplicate stream types.
99 if (!StreamMap.try_emplace(Type, Stream.index()).second)
100 return createError("Duplicate stream type");
101 }
102
103 return std::unique_ptr<MinidumpFile>(
104 new MinidumpFile(Source, Hdr, *ExpectedStreams, std::move(StreamMap)));
105}