1 //=-- CoverageMappingReader.cpp - Code coverage mapping reader ----*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for reading coverage mapping data for
11 // instrumentation based coverage.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ProfileData/CoverageMappingReader.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/Object/ObjectFile.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/LEB128.h"
22 using namespace coverage;
23 using namespace object;
25 #define DEBUG_TYPE "coverage-mapping"
27 void CoverageMappingIterator::increment() {
28 // Check if all the records were read or if an error occurred while reading
30 if (Reader->readNextRecord(Record))
31 *this = CoverageMappingIterator();
34 std::error_code RawCoverageReader::readULEB128(uint64_t &Result) {
36 return error(instrprof_error::truncated);
38 Result = decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
40 return error(instrprof_error::malformed);
41 Data = Data.substr(N);
45 std::error_code RawCoverageReader::readIntMax(uint64_t &Result,
47 if (auto Err = readULEB128(Result))
49 if (Result >= MaxPlus1)
50 return error(instrprof_error::malformed);
54 std::error_code RawCoverageReader::readSize(uint64_t &Result) {
55 if (auto Err = readULEB128(Result))
57 // Sanity check the number.
58 if (Result > Data.size())
59 return error(instrprof_error::malformed);
63 std::error_code RawCoverageReader::readString(StringRef &Result) {
65 if (auto Err = readSize(Length))
67 Result = Data.substr(0, Length);
68 Data = Data.substr(Length);
72 std::error_code RawCoverageFilenamesReader::read() {
73 uint64_t NumFilenames;
74 if (auto Err = readSize(NumFilenames))
76 for (size_t I = 0; I < NumFilenames; ++I) {
78 if (auto Err = readString(Filename))
80 Filenames.push_back(Filename);
85 std::error_code RawCoverageMappingReader::decodeCounter(unsigned Value,
87 auto Tag = Value & Counter::EncodingTagMask;
90 C = Counter::getZero();
92 case Counter::CounterValueReference:
93 C = Counter::getCounter(Value >> Counter::EncodingTagBits);
98 Tag -= Counter::Expression;
100 case CounterExpression::Subtract:
101 case CounterExpression::Add: {
102 auto ID = Value >> Counter::EncodingTagBits;
103 if (ID >= Expressions.size())
104 return error(instrprof_error::malformed);
105 Expressions[ID].Kind = CounterExpression::ExprKind(Tag);
106 C = Counter::getExpression(ID);
110 return error(instrprof_error::malformed);
115 std::error_code RawCoverageMappingReader::readCounter(Counter &C) {
116 uint64_t EncodedCounter;
118 readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max()))
120 if (auto Err = decodeCounter(EncodedCounter, C))
125 static const unsigned EncodingExpansionRegionBit = 1
126 << Counter::EncodingTagBits;
128 /// \brief Read the sub-array of regions for the given inferred file id.
129 /// \param NumFileIDs the number of file ids that are defined for this
131 std::error_code RawCoverageMappingReader::readMappingRegionsSubArray(
132 std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID,
135 if (auto Err = readSize(NumRegions))
137 unsigned LineStart = 0;
138 for (size_t I = 0; I < NumRegions; ++I) {
140 CounterMappingRegion::RegionKind Kind = CounterMappingRegion::CodeRegion;
142 // Read the combined counter + region kind.
143 uint64_t EncodedCounterAndRegion;
144 if (auto Err = readIntMax(EncodedCounterAndRegion,
145 std::numeric_limits<unsigned>::max()))
147 unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
148 uint64_t ExpandedFileID = 0;
149 if (Tag != Counter::Zero) {
150 if (auto Err = decodeCounter(EncodedCounterAndRegion, C))
153 // Is it an expansion region?
154 if (EncodedCounterAndRegion & EncodingExpansionRegionBit) {
155 Kind = CounterMappingRegion::ExpansionRegion;
156 ExpandedFileID = EncodedCounterAndRegion >>
157 Counter::EncodingCounterTagAndExpansionRegionTagBits;
158 if (ExpandedFileID >= NumFileIDs)
159 return error(instrprof_error::malformed);
161 switch (EncodedCounterAndRegion >>
162 Counter::EncodingCounterTagAndExpansionRegionTagBits) {
163 case CounterMappingRegion::CodeRegion:
164 // Don't do anything when we have a code region with a zero counter.
166 case CounterMappingRegion::SkippedRegion:
167 Kind = CounterMappingRegion::SkippedRegion;
170 return error(instrprof_error::malformed);
175 // Read the source range.
176 uint64_t LineStartDelta, ColumnStart, NumLines, ColumnEnd;
178 readIntMax(LineStartDelta, std::numeric_limits<unsigned>::max()))
180 if (auto Err = readULEB128(ColumnStart))
182 if (ColumnStart > std::numeric_limits<unsigned>::max())
183 return error(instrprof_error::malformed);
184 if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max()))
186 if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max()))
188 LineStart += LineStartDelta;
189 // Adjust the column locations for the empty regions that are supposed to
190 // cover whole lines. Those regions should be encoded with the
191 // column range (1 -> std::numeric_limits<unsigned>::max()), but because
192 // the encoded std::numeric_limits<unsigned>::max() is several bytes long,
193 // we set the column range to (0 -> 0) to ensure that the column start and
194 // column end take up one byte each.
195 // The std::numeric_limits<unsigned>::max() is used to represent a column
196 // position at the end of the line without knowing the length of that line.
197 if (ColumnStart == 0 && ColumnEnd == 0) {
199 ColumnEnd = std::numeric_limits<unsigned>::max();
203 dbgs() << "Counter in file " << InferredFileID << " " << LineStart << ":"
204 << ColumnStart << " -> " << (LineStart + NumLines) << ":"
205 << ColumnEnd << ", ";
206 if (Kind == CounterMappingRegion::ExpansionRegion)
207 dbgs() << "Expands to file " << ExpandedFileID;
209 CounterMappingContext(Expressions).dump(C, dbgs());
213 MappingRegions.push_back(CounterMappingRegion(
214 C, InferredFileID, ExpandedFileID, LineStart, ColumnStart,
215 LineStart + NumLines, ColumnEnd, Kind));
220 std::error_code RawCoverageMappingReader::read() {
222 // Read the virtual file mapping.
223 llvm::SmallVector<unsigned, 8> VirtualFileMapping;
224 uint64_t NumFileMappings;
225 if (auto Err = readSize(NumFileMappings))
227 for (size_t I = 0; I < NumFileMappings; ++I) {
228 uint64_t FilenameIndex;
229 if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size()))
231 VirtualFileMapping.push_back(FilenameIndex);
234 // Construct the files using unique filenames and virtual file mapping.
235 for (auto I : VirtualFileMapping) {
236 Filenames.push_back(TranslationUnitFilenames[I]);
239 // Read the expressions.
240 uint64_t NumExpressions;
241 if (auto Err = readSize(NumExpressions))
243 // Create an array of dummy expressions that get the proper counters
244 // when the expressions are read, and the proper kinds when the counters
248 CounterExpression(CounterExpression::Subtract, Counter(), Counter()));
249 for (size_t I = 0; I < NumExpressions; ++I) {
250 if (auto Err = readCounter(Expressions[I].LHS))
252 if (auto Err = readCounter(Expressions[I].RHS))
256 // Read the mapping regions sub-arrays.
257 for (unsigned InferredFileID = 0, S = VirtualFileMapping.size();
258 InferredFileID < S; ++InferredFileID) {
259 if (auto Err = readMappingRegionsSubArray(MappingRegions, InferredFileID,
260 VirtualFileMapping.size()))
264 // Set the counters for the expansion regions.
265 // i.e. Counter of expansion region = counter of the first region
266 // from the expanded file.
267 // Perform multiple passes to correctly propagate the counters through
268 // all the nested expansion regions.
269 SmallVector<CounterMappingRegion *, 8> FileIDExpansionRegionMapping;
270 FileIDExpansionRegionMapping.resize(VirtualFileMapping.size(), nullptr);
271 for (unsigned Pass = 1, S = VirtualFileMapping.size(); Pass < S; ++Pass) {
272 for (auto &R : MappingRegions) {
273 if (R.Kind != CounterMappingRegion::ExpansionRegion)
275 assert(!FileIDExpansionRegionMapping[R.ExpandedFileID]);
276 FileIDExpansionRegionMapping[R.ExpandedFileID] = &R;
278 for (auto &R : MappingRegions) {
279 if (FileIDExpansionRegionMapping[R.FileID]) {
280 FileIDExpansionRegionMapping[R.FileID]->Count = R.Count;
281 FileIDExpansionRegionMapping[R.FileID] = nullptr;
289 ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader(
292 auto File = llvm::object::ObjectFile::createObjectFile(FileName);
294 error(File.getError());
296 Object = std::move(File.get());
300 /// \brief The coverage mapping data for a single function.
301 /// It points to the function's name.
302 template <typename IntPtrT> struct CoverageMappingFunctionRecord {
303 IntPtrT FunctionNamePtr;
304 uint32_t FunctionNameSize;
305 uint32_t CoverageMappingSize;
306 uint64_t FunctionHash;
309 /// \brief The coverage mapping data for a single translation unit.
310 /// It points to the array of function coverage mapping records and the encoded
312 template <typename IntPtrT> struct CoverageMappingTURecord {
313 uint32_t FunctionRecordsSize;
314 uint32_t FilenamesSize;
315 uint32_t CoverageMappingsSize;
319 /// \brief A helper structure to access the data from a section
320 /// in an object file.
325 std::error_code load(SectionRef &Section) {
326 if (auto Err = Section.getContents(Data))
328 Address = Section.getAddress();
329 return instrprof_error::success;
332 std::error_code get(uint64_t Pointer, size_t Size, StringRef &Result) {
333 if (Pointer < Address)
334 return instrprof_error::malformed;
335 auto Offset = Pointer - Address;
336 if (Offset + Size > Data.size())
337 return instrprof_error::malformed;
338 Result = Data.substr(Pointer - Address, Size);
339 return instrprof_error::success;
344 template <typename T>
345 std::error_code readCoverageMappingData(
346 SectionData &ProfileNames, StringRef Data,
347 std::vector<ObjectFileCoverageMappingReader::ProfileMappingRecord> &Records,
348 std::vector<StringRef> &Filenames) {
349 llvm::DenseSet<T> UniqueFunctionMappingData;
351 // Read the records in the coverage data section.
352 while (!Data.empty()) {
353 if (Data.size() < sizeof(CoverageMappingTURecord<T>))
354 return instrprof_error::malformed;
355 auto TU = reinterpret_cast<const CoverageMappingTURecord<T> *>(Data.data());
356 Data = Data.substr(sizeof(CoverageMappingTURecord<T>));
357 switch (TU->Version) {
358 case CoverageMappingVersion1:
361 return instrprof_error::unsupported_version;
363 auto Version = CoverageMappingVersion(TU->Version);
365 // Get the function records.
366 auto FunctionRecords =
367 reinterpret_cast<const CoverageMappingFunctionRecord<T> *>(Data.data());
369 sizeof(CoverageMappingFunctionRecord<T>) * TU->FunctionRecordsSize)
370 return instrprof_error::malformed;
371 Data = Data.substr(sizeof(CoverageMappingFunctionRecord<T>) *
372 TU->FunctionRecordsSize);
374 // Get the filenames.
375 if (Data.size() < TU->FilenamesSize)
376 return instrprof_error::malformed;
377 auto RawFilenames = Data.substr(0, TU->FilenamesSize);
378 Data = Data.substr(TU->FilenamesSize);
379 size_t FilenamesBegin = Filenames.size();
380 RawCoverageFilenamesReader Reader(RawFilenames, Filenames);
381 if (auto Err = Reader.read())
384 // Get the coverage mappings.
385 if (Data.size() < TU->CoverageMappingsSize)
386 return instrprof_error::malformed;
387 auto CoverageMappings = Data.substr(0, TU->CoverageMappingsSize);
388 Data = Data.substr(TU->CoverageMappingsSize);
390 for (unsigned I = 0; I < TU->FunctionRecordsSize; ++I) {
391 auto &MappingRecord = FunctionRecords[I];
393 // Get the coverage mapping.
394 if (CoverageMappings.size() < MappingRecord.CoverageMappingSize)
395 return instrprof_error::malformed;
397 CoverageMappings.substr(0, MappingRecord.CoverageMappingSize);
399 CoverageMappings.substr(MappingRecord.CoverageMappingSize);
401 // Ignore this record if we already have a record that points to the same
403 // This is useful to ignore the redundant records for the functions
405 if (!UniqueFunctionMappingData.insert(MappingRecord.FunctionNamePtr)
408 StringRef FunctionName;
410 ProfileNames.get(MappingRecord.FunctionNamePtr,
411 MappingRecord.FunctionNameSize, FunctionName))
413 Records.push_back(ObjectFileCoverageMappingReader::ProfileMappingRecord(
414 Version, FunctionName, MappingRecord.FunctionHash, Mapping,
415 FilenamesBegin, Filenames.size() - FilenamesBegin));
419 return instrprof_error::success;
422 static const char *TestingFormatMagic = "llvmcovmtestdata";
424 static std::error_code decodeTestingFormat(StringRef Data,
425 SectionData &ProfileNames,
426 StringRef &CoverageMapping) {
427 Data = Data.substr(StringRef(TestingFormatMagic).size());
429 return instrprof_error::truncated;
431 auto ProfileNamesSize =
432 decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
434 return instrprof_error::malformed;
435 Data = Data.substr(N);
437 return instrprof_error::truncated;
439 ProfileNames.Address =
440 decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
442 return instrprof_error::malformed;
443 Data = Data.substr(N);
444 if (Data.size() < ProfileNamesSize)
445 return instrprof_error::malformed;
446 ProfileNames.Data = Data.substr(0, ProfileNamesSize);
447 CoverageMapping = Data.substr(ProfileNamesSize);
448 return instrprof_error::success;
451 ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader(
452 std::unique_ptr<MemoryBuffer> &ObjectBuffer, sys::fs::file_magic Type)
454 if (ObjectBuffer->getBuffer().startswith(TestingFormatMagic)) {
455 // This is a special format used for testing.
456 SectionData ProfileNames;
457 StringRef CoverageMapping;
458 if (auto Err = decodeTestingFormat(ObjectBuffer->getBuffer(), ProfileNames,
463 error(readCoverageMappingData<uint64_t>(ProfileNames, CoverageMapping,
464 MappingRecords, Filenames));
465 Object = OwningBinary<ObjectFile>(std::unique_ptr<ObjectFile>(),
466 std::move(ObjectBuffer));
470 auto File = object::ObjectFile::createObjectFile(
471 ObjectBuffer->getMemBufferRef(), Type);
473 error(File.getError());
475 Object = OwningBinary<ObjectFile>(std::move(File.get()),
476 std::move(ObjectBuffer));
479 std::error_code ObjectFileCoverageMappingReader::readHeader() {
480 const ObjectFile *OF = Object.getBinary();
483 auto BytesInAddress = OF->getBytesInAddress();
484 if (BytesInAddress != 4 && BytesInAddress != 8)
485 return error(instrprof_error::malformed);
487 // Look for the sections that we are interested in.
488 int FoundSectionCount = 0;
489 SectionRef ProfileNames, CoverageMapping;
490 for (const auto &Section : OF->sections()) {
492 if (auto Err = Section.getName(Name))
494 if (Name == "__llvm_prf_names") {
495 ProfileNames = Section;
496 } else if (Name == "__llvm_covmap") {
497 CoverageMapping = Section;
502 if (FoundSectionCount != 2)
503 return error(instrprof_error::bad_header);
505 // Get the contents of the given sections.
507 if (auto Err = CoverageMapping.getContents(Data))
509 SectionData ProfileNamesData;
510 if (auto Err = ProfileNamesData.load(ProfileNames))
513 // Load the data from the found sections.
515 if (BytesInAddress == 4)
516 Err = readCoverageMappingData<uint32_t>(ProfileNamesData, Data,
517 MappingRecords, Filenames);
519 Err = readCoverageMappingData<uint64_t>(ProfileNamesData, Data,
520 MappingRecords, Filenames);
528 ObjectFileCoverageMappingReader::readNextRecord(CoverageMappingRecord &Record) {
529 if (CurrentRecord >= MappingRecords.size())
530 return error(instrprof_error::eof);
532 FunctionsFilenames.clear();
534 MappingRegions.clear();
535 auto &R = MappingRecords[CurrentRecord];
536 RawCoverageMappingReader Reader(
538 makeArrayRef(Filenames).slice(R.FilenamesBegin, R.FilenamesSize),
539 FunctionsFilenames, Expressions, MappingRegions);
540 if (auto Err = Reader.read())
543 Record.FunctionName = R.FunctionName;
544 Record.FunctionHash = R.FunctionHash;
545 Record.Filenames = FunctionsFilenames;
546 Record.Expressions = Expressions;
547 Record.MappingRegions = MappingRegions;