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