summaryrefslogtreecommitdiffstats
path: root/lib/ProfileData/CoverageMappingReader.cpp
blob: 6476d28ec35d2d8f1b663b4c8d7397c81f272a43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//=-- CoverageMappingReader.cpp - Code coverage mapping reader ----*- C++ -*-=//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains support for reading coverage mapping data for
// instrumentation based coverage.
//
//===----------------------------------------------------------------------===//

#include "llvm/ProfileData/CoverageMappingReader.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/LEB128.h"

using namespace llvm;
using namespace coverage;
using namespace object;

#define DEBUG_TYPE "coverage-mapping"

void CoverageMappingIterator::increment() {
  // Check if all the records were read or if an error occurred while reading
  // the next record.
  if (Reader->readNextRecord(Record))
    *this = CoverageMappingIterator();
}

std::error_code RawCoverageReader::readULEB128(uint64_t &Result) {
  if (Data.size() < 1)
    return error(instrprof_error::truncated);
  unsigned N = 0;
  Result = decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
  if (N > Data.size())
    return error(instrprof_error::malformed);
  Data = Data.substr(N);
  return success();
}

std::error_code RawCoverageReader::readIntMax(uint64_t &Result,
                                              uint64_t MaxPlus1) {
  if (auto Err = readULEB128(Result))
    return Err;
  if (Result >= MaxPlus1)
    return error(instrprof_error::malformed);
  return success();
}

std::error_code RawCoverageReader::readSize(uint64_t &Result) {
  if (auto Err = readULEB128(Result))
    return Err;
  // Sanity check the number.
  if (Result > Data.size())
    return error(instrprof_error::malformed);
  return success();
}

std::error_code RawCoverageReader::readString(StringRef &Result) {
  uint64_t Length;
  if (auto Err = readSize(Length))
    return Err;
  Result = Data.substr(0, Length);
  Data = Data.substr(Length);
  return success();
}

std::error_code RawCoverageFilenamesReader::read() {
  uint64_t NumFilenames;
  if (auto Err = readSize(NumFilenames))
    return Err;
  for (size_t I = 0; I < NumFilenames; ++I) {
    StringRef Filename;
    if (auto Err = readString(Filename))
      return Err;
    Filenames.push_back(Filename);
  }
  return success();
}

std::error_code RawCoverageMappingReader::decodeCounter(unsigned Value,
                                                        Counter &C) {
  auto Tag = Value & Counter::EncodingTagMask;
  switch (Tag) {
  case Counter::Zero:
    C = Counter::getZero();
    return success();
  case Counter::CounterValueReference:
    C = Counter::getCounter(Value >> Counter::EncodingTagBits);
    return success();
  default:
    break;
  }
  Tag -= Counter::Expression;
  switch (Tag) {
  case CounterExpression::Subtract:
  case CounterExpression::Add: {
    auto ID = Value >> Counter::EncodingTagBits;
    if (ID >= Expressions.size())
      return error(instrprof_error::malformed);
    Expressions[ID].Kind = CounterExpression::ExprKind(Tag);
    C = Counter::getExpression(ID);
    break;
  }
  default:
    return error(instrprof_error::malformed);
  }
  return success();
}

std::error_code RawCoverageMappingReader::readCounter(Counter &C) {
  uint64_t EncodedCounter;
  if (auto Err =
          readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max()))
    return Err;
  if (auto Err = decodeCounter(EncodedCounter, C))
    return Err;
  return success();
}

static const unsigned EncodingExpansionRegionBit = 1
                                                   << Counter::EncodingTagBits;

