Minidump.h 9.1 KB

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