Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/DebugInfo/CodeView/CodeViewRecordIO.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- CodeViewRecordIO.cpp -------------------------------------*- C++ -*-===//
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/DebugInfo/CodeView/CodeViewRecordIO.h"
10
#include "llvm/DebugInfo/CodeView/CodeView.h"
11
#include "llvm/DebugInfo/CodeView/RecordSerialization.h"
12
#include "llvm/Support/BinaryStreamReader.h"
13
#include "llvm/Support/BinaryStreamWriter.h"
14
15
using namespace llvm;
16
using namespace llvm::codeview;
17
18
96.8k
Error CodeViewRecordIO::beginRecord(Optional<uint32_t> MaxLength) {
19
96.8k
  RecordLimit Limit;
20
96.8k
  Limit.MaxLength = MaxLength;
21
96.8k
  Limit.BeginOffset = getCurrentOffset();
22
96.8k
  Limits.push_back(Limit);
23
96.8k
  return Error::success();
24
96.8k
}
25
26
96.8k
Error CodeViewRecordIO::endRecord() {
27
96.8k
  assert(!Limits.empty() && "Not in a record!");
28
96.8k
  Limits.pop_back();
29
96.8k
  // We would like to assert that we actually read / wrote all the bytes that we
30
96.8k
  // expected to for this record, but unfortunately we can't do this.  Some
31
96.8k
  // producers such as MASM over-allocate for certain types of records and
32
96.8k
  // commit the extraneous data, so when reading we can't be sure every byte
33
96.8k
  // will have been read.  And when writing we over-allocate temporarily since
34
96.8k
  // we don't know how big the record is until we're finished writing it, so
35
96.8k
  // even though we don't commit the extraneous data, we still can't guarantee
36
96.8k
  // we're at the end of the allocated data.
37
96.8k
38
96.8k
  if (isStreaming()) {
39
14.9k
    // For streaming mode, add padding to align with 4 byte boundaries for each
40
14.9k
    // record
41
14.9k
    uint32_t Align = getStreamedLen() % 4;
42
14.9k
    if (Align == 0)
43
11.5k
      return Error::success();
44
3.40k
45
3.40k
    int PaddingBytes = 4 - Align;
46
8.48k
    while (PaddingBytes > 0) {
47
5.07k
      char Pad = static_cast<uint8_t>(LF_PAD0 + PaddingBytes);
48
5.07k
      StringRef BytesSR = StringRef(&Pad, sizeof(Pad));
49
5.07k
      Streamer->EmitBytes(BytesSR);
50
5.07k
      --PaddingBytes;
51
5.07k
    }
52
3.40k
    resetStreamedLen();
53
3.40k
  }
54
96.8k
  
return Error::success()85.2k
;
55
96.8k
}
56
57
37.1k
uint32_t CodeViewRecordIO::maxFieldLength() const {
58
37.1k
  if (isStreaming())
59
0
    return 0;
60
37.1k
61
37.1k
  assert(!Limits.empty() && "Not in a record!");
62
37.1k
63
37.1k
  // The max length of the next field is the minimum of all lengths that would
64
37.1k
  // be allowed by any of the sub-records we're in.  In practice, we can only
65
37.1k
  // ever be at most 1 sub-record deep (in a FieldList), but this works for
66
37.1k
  // the general case.
67
37.1k
  uint32_t Offset = getCurrentOffset();
68
37.1k
  Optional<uint32_t> Min = Limits.front().bytesRemaining(Offset);
69
37.1k
  for (auto X : makeArrayRef(Limits).drop_front()) {
70
12.4k
    Optional<uint32_t> ThisMin = X.bytesRemaining(Offset);
71
12.4k
    if (ThisMin.hasValue())
72
12.4k
      Min = (Min.hasValue()) ? 
std::min(*Min, *ThisMin)0
: *ThisMin;
73
12.4k
  }
74
37.1k
  assert(Min.hasValue() && "Every field must have a maximum length!");
75
37.1k
76
37.1k
  return *Min;
77
37.1k
}
78
79
5.80k
Error CodeViewRecordIO::padToAlignment(uint32_t Align) {
80
5.80k
  if (isReading())
81
4.11k
    return Reader->padToAlignment(Align);
82
1.69k
  return Writer->padToAlignment(Align);
83
1.69k
}
84
85
34.0k
Error CodeViewRecordIO::skipPadding() {
86
34.0k
  assert(!isWriting() && "Cannot skip padding while writing!");
87
34.0k
88
34.0k
  if (Reader->bytesRemaining() == 0)
89
573
    return Error::success();
90
33.4k
91
33.4k
  uint8_t Leaf = Reader->peek();
92
33.4k
  if (Leaf < LF_PAD0)
93
24.6k
    return Error::success();
94
8.83k
  // Leaf is greater than 0xf0. We should advance by the number of bytes in
95
8.83k
  // the low 4 bits.
96
8.83k
  unsigned BytesToAdvance = Leaf & 0x0F;
97
8.83k
  return Reader->skip(BytesToAdvance);
98
8.83k
}
99
100
Error CodeViewRecordIO::mapByteVectorTail(ArrayRef<uint8_t> &Bytes,
101
1.93k
                                          const Twine &Comment) {
102
1.93k
  if (isStreaming()) {
103
0
    emitComment(Comment);
104
0
    Streamer->EmitBinaryData(toStringRef(Bytes));
105
0
    incrStreamedLen(Bytes.size());
106
1.93k
  } else if (isWriting()) {
107
10
    if (auto EC = Writer->writeBytes(Bytes))
108
0
      return EC;
109
1.92k
  } else {
110
1.92k
    if (auto EC = Reader->readBytes(Bytes, Reader->bytesRemaining()))
111
0
      return EC;
112
1.93k
  }
113
1.93k
  return Error::success();
114
1.93k
}
115
116
Error CodeViewRecordIO::mapByteVectorTail(std::vector<uint8_t> &Bytes,
117
40
                                          const Twine &Comment) {
118
40
  ArrayRef<uint8_t> BytesRef(Bytes);
119
40
  if (auto EC = mapByteVectorTail(BytesRef, Comment))
120
0
    return EC;
121
40
  if (!isWriting())
122
39
    Bytes.assign(BytesRef.begin(), BytesRef.end());
123
40
124
40
  return Error::success();
125
40
}
126
127
69.9k
Error CodeViewRecordIO::mapInteger(TypeIndex &TypeInd, const Twine &Comment) {
128
69.9k
  if (isStreaming()) {
129
6.22k
    emitComment(Comment);
130
6.22k
    Streamer->EmitIntValue(TypeInd.getIndex(), sizeof(TypeInd.getIndex()));
131
6.22k
    incrStreamedLen(sizeof(TypeInd.getIndex()));
132
63.7k
  } else if (isWriting()) {
133
9.94k
    if (auto EC = Writer->writeInteger(TypeInd.getIndex()))
134
0
      return EC;
135
53.7k
  } else {
136
53.7k
    uint32_t I;
137
53.7k
    if (auto EC = Reader->readInteger(I))
138
0
      return EC;
139
53.7k
    TypeInd.setIndex(I);
140
53.7k
  }
141
69.9k
  return Error::success();
142
69.9k
}
143
144
Error CodeViewRecordIO::mapEncodedInteger(int64_t &Value,
145
0
                                          const Twine &Comment) {
146
0
  if (isStreaming()) {
147
0
    if (Value >= 0)
148
0
      emitEncodedUnsignedInteger(static_cast<uint64_t>(Value), Comment);
149
0
    else
150
0
      emitEncodedSignedInteger(Value, Comment);
151
0
  } else if (isWriting()) {
152
0
    if (Value >= 0) {
153
0
      if (auto EC = writeEncodedUnsignedInteger(static_cast<uint64_t>(Value)))
154
0
        return EC;
155
0
    } else {
156
0
      if (auto EC = writeEncodedSignedInteger(Value))
157
0
        return EC;
158
0
    }
159
0
  } else {
160
0
    APSInt N;
161
0
    if (auto EC = consume(*Reader, N))
162
0
      return EC;
163
0
    Value = N.getExtValue();
164
0
  }
165
0
166
0
  return Error::success();
167
0
}
168
169
Error CodeViewRecordIO::mapEncodedInteger(uint64_t &Value,
170
10.6k
                                          const Twine &Comment) {
171
10.6k
  if (isStreaming())
172
719
    emitEncodedUnsignedInteger(Value, Comment);
173
9.89k
  else if (isWriting()) {
174
1.13k
    if (auto EC = writeEncodedUnsignedInteger(Value))
175
0
      return EC;
176
8.75k
  } else {
177
8.75k
    APSInt N;
178
8.75k
    if (auto EC = consume(*Reader, N))
179
0
      return EC;
180
8.75k
    Value = N.getZExtValue();
181
8.75k
  }
182
10.6k
  return Error::success();
183
10.6k
}
184
185
53.2k
Error CodeViewRecordIO::mapEncodedInteger(APSInt &Value, const Twine &Comment) {
186
53.2k
  if (isStreaming()) {
187
11.4k
    if (Value.isSigned())
188
0
      emitEncodedSignedInteger(Value.getSExtValue(), Comment);
189
11.4k
    else
190
11.4k
      emitEncodedUnsignedInteger(Value.getZExtValue(), Comment);
191
41.8k
  } else if (isWriting()) {
192
11.8k
    if (Value.isSigned())
193
0
      return writeEncodedSignedInteger(Value.getSExtValue());
194
11.8k
    return writeEncodedUnsignedInteger(Value.getZExtValue());
195
11.8k
  } else
196
30.0k
    return consume(*Reader, Value);
197
11.4k
  return Error::success();
198
11.4k
}
199
200
84.7k
Error CodeViewRecordIO::mapStringZ(StringRef &Value, const Twine &Comment) {
201
84.7k
  if (isStreaming()) {
202
13.5k
    auto NullTerminatedString = StringRef(Value.data(), Value.size() + 1);
203
13.5k
    emitComment(Comment);
204
13.5k
    Streamer->EmitBytes(NullTerminatedString);
205
13.5k
    incrStreamedLen(NullTerminatedString.size());
206
71.2k
  } else if (isWriting()) {
207
17.4k
    // Truncate if we attempt to write too much.
208
17.4k
    StringRef S = Value.take_front(maxFieldLength() - 1);
209
17.4k
    if (auto EC = Writer->writeCString(S))
210
0
      return EC;
211
53.7k
  } else {
212
53.7k
    if (auto EC = Reader->readCString(Value))
213
0
      return EC;
214
84.7k
  }
215
84.7k
  return Error::success();
216
84.7k
}
217
218
29
Error CodeViewRecordIO::mapGuid(GUID &Guid, const Twine &Comment) {
219
29
  constexpr uint32_t GuidSize = 16;
220
29
221
29
  if (isStreaming()) {
222
0
    StringRef GuidSR =
223
0
        StringRef((reinterpret_cast<const char *>(&Guid)), GuidSize);
224
0
    emitComment(Comment);
225
0
    Streamer->EmitBytes(GuidSR);
226
0
    incrStreamedLen(GuidSize);
227
0
    return Error::success();
228
0
  }
229
29
230
29
  if (maxFieldLength() < GuidSize)
231
0
    return make_error<CodeViewError>(cv_error_code::insufficient_buffer);
232
29
233
29
  if (isWriting()) {
234
11
    if (auto EC = Writer->writeBytes(Guid.Guid))
235
0
      return EC;
236
18
  } else {
237
18
    ArrayRef<uint8_t> GuidBytes;
238
18
    if (auto EC = Reader->readBytes(GuidBytes, GuidSize))
239
0
      return EC;
240
18
    memcpy(Guid.Guid, GuidBytes.data(), GuidSize);
241
18
  }
242
29
  return Error::success();
243
29
}
244
245
Error CodeViewRecordIO::mapStringZVectorZ(std::vector<StringRef> &Value,
246
137
                                          const Twine &Comment) {
247
137
248
137
  if (!isReading()) {
249
111
    emitComment(Comment);
250
886
    for (auto V : Value) {
251
886
      if (auto EC = mapStringZ(V))
252
0
        return EC;
253
886
    }
254
111
    uint8_t FinalZero = 0;
255
111
    if (auto EC = mapInteger(FinalZero))
256
0
      return EC;
257
26
  } else {
258
26
    StringRef S;
259
26
    if (auto EC = mapStringZ(S))
260
0
      return EC;
261
220
    
while (26
!S.empty()) {
262
194
      Value.push_back(S);
263
194
      if (auto EC = mapStringZ(S))
264
0
        return EC;
265
194
    };
266
26
  }
267
137
  return Error::success();
268
137
}
269
270
void CodeViewRecordIO::emitEncodedSignedInteger(const int64_t &Value,
271
0
                                                const Twine &Comment) {
272
0
  assert(Value < 0 && "Encoded integer is not signed!");
273
0
  if (Value >= std::numeric_limits<int8_t>::min()) {
274
0
    Streamer->EmitIntValue(LF_CHAR, 2);
275
0
    emitComment(Comment);
276
0
    Streamer->EmitIntValue(Value, 1);
277
0
    incrStreamedLen(3);
278
0
  } else if (Value >= std::numeric_limits<int16_t>::min()) {
279
0
    Streamer->EmitIntValue(LF_SHORT, 2);
280
0
    emitComment(Comment);
281
0
    Streamer->EmitIntValue(Value, 2);
282
0
    incrStreamedLen(4);
283
0
  } else if (Value >= std::numeric_limits<int32_t>::min()) {
284
0
    Streamer->EmitIntValue(LF_LONG, 2);
285
0
    emitComment(Comment);
286
0
    Streamer->EmitIntValue(Value, 4);
287
0
    incrStreamedLen(6);
288
0
  } else {
289
0
    Streamer->EmitIntValue(LF_QUADWORD, 2);
290
0
    emitComment(Comment);
291
0
    Streamer->EmitIntValue(Value, 4);
292
0
    incrStreamedLen(6);
293
0
  }
294
0
}
295
296
void CodeViewRecordIO::emitEncodedUnsignedInteger(const uint64_t &Value,
297
12.1k
                                                  const Twine &Comment) {
298
12.1k
  if (Value < LF_NUMERIC) {
299
12.1k
    emitComment(Comment);
300
12.1k
    Streamer->EmitIntValue(Value, 2);
301
12.1k
    incrStreamedLen(2);
302
12.1k
  } else 
if (4
Value <= std::numeric_limits<uint16_t>::max()4
) {
303
0
    Streamer->EmitIntValue(LF_USHORT, 2);
304
0
    emitComment(Comment);
305
0
    Streamer->EmitIntValue(Value, 2);
306
0
    incrStreamedLen(4);
307
4
  } else if (Value <= std::numeric_limits<uint32_t>::max()) {
308
2
    Streamer->EmitIntValue(LF_ULONG, 2);
309
2
    emitComment(Comment);
310
2
    Streamer->EmitIntValue(Value, 4);
311
2
    incrStreamedLen(6);
312
2
  } else {
313
2
    Streamer->EmitIntValue(LF_UQUADWORD, 2);
314
2
    emitComment(Comment);
315
2
    Streamer->EmitIntValue(Value, 8);
316
2
    incrStreamedLen(6);
317
2
  }
318
12.1k
}
319
320
0
Error CodeViewRecordIO::writeEncodedSignedInteger(const int64_t &Value) {
321
0
  assert(Value < 0 && "Encoded integer is not signed!");
322
0
  if (Value >= std::numeric_limits<int8_t>::min()) {
323
0
    if (auto EC = Writer->writeInteger<uint16_t>(LF_CHAR))
324
0
      return EC;
325
0
    if (auto EC = Writer->writeInteger<int8_t>(Value))
326
0
      return EC;
327
0
  } else if (Value >= std::numeric_limits<int16_t>::min()) {
328
0
    if (auto EC = Writer->writeInteger<uint16_t>(LF_SHORT))
329
0
      return EC;
330
0
    if (auto EC = Writer->writeInteger<int16_t>(Value))
331
0
      return EC;
332
0
  } else if (Value >= std::numeric_limits<int32_t>::min()) {
333
0
    if (auto EC = Writer->writeInteger<uint16_t>(LF_LONG))
334
0
      return EC;
335
0
    if (auto EC = Writer->writeInteger<int32_t>(Value))
336
0
      return EC;
337
0
  } else {
338
0
    if (auto EC = Writer->writeInteger<uint16_t>(LF_QUADWORD))
339
0
      return EC;
340
0
    if (auto EC = Writer->writeInteger(Value))
341
0
      return EC;
342
0
  }
343
0
  return Error::success();
344
0
}
345
346
12.9k
Error CodeViewRecordIO::writeEncodedUnsignedInteger(const uint64_t &Value) {
347
12.9k
  if (Value < LF_NUMERIC) {
348
12.8k
    if (auto EC = Writer->writeInteger<uint16_t>(Value))
349
0
      return EC;
350
107
  } else if (Value <= std::numeric_limits<uint16_t>::max()) {
351
7
    if (auto EC = Writer->writeInteger<uint16_t>(LF_USHORT))
352
0
      return EC;
353
7
    if (auto EC = Writer->writeInteger<uint16_t>(Value))
354
0
      return EC;
355
100
  } else if (Value <= std::numeric_limits<uint32_t>::max()) {
356
95
    if (auto EC = Writer->writeInteger<uint16_t>(LF_ULONG))
357
0
      return EC;
358
95
    if (auto EC = Writer->writeInteger<uint32_t>(Value))
359
0
      return EC;
360
5
  } else {
361
5
    if (auto EC = Writer->writeInteger<uint16_t>(LF_UQUADWORD))
362
0
      return EC;
363
5
    if (auto EC = Writer->writeInteger(Value))
364
0
      return EC;
365
12.9k
  }
366
12.9k
367
12.9k
  return Error::success();
368
12.9k
}