/// \brief Read the sub-array of regions for the given inferred file id.
/// \param NumFileIDs the number of file ids that are defined for this
/// function.
std::error_code RawCoverageMappingReader::readMappingRegionsSubArray(
    std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID,
    size_t NumFileIDs) {
  uint64_t NumRegions;
  if (auto Err = readSize(NumRegions))
    return Err;
  unsigned LineStart = 0;
  for (size_t I = 0; I < NumRegions; ++I) {
    Counter C;
    CounterMappingRegion::RegionKind Kind = CounterMappingRegion::CodeRegion;

    // Read the combined counter + region kind.
    uint64_t EncodedCounterAndRegion;
    if (auto Err = readIntMax(EncodedCounterAndRegion,
                              std::numeric_limits<unsigned>::max()))
      return Err;
    unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
    uint64_t ExpandedFileID = 0;
    if (Tag != Counter::Zero) {
      if (auto Err = decodeCounter(EncodedCounterAndRegion, C))
        return Err;
    } else {
      // Is it an expansion region?
      if (EncodedCounterAndRegion & EncodingExpansionRegionBit) {
        Kind = CounterMappingRegion::ExpansionRegion;
        ExpandedFileID = EncodedCounterAndRegion >>
                         Counter::EncodingCounterTagAndExpansionRegionTagBits;
        if (ExpandedFileID >= NumFileIDs)
          return error(instrprof_error::malformed);
      } else {
        switch (EncodedCounterAndRegion >>
                Counter::EncodingCounterTagAndExpansionRegionTagBits) {
        case CounterMappingRegion::CodeRegion:
          // Don't do anything when we have a code region with a zero counter.
          break;
        case CounterMappingRegion::SkippedRegion:
          Kind = CounterMappingRegion::SkippedRegion;
          break;
        default:
          return error(instrprof_error::malformed);
        }
      }
    }

    // Read the source range.
    uint64_t LineStartDelta, CodeBeforeColumnStart, NumLines, ColumnEnd;
    if (auto Err =
            readIntMax(LineStartDelta, std::numeric_limits<unsigned>::max()))
      return Err;
    if (auto Err = readULEB128(CodeBeforeColumnStart))
      return Err;
    bool HasCodeBefore = CodeBeforeColumnStart & 1;
    uint64_t ColumnStart = CodeBeforeColumnStart >>
                           CounterMappingRegion::EncodingHasCodeBeforeBits;
    if (ColumnStart > std::numeric_limits<unsigned>::max())
      return error(instrprof_error::malformed);
    if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max()))
      return Err;
    if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max()))
      return Err;
    LineStart += LineStartDelta;
    // Adjust the column locations for the empty regions that are supposed to
    // cover whole lines. Those regions should be encoded with the
    // column range (1 -> std::numeric_limits<unsigned>::max()), but because
    // the encoded std::numeric_limits<unsigned>::max() is several bytes long,
    // we set the column range to (0 -> 0) to ensure that the column start and
    // column end take up one byte each.
    // The std::numeric_limits<unsigned>::max() is used to represent a column
    // position at the end of the line without knowing the length of that line.
    if (ColumnStart == 0 && ColumnEnd == 0) {
      ColumnStart = 1;
      ColumnEnd = std::numeric_limits<unsigned>::max();
    }

    DEBUG({
      dbgs() << "Counter in file " << InferredFileID << " " << LineStart << ":"
             << ColumnStart << " -> " << (LineStart + NumLines) << ":"
             << ColumnEnd << ", ";
      if (Kind == CounterMappingRegion::ExpansionRegion)
        dbgs() << "Expands to file " << ExpandedFileID;
      else
        CounterMappingContext(Expressions).dump(C, dbgs());
      dbgs() << "\n";
    });

    MappingRegions.push_back(CounterMappingRegion(
        C, InferredFileID, LineStart, ColumnStart, LineStart + NumLines,
        ColumnEnd, HasCodeBefore, Kind));
    MappingRegions.back().ExpandedFileID = ExpandedFileID;
  }
  return success();
}

