LLVM 22.0.0git
Minidump.h
Go to the documentation of this file.
1//===- Minidump.h - Minidump object file implementation ---------*- C++ -*-===//
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#ifndef LLVM_OBJECT_MINIDUMP_H
10#define LLVM_OBJECT_MINIDUMP_H
11
12#include "llvm/ADT/DenseMap.h"
15#include "llvm/ADT/iterator.h"
17#include "llvm/Object/Binary.h"
19#include "llvm/Support/Error.h"
20
21namespace llvm {
22namespace object {
23
24/// A class providing access to the contents of a minidump file.
25class MinidumpFile : public Binary {
26public:
27 /// Construct a new MinidumpFile object from the given memory buffer. Returns
28 /// an error if this file cannot be identified as a minidump file, or if its
29 /// contents are badly corrupted (i.e. we cannot read the stream directory).
31 create(MemoryBufferRef Source);
32
33 static bool classof(const Binary *B) { return B->isMinidump(); }
34
35 /// Returns the contents of the minidump header.
36 const minidump::Header &header() const { return Header; }
37
38 /// Returns the list of streams (stream directory entries) in this file.
39 ArrayRef<minidump::Directory> streams() const { return Streams; }
40
41 /// Returns the raw contents of the stream given by the directory entry.
43 return getData().slice(Stream.Location.RVA, Stream.Location.DataSize);
44 }
45
46 /// Returns the raw contents of the stream of the given type, or std::nullopt
47 /// if the file does not contain a stream of this type.
48 LLVM_ABI std::optional<ArrayRef<uint8_t>>
50
51 /// Returns the raw contents of an object given by the LocationDescriptor. An
52 /// error is returned if the descriptor points outside of the minidump file.
55 return getDataSlice(getData(), Desc.RVA, Desc.DataSize);
56 }
57
58 /// Returns the minidump string at the given offset. An error is returned if
59 /// we fail to parse the string, or the string is invalid UTF16.
61
62 /// Returns the contents of the SystemInfo stream, cast to the appropriate
63 /// type. An error is returned if the file does not contain this stream, or
64 /// the stream is smaller than the size of the SystemInfo structure. The
65 /// internal consistency of the stream is not checked in any way.
67 return getStream<minidump::SystemInfo>(minidump::StreamType::SystemInfo);
68 }
69
70 /// Returns the module list embedded in the ModuleList stream. An error is
71 /// returned if the file does not contain this stream, or if the stream is
72 /// not large enough to contain the number of modules declared in the stream
73 /// header. The consistency of the Module entries themselves is not checked in
74 /// any way.
76 return getListStream<minidump::Module>(minidump::StreamType::ModuleList);
77 }
78
79 /// Returns the thread list embedded in the ThreadList stream. An error is
80 /// returned if the file does not contain this stream, or if the stream is
81 /// not large enough to contain the number of threads declared in the stream
82 /// header. The consistency of the Thread entries themselves is not checked in
83 /// any way.
85 return getListStream<minidump::Thread>(minidump::StreamType::ThreadList);
86 }
87
88 /// Returns the contents of the Exception stream. An error is returned if the
89 /// associated stream is smaller than the size of the ExceptionStream
90 /// structure. Or the directory supplied is not of kind exception stream.
93 if (Directory.Type != minidump::StreamType::Exception) {
94 return createError("Not an exception stream");
95 }
96
97 return getStreamFromDirectory<minidump::ExceptionStream>(Directory);
98 }
99
100 /// Returns the first exception stream in the file. An error is returned if
101 /// the associated stream is smaller than the size of the ExceptionStream
102 /// structure. Or the directory supplied is not of kind exception stream.
104 auto it = getExceptionStreams();
105 if (it.begin() == it.end())
106 return createError("No exception streams");
107 return *it.begin();
108 }
109
110 /// Returns the list of descriptors embedded in the MemoryList stream. The
111 /// descriptors provide the content of interesting regions of memory at the
112 /// time the minidump was taken. An error is returned if the file does not
113 /// contain this stream, or if the stream is not large enough to contain the
114 /// number of memory descriptors declared in the stream header. The
115 /// consistency of the MemoryDescriptor entries themselves is not checked in
116 /// any way.
118 return getListStream<minidump::MemoryDescriptor>(
119 minidump::StreamType::MemoryList);
120 }
121
122 /// Returns the header to the memory 64 list stream. An error is returned if
123 /// the file does not contain this stream.
125 return getStream<minidump::Memory64ListHeader>(
126 minidump::StreamType::Memory64List);
127 }
128
130 : public iterator_facade_base<MemoryInfoIterator,
131 std::forward_iterator_tag,
132 minidump::MemoryInfo> {
133 public:
134 MemoryInfoIterator(ArrayRef<uint8_t> Storage, size_t Stride)
135 : Storage(Storage), Stride(Stride) {
136 assert(Storage.size() % Stride == 0);
137 }
138
139 bool operator==(const MemoryInfoIterator &R) const {
140 return Storage.size() == R.Storage.size();
141 }
142
144 assert(Storage.size() >= sizeof(minidump::MemoryInfo));
145 return *reinterpret_cast<const minidump::MemoryInfo *>(Storage.data());
146 }
147
149 Storage = Storage.drop_front(Stride);
150 return *this;
151 }
152
153 private:
154 ArrayRef<uint8_t> Storage;
155 size_t Stride;
156 };
157
158 /// Class the provides an iterator over the memory64 memory ranges. Only the
159 /// the first descriptor is validated as readable beforehand.
161 public:
162 static Memory64Iterator
165 return Memory64Iterator(Storage, Descriptors);
166 }
167
168 static Memory64Iterator end() { return Memory64Iterator(); }
169
170 bool operator==(const Memory64Iterator &R) const {
171 return IsEnd == R.IsEnd;
172 }
173
174 bool operator!=(const Memory64Iterator &R) const { return !(*this == R); }
175
176 const std::pair<minidump::MemoryDescriptor_64, ArrayRef<uint8_t>> &
178 return Current;
179 }
180
181 const std::pair<minidump::MemoryDescriptor_64, ArrayRef<uint8_t>> *
183 return &Current;
184 }
185
187 if (Descriptors.empty()) {
188 IsEnd = true;
189 return Error::success();
190 }
191
192 // Drop front gives us an array ref, so we need to call .front() as well.
193 const minidump::MemoryDescriptor_64 &Descriptor = Descriptors.front();
194 if (Descriptor.DataSize > Storage.size()) {
195 IsEnd = true;
196 return make_error<GenericBinaryError>(
197 "Memory64 Descriptor exceeds end of file.",
199 }
200
201 ArrayRef<uint8_t> Content = Storage.take_front(Descriptor.DataSize);
202 Current = std::make_pair(Descriptor, Content);
203
204 Storage = Storage.drop_front(Descriptor.DataSize);
205 Descriptors = Descriptors.drop_front();
206
207 return Error::success();
208 }
209
210 private:
211 // This constructor expects that the first descriptor is readable.
214 : Storage(Storage), Descriptors(Descriptors), IsEnd(false) {
215 assert(!Descriptors.empty() &&
216 Storage.size() >= Descriptors.front().DataSize);
217 minidump::MemoryDescriptor_64 Descriptor = Descriptors.front();
218 ArrayRef<uint8_t> Content = Storage.take_front(Descriptor.DataSize);
219 Current = std::make_pair(Descriptor, Content);
220 this->Descriptors = Descriptors.drop_front();
221 this->Storage = Storage.drop_front(Descriptor.DataSize);
222 }
223
224 Memory64Iterator()
225 : Storage(ArrayRef<uint8_t>()),
226 Descriptors(ArrayRef<minidump::MemoryDescriptor_64>()), IsEnd(true) {}
227
228 std::pair<minidump::MemoryDescriptor_64, ArrayRef<uint8_t>> Current;
229 ArrayRef<uint8_t> Storage;
231 bool IsEnd;
232 };
233
235 public:
237 const MinidumpFile *File)
238 : Streams(Streams), File(File) {}
239
241 return Streams.size() == R.Streams.size();
242 }
243
245 return !(*this == R);
246 }
247
249 return File->getExceptionStream(Streams.front());
250 }
251
253 if (!Streams.empty())
254 Streams = Streams.drop_front();
255
256 return *this;
257 }
258
259 private:
261 const MinidumpFile *File;
262 };
263
265
266 /// Returns an iterator that reads each exception stream independently. The
267 /// contents of the exception strema are not validated before being read, an
268 /// error will be returned if the stream is not large enough to contain an
269 /// exception stream, or if the stream points beyond the end of the file.
271
272 /// Returns an iterator that pairs each descriptor with it's respective
273 /// content from the Memory64List stream. An error is returned if the file
274 /// does not contain a Memory64List stream, or if the descriptor data is
275 /// unreadable.
277 getMemory64List(Error &Err) const;
278
279 /// Returns the list of descriptors embedded in the MemoryInfoList stream. The
280 /// descriptors provide properties (e.g. permissions) of interesting regions
281 /// of memory at the time the minidump was taken. An error is returned if the
282 /// file does not contain this stream, or if the stream is not large enough to
283 /// contain the number of memory descriptors declared in the stream header.
284 /// The consistency of the MemoryInfoList entries themselves is not checked
285 /// in any way.
287 getMemoryInfoList() const;
288
289private:
290 static Error createError(StringRef Str) {
291 return make_error<GenericBinaryError>(Str, object_error::parse_failed);
292 }
293
294 static Error createEOFError() {
295 return make_error<GenericBinaryError>("Unexpected EOF",
297 }
298
299 /// Return a slice of the given data array, with bounds checking.
302
303 /// Return the slice of the given data array as an array of objects of the
304 /// given type. The function checks that the input array is large enough to
305 /// contain the correct number of objects of the given type.
306 template <typename T>
307 static Expected<ArrayRef<T>> getDataSliceAs(ArrayRef<uint8_t> Data,
308 uint64_t Offset, uint64_t Count);
309
310 MinidumpFile(MemoryBufferRef Source, const minidump::Header &Header,
313 std::vector<minidump::Directory> ExceptionStreams)
314 : Binary(ID_Minidump, Source), Header(Header), Streams(Streams),
315 StreamMap(std::move(StreamMap)),
316 ExceptionStreams(std::move(ExceptionStreams)) {}
317
318 ArrayRef<uint8_t> getData() const {
319 return arrayRefFromStringRef(Data.getBuffer());
320 }
321
322 /// Return the stream of the given type, cast to the appropriate type. Checks
323 /// that the stream is large enough to hold an object of this type.
324 template <typename T>
325 Expected<const T &>
326 getStreamFromDirectory(minidump::Directory Directory) const;
327
328 /// Return the stream of the given type, cast to the appropriate type. Checks
329 /// that the stream is large enough to hold an object of this type.
330 template <typename T>
331 Expected<const T &> getStream(minidump::StreamType Stream) const;
332
333 /// Return the contents of a stream which contains a list of fixed-size items,
334 /// prefixed by the list size.
335 template <typename T>
336 Expected<ArrayRef<T>> getListStream(minidump::StreamType Stream) const;
337
338 const minidump::Header &Header;
339 ArrayRef<minidump::Directory> Streams;
340 DenseMap<minidump::StreamType, std::size_t> StreamMap;
341 std::vector<minidump::Directory> ExceptionStreams;
342};
343
344template <typename T>
345Expected<const T &>
346MinidumpFile::getStreamFromDirectory(minidump::Directory Directory) const {
347 ArrayRef<uint8_t> Stream = getRawStream(Directory);
348 if (Stream.size() >= sizeof(T))
349 return *reinterpret_cast<const T *>(Stream.data());
350 return createEOFError();
351}
352
353template <typename T>
354Expected<const T &> MinidumpFile::getStream(minidump::StreamType Type) const {
355 if (std::optional<ArrayRef<uint8_t>> Stream = getRawStream(Type)) {
356 if (Stream->size() >= sizeof(T))
357 return *reinterpret_cast<const T *>(Stream->data());
358 return createEOFError();
359 }
360 return createError("No such stream");
361}
362
363template <typename T>
364Expected<ArrayRef<T>> MinidumpFile::getDataSliceAs(ArrayRef<uint8_t> Data,
366 uint64_t Count) {
367 // Check for overflow.
368 if (Count > std::numeric_limits<uint64_t>::max() / sizeof(T))
369 return createEOFError();
370 Expected<ArrayRef<uint8_t>> Slice =
371 getDataSlice(Data, Offset, sizeof(T) * Count);
372 if (!Slice)
373 return Slice.takeError();
374
375 return ArrayRef<T>(reinterpret_cast<const T *>(Slice->data()), Count);
376}
377
378template <typename T>
379Expected<ArrayRef<T>>
380MinidumpFile::getListStream(minidump::StreamType Type) const {
381 std::optional<ArrayRef<uint8_t>> Stream = getRawStream(Type);
382 if (!Stream)
383 return createError("No such stream");
384 auto ExpectedSize = getDataSliceAs<support::ulittle32_t>(*Stream, 0, 1);
385 if (!ExpectedSize)
386 return ExpectedSize.takeError();
387
388 size_t ListSize = ExpectedSize.get()[0];
389
390 size_t ListOffset = 4;
391 // Some producers insert additional padding bytes to align the list to an
392 // 8-byte boundary. Check for that by comparing the list size with the overall
393 // stream size.
394 if (ListOffset + sizeof(T) * ListSize < Stream->size())
395 ListOffset = 8;
396
397 return getDataSliceAs<T>(*Stream, ListOffset, ListSize);
398}
399
400} // end namespace object
401} // end namespace llvm
402
403#endif // LLVM_OBJECT_MINIDUMP_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition: Compiler.h:213
This file defines the DenseMap class.
T Content
uint64_t Size
This file contains some functions that are useful when dealing with strings.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition: ArrayRef.h:224
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:200
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:150
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:147
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:142
const T * data() const
Definition: ArrayRef.h:144
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition: ArrayRef.h:191
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
static ErrorSuccess success()
Create a success value.
Definition: Error.h:336
Tagged union holding either a T or a Error.
Definition: Error.h:485
StringRef getBuffer() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
A wrapper class for fallible iterators.
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:80
A range adaptor for a pair of iterators.
MemoryBufferRef Data
Definition: Binary.h:38
bool operator!=(const ExceptionStreamsIterator &R) const
Definition: Minidump.h:244
Expected< const minidump::ExceptionStream & > operator*()
Definition: Minidump.h:248
bool operator==(const ExceptionStreamsIterator &R) const
Definition: Minidump.h:240
ExceptionStreamsIterator(ArrayRef< minidump::Directory > Streams, const MinidumpFile *File)
Definition: Minidump.h:236
Class the provides an iterator over the memory64 memory ranges.
Definition: Minidump.h:160
bool operator==(const Memory64Iterator &R) const
Definition: Minidump.h:170
static Memory64Iterator begin(ArrayRef< uint8_t > Storage, ArrayRef< minidump::MemoryDescriptor_64 > Descriptors)
Definition: Minidump.h:163
bool operator!=(const Memory64Iterator &R) const
Definition: Minidump.h:174
const std::pair< minidump::MemoryDescriptor_64, ArrayRef< uint8_t > > & operator*()
Definition: Minidump.h:177
const std::pair< minidump::MemoryDescriptor_64, ArrayRef< uint8_t > > * operator->()
Definition: Minidump.h:182
bool operator==(const MemoryInfoIterator &R) const
Definition: Minidump.h:139
MemoryInfoIterator(ArrayRef< uint8_t > Storage, size_t Stride)
Definition: Minidump.h:134
const minidump::MemoryInfo & operator*() const
Definition: Minidump.h:143
A class providing access to the contents of a minidump file.
Definition: Minidump.h:25
LLVM_ABI Expected< iterator_range< MemoryInfoIterator > > getMemoryInfoList() const
Returns the list of descriptors embedded in the MemoryInfoList stream.
Definition: Minidump.cpp:62
Expected< const minidump::ExceptionStream & > getExceptionStream(minidump::Directory Directory) const
Returns the contents of the Exception stream.
Definition: Minidump.h:92
Expected< minidump::Memory64ListHeader > getMemoryList64Header() const
Returns the header to the memory 64 list stream.
Definition: Minidump.h:124
ArrayRef< uint8_t > getRawStream(const minidump::Directory &Stream) const
Returns the raw contents of the stream given by the directory entry.
Definition: Minidump.h:42
Expected< const minidump::ExceptionStream & > getExceptionStream() const
Returns the first exception stream in the file.
Definition: Minidump.h:103
const minidump::Header & header() const
Returns the contents of the minidump header.
Definition: Minidump.h:36
Expected< const minidump::SystemInfo & > getSystemInfo() const
Returns the contents of the SystemInfo stream, cast to the appropriate type.
Definition: Minidump.h:66
Expected< ArrayRef< uint8_t > > getRawData(minidump::LocationDescriptor Desc) const
Returns the raw contents of an object given by the LocationDescriptor.
Definition: Minidump.h:54
LLVM_ABI iterator_range< ExceptionStreamsIterator > getExceptionStreams() const
Returns an iterator that reads each exception stream independently.
Definition: Minidump.cpp:56
static bool classof(const Binary *B)
Definition: Minidump.h:33
LLVM_ABI Expected< std::string > getString(size_t Offset) const
Returns the minidump string at the given offset.
Definition: Minidump.cpp:24
Expected< ArrayRef< minidump::MemoryDescriptor > > getMemoryList() const
Returns the list of descriptors embedded in the MemoryList stream.
Definition: Minidump.h:117
Expected< ArrayRef< minidump::Module > > getModuleList() const
Returns the module list embedded in the ModuleList stream.
Definition: Minidump.h:75
Expected< ArrayRef< minidump::Thread > > getThreadList() const
Returns the thread list embedded in the ThreadList stream.
Definition: Minidump.h:84
ArrayRef< minidump::Directory > streams() const
Returns the list of streams (stream directory entries) in this file.
Definition: Minidump.h:39
LLVM_ABI 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 LLVM_ABI 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
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
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:1702
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1886
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
Description of the encoding of one expression Op.
Specifies the location and type of a single stream in the minidump file.
Definition: Minidump.h:137
LocationDescriptor Location
Definition: Minidump.h:139
support::little_t< StreamType > Type
Definition: Minidump.h:138
The minidump header is the first part of a minidump file.
Definition: Minidump.h:32
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
support::ulittle64_t DataSize
Definition: Minidump.h:75