aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp')
-rw-r--r--llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp239
1 files changed, 159 insertions, 80 deletions
diff --git a/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp b/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp
index 05737323314a..d6aade6fcd0f 100644
--- a/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp
+++ b/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp
@@ -68,7 +68,8 @@ Error RawCoverageReader::readULEB128(uint64_t &Result) {
unsigned N = 0;
Result = decodeULEB128(Data.bytes_begin(), &N);
if (N > Data.size())
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(coveragemap_error::malformed,
+ "the size of ULEB128 is too big");
Data = Data.substr(N);
return Error::success();
}
@@ -77,7 +78,9 @@ Error RawCoverageReader::readIntMax(uint64_t &Result, uint64_t MaxPlus1) {
if (auto Err = readULEB128(Result))
return Err;
if (Result >= MaxPlus1)
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(
+ coveragemap_error::malformed,
+ "the value of ULEB128 is greater than or equal to MaxPlus1");
return Error::success();
}
@@ -85,7 +88,8 @@ Error RawCoverageReader::readSize(uint64_t &Result) {
if (auto Err = readULEB128(Result))
return Err;
if (Result > Data.size())
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(coveragemap_error::malformed,
+ "the value of ULEB128 is too big");
return Error::success();
}
@@ -103,7 +107,8 @@ Error RawCoverageFilenamesReader::read(CovMapVersion Version) {
if (auto Err = readSize(NumFilenames))
return Err;
if (!NumFilenames)
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(coveragemap_error::malformed,
+ "number of filenames is zero");
if (Version < CovMapVersion::Version4)
return readUncompressed(Version, NumFilenames);
@@ -201,13 +206,15 @@ Error RawCoverageMappingReader::decodeCounter(unsigned Value, Counter &C) {
case CounterExpression::Add: {
auto ID = Value >> Counter::EncodingTagBits;
if (ID >= Expressions.size())
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(coveragemap_error::malformed,
+ "counter expression is invalid");
Expressions[ID].Kind = CounterExpression::ExprKind(Tag);
C = Counter::getExpression(ID);
break;
}
default:
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(coveragemap_error::malformed,
+ "counter expression kind is invalid");
}
return Error::success();
}
@@ -268,7 +275,8 @@ Error RawCoverageMappingReader::readMappingRegionsSubArray(
ExpandedFileID = EncodedCounterAndRegion >>
Counter::EncodingCounterTagAndExpansionRegionTagBits;
if (ExpandedFileID >= NumFileIDs)
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(coveragemap_error::malformed,
+ "ExpandedFileID is invalid");
} else {
switch (EncodedCounterAndRegion >>
Counter::EncodingCounterTagAndExpansionRegionTagBits) {
@@ -287,7 +295,8 @@ Error RawCoverageMappingReader::readMappingRegionsSubArray(
return Err;
break;
default:
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(coveragemap_error::malformed,
+ "region kind is incorrect");
}
}
}
@@ -300,7 +309,8 @@ Error RawCoverageMappingReader::readMappingRegionsSubArray(
if (auto Err = readULEB128(ColumnStart))
return Err;
if (ColumnStart > std::numeric_limits<unsigned>::max())
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(coveragemap_error::malformed,
+ "start column is too big");
if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max()))
return Err;
if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max()))
@@ -341,7 +351,9 @@ Error RawCoverageMappingReader::readMappingRegionsSubArray(
LineStart, ColumnStart,
LineStart + NumLines, ColumnEnd, Kind);
if (CMR.startLoc() > CMR.endLoc())
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(
+ coveragemap_error::malformed,
+ "counter mapping region locations are incorrect");
MappingRegions.push_back(CMR);
}
return Error::success();
@@ -523,7 +535,7 @@ struct CovMapFuncRecordReader {
const char *OutOfLineMappingBuf,
const char *OutOfLineMappingBufEnd) = 0;
- template <class IntPtrT, support::endianness Endian>
+ template <class IntPtrT, llvm::endianness Endian>
static Expected<std::unique_ptr<CovMapFuncRecordReader>>
get(CovMapVersion Version, InstrProfSymtab &P,
std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, StringRef D,
@@ -531,7 +543,7 @@ struct CovMapFuncRecordReader {
};
// A class for reading coverage mapping function records for a module.
-template <CovMapVersion Version, class IntPtrT, support::endianness Endian>
+template <CovMapVersion Version, class IntPtrT, llvm::endianness Endian>
class VersionedCovMapFuncRecordReader : public CovMapFuncRecordReader {
using FuncRecordType =
typename CovMapTraits<Version, IntPtrT>::CovMapFuncRecordType;
@@ -613,7 +625,9 @@ public:
using namespace support;
if (CovBuf + sizeof(CovMapHeader) > CovBufEnd)
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(
+ coveragemap_error::malformed,
+ "coverage mapping header section is larger than buffer size");
auto CovHeader = reinterpret_cast<const CovMapHeader *>(CovBuf);
uint32_t NRecords = CovHeader->getNRecords<Endian>();
uint32_t FilenamesSize = CovHeader->getFilenamesSize<Endian>();
@@ -634,7 +648,9 @@ public:
// Get the filenames.
if (CovBuf + FilenamesSize > CovBufEnd)
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(
+ coveragemap_error::malformed,
+ "filenames section is larger than buffer size");
size_t FilenamesBegin = Filenames.size();
StringRef FilenameRegion(CovBuf, FilenamesSize);
RawCoverageFilenamesReader Reader(FilenameRegion, Filenames,
@@ -673,12 +689,15 @@ public:
// coverage header).
const char *MappingBuf = CovBuf;
if (Version >= CovMapVersion::Version4 && CoverageSize != 0)
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(coveragemap_error::malformed,
+ "coverage mapping size is not zero");
CovBuf += CoverageSize;
const char *MappingEnd = CovBuf;
if (CovBuf > CovBufEnd)
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(
+ coveragemap_error::malformed,
+ "function records section is larger than buffer size");
if (Version < CovMapVersion::Version4) {
// Read each function record.
@@ -707,7 +726,9 @@ public:
CFR->template advanceByOne<Endian>(OutOfLineMappingBuf);
if (Version < CovMapVersion::Version4)
if (NextMappingBuf > OutOfLineMappingBufEnd)
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(
+ coveragemap_error::malformed,
+ "next mapping buffer is larger than buffer size");
// Look up the set of filenames associated with this function record.
std::optional<FilenameRange> FileRange;
@@ -717,7 +738,10 @@ public:
uint64_t FilenamesRef = CFR->template getFilenamesRef<Endian>();
auto It = FileRangeMap.find(FilenamesRef);
if (It == FileRangeMap.end())
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(
+ coveragemap_error::malformed,
+ "no filename found for function with hash=0x" +
+ Twine::utohexstr(FilenamesRef));
else
FileRange = It->getSecond();
}
@@ -728,7 +752,9 @@ public:
CFR->template getCoverageMapping<Endian>(OutOfLineMappingBuf);
if (Version >= CovMapVersion::Version4 &&
Mapping.data() + Mapping.size() > FuncRecBufEnd)
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(
+ coveragemap_error::malformed,
+ "coverage mapping data is larger than buffer size");
if (Error Err = insertFunctionRecordIfNeeded(CFR, Mapping, *FileRange))
return Err;
}
@@ -741,7 +767,7 @@ public:
} // end anonymous namespace
-template <class IntPtrT, support::endianness Endian>
+template <class IntPtrT, llvm::endianness Endian>
Expected<std::unique_ptr<CovMapFuncRecordReader>> CovMapFuncRecordReader::get(
CovMapVersion Version, InstrProfSymtab &P,
std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, StringRef D,
@@ -757,6 +783,7 @@ Expected<std::unique_ptr<CovMapFuncRecordReader>> CovMapFuncRecordReader::get(
case CovMapVersion::Version4:
case CovMapVersion::Version5:
case CovMapVersion::Version6:
+ case CovMapVersion::Version7:
// Decompress the name data.
if (Error E = P.create(P.getNameData()))
return std::move(E);
@@ -775,11 +802,14 @@ Expected<std::unique_ptr<CovMapFuncRecordReader>> CovMapFuncRecordReader::get(
else if (Version == CovMapVersion::Version6)
return std::make_unique<VersionedCovMapFuncRecordReader<
CovMapVersion::Version6, IntPtrT, Endian>>(P, R, D, F);
+ else if (Version == CovMapVersion::Version7)
+ return std::make_unique<VersionedCovMapFuncRecordReader<
+ CovMapVersion::Version7, IntPtrT, Endian>>(P, R, D, F);
}
llvm_unreachable("Unsupported version");
}
-template <typename T, support::endianness Endian>
+template <typename T, llvm::endianness Endian>
static Error readCoverageMappingData(
InstrProfSymtab &ProfileNames, StringRef CovMap, StringRef FuncRecords,
std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records,
@@ -822,105 +852,146 @@ static Error readCoverageMappingData(
return Error::success();
}
-static const char *TestingFormatMagic = "llvmcovmtestdata";
-
Expected<std::unique_ptr<BinaryCoverageReader>>
BinaryCoverageReader::createCoverageReaderFromBuffer(
StringRef Coverage, FuncRecordsStorage &&FuncRecords,
InstrProfSymtab &&ProfileNames, uint8_t BytesInAddress,
- support::endianness Endian, StringRef CompilationDir) {
+ llvm::endianness Endian, StringRef CompilationDir) {
std::unique_ptr<BinaryCoverageReader> Reader(
new BinaryCoverageReader(std::move(FuncRecords)));
Reader->ProfileNames = std::move(ProfileNames);
StringRef FuncRecordsRef = Reader->FuncRecords->getBuffer();
- if (BytesInAddress == 4 && Endian == support::endianness::little) {
- if (Error E =
- readCoverageMappingData<uint32_t, support::endianness::little>(
- Reader->ProfileNames, Coverage, FuncRecordsRef,
- Reader->MappingRecords, CompilationDir, Reader->Filenames))
+ if (BytesInAddress == 4 && Endian == llvm::endianness::little) {
+ if (Error E = readCoverageMappingData<uint32_t, llvm::endianness::little>(
+ Reader->ProfileNames, Coverage, FuncRecordsRef,
+ Reader->MappingRecords, CompilationDir, Reader->Filenames))
return std::move(E);
- } else if (BytesInAddress == 4 && Endian == support::endianness::big) {
- if (Error E = readCoverageMappingData<uint32_t, support::endianness::big>(
+ } else if (BytesInAddress == 4 && Endian == llvm::endianness::big) {
+ if (Error E = readCoverageMappingData<uint32_t, llvm::endianness::big>(
Reader->ProfileNames, Coverage, FuncRecordsRef,
Reader->MappingRecords, CompilationDir, Reader->Filenames))
return std::move(E);
- } else if (BytesInAddress == 8 && Endian == support::endianness::little) {
- if (Error E =
- readCoverageMappingData<uint64_t, support::endianness::little>(
- Reader->ProfileNames, Coverage, FuncRecordsRef,
- Reader->MappingRecords, CompilationDir, Reader->Filenames))
+ } else if (BytesInAddress == 8 && Endian == llvm::endianness::little) {
+ if (Error E = readCoverageMappingData<uint64_t, llvm::endianness::little>(
+ Reader->ProfileNames, Coverage, FuncRecordsRef,
+ Reader->MappingRecords, CompilationDir, Reader->Filenames))
return std::move(E);
- } else if (BytesInAddress == 8 && Endian == support::endianness::big) {
- if (Error E = readCoverageMappingData<uint64_t, support::endianness::big>(
+ } else if (BytesInAddress == 8 && Endian == llvm::endianness::big) {
+ if (Error E = readCoverageMappingData<uint64_t, llvm::endianness::big>(
Reader->ProfileNames, Coverage, FuncRecordsRef,
Reader->MappingRecords, CompilationDir, Reader->Filenames))
return std::move(E);
} else
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(
+ coveragemap_error::malformed,
+ "not supported endianness or bytes in address");
return std::move(Reader);
}
static Expected<std::unique_ptr<BinaryCoverageReader>>
loadTestingFormat(StringRef Data, StringRef CompilationDir) {
uint8_t BytesInAddress = 8;
- support::endianness Endian = support::endianness::little;
+ llvm::endianness Endian = llvm::endianness::little;
- Data = Data.substr(StringRef(TestingFormatMagic).size());
+ // Read the magic and version.
+ Data = Data.substr(sizeof(TestingFormatMagic));
+ if (Data.size() < sizeof(uint64_t))
+ return make_error<CoverageMapError>(coveragemap_error::malformed,
+ "the size of data is too small");
+ auto TestingVersion =
+ support::endian::byte_swap<uint64_t, llvm::endianness::little>(
+ *reinterpret_cast<const uint64_t *>(Data.data()));
+ Data = Data.substr(sizeof(uint64_t));
+
+ // Read the ProfileNames data.
if (Data.empty())
return make_error<CoverageMapError>(coveragemap_error::truncated);
unsigned N = 0;
uint64_t ProfileNamesSize = decodeULEB128(Data.bytes_begin(), &N);
if (N > Data.size())
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(
+ coveragemap_error::malformed,
+ "the size of TestingFormatMagic is too big");
Data = Data.substr(N);
if (Data.empty())
return make_error<CoverageMapError>(coveragemap_error::truncated);
N = 0;
uint64_t Address = decodeULEB128(Data.bytes_begin(), &N);
if (N > Data.size())
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(coveragemap_error::malformed,
+ "the size of ULEB128 is too big");
Data = Data.substr(N);
if (Data.size() < ProfileNamesSize)
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(coveragemap_error::malformed,
+ "the size of ProfileNames is too big");
InstrProfSymtab ProfileNames;
if (Error E = ProfileNames.create(Data.substr(0, ProfileNamesSize), Address))
return std::move(E);
Data = Data.substr(ProfileNamesSize);
+
+ // In Version2, the size of CoverageMapping is stored directly.
+ uint64_t CoverageMappingSize;
+ if (TestingVersion == uint64_t(TestingFormatVersion::Version2)) {
+ N = 0;
+ CoverageMappingSize = decodeULEB128(Data.bytes_begin(), &N);
+ if (N > Data.size())
+ return make_error<CoverageMapError>(coveragemap_error::malformed,
+ "the size of ULEB128 is too big");
+ Data = Data.substr(N);
+ if (CoverageMappingSize < sizeof(CovMapHeader))
+ return make_error<CoverageMapError>(
+ coveragemap_error::malformed,
+ "the size of CoverageMapping is teoo small");
+ } else if (TestingVersion != uint64_t(TestingFormatVersion::Version1)) {
+ return make_error<CoverageMapError>(coveragemap_error::unsupported_version);
+ }
+
// Skip the padding bytes because coverage map data has an alignment of 8.
- size_t Pad = offsetToAlignedAddr(Data.data(), Align(8));
+ auto Pad = offsetToAlignedAddr(Data.data(), Align(8));
if (Data.size() < Pad)
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(coveragemap_error::malformed,
+ "insufficient padding");
Data = Data.substr(Pad);
if (Data.size() < sizeof(CovMapHeader))
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(
+ coveragemap_error::malformed,
+ "coverage mapping header section is larger than data size");
auto const *CovHeader = reinterpret_cast<const CovMapHeader *>(
Data.substr(0, sizeof(CovMapHeader)).data());
- CovMapVersion Version =
- (CovMapVersion)CovHeader->getVersion<support::endianness::little>();
- StringRef CoverageMapping;
- BinaryCoverageReader::FuncRecordsStorage CoverageRecords;
+ auto Version =
+ CovMapVersion(CovHeader->getVersion<llvm::endianness::little>());
+
+ // In Version1, the size of CoverageMapping is calculated.
+ if (TestingVersion == uint64_t(TestingFormatVersion::Version1)) {
+ if (Version < CovMapVersion::Version4) {
+ CoverageMappingSize = Data.size();
+ } else {
+ auto FilenamesSize =
+ CovHeader->getFilenamesSize<llvm::endianness::little>();
+ CoverageMappingSize = sizeof(CovMapHeader) + FilenamesSize;
+ }
+ }
+
+ auto CoverageMapping = Data.substr(0, CoverageMappingSize);
+ Data = Data.substr(CoverageMappingSize);
+
+ // Read the CoverageRecords data.
if (Version < CovMapVersion::Version4) {
- CoverageMapping = Data;
- if (CoverageMapping.empty())
- return make_error<CoverageMapError>(coveragemap_error::truncated);
- CoverageRecords = MemoryBuffer::getMemBuffer("");
+ if (!Data.empty())
+ return make_error<CoverageMapError>(coveragemap_error::malformed,
+ "data is not empty");
} else {
- uint32_t FilenamesSize =
- CovHeader->getFilenamesSize<support::endianness::little>();
- uint32_t CoverageMappingSize = sizeof(CovMapHeader) + FilenamesSize;
- CoverageMapping = Data.substr(0, CoverageMappingSize);
- if (CoverageMapping.empty())
- return make_error<CoverageMapError>(coveragemap_error::truncated);
- Data = Data.substr(CoverageMappingSize);
// Skip the padding bytes because coverage records data has an alignment
// of 8.
Pad = offsetToAlignedAddr(Data.data(), Align(8));
if (Data.size() < Pad)
- return make_error<CoverageMapError>(coveragemap_error::malformed);
- CoverageRecords = MemoryBuffer::getMemBuffer(Data.substr(Pad));
- if (CoverageRecords->getBufferSize() == 0)
- return make_error<CoverageMapError>(coveragemap_error::truncated);
+ return make_error<CoverageMapError>(coveragemap_error::malformed,
+ "insufficient padding");
+ Data = Data.substr(Pad);
}
+ BinaryCoverageReader::FuncRecordsStorage CoverageRecords =
+ MemoryBuffer::getMemBuffer(Data);
+
return BinaryCoverageReader::createCoverageReaderFromBuffer(
CoverageMapping, std::move(CoverageRecords), std::move(ProfileNames),
BytesInAddress, Endian, CompilationDir);
@@ -972,13 +1043,13 @@ loadBinaryFormat(std::unique_ptr<Binary> Bin, StringRef Arch,
return errorCodeToError(object_error::arch_not_found);
} else
// We can only handle object files.
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(coveragemap_error::malformed,
+ "binary is not an object file");
// The coverage uses native pointer sizes for the object it's written in.
uint8_t BytesInAddress = OF->getBytesInAddress();
- support::endianness Endian = OF->isLittleEndian()
- ? support::endianness::little
- : support::endianness::big;
+ llvm::endianness Endian =
+ OF->isLittleEndian() ? llvm::endianness::little : llvm::endianness::big;
// Look for the sections that we are interested in.
auto ObjFormat = OF->getTripleObjectFormat();
@@ -994,7 +1065,8 @@ loadBinaryFormat(std::unique_ptr<Binary> Bin, StringRef Arch,
return std::move(E);
std::vector<SectionRef> CoverageSectionRefs = *CoverageSection;
if (CoverageSectionRefs.size() != 1)
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(coveragemap_error::malformed,
+ "the size of name section is not one");
auto CoverageMappingOrErr = CoverageSectionRefs.back().getContents();
if (!CoverageMappingOrErr)
return CoverageMappingOrErr.takeError();
@@ -1003,7 +1075,9 @@ loadBinaryFormat(std::unique_ptr<Binary> Bin, StringRef Arch,
InstrProfSymtab ProfileNames;
std::vector<SectionRef> NamesSectionRefs = *NamesSection;
if (NamesSectionRefs.size() != 1)
- return make_error<CoverageMapError>(coveragemap_error::malformed);
+ return make_error<CoverageMapError>(
+ coveragemap_error::malformed,
+ "the size of coverage mapping section is not one");
if (Error E = ProfileNames.create(NamesSectionRefs.back()))
return std::move(E);
@@ -1081,14 +1155,19 @@ BinaryCoverageReader::create(
StringRef CompilationDir, SmallVectorImpl<object::BuildIDRef> *BinaryIDs) {
std::vector<std::unique_ptr<BinaryCoverageReader>> Readers;
- if (ObjectBuffer.getBuffer().startswith(TestingFormatMagic)) {
- // This is a special format used for testing.
- auto ReaderOrErr =
- loadTestingFormat(ObjectBuffer.getBuffer(), CompilationDir);
- if (!ReaderOrErr)
- return ReaderOrErr.takeError();
- Readers.push_back(std::move(ReaderOrErr.get()));
- return std::move(Readers);
+ if (ObjectBuffer.getBuffer().size() > sizeof(TestingFormatMagic)) {
+ uint64_t Magic =
+ support::endian::byte_swap<uint64_t, llvm::endianness::little>(
+ *reinterpret_cast<const uint64_t *>(ObjectBuffer.getBufferStart()));
+ if (Magic == TestingFormatMagic) {
+ // This is a special format used for testing.
+ auto ReaderOrErr =
+ loadTestingFormat(ObjectBuffer.getBuffer(), CompilationDir);
+ if (!ReaderOrErr)
+ return ReaderOrErr.takeError();
+ Readers.push_back(std::move(ReaderOrErr.get()));
+ return std::move(Readers);
+ }
}
auto BinOrErr = createBinary(ObjectBuffer);