Minidump.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Minidump.h - Minidump object file implementation ---------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_OBJECT_MINIDUMP_H
  14. #define LLVM_OBJECT_MINIDUMP_H
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/StringExtras.h"
  17. #include "llvm/ADT/iterator.h"
  18. #include "llvm/BinaryFormat/Minidump.h"
  19. #include "llvm/Object/Binary.h"
  20. #include "llvm/Support/Error.h"
  21. namespace llvm {
  22. namespace object {
  23. /// A class providing access to the contents of a minidump file.
  24. class MinidumpFile : public Binary {
  25. public:
  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).
  29. static Expected<std::unique_ptr<MinidumpFile>> create(MemoryBufferRef Source);
  30. static bool classof(const Binary *B) { return B->isMinidump(); }
  31. /// Returns the contents of the minidump header.
  32. const minidump::Header &header() const { return Header; }
  33. /// Returns the list of streams (stream directory entries) in this file.
  34. ArrayRef<minidump::Directory> streams() const { return Streams; }
  35. /// Returns the raw contents of the stream given by the directory entry.
  36. ArrayRef<uint8_t> getRawStream(const minidump::Directory &Stream) const {
  37. return getData().slice(Stream.Location.RVA, Stream.Location.DataSize);
  38. }
  39. /// Returns the raw contents of the stream of the given type, or std::nullopt
  40. /// if the file does not contain a stream of this type.
  41. std::optional<ArrayRef<uint8_t>>
  42. getRawStream(minidump::StreamType Type) const;
  43. /// Returns the raw contents of an object given by the LocationDescriptor. An
  44. /// error is returned if the descriptor points outside of the minidump file.
  45. Expected<ArrayRef<uint8_t>>
  46. getRawData(minidump::LocationDescriptor Desc) const {
  47. return getDataSlice(getData(), Desc.RVA, Desc.DataSize);
  48. }
  49. /// Returns the minidump string at the given offset. An error is returned if
  50. /// we fail to parse the string, or the string is invalid UTF16.
  51. Expected<std::string> getString(size_t Offset) const;
  52. /// Returns the contents of the SystemInfo stream, cast to the appropriate
  53. /// type. An error is returned if the file does not contain this stream, or
  54. /// the stream is smaller than the size of the SystemInfo structure. The
  55. /// internal consistency of the stream is not checked in any way.
  56. Expected<const minidump::SystemInfo &> getSystemInfo() const {
  57. return getStream<minidump::SystemInfo>(minidump::StreamType::SystemInfo);
  58. }
  59. /// Returns the module list embedded in the ModuleList stream. An error is
  60. /// returned if the file does not contain this stream, or if the stream is
  61. /// not large enough to contain the number of modules declared in the stream
  62. /// header. The consistency of the Module entries themselves is not checked in
  63. /// any way.
  64. Expected<ArrayRef<minidump::Module>> getModuleList() const {
  65. return getListStream<minidump::Module>(minidump::StreamType::ModuleList);
  66. }
  67. /// Returns the thread list embedded in the ThreadList stream. An error is
  68. /// returned if the file does not contain this stream, or if the stream is
  69. /// not large enough to contain the number of threads declared in the stream
  70. /// header. The consistency of the Thread entries themselves is not checked in
  71. /// any way.
  72. Expected<ArrayRef<minidump::Thread>> getThreadList() const {
  73. return getListStream<minidump::Thread>(minidump::StreamType::ThreadList);
  74. }
  75. /// Returns the contents of the Exception stream. An error is returned if the
  76. /// file does not contain this stream, or the stream is smaller than the size
  77. /// of the ExceptionStream structure. The internal consistency of the stream
  78. /// is not checked in any way.
  79. Expected<const minidump::ExceptionStream &> getExceptionStream() const {
  80. return getStream<minidump::ExceptionStream>(
  81. minidump::StreamType::Exception);
  82. }
  83. /// Returns the list of descriptors embedded in the MemoryList stream. The
  84. /// descriptors provide the content of interesting regions of memory at the
  85. /// time the minidump was taken. An error is returned if the file does not
  86. /// contain this stream, or if the stream is not large enough to contain the
  87. /// number of memory descriptors declared in the stream header. The
  88. /// consistency of the MemoryDescriptor entries themselves is not checked in
  89. /// any way.
  90. Expected<ArrayRef<minidump::MemoryDescriptor>> getMemoryList() const {
  91. return getListStream<minidump::MemoryDescriptor>(
  92. minidump::StreamType::MemoryList);
  93. }
  94. class MemoryInfoIterator
  95. : public iterator_facade_base<MemoryInfoIterator,
  96. std::forward_iterator_tag,
  97. minidump::MemoryInfo> {
  98. public:
  99. MemoryInfoIterator(ArrayRef<uint8_t> Storage, size_t Stride)
  100. : Storage(Storage), Stride(Stride) {
  101. assert(Storage.size() % Stride == 0);
  102. }
  103. bool operator==(const MemoryInfoIterator &R) const {
  104. return Storage.size() == R.Storage.size();
  105. }
  106. const minidump::MemoryInfo &operator*() const {
  107. assert(Storage.size() >= sizeof(minidump::MemoryInfo));
  108. return *reinterpret_cast<const minidump::MemoryInfo *>(Storage.data());
  109. }
  110. MemoryInfoIterator &operator++() {
  111. Storage = Storage.drop_front(Stride);
  112. return *this;
  113. }
  114. private:
  115. ArrayRef<uint8_t> Storage;
  116. size_t Stride;
  117. };
  118. /// Returns the list of descriptors embedded in the MemoryInfoList stream. The
  119. /// descriptors provide properties (e.g. permissions) of interesting regions
  120. /// of memory at the time the minidump was taken. An error is returned if the
  121. /// file does not contain this stream, or if the stream is not large enough to
  122. /// contain the number of memory descriptors declared in the stream header.
  123. /// The consistency of the MemoryInfoList entries themselves is not checked
  124. /// in any way.
  125. Expected<iterator_range<MemoryInfoIterator>> getMemoryInfoList() const;
  126. private:
  127. static Error createError(StringRef Str) {
  128. return make_error<GenericBinaryError>(Str, object_error::parse_failed);
  129. }
  130. static Error createEOFError() {
  131. return make_error<GenericBinaryError>("Unexpected EOF",
  132. object_error::unexpected_eof);
  133. }
  134. /// Return a slice of the given data array, with bounds checking.
  135. static Expected<ArrayRef<uint8_t>> getDataSlice(ArrayRef<uint8_t> Data,
  136. size_t Offset, size_t Size);
  137. /// Return the slice of the given data array as an array of objects of the
  138. /// given type. The function checks that the input array is large enough to
  139. /// contain the correct number of objects of the given type.
  140. template <typename T>
  141. static Expected<ArrayRef<T>> getDataSliceAs(ArrayRef<uint8_t> Data,
  142. size_t Offset, size_t Count);
  143. MinidumpFile(MemoryBufferRef Source, const minidump::Header &Header,
  144. ArrayRef<minidump::Directory> Streams,
  145. DenseMap<minidump::StreamType, std::size_t> StreamMap)
  146. : Binary(ID_Minidump, Source), Header(Header), Streams(Streams),
  147. StreamMap(std::move(StreamMap)) {}
  148. ArrayRef<uint8_t> getData() const {
  149. return arrayRefFromStringRef(Data.getBuffer());
  150. }
  151. /// Return the stream of the given type, cast to the appropriate type. Checks
  152. /// that the stream is large enough to hold an object of this type.
  153. template <typename T>
  154. Expected<const T &> getStream(minidump::StreamType Stream) const;
  155. /// Return the contents of a stream which contains a list of fixed-size items,
  156. /// prefixed by the list size.
  157. template <typename T>
  158. Expected<ArrayRef<T>> getListStream(minidump::StreamType Stream) const;
  159. const minidump::Header &Header;
  160. ArrayRef<minidump::Directory> Streams;
  161. DenseMap<minidump::StreamType, std::size_t> StreamMap;
  162. };
  163. template <typename T>
  164. Expected<const T &> MinidumpFile::getStream(minidump::StreamType Type) const {
  165. if (std::optional<ArrayRef<uint8_t>> Stream = getRawStream(Type)) {
  166. if (Stream->size() >= sizeof(T))
  167. return *reinterpret_cast<const T *>(Stream->data());
  168. return createEOFError();
  169. }
  170. return createError("No such stream");
  171. }
  172. template <typename T>
  173. Expected<ArrayRef<T>> MinidumpFile::getDataSliceAs(ArrayRef<uint8_t> Data,
  174. size_t Offset,
  175. size_t Count) {
  176. // Check for overflow.
  177. if (Count > std::numeric_limits<size_t>::max() / sizeof(T))
  178. return createEOFError();
  179. Expected<ArrayRef<uint8_t>> Slice =
  180. getDataSlice(Data, Offset, sizeof(T) * Count);
  181. if (!Slice)
  182. return Slice.takeError();
  183. return ArrayRef<T>(reinterpret_cast<const T *>(Slice->data()), Count);
  184. }
  185. } // end namespace object
  186. } // end namespace llvm
  187. #endif // LLVM_OBJECT_MINIDUMP_H
  188. #ifdef __GNUC__
  189. #pragma GCC diagnostic pop
  190. #endif