Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Object/Minidump.cpp
Line
Count
Source (jump to first uncovered line)
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"
11
#include "llvm/Support/ConvertUTF.h"
12
13
using namespace llvm;
14
using namespace llvm::object;
15
using namespace llvm::minidump;
16
17
Optional<ArrayRef<uint8_t>>
18
21
MinidumpFile::getRawStream(minidump::StreamType Type) const {
19
21
  auto It = StreamMap.find(Type);
20
21
  if (It != StreamMap.end())
21
19
    return getRawStream(Streams[It->second]);
22
2
  return None;
23
2
}
24
25
10
Expected<std::string> MinidumpFile::getString(size_t Offset) const {
26
10
  // Minidump strings consist of a 32-bit length field, which gives the size of
27
10
  // the string in *bytes*. This is followed by the actual string encoded in
28
10
  // UTF16.
29
10
  auto ExpectedSize =
30
10
      getDataSliceAs<support::ulittle32_t>(getData(), Offset, 1);
31
10
  if (!ExpectedSize)
32
1
    return ExpectedSize.takeError();
33
9
  size_t Size = (*ExpectedSize)[0];
34
9
  if (Size % 2 != 0)
35
1
    return createError("String size not even");
36
8
  Size /= 2;
37
8
  if (Size == 0)
38
1
    return "";
39
7
40
7
  Offset += sizeof(support::ulittle32_t);
41
7
  auto ExpectedData =
42
7
      getDataSliceAs<support::ulittle16_t>(getData(), Offset, Size);
43
7
  if (!ExpectedData)
44
1
    return ExpectedData.takeError();
45
6
46
6
  SmallVector<UTF16, 32> WStr(Size);
47
6
  copy(*ExpectedData, WStr.begin());
48
6
49
6
  std::string Result;
50
6
  if (!convertUTF16ToUTF8String(WStr, Result))
51
1
    return createError("String decoding failed");
52
5
53
5
  return Result;
54
5
}
55
56
template <typename T>
57
10
Expected<ArrayRef<T>> MinidumpFile::getListStream(StreamType Stream) const {
58
10
  auto OptionalStream = getRawStream(Stream);
59
10
  if (!OptionalStream)
60
0
    return createError("No such stream");
61
10
  auto ExpectedSize =
62
10
      getDataSliceAs<support::ulittle32_t>(*OptionalStream, 0, 1);
63
10
  if (!ExpectedSize)
64
0
    return ExpectedSize.takeError();
65
10
66
10
  size_t ListSize = ExpectedSize.get()[0];
67
10
68
10
  size_t ListOffset = 4;
69
10
  // Some producers insert additional padding bytes to align the list to an
70
10
  // 8-byte boundary. Check for that by comparing the list size with the overall
71
10
  // stream size.
72
10
  if (ListOffset + sizeof(T) * ListSize < OptionalStream->size())
73
3
    ListOffset = 8;
74
10
75
10
  return getDataSliceAs<T>(*OptionalStream, ListOffset, ListSize);
76
10
}
llvm::Expected<llvm::ArrayRef<llvm::minidump::Module> > llvm::object::MinidumpFile::getListStream<llvm::minidump::Module>(llvm::minidump::StreamType) const
Line
Count
Source
57
4
Expected<ArrayRef<T>> MinidumpFile::getListStream(StreamType Stream) const {
58
4
  auto OptionalStream = getRawStream(Stream);
59
4
  if (!OptionalStream)
60
0
    return createError("No such stream");
61
4
  auto ExpectedSize =
62
4
      getDataSliceAs<support::ulittle32_t>(*OptionalStream, 0, 1);
63
4
  if (!ExpectedSize)
64
0
    return ExpectedSize.takeError();
65
4
66
4
  size_t ListSize = ExpectedSize.get()[0];
67
4
68
4
  size_t ListOffset = 4;
69
4
  // Some producers insert additional padding bytes to align the list to an
70
4
  // 8-byte boundary. Check for that by comparing the list size with the overall
71
4
  // stream size.
72
4
  if (ListOffset + sizeof(T) * ListSize < OptionalStream->size())
73
1
    ListOffset = 8;
74
4
75
4
  return getDataSliceAs<T>(*OptionalStream, ListOffset, ListSize);
76
4
}
llvm::Expected<llvm::ArrayRef<llvm::minidump::Thread> > llvm::object::MinidumpFile::getListStream<llvm::minidump::Thread>(llvm::minidump::StreamType) const
Line
Count
Source
57
3
Expected<ArrayRef<T>> MinidumpFile::getListStream(StreamType Stream) const {
58
3
  auto OptionalStream = getRawStream(Stream);
59
3
  if (!OptionalStream)
60
0
    return createError("No such stream");
61
3
  auto ExpectedSize =
62
3
      getDataSliceAs<support::ulittle32_t>(*OptionalStream, 0, 1);
63
3
  if (!ExpectedSize)
64
0
    return ExpectedSize.takeError();
65
3
66
3
  size_t ListSize = ExpectedSize.get()[0];
67
3
68
3
  size_t ListOffset = 4;
69
3
  // Some producers insert additional padding bytes to align the list to an
70
3
  // 8-byte boundary. Check for that by comparing the list size with the overall
71
3
  // stream size.
72
3
  if (ListOffset + sizeof(T) * ListSize < OptionalStream->size())
73
1
    ListOffset = 8;
74
3
75
3
  return getDataSliceAs<T>(*OptionalStream, ListOffset, ListSize);
76
3
}
llvm::Expected<llvm::ArrayRef<llvm::minidump::MemoryDescriptor> > llvm::object::MinidumpFile::getListStream<llvm::minidump::MemoryDescriptor>(llvm::minidump::StreamType) const
Line
Count
Source
57
3
Expected<ArrayRef<T>> MinidumpFile::getListStream(StreamType Stream) const {
58
3
  auto OptionalStream = getRawStream(Stream);
59
3
  if (!OptionalStream)
60
0
    return createError("No such stream");
61
3
  auto ExpectedSize =
62
3
      getDataSliceAs<support::ulittle32_t>(*OptionalStream, 0, 1);
63
3
  if (!ExpectedSize)
64
0
    return ExpectedSize.takeError();
65
3
66
3
  size_t ListSize = ExpectedSize.get()[0];
67
3
68
3
  size_t ListOffset = 4;
69
3
  // Some producers insert additional padding bytes to align the list to an
70
3
  // 8-byte boundary. Check for that by comparing the list size with the overall
71
3
  // stream size.
72
3
  if (ListOffset + sizeof(T) * ListSize < OptionalStream->size())
73
1
    ListOffset = 8;
74
3
75
3
  return getDataSliceAs<T>(*OptionalStream, ListOffset, ListSize);
76
3
}
77
template Expected<ArrayRef<Module>>
78
    MinidumpFile::getListStream(StreamType) const;
