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