std::error_code RawCoverageMappingReader::read(CoverageMappingRecord &Record) {

  // Read the virtual file mapping.
  llvm::SmallVector<unsigned, 8> VirtualFileMapping;
  uint64_t NumFileMappings;
  if (auto Err = readSize(NumFileMappings))
    return Err;
  for (size_t I = 0; I < NumFileMappings; ++I) {
    uint64_t FilenameIndex;
    if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size()))
      return Err;
    VirtualFileMapping.push_back(FilenameIndex);
  }

  // Construct the files using unique filenames and virtual file mapping.
  for (auto I : VirtualFileMapping) {
    Filenames.push_back(TranslationUnitFilenames[I]);
  }

  // Read the expressions.
  uint64_t NumExpressions;
  if (auto Err = readSize(NumExpressions))
    return Err;
  // Create an array of dummy expressions that get the proper counters
  // when the expressions are read, and the proper kinds when the counters
  // are decoded.
  Expressions.resize(
      NumExpressions,
      CounterExpression(CounterExpression::Subtract, Counter(), Counter()));
  for (size_t I = 0; I < NumExpressions; ++I) {
    if (auto Err = readCounter(Expressions[I].LHS))
      return Err;
    if (auto Err = readCounter(Expressions[I].RHS))
      return Err;
  }

  // Read the mapping regions sub-arrays.
  for (unsigned InferredFileID = 0, S = VirtualFileMapping.size();
       InferredFileID < S; ++InferredFileID) {
    if (auto Err = readMappingRegionsSubArray(MappingRegions, InferredFileID,
                                              VirtualFileMapping.size()))
      return Err;
  }

  // Set the counters for the expansion regions.
  // i.e. Counter of expansion region = counter of the first region
  // from the expanded file.
  // Perform multiple passes to correctly propagate the counters through
  // all the nested expansion regions.
  SmallVector<CounterMappingRegion *, 8> FileIDExpansionRegionMapping;
  FileIDExpansionRegionMapping.resize(VirtualFileMapping.size(), nullptr);
  for (unsigned Pass = 1, S = VirtualFileMapping.size(); Pass < S; ++Pass) {
    for (auto &R : MappingRegions) {
      if (R.Kind != CounterMappingRegion::ExpansionRegion)
        continue;
      assert(!FileIDExpansionRegionMapping[R.ExpandedFileID]);
      FileIDExpansionRegionMapping[R.ExpandedFileID] = &R;
    }
    for (auto &R : MappingRegions) {
      if (FileIDExpansionRegionMapping[R.FileID]) {
        FileIDExpansionRegionMapping[R.FileID]->Count = R.Count;
        FileIDExpansionRegionMapping[R.FileID] = nullptr;
      }
    }
  }

  Record.FunctionName = FunctionName;
  Record.Filenames = Filenames;
  Record.Expressions = Expressions;
  Record.MappingRegions = MappingRegions;
  return success();
}

ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader(
    StringRef FileName)
    : CurrentRecord(0) {
  auto File = llvm::object::ObjectFile::createObjectFile(FileName);
  if (!File)
    error(File.getError());
  else
    Object = std::move(File.get());
}

namespace {
/// \brief The coverage mapping data for a single function.
/// It points to the function's name.
template <typename IntPtrT> struct CoverageMappingFunctionRecord {
  IntPtrT FunctionNamePtr;
  uint32_t FunctionNameSize;
  uint32_t CoverageMappingSize;
  uint64_t FunctionHash;
};

/// \brief The coverage mapping data for a single translation unit.
/// It points to the array of function coverage mapping records and the encoded
/// filenames array.
template <typename IntPtrT> struct CoverageMappingTURecord {
  uint32_t FunctionRecordsSize;
  uint32_t FilenamesSize;
  uint32_t CoverageMappingsSize;
  uint32_t Version;
};

/// \brief A helper structure to access the data from a section
/// in an object file.
struct SectionData {
  StringRef Data;
  uint64_t Address;

  std::error_code load(SectionRef &Section) {
    if (auto Err = Section.getContents(Data))
      return Err;
    Address = Section.getAddress();
    return instrprof_error::success;
  }

