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