Path: blob/main/contrib/llvm-project/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp
35271 views
//===- CoverageMappingReader.cpp - Code coverage mapping reader -----------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains support for reading coverage mapping data for9// instrumentation based coverage.10//11//===----------------------------------------------------------------------===//1213#include "llvm/ProfileData/Coverage/CoverageMappingReader.h"14#include "llvm/ADT/ArrayRef.h"15#include "llvm/ADT/DenseMap.h"16#include "llvm/ADT/STLExtras.h"17#include "llvm/ADT/SmallVector.h"18#include "llvm/ADT/Statistic.h"19#include "llvm/ADT/StringRef.h"20#include "llvm/Object/Archive.h"21#include "llvm/Object/Binary.h"22#include "llvm/Object/COFF.h"23#include "llvm/Object/Error.h"24#include "llvm/Object/MachOUniversal.h"25#include "llvm/Object/ObjectFile.h"26#include "llvm/ProfileData/InstrProf.h"27#include "llvm/Support/Casting.h"28#include "llvm/Support/Compression.h"29#include "llvm/Support/Debug.h"30#include "llvm/Support/Endian.h"31#include "llvm/Support/Error.h"32#include "llvm/Support/ErrorHandling.h"33#include "llvm/Support/LEB128.h"34#include "llvm/Support/MathExtras.h"35#include "llvm/Support/Path.h"36#include "llvm/Support/raw_ostream.h"37#include "llvm/TargetParser/Triple.h"38#include <vector>3940using namespace llvm;41using namespace coverage;42using namespace object;4344#define DEBUG_TYPE "coverage-mapping"4546STATISTIC(CovMapNumRecords, "The # of coverage function records");47STATISTIC(CovMapNumUsedRecords, "The # of used coverage function records");4849void CoverageMappingIterator::increment() {50if (ReadErr != coveragemap_error::success)51return;5253// Check if all the records were read or if an error occurred while reading54// the next record.55if (auto E = Reader->readNextRecord(Record))56handleAllErrors(std::move(E), [&](const CoverageMapError &CME) {57if (CME.get() == coveragemap_error::eof)58*this = CoverageMappingIterator();59else60ReadErr = CME.get();61});62}6364Error RawCoverageReader::readULEB128(uint64_t &Result) {65if (Data.empty())66return make_error<CoverageMapError>(coveragemap_error::truncated);67unsigned N = 0;68Result = decodeULEB128(Data.bytes_begin(), &N);69if (N > Data.size())70return make_error<CoverageMapError>(coveragemap_error::malformed,71"the size of ULEB128 is too big");72Data = Data.substr(N);73return Error::success();74}7576Error RawCoverageReader::readIntMax(uint64_t &Result, uint64_t MaxPlus1) {77if (auto Err = readULEB128(Result))78return Err;79if (Result >= MaxPlus1)80return make_error<CoverageMapError>(81coveragemap_error::malformed,82"the value of ULEB128 is greater than or equal to MaxPlus1");83return Error::success();84}8586Error RawCoverageReader::readSize(uint64_t &Result) {87if (auto Err = readULEB128(Result))88return Err;89if (Result > Data.size())90return make_error<CoverageMapError>(coveragemap_error::malformed,91"the value of ULEB128 is too big");92return Error::success();93}9495Error RawCoverageReader::readString(StringRef &Result) {96uint64_t Length;97if (auto Err = readSize(Length))98return Err;99Result = Data.substr(0, Length);100Data = Data.substr(Length);101return Error::success();102}103104Error RawCoverageFilenamesReader::read(CovMapVersion Version) {105uint64_t NumFilenames;106if (auto Err = readSize(NumFilenames))107return Err;108if (!NumFilenames)109return make_error<CoverageMapError>(coveragemap_error::malformed,110"number of filenames is zero");111112if (Version < CovMapVersion::Version4)113return readUncompressed(Version, NumFilenames);114115// The uncompressed length may exceed the size of the encoded filenames.116// Skip size validation.117uint64_t UncompressedLen;118if (auto Err = readULEB128(UncompressedLen))119return Err;120121uint64_t CompressedLen;122if (auto Err = readSize(CompressedLen))123return Err;124125if (CompressedLen > 0) {126if (!compression::zlib::isAvailable())127return make_error<CoverageMapError>(128coveragemap_error::decompression_failed);129130// Allocate memory for the decompressed filenames.131SmallVector<uint8_t, 0> StorageBuf;132133// Read compressed filenames.134StringRef CompressedFilenames = Data.substr(0, CompressedLen);135Data = Data.substr(CompressedLen);136auto Err = compression::zlib::decompress(137arrayRefFromStringRef(CompressedFilenames), StorageBuf,138UncompressedLen);139if (Err) {140consumeError(std::move(Err));141return make_error<CoverageMapError>(142coveragemap_error::decompression_failed);143}144145RawCoverageFilenamesReader Delegate(toStringRef(StorageBuf), Filenames,146CompilationDir);147return Delegate.readUncompressed(Version, NumFilenames);148}149150return readUncompressed(Version, NumFilenames);151}152153Error RawCoverageFilenamesReader::readUncompressed(CovMapVersion Version,154uint64_t NumFilenames) {155// Read uncompressed filenames.156if (Version < CovMapVersion::Version6) {157for (size_t I = 0; I < NumFilenames; ++I) {158StringRef Filename;159if (auto Err = readString(Filename))160return Err;161Filenames.push_back(Filename.str());162}163} else {164StringRef CWD;165if (auto Err = readString(CWD))166return Err;167Filenames.push_back(CWD.str());168169for (size_t I = 1; I < NumFilenames; ++I) {170StringRef Filename;171if (auto Err = readString(Filename))172return Err;173if (sys::path::is_absolute(Filename)) {174Filenames.push_back(Filename.str());175} else {176SmallString<256> P;177if (!CompilationDir.empty())178P.assign(CompilationDir);179else180P.assign(CWD);181llvm::sys::path::append(P, Filename);182sys::path::remove_dots(P, /*remove_dot_dot=*/true);183Filenames.push_back(static_cast<std::string>(P.str()));184}185}186}187return Error::success();188}189190Error RawCoverageMappingReader::decodeCounter(unsigned Value, Counter &C) {191auto Tag = Value & Counter::EncodingTagMask;192switch (Tag) {193case Counter::Zero:194C = Counter::getZero();195return Error::success();196case Counter::CounterValueReference:197C = Counter::getCounter(Value >> Counter::EncodingTagBits);198return Error::success();199default:200break;201}202Tag -= Counter::Expression;203switch (Tag) {204case CounterExpression::Subtract:205case CounterExpression::Add: {206auto ID = Value >> Counter::EncodingTagBits;207if (ID >= Expressions.size())208return make_error<CoverageMapError>(coveragemap_error::malformed,209"counter expression is invalid");210Expressions[ID].Kind = CounterExpression::ExprKind(Tag);211C = Counter::getExpression(ID);212break;213}214default:215return make_error<CoverageMapError>(coveragemap_error::malformed,216"counter expression kind is invalid");217}218return Error::success();219}220221Error RawCoverageMappingReader::readCounter(Counter &C) {222uint64_t EncodedCounter;223if (auto Err =224readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max()))225return Err;226if (auto Err = decodeCounter(EncodedCounter, C))227return Err;228return Error::success();229}230231static const unsigned EncodingExpansionRegionBit = 1232<< Counter::EncodingTagBits;233234/// Read the sub-array of regions for the given inferred file id.235/// \param NumFileIDs the number of file ids that are defined for this236/// function.237Error RawCoverageMappingReader::readMappingRegionsSubArray(238std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID,239size_t NumFileIDs) {240uint64_t NumRegions;241if (auto Err = readSize(NumRegions))242return Err;243unsigned LineStart = 0;244for (size_t I = 0; I < NumRegions; ++I) {245Counter C, C2;246uint64_t BIDX, NC;247// They are stored as internal values plus 1 (min is -1)248uint64_t ID1, TID1, FID1;249mcdc::Parameters Params;250CounterMappingRegion::RegionKind Kind = CounterMappingRegion::CodeRegion;251252// Read the combined counter + region kind.253uint64_t EncodedCounterAndRegion;254if (auto Err = readIntMax(EncodedCounterAndRegion,255std::numeric_limits<unsigned>::max()))256return Err;257unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;258uint64_t ExpandedFileID = 0;259260// If Tag does not represent a ZeroCounter, then it is understood to refer261// to a counter or counter expression with region kind assumed to be262// "CodeRegion". In that case, EncodedCounterAndRegion actually encodes the263// referenced counter or counter expression (and nothing else).264//265// If Tag represents a ZeroCounter and EncodingExpansionRegionBit is set,266// then EncodedCounterAndRegion is interpreted to represent an267// ExpansionRegion. In all other cases, EncodedCounterAndRegion is268// interpreted to refer to a specific region kind, after which additional269// fields may be read (e.g. BranchRegions have two encoded counters that270// follow an encoded region kind value).271if (Tag != Counter::Zero) {272if (auto Err = decodeCounter(EncodedCounterAndRegion, C))273return Err;274} else {275// Is it an expansion region?276if (EncodedCounterAndRegion & EncodingExpansionRegionBit) {277Kind = CounterMappingRegion::ExpansionRegion;278ExpandedFileID = EncodedCounterAndRegion >>279Counter::EncodingCounterTagAndExpansionRegionTagBits;280if (ExpandedFileID >= NumFileIDs)281return make_error<CoverageMapError>(coveragemap_error::malformed,282"ExpandedFileID is invalid");283} else {284switch (EncodedCounterAndRegion >>285Counter::EncodingCounterTagAndExpansionRegionTagBits) {286case CounterMappingRegion::CodeRegion:287// Don't do anything when we have a code region with a zero counter.288break;289case CounterMappingRegion::SkippedRegion:290Kind = CounterMappingRegion::SkippedRegion;291break;292case CounterMappingRegion::BranchRegion:293// For a Branch Region, read two successive counters.294Kind = CounterMappingRegion::BranchRegion;295if (auto Err = readCounter(C))296return Err;297if (auto Err = readCounter(C2))298return Err;299break;300case CounterMappingRegion::MCDCBranchRegion:301// For a MCDC Branch Region, read two successive counters and 3 IDs.302Kind = CounterMappingRegion::MCDCBranchRegion;303if (auto Err = readCounter(C))304return Err;305if (auto Err = readCounter(C2))306return Err;307if (auto Err = readIntMax(ID1, std::numeric_limits<int16_t>::max()))308return Err;309if (auto Err = readIntMax(TID1, std::numeric_limits<int16_t>::max()))310return Err;311if (auto Err = readIntMax(FID1, std::numeric_limits<int16_t>::max()))312return Err;313if (ID1 == 0)314return make_error<CoverageMapError>(315coveragemap_error::malformed,316"MCDCConditionID shouldn't be zero");317Params = mcdc::BranchParameters{318static_cast<int16_t>(static_cast<int16_t>(ID1) - 1),319{static_cast<int16_t>(static_cast<int16_t>(FID1) - 1),320static_cast<int16_t>(static_cast<int16_t>(TID1) - 1)}};321break;322case CounterMappingRegion::MCDCDecisionRegion:323Kind = CounterMappingRegion::MCDCDecisionRegion;324if (auto Err = readIntMax(BIDX, std::numeric_limits<unsigned>::max()))325return Err;326if (auto Err = readIntMax(NC, std::numeric_limits<int16_t>::max()))327return Err;328Params = mcdc::DecisionParameters{static_cast<unsigned>(BIDX),329static_cast<uint16_t>(NC)};330break;331default:332return make_error<CoverageMapError>(coveragemap_error::malformed,333"region kind is incorrect");334}335}336}337338// Read the source range.339uint64_t LineStartDelta, ColumnStart, NumLines, ColumnEnd;340if (auto Err =341readIntMax(LineStartDelta, std::numeric_limits<unsigned>::max()))342return Err;343if (auto Err = readULEB128(ColumnStart))344return Err;345if (ColumnStart > std::numeric_limits<unsigned>::max())346return make_error<CoverageMapError>(coveragemap_error::malformed,347"start column is too big");348if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max()))349return Err;350if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max()))351return Err;352LineStart += LineStartDelta;353354// If the high bit of ColumnEnd is set, this is a gap region.355if (ColumnEnd & (1U << 31)) {356Kind = CounterMappingRegion::GapRegion;357ColumnEnd &= ~(1U << 31);358}359360// Adjust the column locations for the empty regions that are supposed to361// cover whole lines. Those regions should be encoded with the362// column range (1 -> std::numeric_limits<unsigned>::max()), but because363// the encoded std::numeric_limits<unsigned>::max() is several bytes long,364// we set the column range to (0 -> 0) to ensure that the column start and365// column end take up one byte each.366// The std::numeric_limits<unsigned>::max() is used to represent a column367// position at the end of the line without knowing the length of that line.368if (ColumnStart == 0 && ColumnEnd == 0) {369ColumnStart = 1;370ColumnEnd = std::numeric_limits<unsigned>::max();371}372373LLVM_DEBUG({374dbgs() << "Counter in file " << InferredFileID << " " << LineStart << ":"375<< ColumnStart << " -> " << (LineStart + NumLines) << ":"376<< ColumnEnd << ", ";377if (Kind == CounterMappingRegion::ExpansionRegion)378dbgs() << "Expands to file " << ExpandedFileID;379else380CounterMappingContext(Expressions).dump(C, dbgs());381dbgs() << "\n";382});383384auto CMR = CounterMappingRegion(385C, C2, InferredFileID, ExpandedFileID, LineStart, ColumnStart,386LineStart + NumLines, ColumnEnd, Kind, Params);387if (CMR.startLoc() > CMR.endLoc())388return make_error<CoverageMapError>(389coveragemap_error::malformed,390"counter mapping region locations are incorrect");391MappingRegions.push_back(CMR);392}393return Error::success();394}395396Error RawCoverageMappingReader::read() {397// Read the virtual file mapping.398SmallVector<unsigned, 8> VirtualFileMapping;399uint64_t NumFileMappings;400if (auto Err = readSize(NumFileMappings))401return Err;402for (size_t I = 0; I < NumFileMappings; ++I) {403uint64_t FilenameIndex;404if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size()))405return Err;406VirtualFileMapping.push_back(FilenameIndex);407}408409// Construct the files using unique filenames and virtual file mapping.410for (auto I : VirtualFileMapping) {411Filenames.push_back(TranslationUnitFilenames[I]);412}413414// Read the expressions.415uint64_t NumExpressions;416if (auto Err = readSize(NumExpressions))417return Err;418// Create an array of dummy expressions that get the proper counters419// when the expressions are read, and the proper kinds when the counters420// are decoded.421Expressions.resize(422NumExpressions,423CounterExpression(CounterExpression::Subtract, Counter(), Counter()));424for (size_t I = 0; I < NumExpressions; ++I) {425if (auto Err = readCounter(Expressions[I].LHS))426return Err;427if (auto Err = readCounter(Expressions[I].RHS))428return Err;429}430431// Read the mapping regions sub-arrays.432for (unsigned InferredFileID = 0, S = VirtualFileMapping.size();433InferredFileID < S; ++InferredFileID) {434if (auto Err = readMappingRegionsSubArray(MappingRegions, InferredFileID,435VirtualFileMapping.size()))436return Err;437}438439// Set the counters for the expansion regions.440// i.e. Counter of expansion region = counter of the first region441// from the expanded file.442// Perform multiple passes to correctly propagate the counters through443// all the nested expansion regions.444SmallVector<CounterMappingRegion *, 8> FileIDExpansionRegionMapping;445FileIDExpansionRegionMapping.resize(VirtualFileMapping.size(), nullptr);446for (unsigned Pass = 1, S = VirtualFileMapping.size(); Pass < S; ++Pass) {447for (auto &R : MappingRegions) {448if (R.Kind != CounterMappingRegion::ExpansionRegion)449continue;450assert(!FileIDExpansionRegionMapping[R.ExpandedFileID]);451FileIDExpansionRegionMapping[R.ExpandedFileID] = &R;452}453for (auto &R : MappingRegions) {454if (FileIDExpansionRegionMapping[R.FileID]) {455FileIDExpansionRegionMapping[R.FileID]->Count = R.Count;456FileIDExpansionRegionMapping[R.FileID] = nullptr;457}458}459}460461return Error::success();462}463464Expected<bool> RawCoverageMappingDummyChecker::isDummy() {465// A dummy coverage mapping data consists of just one region with zero count.466uint64_t NumFileMappings;467if (Error Err = readSize(NumFileMappings))468return std::move(Err);469if (NumFileMappings != 1)470return false;471// We don't expect any specific value for the filename index, just skip it.472uint64_t FilenameIndex;473if (Error Err =474readIntMax(FilenameIndex, std::numeric_limits<unsigned>::max()))475return std::move(Err);476uint64_t NumExpressions;477if (Error Err = readSize(NumExpressions))478return std::move(Err);479if (NumExpressions != 0)480return false;481uint64_t NumRegions;482if (Error Err = readSize(NumRegions))483return std::move(Err);484if (NumRegions != 1)485return false;486uint64_t EncodedCounterAndRegion;487if (Error Err = readIntMax(EncodedCounterAndRegion,488std::numeric_limits<unsigned>::max()))489return std::move(Err);490unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;491return Tag == Counter::Zero;492}493494Error InstrProfSymtab::create(SectionRef &Section) {495Expected<StringRef> DataOrErr = Section.getContents();496if (!DataOrErr)497return DataOrErr.takeError();498Data = *DataOrErr;499Address = Section.getAddress();500501// If this is a linked PE/COFF file, then we have to skip over the null byte502// that is allocated in the .lprfn$A section in the LLVM profiling runtime.503// If the name section is .lprfcovnames, it doesn't have the null byte at the504// beginning.505const ObjectFile *Obj = Section.getObject();506if (isa<COFFObjectFile>(Obj) && !Obj->isRelocatableObject())507if (Expected<StringRef> NameOrErr = Section.getName())508if (*NameOrErr != getInstrProfSectionName(IPSK_covname, Triple::COFF))509Data = Data.drop_front(1);510511return Error::success();512}513514StringRef InstrProfSymtab::getFuncName(uint64_t Pointer, size_t Size) {515if (Pointer < Address)516return StringRef();517auto Offset = Pointer - Address;518if (Offset + Size > Data.size())519return StringRef();520return Data.substr(Pointer - Address, Size);521}522523// Check if the mapping data is a dummy, i.e. is emitted for an unused function.524static Expected<bool> isCoverageMappingDummy(uint64_t Hash, StringRef Mapping) {525// The hash value of dummy mapping records is always zero.526if (Hash)527return false;528return RawCoverageMappingDummyChecker(Mapping).isDummy();529}530531/// A range of filename indices. Used to specify the location of a batch of532/// filenames in a vector-like container.533struct FilenameRange {534unsigned StartingIndex;535unsigned Length;536537FilenameRange(unsigned StartingIndex, unsigned Length)538: StartingIndex(StartingIndex), Length(Length) {}539540void markInvalid() { Length = 0; }541bool isInvalid() const { return Length == 0; }542};543544namespace {545546/// The interface to read coverage mapping function records for a module.547struct CovMapFuncRecordReader {548virtual ~CovMapFuncRecordReader() = default;549550// Read a coverage header.551//552// \p CovBuf points to the buffer containing the \c CovHeader of the coverage553// mapping data associated with the module.554//555// Returns a pointer to the next \c CovHeader if it exists, or to an address556// greater than \p CovEnd if not.557virtual Expected<const char *> readCoverageHeader(const char *CovBuf,558const char *CovBufEnd) = 0;559560// Read function records.561//562// \p FuncRecBuf points to the buffer containing a batch of function records.563// \p FuncRecBufEnd points past the end of the batch of records.564//565// Prior to Version4, \p OutOfLineFileRange points to a sequence of filenames566// associated with the function records. It is unused in Version4.567//568// Prior to Version4, \p OutOfLineMappingBuf points to a sequence of coverage569// mappings associated with the function records. It is unused in Version4.570virtual Error571readFunctionRecords(const char *FuncRecBuf, const char *FuncRecBufEnd,572std::optional<FilenameRange> OutOfLineFileRange,573const char *OutOfLineMappingBuf,574const char *OutOfLineMappingBufEnd) = 0;575576template <class IntPtrT, llvm::endianness Endian>577static Expected<std::unique_ptr<CovMapFuncRecordReader>>578get(CovMapVersion Version, InstrProfSymtab &P,579std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, StringRef D,580std::vector<std::string> &F);581};582583// A class for reading coverage mapping function records for a module.584template <CovMapVersion Version, class IntPtrT, llvm::endianness Endian>585class VersionedCovMapFuncRecordReader : public CovMapFuncRecordReader {586using FuncRecordType =587typename CovMapTraits<Version, IntPtrT>::CovMapFuncRecordType;588using NameRefType = typename CovMapTraits<Version, IntPtrT>::NameRefType;589590// Maps function's name references to the indexes of their records591// in \c Records.592DenseMap<NameRefType, size_t> FunctionRecords;593InstrProfSymtab &ProfileNames;594StringRef CompilationDir;595std::vector<std::string> &Filenames;596std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records;597598// Maps a hash of the filenames in a TU to a \c FileRange. The range599// specifies the location of the hashed filenames in \c Filenames.600DenseMap<uint64_t, FilenameRange> FileRangeMap;601602// Add the record to the collection if we don't already have a record that603// points to the same function name. This is useful to ignore the redundant604// records for the functions with ODR linkage.605// In addition, prefer records with real coverage mapping data to dummy606// records, which were emitted for inline functions which were seen but607// not used in the corresponding translation unit.608Error insertFunctionRecordIfNeeded(const FuncRecordType *CFR,609StringRef Mapping,610FilenameRange FileRange) {611++CovMapNumRecords;612uint64_t FuncHash = CFR->template getFuncHash<Endian>();613NameRefType NameRef = CFR->template getFuncNameRef<Endian>();614auto InsertResult =615FunctionRecords.insert(std::make_pair(NameRef, Records.size()));616if (InsertResult.second) {617StringRef FuncName;618if (Error Err = CFR->template getFuncName<Endian>(ProfileNames, FuncName))619return Err;620if (FuncName.empty())621return make_error<InstrProfError>(instrprof_error::malformed,622"function name is empty");623++CovMapNumUsedRecords;624Records.emplace_back(Version, FuncName, FuncHash, Mapping,625FileRange.StartingIndex, FileRange.Length);626return Error::success();627}628// Update the existing record if it's a dummy and the new record is real.629size_t OldRecordIndex = InsertResult.first->second;630BinaryCoverageReader::ProfileMappingRecord &OldRecord =631Records[OldRecordIndex];632Expected<bool> OldIsDummyExpected = isCoverageMappingDummy(633OldRecord.FunctionHash, OldRecord.CoverageMapping);634if (Error Err = OldIsDummyExpected.takeError())635return Err;636if (!*OldIsDummyExpected)637return Error::success();638Expected<bool> NewIsDummyExpected =639isCoverageMappingDummy(FuncHash, Mapping);640if (Error Err = NewIsDummyExpected.takeError())641return Err;642if (*NewIsDummyExpected)643return Error::success();644++CovMapNumUsedRecords;645OldRecord.FunctionHash = FuncHash;646OldRecord.CoverageMapping = Mapping;647OldRecord.FilenamesBegin = FileRange.StartingIndex;648OldRecord.FilenamesSize = FileRange.Length;649return Error::success();650}651652public:653VersionedCovMapFuncRecordReader(654InstrProfSymtab &P,655std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, StringRef D,656std::vector<std::string> &F)657: ProfileNames(P), CompilationDir(D), Filenames(F), Records(R) {}658659~VersionedCovMapFuncRecordReader() override = default;660661Expected<const char *> readCoverageHeader(const char *CovBuf,662const char *CovBufEnd) override {663using namespace support;664665if (CovBuf + sizeof(CovMapHeader) > CovBufEnd)666return make_error<CoverageMapError>(667coveragemap_error::malformed,668"coverage mapping header section is larger than buffer size");669auto CovHeader = reinterpret_cast<const CovMapHeader *>(CovBuf);670uint32_t NRecords = CovHeader->getNRecords<Endian>();671uint32_t FilenamesSize = CovHeader->getFilenamesSize<Endian>();672uint32_t CoverageSize = CovHeader->getCoverageSize<Endian>();673assert((CovMapVersion)CovHeader->getVersion<Endian>() == Version);674CovBuf = reinterpret_cast<const char *>(CovHeader + 1);675676// Skip past the function records, saving the start and end for later.677// This is a no-op in Version4 (function records are read after all headers678// are read).679const char *FuncRecBuf = nullptr;680const char *FuncRecBufEnd = nullptr;681if (Version < CovMapVersion::Version4)682FuncRecBuf = CovBuf;683CovBuf += NRecords * sizeof(FuncRecordType);684if (Version < CovMapVersion::Version4)685FuncRecBufEnd = CovBuf;686687// Get the filenames.688if (CovBuf + FilenamesSize > CovBufEnd)689return make_error<CoverageMapError>(690coveragemap_error::malformed,691"filenames section is larger than buffer size");692size_t FilenamesBegin = Filenames.size();693StringRef FilenameRegion(CovBuf, FilenamesSize);694RawCoverageFilenamesReader Reader(FilenameRegion, Filenames,695CompilationDir);696if (auto Err = Reader.read(Version))697return std::move(Err);698CovBuf += FilenamesSize;699FilenameRange FileRange(FilenamesBegin, Filenames.size() - FilenamesBegin);700701if (Version >= CovMapVersion::Version4) {702// Map a hash of the filenames region to the filename range associated703// with this coverage header.704int64_t FilenamesRef =705llvm::IndexedInstrProf::ComputeHash(FilenameRegion);706auto Insert =707FileRangeMap.insert(std::make_pair(FilenamesRef, FileRange));708if (!Insert.second) {709// The same filenames ref was encountered twice. It's possible that710// the associated filenames are the same.711auto It = Filenames.begin();712FilenameRange &OrigRange = Insert.first->getSecond();713if (std::equal(It + OrigRange.StartingIndex,714It + OrigRange.StartingIndex + OrigRange.Length,715It + FileRange.StartingIndex,716It + FileRange.StartingIndex + FileRange.Length))717// Map the new range to the original one.718FileRange = OrigRange;719else720// This is a hash collision. Mark the filenames ref invalid.721OrigRange.markInvalid();722}723}724725// We'll read the coverage mapping records in the loop below.726// This is a no-op in Version4 (coverage mappings are not affixed to the727// coverage header).728const char *MappingBuf = CovBuf;729if (Version >= CovMapVersion::Version4 && CoverageSize != 0)730return make_error<CoverageMapError>(coveragemap_error::malformed,731"coverage mapping size is not zero");732CovBuf += CoverageSize;733const char *MappingEnd = CovBuf;734735if (CovBuf > CovBufEnd)736return make_error<CoverageMapError>(737coveragemap_error::malformed,738"function records section is larger than buffer size");739740if (Version < CovMapVersion::Version4) {741// Read each function record.742if (Error E = readFunctionRecords(FuncRecBuf, FuncRecBufEnd, FileRange,743MappingBuf, MappingEnd))744return std::move(E);745}746747// Each coverage map has an alignment of 8, so we need to adjust alignment748// before reading the next map.749CovBuf += offsetToAlignedAddr(CovBuf, Align(8));750751return CovBuf;752}753754Error readFunctionRecords(const char *FuncRecBuf, const char *FuncRecBufEnd,755std::optional<FilenameRange> OutOfLineFileRange,756const char *OutOfLineMappingBuf,757const char *OutOfLineMappingBufEnd) override {758auto CFR = reinterpret_cast<const FuncRecordType *>(FuncRecBuf);759while ((const char *)CFR < FuncRecBufEnd) {760// Validate the length of the coverage mapping for this function.761const char *NextMappingBuf;762const FuncRecordType *NextCFR;763std::tie(NextMappingBuf, NextCFR) =764CFR->template advanceByOne<Endian>(OutOfLineMappingBuf);765if (Version < CovMapVersion::Version4)766if (NextMappingBuf > OutOfLineMappingBufEnd)767return make_error<CoverageMapError>(768coveragemap_error::malformed,769"next mapping buffer is larger than buffer size");770771// Look up the set of filenames associated with this function record.772std::optional<FilenameRange> FileRange;773if (Version < CovMapVersion::Version4) {774FileRange = OutOfLineFileRange;775} else {776uint64_t FilenamesRef = CFR->template getFilenamesRef<Endian>();777auto It = FileRangeMap.find(FilenamesRef);778if (It == FileRangeMap.end())779return make_error<CoverageMapError>(780coveragemap_error::malformed,781"no filename found for function with hash=0x" +782Twine::utohexstr(FilenamesRef));783else784FileRange = It->getSecond();785}786787// Now, read the coverage data.788if (FileRange && !FileRange->isInvalid()) {789StringRef Mapping =790CFR->template getCoverageMapping<Endian>(OutOfLineMappingBuf);791if (Version >= CovMapVersion::Version4 &&792Mapping.data() + Mapping.size() > FuncRecBufEnd)793return make_error<CoverageMapError>(794coveragemap_error::malformed,795"coverage mapping data is larger than buffer size");796if (Error Err = insertFunctionRecordIfNeeded(CFR, Mapping, *FileRange))797return Err;798}799800std::tie(OutOfLineMappingBuf, CFR) = std::tie(NextMappingBuf, NextCFR);801}802return Error::success();803}804};805806} // end anonymous namespace807808template <class IntPtrT, llvm::endianness Endian>809Expected<std::unique_ptr<CovMapFuncRecordReader>> CovMapFuncRecordReader::get(810CovMapVersion Version, InstrProfSymtab &P,811std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, StringRef D,812std::vector<std::string> &F) {813using namespace coverage;814815switch (Version) {816case CovMapVersion::Version1:817return std::make_unique<VersionedCovMapFuncRecordReader<818CovMapVersion::Version1, IntPtrT, Endian>>(P, R, D, F);819case CovMapVersion::Version2:820case CovMapVersion::Version3:821case CovMapVersion::Version4:822case CovMapVersion::Version5:823case CovMapVersion::Version6:824case CovMapVersion::Version7:825// Decompress the name data.826if (Error E = P.create(P.getNameData()))827return std::move(E);828if (Version == CovMapVersion::Version2)829return std::make_unique<VersionedCovMapFuncRecordReader<830CovMapVersion::Version2, IntPtrT, Endian>>(P, R, D, F);831else if (Version == CovMapVersion::Version3)832return std::make_unique<VersionedCovMapFuncRecordReader<833CovMapVersion::Version3, IntPtrT, Endian>>(P, R, D, F);834else if (Version == CovMapVersion::Version4)835return std::make_unique<VersionedCovMapFuncRecordReader<836CovMapVersion::Version4, IntPtrT, Endian>>(P, R, D, F);837else if (Version == CovMapVersion::Version5)838return std::make_unique<VersionedCovMapFuncRecordReader<839CovMapVersion::Version5, IntPtrT, Endian>>(P, R, D, F);840else if (Version == CovMapVersion::Version6)841return std::make_unique<VersionedCovMapFuncRecordReader<842CovMapVersion::Version6, IntPtrT, Endian>>(P, R, D, F);843else if (Version == CovMapVersion::Version7)844return std::make_unique<VersionedCovMapFuncRecordReader<845CovMapVersion::Version7, IntPtrT, Endian>>(P, R, D, F);846}847llvm_unreachable("Unsupported version");848}849850template <typename T, llvm::endianness Endian>851static Error readCoverageMappingData(852InstrProfSymtab &ProfileNames, StringRef CovMap, StringRef FuncRecords,853std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records,854StringRef CompilationDir, std::vector<std::string> &Filenames) {855using namespace coverage;856857// Read the records in the coverage data section.858auto CovHeader =859reinterpret_cast<const CovMapHeader *>(CovMap.data());860CovMapVersion Version = (CovMapVersion)CovHeader->getVersion<Endian>();861if (Version > CovMapVersion::CurrentVersion)862return make_error<CoverageMapError>(coveragemap_error::unsupported_version);863Expected<std::unique_ptr<CovMapFuncRecordReader>> ReaderExpected =864CovMapFuncRecordReader::get<T, Endian>(Version, ProfileNames, Records,865CompilationDir, Filenames);866if (Error E = ReaderExpected.takeError())867return E;868auto Reader = std::move(ReaderExpected.get());869const char *CovBuf = CovMap.data();870const char *CovBufEnd = CovBuf + CovMap.size();871const char *FuncRecBuf = FuncRecords.data();872const char *FuncRecBufEnd = FuncRecords.data() + FuncRecords.size();873while (CovBuf < CovBufEnd) {874// Read the current coverage header & filename data.875//876// Prior to Version4, this also reads all function records affixed to the877// header.878//879// Return a pointer to the next coverage header.880auto NextOrErr = Reader->readCoverageHeader(CovBuf, CovBufEnd);881if (auto E = NextOrErr.takeError())882return E;883CovBuf = NextOrErr.get();884}885// In Version4, function records are not affixed to coverage headers. Read886// the records from their dedicated section.887if (Version >= CovMapVersion::Version4)888return Reader->readFunctionRecords(FuncRecBuf, FuncRecBufEnd, std::nullopt,889nullptr, nullptr);890return Error::success();891}892893Expected<std::unique_ptr<BinaryCoverageReader>>894BinaryCoverageReader::createCoverageReaderFromBuffer(895StringRef Coverage, FuncRecordsStorage &&FuncRecords,896std::unique_ptr<InstrProfSymtab> ProfileNamesPtr, uint8_t BytesInAddress,897llvm::endianness Endian, StringRef CompilationDir) {898if (ProfileNamesPtr == nullptr)899return make_error<CoverageMapError>(coveragemap_error::malformed,900"Caller must provide ProfileNames");901std::unique_ptr<BinaryCoverageReader> Reader(new BinaryCoverageReader(902std::move(ProfileNamesPtr), std::move(FuncRecords)));903InstrProfSymtab &ProfileNames = *Reader->ProfileNames;904StringRef FuncRecordsRef = Reader->FuncRecords->getBuffer();905if (BytesInAddress == 4 && Endian == llvm::endianness::little) {906if (Error E = readCoverageMappingData<uint32_t, llvm::endianness::little>(907ProfileNames, Coverage, FuncRecordsRef, Reader->MappingRecords,908CompilationDir, Reader->Filenames))909return std::move(E);910} else if (BytesInAddress == 4 && Endian == llvm::endianness::big) {911if (Error E = readCoverageMappingData<uint32_t, llvm::endianness::big>(912ProfileNames, Coverage, FuncRecordsRef, Reader->MappingRecords,913CompilationDir, Reader->Filenames))914return std::move(E);915} else if (BytesInAddress == 8 && Endian == llvm::endianness::little) {916if (Error E = readCoverageMappingData<uint64_t, llvm::endianness::little>(917ProfileNames, Coverage, FuncRecordsRef, Reader->MappingRecords,918CompilationDir, Reader->Filenames))919return std::move(E);920} else if (BytesInAddress == 8 && Endian == llvm::endianness::big) {921if (Error E = readCoverageMappingData<uint64_t, llvm::endianness::big>(922ProfileNames, Coverage, FuncRecordsRef, Reader->MappingRecords,923CompilationDir, Reader->Filenames))924return std::move(E);925} else926return make_error<CoverageMapError>(927coveragemap_error::malformed,928"not supported endianness or bytes in address");929return std::move(Reader);930}931932static Expected<std::unique_ptr<BinaryCoverageReader>>933loadTestingFormat(StringRef Data, StringRef CompilationDir) {934uint8_t BytesInAddress = 8;935llvm::endianness Endian = llvm::endianness::little;936937// Read the magic and version.938Data = Data.substr(sizeof(TestingFormatMagic));939if (Data.size() < sizeof(uint64_t))940return make_error<CoverageMapError>(coveragemap_error::malformed,941"the size of data is too small");942auto TestingVersion =943support::endian::byte_swap<uint64_t, llvm::endianness::little>(944*reinterpret_cast<const uint64_t *>(Data.data()));945Data = Data.substr(sizeof(uint64_t));946947// Read the ProfileNames data.948if (Data.empty())949return make_error<CoverageMapError>(coveragemap_error::truncated);950unsigned N = 0;951uint64_t ProfileNamesSize = decodeULEB128(Data.bytes_begin(), &N);952if (N > Data.size())953return make_error<CoverageMapError>(954coveragemap_error::malformed,955"the size of TestingFormatMagic is too big");956Data = Data.substr(N);957if (Data.empty())958return make_error<CoverageMapError>(coveragemap_error::truncated);959N = 0;960uint64_t Address = decodeULEB128(Data.bytes_begin(), &N);961if (N > Data.size())962return make_error<CoverageMapError>(coveragemap_error::malformed,963"the size of ULEB128 is too big");964Data = Data.substr(N);965if (Data.size() < ProfileNamesSize)966return make_error<CoverageMapError>(coveragemap_error::malformed,967"the size of ProfileNames is too big");968auto ProfileNames = std::make_unique<InstrProfSymtab>();969if (Error E = ProfileNames->create(Data.substr(0, ProfileNamesSize), Address))970return std::move(E);971Data = Data.substr(ProfileNamesSize);972973// In Version2, the size of CoverageMapping is stored directly.974uint64_t CoverageMappingSize;975if (TestingVersion == uint64_t(TestingFormatVersion::Version2)) {976N = 0;977CoverageMappingSize = decodeULEB128(Data.bytes_begin(), &N);978if (N > Data.size())979return make_error<CoverageMapError>(coveragemap_error::malformed,980"the size of ULEB128 is too big");981Data = Data.substr(N);982if (CoverageMappingSize < sizeof(CovMapHeader))983return make_error<CoverageMapError>(984coveragemap_error::malformed,985"the size of CoverageMapping is teoo small");986} else if (TestingVersion != uint64_t(TestingFormatVersion::Version1)) {987return make_error<CoverageMapError>(coveragemap_error::unsupported_version);988}989990// Skip the padding bytes because coverage map data has an alignment of 8.991auto Pad = offsetToAlignedAddr(Data.data(), Align(8));992if (Data.size() < Pad)993return make_error<CoverageMapError>(coveragemap_error::malformed,994"insufficient padding");995Data = Data.substr(Pad);996if (Data.size() < sizeof(CovMapHeader))997return make_error<CoverageMapError>(998coveragemap_error::malformed,999"coverage mapping header section is larger than data size");1000auto const *CovHeader = reinterpret_cast<const CovMapHeader *>(1001Data.substr(0, sizeof(CovMapHeader)).data());1002auto Version =1003CovMapVersion(CovHeader->getVersion<llvm::endianness::little>());10041005// In Version1, the size of CoverageMapping is calculated.1006if (TestingVersion == uint64_t(TestingFormatVersion::Version1)) {1007if (Version < CovMapVersion::Version4) {1008CoverageMappingSize = Data.size();1009} else {1010auto FilenamesSize =1011CovHeader->getFilenamesSize<llvm::endianness::little>();1012CoverageMappingSize = sizeof(CovMapHeader) + FilenamesSize;1013}1014}10151016auto CoverageMapping = Data.substr(0, CoverageMappingSize);1017Data = Data.substr(CoverageMappingSize);10181019// Read the CoverageRecords data.1020if (Version < CovMapVersion::Version4) {1021if (!Data.empty())1022return make_error<CoverageMapError>(coveragemap_error::malformed,1023"data is not empty");1024} else {1025// Skip the padding bytes because coverage records data has an alignment1026// of 8.1027Pad = offsetToAlignedAddr(Data.data(), Align(8));1028if (Data.size() < Pad)1029return make_error<CoverageMapError>(coveragemap_error::malformed,1030"insufficient padding");1031Data = Data.substr(Pad);1032}1033BinaryCoverageReader::FuncRecordsStorage CoverageRecords =1034MemoryBuffer::getMemBuffer(Data);10351036return BinaryCoverageReader::createCoverageReaderFromBuffer(1037CoverageMapping, std::move(CoverageRecords), std::move(ProfileNames),1038BytesInAddress, Endian, CompilationDir);1039}10401041/// Find all sections that match \p IPSK name. There may be more than one if1042/// comdats are in use, e.g. for the __llvm_covfun section on ELF.1043static Expected<std::vector<SectionRef>>1044lookupSections(ObjectFile &OF, InstrProfSectKind IPSK) {1045auto ObjFormat = OF.getTripleObjectFormat();1046auto Name =1047getInstrProfSectionName(IPSK, ObjFormat, /*AddSegmentInfo=*/false);1048// On COFF, the object file section name may end in "$M". This tells the1049// linker to sort these sections between "$A" and "$Z". The linker removes the1050// dollar and everything after it in the final binary. Do the same to match.1051bool IsCOFF = isa<COFFObjectFile>(OF);1052auto stripSuffix = [IsCOFF](StringRef N) {1053return IsCOFF ? N.split('$').first : N;1054};1055Name = stripSuffix(Name);10561057std::vector<SectionRef> Sections;1058for (const auto &Section : OF.sections()) {1059Expected<StringRef> NameOrErr = Section.getName();1060if (!NameOrErr)1061return NameOrErr.takeError();1062if (stripSuffix(*NameOrErr) == Name) {1063// COFF profile name section contains two null bytes indicating the1064// start/end of the section. If its size is 2 bytes, it's empty.1065if (IsCOFF && IPSK == IPSK_name && Section.getSize() == 2)1066continue;1067Sections.push_back(Section);1068}1069}1070if (Sections.empty())1071return make_error<CoverageMapError>(coveragemap_error::no_data_found);1072return Sections;1073}10741075static Expected<std::unique_ptr<BinaryCoverageReader>>1076loadBinaryFormat(std::unique_ptr<Binary> Bin, StringRef Arch,1077StringRef CompilationDir = "",1078object::BuildIDRef *BinaryID = nullptr) {1079std::unique_ptr<ObjectFile> OF;1080if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin.get())) {1081// If we have a universal binary, try to look up the object for the1082// appropriate architecture.1083auto ObjectFileOrErr = Universal->getMachOObjectForArch(Arch);1084if (!ObjectFileOrErr)1085return ObjectFileOrErr.takeError();1086OF = std::move(ObjectFileOrErr.get());1087} else if (isa<ObjectFile>(Bin.get())) {1088// For any other object file, upcast and take ownership.1089OF.reset(cast<ObjectFile>(Bin.release()));1090// If we've asked for a particular arch, make sure they match.1091if (!Arch.empty() && OF->getArch() != Triple(Arch).getArch())1092return errorCodeToError(object_error::arch_not_found);1093} else1094// We can only handle object files.1095return make_error<CoverageMapError>(coveragemap_error::malformed,1096"binary is not an object file");10971098// The coverage uses native pointer sizes for the object it's written in.1099uint8_t BytesInAddress = OF->getBytesInAddress();1100llvm::endianness Endian =1101OF->isLittleEndian() ? llvm::endianness::little : llvm::endianness::big;11021103// Look for the sections that we are interested in.1104auto ProfileNames = std::make_unique<InstrProfSymtab>();1105std::vector<SectionRef> NamesSectionRefs;1106// If IPSK_name is not found, fallback to search for IPK_covname, which is1107// used when binary correlation is enabled.1108auto NamesSection = lookupSections(*OF, IPSK_name);1109if (auto E = NamesSection.takeError()) {1110consumeError(std::move(E));1111NamesSection = lookupSections(*OF, IPSK_covname);1112if (auto E = NamesSection.takeError())1113return std::move(E);1114}1115NamesSectionRefs = *NamesSection;11161117if (NamesSectionRefs.size() != 1)1118return make_error<CoverageMapError>(1119coveragemap_error::malformed,1120"the size of coverage mapping section is not one");1121if (Error E = ProfileNames->create(NamesSectionRefs.back()))1122return std::move(E);11231124auto CoverageSection = lookupSections(*OF, IPSK_covmap);1125if (auto E = CoverageSection.takeError())1126return std::move(E);1127std::vector<SectionRef> CoverageSectionRefs = *CoverageSection;1128if (CoverageSectionRefs.size() != 1)1129return make_error<CoverageMapError>(coveragemap_error::malformed,1130"the size of name section is not one");1131auto CoverageMappingOrErr = CoverageSectionRefs.back().getContents();1132if (!CoverageMappingOrErr)1133return CoverageMappingOrErr.takeError();1134StringRef CoverageMapping = CoverageMappingOrErr.get();11351136// Look for the coverage records section (Version4 only).1137auto CoverageRecordsSections = lookupSections(*OF, IPSK_covfun);11381139BinaryCoverageReader::FuncRecordsStorage FuncRecords;1140if (auto E = CoverageRecordsSections.takeError()) {1141consumeError(std::move(E));1142FuncRecords = MemoryBuffer::getMemBuffer("");1143} else {1144// Compute the FuncRecordsBuffer of the buffer, taking into account the1145// padding between each record, and making sure the first block is aligned1146// in memory to maintain consistency between buffer address and size1147// alignment.1148const Align RecordAlignment(8);1149uint64_t FuncRecordsSize = 0;1150for (SectionRef Section : *CoverageRecordsSections) {1151auto CoverageRecordsOrErr = Section.getContents();1152if (!CoverageRecordsOrErr)1153return CoverageRecordsOrErr.takeError();1154FuncRecordsSize += alignTo(CoverageRecordsOrErr->size(), RecordAlignment);1155}1156auto WritableBuffer =1157WritableMemoryBuffer::getNewUninitMemBuffer(FuncRecordsSize);1158char *FuncRecordsBuffer = WritableBuffer->getBufferStart();1159assert(isAddrAligned(RecordAlignment, FuncRecordsBuffer) &&1160"Allocated memory is correctly aligned");11611162for (SectionRef Section : *CoverageRecordsSections) {1163auto CoverageRecordsOrErr = Section.getContents();1164if (!CoverageRecordsOrErr)1165return CoverageRecordsOrErr.takeError();1166const auto &CoverageRecords = CoverageRecordsOrErr.get();1167FuncRecordsBuffer = std::copy(CoverageRecords.begin(),1168CoverageRecords.end(), FuncRecordsBuffer);1169FuncRecordsBuffer =1170std::fill_n(FuncRecordsBuffer,1171alignAddr(FuncRecordsBuffer, RecordAlignment) -1172(uintptr_t)FuncRecordsBuffer,1173'\0');1174}1175assert(FuncRecordsBuffer == WritableBuffer->getBufferEnd() &&1176"consistent init");1177FuncRecords = std::move(WritableBuffer);1178}11791180if (BinaryID)1181*BinaryID = getBuildID(OF.get());11821183return BinaryCoverageReader::createCoverageReaderFromBuffer(1184CoverageMapping, std::move(FuncRecords), std::move(ProfileNames),1185BytesInAddress, Endian, CompilationDir);1186}11871188/// Determine whether \p Arch is invalid or empty, given \p Bin.1189static bool isArchSpecifierInvalidOrMissing(Binary *Bin, StringRef Arch) {1190// If we have a universal binary and Arch doesn't identify any of its slices,1191// it's user error.1192if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin)) {1193for (auto &ObjForArch : Universal->objects())1194if (Arch == ObjForArch.getArchFlagName())1195return false;1196return true;1197}1198return false;1199}12001201Expected<std::vector<std::unique_ptr<BinaryCoverageReader>>>1202BinaryCoverageReader::create(1203MemoryBufferRef ObjectBuffer, StringRef Arch,1204SmallVectorImpl<std::unique_ptr<MemoryBuffer>> &ObjectFileBuffers,1205StringRef CompilationDir, SmallVectorImpl<object::BuildIDRef> *BinaryIDs) {1206std::vector<std::unique_ptr<BinaryCoverageReader>> Readers;12071208if (ObjectBuffer.getBuffer().size() > sizeof(TestingFormatMagic)) {1209uint64_t Magic =1210support::endian::byte_swap<uint64_t, llvm::endianness::little>(1211*reinterpret_cast<const uint64_t *>(ObjectBuffer.getBufferStart()));1212if (Magic == TestingFormatMagic) {1213// This is a special format used for testing.1214auto ReaderOrErr =1215loadTestingFormat(ObjectBuffer.getBuffer(), CompilationDir);1216if (!ReaderOrErr)1217return ReaderOrErr.takeError();1218Readers.push_back(std::move(ReaderOrErr.get()));1219return std::move(Readers);1220}1221}12221223auto BinOrErr = createBinary(ObjectBuffer);1224if (!BinOrErr)1225return BinOrErr.takeError();1226std::unique_ptr<Binary> Bin = std::move(BinOrErr.get());12271228if (isArchSpecifierInvalidOrMissing(Bin.get(), Arch))1229return make_error<CoverageMapError>(1230coveragemap_error::invalid_or_missing_arch_specifier);12311232// MachO universal binaries which contain archives need to be treated as1233// archives, not as regular binaries.1234if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin.get())) {1235for (auto &ObjForArch : Universal->objects()) {1236// Skip slices within the universal binary which target the wrong arch.1237std::string ObjArch = ObjForArch.getArchFlagName();1238if (Arch != ObjArch)1239continue;12401241auto ArchiveOrErr = ObjForArch.getAsArchive();1242if (!ArchiveOrErr) {1243// If this is not an archive, try treating it as a regular object.1244consumeError(ArchiveOrErr.takeError());1245break;1246}12471248return BinaryCoverageReader::create(1249ArchiveOrErr.get()->getMemoryBufferRef(), Arch, ObjectFileBuffers,1250CompilationDir, BinaryIDs);1251}1252}12531254// Load coverage out of archive members.1255if (auto *Ar = dyn_cast<Archive>(Bin.get())) {1256Error Err = Error::success();1257for (auto &Child : Ar->children(Err)) {1258Expected<MemoryBufferRef> ChildBufOrErr = Child.getMemoryBufferRef();1259if (!ChildBufOrErr)1260return ChildBufOrErr.takeError();12611262auto ChildReadersOrErr = BinaryCoverageReader::create(1263ChildBufOrErr.get(), Arch, ObjectFileBuffers, CompilationDir,1264BinaryIDs);1265if (!ChildReadersOrErr)1266return ChildReadersOrErr.takeError();1267for (auto &Reader : ChildReadersOrErr.get())1268Readers.push_back(std::move(Reader));1269}1270if (Err)1271return std::move(Err);12721273// Thin archives reference object files outside of the archive file, i.e.1274// files which reside in memory not owned by the caller. Transfer ownership1275// to the caller.1276if (Ar->isThin())1277for (auto &Buffer : Ar->takeThinBuffers())1278ObjectFileBuffers.push_back(std::move(Buffer));12791280return std::move(Readers);1281}12821283object::BuildIDRef BinaryID;1284auto ReaderOrErr = loadBinaryFormat(std::move(Bin), Arch, CompilationDir,1285BinaryIDs ? &BinaryID : nullptr);1286if (!ReaderOrErr)1287return ReaderOrErr.takeError();1288Readers.push_back(std::move(ReaderOrErr.get()));1289if (!BinaryID.empty())1290BinaryIDs->push_back(BinaryID);1291return std::move(Readers);1292}12931294Error BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) {1295if (CurrentRecord >= MappingRecords.size())1296return make_error<CoverageMapError>(coveragemap_error::eof);12971298FunctionsFilenames.clear();1299Expressions.clear();1300MappingRegions.clear();1301auto &R = MappingRecords[CurrentRecord];1302auto F = ArrayRef(Filenames).slice(R.FilenamesBegin, R.FilenamesSize);1303RawCoverageMappingReader Reader(R.CoverageMapping, F, FunctionsFilenames,1304Expressions, MappingRegions);1305if (auto Err = Reader.read())1306return Err;13071308Record.FunctionName = R.FunctionName;1309Record.FunctionHash = R.FunctionHash;1310Record.Filenames = FunctionsFilenames;1311Record.Expressions = Expressions;1312Record.MappingRegions = MappingRegions;13131314++CurrentRecord;1315return Error::success();1316}131713181319