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