xref: /llvm-project/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp (revision bf8f5cc9b50b411ab0d8ee6532c6189992d790db)
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 /// Determine if we should skip the first byte of the section content
498 static bool shouldSkipSectionFirstByte(SectionRef &Section) {
499   const ObjectFile *Obj = Section.getObject();
500   // If this is a linked PE/COFF file, then we have to skip over the null byte
501   // that is allocated in the .lprfn$A section in the LLVM profiling runtime.
502   // If the name section is .lprfcovnames, it doesn't have the null byte at the
503   // beginning.
504   if (isa<COFFObjectFile>(Obj) && !Obj->isRelocatableObject())
505     if (Expected<StringRef> NameOrErr = Section.getName())
506       if (*NameOrErr != getInstrProfSectionName(IPSK_covname, Triple::COFF))
507         return true;
508   return false;
509 }
510 
511 Error InstrProfSymtab::create(SectionRef &Section) {
512   Expected<StringRef> DataOrErr = Section.getContents();
513   if (!DataOrErr)
514     return DataOrErr.takeError();
515   Data = *DataOrErr;
516   Address = Section.getAddress();
517 
518   if (shouldSkipSectionFirstByte(Section))
519     Data = Data.substr(1);
520 
521   return Error::success();
522 }
523 
524 StringRef InstrProfSymtab::getFuncName(uint64_t Pointer, size_t Size) {
525   if (Pointer < Address)
526     return StringRef();
527   auto Offset = Pointer - Address;
528   if (Offset + Size > Data.size())
529     return StringRef();
530   return Data.substr(Pointer - Address, Size);
531 }
532 
533 // Check if the mapping data is a dummy, i.e. is emitted for an unused function.
534 static Expected<bool> isCoverageMappingDummy(uint64_t Hash, StringRef Mapping) {
535   // The hash value of dummy mapping records is always zero.
536   if (Hash)
537     return false;
538   return RawCoverageMappingDummyChecker(Mapping).isDummy();
539 }
540 
541 /// A range of filename indices. Used to specify the location of a batch of
542 /// filenames in a vector-like container.
543 struct FilenameRange {
544   unsigned StartingIndex;
545   unsigned Length;
546 
547   FilenameRange(unsigned StartingIndex, unsigned Length)
548       : StartingIndex(StartingIndex), Length(Length) {}
549 
550   void markInvalid() { Length = 0; }
551   bool isInvalid() const { return Length == 0; }
552 };
553 
554 namespace {
555 
556 /// The interface to read coverage mapping function records for a module.
557 struct CovMapFuncRecordReader {
558   virtual ~CovMapFuncRecordReader() = default;
559 
560   // Read a coverage header.
561   //
562   // \p CovBuf points to the buffer containing the \c CovHeader of the coverage
563   // mapping data associated with the module.
564   //
565   // Returns a pointer to the next \c CovHeader if it exists, or to an address
566   // greater than \p CovEnd if not.
567   virtual Expected<const char *> readCoverageHeader(const char *CovBuf,
568                                                     const char *CovBufEnd) = 0;
569 
570   // Read function records.
571   //
572   // \p FuncRecBuf points to the buffer containing a batch of function records.
573   // \p FuncRecBufEnd points past the end of the batch of records.
574   //
575   // Prior to Version4, \p OutOfLineFileRange points to a sequence of filenames
576   // associated with the function records. It is unused in Version4.
577   //
578   // Prior to Version4, \p OutOfLineMappingBuf points to a sequence of coverage
579   // mappings associated with the function records. It is unused in Version4.
580   virtual Error
581   readFunctionRecords(const char *FuncRecBuf, const char *FuncRecBufEnd,
582                       std::optional<FilenameRange> OutOfLineFileRange,
583                       const char *OutOfLineMappingBuf,
584                       const char *OutOfLineMappingBufEnd) = 0;
585 
586   template <class IntPtrT, llvm::endianness Endian>
587   static Expected<std::unique_ptr<CovMapFuncRecordReader>>
588   get(CovMapVersion Version, InstrProfSymtab &P,
589       std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, StringRef D,
590       std::vector<std::string> &F);
591 };
592 
593 // A class for reading coverage mapping function records for a module.
594 template <CovMapVersion Version, class IntPtrT, llvm::endianness Endian>
595 class VersionedCovMapFuncRecordReader : public CovMapFuncRecordReader {
596   using FuncRecordType =
597       typename CovMapTraits<Version, IntPtrT>::CovMapFuncRecordType;
598   using NameRefType = typename CovMapTraits<Version, IntPtrT>::NameRefType;
599 
600   // Maps function's name references to the indexes of their records
601   // in \c Records.
602   DenseMap<NameRefType, size_t> FunctionRecords;
603   InstrProfSymtab &ProfileNames;
604   StringRef CompilationDir;
605   std::vector<std::string> &Filenames;
606   std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records;
607 
608   // Maps a hash of the filenames in a TU to a \c FileRange. The range
609   // specifies the location of the hashed filenames in \c Filenames.
610   DenseMap<uint64_t, FilenameRange> FileRangeMap;
611 
612   // Add the record to the collection if we don't already have a record that
613   // points to the same function name. This is useful to ignore the redundant
614   // records for the functions with ODR linkage.
615   // In addition, prefer records with real coverage mapping data to dummy
616   // records, which were emitted for inline functions which were seen but
617   // not used in the corresponding translation unit.
618   Error insertFunctionRecordIfNeeded(const FuncRecordType *CFR,
619                                      StringRef Mapping,
620                                      FilenameRange FileRange) {
621     ++CovMapNumRecords;
622     uint64_t FuncHash = CFR->template getFuncHash<Endian>();
623     NameRefType NameRef = CFR->template getFuncNameRef<Endian>();
624     auto InsertResult =
625         FunctionRecords.insert(std::make_pair(NameRef, Records.size()));
626     if (InsertResult.second) {
627       StringRef FuncName;
628       if (Error Err = CFR->template getFuncName<Endian>(ProfileNames, FuncName))
629         return Err;
630       if (FuncName.empty())
631         return make_error<InstrProfError>(instrprof_error::malformed,
632                                           "function name is empty");
633       ++CovMapNumUsedRecords;
634       Records.emplace_back(Version, FuncName, FuncHash, Mapping,
635                            FileRange.StartingIndex, FileRange.Length);
636       return Error::success();
637     }
638     // Update the existing record if it's a dummy and the new record is real.
639     size_t OldRecordIndex = InsertResult.first->second;
640     BinaryCoverageReader::ProfileMappingRecord &OldRecord =
641         Records[OldRecordIndex];
642     Expected<bool> OldIsDummyExpected = isCoverageMappingDummy(
643         OldRecord.FunctionHash, OldRecord.CoverageMapping);
644     if (Error Err = OldIsDummyExpected.takeError())
645       return Err;
646     if (!*OldIsDummyExpected)
647       return Error::success();
648     Expected<bool> NewIsDummyExpected =
649         isCoverageMappingDummy(FuncHash, Mapping);
650     if (Error Err = NewIsDummyExpected.takeError())
651       return Err;
652     if (*NewIsDummyExpected)
653       return Error::success();
654     ++CovMapNumUsedRecords;
655     OldRecord.FunctionHash = FuncHash;
656     OldRecord.CoverageMapping = Mapping;
657     OldRecord.FilenamesBegin = FileRange.StartingIndex;
658     OldRecord.FilenamesSize = FileRange.Length;
659     return Error::success();
660   }
661 
662 public:
663   VersionedCovMapFuncRecordReader(
664       InstrProfSymtab &P,
665       std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, StringRef D,
666       std::vector<std::string> &F)
667       : ProfileNames(P), CompilationDir(D), Filenames(F), Records(R) {}
668 
669   ~VersionedCovMapFuncRecordReader() override = default;
670 
671   Expected<const char *> readCoverageHeader(const char *CovBuf,
672                                             const char *CovBufEnd) override {
673     using namespace support;
674 
675     if (CovBuf + sizeof(CovMapHeader) > CovBufEnd)
676       return make_error<CoverageMapError>(
677           coveragemap_error::malformed,
678           "coverage mapping header section is larger than buffer size");
679     auto CovHeader = reinterpret_cast<const CovMapHeader *>(CovBuf);
680     uint32_t NRecords = CovHeader->getNRecords<Endian>();
681     uint32_t FilenamesSize = CovHeader->getFilenamesSize<Endian>();
682     uint32_t CoverageSize = CovHeader->getCoverageSize<Endian>();
683     assert((CovMapVersion)CovHeader->getVersion<Endian>() == Version);
684     CovBuf = reinterpret_cast<const char *>(CovHeader + 1);
685 
686     // Skip past the function records, saving the start and end for later.
687     // This is a no-op in Version4 (function records are read after all headers
688     // are read).
689     const char *FuncRecBuf = nullptr;
690     const char *FuncRecBufEnd = nullptr;
691     if (Version < CovMapVersion::Version4)
692       FuncRecBuf = CovBuf;
693     CovBuf += NRecords * sizeof(FuncRecordType);
694     if (Version < CovMapVersion::Version4)
695       FuncRecBufEnd = CovBuf;
696 
697     // Get the filenames.
698     if (CovBuf + FilenamesSize > CovBufEnd)
699       return make_error<CoverageMapError>(
700           coveragemap_error::malformed,
701           "filenames section is larger than buffer size");
702     size_t FilenamesBegin = Filenames.size();
703     StringRef FilenameRegion(CovBuf, FilenamesSize);
704     RawCoverageFilenamesReader Reader(FilenameRegion, Filenames,
705                                       CompilationDir);
706     if (auto Err = Reader.read(Version))
707       return std::move(Err);
708     CovBuf += FilenamesSize;
709     FilenameRange FileRange(FilenamesBegin, Filenames.size() - FilenamesBegin);
710 
711     if (Version >= CovMapVersion::Version4) {
712       // Map a hash of the filenames region to the filename range associated
713       // with this coverage header.
714       int64_t FilenamesRef =
715           llvm::IndexedInstrProf::ComputeHash(FilenameRegion);
716       auto Insert =
717           FileRangeMap.insert(std::make_pair(FilenamesRef, FileRange));
718       if (!Insert.second) {
719         // The same filenames ref was encountered twice. It's possible that
720         // the associated filenames are the same.
721         auto It = Filenames.begin();
722         FilenameRange &OrigRange = Insert.first->getSecond();
723         if (std::equal(It + OrigRange.StartingIndex,
724                        It + OrigRange.StartingIndex + OrigRange.Length,
725                        It + FileRange.StartingIndex,
726                        It + FileRange.StartingIndex + FileRange.Length))
727           // Map the new range to the original one.
728           FileRange = OrigRange;
729         else
730           // This is a hash collision. Mark the filenames ref invalid.
731           OrigRange.markInvalid();
732       }
733     }
734 
735     // We'll read the coverage mapping records in the loop below.
736     // This is a no-op in Version4 (coverage mappings are not affixed to the
737     // coverage header).
738     const char *MappingBuf = CovBuf;
739     if (Version >= CovMapVersion::Version4 && CoverageSize != 0)
740       return make_error<CoverageMapError>(coveragemap_error::malformed,
741                                           "coverage mapping size is not zero");
742     CovBuf += CoverageSize;
743     const char *MappingEnd = CovBuf;
744 
745     if (CovBuf > CovBufEnd)
746       return make_error<CoverageMapError>(
747           coveragemap_error::malformed,
748           "function records section is larger than buffer size");
749 
750     if (Version < CovMapVersion::Version4) {
751       // Read each function record.
752       if (Error E = readFunctionRecords(FuncRecBuf, FuncRecBufEnd, FileRange,
753                                         MappingBuf, MappingEnd))
754         return std::move(E);
755     }
756 
757     // Each coverage map has an alignment of 8, so we need to adjust alignment
758     // before reading the next map.
759     CovBuf += offsetToAlignedAddr(CovBuf, Align(8));
760 
761     return CovBuf;
762   }
763 
764   Error readFunctionRecords(const char *FuncRecBuf, const char *FuncRecBufEnd,
765                             std::optional<FilenameRange> OutOfLineFileRange,
766                             const char *OutOfLineMappingBuf,
767                             const char *OutOfLineMappingBufEnd) override {
768     auto CFR = reinterpret_cast<const FuncRecordType *>(FuncRecBuf);
769     while ((const char *)CFR < FuncRecBufEnd) {
770       // Validate the length of the coverage mapping for this function.
771       const char *NextMappingBuf;
772       const FuncRecordType *NextCFR;
773       std::tie(NextMappingBuf, NextCFR) =
774           CFR->template advanceByOne<Endian>(OutOfLineMappingBuf);
775       if (Version < CovMapVersion::Version4)
776         if (NextMappingBuf > OutOfLineMappingBufEnd)
777           return make_error<CoverageMapError>(
778               coveragemap_error::malformed,
779               "next mapping buffer is larger than buffer size");
780 
781       // Look up the set of filenames associated with this function record.
782       std::optional<FilenameRange> FileRange;
783       if (Version < CovMapVersion::Version4) {
784         FileRange = OutOfLineFileRange;
785       } else {
786         uint64_t FilenamesRef = CFR->template getFilenamesRef<Endian>();
787         auto It = FileRangeMap.find(FilenamesRef);
788         if (It == FileRangeMap.end())
789           return make_error<CoverageMapError>(
790               coveragemap_error::malformed,
791               "no filename found for function with hash=0x" +
792                   Twine::utohexstr(FilenamesRef));
793         else
794           FileRange = It->getSecond();
795       }
796 
797       // Now, read the coverage data.
798       if (FileRange && !FileRange->isInvalid()) {
799         StringRef Mapping =
800             CFR->template getCoverageMapping<Endian>(OutOfLineMappingBuf);
801         if (Version >= CovMapVersion::Version4 &&
802             Mapping.data() + Mapping.size() > FuncRecBufEnd)
803           return make_error<CoverageMapError>(
804               coveragemap_error::malformed,
805               "coverage mapping data is larger than buffer size");
806         if (Error Err = insertFunctionRecordIfNeeded(CFR, Mapping, *FileRange))
807           return Err;
808       }
809 
810       std::tie(OutOfLineMappingBuf, CFR) = std::tie(NextMappingBuf, NextCFR);
811     }
812     return Error::success();
813   }
814 };
815 
816 } // end anonymous namespace
817 
818 template <class IntPtrT, llvm::endianness Endian>
819 Expected<std::unique_ptr<CovMapFuncRecordReader>> CovMapFuncRecordReader::get(
820     CovMapVersion Version, InstrProfSymtab &P,
821     std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, StringRef D,
822     std::vector<std::string> &F) {
823   using namespace coverage;
824 
825   switch (Version) {
826   case CovMapVersion::Version1:
827     return std::make_unique<VersionedCovMapFuncRecordReader<
828         CovMapVersion::Version1, IntPtrT, Endian>>(P, R, D, F);
829   case CovMapVersion::Version2:
830   case CovMapVersion::Version3:
831   case CovMapVersion::Version4:
832   case CovMapVersion::Version5:
833   case CovMapVersion::Version6:
834   case CovMapVersion::Version7:
835     // Decompress the name data.
836     if (Error E = P.create(P.getNameData()))
837       return std::move(E);
838     if (Version == CovMapVersion::Version2)
839       return std::make_unique<VersionedCovMapFuncRecordReader<
840           CovMapVersion::Version2, IntPtrT, Endian>>(P, R, D, F);
841     else if (Version == CovMapVersion::Version3)
842       return std::make_unique<VersionedCovMapFuncRecordReader<
843           CovMapVersion::Version3, IntPtrT, Endian>>(P, R, D, F);
844     else if (Version == CovMapVersion::Version4)
845       return std::make_unique<VersionedCovMapFuncRecordReader<
846           CovMapVersion::Version4, IntPtrT, Endian>>(P, R, D, F);
847     else if (Version == CovMapVersion::Version5)
848       return std::make_unique<VersionedCovMapFuncRecordReader<
849           CovMapVersion::Version5, IntPtrT, Endian>>(P, R, D, F);
850     else if (Version == CovMapVersion::Version6)
851       return std::make_unique<VersionedCovMapFuncRecordReader<
852           CovMapVersion::Version6, IntPtrT, Endian>>(P, R, D, F);
853     else if (Version == CovMapVersion::Version7)
854       return std::make_unique<VersionedCovMapFuncRecordReader<
855           CovMapVersion::Version7, IntPtrT, Endian>>(P, R, D, F);
856   }
857   llvm_unreachable("Unsupported version");
858 }
859 
860 template <typename T, llvm::endianness Endian>
861 static Error readCoverageMappingData(
862     InstrProfSymtab &ProfileNames, StringRef CovMap, StringRef FuncRecords,
863     std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records,
864     StringRef CompilationDir, std::vector<std::string> &Filenames) {
865   using namespace coverage;
866 
867   // Read the records in the coverage data section.
868   auto CovHeader =
869       reinterpret_cast<const CovMapHeader *>(CovMap.data());
870   CovMapVersion Version = (CovMapVersion)CovHeader->getVersion<Endian>();
871   if (Version > CovMapVersion::CurrentVersion)
872     return make_error<CoverageMapError>(coveragemap_error::unsupported_version);
873   Expected<std::unique_ptr<CovMapFuncRecordReader>> ReaderExpected =
874       CovMapFuncRecordReader::get<T, Endian>(Version, ProfileNames, Records,
875                                              CompilationDir, Filenames);
876   if (Error E = ReaderExpected.takeError())
877     return E;
878   auto Reader = std::move(ReaderExpected.get());
879   const char *CovBuf = CovMap.data();
880   const char *CovBufEnd = CovBuf + CovMap.size();
881   const char *FuncRecBuf = FuncRecords.data();
882   const char *FuncRecBufEnd = FuncRecords.data() + FuncRecords.size();
883   while (CovBuf < CovBufEnd) {
884     // Read the current coverage header & filename data.
885     //
886     // Prior to Version4, this also reads all function records affixed to the
887     // header.
888     //
889     // Return a pointer to the next coverage header.
890     auto NextOrErr = Reader->readCoverageHeader(CovBuf, CovBufEnd);
891     if (auto E = NextOrErr.takeError())
892       return E;
893     CovBuf = NextOrErr.get();
894   }
895   // In Version4, function records are not affixed to coverage headers. Read
896   // the records from their dedicated section.
897   if (Version >= CovMapVersion::Version4)
898     return Reader->readFunctionRecords(FuncRecBuf, FuncRecBufEnd, std::nullopt,
899                                        nullptr, nullptr);
900   return Error::success();
901 }
902 
903 Expected<std::unique_ptr<BinaryCoverageReader>>
904 BinaryCoverageReader::createCoverageReaderFromBuffer(
905     StringRef Coverage, FuncRecordsStorage &&FuncRecords,
906     CoverageMapCopyStorage &&CoverageMap,
907     std::unique_ptr<InstrProfSymtab> ProfileNamesPtr, uint8_t BytesInAddress,
908     llvm::endianness Endian, StringRef CompilationDir) {
909   if (ProfileNamesPtr == nullptr)
910     return make_error<CoverageMapError>(coveragemap_error::malformed,
911                                         "Caller must provide ProfileNames");
912   std::unique_ptr<BinaryCoverageReader> Reader(
913       new BinaryCoverageReader(std::move(ProfileNamesPtr),
914                                std::move(FuncRecords), std::move(CoverageMap)));
915   InstrProfSymtab &ProfileNames = *Reader->ProfileNames;
916   StringRef FuncRecordsRef = Reader->FuncRecords->getBuffer();
917   if (BytesInAddress == 4 && Endian == llvm::endianness::little) {
918     if (Error E = readCoverageMappingData<uint32_t, llvm::endianness::little>(
919             ProfileNames, Coverage, FuncRecordsRef, Reader->MappingRecords,
920             CompilationDir, Reader->Filenames))
921       return std::move(E);
922   } else if (BytesInAddress == 4 && Endian == llvm::endianness::big) {
923     if (Error E = readCoverageMappingData<uint32_t, llvm::endianness::big>(
924             ProfileNames, Coverage, FuncRecordsRef, Reader->MappingRecords,
925             CompilationDir, Reader->Filenames))
926       return std::move(E);
927   } else if (BytesInAddress == 8 && Endian == llvm::endianness::little) {
928     if (Error E = readCoverageMappingData<uint64_t, llvm::endianness::little>(
929             ProfileNames, Coverage, FuncRecordsRef, Reader->MappingRecords,
930             CompilationDir, Reader->Filenames))
931       return std::move(E);
932   } else if (BytesInAddress == 8 && Endian == llvm::endianness::big) {
933     if (Error E = readCoverageMappingData<uint64_t, llvm::endianness::big>(
934             ProfileNames, Coverage, FuncRecordsRef, Reader->MappingRecords,
935             CompilationDir, Reader->Filenames))
936       return std::move(E);
937   } else
938     return make_error<CoverageMapError>(
939         coveragemap_error::malformed,
940         "not supported endianness or bytes in address");
941   return std::move(Reader);
942 }
943 
944 static Expected<std::unique_ptr<BinaryCoverageReader>>
945 loadTestingFormat(StringRef Data, StringRef CompilationDir) {
946   uint8_t BytesInAddress = 8;
947   llvm::endianness Endian = llvm::endianness::little;
948 
949   // Read the magic and version.
950   Data = Data.substr(sizeof(TestingFormatMagic));
951   if (Data.size() < sizeof(uint64_t))
952     return make_error<CoverageMapError>(coveragemap_error::malformed,
953                                         "the size of data is too small");
954   auto TestingVersion =
955       support::endian::byte_swap<uint64_t, llvm::endianness::little>(
956           *reinterpret_cast<const uint64_t *>(Data.data()));
957   Data = Data.substr(sizeof(uint64_t));
958 
959   // Read the ProfileNames data.
960   if (Data.empty())
961     return make_error<CoverageMapError>(coveragemap_error::truncated);
962   unsigned N = 0;
963   uint64_t ProfileNamesSize = decodeULEB128(Data.bytes_begin(), &N);
964   if (N > Data.size())
965     return make_error<CoverageMapError>(
966         coveragemap_error::malformed,
967         "the size of TestingFormatMagic is too big");
968   Data = Data.substr(N);
969   if (Data.empty())
970     return make_error<CoverageMapError>(coveragemap_error::truncated);
971   N = 0;
972   uint64_t Address = decodeULEB128(Data.bytes_begin(), &N);
973   if (N > Data.size())
974     return make_error<CoverageMapError>(coveragemap_error::malformed,
975                                         "the size of ULEB128 is too big");
976   Data = Data.substr(N);
977   if (Data.size() < ProfileNamesSize)
978     return make_error<CoverageMapError>(coveragemap_error::malformed,
979                                         "the size of ProfileNames is too big");
980   auto ProfileNames = std::make_unique<InstrProfSymtab>();
981   if (Error E = ProfileNames->create(Data.substr(0, ProfileNamesSize), Address))
982     return std::move(E);
983   Data = Data.substr(ProfileNamesSize);
984 
985   // In Version2, the size of CoverageMapping is stored directly.
986   uint64_t CoverageMappingSize;
987   if (TestingVersion == uint64_t(TestingFormatVersion::Version2)) {
988     N = 0;
989     CoverageMappingSize = decodeULEB128(Data.bytes_begin(), &N);
990     if (N > Data.size())
991       return make_error<CoverageMapError>(coveragemap_error::malformed,
992                                           "the size of ULEB128 is too big");
993     Data = Data.substr(N);
994     if (CoverageMappingSize < sizeof(CovMapHeader))
995       return make_error<CoverageMapError>(
996           coveragemap_error::malformed,
997           "the size of CoverageMapping is teoo small");
998   } else if (TestingVersion != uint64_t(TestingFormatVersion::Version1)) {
999     return make_error<CoverageMapError>(coveragemap_error::unsupported_version);
1000   }
1001 
1002   // Skip the padding bytes because coverage map data has an alignment of 8.
1003   auto Pad = offsetToAlignedAddr(Data.data(), Align(8));
1004   if (Data.size() < Pad)
1005     return make_error<CoverageMapError>(coveragemap_error::malformed,
1006                                         "insufficient padding");
1007   Data = Data.substr(Pad);
1008   if (Data.size() < sizeof(CovMapHeader))
1009     return make_error<CoverageMapError>(
1010         coveragemap_error::malformed,
1011         "coverage mapping header section is larger than data size");
1012   auto const *CovHeader = reinterpret_cast<const CovMapHeader *>(
1013       Data.substr(0, sizeof(CovMapHeader)).data());
1014   auto Version =
1015       CovMapVersion(CovHeader->getVersion<llvm::endianness::little>());
1016 
1017   // In Version1, the size of CoverageMapping is calculated.
1018   if (TestingVersion == uint64_t(TestingFormatVersion::Version1)) {
1019     if (Version < CovMapVersion::Version4) {
1020       CoverageMappingSize = Data.size();
1021     } else {
1022       auto FilenamesSize =
1023           CovHeader->getFilenamesSize<llvm::endianness::little>();
1024       CoverageMappingSize = sizeof(CovMapHeader) + FilenamesSize;
1025     }
1026   }
1027 
1028   auto CoverageMapping = Data.substr(0, CoverageMappingSize);
1029   Data = Data.substr(CoverageMappingSize);
1030 
1031   // Read the CoverageRecords data.
1032   if (Version < CovMapVersion::Version4) {
1033     if (!Data.empty())
1034       return make_error<CoverageMapError>(coveragemap_error::malformed,
1035                                           "data is not empty");
1036   } else {
1037     // Skip the padding bytes because coverage records data has an alignment
1038     // of 8.
1039     Pad = offsetToAlignedAddr(Data.data(), Align(8));
1040     if (Data.size() < Pad)
1041       return make_error<CoverageMapError>(coveragemap_error::malformed,
1042                                           "insufficient padding");
1043     Data = Data.substr(Pad);
1044   }
1045   BinaryCoverageReader::FuncRecordsStorage CoverageRecords =
1046       MemoryBuffer::getMemBuffer(Data);
1047 
1048   return BinaryCoverageReader::createCoverageReaderFromBuffer(
1049       CoverageMapping, std::move(CoverageRecords), nullptr,
1050       std::move(ProfileNames), BytesInAddress, Endian, CompilationDir);
1051 }
1052 
1053 /// Find all sections that match \p IPSK name. There may be more than one if
1054 /// comdats are in use, e.g. for the __llvm_covfun section on ELF.
1055 static Expected<std::vector<SectionRef>>
1056 lookupSections(ObjectFile &OF, InstrProfSectKind IPSK) {
1057   auto ObjFormat = OF.getTripleObjectFormat();
1058   auto Name =
1059       getInstrProfSectionName(IPSK, ObjFormat, /*AddSegmentInfo=*/false);
1060   // On COFF, the object file section name may end in "$M". This tells the
1061   // linker to sort these sections between "$A" and "$Z". The linker removes the
1062   // dollar and everything after it in the final binary. Do the same to match.
1063   bool IsCOFF = isa<COFFObjectFile>(OF);
1064   auto stripSuffix = [IsCOFF](StringRef N) {
1065     return IsCOFF ? N.split('$').first : N;
1066   };
1067   Name = stripSuffix(Name);
1068 
1069   std::vector<SectionRef> Sections;
1070   for (const auto &Section : OF.sections()) {
1071     Expected<StringRef> NameOrErr = Section.getName();
1072     if (!NameOrErr)
1073       return NameOrErr.takeError();
1074     if (stripSuffix(*NameOrErr) == Name) {
1075       // Skip empty profile name section.
1076       // COFF profile name section contains two null bytes indicating the
1077       // start/end of the section. If its size is 2 bytes, it's empty.
1078       if (IPSK == IPSK_name &&
1079           (Section.getSize() == 0 || (IsCOFF && Section.getSize() == 2)))
1080         continue;
1081       Sections.push_back(Section);
1082     }
1083   }
1084   if (Sections.empty())
1085     return make_error<CoverageMapError>(coveragemap_error::no_data_found);
1086   return Sections;
1087 }
1088 
1089 /// Find a section that matches \p Name and is allocatable at runtime.
1090 ///
1091 /// Returns the contents of the section and its start offset in the object file.
1092 static Expected<std::pair<StringRef, uint64_t>>
1093 lookupAllocatableSection(ObjectFile &OF, InstrProfSectKind IPSK) {
1094   // On Wasm, allocatable sections can live only in data segments.
1095   if (auto *WOF = dyn_cast<WasmObjectFile>(&OF)) {
1096     std::vector<const WasmSegment *> Segments;
1097     auto ObjFormat = OF.getTripleObjectFormat();
1098     auto Name =
1099         getInstrProfSectionName(IPSK, ObjFormat, /*AddSegmentInfo=*/false);
1100     for (const auto &DebugName : WOF->debugNames()) {
1101       if (DebugName.Type != wasm::NameType::DATA_SEGMENT ||
1102           DebugName.Name != Name)
1103         continue;
1104       if (DebugName.Index >= WOF->dataSegments().size())
1105         return make_error<CoverageMapError>(coveragemap_error::malformed);
1106       auto &Segment = WOF->dataSegments()[DebugName.Index];
1107       Segments.push_back(&Segment);
1108     }
1109     if (Segments.empty())
1110       return make_error<CoverageMapError>(coveragemap_error::no_data_found);
1111     if (Segments.size() != 1)
1112       return make_error<CoverageMapError>(coveragemap_error::malformed);
1113 
1114     const auto &Segment = *Segments.front();
1115     auto &Data = Segment.Data;
1116     StringRef Content(reinterpret_cast<const char *>(Data.Content.data()),
1117                       Data.Content.size());
1118     return std::make_pair(Content, Segment.SectionOffset);
1119   }
1120 
1121   // On other object file types, delegate to lookupSections to find the section.
1122   auto Sections = lookupSections(OF, IPSK);
1123   if (!Sections)
1124     return Sections.takeError();
1125   if (Sections->size() != 1)
1126     return make_error<CoverageMapError>(
1127         coveragemap_error::malformed,
1128         "the size of coverage mapping section is not one");
1129   auto &Section = Sections->front();
1130   auto ContentsOrErr = Section.getContents();
1131   if (!ContentsOrErr)
1132     return ContentsOrErr.takeError();
1133   auto Content = *ContentsOrErr;
1134   if (shouldSkipSectionFirstByte(Section))
1135     Content = Content.drop_front(1);
1136   return std::make_pair(Content, Section.getAddress());
1137 }
1138 
1139 static Expected<std::unique_ptr<BinaryCoverageReader>>
1140 loadBinaryFormat(std::unique_ptr<Binary> Bin, StringRef Arch,
1141                  StringRef CompilationDir = "",
1142                  object::BuildIDRef *BinaryID = nullptr) {
1143   std::unique_ptr<ObjectFile> OF;
1144   if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin.get())) {
1145     // If we have a universal binary, try to look up the object for the
1146     // appropriate architecture.
1147     auto ObjectFileOrErr = Universal->getMachOObjectForArch(Arch);
1148     if (!ObjectFileOrErr)
1149       return ObjectFileOrErr.takeError();
1150     OF = std::move(ObjectFileOrErr.get());
1151   } else if (isa<ObjectFile>(Bin.get())) {
1152     // For any other object file, upcast and take ownership.
1153     OF.reset(cast<ObjectFile>(Bin.release()));
1154     // If we've asked for a particular arch, make sure they match.
1155     if (!Arch.empty() && OF->getArch() != Triple(Arch).getArch())
1156       return errorCodeToError(object_error::arch_not_found);
1157   } else
1158     // We can only handle object files.
1159     return make_error<CoverageMapError>(coveragemap_error::malformed,
1160                                         "binary is not an object file");
1161 
1162   // The coverage uses native pointer sizes for the object it's written in.
1163   uint8_t BytesInAddress = OF->getBytesInAddress();
1164   llvm::endianness Endian =
1165       OF->isLittleEndian() ? llvm::endianness::little : llvm::endianness::big;
1166 
1167   // Look for the sections that we are interested in.
1168   auto ProfileNames = std::make_unique<InstrProfSymtab>();
1169   // If IPSK_name is not found, fallback to search for IPK_covname, which is
1170   // used when binary correlation is enabled.
1171   auto NamesSection = lookupAllocatableSection(*OF, IPSK_name);
1172   if (auto E = NamesSection.takeError()) {
1173     consumeError(std::move(E));
1174     NamesSection = lookupAllocatableSection(*OF, IPSK_covname);
1175     if (auto E = NamesSection.takeError())
1176       return std::move(E);
1177   }
1178 
1179   uint64_t NamesAddress;
1180   StringRef NamesContent;
1181   std::tie(NamesContent, NamesAddress) = *NamesSection;
1182   if (Error E = ProfileNames->create(NamesContent, NamesAddress))
1183     return std::move(E);
1184 
1185   auto CoverageSection = lookupSections(*OF, IPSK_covmap);
1186   if (auto E = CoverageSection.takeError())
1187     return std::move(E);
1188   std::vector<SectionRef> CoverageSectionRefs = *CoverageSection;
1189   if (CoverageSectionRefs.size() != 1)
1190     return make_error<CoverageMapError>(coveragemap_error::malformed,
1191                                         "the size of name section is not one");
1192   auto CoverageMappingOrErr = CoverageSectionRefs.back().getContents();
1193   if (!CoverageMappingOrErr)
1194     return CoverageMappingOrErr.takeError();
1195   StringRef CoverageMapping = CoverageMappingOrErr.get();
1196 
1197   // If the coverage mapping section is not aligned to 8 bytes, copy it to a
1198   // new buffer that is. Wasm format typically has unaligned section contents
1199   // because it doesn't have a good way to insert padding bytes.
1200   std::unique_ptr<MemoryBuffer> CoverageMapCopy;
1201   if (!isAddrAligned(Align(8), CoverageMapping.data())) {
1202     CoverageMapCopy = MemoryBuffer::getMemBufferCopy(CoverageMapping);
1203     CoverageMapping = CoverageMapCopy->getBuffer();
1204   }
1205 
1206   // Look for the coverage records section (Version4 only).
1207   auto CoverageRecordsSections = lookupSections(*OF, IPSK_covfun);
1208 
1209   BinaryCoverageReader::FuncRecordsStorage FuncRecords;
1210   if (auto E = CoverageRecordsSections.takeError()) {
1211     consumeError(std::move(E));
1212     FuncRecords = MemoryBuffer::getMemBuffer("");
1213   } else {
1214     // Compute the FuncRecordsBuffer of the buffer, taking into account the
1215     // padding between each record, and making sure the first block is aligned
1216     // in memory to maintain consistency between buffer address and size
1217     // alignment.
1218     const Align RecordAlignment(8);
1219     uint64_t FuncRecordsSize = 0;
1220     for (SectionRef Section : *CoverageRecordsSections) {
1221       auto CoverageRecordsOrErr = Section.getContents();
1222       if (!CoverageRecordsOrErr)
1223         return CoverageRecordsOrErr.takeError();
1224       FuncRecordsSize += alignTo(CoverageRecordsOrErr->size(), RecordAlignment);
1225     }
1226     auto WritableBuffer =
1227         WritableMemoryBuffer::getNewUninitMemBuffer(FuncRecordsSize);
1228     char *FuncRecordsBuffer = WritableBuffer->getBufferStart();
1229     assert(isAddrAligned(RecordAlignment, FuncRecordsBuffer) &&
1230            "Allocated memory is correctly aligned");
1231 
1232     for (SectionRef Section : *CoverageRecordsSections) {
1233       auto CoverageRecordsOrErr = Section.getContents();
1234       if (!CoverageRecordsOrErr)
1235         return CoverageRecordsOrErr.takeError();
1236       const auto &CoverageRecords = CoverageRecordsOrErr.get();
1237       FuncRecordsBuffer = std::copy(CoverageRecords.begin(),
1238                                     CoverageRecords.end(), FuncRecordsBuffer);
1239       FuncRecordsBuffer =
1240           std::fill_n(FuncRecordsBuffer,
1241                       alignAddr(FuncRecordsBuffer, RecordAlignment) -
1242                           (uintptr_t)FuncRecordsBuffer,
1243                       '\0');
1244     }
1245     assert(FuncRecordsBuffer == WritableBuffer->getBufferEnd() &&
1246            "consistent init");
1247     FuncRecords = std::move(WritableBuffer);
1248   }
1249 
1250   if (BinaryID)
1251     *BinaryID = getBuildID(OF.get());
1252 
1253   return BinaryCoverageReader::createCoverageReaderFromBuffer(
1254       CoverageMapping, std::move(FuncRecords), std::move(CoverageMapCopy),
1255       std::move(ProfileNames), BytesInAddress, Endian, CompilationDir);
1256 }
1257 
1258 /// Determine whether \p Arch is invalid or empty, given \p Bin.
1259 static bool isArchSpecifierInvalidOrMissing(Binary *Bin, StringRef Arch) {
1260   // If we have a universal binary and Arch doesn't identify any of its slices,
1261   // it's user error.
1262   if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin)) {
1263     for (auto &ObjForArch : Universal->objects())
1264       if (Arch == ObjForArch.getArchFlagName())
1265         return false;
1266     return true;
1267   }
1268   return false;
1269 }
1270 
1271 Expected<std::vector<std::unique_ptr<BinaryCoverageReader>>>
1272 BinaryCoverageReader::create(
1273     MemoryBufferRef ObjectBuffer, StringRef Arch,
1274     SmallVectorImpl<std::unique_ptr<MemoryBuffer>> &ObjectFileBuffers,
1275     StringRef CompilationDir, SmallVectorImpl<object::BuildIDRef> *BinaryIDs) {
1276   std::vector<std::unique_ptr<BinaryCoverageReader>> Readers;
1277 
1278   if (ObjectBuffer.getBuffer().size() > sizeof(TestingFormatMagic)) {
1279     uint64_t Magic =
1280         support::endian::byte_swap<uint64_t, llvm::endianness::little>(
1281             *reinterpret_cast<const uint64_t *>(ObjectBuffer.getBufferStart()));
1282     if (Magic == TestingFormatMagic) {
1283       // This is a special format used for testing.
1284       auto ReaderOrErr =
1285           loadTestingFormat(ObjectBuffer.getBuffer(), CompilationDir);
1286       if (!ReaderOrErr)
1287         return ReaderOrErr.takeError();
1288       Readers.push_back(std::move(ReaderOrErr.get()));
1289       return std::move(Readers);
1290     }
1291   }
1292 
1293   auto BinOrErr = createBinary(ObjectBuffer);
1294   if (!BinOrErr)
1295     return BinOrErr.takeError();
1296   std::unique_ptr<Binary> Bin = std::move(BinOrErr.get());
1297 
1298   if (isArchSpecifierInvalidOrMissing(Bin.get(), Arch))
1299     return make_error<CoverageMapError>(
1300         coveragemap_error::invalid_or_missing_arch_specifier);
1301 
1302   // MachO universal binaries which contain archives need to be treated as
1303   // archives, not as regular binaries.
1304   if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin.get())) {
1305     for (auto &ObjForArch : Universal->objects()) {
1306       // Skip slices within the universal binary which target the wrong arch.
1307       std::string ObjArch = ObjForArch.getArchFlagName();
1308       if (Arch != ObjArch)
1309         continue;
1310 
1311       auto ArchiveOrErr = ObjForArch.getAsArchive();
1312       if (!ArchiveOrErr) {
1313         // If this is not an archive, try treating it as a regular object.
1314         consumeError(ArchiveOrErr.takeError());
1315         break;
1316       }
1317 
1318       return BinaryCoverageReader::create(
1319           ArchiveOrErr.get()->getMemoryBufferRef(), Arch, ObjectFileBuffers,
1320           CompilationDir, BinaryIDs);
1321     }
1322   }
1323 
1324   // Load coverage out of archive members.
1325   if (auto *Ar = dyn_cast<Archive>(Bin.get())) {
1326     Error Err = Error::success();
1327     for (auto &Child : Ar->children(Err)) {
1328       Expected<MemoryBufferRef> ChildBufOrErr = Child.getMemoryBufferRef();
1329       if (!ChildBufOrErr)
1330         return ChildBufOrErr.takeError();
1331 
1332       auto ChildReadersOrErr = BinaryCoverageReader::create(
1333           ChildBufOrErr.get(), Arch, ObjectFileBuffers, CompilationDir,
1334           BinaryIDs);
1335       if (!ChildReadersOrErr)
1336         return ChildReadersOrErr.takeError();
1337       for (auto &Reader : ChildReadersOrErr.get())
1338         Readers.push_back(std::move(Reader));
1339     }
1340     if (Err)
1341       return std::move(Err);
1342 
1343     // Thin archives reference object files outside of the archive file, i.e.
1344     // files which reside in memory not owned by the caller. Transfer ownership
1345     // to the caller.
1346     if (Ar->isThin())
1347       for (auto &Buffer : Ar->takeThinBuffers())
1348         ObjectFileBuffers.push_back(std::move(Buffer));
1349 
1350     return std::move(Readers);
1351   }
1352 
1353   object::BuildIDRef BinaryID;
1354   auto ReaderOrErr = loadBinaryFormat(std::move(Bin), Arch, CompilationDir,
1355                                       BinaryIDs ? &BinaryID : nullptr);
1356   if (!ReaderOrErr)
1357     return ReaderOrErr.takeError();
1358   Readers.push_back(std::move(ReaderOrErr.get()));
1359   if (!BinaryID.empty())
1360     BinaryIDs->push_back(BinaryID);
1361   return std::move(Readers);
1362 }
1363 
1364 Error BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) {
1365   if (CurrentRecord >= MappingRecords.size())
1366     return make_error<CoverageMapError>(coveragemap_error::eof);
1367 
1368   FunctionsFilenames.clear();
1369   Expressions.clear();
1370   MappingRegions.clear();
1371   auto &R = MappingRecords[CurrentRecord];
1372   auto F = ArrayRef(Filenames).slice(R.FilenamesBegin, R.FilenamesSize);
1373   RawCoverageMappingReader Reader(R.CoverageMapping, F, FunctionsFilenames,
1374                                   Expressions, MappingRegions);
1375   if (auto Err = Reader.read())
1376     return Err;
1377 
1378   Record.FunctionName = R.FunctionName;
1379   Record.FunctionHash = R.FunctionHash;
1380   Record.Filenames = FunctionsFilenames;
1381   Record.Expressions = Expressions;
1382   Record.MappingRegions = MappingRegions;
1383 
1384   ++CurrentRecord;
1385   return Error::success();
1386 }
1387