  std::error_code get(uint64_t Pointer, size_t Size, StringRef &Result) {
    if (Pointer < Address)
      return instrprof_error::malformed;
    auto Offset = Pointer - Address;
    if (Offset + Size > Data.size())
      return instrprof_error::malformed;
    Result = Data.substr(Pointer - Address, Size);
    return instrprof_error::success;
  }
};
}

template <typename T>
std::error_code readCoverageMappingData(
    SectionData &ProfileNames, StringRef Data,
    std::vector<ObjectFileCoverageMappingReader::ProfileMappingRecord> &Records,
    std::vector<StringRef> &Filenames) {
  llvm::DenseSet<T> UniqueFunctionMappingData;

  // Read the records in the coverage data section.
  while (!Data.empty()) {
    if (Data.size() < sizeof(CoverageMappingTURecord<T>))
      return instrprof_error::malformed;
    auto TU = reinterpret_cast<const CoverageMappingTURecord<T> *>(Data.data());
    Data = Data.substr(sizeof(CoverageMappingTURecord<T>));
    switch (TU->Version) {
    case CoverageMappingVersion1:
      break;
    default:
      return instrprof_error::unsupported_version;
    }
    auto Version = CoverageMappingVersion(TU->Version);

    // Get the function records.
    auto FunctionRecords =
        reinterpret_cast<const CoverageMappingFunctionRecord<T> *>(Data.data());
    if (Data.size() <
        sizeof(CoverageMappingFunctionRecord<T>) * TU->FunctionRecordsSize)
      return instrprof_error::malformed;
    Data = Data.substr(sizeof(CoverageMappingFunctionRecord<T>) *
                       TU->FunctionRecordsSize);

    // Get the filenames.
    if (Data.size() < TU->FilenamesSize)
      return instrprof_error::malformed;
    auto RawFilenames = Data.substr(0, TU->FilenamesSize);
    Data = Data.substr(TU->FilenamesSize);
    size_t FilenamesBegin = Filenames.size();
    RawCoverageFilenamesReader Reader(RawFilenames, Filenames);
    if (auto Err = Reader.read())
      return Err;

    // Get the coverage mappings.
    if (Data.size() < TU->CoverageMappingsSize)
      return instrprof_error::malformed;
    auto CoverageMappings = Data.substr(0, TU->CoverageMappingsSize);
    Data = Data.substr(TU->CoverageMappingsSize);

    for (unsigned I = 0; I < TU->FunctionRecordsSize; ++I) {
      auto &MappingRecord = FunctionRecords[I];

      // Get the coverage mapping.
      if (CoverageMappings.size() < MappingRecord.CoverageMappingSize)
        return instrprof_error::malformed;
      auto Mapping =
          CoverageMappings.substr(0, MappingRecord.CoverageMappingSize);
      CoverageMappings =
          CoverageMappings.substr(MappingRecord.CoverageMappingSize);

      // Ignore this record if we already have a record that points to the same
      // function name.
      // This is useful to ignore the redundant records for the functions
      // with ODR linkage.
      if (!UniqueFunctionMappingData.insert(MappingRecord.FunctionNamePtr)
               .second)
        continue;
      StringRef FunctionName;
      if (auto Err =
              ProfileNames.get(MappingRecord.FunctionNamePtr,
                               MappingRecord.FunctionNameSize, FunctionName))
        return Err;
      Records.push_back(ObjectFileCoverageMappingReader::ProfileMappingRecord(
          Version, FunctionName, MappingRecord.FunctionHash, Mapping,
          FilenamesBegin, Filenames.size() - FilenamesBegin));
    }
  }

  return instrprof_error::success;
}

static const char *TestingFormatMagic = "llvmcovmtestdata";

