LLVM  17.0.0git
Minidump.cpp
Go to the documentation of this file.
1 //===- Minidump.cpp - Minidump object file implementation -----------------===//
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 #include "llvm/Object/Minidump.h"
10 #include "llvm/Object/Error.h"
12 
13 using namespace llvm;
14 using namespace llvm::object;
15 using namespace llvm::minidump;
16 
17 std::optional<ArrayRef<uint8_t>>
19  auto It = StreamMap.find(Type);
20  if (It != StreamMap.end())
21  return getRawStream(Streams[It->second]);
22  return std::nullopt;
23 }
24 
26  // Minidump strings consist of a 32-bit length field, which gives the size of
27  // the string in *bytes*. This is followed by the actual string encoded in
28  // UTF16.
29  auto ExpectedSize =
30  getDataSliceAs<support::ulittle32_t>(getData(), Offset, 1);
31  if (!ExpectedSize)
32  return ExpectedSize.takeError();
33  size_t Size = (*ExpectedSize)[0];
34  if (Size % 2 != 0)
35  return createError("String size not even");
36  Size /= 2;
37  if (Size == 0)
38  return "";
39 
40  Offset += sizeof(support::ulittle32_t);
41  auto ExpectedData =
42  getDataSliceAs<support::ulittle16_t>(getData(), Offset, Size);
43  if (!ExpectedData)
44  return ExpectedData.takeError();
45 
46  SmallVector<UTF16, 32> WStr(Size);
47  copy(*ExpectedData, WStr.begin());
48 
49  std::string Result;
50  if (!convertUTF16ToUTF8String(WStr, Result))
51  return createError("String decoding failed");
52 
53  return Result;
54 }
55 
58  std::optional<ArrayRef<uint8_t>> Stream =
59  getRawStream(StreamType::MemoryInfoList);
60  if (!Stream)
61  return createError("No such stream");
62  auto ExpectedHeader =
63  getDataSliceAs<minidump::MemoryInfoListHeader>(*Stream, 0, 1);
64  if (!ExpectedHeader)
65  return ExpectedHeader.takeError();
66  const minidump::MemoryInfoListHeader &H = ExpectedHeader.get()[0];
68  getDataSlice(*Stream, H.SizeOfHeader, H.SizeOfEntry * H.NumberOfEntries);
69  if (!Data)
70  return Data.takeError();
71  return make_range(MemoryInfoIterator(*Data, H.SizeOfEntry),
72  MemoryInfoIterator({}, H.SizeOfEntry));
73 }
74 
75 template <typename T>
76 Expected<ArrayRef<T>> MinidumpFile::getListStream(StreamType Type) const {
77  std::optional<ArrayRef<uint8_t>> Stream = getRawStream(Type);
78  if (!Stream)
79  return createError("No such stream");
80  auto ExpectedSize = getDataSliceAs<support::ulittle32_t>(*Stream, 0, 1);
81  if (!ExpectedSize)
82  return ExpectedSize.takeError();
83 
84  size_t ListSize = ExpectedSize.get()[0];
85 
86  size_t ListOffset = 4;
87  // Some producers insert additional padding bytes to align the list to an
88  // 8-byte boundary. Check for that by comparing the list size with the overall
89  // stream size.
90  if (ListOffset + sizeof(T) * ListSize < Stream->size())
91  ListOffset = 8;
92 
93  return getDataSliceAs<T>(*Stream, ListOffset, ListSize);
94 }
96  MinidumpFile::getListStream(StreamType) const;
98  MinidumpFile::getListStream(StreamType) const;
100  MinidumpFile::getListStream(StreamType) const;
101 
103 MinidumpFile::getDataSlice(ArrayRef<uint8_t> Data, size_t Offset, size_t Size) {
104  // Check for overflow.
105  if (Offset + Size < Offset || Offset + Size < Size ||
106  Offset + Size > Data.size())
107  return createEOFError();
108  return Data.slice(Offset, Size);
109 }
110 
113  ArrayRef<uint8_t> Data = arrayRefFromStringRef(Source.getBuffer());
114  auto ExpectedHeader = getDataSliceAs<minidump::Header>(Data, 0, 1);
115  if (!ExpectedHeader)
116  return ExpectedHeader.takeError();
117 
118  const minidump::Header &Hdr = (*ExpectedHeader)[0];
119  if (Hdr.Signature != Header::MagicSignature)
120  return createError("Invalid signature");
121  if ((Hdr.Version & 0xffff) != Header::MagicVersion)
122  return createError("Invalid version");
123 
124  auto ExpectedStreams = getDataSliceAs<Directory>(Data, Hdr.StreamDirectoryRVA,
125  Hdr.NumberOfStreams);
126  if (!ExpectedStreams)
127  return ExpectedStreams.takeError();
128 
130  for (const auto &StreamDescriptor : llvm::enumerate(*ExpectedStreams)) {
131  StreamType Type = StreamDescriptor.value().Type;
132  const LocationDescriptor &Loc = StreamDescriptor.value().Location;
133 
135  getDataSlice(Data, Loc.RVA, Loc.DataSize);
136  if (!Stream)
137  return Stream.takeError();
138 
139  if (Type == StreamType::Unused && Loc.DataSize == 0) {
140  // Ignore dummy streams. This is technically ill-formed, but a number of
141  // existing minidumps seem to contain such streams.
142  continue;
143  }
144 
147  return createError("Cannot handle one of the minidump streams");
148 
149  // Update the directory map, checking for duplicate stream types.
150  if (!StreamMap.try_emplace(Type, StreamDescriptor.index()).second)
151  return createError("Duplicate stream type");
152  }
153 
154  return std::unique_ptr<MinidumpFile>(
155  new MinidumpFile(Source, Hdr, *ExpectedStreams, std::move(StreamMap)));
156 }
llvm::minidump::MemoryInfoListHeader
Definition: Minidump.h:73
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::make_range
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Definition: iterator_range.h:53
T
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
llvm::enumerate
detail::enumerator< R > enumerate(R &&TheRange)
Given an input range, returns a new range whose values are are pair (A,B) such that A is the 0-based ...
Definition: STLExtras.h:2264
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::copy
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1837
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::minidump::Header::Signature
support::ulittle32_t Signature
Definition: Minidump.h:36
llvm::minidump::LocationDescriptor
Specifies the location (and size) of various objects in the minidump file.
Definition: Minidump.h:59
llvm::minidump::LocationDescriptor::DataSize
support::ulittle32_t DataSize
Definition: Minidump.h:60
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::object::MinidumpFile::getString
Expected< std::string > getString(size_t Offset) const
Returns the minidump string at the given offset.
Definition: Minidump.cpp:25
llvm::DenseMapInfo
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: APInt.h:34
llvm::object
Definition: DWARFDebugLoc.h:24
Error.h
llvm::minidump::Header
The minidump header is the first part of a minidump file.
Definition: Minidump.h:32
llvm::convertUTF16ToUTF8String
bool convertUTF16ToUTF8String(ArrayRef< char > SrcBytes, std::string &Out)
Converts a stream of raw bytes assumed to be UTF16 into a UTF8 std::string.
Definition: ConvertUTFWrapper.cpp:84
llvm::object::MinidumpFile::create
static Expected< std::unique_ptr< MinidumpFile > > create(MemoryBufferRef Source)
Construct a new MinidumpFile object from the given memory buffer.
Definition: Minidump.cpp:112
llvm::support::ulittle32_t
detail::packed_endian_specific_integral< uint32_t, little, unaligned > ulittle32_t
Definition: Endian.h:272
llvm::minidump::StreamType
StreamType
The type of a minidump stream identifies its contents.
Definition: Minidump.h:50
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::DenseMap
Definition: DenseMap.h:714
llvm::size
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1716
llvm::Type::Type
Type(LLVMContext &C, TypeID tid)
Definition: Type.h:94
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:100
llvm::ArrayRef< uint8_t >
llvm::minidump
Definition: Minidump.h:26
ConvertUTF.h
H
#define H(x, y, z)
Definition: MD5.cpp:57
llvm::object::MinidumpFile::getRawStream
ArrayRef< uint8_t > getRawStream(const minidump::Directory &Stream) const
Returns the raw contents of the stream given by the directory entry.
Definition: Minidump.h:39
llvm::minidump::Header::Version
support::ulittle32_t Version
Definition: Minidump.h:39
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
Minidump.h
llvm::object::MinidumpFile
A class providing access to the contents of a minidump file.
Definition: Minidump.h:23
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::try_emplace
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&... Args)
Definition: DenseMap.h:222
llvm::object::createError
Error createError(const Twine &Err)
Definition: Error.h:84
llvm::object::MinidumpFile::MemoryInfoIterator
Definition: Minidump.h:106
llvm::minidump::LocationDescriptor::RVA
support::ulittle32_t RVA
Definition: Minidump.h:61
llvm::object::MinidumpFile::getMemoryInfoList
Expected< iterator_range< MemoryInfoIterator > > getMemoryInfoList() const
Returns the list of descriptors embedded in the MemoryInfoList stream.
Definition: Minidump.cpp:57
llvm::minidump::Header::StreamDirectoryRVA
support::ulittle32_t StreamDirectoryRVA
Definition: Minidump.h:41
llvm::minidump::Header::NumberOfStreams
support::ulittle32_t NumberOfStreams
Definition: Minidump.h:40