xref: /llvm-project/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp (revision d4efc3e097f40afbe8ae275150f49bb08fc04572)
1 //===- CoverageMappingReader.cpp - Code coverage mapping reader -----------===//
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 // This file contains support for reading coverage mapping data for
10 // instrumentation based coverage.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/BinaryFormat/Wasm.h"
22 #include "llvm/Object/Archive.h"
23 #include "llvm/Object/Binary.h"
24 #include "llvm/Object/COFF.h"
25 #include "llvm/Object/Error.h"
26 #include "llvm/Object/MachOUniversal.h"
27 #include "llvm/Object/ObjectFile.h"
28 #include "llvm/Object/Wasm.h"
29 #include "llvm/ProfileData/InstrProf.h"
30 #include "llvm/Support/Casting.h"
31 #include "llvm/Support/Compression.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/Endian.h"
34 #include "llvm/Support/Error.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/LEB128.h"
37 #include "llvm/Support/MathExtras.h"
38 #include "llvm/Support/Path.h"
39 #include "llvm/Support/raw_ostream.h"
40 #include "llvm/TargetParser/Triple.h"
41 #include <vector>
42 
43 using namespace llvm;
44 using namespace coverage;
45 using namespace object;
46 
47 #define DEBUG_TYPE "coverage-mapping"
48 
49 STATISTIC(CovMapNumRecords, "The # of coverage function records");
50 STATISTIC(CovMapNumUsedRecords, "The # of used coverage function records");
51 
52 void CoverageMappingIterator::increment() {
53   if (ReadErr != coveragemap_error::success)
54     return;
55 
56   // Check if all the records were read or if an error occurred while reading
57   // the next record.
58   if (auto E = Reader->readNextRecord(Record))
59     handleAllErrors(std::move(E), [&](const CoverageMapError &CME) {
60       if (CME.get() == coveragemap_error::eof)
61         *this = CoverageMappingIterator();
62       else
63         ReadErr = CME.get();
64     });
65 }
66 
67 Error RawCoverageReader::readULEB128(uint64_t &Result) {
68   if (Data.empty())
69     return make_error<CoverageMapError>(coveragemap_error::truncated);
70   unsigned N = 0;
71   Result = decodeULEB128(Data.bytes_begin(), &N);
72   if (N > Data.size())
73     return make_error<CoverageMapError>(coveragemap_error::malformed,
74                                         "the size of ULEB128 is too big");
75   Data = Data.substr(N);
76   return Error::success();
77 }
78 
79 Error RawCoverageReader::readIntMax(uint64_t &Result, uint64_t MaxPlus1) {
80   if (auto Err = readULEB128(Result))
81     return Err;
82   if (Result >= MaxPlus1)
83     return make_error<CoverageMapError>(
84         coveragemap_error::malformed,
85         "the value of ULEB128 is greater than or equal to MaxPlus1");
86   return Error::success();
87 }
88 
89 Error RawCoverageReader::readSize(uint64_t &Result) {
90   if (auto Err = readULEB128(Result))
91     return Err;
92   if (Result > Data.size())
93     return make_error<CoverageMapError>(coveragemap_error::malformed,
94                                         "the value of ULEB128 is too big");
95   return Error::success();
96 }
97 
98 Error RawCoverageReader::readString(StringRef &Result) {
99   uint64_t Length;
100   if (auto Err = readSize(Length))
101     return Err;
102   Result = Data.substr(0, Length);
103   Data = Data.substr(Length);
104   return Error::success();
105 }
106 
107 Error RawCoverageFilenamesReader::read(CovMapVersion Version) {
108   uint64_t NumFilenames;
109   if (auto Err = readSize(NumFilenames))
110     return Err;
111   if (!NumFilenames)
112     return make_error<CoverageMapError>(coveragemap_error::malformed,
113                                         "number of filenames is zero");
114 
115   if (Version < CovMapVersion::Version4)
116     return readUncompressed(Version, NumFilenames);
117 
118   // The uncompressed length may exceed the size of the encoded filenames.
119   // Skip size validation.
120   uint64_t UncompressedLen;
121   if (auto Err = readULEB128(UncompressedLen))
122     return Err;
123 
124   uint64_t CompressedLen;
125   if (auto Err = readSize(CompressedLen))
126     return Err;
127 
128   if (CompressedLen > 0) {
129     if (!compression::zlib::isAvailable())
130       return make_error<CoverageMapError>(
131           coveragemap_error::decompression_failed);
132 
133     // Allocate memory for the decompressed filenames.
134     SmallVector<uint8_t, 0> StorageBuf;
135 
136     // Read compressed filenames.
137     StringRef CompressedFilenames = Data.substr(0, CompressedLen);
138     Data = Data.substr(CompressedLen);
139     auto Err = compression::zlib::decompress(
140         arrayRefFromStringRef(CompressedFilenames), StorageBuf,
141         UncompressedLen);
142     if (Err) {
143       consumeError(std::move(Err));
144       return make_error<CoverageMapError>(
145           coveragemap_error::decompression_failed);
146     }
147 
148     RawCoverageFilenamesReader Delegate(toStringRef(StorageBuf), Filenames,
149                                         CompilationDir);
150     return Delegate.readUncompressed(Version, NumFilenames);
151   }
152 
153   return readUncompressed(Version, NumFilenames);
154 }
155 
156 Error RawCoverageFilenamesReader::readUncompressed(CovMapVersion Version,
157                                                    uint64_t NumFilenames) {
158   // Read uncompressed filenames.
159   if (Version < CovMapVersion::Version6) {
160     for (size_t I = 0; I < NumFilenames; ++I) {
161       StringRef Filename;
162       if (auto Err = readString(Filename))
163         return Err;
164       Filenames.push_back(Filename.str());
165     }
166   } else {
167     StringRef CWD;
168     if (auto Err = readString(CWD))
169       return Err;
170     Filenames.push_back(CWD.str());
171 
172     for (size_t I = 1; I < NumFilenames; ++I) {
173       StringRef Filename;
174       if (auto Err = readString(Filename))
175         return Err;
176       if (sys::path::is_absolute(Filename)) {
177         Filenames.push_back(Filename.str());
178       } else {
179         SmallString<256> P;
180         if (!CompilationDir.empty())
181           P.assign(CompilationDir);
182         else
183           P.assign(CWD);
184         llvm::sys::path::append(P, Filename);
185         sys::path::remove_dots(P, /*remove_dot_dot=*/true);
186         Filenames.push_back(static_cast<std::string>(P.str()));
187       }
188     }
189   }
190   return Error::success();
191 }
192 
193 Error RawCoverageMappingReader::decodeCounter(unsigned Value, Counter &C) {
194   auto Tag = Value & Counter::EncodingTagMask;
195   switch (Tag) {
196   case Counter::Zero:
197     C = Counter::getZero();
198     return Error::success();
199   case Counter::CounterValueReference:
200     C = Counter::getCounter(Value >> Counter::EncodingTagBits);
201     return Error::success();
202   default:
203     break;
204   }
205   Tag -= Counter::Expression;
206   switch (Tag) {
207   case CounterExpression::Subtract:
208   case CounterExpression::Add: {
209     auto ID = Value >> Counter::EncodingTagBits;
210     if (ID >= Expressions.size())
211       return make_error<CoverageMapError>(coveragemap_error::malformed,
212                                           "counter expression is invalid");
213     Expressions[ID].Kind = CounterExpression::ExprKind(Tag);
214     C = Counter::getExpression(ID);
215     break;
216   }
217   default:
218     return make_error<CoverageMapError>(coveragemap_error::malformed,
219                                         "counter expression kind is invalid");
220   }
221   return Error::success();
222 }
223 
224 Error RawCoverageMappingReader::readCounter(Counter &C) {
225   uint64_t EncodedCounter;
226   if (auto Err =
227           readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max()))
228     return Err;
229   if (auto Err = decodeCounter(EncodedCounter, C))
230     return Err;
231   return Error::success();
232 }
233 
234 static const unsigned EncodingExpansionRegionBit = 1
235                                                    << Counter::EncodingTagBits;
236 
237 /// Read the sub-array of regions for the given inferred file id.
238 /// \param NumFileIDs the number of file ids that are defined for this
239 /// function.
240 Error RawCoverageMappingReader::readMappingRegionsSubArray(
241     std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID,
242     size_t NumFileIDs) {
243   uint64_t NumRegions;
244   if (auto Err = readSize(NumRegions))
245     return Err;
246   unsigned LineStart = 0;
247   for (size_t I = 0; I < NumRegions; ++I) {
248     Counter C, C2;
249     uint64_t BIDX, NC;
250     // They are stored as internal values plus 1 (min is -1)
251     uint64_t ID1, TID1, FID1;
252     mcdc::Parameters Params;
253     CounterMappingRegion::RegionKind Kind = CounterMappingRegion::CodeRegion;
254 
255     // Read the combined counter + region kind.
256     uint64_t EncodedCounterAndRegion;
257     if (auto Err = readIntMax(EncodedCounterAndRegion,
258                               std::numeric_limits<unsigned>::max()))
259       return Err;
260     unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
261     uint64_t ExpandedFileID = 0;
262 
263     // If Tag does not represent a ZeroCounter, then it is understood to refer
264     // to a counter or counter expression with region kind assumed to be
265     // "CodeRegion". In that case, EncodedCounterAndRegion actually encodes the
266     // referenced counter or counter expression (and nothing else).
267     //
268     // If Tag represents a ZeroCounter and EncodingExpansionRegionBit is set,
269     // then EncodedCounterAndRegion is interpreted to represent an
270     // ExpansionRegion. In all other cases, EncodedCounterAndRegion is
271     // interpreted to refer to a specific region kind, after which additional
272     // fields may be read (e.g. BranchRegions have two encoded counters that
273     // follow an encoded region kind value).
274     if (Tag != Counter::Zero) {
275       if (auto Err = decodeCounter(EncodedCounterAndRegion, C))
276         return Err;
277     } else {
278       // Is it an expansion region?
279       if (EncodedCounterAndRegion & EncodingExpansionRegionBit) {
280         Kind = CounterMappingRegion::ExpansionRegion;
281         ExpandedFileID = EncodedCounterAndRegion >>
282                          Counter::EncodingCounterTagAndExpansionRegionTagBits;
283         if (ExpandedFileID >= NumFileIDs)
284           return make_error<CoverageMapError>(coveragemap_error::malformed,
285                                               "ExpandedFileID is invalid");
286       } else {
287         switch (EncodedCounterAndRegion >>
288                 Counter::EncodingCounterTagAndExpansionRegionTagBits) {
289         case CounterMappingRegion::CodeRegion:
290           // Don't do anything when we have a code region with a zero counter.
291           break;
292         case CounterMappingRegion::SkippedRegion:
293           Kind = CounterMappingRegion::SkippedRegion;
294           break;
295         case CounterMappingRegion::BranchRegion:
296           // For a Branch Region, read two successive counters.
297           Kind = CounterMappingRegion::BranchRegion;
298           if (auto Err = readCounter(C))
299             return Err;
300           if (auto Err = readCounter(C2))
301             return Err;
302           break;
303         case CounterMappingRegion::MCDCBranchRegion:
304           // For a MCDC Branch Region, read two successive counters and 3 IDs.
305           Kind = CounterMappingRegion::MCDCBranchRegion;
306           if (auto Err = readCounter(C))
307             return Err;
308           if (auto Err = readCounter(C2))
309             return Err;
310           if (auto Err = readIntMax(ID1, std::numeric_limits<int16_t>::max()))
311             return Err;
312           if (auto Err = readIntMax(TID1, std::numeric_limits<int16_t>::max()))
313             return Err;
314           if (auto Err = readIntMax(FID1, std::numeric_limits<int16_t>::max()))
315             return Err;
316           if (ID1 == 0)
317             return make_error<CoverageMapError>(
318                 coveragemap_error::malformed,
319                 "MCDCConditionID shouldn't be zero");
320           Params = mcdc::BranchParameters{
321               static_cast<int16_t>(static_cast<int16_t>(ID1) - 1),
322               {static_cast<int16_t>(static_cast<int16_t>(FID1) - 1),
323                static_cast<int16_t>(static_cast<int16_t>(TID1) - 1)}};
324           break;
325         case CounterMappingRegion::MCDCDecisionRegion:
326           Kind = CounterMappingRegion::MCDCDecisionRegion;
327           if (auto Err = readIntMax(BIDX, std::numeric_limits<unsigned>::max()))
328             return Err;
329           if (auto Err = readIntMax(NC, std::numeric_limits<int16_t>::max()))
330             return Err;
331           Params = mcdc::DecisionParameters{static_cast<unsigned>(BIDX),
332                                             static_cast<uint16_t>(NC)};
333           break;
334         default:
335           return make_error<CoverageMapError>(coveragemap_error::malformed,
336                                               "region kind is incorrect");
337         }
338       }
339     }
340 
341     // Read the source range.
342     uint64_t LineStartDelta, ColumnStart, NumLines, ColumnEnd;
343     if (auto Err =
344             readIntMax(LineStartDelta, std::numeric_limits<unsigned>::max()))
345       return Err;
346     if (auto Err = readULEB128(ColumnStart))
347       return Err;
348     if (ColumnStart > std::numeric_limits<unsigned>::max())
349       return make_error<CoverageMapError>(coveragemap_error::malformed,
350                                           "start column is too big");
351     if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max()))
352       return Err;
353     if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max()))
354       return Err;
355     LineStart += LineStartDelta;
356 
357     // If the high bit of ColumnEnd is set, this is a gap region.
358     if (ColumnEnd & (1U << 31)) {
359       Kind = CounterMappingRegion::GapRegion;
360       ColumnEnd &= ~(1U << 31);
361     }
362 
363     // Adjust the column locations for the empty regions that are supposed to
364     // cover whole lines. Those regions should be encoded with the
365     // column range (1 -> std::numeric_limits<unsigned>::max()), but because
366     // the encoded std::numeric_limits<unsigned>::max() is several bytes long,
367     // we set the column range to (0 -> 0) to ensure that the column start and
368     // column end take up one byte each.
369     // The std::numeric_limits<unsigned>::max() is used to represent a column
370     // position at the end of the line without knowing the length of that line.
371     if (ColumnStart == 0 && ColumnEnd == 0) {
372       ColumnStart = 1;
373       ColumnEnd = std::numeric_limits<unsigned>::max();
374     }
375 
376     LLVM_DEBUG({
377       dbgs() << "Counter in file " << InferredFileID << " " << LineStart << ":"
378              << ColumnStart << " -> " << (LineStart + NumLines) << ":"
379              << ColumnEnd << ", ";
380       if (Kind == CounterMappingRegion::ExpansionRegion)
381         dbgs() << "Expands to file " << ExpandedFileID;
382       else
383         CounterMappingContext(Expressions).dump(C, dbgs());
384       dbgs() << "\n";
385     });
386 
387     auto CMR = CounterMappingRegion(
388         C, C2, InferredFileID, ExpandedFileID, LineStart, ColumnStart,
389         LineStart + NumLines, ColumnEnd, Kind, Params);
390     if (CMR.startLoc() > CMR.endLoc())
391       return make_error<CoverageMapError>(
392           coveragemap_error::malformed,
393           "counter mapping region locations are incorrect");
394     MappingRegions.push_back(CMR);
395   }
396   return Error::success();
397 }
398 
399 Error RawCoverageMappingReader::read() {
400   // Read the virtual file mapping.
401   SmallVector<unsigned, 8> VirtualFileMapping;
402   uint64_t NumFileMappings;
403   if (auto Err = readSize(NumFileMappings))
404     return Err;
405   for (size_t I = 0; I < NumFileMappings; ++I) {
406     uint64_t FilenameIndex;
407     if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size()))
408       return Err;
409     VirtualFileMapping.push_back(FilenameIndex);
410   }
411 
412   // Construct the files using unique filenames and virtual file mapping.
413   for (auto I : VirtualFileMapping) {
414     Filenames.push_back(TranslationUnitFilenames[I]);
415   }
416 
417   // Read the expressions.
418   uint64_t NumExpressions;
419   if (auto Err = readSize(NumExpressions))
420     return Err;
421   // Create an array of dummy expressions that get the proper counters
422   // when the expressions are read, and the proper kinds when the counters
423   // are decoded.
424   Expressions.resize(
425       NumExpressions,
426       CounterExpression(CounterExpression::Subtract, Counter(), Counter()));
427   for (size_t I = 0; I < NumExpressions; ++I) {
428     if (auto Err = readCounter(Expressions[I].LHS))
429       return Err;
430     if (auto Err = readCounter(Expressions[I].RHS))
431       return Err;
432   }
433 
434   // Read the mapping regions sub-arrays.
435   for (unsigned InferredFileID = 0, S = VirtualFileMapping.size();
436        InferredFileID < S; ++InferredFileID) {
437     if (auto Err = readMappingRegionsSubArray(MappingRegions, InferredFileID,
438                                               VirtualFileMapping.size()))
439       return Err;
440   }
441 
442   // Set the counters for the expansion regions.
443   // i.e. Counter of expansion region = counter of the first region
444   // from the expanded file.
445   // Perform multiple passes to correctly propagate the counters through
446   // all the nested expansion regions.
447   SmallVector<CounterMappingRegion *, 8> FileIDExpansionRegionMapping;
448   FileIDExpansionRegionMapping.resize(VirtualFileMapping.size(), nullptr);
449   for (unsigned Pass = 1, S = VirtualFileMapping.size(); Pass < S; ++Pass) {
450     for (auto &R : MappingRegions) {
451       if (R.Kind != CounterMappingRegion::ExpansionRegion)
452         continue;
453       assert(!FileIDExpansionRegionMapping[R.ExpandedFileID]);
454       FileIDExpansionRegionMapping[R.ExpandedFileID] = &R;
455     }
456     for (auto &R : MappingRegions) {
457       if (FileIDExpansionRegionMapping[R.FileID]) {
458         FileIDExpansionRegionMapping[R.FileID]->Count = R.Count;
459         FileIDExpansionRegionMapping[R.FileID] = nullptr;
460       }
461     }
462   }
463 
464   return Error::success();
465 }
466 
467 Expected<bool> RawCoverageMappingDummyChecker::isDummy() {
468   // A dummy coverage mapping data consists of just one region with zero count.
469   uint64_t NumFileMappings;
470   if (Error Err = readSize(NumFileMappings))
471     return std::move(Err);
472   if (NumFileMappings != 1)
473     return false;
474   // We don't expect any specific value for the filename index, just skip it.
475   uint64_t FilenameIndex;
476   if (Error Err =
477           readIntMax(FilenameIndex, std::numeric_limits<unsigned>::max()))
478     return std::move(Err);
479   uint64_t NumExpressions;
480   if (Error Err = readSize(NumExpressions))
481     return std::move(Err);
482   if (NumExpressions != 0)
483     return false;
484   uint64_t NumRegions;
485   if (Error Err = readSize(NumRegions))
486     return std::move(Err);
487   if (NumRegions != 1)
488     return false;
489   uint64_t EncodedCounterAndRegion;
490   if (Error Err = readIntMax(EncodedCounterAndRegion,
491                              std::numeric_limits<unsigned>::max()))
492     return std::move(Err);
493   unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
494   return Tag == Counter::Zero;
495 }
496 
497 Error InstrProfSymtab::create(SectionRef &Section) {
498   Expected<StringRef> DataOrErr = Section.getContents();
499   if (!DataOrErr)
500     return DataOrErr.takeError();
501   Data = *DataOrErr;
502   Address = Section.getAddress();
503 
504   // If this is a linked PE/COFF file, then we have to skip over the null byte
505   // that is allocated in the .lprfn$A section in the LLVM profiling runtime.
506   // If the name section is .lprfcovnames, it doesn't have the null byte at the
507   // beginning.
508   const ObjectFile *Obj = Section.getObject();
509   if (isa<COFFObjectFile>(Obj) && !Obj->isRelocatableObject())
510     if (Expected<StringRef> NameOrErr = Section.getName())
511       if (*NameOrErr != getInstrProfSectionName(IPSK_covname, Triple::COFF))
512         Data = Data.drop_front(1);
513 
514   return Error::success();
515 }
516 
517 StringRef InstrProfSymtab::getFuncName(uint64_t Pointer, size_t Size) {
518   if (Pointer < Address)
519     return StringRef();
520   auto Offset = Pointer - Address;
521   if (Offset + Size > Data.size())
522     return StringRef();
523   return Data.substr(Pointer - Address, Size);
524 }
525 
526 // Check if the mapping data is a dummy, i.e. is emitted for an unused function.
527 static Expected<bool> isCoverageMappingDummy(uint64_t Hash, StringRef Mapping) {
528   // The hash value of dummy mapping records is always zero.
529   if (Hash)
530     return false;
531   return RawCoverageMappingDummyChecker(Mapping).isDummy();
532 }
533 
534 /// A range of filename indices. Used to specify the location of a batch of
535 /// filenames in a vector-like container.
536 struct FilenameRange {
537   unsigned StartingIndex;
538   unsigned Length;
539 
540   FilenameRange(unsigned StartingIndex, unsigned Length)
541       : StartingIndex(StartingIndex), Length(Length) {}
542 
543   void markInvalid() { Length = 0; }
544   bool isInvalid() const { return Length == 0; }
545 };
546 
547 namespace {
548 
549 /// The interface to read coverage mapping function records for a module.
550 struct CovMapFuncRecordReader {
551   virtual ~CovMapFuncRecordReader() = default;
552 
553   // Read a coverage header.
554   //
555   // \p CovBuf points to the buffer containing the \c CovHeader of the coverage
556   // mapping data associated with the module.
557   //
558   // Returns a pointer to the next \c CovHeader if it exists, or to an address
559   // greater than \p CovEnd if not.
560   virtual Expected<const char *> readCoverageHeader(const char *CovBuf,
561                                                     const char *CovBufEnd) = 0;
562 
563   // Read function records.
564   //
565   // \p FuncRecBuf points to the buffer containing a batch of function records.
566   // \p FuncRecBufEnd points past the end of the batch of records.
567   //
568   // Prior to Version4, \p OutOfLineFileRange points to a sequence of filenames
569   // associated with the function records. It is unused in Version4.
570   //
571   // Prior to Version4, \p OutOfLineMappingBuf points to a sequence of coverage
572   // mappings associated with the function records. It is unused in Version4.
573   virtual Error
574   readFunctionRecords(const char *FuncRecBuf, const char *FuncRecBufEnd,
575                       std::optional<FilenameRange> OutOfLineFileRange,
576                       const char *OutOfLineMappingBuf,
577                       const char *OutOfLineMappingBufEnd) = 0;
578 
579   template <class IntPtrT, llvm::endianness Endian>
580   static Expected<std::unique_ptr<CovMapFuncRecordReader>>
581   get(CovMapVersion Version, InstrProfSymtab &P,
582       std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, StringRef D,
583       std::vector<std::string> &F);
584 };
585 
586 // A class for reading coverage mapping function records for a module.
587 template <CovMapVersion Version, class IntPtrT, llvm::endianness Endian>
588 class VersionedCovMapFuncRecordReader : public CovMapFuncRecordReader {
589   using FuncRecordType =
590       typename CovMapTraits<Version, IntPtrT>::CovMapFuncRecordType;
591   using NameRefType = typename CovMapTraits<Version, IntPtrT>::NameRefType;
592 
593   // Maps function's name references to the indexes of their records
594   // in \c Records.
595   DenseMap<NameRefType, size_t> FunctionRecords;
596   InstrProfSymtab &ProfileNames;
597   StringRef CompilationDir;
598   std::vector<std::string> &Filenames;
599   std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records;
600 
601   // Maps a hash of the filenames in a TU to a \c FileRange. The range
602   // specifies the location of the hashed filenames in \c Filenames.
603   DenseMap<uint64_t, FilenameRange> FileRangeMap;
604 
605   // Add the record to the collection if we don't already have a record that
606   // points to the same function name. This is useful to ignore the redundant
607   // records for the functions with ODR linkage.
608   // In addition, prefer records with real coverage mapping data to dummy
609   // records, which were emitted for inline functions which were seen but
610   // not used in the corresponding translation unit.
611   Error insertFunctionRecordIfNeeded(const FuncRecordType *CFR,
612                                      StringRef Mapping,
613                                      FilenameRange FileRange) {
614     ++CovMapNumRecords;
615     uint64_t FuncHash = CFR->template getFuncHash<Endian>();
616     NameRefType NameRef = CFR->template getFuncNameRef<Endian>();
617     auto InsertResult =
618         FunctionRecords.insert(std::make_pair(NameRef, Records.size()));
619     if (InsertResult.second) {
620       StringRef FuncName;
621       if (Error Err = CFR->template getFuncName<Endian>(ProfileNames, FuncName))
622         return Err;
623       if (FuncName.empty())
624         return make_error<InstrProfError>(instrprof_error::malformed,
625                                           "function name is empty");
626       ++CovMapNumUsedRecords;
627       Records.emplace_back(Version, FuncName, FuncHash, Mapping,
628                            FileRange.StartingIndex, FileRange.Length);
629       return Error::success();
630     }
631     // Update the existing record if it's a dummy and the new record is real.
632     size_t OldRecordIndex = InsertResult.first->second;
633     BinaryCoverageReader::ProfileMappingRecord &OldRecord =
634         Records[OldRecordIndex];
635     Expected<bool> OldIsDummyExpected = isCoverageMappingDummy(
636         OldRecord.FunctionHash, OldRecord.CoverageMapping);
637     if (Error Err = OldIsDummyExpected.takeError())
638       return Err;
639     if (!*OldIsDummyExpected)
640       return Error::success();
641     Expected<bool> NewIsDummyExpected =
642         isCoverageMappingDummy(FuncHash, Mapping);
643     if (Error Err = NewIsDummyExpected.takeError())
644       return Err;
645     if (*NewIsDummyExpected)
646       return Error::success();
647     ++CovMapNumUsedRecords;
648     OldRecord.FunctionHash = FuncHash;
649     OldRecord.CoverageMapping = Mapping;
650     OldRecord.FilenamesBegin = FileRange.StartingIndex;
651     OldRecord.FilenamesSize = FileRange.Length;
652     return Error::success();
653   }
654 
655 public:
656   VersionedCovMapFuncRecordReader(
657       InstrProfSymtab &P,
658       std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, StringRef D,
659       std::vector<std::string> &F)
660       : ProfileNames(P), CompilationDir(D), Filenames(F), Records(R) {}
661 
662   ~VersionedCovMapFuncRecordReader() override = default;
663 
664   Expected<const char *> readCoverageHeader(const char *CovBuf,
665                                             const char *CovBufEnd) override {
666     using namespace support;
667 
668     if (CovBuf + sizeof(CovMapHeader) > CovBufEnd)
669       return make_error<CoverageMapError>(
670           coveragemap_error::malformed,
671           "coverage mapping header section is larger than buffer size");
672     auto CovHeader = reinterpret_cast<const CovMapHeader *>(CovBuf);
673     uint32_t NRecords = CovHeader->getNRecords<Endian>();
674     uint32_t FilenamesSize = CovHeader->getFilenamesSize<Endian>();
675     uint32_t CoverageSize = CovHeader->getCoverageSize<Endian>();
676     assert((CovMapVersion)CovHeader->getVersion<Endian>() == Version);
677     CovBuf = reinterpret_cast<const char *>(CovHeader + 1);
678 
679     // Skip past the function records, saving the start and end for later.
680     // This is a no-op in Version4 (function records are read after all headers
681     // are read).
682     const char *FuncRecBuf = nullptr;
683     const char *FuncRecBufEnd = nullptr;
684     if (Version < CovMapVersion::Version4)
685       FuncRecBuf = CovBuf;
686     CovBuf += NRecords * sizeof(FuncRecordType);
687     if (Version < CovMapVersion::Version4)
688       FuncRecBufEnd = CovBuf;
689 
690     // Get the filenames.
691     if (CovBuf + FilenamesSize > CovBufEnd)
692       return make_error<CoverageMapError>(
693           coveragemap_error::malformed,
694           "filenames section is larger than buffer size");
695     size_t FilenamesBegin = Filenames.size();
696     StringRef FilenameRegion(CovBuf, FilenamesSize);
697     RawCoverageFilenamesReader Reader(FilenameRegion, Filenames,
698                                       CompilationDir);
699     if (auto Err = Reader.read(Version))
700       return std::move(Err);
701     CovBuf += FilenamesSize;
702     FilenameRange FileRange(FilenamesBegin, Filenames.size() - FilenamesBegin);
703 
704     if (Version >= CovMapVersion::Version4) {
705       // Map a hash of the filenames region to the filename range associated
706       // with this coverage header.
707       int64_t FilenamesRef =
708           llvm::IndexedInstrProf::ComputeHash(FilenameRegion);
709       auto Insert =
710           FileRangeMap.insert(std::make_pair(FilenamesRef, FileRange));
711       if (!Insert.second) {
712         // The same filenames ref was encountered twice. It's possible that
713         // the associated filenames are the same.
714         auto It = Filenames.begin();
715         FilenameRange &OrigRange = Insert.first->getSecond();
716         if (std::equal(It + OrigRange.StartingIndex,
717                        It + OrigRange.StartingIndex + OrigRange.Length,
718                        It + FileRange.StartingIndex,
719                        It + FileRange.StartingIndex + FileRange.Length))
720           // Map the new range to the original one.
721           FileRange = OrigRange;
722         else
723           // This is a hash collision. Mark the filenames ref invalid.
724           OrigRange.markInvalid();
725       }
726     }
727 
728     // We'll read the coverage mapping records in the loop below.
729     // This is a no-op in Version4 (coverage mappings are not affixed to the
730     // coverage header).
731     const char *MappingBuf = CovBuf;
732     if (Version >= CovMapVersion::Version4 && CoverageSize != 0)
733       return make_error<CoverageMapError>(coveragemap_error::malformed,
734                                           "coverage mapping size is not zero");
735     CovBuf += CoverageSize;
736     const char *MappingEnd = CovBuf;
737 
738     if (CovBuf > CovBufEnd)
739       return make_error<CoverageMapError>(
740           coveragemap_error::malformed,
741           "function records section is larger than buffer size");
742 
743     if (Version < CovMapVersion::Version4) {
744       // Read each function record.
745       if (Error E = readFunctionRecords(FuncRecBuf, FuncRecBufEnd, FileRange,
746                                         MappingBuf, MappingEnd))
747         return std::move(E);
748     }
749 
750     // Each coverage map has an alignment of 8, so we need to adjust alignment
751     // before reading the next map.
752     CovBuf += offsetToAlignedAddr(CovBuf, Align(8));
753 
754     return CovBuf;
755   }
756 
757   Error readFunctionRecords(const char *FuncRecBuf, const char *FuncRecBufEnd,
758                             std::optional<FilenameRange> OutOfLineFileRange,
759                             const char *OutOfLineMappingBuf,
760                             const char *OutOfLineMappingBufEnd) override {
761     auto CFR = reinterpret_cast<const FuncRecordType *>(FuncRecBuf);
762     while ((const char *)CFR < FuncRecBufEnd) {
763       // Validate the length of the coverage mapping for this function.
764       const char *NextMappingBuf;
765       const FuncRecordType *NextCFR;
766       std::tie(NextMappingBuf, NextCFR) =
767           CFR->template advanceByOne<Endian>(OutOfLineMappingBuf);
768       if (Version < CovMapVersion::Version4)
769         if (NextMappingBuf > OutOfLineMappingBufEnd)
770           return make_error<CoverageMapError>(
771               coveragemap_error::malformed,
772               "next mapping buffer is larger than buffer size");
773 
774       // Look up the set of filenames associated with this function record.
775       std::optional<FilenameRange> FileRange;
776       if (Version < CovMapVersion::Version4) {
777         FileRange = OutOfLineFileRange;
778       } else {
779         uint64_t FilenamesRef = CFR->template getFilenamesRef<Endian>();
780         auto It = FileRangeMap.find(FilenamesRef);
781         if (It == FileRangeMap.end())
782           return make_error<CoverageMapError>(
783               coveragemap_error::malformed,
784               "no filename found for function with hash=0x" +
785                   Twine::utohexstr(FilenamesRef));
786         else
787           FileRange = It->getSecond();
788       }
789 
790       // Now, read the coverage data.
791       if (FileRange && !FileRange->isInvalid()) {
792         StringRef Mapping =
793             CFR->template getCoverageMapping<Endian>(OutOfLineMappingBuf);
794         if (Version >= CovMapVersion::Version4 &&
795             Mapping.data() + Mapping.size() > FuncRecBufEnd)
796           return make_error<CoverageMapError>(
797               coveragemap_error::malformed,
798               "coverage mapping data is larger than buffer size");
799         if (Error Err = insertFunctionRecordIfNeeded(CFR, Mapping, *FileRange))
800           return Err;
801       }
802 
803       std::tie(OutOfLineMappingBuf, CFR) = std::tie(NextMappingBuf, NextCFR);
804     }
805     return Error::success();
806   }
807 };
808 
809 } // end anonymous namespace
810 
811 template <class IntPtrT, llvm::endianness Endian>
812 Expected<std::unique_ptr<CovMapFuncRecordReader>> CovMapFuncRecordReader::get(
813     CovMapVersion Version, InstrProfSymtab &P,
814     std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, StringRef D,
815     std::vector<std::string> &F) {
816   using namespace coverage;
817 
818   switch (Version) {
819   case CovMapVersion::Version1:
820     return std::make_unique<VersionedCovMapFuncRecordReader<
821         CovMapVersion::Version1, IntPtrT, Endian>>(P, R, D, F);
822   case CovMapVersion::Version2:
823   case CovMapVersion::Version3:
824   case CovMapVersion::Version4:
825   case CovMapVersion::Version5:
826   case CovMapVersion::Version6:
827   case CovMapVersion::Version7:
828     // Decompress the name data.
829     if (Error E = P.create(P.getNameData()))
830       return std::move(E);
831     if (Version == CovMapVersion::Version2)
832       return std::make_unique<VersionedCovMapFuncRecordReader<
833           CovMapVersion::Version2, IntPtrT, Endian>>(P, R, D, F);
834     else if (Version == CovMapVersion::Version3)
835       return std::make_unique<VersionedCovMapFuncRecordReader<
836           CovMapVersion::Version3, IntPtrT, Endian>>(P, R, D, F);
837     else if (Version == CovMapVersion::Version4)
838       return std::make_unique<VersionedCovMapFuncRecordReader<
839           CovMapVersion::Version4, IntPtrT, Endian>>(P, R, D, F);
840     else if (Version == CovMapVersion::Version5)
841       return std::make_unique<VersionedCovMapFuncRecordReader<
842           CovMapVersion::Version5, IntPtrT, Endian>>(P, R, D, F);
843     else if (Version == CovMapVersion::Version6)
844       return std::make_unique<VersionedCovMapFuncRecordReader<
845           CovMapVersion::Version6, IntPtrT, Endian>>(P, R, D, F);
846     else if (Version == CovMapVersion::Version7)
847       return std::make_unique<VersionedCovMapFuncRecordReader<
848           CovMapVersion::Version7, IntPtrT, Endian>>(P, R, D, F);
849   }
850   llvm_unreachable("Unsupported version");
851 }
852 
853 template <typename T, llvm::endianness Endian>
854 static Error readCoverageMappingData(
855     InstrProfSymtab &ProfileNames, StringRef CovMap, StringRef FuncRecords,
856     std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records,
857     StringRef CompilationDir, std::vector<std::string> &Filenames) {
858   using namespace coverage;
859 
860   // Read the records in the coverage data section.
861   auto CovHeader =
862       reinterpret_cast<const CovMapHeader *>(CovMap.data());
863   CovMapVersion Version = (CovMapVersion)CovHeader->getVersion<Endian>();
864   if (Version > CovMapVersion::CurrentVersion)
865     return make_error<CoverageMapError>(coveragemap_error::unsupported_version);
866   Expected<std::unique_ptr<CovMapFuncRecordReader>> ReaderExpected =
867       CovMapFuncRecordReader::get<T, Endian>(Version, ProfileNames, Records,
868                                              CompilationDir, Filenames);
869   if (Error E = ReaderExpected.takeError())
870     return E;
871   auto Reader = std::move(ReaderExpected.get());
872   const char *CovBuf = CovMap.data();
873   const char *CovBufEnd = CovBuf + CovMap.size();
874   const char *FuncRecBuf = FuncRecords.data();
875   const char *FuncRecBufEnd = FuncRecords.data() + FuncRecords.size();
876   while (CovBuf < CovBufEnd) {
877     // Read the current coverage header & filename data.
878     //
879     // Prior to Version4, this also reads all function records affixed to the
880     // header.
881     //
882     // Return a pointer to the next coverage header.
883     auto NextOrErr = Reader->readCoverageHeader(CovBuf, CovBufEnd);
884     if (auto E = NextOrErr.takeError())
885       return E;
886     CovBuf = NextOrErr.get();
887   }
888   // In Version4, function records are not affixed to coverage headers. Read
889   // the records from their dedicated section.
890   if (Version >= CovMapVersion::Version4)
891     return Reader->readFunctionRecords(FuncRecBuf, FuncRecBufEnd, std::nullopt,
892                                        nullptr, nullptr);
893   return Error::success();
894 }
895 
896 Expected<std::unique_ptr<BinaryCoverageReader>>
897 BinaryCoverageReader::createCoverageReaderFromBuffer(
898     StringRef Coverage, FuncRecordsStorage &&FuncRecords,
899     CoverageMapCopyStorage &&CoverageMap,
900     std::unique_ptr<InstrProfSymtab> ProfileNamesPtr, uint8_t BytesInAddress,
901     llvm::endianness Endian, StringRef CompilationDir) {
902   if (ProfileNamesPtr == nullptr)
903     return make_error<CoverageMapError>(coveragemap_error::malformed,
904                                         "Caller must provide ProfileNames");
905   std::unique_ptr<BinaryCoverageReader> Reader(
906       new BinaryCoverageReader(std::move(ProfileNamesPtr),
907                                std::move(FuncRecords), std::move(CoverageMap)));
908   InstrProfSymtab &ProfileNames = *Reader->ProfileNames;
909   StringRef FuncRecordsRef = Reader->FuncRecords->getBuffer();
910   if (BytesInAddress == 4 && Endian == llvm::endianness::little) {
911     if (Error E = readCoverageMappingData<uint32_t, llvm::endianness::little>(
912             ProfileNames, Coverage, FuncRecordsRef, Reader->MappingRecords,
913             CompilationDir, Reader->Filenames))
914       return std::move(E);
915   } else if (BytesInAddress == 4 && Endian == llvm::endianness::big) {
916     if (Error E = readCoverageMappingData<uint32_t, llvm::endianness::big>(
917             ProfileNames, Coverage, FuncRecordsRef, Reader->MappingRecords,
918             CompilationDir, Reader->Filenames))
919       return std::move(E);
920   } else if (BytesInAddress == 8 && Endian == llvm::endianness::little) {
921     if (Error E = readCoverageMappingData<uint64_t, llvm::endianness::little>(
922             ProfileNames, Coverage, FuncRecordsRef, Reader->MappingRecords,
923             CompilationDir, Reader->Filenames))
924       return std::move(E);
925   } else if (BytesInAddress == 8 && Endian == llvm::endianness::big) {
926     if (Error E = readCoverageMappingData<uint64_t, llvm::endianness::big>(
927             ProfileNames, Coverage, FuncRecordsRef, Reader->MappingRecords,
928             CompilationDir, Reader->Filenames))
929       return std::move(E);
930   } else
931     return make_error<CoverageMapError>(
932         coveragemap_error::malformed,
933         "not supported endianness or bytes in address");
934   return std::move(Reader);
935 }
936 
937 static Expected<std::unique_ptr<BinaryCoverageReader>>
938 loadTestingFormat(StringRef Data, StringRef CompilationDir) {
939   uint8_t BytesInAddress = 8;
940   llvm::endianness Endian = llvm::endianness::little;
941 
942   // Read the magic and version.
943   Data = Data.substr(sizeof(TestingFormatMagic));
944   if (Data.size() < sizeof(uint64_t))
945     return make_error<CoverageMapError>(coveragemap_error::malformed,
946                                         "the size of data is too small");
947   auto TestingVersion =
948       support::endian::byte_swap<uint64_t, llvm::endianness::little>(
949           *reinterpret_cast<const uint64_t *>(Data.data()));
950   Data = Data.substr(sizeof(uint64_t));
951 
952   // Read the ProfileNames data.
953   if (Data.empty())
954     return make_error<CoverageMapError>(coveragemap_error::truncated);
955   unsigned N = 0;
956   uint64_t ProfileNamesSize = decodeULEB128(Data.bytes_begin(), &N);
957   if (N > Data.size())
958     return make_error<CoverageMapError>(
959         coveragemap_error::malformed,
960         "the size of TestingFormatMagic is too big");
961   Data = Data.substr(N);
962   if (Data.empty())
963     return make_error<CoverageMapError>(coveragemap_error::truncated);
964   N = 0;
965   uint64_t Address = decodeULEB128(Data.bytes_begin(), &N);
966   if (N > Data.size())
967     return make_error<CoverageMapError>(coveragemap_error::malformed,
968                                         "the size of ULEB128 is too big");
969   Data = Data.substr(N);
970   if (Data.size() < ProfileNamesSize)
971     return make_error<CoverageMapError>(coveragemap_error::malformed,
972                                         "the size of ProfileNames is too big");
973   auto ProfileNames = std::make_unique<InstrProfSymtab>();
974   if (Error E = ProfileNames->create(Data.substr(0, ProfileNamesSize), Address))
975     return std::move(E);
976   Data = Data.substr(ProfileNamesSize);
977 
978   // In Version2, the size of CoverageMapping is stored directly.
979   uint64_t CoverageMappingSize;
980   if (TestingVersion == uint64_t(TestingFormatVersion::Version2)) {
981     N = 0;
982     CoverageMappingSize = decodeULEB128(Data.bytes_begin(), &N);
983     if (N > Data.size())
984       return make_error<CoverageMapError>(coveragemap_error::malformed,
985                                           "the size of ULEB128 is too big");
986     Data = Data.substr(N);
987     if (CoverageMappingSize < sizeof(CovMapHeader))
988       return make_error<CoverageMapError>(
989           coveragemap_error::malformed,
990           "the size of CoverageMapping is teoo small");
991   } else if (TestingVersion != uint64_t(TestingFormatVersion::Version1)) {
992     return make_error<CoverageMapError>(coveragemap_error::unsupported_version);
993   }
994 
995   // Skip the padding bytes because coverage map data has an alignment of 8.
996   auto Pad = offsetToAlignedAddr(Data.data(), Align(8));
997   if (Data.size() < Pad)
998     return make_error<CoverageMapError>(coveragemap_error::malformed,
999                                         "insufficient padding");
1000   Data = Data.substr(Pad);
1001   if (Data.size() < sizeof(CovMapHeader))
1002     return make_error<CoverageMapError>(
1003         coveragemap_error::malformed,
1004         "coverage mapping header section is larger than data size");
1005   auto const *CovHeader = reinterpret_cast<const CovMapHeader *>(
1006       Data.substr(0, sizeof(CovMapHeader)).data());
1007   auto Version =
1008       CovMapVersion(CovHeader->getVersion<llvm::endianness::little>());
1009 
1010   // In Version1, the size of CoverageMapping is calculated.
1011   if (TestingVersion == uint64_t(TestingFormatVersion::Version1)) {
1012     if (Version < CovMapVersion::Version4) {
1013       CoverageMappingSize = Data.size();
1014     } else {
1015       auto FilenamesSize =
1016           CovHeader->getFilenamesSize<llvm::endianness::little>();
1017       CoverageMappingSize = sizeof(CovMapHeader) + FilenamesSize;
1018     }
1019   }
1020 
1021   auto CoverageMapping = Data.substr(0, CoverageMappingSize);
1022   Data = Data.substr(CoverageMappingSize);
1023 
1024   // Read the CoverageRecords data.
1025   if (Version < CovMapVersion::Version4) {
1026     if (!Data.empty())
1027       return make_error<CoverageMapError>(coveragemap_error::malformed,
1028                                           "data is not empty");
1029   } else {
1030     // Skip the padding bytes because coverage records data has an alignment
1031     // of 8.
1032     Pad = offsetToAlignedAddr(Data.data(), Align(8));
1033     if (Data.size() < Pad)
1034       return make_error<CoverageMapError>(coveragemap_error::malformed,
1035                                           "insufficient padding");
1036     Data = Data.substr(Pad);
1037   }
1038   BinaryCoverageReader::FuncRecordsStorage CoverageRecords =
1039       MemoryBuffer::getMemBuffer(Data);
1040 
1041   return BinaryCoverageReader::createCoverageReaderFromBuffer(
1042       CoverageMapping, std::move(CoverageRecords), nullptr,
1043       std::move(ProfileNames), BytesInAddress, Endian, CompilationDir);
1044 }
1045 
1046 /// Find all sections that match \p IPSK name. There may be more than one if
1047 /// comdats are in use, e.g. for the __llvm_covfun section on ELF.
1048 static Expected<std::vector<SectionRef>>
1049 lookupSections(ObjectFile &OF, InstrProfSectKind IPSK) {
1050   auto ObjFormat = OF.getTripleObjectFormat();
1051   auto Name =
1052       getInstrProfSectionName(IPSK, ObjFormat, /*AddSegmentInfo=*/false);
1053   // On COFF, the object file section name may end in "$M". This tells the
1054   // linker to sort these sections between "$A" and "$Z". The linker removes the
1055   // dollar and everything after it in the final binary. Do the same to match.
1056   bool IsCOFF = isa<COFFObjectFile>(OF);
1057   auto stripSuffix = [IsCOFF](StringRef N) {
1058     return IsCOFF ? N.split('$').first : N;
1059   };
1060   Name = stripSuffix(Name);
1061 
1062   std::vector<SectionRef> Sections;
1063   for (const auto &Section : OF.sections()) {
1064     Expected<StringRef> NameOrErr = Section.getName();
1065     if (!NameOrErr)
1066       return NameOrErr.takeError();
1067     if (stripSuffix(*NameOrErr) == Name) {
1068       // Skip empty profile name section.
1069       // COFF profile name section contains two null bytes indicating the
1070       // start/end of the section. If its size is 2 bytes, it's empty.
1071       if (IPSK == IPSK_name &&
1072           (Section.getSize() == 0 || (IsCOFF && Section.getSize() == 2)))
1073         continue;
1074       Sections.push_back(Section);
1075     }
1076   }
1077   if (Sections.empty())
1078     return make_error<CoverageMapError>(coveragemap_error::no_data_found);
1079   return Sections;
1080 }
1081 
1082 /// Find a section that matches \p Name and is allocatable at runtime.
1083 ///
1084 /// Returns the contents of the section and its start offset in the object file.
1085 static Expected<std::pair<StringRef, uint64_t>>
1086 lookupAllocatableSection(ObjectFile &OF, InstrProfSectKind IPSK) {
1087   // On Wasm, allocatable sections can live only in data segments.
1088   if (auto *WOF = dyn_cast<WasmObjectFile>(&OF)) {
1089     std::vector<const WasmSegment *> Segments;
1090     auto ObjFormat = OF.getTripleObjectFormat();
1091     auto Name =
1092         getInstrProfSectionName(IPSK, ObjFormat, /*AddSegmentInfo=*/false);
1093     for (const auto &DebugName : WOF->debugNames()) {
1094       if (DebugName.Type != wasm::NameType::DATA_SEGMENT ||
1095           DebugName.Name != Name)
1096         continue;
1097       if (DebugName.Index >= WOF->dataSegments().size())
1098         return make_error<CoverageMapError>(coveragemap_error::malformed);
1099       auto &Segment = WOF->dataSegments()[DebugName.Index];
1100       Segments.push_back(&Segment);
1101     }
1102     if (Segments.empty())
1103       return make_error<CoverageMapError>(coveragemap_error::no_data_found);
1104     if (Segments.size() != 1)
1105       return make_error<CoverageMapError>(coveragemap_error::malformed);
1106 
1107     const auto &Segment = *Segments.front();
1108     auto &Data = Segment.Data;
1109     StringRef Content(reinterpret_cast<const char *>(Data.Content.data()),
1110                       Data.Content.size());
1111     return std::make_pair(Content, Segment.SectionOffset);
1112   }
1113 
1114   // On other object file types, delegate to lookupSections to find the section.
1115   auto Sections = lookupSections(OF, IPSK);
1116   if (!Sections)
1117     return Sections.takeError();
1118   if (Sections->size() != 1)
1119     return make_error<CoverageMapError>(
1120         coveragemap_error::malformed,
1121         "the size of coverage mapping section is not one");
1122   auto &Section = Sections->front();
1123   auto ContentsOrErr = Section.getContents();
1124   if (!ContentsOrErr)
1125     return ContentsOrErr.takeError();
1126   return std::make_pair(*ContentsOrErr, Section.getAddress());
1127 }
1128 
1129 static Expected<std::unique_ptr<BinaryCoverageReader>>
1130 loadBinaryFormat(std::unique_ptr<Binary> Bin, StringRef Arch,
1131                  StringRef CompilationDir = "",
1132                  object::BuildIDRef *BinaryID = nullptr) {
1133   std::unique_ptr<ObjectFile> OF;
1134   if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin.get())) {
1135     // If we have a universal binary, try to look up the object for the
1136     // appropriate architecture.
1137     auto ObjectFileOrErr = Universal->getMachOObjectForArch(Arch);
1138     if (!ObjectFileOrErr)
1139       return ObjectFileOrErr.takeError();
1140     OF = std::move(ObjectFileOrErr.get());
1141   } else if (isa<ObjectFile>(Bin.get())) {
1142     // For any other object file, upcast and take ownership.
1143     OF.reset(cast<ObjectFile>(Bin.release()));
1144     // If we've asked for a particular arch, make sure they match.
1145     if (!Arch.empty() && OF->getArch() != Triple(Arch).getArch())
1146       return errorCodeToError(object_error::arch_not_found);
1147   } else
1148     // We can only handle object files.
1149     return make_error<CoverageMapError>(coveragemap_error::malformed,
1150                                         "binary is not an object file");
1151 
1152   // The coverage uses native pointer sizes for the object it's written in.
1153   uint8_t BytesInAddress = OF->getBytesInAddress();
1154   llvm::endianness Endian =
1155       OF->isLittleEndian() ? llvm::endianness::little : llvm::endianness::big;
1156 
1157   // Look for the sections that we are interested in.
1158   auto ProfileNames = std::make_unique<InstrProfSymtab>();
1159   // If IPSK_name is not found, fallback to search for IPK_covname, which is
1160   // used when binary correlation is enabled.
1161   auto NamesSection = lookupAllocatableSection(*OF, IPSK_name);
1162   if (auto E = NamesSection.takeError()) {
1163     consumeError(std::move(E));
1164     NamesSection = lookupAllocatableSection(*OF, IPSK_covname);
1165     if (auto E = NamesSection.takeError())
1166       return std::move(E);
1167   }
1168 
1169   uint64_t NamesAddress;
1170   StringRef NamesContent;
1171   std::tie(NamesContent, NamesAddress) = *NamesSection;
1172   if (Error E = ProfileNames->create(NamesContent, NamesAddress))
1173     return std::move(E);
1174 
1175   auto CoverageSection = lookupSections(*OF, IPSK_covmap);
1176   if (auto E = CoverageSection.takeError())
1177     return std::move(E);
1178   std::vector<SectionRef> CoverageSectionRefs = *CoverageSection;
1179   if (CoverageSectionRefs.size() != 1)
1180     return make_error<CoverageMapError>(coveragemap_error::malformed,
1181                                         "the size of name section is not one");
1182   auto CoverageMappingOrErr = CoverageSectionRefs.back().getContents();
1183   if (!CoverageMappingOrErr)
1184     return CoverageMappingOrErr.takeError();
1185   StringRef CoverageMapping = CoverageMappingOrErr.get();
1186 
1187   // If the coverage mapping section is not aligned to 8 bytes, copy it to a
1188   // new buffer that is. Wasm format typically has unaligned section contents
1189   // because it doesn't have a good way to insert padding bytes.
1190   std::unique_ptr<MemoryBuffer> CoverageMapCopy;
1191   if (!isAddrAligned(Align(8), CoverageMapping.data())) {
1192     CoverageMapCopy = MemoryBuffer::getMemBufferCopy(CoverageMapping);
1193     CoverageMapping = CoverageMapCopy->getBuffer();
1194   }
1195 
1196   // Look for the coverage records section (Version4 only).
1197   auto CoverageRecordsSections = lookupSections(*OF, IPSK_covfun);
1198 
1199   BinaryCoverageReader::FuncRecordsStorage FuncRecords;
1200   if (auto E = CoverageRecordsSections.takeError()) {
1201     consumeError(std::move(E));
1202     FuncRecords = MemoryBuffer::getMemBuffer("");
1203   } else {
1204     // Compute the FuncRecordsBuffer of the buffer, taking into account the
1205     // padding between each record, and making sure the first block is aligned
1206     // in memory to maintain consistency between buffer address and size
1207     // alignment.
1208     const Align RecordAlignment(8);
1209     uint64_t FuncRecordsSize = 0;
1210     for (SectionRef Section : *CoverageRecordsSections) {
1211       auto CoverageRecordsOrErr = Section.getContents();
1212       if (!CoverageRecordsOrErr)
1213         return CoverageRecordsOrErr.takeError();
1214       FuncRecordsSize += alignTo(CoverageRecordsOrErr->size(), RecordAlignment);
1215     }
1216     auto WritableBuffer =
1217         WritableMemoryBuffer::getNewUninitMemBuffer(FuncRecordsSize);
1218     char *FuncRecordsBuffer = WritableBuffer->getBufferStart();
1219     assert(isAddrAligned(RecordAlignment, FuncRecordsBuffer) &&
1220            "Allocated memory is correctly aligned");
1221 
1222     for (SectionRef Section : *CoverageRecordsSections) {
1223       auto CoverageRecordsOrErr = Section.getContents();
1224       if (!CoverageRecordsOrErr)
1225         return CoverageRecordsOrErr.takeError();
1226       const auto &CoverageRecords = CoverageRecordsOrErr.get();
1227       FuncRecordsBuffer = std::copy(CoverageRecords.begin(),
1228                                     CoverageRecords.end(), FuncRecordsBuffer);
1229       FuncRecordsBuffer =
1230           std::fill_n(FuncRecordsBuffer,
1231                       alignAddr(FuncRecordsBuffer, RecordAlignment) -
1232                           (uintptr_t)FuncRecordsBuffer,
1233                       '\0');
1234     }
1235     assert(FuncRecordsBuffer == WritableBuffer->getBufferEnd() &&
1236            "consistent init");
1237     FuncRecords = std::move(WritableBuffer);
1238   }
1239 
1240   if (BinaryID)
1241     *BinaryID = getBuildID(OF.get());
1242 
1243   return BinaryCoverageReader::createCoverageReaderFromBuffer(
1244       CoverageMapping, std::move(FuncRecords), std::move(CoverageMapCopy),
1245       std::move(ProfileNames), BytesInAddress, Endian, CompilationDir);
1246 }
1247 
1248 /// Determine whether \p Arch is invalid or empty, given \p Bin.
1249 static bool isArchSpecifierInvalidOrMissing(Binary *Bin, StringRef Arch) {
1250   // If we have a universal binary and Arch doesn't identify any of its slices,
1251   // it's user error.
1252   if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin)) {
1253     for (auto &ObjForArch : Universal->objects())
1254       if (Arch == ObjForArch.getArchFlagName())
1255         return false;
1256     return true;
1257   }
1258   return false;
1259 }
1260 
1261 Expected<std::vector<std::unique_ptr<BinaryCoverageReader>>>
1262 BinaryCoverageReader::create(
1263     MemoryBufferRef ObjectBuffer, StringRef Arch,
1264     SmallVectorImpl<std::unique_ptr<MemoryBuffer>> &ObjectFileBuffers,
1265     StringRef CompilationDir, SmallVectorImpl<object::BuildIDRef> *BinaryIDs) {
1266   std::vector<std::unique_ptr<BinaryCoverageReader>> Readers;
1267 
1268   if (ObjectBuffer.getBuffer().size() > sizeof(TestingFormatMagic)) {
1269     uint64_t Magic =
1270         support::endian::byte_swap<uint64_t, llvm::endianness::little>(
1271             *reinterpret_cast<const uint64_t *>(ObjectBuffer.getBufferStart()));
1272     if (Magic == TestingFormatMagic) {
1273       // This is a special format used for testing.
1274       auto ReaderOrErr =
1275           loadTestingFormat(ObjectBuffer.getBuffer(), CompilationDir);
1276       if (!ReaderOrErr)
1277         return ReaderOrErr.takeError();
1278       Readers.push_back(std::move(ReaderOrErr.get()));
1279       return std::move(Readers);
1280     }
1281   }
1282 
1283   auto BinOrErr = createBinary(ObjectBuffer);
1284   if (!BinOrErr)
1285     return BinOrErr.takeError();
1286   std::unique_ptr<Binary> Bin = std::move(BinOrErr.get());
1287 
1288   if (isArchSpecifierInvalidOrMissing(Bin.get(), Arch))
1289     return make_error<CoverageMapError>(
1290         coveragemap_error::invalid_or_missing_arch_specifier);
1291 
1292   // MachO universal binaries which contain archives need to be treated as
1293   // archives, not as regular binaries.
1294   if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin.get())) {
1295     for (auto &ObjForArch : Universal->objects()) {
1296       // Skip slices within the universal binary which target the wrong arch.
1297       std::string ObjArch = ObjForArch.getArchFlagName();
1298       if (Arch != ObjArch)
1299         continue;
1300 
1301       auto ArchiveOrErr = ObjForArch.getAsArchive();
1302       if (!ArchiveOrErr) {
1303         // If this is not an archive, try treating it as a regular object.
1304         consumeError(ArchiveOrErr.takeError());
1305         break;
1306       }
1307 
1308       return BinaryCoverageReader::create(
1309           ArchiveOrErr.get()->getMemoryBufferRef(), Arch, ObjectFileBuffers,
1310           CompilationDir, BinaryIDs);
1311     }
1312   }
1313 
1314   // Load coverage out of archive members.
1315   if (auto *Ar = dyn_cast<Archive>(Bin.get())) {
1316     Error Err = Error::success();
1317     for (auto &Child : Ar->children(Err)) {
1318       Expected<MemoryBufferRef> ChildBufOrErr = Child.getMemoryBufferRef();
1319       if (!ChildBufOrErr)
1320         return ChildBufOrErr.takeError();
1321 
1322       auto ChildReadersOrErr = BinaryCoverageReader::create(
1323           ChildBufOrErr.get(), Arch, ObjectFileBuffers, CompilationDir,
1324           BinaryIDs);
1325       if (!ChildReadersOrErr)
1326         return ChildReadersOrErr.takeError();
1327       for (auto &Reader : ChildReadersOrErr.get())
1328         Readers.push_back(std::move(Reader));
1329     }
1330     if (Err)
1331       return std::move(Err);
1332 
1333     // Thin archives reference object files outside of the archive file, i.e.
1334     // files which reside in memory not owned by the caller. Transfer ownership
1335     // to the caller.
1336     if (Ar->isThin())
1337       for (auto &Buffer : Ar->takeThinBuffers())
1338         ObjectFileBuffers.push_back(std::move(Buffer));
1339 
1340     return std::move(Readers);
1341   }
1342 
1343   object::BuildIDRef BinaryID;
1344   auto ReaderOrErr = loadBinaryFormat(std::move(Bin), Arch, CompilationDir,
1345                                       BinaryIDs ? &BinaryID : nullptr);
1346   if (!ReaderOrErr)
1347     return ReaderOrErr.takeError();
1348   Readers.push_back(std::move(ReaderOrErr.get()));
1349   if (!BinaryID.empty())
1350     BinaryIDs->push_back(BinaryID);
1351   return std::move(Readers);
1352 }
1353 
1354 Error BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) {
1355   if (CurrentRecord >= MappingRecords.size())
1356     return make_error<CoverageMapError>(coveragemap_error::eof);
1357 
1358   FunctionsFilenames.clear();
1359   Expressions.clear();
1360   MappingRegions.clear();
1361   auto &R = MappingRecords[CurrentRecord];
1362   auto F = ArrayRef(Filenames).slice(R.FilenamesBegin, R.FilenamesSize);
1363   RawCoverageMappingReader Reader(R.CoverageMapping, F, FunctionsFilenames,
1364                                   Expressions, MappingRegions);
1365   if (auto Err = Reader.read())
1366     return Err;
1367 
1368   Record.FunctionName = R.FunctionName;
1369   Record.FunctionHash = R.FunctionHash;
1370   Record.Filenames = FunctionsFilenames;
1371   Record.Expressions = Expressions;
1372   Record.MappingRegions = MappingRegions;
1373 
1374   ++CurrentRecord;
1375   return Error::success();
1376 }
1377