static std::error_code decodeTestingFormat(StringRef Data,
                                           SectionData &ProfileNames,
                                           StringRef &CoverageMapping) {
  Data = Data.substr(StringRef(TestingFormatMagic).size());
  if (Data.size() < 1)
    return instrprof_error::truncated;
  unsigned N = 0;
  auto ProfileNamesSize =
      decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
  if (N > Data.size())
    return instrprof_error::malformed;
  Data = Data.substr(N);
  if (Data.size() < 1)
    return instrprof_error::truncated;
  N = 0;
  ProfileNames.Address =
      decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
  if (N > Data.size())
    return instrprof_error::malformed;
  Data = Data.substr(N);
  if (Data.size() < ProfileNamesSize)
    return instrprof_error::malformed;
  ProfileNames.Data = Data.substr(0, ProfileNamesSize);
  CoverageMapping = Data.substr(ProfileNamesSize);
  return instrprof_error::success;
}

ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader(
    std::unique_ptr<MemoryBuffer> &ObjectBuffer, sys::fs::file_magic Type)
    : CurrentRecord(0) {
  if (ObjectBuffer->getBuffer().startswith(TestingFormatMagic)) {
    // This is a special format used for testing.
    SectionData ProfileNames;
    StringRef CoverageMapping;
    if (auto Err = decodeTestingFormat(ObjectBuffer->getBuffer(), ProfileNames,
                                       CoverageMapping)) {
      error(Err);
      return;
    }
    error(readCoverageMappingData<uint64_t>(ProfileNames, CoverageMapping,
                                            MappingRecords, Filenames));
    Object = OwningBinary<ObjectFile>(std::unique_ptr<ObjectFile>(),
                                      std::move(ObjectBuffer));
    return;
  }

  auto File = object::ObjectFile::createObjectFile(
      ObjectBuffer->getMemBufferRef(), Type);
  if (!File)
    error(File.getError());
  else
    Object = OwningBinary<ObjectFile>(std::move(File.get()),
                                      std::move(ObjectBuffer));
}

std::error_code ObjectFileCoverageMappingReader::readHeader() {
  const ObjectFile *OF = Object.getBinary();
  if (!OF)
    return getError();
  auto BytesInAddress = OF->getBytesInAddress();
  if (BytesInAddress != 4 && BytesInAddress != 8)
    return error(instrprof_error::malformed);

  // Look for the sections that we are interested in.
  int FoundSectionCount = 0;
  SectionRef ProfileNames, CoverageMapping;
  for (const auto &Section : OF->sections()) {
    StringRef Name;
    if (auto Err = Section.getName(Name))
      return Err;
    if (Name == "__llvm_prf_names") {
      ProfileNames = Section;
    } else if (Name == "__llvm_covmap") {
      CoverageMapping = Section;
    } else
      continue;
    ++FoundSectionCount;
  }
  if (FoundSectionCount != 2)
    return error(instrprof_error::bad_header);

  // Get the contents of the given sections.
  StringRef Data;
  if (auto Err = CoverageMapping.getContents(Data))
    return Err;
  SectionData ProfileNamesData;
  if (auto Err = ProfileNamesData.load(ProfileNames))
    return Err;

  // Load the data from the found sections.
  std::error_code Err;
  if (BytesInAddress == 4)
    Err = readCoverageMappingData<uint32_t>(ProfileNamesData, Data,
                                            MappingRecords, Filenames);
  else
    Err = readCoverageMappingData<uint64_t>(ProfileNamesData, Data,
                                            MappingRecords, Filenames);
  if (Err)
    return error(Err);

  return success();
}

std::error_code
ObjectFileCoverageMappingReader::readNextRecord(CoverageMappingRecord &Record) {
  if (CurrentRecord >= MappingRecords.size())
    return error(instrprof_error::eof);

  FunctionsFilenames.clear();
  Expressions.clear();
  MappingRegions.clear();
  auto &R = MappingRecords[CurrentRecord];
  RawCoverageMappingReader Reader(
      R.FunctionName, R.CoverageMapping,
      makeArrayRef(Filenames.data() + R.FilenamesBegin, R.FilenamesSize),
      FunctionsFilenames, Expressions, MappingRegions);
  if (auto Err = Reader.read(Record))
    return Err;
  Record.FunctionHash = R.FunctionHash;
  ++CurrentRecord;
  return success();
}