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