LLVM  5.0.0
CoverageMappingReader.cpp
Go to the documentation of this file.
1 //===- CoverageMappingReader.cpp - Code coverage mapping reader -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for reading coverage mapping data for
11 // instrumentation based coverage.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Object/Binary.h"
23 #include "llvm/Object/COFF.h"
24 #include "llvm/Object/Error.h"
26 #include "llvm/Object/ObjectFile.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/Endian.h"
31 #include "llvm/Support/Error.h"
33 #include "llvm/Support/LEB128.h"
36 #include <algorithm>
37 #include <cassert>
38 #include <cstddef>
39 #include <cstdint>
40 #include <limits>
41 #include <memory>
42 #include <utility>
43 #include <vector>
44 
45 using namespace llvm;
46 using namespace coverage;
47 using namespace object;
48 
49 #define DEBUG_TYPE "coverage-mapping"
50 
51 void CoverageMappingIterator::increment() {
52  // Check if all the records were read or if an error occurred while reading
53  // the next record.
54  if (auto E = Reader->readNextRecord(Record)) {
55  handleAllErrors(std::move(E), [&](const CoverageMapError &CME) {
56  if (CME.get() == coveragemap_error::eof)
57  *this = CoverageMappingIterator();
58  else
59  llvm_unreachable("Unexpected error in coverage mapping iterator");
60  });
61  }
62 }
63 
65  if (Data.empty())
66  return make_error<CoverageMapError>(coveragemap_error::truncated);
67  unsigned N = 0;
68  Result = decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
69  if (N > Data.size())
70  return make_error<CoverageMapError>(coveragemap_error::malformed);
71  Data = Data.substr(N);
72  return Error::success();
73 }
74 
75 Error RawCoverageReader::readIntMax(uint64_t &Result, uint64_t MaxPlus1) {
76  if (auto Err = readULEB128(Result))
77  return Err;
78  if (Result >= MaxPlus1)
79  return make_error<CoverageMapError>(coveragemap_error::malformed);
80  return Error::success();
81 }
82 
84  if (auto Err = readULEB128(Result))
85  return Err;
86  // Sanity check the number.
87  if (Result > Data.size())
88  return make_error<CoverageMapError>(coveragemap_error::malformed);
89  return Error::success();
90 }
91 
93  uint64_t Length;
94  if (auto Err = readSize(Length))
95  return Err;
96  Result = Data.substr(0, Length);
97  Data = Data.substr(Length);
98  return Error::success();
99 }
100 
102  uint64_t NumFilenames;
103  if (auto Err = readSize(NumFilenames))
104  return Err;
105  for (size_t I = 0; I < NumFilenames; ++I) {
106  StringRef Filename;
107  if (auto Err = readString(Filename))
108  return Err;
109  Filenames.push_back(Filename);
110  }
111  return Error::success();
112 }
113 
114 Error RawCoverageMappingReader::decodeCounter(unsigned Value, Counter &C) {
115  auto Tag = Value & Counter::EncodingTagMask;
116  switch (Tag) {
117  case Counter::Zero:
118  C = Counter::getZero();
119  return Error::success();
122  return Error::success();
123  default:
124  break;
125  }
127  switch (Tag) {
129  case CounterExpression::Add: {
130  auto ID = Value >> Counter::EncodingTagBits;
131  if (ID >= Expressions.size())
132  return make_error<CoverageMapError>(coveragemap_error::malformed);
135  break;
136  }
137  default:
138  return make_error<CoverageMapError>(coveragemap_error::malformed);
139  }
140  return Error::success();
141 }
142 
143 Error RawCoverageMappingReader::readCounter(Counter &C) {
144  uint64_t EncodedCounter;
145  if (auto Err =
146  readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max()))
147  return Err;
148  if (auto Err = decodeCounter(EncodedCounter, C))
149  return Err;
150  return Error::success();
151 }
152 
153 static const unsigned EncodingExpansionRegionBit = 1
155 
156 /// \brief Read the sub-array of regions for the given inferred file id.
157 /// \param NumFileIDs the number of file ids that are defined for this
158 /// function.
159 Error RawCoverageMappingReader::readMappingRegionsSubArray(
160  std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID,
161  size_t NumFileIDs) {
162  uint64_t NumRegions;
163  if (auto Err = readSize(NumRegions))
164  return Err;
165  unsigned LineStart = 0;
166  for (size_t I = 0; I < NumRegions; ++I) {
167  Counter C;
169 
170  // Read the combined counter + region kind.
171  uint64_t EncodedCounterAndRegion;
172  if (auto Err = readIntMax(EncodedCounterAndRegion,
174  return Err;
175  unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
176  uint64_t ExpandedFileID = 0;
177  if (Tag != Counter::Zero) {
178  if (auto Err = decodeCounter(EncodedCounterAndRegion, C))
179  return Err;
180  } else {
181  // Is it an expansion region?
182  if (EncodedCounterAndRegion & EncodingExpansionRegionBit) {
184  ExpandedFileID = EncodedCounterAndRegion >>
186  if (ExpandedFileID >= NumFileIDs)
187  return make_error<CoverageMapError>(coveragemap_error::malformed);
188  } else {
189  switch (EncodedCounterAndRegion >>
192  // Don't do anything when we have a code region with a zero counter.
193  break;
196  break;
197  default:
198  return make_error<CoverageMapError>(coveragemap_error::malformed);
199  }
200  }
201  }
202 
203  // Read the source range.
204  uint64_t LineStartDelta, ColumnStart, NumLines, ColumnEnd;
205  if (auto Err =
206  readIntMax(LineStartDelta, std::numeric_limits<unsigned>::max()))
207  return Err;
208  if (auto Err = readULEB128(ColumnStart))
209  return Err;
210  if (ColumnStart > std::numeric_limits<unsigned>::max())
211  return make_error<CoverageMapError>(coveragemap_error::malformed);
212  if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max()))
213  return Err;
214  if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max()))
215  return Err;
216  LineStart += LineStartDelta;
217  // Adjust the column locations for the empty regions that are supposed to
218  // cover whole lines. Those regions should be encoded with the
219  // column range (1 -> std::numeric_limits<unsigned>::max()), but because
220  // the encoded std::numeric_limits<unsigned>::max() is several bytes long,
221  // we set the column range to (0 -> 0) to ensure that the column start and
222  // column end take up one byte each.
223  // The std::numeric_limits<unsigned>::max() is used to represent a column
224  // position at the end of the line without knowing the length of that line.
225  if (ColumnStart == 0 && ColumnEnd == 0) {
226  ColumnStart = 1;
228  }
229 
230  DEBUG({
231  dbgs() << "Counter in file " << InferredFileID << " " << LineStart << ":"
232  << ColumnStart << " -> " << (LineStart + NumLines) << ":"
233  << ColumnEnd << ", ";
235  dbgs() << "Expands to file " << ExpandedFileID;
236  else
238  dbgs() << "\n";
239  });
240 
241  MappingRegions.push_back(CounterMappingRegion(
242  C, InferredFileID, ExpandedFileID, LineStart, ColumnStart,
243  LineStart + NumLines, ColumnEnd, Kind));
244  }
245  return Error::success();
246 }
247 
249  // Read the virtual file mapping.
250  SmallVector<unsigned, 8> VirtualFileMapping;
251  uint64_t NumFileMappings;
252  if (auto Err = readSize(NumFileMappings))
253  return Err;
254  for (size_t I = 0; I < NumFileMappings; ++I) {
255  uint64_t FilenameIndex;
256  if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size()))
257  return Err;
258  VirtualFileMapping.push_back(FilenameIndex);
259  }
260 
261  // Construct the files using unique filenames and virtual file mapping.
262  for (auto I : VirtualFileMapping) {
263  Filenames.push_back(TranslationUnitFilenames[I]);
264  }
265 
266  // Read the expressions.
267  uint64_t NumExpressions;
268  if (auto Err = readSize(NumExpressions))
269  return Err;
270  // Create an array of dummy expressions that get the proper counters
271  // when the expressions are read, and the proper kinds when the counters
272  // are decoded.
273  Expressions.resize(
274  NumExpressions,
276  for (size_t I = 0; I < NumExpressions; ++I) {
277  if (auto Err = readCounter(Expressions[I].LHS))
278  return Err;
279  if (auto Err = readCounter(Expressions[I].RHS))
280  return Err;
281  }
282 
283  // Read the mapping regions sub-arrays.
284  for (unsigned InferredFileID = 0, S = VirtualFileMapping.size();
285  InferredFileID < S; ++InferredFileID) {
286  if (auto Err = readMappingRegionsSubArray(MappingRegions, InferredFileID,
287  VirtualFileMapping.size()))
288  return Err;
289  }
290 
291  // Set the counters for the expansion regions.
292  // i.e. Counter of expansion region = counter of the first region
293  // from the expanded file.
294  // Perform multiple passes to correctly propagate the counters through
295  // all the nested expansion regions.
296  SmallVector<CounterMappingRegion *, 8> FileIDExpansionRegionMapping;
297  FileIDExpansionRegionMapping.resize(VirtualFileMapping.size(), nullptr);
298  for (unsigned Pass = 1, S = VirtualFileMapping.size(); Pass < S; ++Pass) {
299  for (auto &R : MappingRegions) {
301  continue;
302  assert(!FileIDExpansionRegionMapping[R.ExpandedFileID]);
303  FileIDExpansionRegionMapping[R.ExpandedFileID] = &R;
304  }
305  for (auto &R : MappingRegions) {
306  if (FileIDExpansionRegionMapping[R.FileID]) {
307  FileIDExpansionRegionMapping[R.FileID]->Count = R.Count;
308  FileIDExpansionRegionMapping[R.FileID] = nullptr;
309  }
310  }
311  }
312 
313  return Error::success();
314 }
315 
317  // A dummy coverage mapping data consists of just one region with zero count.
318  uint64_t NumFileMappings;
319  if (Error Err = readSize(NumFileMappings))
320  return std::move(Err);
321  if (NumFileMappings != 1)
322  return false;
323  // We don't expect any specific value for the filename index, just skip it.
324  uint64_t FilenameIndex;
325  if (Error Err =
326  readIntMax(FilenameIndex, std::numeric_limits<unsigned>::max()))
327  return std::move(Err);
328  uint64_t NumExpressions;
329  if (Error Err = readSize(NumExpressions))
330  return std::move(Err);
331  if (NumExpressions != 0)
332  return false;
333  uint64_t NumRegions;
334  if (Error Err = readSize(NumRegions))
335  return std::move(Err);
336  if (NumRegions != 1)
337  return false;
338  uint64_t EncodedCounterAndRegion;
339  if (Error Err = readIntMax(EncodedCounterAndRegion,
341  return std::move(Err);
342  unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
343  return Tag == Counter::Zero;
344 }
345 
347  if (auto EC = Section.getContents(Data))
348  return errorCodeToError(EC);
349  Address = Section.getAddress();
350  return Error::success();
351 }
352 
353 StringRef InstrProfSymtab::getFuncName(uint64_t Pointer, size_t Size) {
354  if (Pointer < Address)
355  return StringRef();
356  auto Offset = Pointer - Address;
357  if (Offset + Size > Data.size())
358  return StringRef();
359  return Data.substr(Pointer - Address, Size);
360 }
361 
362 // Check if the mapping data is a dummy, i.e. is emitted for an unused function.
364  // The hash value of dummy mapping records is always zero.
365  if (Hash)
366  return false;
367  return RawCoverageMappingDummyChecker(Mapping).isDummy();
368 }
369 
370 namespace {
371 
372 struct CovMapFuncRecordReader {
373  virtual ~CovMapFuncRecordReader() = default;
374 
375  // The interface to read coverage mapping function records for a module.
376  //
377  // \p Buf points to the buffer containing the \c CovHeader of the coverage
378  // mapping data associated with the module.
379  //
380  // Returns a pointer to the next \c CovHeader if it exists, or a pointer
381  // greater than \p End if not.
382  virtual Expected<const char *> readFunctionRecords(const char *Buf,
383  const char *End) = 0;
384 
385  template <class IntPtrT, support::endianness Endian>
388  std::vector<BinaryCoverageReader::ProfileMappingRecord> &R,
389  std::vector<StringRef> &F);
390 };
391 
392 // A class for reading coverage mapping function records for a module.
393 template <CovMapVersion Version, class IntPtrT, support::endianness Endian>
394 class VersionedCovMapFuncRecordReader : public CovMapFuncRecordReader {
395  using FuncRecordType =
397  using NameRefType = typename CovMapTraits<Version, IntPtrT>::NameRefType;
398 
399  // Maps function's name references to the indexes of their records
400  // in \c Records.
401  DenseMap<NameRefType, size_t> FunctionRecords;
402  InstrProfSymtab &ProfileNames;
403  std::vector<StringRef> &Filenames;
404  std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records;
405 
406  // Add the record to the collection if we don't already have a record that
407  // points to the same function name. This is useful to ignore the redundant
408  // records for the functions with ODR linkage.
409  // In addition, prefer records with real coverage mapping data to dummy
410  // records, which were emitted for inline functions which were seen but
411  // not used in the corresponding translation unit.
412  Error insertFunctionRecordIfNeeded(const FuncRecordType *CFR,
413  StringRef Mapping, size_t FilenamesBegin) {
414  uint64_t FuncHash = CFR->template getFuncHash<Endian>();
415  NameRefType NameRef = CFR->template getFuncNameRef<Endian>();
416  auto InsertResult =
417  FunctionRecords.insert(std::make_pair(NameRef, Records.size()));
418  if (InsertResult.second) {
419  StringRef FuncName;
420  if (Error Err = CFR->template getFuncName<Endian>(ProfileNames, FuncName))
421  return Err;
422  if (FuncName.empty())
423  return make_error<InstrProfError>(instrprof_error::malformed);
424  Records.emplace_back(Version, FuncName, FuncHash, Mapping, FilenamesBegin,
425  Filenames.size() - FilenamesBegin);
426  return Error::success();
427  }
428  // Update the existing record if it's a dummy and the new record is real.
429  size_t OldRecordIndex = InsertResult.first->second;
431  Records[OldRecordIndex];
432  Expected<bool> OldIsDummyExpected = isCoverageMappingDummy(
433  OldRecord.FunctionHash, OldRecord.CoverageMapping);
434  if (Error Err = OldIsDummyExpected.takeError())
435  return Err;
436  if (!*OldIsDummyExpected)
437  return Error::success();
438  Expected<bool> NewIsDummyExpected =
439  isCoverageMappingDummy(FuncHash, Mapping);
440  if (Error Err = NewIsDummyExpected.takeError())
441  return Err;
442  if (*NewIsDummyExpected)
443  return Error::success();
444  OldRecord.FunctionHash = FuncHash;
445  OldRecord.CoverageMapping = Mapping;
446  OldRecord.FilenamesBegin = FilenamesBegin;
447  OldRecord.FilenamesSize = Filenames.size() - FilenamesBegin;
448  return Error::success();
449  }
450 
451 public:
452  VersionedCovMapFuncRecordReader(
454  std::vector<BinaryCoverageReader::ProfileMappingRecord> &R,
455  std::vector<StringRef> &F)
456  : ProfileNames(P), Filenames(F), Records(R) {}
457 
458  ~VersionedCovMapFuncRecordReader() override = default;
459 
460  Expected<const char *> readFunctionRecords(const char *Buf,
461  const char *End) override {
462  using namespace support;
463 
464  if (Buf + sizeof(CovMapHeader) > End)
465  return make_error<CoverageMapError>(coveragemap_error::malformed);
466  auto CovHeader = reinterpret_cast<const CovMapHeader *>(Buf);
467  uint32_t NRecords = CovHeader->getNRecords<Endian>();
468  uint32_t FilenamesSize = CovHeader->getFilenamesSize<Endian>();
469  uint32_t CoverageSize = CovHeader->getCoverageSize<Endian>();
470  assert((CovMapVersion)CovHeader->getVersion<Endian>() == Version);
471  Buf = reinterpret_cast<const char *>(CovHeader + 1);
472 
473  // Skip past the function records, saving the start and end for later.
474  const char *FunBuf = Buf;
475  Buf += NRecords * sizeof(FuncRecordType);
476  const char *FunEnd = Buf;
477 
478  // Get the filenames.
479  if (Buf + FilenamesSize > End)
480  return make_error<CoverageMapError>(coveragemap_error::malformed);
481  size_t FilenamesBegin = Filenames.size();
482  RawCoverageFilenamesReader Reader(StringRef(Buf, FilenamesSize), Filenames);
483  if (auto Err = Reader.read())
484  return std::move(Err);
485  Buf += FilenamesSize;
486 
487  // We'll read the coverage mapping records in the loop below.
488  const char *CovBuf = Buf;
489  Buf += CoverageSize;
490  const char *CovEnd = Buf;
491 
492  if (Buf > End)
493  return make_error<CoverageMapError>(coveragemap_error::malformed);
494  // Each coverage map has an alignment of 8, so we need to adjust alignment
495  // before reading the next map.
496  Buf += alignmentAdjustment(Buf, 8);
497 
498  auto CFR = reinterpret_cast<const FuncRecordType *>(FunBuf);
499  while ((const char *)CFR < FunEnd) {
500  // Read the function information
501  uint32_t DataSize = CFR->template getDataSize<Endian>();
502 
503  // Now use that to read the coverage data.
504  if (CovBuf + DataSize > CovEnd)
505  return make_error<CoverageMapError>(coveragemap_error::malformed);
506  auto Mapping = StringRef(CovBuf, DataSize);
507  CovBuf += DataSize;
508 
509  if (Error Err =
510  insertFunctionRecordIfNeeded(CFR, Mapping, FilenamesBegin))
511  return std::move(Err);
512  CFR++;
513  }
514  return Buf;
515  }
516 };
517 
518 } // end anonymous namespace
519 
520 template <class IntPtrT, support::endianness Endian>
521 Expected<std::unique_ptr<CovMapFuncRecordReader>> CovMapFuncRecordReader::get(
523  std::vector<BinaryCoverageReader::ProfileMappingRecord> &R,
524  std::vector<StringRef> &F) {
525  using namespace coverage;
526 
527  switch (Version) {
529  return llvm::make_unique<VersionedCovMapFuncRecordReader<
530  CovMapVersion::Version1, IntPtrT, Endian>>(P, R, F);
532  // Decompress the name data.
533  if (Error E = P.create(P.getNameData()))
534  return std::move(E);
535  return llvm::make_unique<VersionedCovMapFuncRecordReader<
536  CovMapVersion::Version2, IntPtrT, Endian>>(P, R, F);
537  }
538  llvm_unreachable("Unsupported version");
539 }
540 
541 template <typename T, support::endianness Endian>
543  InstrProfSymtab &ProfileNames, StringRef Data,
544  std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records,
545  std::vector<StringRef> &Filenames) {
546  using namespace coverage;
547 
548  // Read the records in the coverage data section.
549  auto CovHeader =
550  reinterpret_cast<const CovMapHeader *>(Data.data());
551  CovMapVersion Version = (CovMapVersion)CovHeader->getVersion<Endian>();
552  if (Version > CovMapVersion::CurrentVersion)
553  return make_error<CoverageMapError>(coveragemap_error::unsupported_version);
555  CovMapFuncRecordReader::get<T, Endian>(Version, ProfileNames, Records,
556  Filenames);
557  if (Error E = ReaderExpected.takeError())
558  return E;
559  auto Reader = std::move(ReaderExpected.get());
560  for (const char *Buf = Data.data(), *End = Buf + Data.size(); Buf < End;) {
561  auto NextHeaderOrErr = Reader->readFunctionRecords(Buf, End);
562  if (auto E = NextHeaderOrErr.takeError())
563  return E;
564  Buf = NextHeaderOrErr.get();
565  }
566  return Error::success();
567 }
568 
569 static const char *TestingFormatMagic = "llvmcovmtestdata";
570 
573  uint8_t &BytesInAddress,
574  support::endianness &Endian) {
575  BytesInAddress = 8;
577 
578  Data = Data.substr(StringRef(TestingFormatMagic).size());
579  if (Data.empty())
580  return make_error<CoverageMapError>(coveragemap_error::truncated);
581  unsigned N = 0;
582  auto ProfileNamesSize =
583  decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
584  if (N > Data.size())
585  return make_error<CoverageMapError>(coveragemap_error::malformed);
586  Data = Data.substr(N);
587  if (Data.empty())
588  return make_error<CoverageMapError>(coveragemap_error::truncated);
589  N = 0;
590  uint64_t Address =
591  decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
592  if (N > Data.size())
593  return make_error<CoverageMapError>(coveragemap_error::malformed);
594  Data = Data.substr(N);
595  if (Data.size() < ProfileNamesSize)
596  return make_error<CoverageMapError>(coveragemap_error::malformed);
597  if (Error E = ProfileNames.create(Data.substr(0, ProfileNamesSize), Address))
598  return E;
599  CoverageMapping = Data.substr(ProfileNamesSize);
600  // Skip the padding bytes because coverage map data has an alignment of 8.
601  if (CoverageMapping.empty())
602  return make_error<CoverageMapError>(coveragemap_error::truncated);
603  size_t Pad = alignmentAdjustment(CoverageMapping.data(), 8);
604  if (CoverageMapping.size() < Pad)
605  return make_error<CoverageMapError>(coveragemap_error::malformed);
606  CoverageMapping = CoverageMapping.substr(Pad);
607  return Error::success();
608 }
609 
611  StringRef FoundName;
612  for (const auto &Section : OF.sections()) {
613  if (auto EC = Section.getName(FoundName))
614  return errorCodeToError(EC);
615  if (FoundName == Name)
616  return Section;
617  }
618  return make_error<CoverageMapError>(coveragemap_error::no_data_found);
619 }
620 
622  InstrProfSymtab &ProfileNames,
624  uint8_t &BytesInAddress,
625  support::endianness &Endian, StringRef Arch) {
626  auto BinOrErr = createBinary(ObjectBuffer);
627  if (!BinOrErr)
628  return BinOrErr.takeError();
629  auto Bin = std::move(BinOrErr.get());
630  std::unique_ptr<ObjectFile> OF;
631  if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin.get())) {
632  // If we have a universal binary, try to look up the object for the
633  // appropriate architecture.
634  auto ObjectFileOrErr = Universal->getObjectForArch(Arch);
635  if (!ObjectFileOrErr)
636  return ObjectFileOrErr.takeError();
637  OF = std::move(ObjectFileOrErr.get());
638  } else if (isa<ObjectFile>(Bin.get())) {
639  // For any other object file, upcast and take ownership.
640  OF.reset(cast<ObjectFile>(Bin.release()));
641  // If we've asked for a particular arch, make sure they match.
642  if (!Arch.empty() && OF->getArch() != Triple(Arch).getArch())
643  return errorCodeToError(object_error::arch_not_found);
644  } else
645  // We can only handle object files.
646  return make_error<CoverageMapError>(coveragemap_error::malformed);
647 
648  // The coverage uses native pointer sizes for the object it's written in.
649  BytesInAddress = OF->getBytesInAddress();
650  Endian = OF->isLittleEndian() ? support::endianness::little
652 
653  // Look for the sections that we are interested in.
654  auto ObjFormat = OF->getTripleObjectFormat();
655  auto NamesSection =
656  lookupSection(*OF, getInstrProfSectionName(IPSK_name, ObjFormat,
657  /*AddSegmentInfo=*/false));
658  if (auto E = NamesSection.takeError())
659  return E;
660  auto CoverageSection =
661  lookupSection(*OF, getInstrProfSectionName(IPSK_covmap, ObjFormat,
662  /*AddSegmentInfo=*/false));
663  if (auto E = CoverageSection.takeError())
664  return E;
665 
666  // Get the contents of the given sections.
667  if (auto EC = CoverageSection->getContents(CoverageMapping))
668  return errorCodeToError(EC);
669  if (Error E = ProfileNames.create(*NamesSection))
670  return E;
671 
672  return Error::success();
673 }
674 
676 BinaryCoverageReader::create(std::unique_ptr<MemoryBuffer> &ObjectBuffer,
677  StringRef Arch) {
678  std::unique_ptr<BinaryCoverageReader> Reader(new BinaryCoverageReader());
679 
680  StringRef Coverage;
681  uint8_t BytesInAddress;
682  support::endianness Endian;
683  Error E = Error::success();
684  consumeError(std::move(E));
685  if (ObjectBuffer->getBuffer().startswith(TestingFormatMagic))
686  // This is a special format used for testing.
687  E = loadTestingFormat(ObjectBuffer->getBuffer(), Reader->ProfileNames,
688  Coverage, BytesInAddress, Endian);
689  else
690  E = loadBinaryFormat(ObjectBuffer->getMemBufferRef(), Reader->ProfileNames,
691  Coverage, BytesInAddress, Endian, Arch);
692  if (E)
693  return std::move(E);
694 
695  if (BytesInAddress == 4 && Endian == support::endianness::little)
696  E = readCoverageMappingData<uint32_t, support::endianness::little>(
697  Reader->ProfileNames, Coverage, Reader->MappingRecords,
698  Reader->Filenames);
699  else if (BytesInAddress == 4 && Endian == support::endianness::big)
700  E = readCoverageMappingData<uint32_t, support::endianness::big>(
701  Reader->ProfileNames, Coverage, Reader->MappingRecords,
702  Reader->Filenames);
703  else if (BytesInAddress == 8 && Endian == support::endianness::little)
704  E = readCoverageMappingData<uint64_t, support::endianness::little>(
705  Reader->ProfileNames, Coverage, Reader->MappingRecords,
706  Reader->Filenames);
707  else if (BytesInAddress == 8 && Endian == support::endianness::big)
708  E = readCoverageMappingData<uint64_t, support::endianness::big>(
709  Reader->ProfileNames, Coverage, Reader->MappingRecords,
710  Reader->Filenames);
711  else
712  return make_error<CoverageMapError>(coveragemap_error::malformed);
713  if (E)
714  return std::move(E);
715  return std::move(Reader);
716 }
717 
719  if (CurrentRecord >= MappingRecords.size())
720  return make_error<CoverageMapError>(coveragemap_error::eof);
721 
722  FunctionsFilenames.clear();
723  Expressions.clear();
724  MappingRegions.clear();
725  auto &R = MappingRecords[CurrentRecord];
727  R.CoverageMapping,
728  makeArrayRef(Filenames).slice(R.FilenamesBegin, R.FilenamesSize),
729  FunctionsFilenames, Expressions, MappingRegions);
730  if (auto Err = Reader.read())
731  return Err;
732 
733  Record.FunctionName = R.FunctionName;
734  Record.FunctionHash = R.FunctionHash;
735  Record.Filenames = FunctionsFilenames;
736  Record.Expressions = Expressions;
737  Record.MappingRegions = MappingRegions;
738 
739  ++CurrentRecord;
740  return Error::success();
741 }
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:81
void push_back(const T &Elt)
Definition: SmallVector.h:211
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
Definition: InstrProf.h:410
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
static const unsigned EncodingTagMask
Reader for the raw coverage mapping data.
static Counter getZero()
Return the counter that represents the number zero.
coveragemap_error get() const
static int Counter
Checks if the given coverage mapping data is exported for an unused function.
std::string getInstrProfSectionName(InstrProfSectKind IPSK, Triple::ObjectFormatType OF, bool AddSegmentInfo=true)
Return the name of the profile section corresponding to IPSK.
Definition: InstrProf.cpp:163
A Counter expression is a value that represents an arithmetic operation with two counters.
Error takeError()
Take ownership of the stored error.
Definition: Error.h:764
ArrayRef< CounterMappingRegion > MappingRegions
static StringRef readString(const uint8_t *&Ptr)
gvn Early GVN Hoisting of Expressions
Definition: GVNHoist.cpp:1042
Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr)
Create a Binary from Source, autodetecting the file type.
Definition: Binary.cpp:45
Reader for the coverage mapping data that is emitted by the frontend and stored in an object file...
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
ArrayRef< CounterExpression > Expressions
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
#define F(x, y, z)
Definition: MD5.cpp:55
StringRef getFuncName(uint64_t FuncNameAddress, size_t NameSize)
Return function's PGO name from the function name's symbol address in the object file.
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:281
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
Error readIntMax(uint64_t &Result, uint64_t MaxPlus1)
const char * Name
Error readNextRecord(CoverageMappingRecord &Record) override
#define P(N)
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&...args)
Constructs a new T() with the given args and returns a unique_ptr<T> which owns the object...
Definition: STLExtras.h:943
Coverage mapping information for a single function.
void dump(const Counter &C, raw_ostream &OS) const
static Error readCoverageMappingData(InstrProfSymtab &ProfileNames, StringRef Data, std::vector< BinaryCoverageReader::ProfileMappingRecord > &Records, std::vector< StringRef > &Filenames)
size_t alignmentAdjustment(const void *Ptr, size_t Alignment)
Returns the necessary adjustment for aligning Ptr to Alignment bytes, rounding up.
Definition: MathExtras.h:626
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:78
Reader for the raw coverage filenames.
A CodeRegion associates some code with a counter.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:598
A Counter mapping context is used to connect the counters, expressions and the obtained counter value...
A Counter mapping region associates a source range with a specific counter.
uint64_t decodeULEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a ULEB128 value.
Definition: LEB128.h:114
static const unsigned End
void handleAllErrors(Error E, HandlerTs &&...Handlers)
Behaves the same as handleErrors, except that it requires that all errors be handled by the given han...
Definition: Error.h:555
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:593
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
A file format agnostic iterator over coverage mapping data.
Error create(object::SectionRef &Section)
Create InstrProfSymtab from an object file section which contains function PGO names.
print lazy value Lazy Value Info Printer Pass
static ErrorSuccess success()
Create a success value.
Definition: Error.h:311
static uint64_t readULEB128(const uint8_t *&Ptr)
#define E
Definition: LargeTest.cpp:27
static Expected< SectionRef > lookupSection(ObjectFile &OF, StringRef Name)
static const unsigned EncodingExpansionRegionBit
static Error loadBinaryFormat(MemoryBufferRef ObjectBuffer, InstrProfSymtab &ProfileNames, StringRef &CoverageMapping, uint8_t &BytesInAddress, support::endianness &Endian, StringRef Arch)
const size_t N
static const char * TestingFormatMagic
static Expected< std::unique_ptr< BinaryCoverageReader > > create(std::unique_ptr< MemoryBuffer > &ObjectBuffer, StringRef Arch)
An ExpansionRegion represents a file expansion region that associates a source range with the expansi...
A SkippedRegion represents a source range with code that was skipped by a preprocessor or similar mea...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
static const unsigned EncodingCounterTagAndExpansionRegionTagBits
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
llvm_unreachable("Unknown RegBank!")
The mapping of profile information to coverage data.
static Error loadTestingFormat(StringRef Data, InstrProfSymtab &ProfileNames, StringRef &CoverageMapping, uint8_t &BytesInAddress, support::endianness &Endian)
#define C
Definition: LargeTest.cpp:25
#define I(x, y, z)
Definition: MD5.cpp:58
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const unsigned EncodingTagBits
static Expected< bool > isCoverageMappingDummy(uint64_t Hash, StringRef Mapping)
LLVM Value Representation.
Definition: Value.h:73
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
#define DEBUG(X)
Definition: Debug.h:118
std::string Hash(const Unit &U)
Definition: FuzzerSHA1.cpp:216
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static Counter getExpression(unsigned ExpressionId)
Return the counter that corresponds to a specific addition counter expression.
A Counter is an abstract value that describes how to compute the execution count for a region of code...
const uint64_t Version
Definition: InstrProf.h:864
static Counter getCounter(unsigned CounterId)
Return the counter that corresponds to a specific profile counter.
void resize(size_type N)
Definition: SmallVector.h:352