xref: /llvm-project/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp (revision cd8fe1dbcb7dd36f700681ced34b41993e844239)
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 == support::endianness::little) {
861     if (Error E =
862             readCoverageMappingData<uint32_t, support::endianness::little>(
863                 Reader->ProfileNames, Coverage, FuncRecordsRef,
864                 Reader->MappingRecords, CompilationDir, Reader->Filenames))
865       return std::move(E);
866   } else if (BytesInAddress == 4 && Endian == support::endianness::big) {
867     if (Error E = readCoverageMappingData<uint32_t, support::endianness::big>(
868             Reader->ProfileNames, Coverage, FuncRecordsRef,
869             Reader->MappingRecords, CompilationDir, Reader->Filenames))
870       return std::move(E);
871   } else if (BytesInAddress == 8 && Endian == support::endianness::little) {
872     if (Error E =
873             readCoverageMappingData<uint64_t, support::endianness::little>(
874                 Reader->ProfileNames, Coverage, FuncRecordsRef,
875                 Reader->MappingRecords, CompilationDir, Reader->Filenames))
876       return std::move(E);
877   } else if (BytesInAddress == 8 && Endian == support::endianness::big) {
878     if (Error E = readCoverageMappingData<uint64_t, support::endianness::big>(
879             Reader->ProfileNames, Coverage, FuncRecordsRef,
880             Reader->MappingRecords, CompilationDir, Reader->Filenames))
881       return std::move(E);
882   } else
883     return make_error<CoverageMapError>(
884         coveragemap_error::malformed,
885         "not supported endianness or bytes in address");
886   return std::move(Reader);
887 }
888 
889 static Expected<std::unique_ptr<BinaryCoverageReader>>
890 loadTestingFormat(StringRef Data, StringRef CompilationDir) {
891   uint8_t BytesInAddress = 8;
892   support::endianness Endian = support::endianness::little;
893 
894   // Read the magic and version.
895   Data = Data.substr(sizeof(TestingFormatMagic));
896   if (Data.size() < sizeof(uint64_t))
897     return make_error<CoverageMapError>(coveragemap_error::malformed,
898                                         "the size of data is too small");
899   auto TestingVersion =
900       support::endian::byte_swap<uint64_t, support::endianness::little>(
901           *reinterpret_cast<const uint64_t *>(Data.data()));
902   Data = Data.substr(sizeof(uint64_t));
903 
904   // Read the ProfileNames data.
905   if (Data.empty())
906     return make_error<CoverageMapError>(coveragemap_error::truncated);
907   unsigned N = 0;
908   uint64_t ProfileNamesSize = decodeULEB128(Data.bytes_begin(), &N);
909   if (N > Data.size())
910     return make_error<CoverageMapError>(
911         coveragemap_error::malformed,
912         "the size of TestingFormatMagic is too big");
913   Data = Data.substr(N);
914   if (Data.empty())
915     return make_error<CoverageMapError>(coveragemap_error::truncated);
916   N = 0;
917   uint64_t Address = decodeULEB128(Data.bytes_begin(), &N);
918   if (N > Data.size())
919     return make_error<CoverageMapError>(coveragemap_error::malformed,
920                                         "the size of ULEB128 is too big");
921   Data = Data.substr(N);
922   if (Data.size() < ProfileNamesSize)
923     return make_error<CoverageMapError>(coveragemap_error::malformed,
924                                         "the size of ProfileNames is too big");
925   InstrProfSymtab ProfileNames;
926   if (Error E = ProfileNames.create(Data.substr(0, ProfileNamesSize), Address))
927     return std::move(E);
928   Data = Data.substr(ProfileNamesSize);
929 
930   // In Version2, the size of CoverageMapping is stored directly.
931   uint64_t CoverageMappingSize;
932   if (TestingVersion == uint64_t(TestingFormatVersion::Version2)) {
933     N = 0;
934     CoverageMappingSize = decodeULEB128(Data.bytes_begin(), &N);
935     if (N > Data.size())
936       return make_error<CoverageMapError>(coveragemap_error::malformed,
937                                           "the size of ULEB128 is too big");
938     Data = Data.substr(N);
939     if (CoverageMappingSize < sizeof(CovMapHeader))
940       return make_error<CoverageMapError>(
941           coveragemap_error::malformed,
942           "the size of CoverageMapping is teoo small");
943   } else if (TestingVersion != uint64_t(TestingFormatVersion::Version1)) {
944     return make_error<CoverageMapError>(coveragemap_error::unsupported_version);
945   }
946 
947   // Skip the padding bytes because coverage map data has an alignment of 8.
948   auto Pad = offsetToAlignedAddr(Data.data(), Align(8));
949   if (Data.size() < Pad)
950     return make_error<CoverageMapError>(coveragemap_error::malformed,
951                                         "insufficient padding");
952   Data = Data.substr(Pad);
953   if (Data.size() < sizeof(CovMapHeader))
954     return make_error<CoverageMapError>(
955         coveragemap_error::malformed,
956         "coverage mapping header section is larger than data size");
957   auto const *CovHeader = reinterpret_cast<const CovMapHeader *>(
958       Data.substr(0, sizeof(CovMapHeader)).data());
959   auto Version =
960       CovMapVersion(CovHeader->getVersion<support::endianness::little>());
961 
962   // In Version1, the size of CoverageMapping is calculated.
963   if (TestingVersion == uint64_t(TestingFormatVersion::Version1)) {
964     if (Version < CovMapVersion::Version4) {
965       CoverageMappingSize = Data.size();
966     } else {
967       auto FilenamesSize =
968           CovHeader->getFilenamesSize<support::endianness::little>();
969       CoverageMappingSize = sizeof(CovMapHeader) + FilenamesSize;
970     }
971   }
972 
973   auto CoverageMapping = Data.substr(0, CoverageMappingSize);
974   Data = Data.substr(CoverageMappingSize);
975 
976   // Read the CoverageRecords data.
977   if (Version < CovMapVersion::Version4) {
978     if (!Data.empty())
979       return make_error<CoverageMapError>(coveragemap_error::malformed,
980                                           "data is not empty");
981   } else {
982     // Skip the padding bytes because coverage records data has an alignment
983     // of 8.
984     Pad = offsetToAlignedAddr(Data.data(), Align(8));
985     if (Data.size() < Pad)
986       return make_error<CoverageMapError>(coveragemap_error::malformed,
987                                           "insufficient padding");
988     Data = Data.substr(Pad);
989   }
990   BinaryCoverageReader::FuncRecordsStorage CoverageRecords =
991       MemoryBuffer::getMemBuffer(Data);
992 
993   return BinaryCoverageReader::createCoverageReaderFromBuffer(
994       CoverageMapping, std::move(CoverageRecords), std::move(ProfileNames),
995       BytesInAddress, Endian, CompilationDir);
996 }
997 
998 /// Find all sections that match \p Name. There may be more than one if comdats
999 /// are in use, e.g. for the __llvm_covfun section on ELF.
1000 static Expected<std::vector<SectionRef>> lookupSections(ObjectFile &OF,
1001                                                         StringRef Name) {
1002   // On COFF, the object file section name may end in "$M". This tells the
1003   // linker to sort these sections between "$A" and "$Z". The linker removes the
1004   // dollar and everything after it in the final binary. Do the same to match.
1005   bool IsCOFF = isa<COFFObjectFile>(OF);
1006   auto stripSuffix = [IsCOFF](StringRef N) {
1007     return IsCOFF ? N.split('$').first : N;
1008   };
1009   Name = stripSuffix(Name);
1010 
1011   std::vector<SectionRef> Sections;
1012   for (const auto &Section : OF.sections()) {
1013     Expected<StringRef> NameOrErr = Section.getName();
1014     if (!NameOrErr)
1015       return NameOrErr.takeError();
1016     if (stripSuffix(*NameOrErr) == Name)
1017       Sections.push_back(Section);
1018   }
1019   if (Sections.empty())
1020     return make_error<CoverageMapError>(coveragemap_error::no_data_found);
1021   return Sections;
1022 }
1023 
1024 static Expected<std::unique_ptr<BinaryCoverageReader>>
1025 loadBinaryFormat(std::unique_ptr<Binary> Bin, StringRef Arch,
1026                  StringRef CompilationDir = "",
1027                  object::BuildIDRef *BinaryID = nullptr) {
1028   std::unique_ptr<ObjectFile> OF;
1029   if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin.get())) {
1030     // If we have a universal binary, try to look up the object for the
1031     // appropriate architecture.
1032     auto ObjectFileOrErr = Universal->getMachOObjectForArch(Arch);
1033     if (!ObjectFileOrErr)
1034       return ObjectFileOrErr.takeError();
1035     OF = std::move(ObjectFileOrErr.get());
1036   } else if (isa<ObjectFile>(Bin.get())) {
1037     // For any other object file, upcast and take ownership.
1038     OF.reset(cast<ObjectFile>(Bin.release()));
1039     // If we've asked for a particular arch, make sure they match.
1040     if (!Arch.empty() && OF->getArch() != Triple(Arch).getArch())
1041       return errorCodeToError(object_error::arch_not_found);
1042   } else
1043     // We can only handle object files.
1044     return make_error<CoverageMapError>(coveragemap_error::malformed,
1045                                         "binary is not an object file");
1046 
1047   // The coverage uses native pointer sizes for the object it's written in.
1048   uint8_t BytesInAddress = OF->getBytesInAddress();
1049   support::endianness Endian = OF->isLittleEndian()
1050                                    ? support::endianness::little
1051                                    : support::endianness::big;
1052 
1053   // Look for the sections that we are interested in.
1054   auto ObjFormat = OF->getTripleObjectFormat();
1055   auto NamesSection =
1056       lookupSections(*OF, getInstrProfSectionName(IPSK_name, ObjFormat,
1057                                                  /*AddSegmentInfo=*/false));
1058   if (auto E = NamesSection.takeError())
1059     return std::move(E);
1060   auto CoverageSection =
1061       lookupSections(*OF, getInstrProfSectionName(IPSK_covmap, ObjFormat,
1062                                                   /*AddSegmentInfo=*/false));
1063   if (auto E = CoverageSection.takeError())
1064     return std::move(E);
1065   std::vector<SectionRef> CoverageSectionRefs = *CoverageSection;
1066   if (CoverageSectionRefs.size() != 1)
1067     return make_error<CoverageMapError>(coveragemap_error::malformed,
1068                                         "the size of name section is not one");
1069   auto CoverageMappingOrErr = CoverageSectionRefs.back().getContents();
1070   if (!CoverageMappingOrErr)
1071     return CoverageMappingOrErr.takeError();
1072   StringRef CoverageMapping = CoverageMappingOrErr.get();
1073 
1074   InstrProfSymtab ProfileNames;
1075   std::vector<SectionRef> NamesSectionRefs = *NamesSection;
1076   if (NamesSectionRefs.size() != 1)
1077     return make_error<CoverageMapError>(
1078         coveragemap_error::malformed,
1079         "the size of coverage mapping section is not one");
1080   if (Error E = ProfileNames.create(NamesSectionRefs.back()))
1081     return std::move(E);
1082 
1083   // Look for the coverage records section (Version4 only).
1084   auto CoverageRecordsSections =
1085       lookupSections(*OF, getInstrProfSectionName(IPSK_covfun, ObjFormat,
1086                                                   /*AddSegmentInfo=*/false));
1087 
1088   BinaryCoverageReader::FuncRecordsStorage FuncRecords;
1089   if (auto E = CoverageRecordsSections.takeError()) {
1090     consumeError(std::move(E));
1091     FuncRecords = MemoryBuffer::getMemBuffer("");
1092   } else {
1093     // Compute the FuncRecordsBuffer of the buffer, taking into account the
1094     // padding between each record, and making sure the first block is aligned
1095     // in memory to maintain consistency between buffer address and size
1096     // alignment.
1097     const Align RecordAlignment(8);
1098     uint64_t FuncRecordsSize = 0;
1099     for (SectionRef Section : *CoverageRecordsSections) {
1100       auto CoverageRecordsOrErr = Section.getContents();
1101       if (!CoverageRecordsOrErr)
1102         return CoverageRecordsOrErr.takeError();
1103       FuncRecordsSize += alignTo(CoverageRecordsOrErr->size(), RecordAlignment);
1104     }
1105     auto WritableBuffer =
1106         WritableMemoryBuffer::getNewUninitMemBuffer(FuncRecordsSize);
1107     char *FuncRecordsBuffer = WritableBuffer->getBufferStart();
1108     assert(isAddrAligned(RecordAlignment, FuncRecordsBuffer) &&
1109            "Allocated memory is correctly aligned");
1110 
1111     for (SectionRef Section : *CoverageRecordsSections) {
1112       auto CoverageRecordsOrErr = Section.getContents();
1113       if (!CoverageRecordsOrErr)
1114         return CoverageRecordsOrErr.takeError();
1115       const auto &CoverageRecords = CoverageRecordsOrErr.get();
1116       FuncRecordsBuffer = std::copy(CoverageRecords.begin(),
1117                                     CoverageRecords.end(), FuncRecordsBuffer);
1118       FuncRecordsBuffer =
1119           std::fill_n(FuncRecordsBuffer,
1120                       alignAddr(FuncRecordsBuffer, RecordAlignment) -
1121                           (uintptr_t)FuncRecordsBuffer,
1122                       '\0');
1123     }
1124     assert(FuncRecordsBuffer == WritableBuffer->getBufferEnd() &&
1125            "consistent init");
1126     FuncRecords = std::move(WritableBuffer);
1127   }
1128 
1129   if (BinaryID)
1130     *BinaryID = getBuildID(OF.get());
1131 
1132   return BinaryCoverageReader::createCoverageReaderFromBuffer(
1133       CoverageMapping, std::move(FuncRecords), std::move(ProfileNames),
1134       BytesInAddress, Endian, CompilationDir);
1135 }
1136 
1137 /// Determine whether \p Arch is invalid or empty, given \p Bin.
1138 static bool isArchSpecifierInvalidOrMissing(Binary *Bin, StringRef Arch) {
1139   // If we have a universal binary and Arch doesn't identify any of its slices,
1140   // it's user error.
1141   if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin)) {
1142     for (auto &ObjForArch : Universal->objects())
1143       if (Arch == ObjForArch.getArchFlagName())
1144         return false;
1145     return true;
1146   }
1147   return false;
1148 }
1149 
1150 Expected<std::vector<std::unique_ptr<BinaryCoverageReader>>>
1151 BinaryCoverageReader::create(
1152     MemoryBufferRef ObjectBuffer, StringRef Arch,
1153     SmallVectorImpl<std::unique_ptr<MemoryBuffer>> &ObjectFileBuffers,
1154     StringRef CompilationDir, SmallVectorImpl<object::BuildIDRef> *BinaryIDs) {
1155   std::vector<std::unique_ptr<BinaryCoverageReader>> Readers;
1156 
1157   if (ObjectBuffer.getBuffer().size() > sizeof(TestingFormatMagic)) {
1158     uint64_t Magic =
1159         support::endian::byte_swap<uint64_t, support::endianness::little>(
1160             *reinterpret_cast<const uint64_t *>(ObjectBuffer.getBufferStart()));
1161     if (Magic == TestingFormatMagic) {
1162       // This is a special format used for testing.
1163       auto ReaderOrErr =
1164           loadTestingFormat(ObjectBuffer.getBuffer(), CompilationDir);
1165       if (!ReaderOrErr)
1166         return ReaderOrErr.takeError();
1167       Readers.push_back(std::move(ReaderOrErr.get()));
1168       return std::move(Readers);
1169     }
1170   }
1171 
1172   auto BinOrErr = createBinary(ObjectBuffer);
1173   if (!BinOrErr)
1174     return BinOrErr.takeError();
1175   std::unique_ptr<Binary> Bin = std::move(BinOrErr.get());
1176 
1177   if (isArchSpecifierInvalidOrMissing(Bin.get(), Arch))
1178     return make_error<CoverageMapError>(
1179         coveragemap_error::invalid_or_missing_arch_specifier);
1180 
1181   // MachO universal binaries which contain archives need to be treated as
1182   // archives, not as regular binaries.
1183   if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin.get())) {
1184     for (auto &ObjForArch : Universal->objects()) {
1185       // Skip slices within the universal binary which target the wrong arch.
1186       std::string ObjArch = ObjForArch.getArchFlagName();
1187       if (Arch != ObjArch)
1188         continue;
1189 
1190       auto ArchiveOrErr = ObjForArch.getAsArchive();
1191       if (!ArchiveOrErr) {
1192         // If this is not an archive, try treating it as a regular object.
1193         consumeError(ArchiveOrErr.takeError());
1194         break;
1195       }
1196 
1197       return BinaryCoverageReader::create(
1198           ArchiveOrErr.get()->getMemoryBufferRef(), Arch, ObjectFileBuffers,
1199           CompilationDir, BinaryIDs);
1200     }
1201   }
1202 
1203   // Load coverage out of archive members.
1204   if (auto *Ar = dyn_cast<Archive>(Bin.get())) {
1205     Error Err = Error::success();
1206     for (auto &Child : Ar->children(Err)) {
1207       Expected<MemoryBufferRef> ChildBufOrErr = Child.getMemoryBufferRef();
1208       if (!ChildBufOrErr)
1209         return ChildBufOrErr.takeError();
1210 
1211       auto ChildReadersOrErr = BinaryCoverageReader::create(
1212           ChildBufOrErr.get(), Arch, ObjectFileBuffers, CompilationDir,
1213           BinaryIDs);
1214       if (!ChildReadersOrErr)
1215         return ChildReadersOrErr.takeError();
1216       for (auto &Reader : ChildReadersOrErr.get())
1217         Readers.push_back(std::move(Reader));
1218     }
1219     if (Err)
1220       return std::move(Err);
1221 
1222     // Thin archives reference object files outside of the archive file, i.e.
1223     // files which reside in memory not owned by the caller. Transfer ownership
1224     // to the caller.
1225     if (Ar->isThin())
1226       for (auto &Buffer : Ar->takeThinBuffers())
1227         ObjectFileBuffers.push_back(std::move(Buffer));
1228 
1229     return std::move(Readers);
1230   }
1231 
1232   object::BuildIDRef BinaryID;
1233   auto ReaderOrErr = loadBinaryFormat(std::move(Bin), Arch, CompilationDir,
1234                                       BinaryIDs ? &BinaryID : nullptr);
1235   if (!ReaderOrErr)
1236     return ReaderOrErr.takeError();
1237   Readers.push_back(std::move(ReaderOrErr.get()));
1238   if (!BinaryID.empty())
1239     BinaryIDs->push_back(BinaryID);
1240   return std::move(Readers);
1241 }
1242 
1243 Error BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) {
1244   if (CurrentRecord >= MappingRecords.size())
1245     return make_error<CoverageMapError>(coveragemap_error::eof);
1246 
1247   FunctionsFilenames.clear();
1248   Expressions.clear();
1249   MappingRegions.clear();
1250   auto &R = MappingRecords[CurrentRecord];
1251   auto F = ArrayRef(Filenames).slice(R.FilenamesBegin, R.FilenamesSize);
1252   RawCoverageMappingReader Reader(R.CoverageMapping, F, FunctionsFilenames,
1253                                   Expressions, MappingRegions);
1254   if (auto Err = Reader.read())
1255     return Err;
1256 
1257   Record.FunctionName = R.FunctionName;
1258   Record.FunctionHash = R.FunctionHash;
1259   Record.Filenames = FunctionsFilenames;
1260   Record.Expressions = Expressions;
1261   Record.MappingRegions = MappingRegions;
1262 
1263   ++CurrentRecord;
1264   return Error::success();
1265 }
1266