xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Object/Minidump.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/ADT/fallible_iterator.h"
15 #include "llvm/ADT/iterator.h"
16 #include "llvm/BinaryFormat/Minidump.h"
17 #include "llvm/Object/Binary.h"
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/Error.h"
20 
21 namespace llvm {
22 namespace object {
23 
24 /// A class providing access to the contents of a minidump file.
25 class MinidumpFile : public Binary {
26 public:
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).
30   LLVM_ABI static Expected<std::unique_ptr<MinidumpFile>>
31   create(MemoryBufferRef Source);
32 
classof(const Binary * B)33   static bool classof(const Binary *B) { return B->isMinidump(); }
34 
35   /// Returns the contents of the minidump header.
header()36   const minidump::Header &header() const { return Header; }
37 
38   /// Returns the list of streams (stream directory entries) in this file.
streams()39   ArrayRef<minidump::Directory> streams() const { return Streams; }
40 
41   /// Returns the raw contents of the stream given by the directory entry.
getRawStream(const minidump::Directory & Stream)42   ArrayRef<uint8_t> getRawStream(const minidump::Directory &Stream) const {
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>>
49   getRawStream(minidump::StreamType Type) const;
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.
53   Expected<ArrayRef<uint8_t>>
getRawData(minidump::LocationDescriptor Desc)54   getRawData(minidump::LocationDescriptor Desc) const {
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.
60   LLVM_ABI Expected<std::string> getString(size_t Offset) const;
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.
getSystemInfo()66   Expected<const minidump::SystemInfo &> getSystemInfo() const {
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.
getModuleList()75   Expected<ArrayRef<minidump::Module>> getModuleList() const {
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.
getThreadList()84   Expected<ArrayRef<minidump::Thread>> getThreadList() const {
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.
91   Expected<const minidump::ExceptionStream &>
getExceptionStream(minidump::Directory Directory)92   getExceptionStream(minidump::Directory Directory) const {
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.
getExceptionStream()103   Expected<const minidump::ExceptionStream &> getExceptionStream() const {
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.
getMemoryList()117   Expected<ArrayRef<minidump::MemoryDescriptor>> getMemoryList() const {
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.
getMemoryList64Header()124   Expected<minidump::Memory64ListHeader> getMemoryList64Header() const {
125     return getStream<minidump::Memory64ListHeader>(
126         minidump::StreamType::Memory64List);
127   }
128 
129   class MemoryInfoIterator
130       : public iterator_facade_base<MemoryInfoIterator,
131                                     std::forward_iterator_tag,
132                                     minidump::MemoryInfo> {
133   public:
MemoryInfoIterator(ArrayRef<uint8_t> Storage,size_t Stride)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 
143     const minidump::MemoryInfo &operator*() const {
144       assert(Storage.size() >= sizeof(minidump::MemoryInfo));
145       return *reinterpret_cast<const minidump::MemoryInfo *>(Storage.data());
146     }
147 
148     MemoryInfoIterator &operator++() {
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.
160   class Memory64Iterator {
161   public:
162     static Memory64Iterator
begin(ArrayRef<uint8_t> Storage,ArrayRef<minidump::MemoryDescriptor_64> Descriptors)163     begin(ArrayRef<uint8_t> Storage,
164           ArrayRef<minidump::MemoryDescriptor_64> Descriptors) {
165       return Memory64Iterator(Storage, Descriptors);
166     }
167 
end()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>> &
177     operator*() {
178       return Current;
179     }
180 
181     const std::pair<minidump::MemoryDescriptor_64, ArrayRef<uint8_t>> *
182     operator->() {
183       return &Current;
184     }
185 
inc()186     Error inc() {
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.",
198             object_error::unexpected_eof);
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.
Memory64Iterator(ArrayRef<uint8_t> Storage,ArrayRef<minidump::MemoryDescriptor_64> Descriptors)212     Memory64Iterator(ArrayRef<uint8_t> Storage,
213                      ArrayRef<minidump::MemoryDescriptor_64> Descriptors)
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 
Memory64Iterator()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;
230     ArrayRef<minidump::MemoryDescriptor_64> Descriptors;
231     bool IsEnd;
232   };
233 
234   class ExceptionStreamsIterator {
235   public:
ExceptionStreamsIterator(ArrayRef<minidump::Directory> Streams,const MinidumpFile * File)236     ExceptionStreamsIterator(ArrayRef<minidump::Directory> Streams,
237                              const MinidumpFile *File)
238         : Streams(Streams), File(File) {}
239 
240     bool operator==(const ExceptionStreamsIterator &R) const {
241       return Streams.size() == R.Streams.size();
242     }
243 
244     bool operator!=(const ExceptionStreamsIterator &R) const {
245       return !(*this == R);
246     }
247 
248     Expected<const minidump::ExceptionStream &> operator*() {
249       return File->getExceptionStream(Streams.front());
250     }
251 
252     ExceptionStreamsIterator &operator++() {
253       if (!Streams.empty())
254         Streams = Streams.drop_front();
255 
256       return *this;
257     }
258 
259   private:
260     ArrayRef<minidump::Directory> Streams;
261     const MinidumpFile *File;
262   };
263 
264   using FallibleMemory64Iterator = llvm::fallible_iterator<Memory64Iterator>;
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.
270   LLVM_ABI iterator_range<ExceptionStreamsIterator> getExceptionStreams() const;
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.
276   LLVM_ABI iterator_range<FallibleMemory64Iterator>
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.
286   LLVM_ABI Expected<iterator_range<MemoryInfoIterator>>
287   getMemoryInfoList() const;
288 
289 private:
createError(StringRef Str)290   static Error createError(StringRef Str) {
291     return make_error<GenericBinaryError>(Str, object_error::parse_failed);
292   }
293 
createEOFError()294   static Error createEOFError() {
295     return make_error<GenericBinaryError>("Unexpected EOF",
296                                           object_error::unexpected_eof);
297   }
298 
299   /// Return a slice of the given data array, with bounds checking.
300   LLVM_ABI static Expected<ArrayRef<uint8_t>>
301   getDataSlice(ArrayRef<uint8_t> Data, uint64_t Offset, uint64_t Size);
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 
MinidumpFile(MemoryBufferRef Source,const minidump::Header & Header,ArrayRef<minidump::Directory> Streams,DenseMap<minidump::StreamType,std::size_t> StreamMap,std::vector<minidump::Directory> ExceptionStreams)310   MinidumpFile(MemoryBufferRef Source, const minidump::Header &Header,
311                ArrayRef<minidump::Directory> Streams,
312                DenseMap<minidump::StreamType, std::size_t> StreamMap,
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 
getData()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 
344 template <typename T>
345 Expected<const T &>
getStreamFromDirectory(minidump::Directory Directory)346 MinidumpFile::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 
353 template <typename T>
getStream(minidump::StreamType Type)354 Expected<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 
363 template <typename T>
getDataSliceAs(ArrayRef<uint8_t> Data,uint64_t Offset,uint64_t Count)364 Expected<ArrayRef<T>> MinidumpFile::getDataSliceAs(ArrayRef<uint8_t> Data,
365                                                    uint64_t Offset,
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 
378 template <typename T>
379 Expected<ArrayRef<T>>
getListStream(minidump::StreamType Type)380 MinidumpFile::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
404