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/ADT/Triple.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 <vector> 39 40 using namespace llvm; 41 using namespace coverage; 42 using namespace object; 43 44 #define DEBUG_TYPE "coverage-mapping" 45 46 STATISTIC(CovMapNumRecords, "The # of coverage function records"); 47 STATISTIC(CovMapNumUsedRecords, "The # of used coverage function records"); 48 49 void CoverageMappingIterator::increment() { 50 if (ReadErr != coveragemap_error::success) 51 return; 52 53 // Check if all the records were read or if an error occurred while reading 54 // the next record. 55 if (auto E = Reader->readNextRecord(Record)) 56 handleAllErrors(std::move(E), [&](const CoverageMapError &CME) { 57 if (CME.get() == coveragemap_error::eof) 58 *this = CoverageMappingIterator(); 59 else 60 ReadErr = CME.get(); 61 }); 62 } 63 64 Error RawCoverageReader::readULEB128(uint64_t &Result) { 65 if (Data.empty()) 66 return make_error<CoverageMapError>(coveragemap_error::truncated); 67 unsigned N = 0; 68 Result = decodeULEB128(Data.bytes_begin(), &N); 69 if (N > Data.size()) 70 return make_error<CoverageMapError>(coveragemap_error::malformed); 71 Data = Data.substr(N); 72 return Error::success(); 73 } 74 75 Error RawCoverageReader::readIntMax(uint64_t &Result, uint64_t MaxPlus1) { 76 if (auto Err = readULEB128(Result)) 77 return Err; 78 if (Result >= MaxPlus1) 79 return make_error<CoverageMapError>(coveragemap_error::malformed); 80 return Error::success(); 81 } 82 83 Error RawCoverageReader::readSize(uint64_t &Result) { 84 if (auto Err = readULEB128(Result)) 85 return Err; 86 // Sanity check the number. 87 if (Result > Data.size()) 88 return make_error<CoverageMapError>(coveragemap_error::malformed); 89 return Error::success(); 90 } 91 92 Error RawCoverageReader::readString(StringRef &Result) { 93 uint64_t Length; 94 if (auto Err = readSize(Length)) 95 return Err; 96 Result = Data.substr(0, Length); 97 Data = Data.substr(Length); 98 return Error::success(); 99 } 100 101 Error RawCoverageFilenamesReader::read(CovMapVersion Version) { 102 uint64_t NumFilenames; 103 if (auto Err = readSize(NumFilenames)) 104 return Err; 105 if (!NumFilenames) 106 return make_error<CoverageMapError>(coveragemap_error::malformed); 107 108 if (Version < CovMapVersion::Version4) 109 return readUncompressed(Version, NumFilenames); 110 111 // The uncompressed length may exceed the size of the encoded filenames. 112 // Skip size validation. 113 uint64_t UncompressedLen; 114 if (auto Err = readULEB128(UncompressedLen)) 115 return Err; 116 117 uint64_t CompressedLen; 118 if (auto Err = readSize(CompressedLen)) 119 return Err; 120 121 if (CompressedLen > 0) { 122 if (!zlib::isAvailable()) 123 return make_error<CoverageMapError>( 124 coveragemap_error::decompression_failed); 125 126 // Allocate memory for the decompressed filenames. 127 SmallVector<char, 0> StorageBuf; 128 129 // Read compressed filenames. 130 StringRef CompressedFilenames = Data.substr(0, CompressedLen); 131 Data = Data.substr(CompressedLen); 132 auto Err = 133 zlib::uncompress(CompressedFilenames, StorageBuf, UncompressedLen); 134 if (Err) { 135 consumeError(std::move(Err)); 136 return make_error<CoverageMapError>( 137 coveragemap_error::decompression_failed); 138 } 139 140 StringRef UncompressedFilenames(StorageBuf.data(), StorageBuf.size()); 141 RawCoverageFilenamesReader Delegate(UncompressedFilenames, Filenames); 142 return Delegate.readUncompressed(Version, NumFilenames); 143 } 144 145 return readUncompressed(Version, NumFilenames); 146 } 147 148 Error RawCoverageFilenamesReader::readUncompressed(CovMapVersion Version, 149 uint64_t NumFilenames) { 150 // Read uncompressed filenames. 151 if (Version < CovMapVersion::Version6) { 152 for (size_t I = 0; I < NumFilenames; ++I) { 153 StringRef Filename; 154 if (auto Err = readString(Filename)) 155 return Err; 156 Filenames.push_back(Filename.str()); 157 } 158 } else { 159 StringRef CWD; 160 if (auto Err = readString(CWD)) 161 return Err; 162 Filenames.push_back(CWD.str()); 163 164 for (size_t I = 1; I < NumFilenames; ++I) { 165 StringRef Filename; 166 if (auto Err = readString(Filename)) 167 return Err; 168 if (sys::path::is_absolute(Filename)) { 169 Filenames.push_back(Filename.str()); 170 } else { 171 SmallString<256> P(CWD); 172 llvm::sys::path::append(P, Filename); 173 Filenames.push_back(static_cast<std::string>(P)); 174 } 175 } 176 } 177 return Error::success(); 178 } 179 180 Error RawCoverageMappingReader::decodeCounter(unsigned Value, Counter &C) { 181 auto Tag = Value & Counter::EncodingTagMask; 182 switch (Tag) { 183 case Counter::Zero: 184 C = Counter::getZero(); 185 return Error::success(); 186 case Counter::CounterValueReference: 187 C = Counter::getCounter(Value >> Counter::EncodingTagBits); 188 return Error::success(); 189 default: 190 break; 191 } 192 Tag -= Counter::Expression; 193 switch (Tag) { 194 case CounterExpression::Subtract: 195 case CounterExpression::Add: { 196 auto ID = Value >> Counter::EncodingTagBits; 197 if (ID >= Expressions.size()) 198 return make_error<CoverageMapError>(coveragemap_error::malformed); 199 Expressions[ID].Kind = CounterExpression::ExprKind(Tag); 200 C = Counter::getExpression(ID); 201 break; 202 } 203 default: 204 return make_error<CoverageMapError>(coveragemap_error::malformed); 205 } 206 return Error::success(); 207 } 208 209 Error RawCoverageMappingReader::readCounter(Counter &C) { 210 uint64_t EncodedCounter; 211 if (auto Err = 212 readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max())) 213 return Err; 214 if (auto Err = decodeCounter(EncodedCounter, C)) 215 return Err; 216 return Error::success(); 217 } 218 219 static const unsigned EncodingExpansionRegionBit = 1 220 << Counter::EncodingTagBits; 221 222 /// Read the sub-array of regions for the given inferred file id. 223 /// \param NumFileIDs the number of file ids that are defined for this 224 /// function. 225 Error RawCoverageMappingReader::readMappingRegionsSubArray( 226 std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID, 227 size_t NumFileIDs) { 228 uint64_t NumRegions; 229 if (auto Err = readSize(NumRegions)) 230 return Err; 231 unsigned LineStart = 0; 232 for (size_t I = 0; I < NumRegions; ++I) { 233 Counter C, C2; 234 CounterMappingRegion::RegionKind Kind = CounterMappingRegion::CodeRegion; 235 236 // Read the combined counter + region kind. 237 uint64_t EncodedCounterAndRegion; 238 if (auto Err = readIntMax(EncodedCounterAndRegion, 239 std::numeric_limits<unsigned>::max())) 240 return Err; 241 unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask; 242 uint64_t ExpandedFileID = 0; 243 244 // If Tag does not represent a ZeroCounter, then it is understood to refer 245 // to a counter or counter expression with region kind assumed to be 246 // "CodeRegion". In that case, EncodedCounterAndRegion actually encodes the 247 // referenced counter or counter expression (and nothing else). 248 // 249 // If Tag represents a ZeroCounter and EncodingExpansionRegionBit is set, 250 // then EncodedCounterAndRegion is interpreted to represent an 251 // ExpansionRegion. In all other cases, EncodedCounterAndRegion is 252 // interpreted to refer to a specific region kind, after which additional 253 // fields may be read (e.g. BranchRegions have two encoded counters that 254 // follow an encoded region kind value). 255 if (Tag != Counter::Zero) { 256 if (auto Err = decodeCounter(EncodedCounterAndRegion, C)) 257 return Err; 258 } else { 259 // Is it an expansion region? 260 if (EncodedCounterAndRegion & EncodingExpansionRegionBit) { 261 Kind = CounterMappingRegion::ExpansionRegion; 262 ExpandedFileID = EncodedCounterAndRegion >> 263 Counter::EncodingCounterTagAndExpansionRegionTagBits; 264 if (ExpandedFileID >= NumFileIDs) 265 return make_error<CoverageMapError>(coveragemap_error::malformed); 266 } else { 267 switch (EncodedCounterAndRegion >> 268 Counter::EncodingCounterTagAndExpansionRegionTagBits) { 269 case CounterMappingRegion::CodeRegion: 270 // Don't do anything when we have a code region with a zero counter. 271 break; 272 case CounterMappingRegion::SkippedRegion: 273 Kind = CounterMappingRegion::SkippedRegion; 274 break; 275 case CounterMappingRegion::BranchRegion: 276 // For a Branch Region, read two successive counters. 277 Kind = CounterMappingRegion::BranchRegion; 278 if (auto Err = readCounter(C)) 279 return Err; 280 if (auto Err = readCounter(C2)) 281 return Err; 282 break; 283 default: 284 return make_error<CoverageMapError>(coveragemap_error::malformed); 285 } 286 } 287 } 288 289 // Read the source range. 290 uint64_t LineStartDelta, ColumnStart, NumLines, ColumnEnd; 291 if (auto Err = 292 readIntMax(LineStartDelta, std::numeric_limits<unsigned>::max())) 293 return Err; 294 if (auto Err = readULEB128(ColumnStart)) 295 return Err; 296 if (ColumnStart > std::numeric_limits<unsigned>::max()) 297 return make_error<CoverageMapError>(coveragemap_error::malformed); 298 if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max())) 299 return Err; 300 if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max())) 301 return Err; 302 LineStart += LineStartDelta; 303 304 // If the high bit of ColumnEnd is set, this is a gap region. 305 if (ColumnEnd & (1U << 31)) { 306 Kind = CounterMappingRegion::GapRegion; 307 ColumnEnd &= ~(1U << 31); 308 } 309 310 // Adjust the column locations for the empty regions that are supposed to 311 // cover whole lines. Those regions should be encoded with the 312 // column range (1 -> std::numeric_limits<unsigned>::max()), but because 313 // the encoded std::numeric_limits<unsigned>::max() is several bytes long, 314 // we set the column range to (0 -> 0) to ensure that the column start and 315 // column end take up one byte each. 316 // The std::numeric_limits<unsigned>::max() is used to represent a column 317 // position at the end of the line without knowing the length of that line. 318 if (ColumnStart == 0 && ColumnEnd == 0) { 319 ColumnStart = 1; 320 ColumnEnd = std::numeric_limits<unsigned>::max(); 321 } 322 323 LLVM_DEBUG({ 324 dbgs() << "Counter in file " << InferredFileID << " " << LineStart << ":" 325 << ColumnStart << " -> " << (LineStart + NumLines) << ":" 326 << ColumnEnd << ", "; 327 if (Kind == CounterMappingRegion::ExpansionRegion) 328 dbgs() << "Expands to file " << ExpandedFileID; 329 else 330 CounterMappingContext(Expressions).dump(C, dbgs()); 331 dbgs() << "\n"; 332 }); 333 334 auto CMR = CounterMappingRegion(C, C2, InferredFileID, ExpandedFileID, 335 LineStart, ColumnStart, 336 LineStart + NumLines, ColumnEnd, Kind); 337 if (CMR.startLoc() > CMR.endLoc()) 338 return make_error<CoverageMapError>(coveragemap_error::malformed); 339 MappingRegions.push_back(CMR); 340 } 341 return Error::success(); 342 } 343 344 Error RawCoverageMappingReader::read() { 345 // Read the virtual file mapping. 346 SmallVector<unsigned, 8> VirtualFileMapping; 347 uint64_t NumFileMappings; 348 if (auto Err = readSize(NumFileMappings)) 349 return Err; 350 for (size_t I = 0; I < NumFileMappings; ++I) { 351 uint64_t FilenameIndex; 352 if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size())) 353 return Err; 354 VirtualFileMapping.push_back(FilenameIndex); 355 } 356 357 // Construct the files using unique filenames and virtual file mapping. 358 for (auto I : VirtualFileMapping) { 359 Filenames.push_back(TranslationUnitFilenames[I]); 360 } 361 362 // Read the expressions. 363 uint64_t NumExpressions; 364 if (auto Err = readSize(NumExpressions)) 365 return Err; 366 // Create an array of dummy expressions that get the proper counters 367 // when the expressions are read, and the proper kinds when the counters 368 // are decoded. 369 Expressions.resize( 370 NumExpressions, 371 CounterExpression(CounterExpression::Subtract, Counter(), Counter())); 372 for (size_t I = 0; I < NumExpressions; ++I) { 373 if (auto Err = readCounter(Expressions[I].LHS)) 374 return Err; 375 if (auto Err = readCounter(Expressions[I].RHS)) 376 return Err; 377 } 378 379 // Read the mapping regions sub-arrays. 380 for (unsigned InferredFileID = 0, S = VirtualFileMapping.size(); 381 InferredFileID < S; ++InferredFileID) { 382 if (auto Err = readMappingRegionsSubArray(MappingRegions, InferredFileID, 383 VirtualFileMapping.size())) 384 return Err; 385 } 386 387 // Set the counters for the expansion regions. 388 // i.e. Counter of expansion region = counter of the first region 389 // from the expanded file. 390 // Perform multiple passes to correctly propagate the counters through 391 // all the nested expansion regions. 392 SmallVector<CounterMappingRegion *, 8> FileIDExpansionRegionMapping; 393 FileIDExpansionRegionMapping.resize(VirtualFileMapping.size(), nullptr); 394 for (unsigned Pass = 1, S = VirtualFileMapping.size(); Pass < S; ++Pass) { 395 for (auto &R : MappingRegions) { 396 if (R.Kind != CounterMappingRegion::ExpansionRegion) 397 continue; 398 assert(!FileIDExpansionRegionMapping[R.ExpandedFileID]); 399 FileIDExpansionRegionMapping[R.ExpandedFileID] = &R; 400 } 401 for (auto &R : MappingRegions) { 402 if (FileIDExpansionRegionMapping[R.FileID]) { 403 FileIDExpansionRegionMapping[R.FileID]->Count = R.Count; 404 FileIDExpansionRegionMapping[R.FileID] = nullptr; 405 } 406 } 407 } 408 409 return Error::success(); 410 } 411 412 Expected<bool> RawCoverageMappingDummyChecker::isDummy() { 413 // A dummy coverage mapping data consists of just one region with zero count. 414 uint64_t NumFileMappings; 415 if (Error Err = readSize(NumFileMappings)) 416 return std::move(Err); 417 if (NumFileMappings != 1) 418 return false; 419 // We don't expect any specific value for the filename index, just skip it. 420 uint64_t FilenameIndex; 421 if (Error Err = 422 readIntMax(FilenameIndex, std::numeric_limits<unsigned>::max())) 423 return std::move(Err); 424 uint64_t NumExpressions; 425 if (Error Err = readSize(NumExpressions)) 426 return std::move(Err); 427 if (NumExpressions != 0) 428 return false; 429 uint64_t NumRegions; 430 if (Error Err = readSize(NumRegions)) 431 return std::move(Err); 432 if (NumRegions != 1) 433 return false; 434 uint64_t EncodedCounterAndRegion; 435 if (Error Err = readIntMax(EncodedCounterAndRegion, 436 std::numeric_limits<unsigned>::max())) 437 return std::move(Err); 438 unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask; 439 return Tag == Counter::Zero; 440 } 441 442 Error InstrProfSymtab::create(SectionRef &Section) { 443 Expected<StringRef> DataOrErr = Section.getContents(); 444 if (!DataOrErr) 445 return DataOrErr.takeError(); 446 Data = *DataOrErr; 447 Address = Section.getAddress(); 448 449 // If this is a linked PE/COFF file, then we have to skip over the null byte 450 // that is allocated in the .lprfn$A section in the LLVM profiling runtime. 451 const ObjectFile *Obj = Section.getObject(); 452 if (isa<COFFObjectFile>(Obj) && !Obj->isRelocatableObject()) 453 Data = Data.drop_front(1); 454 455 return Error::success(); 456 } 457 458 StringRef InstrProfSymtab::getFuncName(uint64_t Pointer, size_t Size) { 459 if (Pointer < Address) 460 return StringRef(); 461 auto Offset = Pointer - Address; 462 if (Offset + Size > Data.size()) 463 return StringRef(); 464 return Data.substr(Pointer - Address, Size); 465 } 466 467 // Check if the mapping data is a dummy, i.e. is emitted for an unused function. 468 static Expected<bool> isCoverageMappingDummy(uint64_t Hash, StringRef Mapping) { 469 // The hash value of dummy mapping records is always zero. 470 if (Hash) 471 return false; 472 return RawCoverageMappingDummyChecker(Mapping).isDummy(); 473 } 474 475 /// A range of filename indices. Used to specify the location of a batch of 476 /// filenames in a vector-like container. 477 struct FilenameRange { 478 unsigned StartingIndex; 479 unsigned Length; 480 481 FilenameRange(unsigned StartingIndex, unsigned Length) 482 : StartingIndex(StartingIndex), Length(Length) {} 483 484 void markInvalid() { Length = 0; } 485 bool isInvalid() const { return Length == 0; } 486 }; 487 488 namespace { 489 490 /// The interface to read coverage mapping function records for a module. 491 struct CovMapFuncRecordReader { 492 virtual ~CovMapFuncRecordReader() = default; 493 494 // Read a coverage header. 495 // 496 // \p CovBuf points to the buffer containing the \c CovHeader of the coverage 497 // mapping data associated with the module. 498 // 499 // Returns a pointer to the next \c CovHeader if it exists, or to an address 500 // greater than \p CovEnd if not. 501 virtual Expected<const char *> readCoverageHeader(const char *CovBuf, 502 const char *CovBufEnd) = 0; 503 504 // Read function records. 505 // 506 // \p FuncRecBuf points to the buffer containing a batch of function records. 507 // \p FuncRecBufEnd points past the end of the batch of records. 508 // 509 // Prior to Version4, \p OutOfLineFileRange points to a sequence of filenames 510 // associated with the function records. It is unused in Version4. 511 // 512 // Prior to Version4, \p OutOfLineMappingBuf points to a sequence of coverage 513 // mappings associated with the function records. It is unused in Version4. 514 virtual Error readFunctionRecords(const char *FuncRecBuf, 515 const char *FuncRecBufEnd, 516 Optional<FilenameRange> OutOfLineFileRange, 517 const char *OutOfLineMappingBuf, 518 const char *OutOfLineMappingBufEnd) = 0; 519 520 template <class IntPtrT, support::endianness Endian> 521 static Expected<std::unique_ptr<CovMapFuncRecordReader>> 522 get(CovMapVersion Version, InstrProfSymtab &P, 523 std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, 524 std::vector<std::string> &F); 525 }; 526 527 // A class for reading coverage mapping function records for a module. 528 template <CovMapVersion Version, class IntPtrT, support::endianness Endian> 529 class VersionedCovMapFuncRecordReader : public CovMapFuncRecordReader { 530 using FuncRecordType = 531 typename CovMapTraits<Version, IntPtrT>::CovMapFuncRecordType; 532 using NameRefType = typename CovMapTraits<Version, IntPtrT>::NameRefType; 533 534 // Maps function's name references to the indexes of their records 535 // in \c Records. 536 DenseMap<NameRefType, size_t> FunctionRecords; 537 InstrProfSymtab &ProfileNames; 538 std::vector<std::string> &Filenames; 539 std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records; 540 541 // Maps a hash of the filenames in a TU to a \c FileRange. The range 542 // specifies the location of the hashed filenames in \c Filenames. 543 DenseMap<uint64_t, FilenameRange> FileRangeMap; 544 545 // Add the record to the collection if we don't already have a record that 546 // points to the same function name. This is useful to ignore the redundant 547 // records for the functions with ODR linkage. 548 // In addition, prefer records with real coverage mapping data to dummy 549 // records, which were emitted for inline functions which were seen but 550 // not used in the corresponding translation unit. 551 Error insertFunctionRecordIfNeeded(const FuncRecordType *CFR, 552 StringRef Mapping, 553 FilenameRange FileRange) { 554 ++CovMapNumRecords; 555 uint64_t FuncHash = CFR->template getFuncHash<Endian>(); 556 NameRefType NameRef = CFR->template getFuncNameRef<Endian>(); 557 auto InsertResult = 558 FunctionRecords.insert(std::make_pair(NameRef, Records.size())); 559 if (InsertResult.second) { 560 StringRef FuncName; 561 if (Error Err = CFR->template getFuncName<Endian>(ProfileNames, FuncName)) 562 return Err; 563 if (FuncName.empty()) 564 return make_error<InstrProfError>(instrprof_error::malformed); 565 ++CovMapNumUsedRecords; 566 Records.emplace_back(Version, FuncName, FuncHash, Mapping, 567 FileRange.StartingIndex, FileRange.Length); 568 return Error::success(); 569 } 570 // Update the existing record if it's a dummy and the new record is real. 571 size_t OldRecordIndex = InsertResult.first->second; 572 BinaryCoverageReader::ProfileMappingRecord &OldRecord = 573 Records[OldRecordIndex]; 574 Expected<bool> OldIsDummyExpected = isCoverageMappingDummy( 575 OldRecord.FunctionHash, OldRecord.CoverageMapping); 576 if (Error Err = OldIsDummyExpected.takeError()) 577 return Err; 578 if (!*OldIsDummyExpected) 579 return Error::success(); 580 Expected<bool> NewIsDummyExpected = 581 isCoverageMappingDummy(FuncHash, Mapping); 582 if (Error Err = NewIsDummyExpected.takeError()) 583 return Err; 584 if (*NewIsDummyExpected) 585 return Error::success(); 586 ++CovMapNumUsedRecords; 587 OldRecord.FunctionHash = FuncHash; 588 OldRecord.CoverageMapping = Mapping; 589 OldRecord.FilenamesBegin = FileRange.StartingIndex; 590 OldRecord.FilenamesSize = FileRange.Length; 591 return Error::success(); 592 } 593 594 public: 595 VersionedCovMapFuncRecordReader( 596 InstrProfSymtab &P, 597 std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, 598 std::vector<std::string> &F) 599 : ProfileNames(P), Filenames(F), Records(R) {} 600 601 ~VersionedCovMapFuncRecordReader() override = default; 602 603 Expected<const char *> readCoverageHeader(const char *CovBuf, 604 const char *CovBufEnd) override { 605 using namespace support; 606 607 if (CovBuf + sizeof(CovMapHeader) > CovBufEnd) 608 return make_error<CoverageMapError>(coveragemap_error::malformed); 609 auto CovHeader = reinterpret_cast<const CovMapHeader *>(CovBuf); 610 uint32_t NRecords = CovHeader->getNRecords<Endian>(); 611 uint32_t FilenamesSize = CovHeader->getFilenamesSize<Endian>(); 612 uint32_t CoverageSize = CovHeader->getCoverageSize<Endian>(); 613 assert((CovMapVersion)CovHeader->getVersion<Endian>() == Version); 614 CovBuf = reinterpret_cast<const char *>(CovHeader + 1); 615 616 // Skip past the function records, saving the start and end for later. 617 // This is a no-op in Version4 (function records are read after all headers 618 // are read). 619 const char *FuncRecBuf = nullptr; 620 const char *FuncRecBufEnd = nullptr; 621 if (Version < CovMapVersion::Version4) 622 FuncRecBuf = CovBuf; 623 CovBuf += NRecords * sizeof(FuncRecordType); 624 if (Version < CovMapVersion::Version4) 625 FuncRecBufEnd = CovBuf; 626 627 // Get the filenames. 628 if (CovBuf + FilenamesSize > CovBufEnd) 629 return make_error<CoverageMapError>(coveragemap_error::malformed); 630 size_t FilenamesBegin = Filenames.size(); 631 StringRef FilenameRegion(CovBuf, FilenamesSize); 632 RawCoverageFilenamesReader Reader(FilenameRegion, Filenames); 633 if (auto Err = Reader.read(Version)) 634 return std::move(Err); 635 CovBuf += FilenamesSize; 636 FilenameRange FileRange(FilenamesBegin, Filenames.size() - FilenamesBegin); 637 638 if (Version >= CovMapVersion::Version4) { 639 // Map a hash of the filenames region to the filename range associated 640 // with this coverage header. 641 int64_t FilenamesRef = 642 llvm::IndexedInstrProf::ComputeHash(FilenameRegion); 643 auto Insert = 644 FileRangeMap.insert(std::make_pair(FilenamesRef, FileRange)); 645 if (!Insert.second) { 646 // The same filenames ref was encountered twice. It's possible that 647 // the associated filenames are the same. 648 auto It = Filenames.begin(); 649 FilenameRange &OrigRange = Insert.first->getSecond(); 650 if (std::equal(It + OrigRange.StartingIndex, 651 It + OrigRange.StartingIndex + OrigRange.Length, 652 It + FileRange.StartingIndex, 653 It + FileRange.StartingIndex + FileRange.Length)) 654 // Map the new range to the original one. 655 FileRange = OrigRange; 656 else 657 // This is a hash collision. Mark the filenames ref invalid. 658 OrigRange.markInvalid(); 659 } 660 } 661 662 // We'll read the coverage mapping records in the loop below. 663 // This is a no-op in Version4 (coverage mappings are not affixed to the 664 // coverage header). 665 const char *MappingBuf = CovBuf; 666 if (Version >= CovMapVersion::Version4 && CoverageSize != 0) 667 return make_error<CoverageMapError>(coveragemap_error::malformed); 668 CovBuf += CoverageSize; 669 const char *MappingEnd = CovBuf; 670 671 if (CovBuf > CovBufEnd) 672 return make_error<CoverageMapError>(coveragemap_error::malformed); 673 674 if (Version < CovMapVersion::Version4) { 675 // Read each function record. 676 if (Error E = readFunctionRecords(FuncRecBuf, FuncRecBufEnd, FileRange, 677 MappingBuf, MappingEnd)) 678 return std::move(E); 679 } 680 681 // Each coverage map has an alignment of 8, so we need to adjust alignment 682 // before reading the next map. 683 CovBuf += offsetToAlignedAddr(CovBuf, Align(8)); 684 685 return CovBuf; 686 } 687 688 Error readFunctionRecords(const char *FuncRecBuf, const char *FuncRecBufEnd, 689 Optional<FilenameRange> OutOfLineFileRange, 690 const char *OutOfLineMappingBuf, 691 const char *OutOfLineMappingBufEnd) override { 692 auto CFR = reinterpret_cast<const FuncRecordType *>(FuncRecBuf); 693 while ((const char *)CFR < FuncRecBufEnd) { 694 // Validate the length of the coverage mapping for this function. 695 const char *NextMappingBuf; 696 const FuncRecordType *NextCFR; 697 std::tie(NextMappingBuf, NextCFR) = 698 CFR->template advanceByOne<Endian>(OutOfLineMappingBuf); 699 if (Version < CovMapVersion::Version4) 700 if (NextMappingBuf > OutOfLineMappingBufEnd) 701 return make_error<CoverageMapError>(coveragemap_error::malformed); 702 703 // Look up the set of filenames associated with this function record. 704 Optional<FilenameRange> FileRange; 705 if (Version < CovMapVersion::Version4) { 706 FileRange = OutOfLineFileRange; 707 } else { 708 uint64_t FilenamesRef = CFR->template getFilenamesRef<Endian>(); 709 auto It = FileRangeMap.find(FilenamesRef); 710 if (It == FileRangeMap.end()) 711 return make_error<CoverageMapError>(coveragemap_error::malformed); 712 else 713 FileRange = It->getSecond(); 714 } 715 716 // Now, read the coverage data. 717 if (FileRange && !FileRange->isInvalid()) { 718 StringRef Mapping = 719 CFR->template getCoverageMapping<Endian>(OutOfLineMappingBuf); 720 if (Version >= CovMapVersion::Version4 && 721 Mapping.data() + Mapping.size() > FuncRecBufEnd) 722 return make_error<CoverageMapError>(coveragemap_error::malformed); 723 if (Error Err = insertFunctionRecordIfNeeded(CFR, Mapping, *FileRange)) 724 return Err; 725 } 726 727 std::tie(OutOfLineMappingBuf, CFR) = std::tie(NextMappingBuf, NextCFR); 728 } 729 return Error::success(); 730 } 731 }; 732 733 } // end anonymous namespace 734 735 template <class IntPtrT, support::endianness Endian> 736 Expected<std::unique_ptr<CovMapFuncRecordReader>> CovMapFuncRecordReader::get( 737 CovMapVersion Version, InstrProfSymtab &P, 738 std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, 739 std::vector<std::string> &F) { 740 using namespace coverage; 741 742 switch (Version) { 743 case CovMapVersion::Version1: 744 return std::make_unique<VersionedCovMapFuncRecordReader< 745 CovMapVersion::Version1, IntPtrT, Endian>>(P, R, F); 746 case CovMapVersion::Version2: 747 case CovMapVersion::Version3: 748 case CovMapVersion::Version4: 749 case CovMapVersion::Version5: 750 case CovMapVersion::Version6: 751 // Decompress the name data. 752 if (Error E = P.create(P.getNameData())) 753 return std::move(E); 754 if (Version == CovMapVersion::Version2) 755 return std::make_unique<VersionedCovMapFuncRecordReader< 756 CovMapVersion::Version2, IntPtrT, Endian>>(P, R, F); 757 else if (Version == CovMapVersion::Version3) 758 return std::make_unique<VersionedCovMapFuncRecordReader< 759 CovMapVersion::Version3, IntPtrT, Endian>>(P, R, F); 760 else if (Version == CovMapVersion::Version4) 761 return std::make_unique<VersionedCovMapFuncRecordReader< 762 CovMapVersion::Version4, IntPtrT, Endian>>(P, R, F); 763 else if (Version == CovMapVersion::Version5) 764 return std::make_unique<VersionedCovMapFuncRecordReader< 765 CovMapVersion::Version5, IntPtrT, Endian>>(P, R, F); 766 else if (Version == CovMapVersion::Version6) 767 return std::make_unique<VersionedCovMapFuncRecordReader< 768 CovMapVersion::Version6, IntPtrT, Endian>>(P, R, F); 769 } 770 llvm_unreachable("Unsupported version"); 771 } 772 773 template <typename T, support::endianness Endian> 774 static Error readCoverageMappingData( 775 InstrProfSymtab &ProfileNames, StringRef CovMap, StringRef FuncRecords, 776 std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records, 777 std::vector<std::string> &Filenames) { 778 using namespace coverage; 779 780 // Read the records in the coverage data section. 781 auto CovHeader = 782 reinterpret_cast<const CovMapHeader *>(CovMap.data()); 783 CovMapVersion Version = (CovMapVersion)CovHeader->getVersion<Endian>(); 784 if (Version > CovMapVersion::CurrentVersion) 785 return make_error<CoverageMapError>(coveragemap_error::unsupported_version); 786 Expected<std::unique_ptr<CovMapFuncRecordReader>> ReaderExpected = 787 CovMapFuncRecordReader::get<T, Endian>(Version, ProfileNames, Records, 788 Filenames); 789 if (Error E = ReaderExpected.takeError()) 790 return E; 791 auto Reader = std::move(ReaderExpected.get()); 792 const char *CovBuf = CovMap.data(); 793 const char *CovBufEnd = CovBuf + CovMap.size(); 794 const char *FuncRecBuf = FuncRecords.data(); 795 const char *FuncRecBufEnd = FuncRecords.data() + FuncRecords.size(); 796 while (CovBuf < CovBufEnd) { 797 // Read the current coverage header & filename data. 798 // 799 // Prior to Version4, this also reads all function records affixed to the 800 // header. 801 // 802 // Return a pointer to the next coverage header. 803 auto NextOrErr = Reader->readCoverageHeader(CovBuf, CovBufEnd); 804 if (auto E = NextOrErr.takeError()) 805 return E; 806 CovBuf = NextOrErr.get(); 807 } 808 // In Version4, function records are not affixed to coverage headers. Read 809 // the records from their dedicated section. 810 if (Version >= CovMapVersion::Version4) 811 return Reader->readFunctionRecords(FuncRecBuf, FuncRecBufEnd, None, nullptr, 812 nullptr); 813 return Error::success(); 814 } 815 816 static const char *TestingFormatMagic = "llvmcovmtestdata"; 817 818 Expected<std::unique_ptr<BinaryCoverageReader>> 819 BinaryCoverageReader::createCoverageReaderFromBuffer( 820 StringRef Coverage, std::string &&FuncRecords, InstrProfSymtab &&ProfileNames, 821 uint8_t BytesInAddress, support::endianness Endian) { 822 std::unique_ptr<BinaryCoverageReader> Reader( 823 new BinaryCoverageReader(std::move(FuncRecords))); 824 Reader->ProfileNames = std::move(ProfileNames); 825 StringRef FuncRecordsRef = Reader->FuncRecords; 826 if (BytesInAddress == 4 && Endian == support::endianness::little) { 827 if (Error E = 828 readCoverageMappingData<uint32_t, support::endianness::little>( 829 Reader->ProfileNames, Coverage, FuncRecordsRef, 830 Reader->MappingRecords, Reader->Filenames)) 831 return std::move(E); 832 } else if (BytesInAddress == 4 && Endian == support::endianness::big) { 833 if (Error E = readCoverageMappingData<uint32_t, support::endianness::big>( 834 Reader->ProfileNames, Coverage, FuncRecordsRef, 835 Reader->MappingRecords, Reader->Filenames)) 836 return std::move(E); 837 } else if (BytesInAddress == 8 && Endian == support::endianness::little) { 838 if (Error E = 839 readCoverageMappingData<uint64_t, support::endianness::little>( 840 Reader->ProfileNames, Coverage, FuncRecordsRef, 841 Reader->MappingRecords, Reader->Filenames)) 842 return std::move(E); 843 } else if (BytesInAddress == 8 && Endian == support::endianness::big) { 844 if (Error E = readCoverageMappingData<uint64_t, support::endianness::big>( 845 Reader->ProfileNames, Coverage, FuncRecordsRef, 846 Reader->MappingRecords, Reader->Filenames)) 847 return std::move(E); 848 } else 849 return make_error<CoverageMapError>(coveragemap_error::malformed); 850 return std::move(Reader); 851 } 852 853 static Expected<std::unique_ptr<BinaryCoverageReader>> 854 loadTestingFormat(StringRef Data) { 855 uint8_t BytesInAddress = 8; 856 support::endianness Endian = support::endianness::little; 857 858 Data = Data.substr(StringRef(TestingFormatMagic).size()); 859 if (Data.empty()) 860 return make_error<CoverageMapError>(coveragemap_error::truncated); 861 unsigned N = 0; 862 uint64_t ProfileNamesSize = decodeULEB128(Data.bytes_begin(), &N); 863 if (N > Data.size()) 864 return make_error<CoverageMapError>(coveragemap_error::malformed); 865 Data = Data.substr(N); 866 if (Data.empty()) 867 return make_error<CoverageMapError>(coveragemap_error::truncated); 868 N = 0; 869 uint64_t Address = decodeULEB128(Data.bytes_begin(), &N); 870 if (N > Data.size()) 871 return make_error<CoverageMapError>(coveragemap_error::malformed); 872 Data = Data.substr(N); 873 if (Data.size() < ProfileNamesSize) 874 return make_error<CoverageMapError>(coveragemap_error::malformed); 875 InstrProfSymtab ProfileNames; 876 if (Error E = ProfileNames.create(Data.substr(0, ProfileNamesSize), Address)) 877 return std::move(E); 878 StringRef CoverageMapping = Data.substr(ProfileNamesSize); 879 // Skip the padding bytes because coverage map data has an alignment of 8. 880 if (CoverageMapping.empty()) 881 return make_error<CoverageMapError>(coveragemap_error::truncated); 882 size_t Pad = offsetToAlignedAddr(CoverageMapping.data(), Align(8)); 883 if (CoverageMapping.size() < Pad) 884 return make_error<CoverageMapError>(coveragemap_error::malformed); 885 CoverageMapping = CoverageMapping.substr(Pad); 886 return BinaryCoverageReader::createCoverageReaderFromBuffer( 887 CoverageMapping, "", std::move(ProfileNames), BytesInAddress, Endian); 888 } 889 890 /// Find all sections that match \p Name. There may be more than one if comdats 891 /// are in use, e.g. for the __llvm_covfun section on ELF. 892 static Expected<std::vector<SectionRef>> lookupSections(ObjectFile &OF, 893 StringRef Name) { 894 // On COFF, the object file section name may end in "$M". This tells the 895 // linker to sort these sections between "$A" and "$Z". The linker removes the 896 // dollar and everything after it in the final binary. Do the same to match. 897 bool IsCOFF = isa<COFFObjectFile>(OF); 898 auto stripSuffix = [IsCOFF](StringRef N) { 899 return IsCOFF ? N.split('$').first : N; 900 }; 901 Name = stripSuffix(Name); 902 903 std::vector<SectionRef> Sections; 904 for (const auto &Section : OF.sections()) { 905 Expected<StringRef> NameOrErr = Section.getName(); 906 if (!NameOrErr) 907 return NameOrErr.takeError(); 908 if (stripSuffix(*NameOrErr) == Name) 909 Sections.push_back(Section); 910 } 911 if (Sections.empty()) 912 return make_error<CoverageMapError>(coveragemap_error::no_data_found); 913 return Sections; 914 } 915 916 static Expected<std::unique_ptr<BinaryCoverageReader>> 917 loadBinaryFormat(std::unique_ptr<Binary> Bin, StringRef Arch) { 918 std::unique_ptr<ObjectFile> OF; 919 if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin.get())) { 920 // If we have a universal binary, try to look up the object for the 921 // appropriate architecture. 922 auto ObjectFileOrErr = Universal->getMachOObjectForArch(Arch); 923 if (!ObjectFileOrErr) 924 return ObjectFileOrErr.takeError(); 925 OF = std::move(ObjectFileOrErr.get()); 926 } else if (isa<ObjectFile>(Bin.get())) { 927 // For any other object file, upcast and take ownership. 928 OF.reset(cast<ObjectFile>(Bin.release())); 929 // If we've asked for a particular arch, make sure they match. 930 if (!Arch.empty() && OF->getArch() != Triple(Arch).getArch()) 931 return errorCodeToError(object_error::arch_not_found); 932 } else 933 // We can only handle object files. 934 return make_error<CoverageMapError>(coveragemap_error::malformed); 935 936 // The coverage uses native pointer sizes for the object it's written in. 937 uint8_t BytesInAddress = OF->getBytesInAddress(); 938 support::endianness Endian = OF->isLittleEndian() 939 ? support::endianness::little 940 : support::endianness::big; 941 942 // Look for the sections that we are interested in. 943 auto ObjFormat = OF->getTripleObjectFormat(); 944 auto NamesSection = 945 lookupSections(*OF, getInstrProfSectionName(IPSK_name, ObjFormat, 946 /*AddSegmentInfo=*/false)); 947 if (auto E = NamesSection.takeError()) 948 return std::move(E); 949 auto CoverageSection = 950 lookupSections(*OF, getInstrProfSectionName(IPSK_covmap, ObjFormat, 951 /*AddSegmentInfo=*/false)); 952 if (auto E = CoverageSection.takeError()) 953 return std::move(E); 954 std::vector<SectionRef> CoverageSectionRefs = *CoverageSection; 955 if (CoverageSectionRefs.size() != 1) 956 return make_error<CoverageMapError>(coveragemap_error::malformed); 957 auto CoverageMappingOrErr = CoverageSectionRefs.back().getContents(); 958 if (!CoverageMappingOrErr) 959 return CoverageMappingOrErr.takeError(); 960 StringRef CoverageMapping = CoverageMappingOrErr.get(); 961 962 InstrProfSymtab ProfileNames; 963 std::vector<SectionRef> NamesSectionRefs = *NamesSection; 964 if (NamesSectionRefs.size() != 1) 965 return make_error<CoverageMapError>(coveragemap_error::malformed); 966 if (Error E = ProfileNames.create(NamesSectionRefs.back())) 967 return std::move(E); 968 969 // Look for the coverage records section (Version4 only). 970 std::string FuncRecords; 971 auto CoverageRecordsSections = 972 lookupSections(*OF, getInstrProfSectionName(IPSK_covfun, ObjFormat, 973 /*AddSegmentInfo=*/false)); 974 if (auto E = CoverageRecordsSections.takeError()) 975 consumeError(std::move(E)); 976 else { 977 for (SectionRef Section : *CoverageRecordsSections) { 978 auto CoverageRecordsOrErr = Section.getContents(); 979 if (!CoverageRecordsOrErr) 980 return CoverageRecordsOrErr.takeError(); 981 FuncRecords += CoverageRecordsOrErr.get(); 982 while (FuncRecords.size() % 8 != 0) 983 FuncRecords += '\0'; 984 } 985 } 986 987 return BinaryCoverageReader::createCoverageReaderFromBuffer( 988 CoverageMapping, std::move(FuncRecords), std::move(ProfileNames), 989 BytesInAddress, Endian); 990 } 991 992 /// Determine whether \p Arch is invalid or empty, given \p Bin. 993 static bool isArchSpecifierInvalidOrMissing(Binary *Bin, StringRef Arch) { 994 // If we have a universal binary and Arch doesn't identify any of its slices, 995 // it's user error. 996 if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin)) { 997 for (auto &ObjForArch : Universal->objects()) 998 if (Arch == ObjForArch.getArchFlagName()) 999 return false; 1000 return true; 1001 } 1002 return false; 1003 } 1004 1005 Expected<std::vector<std::unique_ptr<BinaryCoverageReader>>> 1006 BinaryCoverageReader::create( 1007 MemoryBufferRef ObjectBuffer, StringRef Arch, 1008 SmallVectorImpl<std::unique_ptr<MemoryBuffer>> &ObjectFileBuffers) { 1009 std::vector<std::unique_ptr<BinaryCoverageReader>> Readers; 1010 1011 if (ObjectBuffer.getBuffer().startswith(TestingFormatMagic)) { 1012 // This is a special format used for testing. 1013 auto ReaderOrErr = loadTestingFormat(ObjectBuffer.getBuffer()); 1014 if (!ReaderOrErr) 1015 return ReaderOrErr.takeError(); 1016 Readers.push_back(std::move(ReaderOrErr.get())); 1017 return std::move(Readers); 1018 } 1019 1020 auto BinOrErr = createBinary(ObjectBuffer); 1021 if (!BinOrErr) 1022 return BinOrErr.takeError(); 1023 std::unique_ptr<Binary> Bin = std::move(BinOrErr.get()); 1024 1025 if (isArchSpecifierInvalidOrMissing(Bin.get(), Arch)) 1026 return make_error<CoverageMapError>( 1027 coveragemap_error::invalid_or_missing_arch_specifier); 1028 1029 // MachO universal binaries which contain archives need to be treated as 1030 // archives, not as regular binaries. 1031 if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin.get())) { 1032 for (auto &ObjForArch : Universal->objects()) { 1033 // Skip slices within the universal binary which target the wrong arch. 1034 std::string ObjArch = ObjForArch.getArchFlagName(); 1035 if (Arch != ObjArch) 1036 continue; 1037 1038 auto ArchiveOrErr = ObjForArch.getAsArchive(); 1039 if (!ArchiveOrErr) { 1040 // If this is not an archive, try treating it as a regular object. 1041 consumeError(ArchiveOrErr.takeError()); 1042 break; 1043 } 1044 1045 return BinaryCoverageReader::create( 1046 ArchiveOrErr.get()->getMemoryBufferRef(), Arch, ObjectFileBuffers); 1047 } 1048 } 1049 1050 // Load coverage out of archive members. 1051 if (auto *Ar = dyn_cast<Archive>(Bin.get())) { 1052 Error Err = Error::success(); 1053 for (auto &Child : Ar->children(Err)) { 1054 Expected<MemoryBufferRef> ChildBufOrErr = Child.getMemoryBufferRef(); 1055 if (!ChildBufOrErr) 1056 return ChildBufOrErr.takeError(); 1057 1058 auto ChildReadersOrErr = BinaryCoverageReader::create( 1059 ChildBufOrErr.get(), Arch, ObjectFileBuffers); 1060 if (!ChildReadersOrErr) 1061 return ChildReadersOrErr.takeError(); 1062 for (auto &Reader : ChildReadersOrErr.get()) 1063 Readers.push_back(std::move(Reader)); 1064 } 1065 if (Err) 1066 return std::move(Err); 1067 1068 // Thin archives reference object files outside of the archive file, i.e. 1069 // files which reside in memory not owned by the caller. Transfer ownership 1070 // to the caller. 1071 if (Ar->isThin()) 1072 for (auto &Buffer : Ar->takeThinBuffers()) 1073 ObjectFileBuffers.push_back(std::move(Buffer)); 1074 1075 return std::move(Readers); 1076 } 1077 1078 auto ReaderOrErr = loadBinaryFormat(std::move(Bin), Arch); 1079 if (!ReaderOrErr) 1080 return ReaderOrErr.takeError(); 1081 Readers.push_back(std::move(ReaderOrErr.get())); 1082 return std::move(Readers); 1083 } 1084 1085 Error BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) { 1086 if (CurrentRecord >= MappingRecords.size()) 1087 return make_error<CoverageMapError>(coveragemap_error::eof); 1088 1089 FunctionsFilenames.clear(); 1090 Expressions.clear(); 1091 MappingRegions.clear(); 1092 auto &R = MappingRecords[CurrentRecord]; 1093 auto F = makeArrayRef(Filenames).slice(R.FilenamesBegin, R.FilenamesSize); 1094 RawCoverageMappingReader Reader(R.CoverageMapping, F, FunctionsFilenames, 1095 Expressions, MappingRegions); 1096 if (auto Err = Reader.read()) 1097 return Err; 1098 1099 Record.FunctionName = R.FunctionName; 1100 Record.FunctionHash = R.FunctionHash; 1101 Record.Filenames = FunctionsFilenames; 1102 Record.Expressions = Expressions; 1103 Record.MappingRegions = MappingRegions; 1104 1105 ++CurrentRecord; 1106 return Error::success(); 1107 } 1108