LLVM  10.0.0svn
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 
19  auto It = StreamMap.find(Type);
20  if (It != StreamMap.end())
21  return getRawStream(Streams[It->second]);
22  return None;
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  Optional<ArrayRef<uint8_t>> Stream = getRawStream(StreamType::MemoryInfoList);
59  if (!Stream)
60  return createError("No such stream");
61  auto ExpectedHeader =
62  getDataSliceAs<minidump::MemoryInfoListHeader>(*Stream, 0, 1);
63  if (!ExpectedHeader)
64  return ExpectedHeader.takeError();
65  const minidump::MemoryInfoListHeader &H = ExpectedHeader.get()[0];
67  getDataSlice(*Stream, H.SizeOfHeader, H.SizeOfEntry * H.NumberOfEntries);
68  if (!Data)
69  return Data.takeError();
70  return make_range(MemoryInfoIterator(*Data, H.SizeOfEntry),
72 }
73 
74 template <typename T>
75 Expected<ArrayRef<T>> MinidumpFile::getListStream(StreamType Type) const {
76  Optional<ArrayRef<uint8_t>> Stream = getRawStream(Type);
77  if (!Stream)
78  return createError("No such stream");
79  auto ExpectedSize = getDataSliceAs<support::ulittle32_t>(*Stream, 0, 1);
80  if (!ExpectedSize)
81  return ExpectedSize.takeError();
82 
83  size_t ListSize = ExpectedSize.get()[0];
84 
85  size_t ListOffset = 4;
86  // Some producers insert additional padding bytes to align the list to an
87  // 8-byte boundary. Check for that by comparing the list size with the overall
88  // stream size.
89  if (ListOffset + sizeof(T) * ListSize < Stream->size())
90  ListOffset = 8;
91 
92  return getDataSliceAs<T>(*Stream, ListOffset, ListSize);
93 }
95  MinidumpFile::getListStream(StreamType) const;
97  MinidumpFile::getListStream(StreamType) const;
99  MinidumpFile::getListStream(StreamType) const;
100 
102 MinidumpFile::getDataSlice(ArrayRef<uint8_t> Data, size_t Offset, size_t Size) {
103  // Check for overflow.
104  if (Offset + Size < Offset || Offset + Size < Size ||
105  Offset + Size > Data.size())
106  return createEOFError();
107  return Data.slice(Offset, Size);
108 }
109 
113  auto ExpectedHeader = getDataSliceAs<minidump::Header>(Data, 0, 1);
114  if (!ExpectedHeader)
115  return ExpectedHeader.takeError();
116 
117  const minidump::Header &Hdr = (*ExpectedHeader)[0];
118  if (Hdr.Signature != Header::MagicSignature)
119  return createError("Invalid signature");
120  if ((Hdr.Version & 0xffff) != Header::MagicVersion)
121  return createError("Invalid version");
122 
123  auto ExpectedStreams = getDataSliceAs<Directory>(Data, Hdr.StreamDirectoryRVA,
124  Hdr.NumberOfStreams);
125  if (!ExpectedStreams)
126  return ExpectedStreams.takeError();
127 
129  for (const auto &StreamDescriptor : llvm::enumerate(*ExpectedStreams)) {
130  StreamType Type = StreamDescriptor.value().Type;
131  const LocationDescriptor &Loc = StreamDescriptor.value().Location;
132 
134  getDataSlice(Data, Loc.RVA, Loc.DataSize);
135  if (!Stream)
136  return Stream.takeError();
137 
138  if (Type == StreamType::Unused && Loc.DataSize == 0) {
139  // Ignore dummy streams. This is technically ill-formed, but a number of
140  // existing minidumps seem to contain such streams.
141  continue;
142  }
143 
146  return createError("Cannot handle one of the minidump streams");
147 
148  // Update the directory map, checking for duplicate stream types.
149  if (!StreamMap.try_emplace(Type, StreamDescriptor.index()).second)
150  return createError("Duplicate stream type");
151  }
152 
153  return std::unique_ptr<MinidumpFile>(
154  new MinidumpFile(Source, Hdr, *ExpectedStreams, std::move(StreamMap)));
155 }
const NoneType None
Definition: None.h:23
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Specifies the location (and size) of various objects in the minidump file.
Definition: Minidump.h:59
support::ulittle32_t NumberOfStreams
Definition: Minidump.h:40
support::ulittle32_t Signature
Definition: Minidump.h:36
ArrayRef< uint8_t > arrayRefFromStringRef(StringRef Input)
Construct a string ref from an array ref of unsigned chars.
Definition: StringExtras.h:60
unsigned second
Error takeError()
Take ownership of the stored error.
Definition: Error.h:552
Expected< std::string > getString(size_t Offset) const
Returns the minidump string at the given offset.
Definition: Minidump.cpp:25
StreamType
The type of a minidump stream identifies its contents.
Definition: Minidump.h:50
StringRef getBuffer() const
Definition: MemoryBuffer.h:272
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
The minidump header is the first part of a minidump file.
Definition: Minidump.h:32
support::ulittle32_t StreamDirectoryRVA
Definition: Minidump.h:41
support::ulittle32_t SizeOfEntry
Definition: Minidump.h:75
static Error createError(const Twine &Err)
Definition: ELF.h:47
Type(LLVMContext &C, TypeID tid)
Definition: Type.h:91
static Expected< std::unique_ptr< MinidumpFile > > create(MemoryBufferRef Source)
Construct a new MinidumpFile object from the given memory buffer.
Definition: Minidump.cpp:111
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
detail::packed_endian_specific_integral< uint32_t, little, unaligned > ulittle32_t
Definition: Endian.h:274
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
#define H(x, y, z)
Definition: MD5.cpp:57
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
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
support::ulittle32_t Version
Definition: Minidump.h:39
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1146
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
Expected< iterator_range< MemoryInfoIterator > > getMemoryInfoList() const
Returns the list of descriptors embedded in the MemoryInfoList stream.
Definition: Minidump.cpp:57
support::ulittle32_t RVA
Definition: Minidump.h:61
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array...
Definition: ArrayRef.h:178
bool convertUTF16ToUTF8String(ArrayRef< char > SrcBytes, std::string &Out)
Converts a stream of raw bytes assumed to be UTF16 into a UTF8 std::string.
support::ulittle32_t DataSize
Definition: Minidump.h:60
support::ulittle64_t NumberOfEntries
Definition: Minidump.h:76
uint32_t Size
Definition: Profile.cpp:46
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1217
support::ulittle32_t SizeOfHeader
Definition: Minidump.h:74
A class providing access to the contents of a minidump file.
Definition: Minidump.h:23
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:1500