LLVM 23.0.0git
CoverageMappingReader.cpp
Go to the documentation of this file.
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
15#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/Statistic.h"
19#include "llvm/ADT/StringRef.h"
21#include "llvm/Object/Archive.h"
22#include "llvm/Object/Binary.h"
23#include "llvm/Object/COFF.h"
24#include "llvm/Object/Error.h"
27#include "llvm/Object/Wasm.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/Endian.h"
33#include "llvm/Support/Error.h"
35#include "llvm/Support/LEB128.h"
36#include "llvm/Support/Path.h"
39#include <vector>
40
41using namespace llvm;
42using namespace coverage;
43using namespace object;
44
45#define DEBUG_TYPE "coverage-mapping"
46
47STATISTIC(CovMapNumRecords, "The # of coverage function records");
48STATISTIC(CovMapNumUsedRecords, "The # of used coverage function records");
49
50void CoverageMappingIterator::increment() {
51 if (ReadErr != coveragemap_error::success)
52 return;
53
54 // Check if all the records were read or if an error occurred while reading
55 // the next record.
56 if (auto E = Reader->readNextRecord(Record))
57 handleAllErrors(std::move(E), [&](const CoverageMapError &CME) {
58 if (CME.get() == coveragemap_error::eof)
60 else
61 ReadErr = CME.get();
62 });
63}
64
66 if (Data.empty())
68 unsigned N = 0;
69 Result = decodeULEB128(Data.bytes_begin(), &N);
70 if (N > Data.size())
72 "the size of ULEB128 is too big");
73 Data = Data.substr(N);
74 return Error::success();
75}
76
78 if (auto Err = readULEB128(Result))
79 return Err;
80 if (Result >= MaxPlus1)
83 "the value of ULEB128 is greater than or equal to MaxPlus1");
84 return Error::success();
85}
86
88 if (auto Err = readULEB128(Result))
89 return Err;
90 if (Result > Data.size())
92 "the value of ULEB128 is too big");
93 return Error::success();
94}
95
98 if (auto Err = readSize(Length))
99 return Err;
100 Result = Data.substr(0, Length);
101 Data = Data.substr(Length);
102 return Error::success();
103}
104
106 uint64_t NumFilenames;
107 if (auto Err = readSize(NumFilenames))
108 return Err;
109 if (!NumFilenames)
111 "number of filenames is zero");
112
114 return readUncompressed(Version, NumFilenames);
115
116 // The uncompressed length may exceed the size of the encoded filenames.
117 // Skip size validation.
118 uint64_t UncompressedLen;
119 if (auto Err = readULEB128(UncompressedLen))
120 return Err;
121
122 uint64_t CompressedLen;
123 if (auto Err = readSize(CompressedLen))
124 return Err;
125
126 if (CompressedLen > 0) {
130
131 // Allocate memory for the decompressed filenames.
132 SmallVector<uint8_t, 0> StorageBuf;
133
134 // Read compressed filenames.
135 StringRef CompressedFilenames = Data.substr(0, CompressedLen);
136 Data = Data.substr(CompressedLen);
138 arrayRefFromStringRef(CompressedFilenames), StorageBuf,
139 UncompressedLen);
140 if (Err) {
141 consumeError(std::move(Err));
144 }
145
146 RawCoverageFilenamesReader Delegate(toStringRef(StorageBuf), Filenames,
147 CompilationDir);
148 return Delegate.readUncompressed(Version, NumFilenames);
149 }
150
151 return readUncompressed(Version, NumFilenames);
152}
153
154Error RawCoverageFilenamesReader::readUncompressed(CovMapVersion Version,
155 uint64_t NumFilenames) {
156 // Read uncompressed filenames.
158 for (size_t I = 0; I < NumFilenames; ++I) {
160 if (auto Err = readString(Filename))
161 return Err;
162 Filenames.push_back(Filename.str());
163 }
164 } else {
165 StringRef CWD;
166 if (auto Err = readString(CWD))
167 return Err;
168 Filenames.push_back(CWD.str());
169
170 for (size_t I = 1; I < NumFilenames; ++I) {
172 if (auto Err = readString(Filename))
173 return Err;
175 Filenames.push_back(Filename.str());
176 } else {
177 SmallString<256> P;
178 if (!CompilationDir.empty())
179 P.assign(CompilationDir);
180 else
181 P.assign(CWD);
183 sys::path::remove_dots(P, /*remove_dot_dot=*/true);
184 Filenames.push_back(static_cast<std::string>(P.str()));
185 }
186 }
187 }
188 return Error::success();
189}
190
191Error RawCoverageMappingReader::decodeCounter(unsigned Value, Counter &C) {
193 switch (Tag) {
194 case Counter::Zero:
196 return Error::success();
199 return Error::success();
200 default:
201 break;
202 }
204 switch (Tag) {
208 if (ID >= Expressions.size())
210 "counter expression is invalid");
211 Expressions[ID].Kind = CounterExpression::ExprKind(Tag);
213 break;
214 }
215 default:
217 "counter expression kind is invalid");
218 }
219 return Error::success();
220}
221
222Error RawCoverageMappingReader::readCounter(Counter &C) {
223 uint64_t EncodedCounter;
224 if (auto Err =
225 readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max()))
226 return Err;
227 if (auto Err = decodeCounter(EncodedCounter, C))
228 return Err;
229 return Error::success();
230}
231
232static const unsigned EncodingExpansionRegionBit = 1
234
235/// Read the sub-array of regions for the given inferred file id.
236/// \param NumFileIDs the number of file ids that are defined for this
237/// function.
238Error RawCoverageMappingReader::readMappingRegionsSubArray(
239 std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID,
240 size_t NumFileIDs) {
241 uint64_t NumRegions;
242 if (auto Err = readSize(NumRegions))
243 return Err;
244 unsigned LineStart = 0;
245 for (size_t I = 0; I < NumRegions; ++I) {
246 Counter C, C2;
247 uint64_t BIDX, NC;
248 // They are stored as internal values plus 1 (min is -1)
249 uint64_t ID1, TID1, FID1;
250 mcdc::Parameters Params;
252
253 // Read the combined counter + region kind.
254 uint64_t EncodedCounterAndRegion;
255 if (auto Err = readIntMax(EncodedCounterAndRegion,
256 std::numeric_limits<unsigned>::max()))
257 return Err;
258 unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
259 uint64_t ExpandedFileID = 0;
260
261 // If Tag does not represent a ZeroCounter, then it is understood to refer
262 // to a counter or counter expression with region kind assumed to be
263 // "CodeRegion". In that case, EncodedCounterAndRegion actually encodes the
264 // referenced counter or counter expression (and nothing else).
265 //
266 // If Tag represents a ZeroCounter and EncodingExpansionRegionBit is set,
267 // then EncodedCounterAndRegion is interpreted to represent an
268 // ExpansionRegion. In all other cases, EncodedCounterAndRegion is
269 // interpreted to refer to a specific region kind, after which additional
270 // fields may be read (e.g. BranchRegions have two encoded counters that
271 // follow an encoded region kind value).
272 if (Tag != Counter::Zero) {
273 if (auto Err = decodeCounter(EncodedCounterAndRegion, C))
274 return Err;
275 } else {
276 // Is it an expansion region?
277 if (EncodedCounterAndRegion & EncodingExpansionRegionBit) {
279 ExpandedFileID = EncodedCounterAndRegion >>
281 if (ExpandedFileID >= NumFileIDs)
283 "ExpandedFileID is invalid");
284 } else {
285 switch (EncodedCounterAndRegion >>
288 // Don't do anything when we have a code region with a zero counter.
289 break;
292 break;
294 // For a Branch Region, read two successive counters.
296 if (auto Err = readCounter(C))
297 return Err;
298 if (auto Err = readCounter(C2))
299 return Err;
300 break;
302 // For a MCDC Branch Region, read two successive counters and 3 IDs.
304 if (auto Err = readCounter(C))
305 return Err;
306 if (auto Err = readCounter(C2))
307 return Err;
308 if (auto Err = readIntMax(ID1, std::numeric_limits<int16_t>::max()))
309 return Err;
310 if (auto Err = readIntMax(TID1, std::numeric_limits<int16_t>::max()))
311 return Err;
312 if (auto Err = readIntMax(FID1, std::numeric_limits<int16_t>::max()))
313 return Err;
314 if (ID1 == 0)
317 "MCDCConditionID shouldn't be zero");
318 Params = mcdc::BranchParameters{
319 static_cast<int16_t>(static_cast<int16_t>(ID1) - 1),
320 {static_cast<int16_t>(static_cast<int16_t>(FID1) - 1),
321 static_cast<int16_t>(static_cast<int16_t>(TID1) - 1)}};
322 break;
325 if (auto Err = readIntMax(BIDX, std::numeric_limits<unsigned>::max()))
326 return Err;
327 if (auto Err = readIntMax(NC, std::numeric_limits<int16_t>::max()))
328 return Err;
329 Params = mcdc::DecisionParameters{static_cast<unsigned>(BIDX),
330 static_cast<uint16_t>(NC)};
331 break;
332 default:
334 "region kind is incorrect");
335 }
336 }
337 }
338
339 // Read the source range.
340 uint64_t LineStartDelta, ColumnStart, NumLines, ColumnEnd;
341 if (auto Err =
342 readIntMax(LineStartDelta, std::numeric_limits<unsigned>::max()))
343 return Err;
344 if (auto Err = readULEB128(ColumnStart))
345 return Err;
346 if (ColumnStart > std::numeric_limits<unsigned>::max())
348 "start column is too big");
349 if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max()))
350 return Err;
351 if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max()))
352 return Err;
353 LineStart += LineStartDelta;
354
355 // If the high bit of ColumnEnd is set, this is a gap region.
356 if (ColumnEnd & (1U << 31)) {
358 ColumnEnd &= ~(1U << 31);
359 }
360
361 // Adjust the column locations for the empty regions that are supposed to
362 // cover whole lines. Those regions should be encoded with the
363 // column range (1 -> std::numeric_limits<unsigned>::max()), but because
364 // the encoded std::numeric_limits<unsigned>::max() is several bytes long,
365 // we set the column range to (0 -> 0) to ensure that the column start and
366 // column end take up one byte each.
367 // The std::numeric_limits<unsigned>::max() is used to represent a column
368 // position at the end of the line without knowing the length of that line.
369 if (ColumnStart == 0 && ColumnEnd == 0) {
370 ColumnStart = 1;
371 ColumnEnd = std::numeric_limits<unsigned>::max();
372 }
373
374 LLVM_DEBUG({
375 dbgs() << "Counter in file " << InferredFileID << " " << LineStart << ":"
376 << ColumnStart << " -> " << (LineStart + NumLines) << ":"
377 << ColumnEnd << ", ";
379 dbgs() << "Expands to file " << ExpandedFileID;
380 else
381 CounterMappingContext(Expressions).dump(C, dbgs());
382 dbgs() << "\n";
383 });
384
385 auto CMR = CounterMappingRegion(
386 C, C2, InferredFileID, ExpandedFileID, LineStart, ColumnStart,
387 LineStart + NumLines, ColumnEnd, Kind, Params);
388 if (CMR.startLoc() > CMR.endLoc())
391 "counter mapping region locations are incorrect");
392 MappingRegions.push_back(CMR);
393 }
394 return Error::success();
395}
396
398 // Read the virtual file mapping.
399 SmallVector<unsigned, 8> VirtualFileMapping;
400 uint64_t NumFileMappings;
401 if (auto Err = readSize(NumFileMappings))
402 return Err;
403 for (size_t I = 0; I < NumFileMappings; ++I) {
404 uint64_t FilenameIndex;
405 if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size()))
406 return Err;
407 VirtualFileMapping.push_back(FilenameIndex);
408 }
409
410 // Construct the files using unique filenames and virtual file mapping.
411 for (auto I : VirtualFileMapping) {
412 Filenames.push_back(TranslationUnitFilenames[I]);
413 }
414
415 // Read the expressions.
416 uint64_t NumExpressions;
417 if (auto Err = readSize(NumExpressions))
418 return Err;
419 // Create an array of dummy expressions that get the proper counters
420 // when the expressions are read, and the proper kinds when the counters
421 // are decoded.
422 Expressions.resize(
423 NumExpressions,
425 for (size_t I = 0; I < NumExpressions; ++I) {
426 if (auto Err = readCounter(Expressions[I].LHS))
427 return Err;
428 if (auto Err = readCounter(Expressions[I].RHS))
429 return Err;
430 }
431
432 // Read the mapping regions sub-arrays.
433 for (unsigned InferredFileID = 0, S = VirtualFileMapping.size();
434 InferredFileID < S; ++InferredFileID) {
435 if (auto Err = readMappingRegionsSubArray(MappingRegions, InferredFileID,
436 VirtualFileMapping.size()))
437 return Err;
438 }
439
440 // Set the counters for the expansion regions.
441 // i.e. Counter of expansion region = counter of the first region
442 // from the expanded file.
443 // Perform multiple passes to correctly propagate the counters through all the
444 // nested expansion regions. Iterate until no count changes.
445 SmallVector<CounterMappingRegion *, 8> FileIDExpansionRegionMapping;
446 FileIDExpansionRegionMapping.resize(VirtualFileMapping.size(), nullptr);
447 for (;;) {
448 for (auto &R : MappingRegions) {
450 continue;
451 assert(!FileIDExpansionRegionMapping[R.ExpandedFileID]);
452 FileIDExpansionRegionMapping[R.ExpandedFileID] = &R;
453 }
454 bool Changed = false;
455 for (auto &R : MappingRegions) {
456 auto *&Slot = FileIDExpansionRegionMapping[R.FileID];
457 if (Slot) {
458 if (Slot->Count != R.Count) {
459 Slot->Count = R.Count;
460 Changed = true;
461 }
462 Slot = nullptr;
463 }
464 }
465 if (!Changed)
466 break;
467 }
468
469 return Error::success();
470}
471
473 // A dummy coverage mapping data consists of just one region with zero count.
474 uint64_t NumFileMappings;
475 if (Error Err = readSize(NumFileMappings))
476 return std::move(Err);
477 if (NumFileMappings != 1)
478 return false;
479 // We don't expect any specific value for the filename index, just skip it.
480 uint64_t FilenameIndex;
481 if (Error Err =
482 readIntMax(FilenameIndex, std::numeric_limits<unsigned>::max()))
483 return std::move(Err);
484 uint64_t NumExpressions;
485 if (Error Err = readSize(NumExpressions))
486 return std::move(Err);
487 if (NumExpressions != 0)
488 return false;
489 uint64_t NumRegions;
490 if (Error Err = readSize(NumRegions))
491 return std::move(Err);
492 if (NumRegions != 1)
493 return false;
494 uint64_t EncodedCounterAndRegion;
495 if (Error Err = readIntMax(EncodedCounterAndRegion,
496 std::numeric_limits<unsigned>::max()))
497 return std::move(Err);
498 unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
499 return Tag == Counter::Zero;
500}
501
502/// Determine if we should skip the first byte of the section content
504 const ObjectFile *Obj = Section.getObject();
505 // If this is a linked PE/COFF file, then we have to skip over the null byte
506 // that is allocated in the .lprfn$A section in the LLVM profiling runtime.
507 // If the name section is .lprfcovnames, it doesn't have the null byte at the
508 // beginning.
509 if (isa<COFFObjectFile>(Obj) && !Obj->isRelocatableObject())
510 if (Expected<StringRef> NameOrErr = Section.getName())
511 if (*NameOrErr != getInstrProfSectionName(IPSK_covname, Triple::COFF))
512 return true;
513 return false;
514}
515
517 Expected<StringRef> DataOrErr = Section.getContents();
518 if (!DataOrErr)
519 return DataOrErr.takeError();
520 Data = *DataOrErr;
521 Address = Section.getAddress();
522
523 if (shouldSkipSectionFirstByte(Section))
524 Data = Data.substr(1);
525
526 return Error::success();
527}
528
530 if (Pointer < Address)
531 return StringRef();
532 auto Offset = Pointer - Address;
533 if (Offset + Size > Data.size())
534 return StringRef();
535 return Data.substr(Pointer - Address, Size);
536}
537
538// Check if the mapping data is a dummy, i.e. is emitted for an unused function.
540 // The hash value of dummy mapping records is always zero.
541 if (Hash)
542 return false;
543 return RawCoverageMappingDummyChecker(Mapping).isDummy();
544}
545
546/// A range of filename indices. Used to specify the location of a batch of
547/// filenames in a vector-like container.
550 unsigned Length;
551
554
555 void markInvalid() { Length = 0; }
556 bool isInvalid() const { return Length == 0; }
557};
558
559namespace {
560
561/// The interface to read coverage mapping function records for a module.
562struct CovMapFuncRecordReader {
563 virtual ~CovMapFuncRecordReader() = default;
564
565 // Read a coverage header.
566 //
567 // \p CovBuf points to the buffer containing the \c CovHeader of the coverage
568 // mapping data associated with the module.
569 //
570 // Returns a pointer to the next \c CovHeader if it exists, or to an address
571 // greater than \p CovEnd if not.
572 virtual Expected<const char *> readCoverageHeader(const char *CovBuf,
573 const char *CovBufEnd) = 0;
574
575 // Read function records.
576 //
577 // \p FuncRecBuf points to the buffer containing a batch of function records.
578 // \p FuncRecBufEnd points past the end of the batch of records.
579 //
580 // Prior to Version4, \p OutOfLineFileRange points to a sequence of filenames
581 // associated with the function records. It is unused in Version4.
582 //
583 // Prior to Version4, \p OutOfLineMappingBuf points to a sequence of coverage
584 // mappings associated with the function records. It is unused in Version4.
585 virtual Error
586 readFunctionRecords(const char *FuncRecBuf, const char *FuncRecBufEnd,
587 std::optional<FilenameRange> OutOfLineFileRange,
588 const char *OutOfLineMappingBuf,
589 const char *OutOfLineMappingBufEnd) = 0;
590
591 template <class IntPtrT, llvm::endianness Endian>
592 static Expected<std::unique_ptr<CovMapFuncRecordReader>>
593 get(CovMapVersion Version, InstrProfSymtab &P,
594 std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, StringRef D,
595 std::vector<std::string> &F);
596};
597
598// A class for reading coverage mapping function records for a module.
599template <CovMapVersion Version, class IntPtrT, llvm::endianness Endian>
600class VersionedCovMapFuncRecordReader : public CovMapFuncRecordReader {
601 using FuncRecordType =
603 using NameRefType = typename CovMapTraits<Version, IntPtrT>::NameRefType;
604
605 // Maps function's name references to the indexes of their records
606 // in \c Records.
607 DenseMap<NameRefType, size_t> FunctionRecords;
608 InstrProfSymtab &ProfileNames;
609 StringRef CompilationDir;
610 std::vector<std::string> &Filenames;
611 std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records;
612
613 // Maps a hash of the filenames in a TU to a \c FileRange. The range
614 // specifies the location of the hashed filenames in \c Filenames.
615 DenseMap<uint64_t, FilenameRange> FileRangeMap;
616
617 // Add the record to the collection if we don't already have a record that
618 // points to the same function name. This is useful to ignore the redundant
619 // records for the functions with ODR linkage.
620 // In addition, prefer records with real coverage mapping data to dummy
621 // records, which were emitted for inline functions which were seen but
622 // not used in the corresponding translation unit.
623 Error insertFunctionRecordIfNeeded(const FuncRecordType *CFR,
624 StringRef Mapping,
625 FilenameRange FileRange) {
626 ++CovMapNumRecords;
627 uint64_t FuncHash = CFR->template getFuncHash<Endian>();
628 NameRefType NameRef = CFR->template getFuncNameRef<Endian>();
629 auto InsertResult =
630 FunctionRecords.insert(std::make_pair(NameRef, Records.size()));
631 if (InsertResult.second) {
632 StringRef FuncName;
633 if (Error Err = CFR->template getFuncName<Endian>(ProfileNames, FuncName))
634 return Err;
635 if (FuncName.empty())
636 return make_error<InstrProfError>(instrprof_error::malformed,
637 "function name is empty");
638 ++CovMapNumUsedRecords;
639 Records.emplace_back(Version, FuncName, FuncHash, Mapping,
640 FileRange.StartingIndex, FileRange.Length);
641 return Error::success();
642 }
643 // Update the existing record if it's a dummy and the new record is real.
644 size_t OldRecordIndex = InsertResult.first->second;
645 BinaryCoverageReader::ProfileMappingRecord &OldRecord =
646 Records[OldRecordIndex];
647 Expected<bool> OldIsDummyExpected = isCoverageMappingDummy(
648 OldRecord.FunctionHash, OldRecord.CoverageMapping);
649 if (Error Err = OldIsDummyExpected.takeError())
650 return Err;
651 if (!*OldIsDummyExpected)
652 return Error::success();
653 Expected<bool> NewIsDummyExpected =
655 if (Error Err = NewIsDummyExpected.takeError())
656 return Err;
657 if (*NewIsDummyExpected)
658 return Error::success();
659 ++CovMapNumUsedRecords;
660 OldRecord.FunctionHash = FuncHash;
661 OldRecord.CoverageMapping = Mapping;
662 OldRecord.FilenamesBegin = FileRange.StartingIndex;
663 OldRecord.FilenamesSize = FileRange.Length;
664 return Error::success();
665 }
666
667public:
668 VersionedCovMapFuncRecordReader(
669 InstrProfSymtab &P,
670 std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, StringRef D,
671 std::vector<std::string> &F)
672 : ProfileNames(P), CompilationDir(D), Filenames(F), Records(R) {}
673
674 ~VersionedCovMapFuncRecordReader() override = default;
675
676 Expected<const char *> readCoverageHeader(const char *CovBuf,
677 const char *CovBufEnd) override {
678 using namespace support;
679
680 if (CovBuf + sizeof(CovMapHeader) > CovBufEnd)
682 coveragemap_error::malformed,
683 "coverage mapping header section is larger than buffer size");
684 auto CovHeader = reinterpret_cast<const CovMapHeader *>(CovBuf);
685 uint32_t NRecords = CovHeader->getNRecords<Endian>();
686 uint32_t FilenamesSize = CovHeader->getFilenamesSize<Endian>();
687 uint32_t CoverageSize = CovHeader->getCoverageSize<Endian>();
688 assert((CovMapVersion)CovHeader->getVersion<Endian>() == Version);
689 CovBuf = reinterpret_cast<const char *>(CovHeader + 1);
690
691 // Skip past the function records, saving the start and end for later.
692 // This is a no-op in Version4 (function records are read after all headers
693 // are read).
694 const char *FuncRecBuf = nullptr;
695 const char *FuncRecBufEnd = nullptr;
696 if (Version < CovMapVersion::Version4)
697 FuncRecBuf = CovBuf;
698 CovBuf += NRecords * sizeof(FuncRecordType);
699 if (Version < CovMapVersion::Version4)
700 FuncRecBufEnd = CovBuf;
701
702 // Get the filenames.
703 if (CovBuf + FilenamesSize > CovBufEnd)
705 coveragemap_error::malformed,
706 "filenames section is larger than buffer size");
707 size_t FilenamesBegin = Filenames.size();
708 StringRef FilenameRegion(CovBuf, FilenamesSize);
709 RawCoverageFilenamesReader Reader(FilenameRegion, Filenames,
710 CompilationDir);
711 if (auto Err = Reader.read(Version))
712 return std::move(Err);
713 CovBuf += FilenamesSize;
714 FilenameRange FileRange(FilenamesBegin, Filenames.size() - FilenamesBegin);
715
716 if (Version >= CovMapVersion::Version4) {
717 // Map a hash of the filenames region to the filename range associated
718 // with this coverage header.
719 int64_t FilenamesRef =
721 auto Insert =
722 FileRangeMap.insert(std::make_pair(FilenamesRef, FileRange));
723 if (!Insert.second) {
724 // The same filenames ref was encountered twice. It's possible that
725 // the associated filenames are the same.
726 auto It = Filenames.begin();
727 FilenameRange &OrigRange = Insert.first->getSecond();
728 if (std::equal(It + OrigRange.StartingIndex,
729 It + OrigRange.StartingIndex + OrigRange.Length,
730 It + FileRange.StartingIndex,
731 It + FileRange.StartingIndex + FileRange.Length))
732 // Map the new range to the original one.
733 FileRange = OrigRange;
734 else
735 // This is a hash collision. Mark the filenames ref invalid.
736 OrigRange.markInvalid();
737 }
738 }
739
740 // We'll read the coverage mapping records in the loop below.
741 // This is a no-op in Version4 (coverage mappings are not affixed to the
742 // coverage header).
743 const char *MappingBuf = CovBuf;
744 if (Version >= CovMapVersion::Version4 && CoverageSize != 0)
745 return make_error<CoverageMapError>(coveragemap_error::malformed,
746 "coverage mapping size is not zero");
747 CovBuf += CoverageSize;
748 const char *MappingEnd = CovBuf;
749
750 if (CovBuf > CovBufEnd)
752 coveragemap_error::malformed,
753 "function records section is larger than buffer size");
754
755 if (Version < CovMapVersion::Version4) {
756 // Read each function record.
757 if (Error E = readFunctionRecords(FuncRecBuf, FuncRecBufEnd, FileRange,
758 MappingBuf, MappingEnd))
759 return std::move(E);
760 }
761
762 // Each coverage map has an alignment of 8, so we need to adjust alignment
763 // before reading the next map.
764 CovBuf += offsetToAlignedAddr(CovBuf, Align(8));
765
766 return CovBuf;
767 }
768
769 Error readFunctionRecords(const char *FuncRecBuf, const char *FuncRecBufEnd,
770 std::optional<FilenameRange> OutOfLineFileRange,
771 const char *OutOfLineMappingBuf,
772 const char *OutOfLineMappingBufEnd) override {
773 auto CFR = reinterpret_cast<const FuncRecordType *>(FuncRecBuf);
774 while ((const char *)CFR < FuncRecBufEnd) {
775 // Validate the length of the coverage mapping for this function.
776 const char *NextMappingBuf;
777 const FuncRecordType *NextCFR;
778 std::tie(NextMappingBuf, NextCFR) =
779 CFR->template advanceByOne<Endian>(OutOfLineMappingBuf);
780 if (Version < CovMapVersion::Version4)
781 if (NextMappingBuf > OutOfLineMappingBufEnd)
783 coveragemap_error::malformed,
784 "next mapping buffer is larger than buffer size");
785
786 // Look up the set of filenames associated with this function record.
787 std::optional<FilenameRange> FileRange;
788 if (Version < CovMapVersion::Version4) {
789 FileRange = OutOfLineFileRange;
790 } else {
791 uint64_t FilenamesRef = CFR->template getFilenamesRef<Endian>();
792 auto It = FileRangeMap.find(FilenamesRef);
793 if (It == FileRangeMap.end())
795 coveragemap_error::malformed,
796 "no filename found for function with hash=0x" +
797 Twine::utohexstr(FilenamesRef));
798 else
799 FileRange = It->getSecond();
800 }
801
802 // Now, read the coverage data.
803 if (FileRange && !FileRange->isInvalid()) {
804 StringRef Mapping =
805 CFR->template getCoverageMapping<Endian>(OutOfLineMappingBuf);
806 if (Version >= CovMapVersion::Version4 &&
807 Mapping.data() + Mapping.size() > FuncRecBufEnd)
809 coveragemap_error::malformed,
810 "coverage mapping data is larger than buffer size");
811 if (Error Err = insertFunctionRecordIfNeeded(CFR, Mapping, *FileRange))
812 return Err;
813 }
814
815 std::tie(OutOfLineMappingBuf, CFR) = std::tie(NextMappingBuf, NextCFR);
816 }
817 return Error::success();
818 }
819};
820
821} // end anonymous namespace
822
823template <class IntPtrT, llvm::endianness Endian>
824Expected<std::unique_ptr<CovMapFuncRecordReader>> CovMapFuncRecordReader::get(
825 CovMapVersion Version, InstrProfSymtab &P,
826 std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, StringRef D,
827 std::vector<std::string> &F) {
828 using namespace coverage;
829
830 switch (Version) {
832 return std::make_unique<VersionedCovMapFuncRecordReader<
833 CovMapVersion::Version1, IntPtrT, Endian>>(P, R, D, F);
840 // Decompress the name data.
841 if (Error E = P.create(P.getNameData()))
842 return std::move(E);
844 return std::make_unique<VersionedCovMapFuncRecordReader<
845 CovMapVersion::Version2, IntPtrT, Endian>>(P, R, D, F);
847 return std::make_unique<VersionedCovMapFuncRecordReader<
848 CovMapVersion::Version3, IntPtrT, Endian>>(P, R, D, F);
850 return std::make_unique<VersionedCovMapFuncRecordReader<
851 CovMapVersion::Version4, IntPtrT, Endian>>(P, R, D, F);
853 return std::make_unique<VersionedCovMapFuncRecordReader<
854 CovMapVersion::Version5, IntPtrT, Endian>>(P, R, D, F);
856 return std::make_unique<VersionedCovMapFuncRecordReader<
857 CovMapVersion::Version6, IntPtrT, Endian>>(P, R, D, F);
859 return std::make_unique<VersionedCovMapFuncRecordReader<
860 CovMapVersion::Version7, IntPtrT, Endian>>(P, R, D, F);
861 }
862 llvm_unreachable("Unsupported version");
863}
864
865template <typename T, llvm::endianness Endian>
867 InstrProfSymtab &ProfileNames, StringRef CovMap, StringRef FuncRecords,
868 std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records,
869 StringRef CompilationDir, std::vector<std::string> &Filenames) {
870 using namespace coverage;
871
872 // Read the records in the coverage data section.
873 auto CovHeader =
874 reinterpret_cast<const CovMapHeader *>(CovMap.data());
875 CovMapVersion Version = (CovMapVersion)CovHeader->getVersion<Endian>();
876 if (Version > CovMapVersion::CurrentVersion)
879 CovMapFuncRecordReader::get<T, Endian>(Version, ProfileNames, Records,
880 CompilationDir, Filenames);
881 if (Error E = ReaderExpected.takeError())
882 return E;
883 auto Reader = std::move(ReaderExpected.get());
884 const char *CovBuf = CovMap.data();
885 const char *CovBufEnd = CovBuf + CovMap.size();
886 const char *FuncRecBuf = FuncRecords.data();
887 const char *FuncRecBufEnd = FuncRecords.data() + FuncRecords.size();
888 while (CovBuf < CovBufEnd) {
889 // Read the current coverage header & filename data.
890 //
891 // Prior to Version4, this also reads all function records affixed to the
892 // header.
893 //
894 // Return a pointer to the next coverage header.
895 auto NextOrErr = Reader->readCoverageHeader(CovBuf, CovBufEnd);
896 if (auto E = NextOrErr.takeError())
897 return E;
898 CovBuf = NextOrErr.get();
899 }
900 // In Version4, function records are not affixed to coverage headers. Read
901 // the records from their dedicated section.
902 if (Version >= CovMapVersion::Version4)
903 return Reader->readFunctionRecords(FuncRecBuf, FuncRecBufEnd, std::nullopt,
904 nullptr, nullptr);
905 return Error::success();
906}
907
908Expected<std::unique_ptr<BinaryCoverageReader>>
910 StringRef Coverage, FuncRecordsStorage &&FuncRecords,
911 CoverageMapCopyStorage &&CoverageMap,
912 std::unique_ptr<InstrProfSymtab> ProfileNamesPtr, uint8_t BytesInAddress,
913 llvm::endianness Endian, StringRef CompilationDir) {
914 if (ProfileNamesPtr == nullptr)
916 "Caller must provide ProfileNames");
917 std::unique_ptr<BinaryCoverageReader> Reader(
918 new BinaryCoverageReader(std::move(ProfileNamesPtr),
919 std::move(FuncRecords), std::move(CoverageMap)));
920 InstrProfSymtab &ProfileNames = *Reader->ProfileNames;
921 StringRef FuncRecordsRef = Reader->FuncRecords->getBuffer();
922 if (BytesInAddress == 4 && Endian == llvm::endianness::little) {
924 ProfileNames, Coverage, FuncRecordsRef, Reader->MappingRecords,
925 CompilationDir, Reader->Filenames))
926 return std::move(E);
927 } else if (BytesInAddress == 4 && Endian == llvm::endianness::big) {
929 ProfileNames, Coverage, FuncRecordsRef, Reader->MappingRecords,
930 CompilationDir, Reader->Filenames))
931 return std::move(E);
932 } else if (BytesInAddress == 8 && Endian == llvm::endianness::little) {
934 ProfileNames, Coverage, FuncRecordsRef, Reader->MappingRecords,
935 CompilationDir, Reader->Filenames))
936 return std::move(E);
937 } else if (BytesInAddress == 8 && Endian == llvm::endianness::big) {
939 ProfileNames, Coverage, FuncRecordsRef, Reader->MappingRecords,
940 CompilationDir, Reader->Filenames))
941 return std::move(E);
942 } else
945 "not supported endianness or bytes in address");
946 return std::move(Reader);
947}
948
951 uint8_t BytesInAddress = 8;
953
954 // Read the magic and version.
955 Data = Data.substr(sizeof(TestingFormatMagic));
956 if (Data.size() < sizeof(uint64_t))
958 "the size of data is too small");
959 auto TestingVersion = support::endian::byte_swap<uint64_t>(
960 *reinterpret_cast<const uint64_t *>(Data.data()),
962 Data = Data.substr(sizeof(uint64_t));
963
964 // Read the ProfileNames data.
965 if (Data.empty())
967 unsigned N = 0;
968 uint64_t ProfileNamesSize = decodeULEB128(Data.bytes_begin(), &N);
969 if (N > Data.size())
972 "the size of TestingFormatMagic is too big");
973 Data = Data.substr(N);
974 if (Data.empty())
976 N = 0;
977 uint64_t Address = decodeULEB128(Data.bytes_begin(), &N);
978 if (N > Data.size())
980 "the size of ULEB128 is too big");
981 Data = Data.substr(N);
982 if (Data.size() < ProfileNamesSize)
984 "the size of ProfileNames is too big");
985 auto ProfileNames = std::make_unique<InstrProfSymtab>();
986 if (Error E = ProfileNames->create(Data.substr(0, ProfileNamesSize), Address))
987 return std::move(E);
988 Data = Data.substr(ProfileNamesSize);
989
990 // In Version2, the size of CoverageMapping is stored directly.
991 uint64_t CoverageMappingSize;
992 if (TestingVersion == uint64_t(TestingFormatVersion::Version2)) {
993 N = 0;
994 CoverageMappingSize = decodeULEB128(Data.bytes_begin(), &N);
995 if (N > Data.size())
997 "the size of ULEB128 is too big");
998 Data = Data.substr(N);
999 if (CoverageMappingSize < sizeof(CovMapHeader))
1002 "the size of CoverageMapping is teoo small");
1003 } else if (TestingVersion != uint64_t(TestingFormatVersion::Version1)) {
1005 }
1006
1007 // Skip the padding bytes because coverage map data has an alignment of 8.
1008 auto Pad = offsetToAlignedAddr(Data.data(), Align(8));
1009 if (Data.size() < Pad)
1011 "insufficient padding");
1012 Data = Data.substr(Pad);
1013 if (Data.size() < sizeof(CovMapHeader))
1016 "coverage mapping header section is larger than data size");
1017 auto const *CovHeader = reinterpret_cast<const CovMapHeader *>(
1018 Data.substr(0, sizeof(CovMapHeader)).data());
1019 auto Version =
1020 CovMapVersion(CovHeader->getVersion<llvm::endianness::little>());
1021
1022 // In Version1, the size of CoverageMapping is calculated.
1023 if (TestingVersion == uint64_t(TestingFormatVersion::Version1)) {
1024 if (Version < CovMapVersion::Version4) {
1025 CoverageMappingSize = Data.size();
1026 } else {
1027 auto FilenamesSize =
1028 CovHeader->getFilenamesSize<llvm::endianness::little>();
1029 CoverageMappingSize = sizeof(CovMapHeader) + FilenamesSize;
1030 }
1031 }
1032
1033 auto CoverageMapping = Data.substr(0, CoverageMappingSize);
1034 Data = Data.substr(CoverageMappingSize);
1035
1036 // Read the CoverageRecords data.
1037 if (Version < CovMapVersion::Version4) {
1038 if (!Data.empty())
1040 "data is not empty");
1041 } else {
1042 // Skip the padding bytes because coverage records data has an alignment
1043 // of 8.
1044 Pad = offsetToAlignedAddr(Data.data(), Align(8));
1045 if (Data.size() < Pad)
1047 "insufficient padding");
1048 Data = Data.substr(Pad);
1049 }
1052
1054 CoverageMapping, std::move(CoverageRecords), nullptr,
1055 std::move(ProfileNames), BytesInAddress, Endian, CompilationDir);
1056}
1057
1058/// Find all sections that match \p IPSK name. There may be more than one if
1059/// comdats are in use, e.g. for the __llvm_covfun section on ELF.
1060static Expected<std::vector<SectionRef>>
1062 auto ObjFormat = OF.getTripleObjectFormat();
1063 auto Name =
1064 getInstrProfSectionName(IPSK, ObjFormat, /*AddSegmentInfo=*/false);
1065 // On COFF, the object file section name may end in "$M". This tells the
1066 // linker to sort these sections between "$A" and "$Z". The linker removes the
1067 // dollar and everything after it in the final binary. Do the same to match.
1068 bool IsCOFF = isa<COFFObjectFile>(OF);
1069 auto stripSuffix = [IsCOFF](StringRef N) {
1070 return IsCOFF ? N.split('$').first : N;
1071 };
1072 Name = stripSuffix(Name);
1073
1074 std::vector<SectionRef> Sections;
1075 for (const auto &Section : OF.sections()) {
1076 Expected<StringRef> NameOrErr = Section.getName();
1077 if (!NameOrErr)
1078 return NameOrErr.takeError();
1079 if (stripSuffix(*NameOrErr) == Name) {
1080 // Skip empty profile name section.
1081 // COFF profile name section contains two null bytes indicating the
1082 // start/end of the section. If its size is 2 bytes, it's empty.
1083 if (IPSK == IPSK_name &&
1084 (Section.getSize() == 0 || (IsCOFF && Section.getSize() == 2)))
1085 continue;
1086 Sections.push_back(Section);
1087 }
1088 }
1089 if (Sections.empty())
1091 return Sections;
1092}
1093
1094/// Find a section that matches \p Name and is allocatable at runtime.
1095///
1096/// Returns the contents of the section and its start offset in the object file.
1097static Expected<std::pair<StringRef, uint64_t>>
1099 // On Wasm, allocatable sections can live only in data segments.
1100 if (auto *WOF = dyn_cast<WasmObjectFile>(&OF)) {
1101 std::vector<const WasmSegment *> Segments;
1102 auto ObjFormat = OF.getTripleObjectFormat();
1103 auto Name =
1104 getInstrProfSectionName(IPSK, ObjFormat, /*AddSegmentInfo=*/false);
1105 for (const auto &DebugName : WOF->debugNames()) {
1106 if (DebugName.Type != wasm::NameType::DATA_SEGMENT ||
1107 DebugName.Name != Name)
1108 continue;
1109 if (DebugName.Index >= WOF->dataSegments().size())
1111 auto &Segment = WOF->dataSegments()[DebugName.Index];
1112 Segments.push_back(&Segment);
1113 }
1114 if (Segments.empty())
1116 if (Segments.size() != 1)
1118
1119 const auto &Segment = *Segments.front();
1120 auto &Data = Segment.Data;
1121 StringRef Content(reinterpret_cast<const char *>(Data.Content.data()),
1122 Data.Content.size());
1123 return std::make_pair(Content, Segment.SectionOffset);
1124 }
1125
1126 // On other object file types, delegate to lookupSections to find the section.
1127 auto Sections = lookupSections(OF, IPSK);
1128 if (!Sections)
1129 return Sections.takeError();
1130 if (Sections->size() != 1)
1133 "the size of coverage mapping section is not one");
1134 auto &Section = Sections->front();
1135 auto ContentsOrErr = Section.getContents();
1136 if (!ContentsOrErr)
1137 return ContentsOrErr.takeError();
1138 auto Content = *ContentsOrErr;
1139 if (shouldSkipSectionFirstByte(Section))
1140 Content = Content.drop_front(1);
1141 return std::make_pair(Content, Section.getAddress());
1142}
1143
1144static Expected<std::unique_ptr<BinaryCoverageReader>>
1145loadBinaryFormat(std::unique_ptr<Binary> Bin, StringRef Arch,
1146 StringRef CompilationDir = "",
1147 object::BuildIDRef *BinaryID = nullptr) {
1148 std::unique_ptr<ObjectFile> OF;
1149 if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin.get())) {
1150 // If we have a universal binary, try to look up the object for the
1151 // appropriate architecture.
1152 auto ObjectFileOrErr = Universal->getMachOObjectForArch(Arch);
1153 if (!ObjectFileOrErr)
1154 return ObjectFileOrErr.takeError();
1155 OF = std::move(ObjectFileOrErr.get());
1156 } else if (isa<ObjectFile>(Bin.get())) {
1157 // For any other object file, upcast and take ownership.
1158 OF.reset(cast<ObjectFile>(Bin.release()));
1159 // If we've asked for a particular arch, make sure they match.
1160 if (!Arch.empty() && OF->getArch() != Triple(Arch).getArch())
1161 return errorCodeToError(object_error::arch_not_found);
1162 } else
1163 // We can only handle object files.
1165 "binary is not an object file");
1166
1167 // The coverage uses native pointer sizes for the object it's written in.
1168 uint8_t BytesInAddress = OF->getBytesInAddress();
1169 llvm::endianness Endian =
1170 OF->isLittleEndian() ? llvm::endianness::little : llvm::endianness::big;
1171
1172 // Look for the sections that we are interested in.
1173 auto ProfileNames = std::make_unique<InstrProfSymtab>();
1174 // If IPSK_name is not found, fallback to search for IPK_covname, which is
1175 // used when binary correlation is enabled.
1176 auto NamesSection = lookupAllocatableSection(*OF, IPSK_name);
1177 if (auto E = NamesSection.takeError()) {
1178 consumeError(std::move(E));
1179 NamesSection = lookupAllocatableSection(*OF, IPSK_covname);
1180 if (auto E = NamesSection.takeError())
1181 return std::move(E);
1182 }
1183
1184 uint64_t NamesAddress;
1185 StringRef NamesContent;
1186 std::tie(NamesContent, NamesAddress) = *NamesSection;
1187 if (Error E = ProfileNames->create(NamesContent, NamesAddress))
1188 return std::move(E);
1189
1190 auto CoverageSection = lookupSections(*OF, IPSK_covmap);
1191 if (auto E = CoverageSection.takeError())
1192 return std::move(E);
1193 std::vector<SectionRef> CoverageSectionRefs = *CoverageSection;
1194 if (CoverageSectionRefs.size() != 1)
1196 "the size of name section is not one");
1197 auto CoverageMappingOrErr = CoverageSectionRefs.back().getContents();
1198 if (!CoverageMappingOrErr)
1199 return CoverageMappingOrErr.takeError();
1200 StringRef CoverageMapping = CoverageMappingOrErr.get();
1201
1202 // If the coverage mapping section is not aligned to 8 bytes, copy it to a
1203 // new buffer that is. Wasm format typically has unaligned section contents
1204 // because it doesn't have a good way to insert padding bytes.
1205 std::unique_ptr<MemoryBuffer> CoverageMapCopy;
1206 if (!isAddrAligned(Align(8), CoverageMapping.data())) {
1208 CoverageMapping = CoverageMapCopy->getBuffer();
1209 }
1210
1211 // Look for the coverage records section (Version4 only).
1212 auto CoverageRecordsSections = lookupSections(*OF, IPSK_covfun);
1213
1215 if (auto E = CoverageRecordsSections.takeError()) {
1216 consumeError(std::move(E));
1217 FuncRecords = MemoryBuffer::getMemBuffer("");
1218 } else {
1219 // Compute the FuncRecordsBuffer of the buffer, taking into account the
1220 // padding between each record, and making sure the first block is aligned
1221 // in memory to maintain consistency between buffer address and size
1222 // alignment.
1223 const Align RecordAlignment(8);
1224 uint64_t FuncRecordsSize = 0;
1225 for (SectionRef Section : *CoverageRecordsSections) {
1226 auto CoverageRecordsOrErr = Section.getContents();
1227 if (!CoverageRecordsOrErr)
1228 return CoverageRecordsOrErr.takeError();
1229 FuncRecordsSize += alignTo(CoverageRecordsOrErr->size(), RecordAlignment);
1230 }
1231 auto WritableBuffer =
1233 char *FuncRecordsBuffer = WritableBuffer->getBufferStart();
1234 assert(isAddrAligned(RecordAlignment, FuncRecordsBuffer) &&
1235 "Allocated memory is correctly aligned");
1236
1237 for (SectionRef Section : *CoverageRecordsSections) {
1238 auto CoverageRecordsOrErr = Section.getContents();
1239 if (!CoverageRecordsOrErr)
1240 return CoverageRecordsOrErr.takeError();
1241 const auto &CoverageRecords = CoverageRecordsOrErr.get();
1242 FuncRecordsBuffer = llvm::copy(CoverageRecords, FuncRecordsBuffer);
1243 FuncRecordsBuffer =
1244 std::fill_n(FuncRecordsBuffer,
1245 alignAddr(FuncRecordsBuffer, RecordAlignment) -
1246 (uintptr_t)FuncRecordsBuffer,
1247 '\0');
1248 }
1249 assert(FuncRecordsBuffer == WritableBuffer->getBufferEnd() &&
1250 "consistent init");
1251 FuncRecords = std::move(WritableBuffer);
1252 }
1253
1254 if (BinaryID)
1255 *BinaryID = getBuildID(OF.get());
1256
1258 CoverageMapping, std::move(FuncRecords), std::move(CoverageMapCopy),
1259 std::move(ProfileNames), BytesInAddress, Endian, CompilationDir);
1260}
1261
1262/// Determine whether \p Arch is invalid or empty, given \p Bin.
1264 // If we have a universal binary and Arch doesn't identify any of its slices,
1265 // it's user error.
1266 if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin)) {
1267 for (auto &ObjForArch : Universal->objects())
1268 if (Arch == ObjForArch.getArchFlagName())
1269 return false;
1270 return true;
1271 }
1272 return false;
1273}
1274
1275Expected<std::vector<std::unique_ptr<BinaryCoverageReader>>>
1277 MemoryBufferRef ObjectBuffer, StringRef Arch,
1278 SmallVectorImpl<std::unique_ptr<MemoryBuffer>> &ObjectFileBuffers,
1279 StringRef CompilationDir, SmallVectorImpl<object::BuildIDRef> *BinaryIDs) {
1280 std::vector<std::unique_ptr<BinaryCoverageReader>> Readers;
1281
1282 if (ObjectBuffer.getBuffer().size() > sizeof(TestingFormatMagic)) {
1284 *reinterpret_cast<const uint64_t *>(ObjectBuffer.getBufferStart()),
1286 if (Magic == TestingFormatMagic) {
1287 // This is a special format used for testing.
1288 auto ReaderOrErr =
1289 loadTestingFormat(ObjectBuffer.getBuffer(), CompilationDir);
1290 if (!ReaderOrErr)
1291 return ReaderOrErr.takeError();
1292 Readers.push_back(std::move(ReaderOrErr.get()));
1293 return std::move(Readers);
1294 }
1295 }
1296
1297 auto BinOrErr = createBinary(ObjectBuffer);
1298 if (!BinOrErr)
1299 return BinOrErr.takeError();
1300 std::unique_ptr<Binary> Bin = std::move(BinOrErr.get());
1301
1302 if (isArchSpecifierInvalidOrMissing(Bin.get(), Arch))
1305
1306 // MachO universal binaries which contain archives need to be treated as
1307 // archives, not as regular binaries.
1308 if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin.get())) {
1309 for (auto &ObjForArch : Universal->objects()) {
1310 // Skip slices within the universal binary which target the wrong arch.
1311 std::string ObjArch = ObjForArch.getArchFlagName();
1312 if (Arch != ObjArch)
1313 continue;
1314
1315 auto ArchiveOrErr = ObjForArch.getAsArchive();
1316 if (!ArchiveOrErr) {
1317 // If this is not an archive, try treating it as a regular object.
1318 consumeError(ArchiveOrErr.takeError());
1319 break;
1320 }
1321
1323 ArchiveOrErr.get()->getMemoryBufferRef(), Arch, ObjectFileBuffers,
1324 CompilationDir, BinaryIDs);
1325 }
1326 }
1327
1328 // Load coverage out of archive members.
1329 if (auto *Ar = dyn_cast<Archive>(Bin.get())) {
1330 Error Err = Error::success();
1331 for (auto &Child : Ar->children(Err)) {
1332 Expected<MemoryBufferRef> ChildBufOrErr = Child.getMemoryBufferRef();
1333 if (!ChildBufOrErr)
1334 return ChildBufOrErr.takeError();
1335
1336 auto ChildReadersOrErr = BinaryCoverageReader::create(
1337 ChildBufOrErr.get(), Arch, ObjectFileBuffers, CompilationDir,
1338 BinaryIDs);
1339 if (!ChildReadersOrErr)
1340 return ChildReadersOrErr.takeError();
1341 for (auto &Reader : ChildReadersOrErr.get())
1342 Readers.push_back(std::move(Reader));
1343 }
1344 if (Err)
1345 return std::move(Err);
1346
1347 // Thin archives reference object files outside of the archive file, i.e.
1348 // files which reside in memory not owned by the caller. Transfer ownership
1349 // to the caller.
1350 if (Ar->isThin())
1351 for (auto &Buffer : Ar->takeThinBuffers())
1352 ObjectFileBuffers.push_back(std::move(Buffer));
1353
1354 return std::move(Readers);
1355 }
1356
1357 object::BuildIDRef BinaryID;
1358 auto ReaderOrErr = loadBinaryFormat(std::move(Bin), Arch, CompilationDir,
1359 BinaryIDs ? &BinaryID : nullptr);
1360 if (!ReaderOrErr)
1361 return ReaderOrErr.takeError();
1362 Readers.push_back(std::move(ReaderOrErr.get()));
1363 if (!BinaryID.empty())
1364 BinaryIDs->push_back(BinaryID);
1365 return std::move(Readers);
1366}
1367
1369 if (CurrentRecord >= MappingRecords.size())
1371
1372 FunctionsFilenames.clear();
1373 Expressions.clear();
1374 MappingRegions.clear();
1375 auto &R = MappingRecords[CurrentRecord];
1376 auto F = ArrayRef(Filenames).slice(R.FilenamesBegin, R.FilenamesSize);
1377 RawCoverageMappingReader Reader(R.CoverageMapping, F, FunctionsFilenames,
1378 Expressions, MappingRegions);
1379 if (auto Err = Reader.read())
1380 return Err;
1381
1382 Record.FunctionName = R.FunctionName;
1383 Record.FunctionHash = R.FunctionHash;
1384 Record.Filenames = FunctionsFilenames;
1385 Record.Expressions = Expressions;
1386 Record.MappingRegions = MappingRegions;
1387
1388 ++CurrentRecord;
1389 return Error::success();
1390}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static Error readCoverageMappingData(InstrProfSymtab &ProfileNames, StringRef CovMap, StringRef FuncRecords, std::vector< BinaryCoverageReader::ProfileMappingRecord > &Records, StringRef CompilationDir, std::vector< std::string > &Filenames)
static Expected< std::pair< StringRef, uint64_t > > lookupAllocatableSection(ObjectFile &OF, InstrProfSectKind IPSK)
Find a section that matches Name and is allocatable at runtime.
static Expected< std::unique_ptr< BinaryCoverageReader > > loadBinaryFormat(std::unique_ptr< Binary > Bin, StringRef Arch, StringRef CompilationDir="", object::BuildIDRef *BinaryID=nullptr)
static Expected< std::unique_ptr< BinaryCoverageReader > > loadTestingFormat(StringRef Data, StringRef CompilationDir)
static Expected< std::vector< SectionRef > > lookupSections(ObjectFile &OF, InstrProfSectKind IPSK)
Find all sections that match IPSK name.
static bool isArchSpecifierInvalidOrMissing(Binary *Bin, StringRef Arch)
Determine whether Arch is invalid or empty, given Bin.
static bool shouldSkipSectionFirstByte(SectionRef &Section)
Determine if we should skip the first byte of the section content.
static Expected< bool > isCoverageMappingDummy(uint64_t Hash, StringRef Mapping)
static const unsigned EncodingExpansionRegionBit
This file defines the DenseMap class.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
static constexpr StringLiteral Filename
#define P(N)
This file defines the SmallVector class.
static Split data
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
iterator end()
Definition DenseMap.h:81
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:241
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
reference get()
Returns a reference to the stored T value.
Definition Error.h:582
A symbol table used for function [IR]PGO name look-up with keys (such as pointers,...
Definition InstrProf.h:508
LLVM_ABI StringRef getFuncName(uint64_t FuncNameAddress, size_t NameSize) const
Return function's PGO name from the function name's symbol address in the object file.
LLVM_ABI Error create(object::SectionRef &Section)
Create InstrProfSymtab from an object file section which contains function PGO names.
const char * getBufferStart() const
StringRef getBuffer() const
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void resize(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
std::string str() const
str - Get the contents as an std::string.
Definition StringRef.h:222
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:140
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:143
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:137
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
static Twine utohexstr(uint64_t Val)
Definition Twine.h:385
static LLVM_ABI std::unique_ptr< WritableMemoryBuffer > getNewUninitMemBuffer(size_t Size, const Twine &BufferName="", std::optional< Align > Alignment=std::nullopt)
Allocate a new MemoryBuffer of the specified size that is not initialized.
static Expected< std::unique_ptr< BinaryCoverageReader > > createCoverageReaderFromBuffer(StringRef Coverage, FuncRecordsStorage &&FuncRecords, CoverageMapCopyStorage &&CoverageMap, std::unique_ptr< InstrProfSymtab > ProfileNamesPtr, uint8_t BytesInAddress, llvm::endianness Endian, StringRef CompilationDir="")
std::unique_ptr< MemoryBuffer > CoverageMapCopyStorage
static Expected< std::vector< std::unique_ptr< BinaryCoverageReader > > > create(MemoryBufferRef ObjectBuffer, StringRef Arch, SmallVectorImpl< std::unique_ptr< MemoryBuffer > > &ObjectFileBuffers, StringRef CompilationDir="", SmallVectorImpl< object::BuildIDRef > *BinaryIDs=nullptr)
std::unique_ptr< MemoryBuffer > FuncRecordsStorage
Error readNextRecord(CoverageMappingRecord &Record) override
A Counter mapping context is used to connect the counters, expressions and the obtained counter value...
LLVM_ABI void dump(const Counter &C, raw_ostream &OS) const
coveragemap_error get() const
virtual Error readNextRecord(CoverageMappingRecord &Record)=0
The mapping of profile information to coverage data.
RawCoverageFilenamesReader(StringRef Data, std::vector< std::string > &Filenames, StringRef CompilationDir="")
LLVM_ABI Error read(CovMapVersion Version)
Checks if the given coverage mapping data is exported for an unused function.
Reader for the raw coverage mapping data.
LLVM_ABI Error readSize(uint64_t &Result)
LLVM_ABI Error readIntMax(uint64_t &Result, uint64_t MaxPlus1)
LLVM_ABI Error readULEB128(uint64_t &Result)
LLVM_ABI Error readString(StringRef &Result)
This class is the base class for all object file types.
Definition ObjectFile.h:231
Represents a GOFF physical record.
Definition GOFF.h:31
This is a value type class that represents a single section in the list of sections in the object fil...
Definition ObjectFile.h:83
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
uint64_t ComputeHash(StringRef K)
Definition InstrProf.h:1190
llvm::SmallVector< std::shared_ptr< RecordsSlice >, 4 > Records
LLVM_ABI Error decompress(ArrayRef< uint8_t > Input, uint8_t *Output, size_t &UncompressedSize)
LLVM_ABI bool isAvailable()
std::variant< std::monostate, DecisionParameters, BranchParameters > Parameters
The type of MC/DC-specific parameters.
Definition MCDCTypes.h:56
constexpr uint64_t TestingFormatMagic
LLVM_ABI BuildIDRef getBuildID(const ObjectFile *Obj)
Returns the build ID, if any, contained in the given object file.
Definition BuildID.cpp:70
ArrayRef< uint8_t > BuildIDRef
A reference to a BuildID in binary form.
Definition BuildID.h:29
LLVM_ABI Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr, bool InitContent=true)
Create a Binary from Source, autodetecting the file type.
Definition Binary.cpp:45
value_type byte_swap(value_type value, endianness endian)
Definition Endian.h:44
LLVM_ABI bool remove_dots(SmallVectorImpl< char > &path, bool remove_dot_dot=false, Style style=Style::native)
Remove '.
Definition Path.cpp:769
LLVM_ABI bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition Path.cpp:678
LLVM_ABI void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition Path.cpp:457
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:557
@ Length
Definition DWP.cpp:557
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
ArrayRef< CharT > arrayRefFromStringRef(StringRef Input)
Construct a string ref from an array ref of unsigned chars.
FunctionAddr VTableAddr uintptr_t uintptr_t FilenamesSize
Definition InstrProf.h:330
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition Error.h:1013
FunctionAddr VTableAddr uintptr_t uintptr_t CoverageSize
Definition InstrProf.h:332
uint64_t decodeULEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a ULEB128 value.
Definition LEB128.h:130
FuncHash
Definition InstrProf.h:78
uint64_t offsetToAlignedAddr(const void *Addr, Align Alignment)
Returns the necessary adjustment for aligning Addr to Alignment bytes, rounding up.
Definition Alignment.h:192
LLVM_ABI std::string getInstrProfSectionName(InstrProfSectKind IPSK, Triple::ObjectFormatType OF, bool AddSegmentInfo=true)
Return the name of the profile section corresponding to IPSK.
FunctionAddr VTableAddr uintptr_t uintptr_t Version
Definition InstrProf.h:334
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
InstrProfSectKind
Definition InstrProf.h:91
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:221
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1884
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition Error.cpp:107
endianness
Definition bit.h:71
FunctionAddr VTableAddr uintptr_t uintptr_t NRecords
Definition InstrProf.h:328
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1106
StringRef toStringRef(bool B)
Construct a string ref from a boolean.
bool isAddrAligned(Align Lhs, const void *Addr)
Checks that Addr is a multiple of the alignment.
Definition Alignment.h:139
uintptr_t alignAddr(const void *Addr, Align Alignment)
Aligns Addr to Alignment bytes, rounding up.
Definition Alignment.h:176
#define N
#define NC
Definition regutils.h:42
FilenameRange(unsigned StartingIndex, unsigned Length)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
A Counter expression is a value that represents an arithmetic operation with two counters.
A Counter mapping region associates a source range with a specific counter.
@ ExpansionRegion
An ExpansionRegion represents a file expansion region that associates a source range with the expansi...
@ MCDCDecisionRegion
A DecisionRegion represents a top-level boolean expression and is associated with a variable length b...
@ MCDCBranchRegion
A Branch Region can be extended to include IDs to facilitate MC/DC.
@ SkippedRegion
A SkippedRegion represents a source range with code that was skipped by a preprocessor or similar mea...
@ GapRegion
A GapRegion is like a CodeRegion, but its count is only set as the line execution count when its the ...
@ BranchRegion
A BranchRegion represents leaf-level boolean expressions and is associated with two counters,...
@ CodeRegion
A CodeRegion associates some code with a counter.
A Counter is an abstract value that describes how to compute the execution count for a region of code...
static const unsigned EncodingTagBits
static Counter getZero()
Return the counter that represents the number zero.
static Counter getCounter(unsigned CounterId)
Return the counter that corresponds to a specific profile counter.
static const unsigned EncodingCounterTagAndExpansionRegionTagBits
static const unsigned EncodingTagMask
static Counter getExpression(unsigned ExpressionId)
Return the counter that corresponds to a specific addition counter expression.
CovMapFunctionRecordV3 CovMapFuncRecordType
Coverage mapping information for a single function.