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