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