LLVM 20.0.0git
Minidump.cpp
Go to the documentation of this file.
1//===- 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
11
12using namespace llvm;
13using namespace llvm::object;
14using namespace llvm::minidump;
15
16std::optional<ArrayRef<uint8_t>>
18 auto It = StreamMap.find(Type);
19 if (It != StreamMap.end())
20 return getRawStream(Streams[It->second]);
21 return std::nullopt;
22}
23
25 // Minidump strings consist of a 32-bit length field, which gives the size of
26 // the string in *bytes*. This is followed by the actual string encoded in
27 // UTF16.
28 auto ExpectedSize =
29 getDataSliceAs<support::ulittle32_t>(getData(), Offset, 1);
30 if (!ExpectedSize)
31 return ExpectedSize.takeError();
32 size_t Size = (*ExpectedSize)[0];
33 if (Size % 2 != 0)
34 return createError("String size not even");
35 Size /= 2;
36 if (Size == 0)
37 return "";
38
40 auto ExpectedData =
41 getDataSliceAs<support::ulittle16_t>(getData(), Offset, Size);
42 if (!ExpectedData)
43 return ExpectedData.takeError();
44
46 copy(*ExpectedData, WStr.begin());
47
48 std::string Result;
49 if (!convertUTF16ToUTF8String(WStr, Result))
50 return createError("String decoding failed");
51
52 return Result;
53}
54
57 return make_range(ExceptionStreamsIterator(ExceptionStreams, this),
58 ExceptionStreamsIterator({}, this));
59}
60
63 std::optional<ArrayRef<uint8_t>> Stream =
64 getRawStream(StreamType::MemoryInfoList);
65 if (!Stream)
66 return createError("No such stream");
67 auto ExpectedHeader =
68 getDataSliceAs<minidump::MemoryInfoListHeader>(*Stream, 0, 1);
69 if (!ExpectedHeader)
70 return ExpectedHeader.takeError();
71 const minidump::MemoryInfoListHeader &H = ExpectedHeader.get()[0];
73 getDataSlice(*Stream, H.SizeOfHeader, H.SizeOfEntry * H.NumberOfEntries);
74 if (!Data)
75 return Data.takeError();
76 return make_range(MemoryInfoIterator(*Data, H.SizeOfEntry),
77 MemoryInfoIterator({}, H.SizeOfEntry));
78}
79
80Expected<ArrayRef<uint8_t>> MinidumpFile::getDataSlice(ArrayRef<uint8_t> Data,
82 uint64_t Size) {
83 // Check for overflow.
84 if (Offset + Size < Offset || Offset + Size < Size ||
85 Offset + Size > Data.size())
86 return createEOFError();
87 return Data.slice(Offset, Size);
88}
89
92 ArrayRef<uint8_t> Data = arrayRefFromStringRef(Source.getBuffer());
93 auto ExpectedHeader = getDataSliceAs<minidump::Header>(Data, 0, 1);
94 if (!ExpectedHeader)
95 return ExpectedHeader.takeError();
96
97 const minidump::Header &Hdr = (*ExpectedHeader)[0];
99 return createError("Invalid signature");
100 if ((Hdr.Version & 0xffff) != Header::MagicVersion)
101 return createError("Invalid version");
102
103 auto ExpectedStreams = getDataSliceAs<Directory>(Data, Hdr.StreamDirectoryRVA,
104 Hdr.NumberOfStreams);
105 if (!ExpectedStreams)
106 return ExpectedStreams.takeError();
107
109 std::vector<Directory> ExceptionStreams;
110 for (const auto &StreamDescriptor : llvm::enumerate(*ExpectedStreams)) {
111 StreamType Type = StreamDescriptor.value().Type;
112 const LocationDescriptor &Loc = StreamDescriptor.value().Location;
113
115 getDataSlice(Data, Loc.RVA, Loc.DataSize);
116 if (!Stream)
117 return Stream.takeError();
118
119 if (Type == StreamType::Unused && Loc.DataSize == 0) {
120 // Ignore dummy streams. This is technically ill-formed, but a number of
121 // existing minidumps seem to contain such streams.
122 continue;
123 }
124
125 // Exceptions can be treated as a special case of streams. Other streams
126 // represent a list of entities, but exceptions are unique per stream.
127 if (Type == StreamType::Exception) {
128 ExceptionStreams.push_back(StreamDescriptor.value());
129 continue;
130 }
131
134 return createError("Cannot handle one of the minidump streams");
135
136 // Update the directory map, checking for duplicate stream types.
137 if (!StreamMap.try_emplace(Type, StreamDescriptor.index()).second)
138 return createError("Duplicate stream type");
139 }
140
141 return std::unique_ptr<MinidumpFile>(
142 new MinidumpFile(Source, Hdr, *ExpectedStreams, std::move(StreamMap),
143 std::move(ExceptionStreams)));
144}
145
148 ErrorAsOutParameter ErrAsOutParam(Err);
151 if (!ListHeader) {
152 Err = ListHeader.takeError();
153 return make_range(end, end);
154 }
155
156 std::optional<ArrayRef<uint8_t>> Stream =
157 getRawStream(StreamType::Memory64List);
158 if (!Stream) {
159 Err = createError("No such stream");
160 return make_range(end, end);
161 }
162
164 getDataSliceAs<minidump::MemoryDescriptor_64>(
165 *Stream, sizeof(Memory64ListHeader),
166 ListHeader->NumberOfMemoryRanges);
167
168 if (!Descriptors) {
169 Err = Descriptors.takeError();
170 return make_range(end, end);
171 }
172
173 if (!Descriptors->empty() &&
174 ListHeader->BaseRVA + Descriptors->front().DataSize > getData().size()) {
175 Err = createError("Memory64List header RVA out of range");
176 return make_range(end, end);
177 }
178
181 getData().slice(ListHeader->BaseRVA), *Descriptors),
182 Err),
184}
uint64_t Size
#define H(x, y, z)
Definition: MD5.cpp:57
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:156
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition: DenseMap.h:226
iterator end()
Definition: DenseMap.h:84
Helper for Errors used as out-parameters.
Definition: Error.h:1130
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Type(LLVMContext &C, TypeID tid)
Definition: Type.h:93
static fallible_iterator end(Underlying I)
Construct a fallible iterator that can be used as an end-of-range value.
static fallible_iterator itr(Underlying I, Error &Err)
Construct a fallible iterator that cannot be used as an end-of-range value.
A range adaptor for a pair of iterators.
MemoryBufferRef Data
Definition: Binary.h:37
static Memory64Iterator begin(ArrayRef< uint8_t > Storage, ArrayRef< minidump::MemoryDescriptor_64 > Descriptors)
Definition: Minidump.h:161
A class providing access to the contents of a minidump file.
Definition: Minidump.h:24
Expected< iterator_range< MemoryInfoIterator > > getMemoryInfoList() const
Returns the list of descriptors embedded in the MemoryInfoList stream.
Definition: Minidump.cpp:62
Expected< minidump::Memory64ListHeader > getMemoryList64Header() const
Returns the header to the memory 64 list stream.
Definition: Minidump.h:122
ArrayRef< uint8_t > getRawStream(const minidump::Directory &Stream) const
Returns the raw contents of the stream given by the directory entry.
Definition: Minidump.h:40
iterator_range< ExceptionStreamsIterator > getExceptionStreams() const
Returns an iterator that reads each exception stream independently.
Definition: Minidump.cpp:56
Expected< std::string > getString(size_t Offset) const
Returns the minidump string at the given offset.
Definition: Minidump.cpp:24
iterator_range< FallibleMemory64Iterator > getMemory64List(Error &Err) const
Returns an iterator that pairs each descriptor with it's respective content from the Memory64List str...
Definition: Minidump.cpp:147
static Expected< std::unique_ptr< MinidumpFile > > create(MemoryBufferRef Source)
Construct a new MinidumpFile object from the given memory buffer.
Definition: Minidump.cpp:91
StreamType
The type of a minidump stream identifies its contents.
Definition: Minidump.h:50
detail::packed_endian_specific_integral< uint32_t, llvm::endianness::little, unaligned > ulittle32_t
Definition: Endian.h:285
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1697
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition: STLExtras.h:2448
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
bool convertUTF16ToUTF8String(ArrayRef< char > SrcBytes, std::string &Out)
Converts a stream of raw bytes assumed to be UTF16 into a UTF8 std::string.
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1841
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: DenseMapInfo.h:52
The minidump header is the first part of a minidump file.
Definition: Minidump.h:32
support::ulittle32_t NumberOfStreams
Definition: Minidump.h:40
support::ulittle32_t Version
Definition: Minidump.h:39
static constexpr uint32_t MagicSignature
Definition: Minidump.h:33
static constexpr uint16_t MagicVersion
Definition: Minidump.h:34
support::ulittle32_t Signature
Definition: Minidump.h:36
support::ulittle32_t StreamDirectoryRVA
Definition: Minidump.h:41
Specifies the location (and size) of various objects in the minidump file.
Definition: Minidump.h:59
support::ulittle32_t RVA
Definition: Minidump.h:61
support::ulittle32_t DataSize
Definition: Minidump.h:60