79
template Expected<ArrayRef<Thread>>
80
    MinidumpFile::getListStream(StreamType) const;
81
template Expected<ArrayRef<MemoryDescriptor>>
82
    MinidumpFile::getListStream(StreamType) const;
83
84
Expected<ArrayRef<uint8_t>>
85
120
MinidumpFile::getDataSlice(ArrayRef<uint8_t> Data, size_t Offset, size_t Size) {
86
120
  // Check for overflow.
87
120
  if (Offset + Size < Offset || Offset + Size < Size ||
88
120
      Offset + Size > Data.size())
89
9
    return createEOFError();
90
111
  return Data.slice(Offset, Size);
91
111
}
92
93
Expected<std::unique_ptr<MinidumpFile>>
94
25
MinidumpFile::create(MemoryBufferRef Source) {
95
25
  ArrayRef<uint8_t> Data = arrayRefFromStringRef(Source.getBuffer());
96
25
  auto ExpectedHeader = getDataSliceAs<minidump::Header>(Data, 0, 1);
97
25
  if (!ExpectedHeader)
98
1
    return ExpectedHeader.takeError();
99
24
100
24
  const minidump::Header &Hdr = (*ExpectedHeader)[0];
101
24
  if (Hdr.Signature != Header::MagicSignature)
102
1
    return createError("Invalid signature");
103
23
  if ((Hdr.Version & 0xffff) != Header::MagicVersion)
104
1
    return createError("Invalid version");
105
22
106
22
  auto ExpectedStreams = getDataSliceAs<Directory>(Data, Hdr.StreamDirectoryRVA,
107
22
                                                   Hdr.NumberOfStreams);
108
22
  if (!ExpectedStreams)
109
2
    return ExpectedStreams.takeError();
110
20
111
20
  DenseMap<StreamType, std::size_t> StreamMap;
112
29
  for (const auto &Stream : llvm::enumerate(*ExpectedStreams)) {
113
29
    StreamType Type = Stream.value().Type;
114
29
    const LocationDescriptor &Loc = Stream.value().Location;
115
29
116
29
    auto ExpectedStream = getDataSlice(Data, Loc.RVA, Loc.DataSize);
117
29
    if (!ExpectedStream)
118
3
      return ExpectedStream.takeError();
119
26
120
26
    if (Type == StreamType::Unused && 
Loc.DataSize == 03
) {
121
3
      // Ignore dummy streams. This is technically ill-formed, but a number of
122
3
      // existing minidumps seem to contain such streams.
123
3
      continue;
124
3
    }
125
23
126
23
    if (Type == DenseMapInfo<StreamType>::getEmptyKey() ||
127
23
        
Type == DenseMapInfo<StreamType>::getTombstoneKey()22
)
128
1
      return createError("Cannot handle one of the minidump streams");
129
22
130
22
    // Update the directory map, checking for duplicate stream types.
131
22
    if (!StreamMap.try_emplace(Type, Stream.index()).second)
132
0
      return createError("Duplicate stream type");
133
22
  }
134
20
135
20
  return std::unique_ptr<MinidumpFile>(
136
16
      new MinidumpFile(Source, Hdr, *ExpectedStreams, std::move(StreamMap)));
137
20
}