Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:19

0001 //===- Minidump.h - Minidump object file implementation ---------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_OBJECT_MINIDUMP_H
0010 #define LLVM_OBJECT_MINIDUMP_H
0011 
0012 #include "llvm/ADT/DenseMap.h"
0013 #include "llvm/ADT/StringExtras.h"
0014 #include "llvm/ADT/fallible_iterator.h"
0015 #include "llvm/ADT/iterator.h"
0016 #include "llvm/BinaryFormat/Minidump.h"
0017 #include "llvm/Object/Binary.h"
0018 #include "llvm/Support/Error.h"
0019 
0020 namespace llvm {
0021 namespace object {
0022 
0023 /// A class providing access to the contents of a minidump file.
0024 class MinidumpFile : public Binary {
0025 public:
0026   /// Construct a new MinidumpFile object from the given memory buffer. Returns
0027   /// an error if this file cannot be identified as a minidump file, or if its
0028   /// contents are badly corrupted (i.e. we cannot read the stream directory).
0029   static Expected<std::unique_ptr<MinidumpFile>> create(MemoryBufferRef Source);
0030 
0031   static bool classof(const Binary *B) { return B->isMinidump(); }
0032 
0033   /// Returns the contents of the minidump header.
0034   const minidump::Header &header() const { return Header; }
0035 
0036   /// Returns the list of streams (stream directory entries) in this file.
0037   ArrayRef<minidump::Directory> streams() const { return Streams; }
0038 
0039   /// Returns the raw contents of the stream given by the directory entry.
0040   ArrayRef<uint8_t> getRawStream(const minidump::Directory &Stream) const {
0041     return getData().slice(Stream.Location.RVA, Stream.Location.DataSize);
0042   }
0043 
0044   /// Returns the raw contents of the stream of the given type, or std::nullopt
0045   /// if the file does not contain a stream of this type.
0046   std::optional<ArrayRef<uint8_t>>
0047   getRawStream(minidump::StreamType Type) const;
0048 
0049   /// Returns the raw contents of an object given by the LocationDescriptor. An
0050   /// error is returned if the descriptor points outside of the minidump file.
0051   Expected<ArrayRef<uint8_t>>
0052   getRawData(minidump::LocationDescriptor Desc) const {
0053     return getDataSlice(getData(), Desc.RVA, Desc.DataSize);
0054   }
0055 
0056   /// Returns the minidump string at the given offset. An error is returned if
0057   /// we fail to parse the string, or the string is invalid UTF16.
0058   Expected<std::string> getString(size_t Offset) const;
0059 
0060   /// Returns the contents of the SystemInfo stream, cast to the appropriate
0061   /// type. An error is returned if the file does not contain this stream, or
0062   /// the stream is smaller than the size of the SystemInfo structure. The
0063   /// internal consistency of the stream is not checked in any way.
0064   Expected<const minidump::SystemInfo &> getSystemInfo() const {
0065     return getStream<minidump::SystemInfo>(minidump::StreamType::SystemInfo);
0066   }
0067 
0068   /// Returns the module list embedded in the ModuleList stream. An error is
0069   /// returned if the file does not contain this stream, or if the stream is
0070   /// not large enough to contain the number of modules declared in the stream
0071   /// header. The consistency of the Module entries themselves is not checked in
0072   /// any way.
0073   Expected<ArrayRef<minidump::Module>> getModuleList() const {
0074     return getListStream<minidump::Module>(minidump::StreamType::ModuleList);
0075   }
0076 
0077   /// Returns the thread list embedded in the ThreadList stream. An error is
0078   /// returned if the file does not contain this stream, or if the stream is
0079   /// not large enough to contain the number of threads declared in the stream
0080   /// header. The consistency of the Thread entries themselves is not checked in
0081   /// any way.
0082   Expected<ArrayRef<minidump::Thread>> getThreadList() const {
0083     return getListStream<minidump::Thread>(minidump::StreamType::ThreadList);
0084   }
0085 
0086   /// Returns the contents of the Exception stream. An error is returned if the
0087   /// associated stream is smaller than the size of the ExceptionStream
0088   /// structure. Or the directory supplied is not of kind exception stream.
0089   Expected<const minidump::ExceptionStream &>
0090   getExceptionStream(minidump::Directory Directory) const {
0091     if (Directory.Type != minidump::StreamType::Exception) {
0092       return createError("Not an exception stream");
0093     }
0094 
0095     return getStreamFromDirectory<minidump::ExceptionStream>(Directory);
0096   }
0097 
0098   /// Returns the first exception stream in the file. An error is returned if
0099   /// the associated stream is smaller than the size of the ExceptionStream
0100   /// structure. Or the directory supplied is not of kind exception stream.
0101   Expected<const minidump::ExceptionStream &> getExceptionStream() const {
0102     auto it = getExceptionStreams();
0103     if (it.begin() == it.end())
0104       return createError("No exception streams");
0105     return *it.begin();
0106   }
0107 
0108   /// Returns the list of descriptors embedded in the MemoryList stream. The
0109   /// descriptors provide the content of interesting regions of memory at the
0110   /// time the minidump was taken. An error is returned if the file does not
0111   /// contain this stream, or if the stream is not large enough to contain the
0112   /// number of memory descriptors declared in the stream header. The
0113   /// consistency of the MemoryDescriptor entries themselves is not checked in
0114   /// any way.
0115   Expected<ArrayRef<minidump::MemoryDescriptor>> getMemoryList() const {
0116     return getListStream<minidump::MemoryDescriptor>(
0117         minidump::StreamType::MemoryList);
0118   }
0119 
0120   /// Returns the header to the memory 64 list stream. An error is returned if
0121   /// the file does not contain this stream.
0122   Expected<minidump::Memory64ListHeader> getMemoryList64Header() const {
0123     return getStream<minidump::Memory64ListHeader>(
0124         minidump::StreamType::Memory64List);
0125   }
0126 
0127   class MemoryInfoIterator
0128       : public iterator_facade_base<MemoryInfoIterator,
0129                                     std::forward_iterator_tag,
0130                                     minidump::MemoryInfo> {
0131   public:
0132     MemoryInfoIterator(ArrayRef<uint8_t> Storage, size_t Stride)
0133         : Storage(Storage), Stride(Stride) {
0134       assert(Storage.size() % Stride == 0);
0135     }
0136 
0137     bool operator==(const MemoryInfoIterator &R) const {
0138       return Storage.size() == R.Storage.size();
0139     }
0140 
0141     const minidump::MemoryInfo &operator*() const {
0142       assert(Storage.size() >= sizeof(minidump::MemoryInfo));
0143       return *reinterpret_cast<const minidump::MemoryInfo *>(Storage.data());
0144     }
0145 
0146     MemoryInfoIterator &operator++() {
0147       Storage = Storage.drop_front(Stride);
0148       return *this;
0149     }
0150 
0151   private:
0152     ArrayRef<uint8_t> Storage;
0153     size_t Stride;
0154   };
0155 
0156   /// Class the provides an iterator over the memory64 memory ranges. Only the
0157   /// the first descriptor is validated as readable beforehand.
0158   class Memory64Iterator {
0159   public:
0160     static Memory64Iterator
0161     begin(ArrayRef<uint8_t> Storage,
0162           ArrayRef<minidump::MemoryDescriptor_64> Descriptors) {
0163       return Memory64Iterator(Storage, Descriptors);
0164     }
0165 
0166     static Memory64Iterator end() { return Memory64Iterator(); }
0167 
0168     bool operator==(const Memory64Iterator &R) const {
0169       return IsEnd == R.IsEnd;
0170     }
0171 
0172     bool operator!=(const Memory64Iterator &R) const { return !(*this == R); }
0173 
0174     const std::pair<minidump::MemoryDescriptor_64, ArrayRef<uint8_t>> &
0175     operator*() {
0176       return Current;
0177     }
0178 
0179     const std::pair<minidump::MemoryDescriptor_64, ArrayRef<uint8_t>> *
0180     operator->() {
0181       return &Current;
0182     }
0183 
0184     Error inc() {
0185       if (Descriptors.empty()) {
0186         IsEnd = true;
0187         return Error::success();
0188       }
0189 
0190       // Drop front gives us an array ref, so we need to call .front() as well.
0191       const minidump::MemoryDescriptor_64 &Descriptor = Descriptors.front();
0192       if (Descriptor.DataSize > Storage.size()) {
0193         IsEnd = true;
0194         return make_error<GenericBinaryError>(
0195             "Memory64 Descriptor exceeds end of file.",
0196             object_error::unexpected_eof);
0197       }
0198 
0199       ArrayRef<uint8_t> Content = Storage.take_front(Descriptor.DataSize);
0200       Current = std::make_pair(Descriptor, Content);
0201 
0202       Storage = Storage.drop_front(Descriptor.DataSize);
0203       Descriptors = Descriptors.drop_front();
0204 
0205       return Error::success();
0206     }
0207 
0208   private:
0209     // This constructor expects that the first descriptor is readable.
0210     Memory64Iterator(ArrayRef<uint8_t> Storage,
0211                      ArrayRef<minidump::MemoryDescriptor_64> Descriptors)
0212         : Storage(Storage), Descriptors(Descriptors), IsEnd(false) {
0213       assert(!Descriptors.empty() &&
0214              Storage.size() >= Descriptors.front().DataSize);
0215       minidump::MemoryDescriptor_64 Descriptor = Descriptors.front();
0216       ArrayRef<uint8_t> Content = Storage.take_front(Descriptor.DataSize);
0217       Current = std::make_pair(Descriptor, Content);
0218       this->Descriptors = Descriptors.drop_front();
0219       this->Storage = Storage.drop_front(Descriptor.DataSize);
0220     }
0221 
0222     Memory64Iterator()
0223         : Storage(ArrayRef<uint8_t>()),
0224           Descriptors(ArrayRef<minidump::MemoryDescriptor_64>()), IsEnd(true) {}
0225 
0226     std::pair<minidump::MemoryDescriptor_64, ArrayRef<uint8_t>> Current;
0227     ArrayRef<uint8_t> Storage;
0228     ArrayRef<minidump::MemoryDescriptor_64> Descriptors;
0229     bool IsEnd;
0230   };
0231 
0232   class ExceptionStreamsIterator {
0233   public:
0234     ExceptionStreamsIterator(ArrayRef<minidump::Directory> Streams,
0235                              const MinidumpFile *File)
0236         : Streams(Streams), File(File) {}
0237 
0238     bool operator==(const ExceptionStreamsIterator &R) const {
0239       return Streams.size() == R.Streams.size();
0240     }
0241 
0242     bool operator!=(const ExceptionStreamsIterator &R) const {
0243       return !(*this == R);
0244     }
0245 
0246     Expected<const minidump::ExceptionStream &> operator*() {
0247       return File->getExceptionStream(Streams.front());
0248     }
0249 
0250     ExceptionStreamsIterator &operator++() {
0251       if (!Streams.empty())
0252         Streams = Streams.drop_front();
0253 
0254       return *this;
0255     }
0256 
0257   private:
0258     ArrayRef<minidump::Directory> Streams;
0259     const MinidumpFile *File;
0260   };
0261 
0262   using FallibleMemory64Iterator = llvm::fallible_iterator<Memory64Iterator>;
0263 
0264   /// Returns an iterator that reads each exception stream independently. The
0265   /// contents of the exception strema are not validated before being read, an
0266   /// error will be returned if the stream is not large enough to contain an
0267   /// exception stream, or if the stream points beyond the end of the file.
0268   iterator_range<ExceptionStreamsIterator> getExceptionStreams() const;
0269 
0270   /// Returns an iterator that pairs each descriptor with it's respective
0271   /// content from the Memory64List stream. An error is returned if the file
0272   /// does not contain a Memory64List stream, or if the descriptor data is
0273   /// unreadable.
0274   iterator_range<FallibleMemory64Iterator> getMemory64List(Error &Err) const;
0275 
0276   /// Returns the list of descriptors embedded in the MemoryInfoList stream. The
0277   /// descriptors provide properties (e.g. permissions) of interesting regions
0278   /// of memory at the time the minidump was taken. An error is returned if the
0279   /// file does not contain this stream, or if the stream is not large enough to
0280   /// contain the number of memory descriptors declared in the stream header.
0281   /// The consistency of the MemoryInfoList entries themselves is not checked
0282   /// in any way.
0283   Expected<iterator_range<MemoryInfoIterator>> getMemoryInfoList() const;
0284 
0285 private:
0286   static Error createError(StringRef Str) {
0287     return make_error<GenericBinaryError>(Str, object_error::parse_failed);
0288   }
0289 
0290   static Error createEOFError() {
0291     return make_error<GenericBinaryError>("Unexpected EOF",
0292                                           object_error::unexpected_eof);
0293   }
0294 
0295   /// Return a slice of the given data array, with bounds checking.
0296   static Expected<ArrayRef<uint8_t>>
0297   getDataSlice(ArrayRef<uint8_t> Data, uint64_t Offset, uint64_t Size);
0298 
0299   /// Return the slice of the given data array as an array of objects of the
0300   /// given type. The function checks that the input array is large enough to
0301   /// contain the correct number of objects of the given type.
0302   template <typename T>
0303   static Expected<ArrayRef<T>> getDataSliceAs(ArrayRef<uint8_t> Data,
0304                                               uint64_t Offset, uint64_t Count);
0305 
0306   MinidumpFile(MemoryBufferRef Source, const minidump::Header &Header,
0307                ArrayRef<minidump::Directory> Streams,
0308                DenseMap<minidump::StreamType, std::size_t> StreamMap,
0309                std::vector<minidump::Directory> ExceptionStreams)
0310       : Binary(ID_Minidump, Source), Header(Header), Streams(Streams),
0311         StreamMap(std::move(StreamMap)),
0312         ExceptionStreams(std::move(ExceptionStreams)) {}
0313 
0314   ArrayRef<uint8_t> getData() const {
0315     return arrayRefFromStringRef(Data.getBuffer());
0316   }
0317 
0318   /// Return the stream of the given type, cast to the appropriate type. Checks
0319   /// that the stream is large enough to hold an object of this type.
0320   template <typename T>
0321   Expected<const T &>
0322   getStreamFromDirectory(minidump::Directory Directory) const;
0323 
0324   /// Return the stream of the given type, cast to the appropriate type. Checks
0325   /// that the stream is large enough to hold an object of this type.
0326   template <typename T>
0327   Expected<const T &> getStream(minidump::StreamType Stream) const;
0328 
0329   /// Return the contents of a stream which contains a list of fixed-size items,
0330   /// prefixed by the list size.
0331   template <typename T>
0332   Expected<ArrayRef<T>> getListStream(minidump::StreamType Stream) const;
0333 
0334   const minidump::Header &Header;
0335   ArrayRef<minidump::Directory> Streams;
0336   DenseMap<minidump::StreamType, std::size_t> StreamMap;
0337   std::vector<minidump::Directory> ExceptionStreams;
0338 };
0339 
0340 template <typename T>
0341 Expected<const T &>
0342 MinidumpFile::getStreamFromDirectory(minidump::Directory Directory) const {
0343   ArrayRef<uint8_t> Stream = getRawStream(Directory);
0344   if (Stream.size() >= sizeof(T))
0345     return *reinterpret_cast<const T *>(Stream.data());
0346   return createEOFError();
0347 }
0348 
0349 template <typename T>
0350 Expected<const T &> MinidumpFile::getStream(minidump::StreamType Type) const {
0351   if (std::optional<ArrayRef<uint8_t>> Stream = getRawStream(Type)) {
0352     if (Stream->size() >= sizeof(T))
0353       return *reinterpret_cast<const T *>(Stream->data());
0354     return createEOFError();
0355   }
0356   return createError("No such stream");
0357 }
0358 
0359 template <typename T>
0360 Expected<ArrayRef<T>> MinidumpFile::getDataSliceAs(ArrayRef<uint8_t> Data,
0361                                                    uint64_t Offset,
0362                                                    uint64_t Count) {
0363   // Check for overflow.
0364   if (Count > std::numeric_limits<uint64_t>::max() / sizeof(T))
0365     return createEOFError();
0366   Expected<ArrayRef<uint8_t>> Slice =
0367       getDataSlice(Data, Offset, sizeof(T) * Count);
0368   if (!Slice)
0369     return Slice.takeError();
0370 
0371   return ArrayRef<T>(reinterpret_cast<const T *>(Slice->data()), Count);
0372 }
0373 
0374 template <typename T>
0375 Expected<ArrayRef<T>>
0376 MinidumpFile::getListStream(minidump::StreamType Type) const {
0377   std::optional<ArrayRef<uint8_t>> Stream = getRawStream(Type);
0378   if (!Stream)
0379     return createError("No such stream");
0380   auto ExpectedSize = getDataSliceAs<support::ulittle32_t>(*Stream, 0, 1);
0381   if (!ExpectedSize)
0382     return ExpectedSize.takeError();
0383 
0384   size_t ListSize = ExpectedSize.get()[0];
0385 
0386   size_t ListOffset = 4;
0387   // Some producers insert additional padding bytes to align the list to an
0388   // 8-byte boundary. Check for that by comparing the list size with the overall
0389   // stream size.
0390   if (ListOffset + sizeof(T) * ListSize < Stream->size())
0391     ListOffset = 8;
0392 
0393   return getDataSliceAs<T>(*Stream, ListOffset, ListSize);
0394 }
0395 
0396 } // end namespace object
0397 } // end namespace llvm
0398 
0399 #endif // LLVM_OBJECT_MINIDUMP_H