Minidump.h 9.0 KB

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