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/MachOUniversal.h"
18 #include "llvm/Object/ObjectFile.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/LEB128.h"
23 using namespace coverage;
24 using namespace object;
26 #define DEBUG_TYPE "coverage-mapping"
28 void CoverageMappingIterator::increment() {
29 // Check if all the records were read or if an error occurred while reading
31 if (Reader->readNextRecord(Record))
32 *this = CoverageMappingIterator();
35 std::error_code RawCoverageReader::readULEB128(uint64_t &Result) {
37 return error(instrprof_error::truncated);
39 Result = decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
41 return error(instrprof_error::malformed);
42 Data = Data.substr(N);
46 std::error_code RawCoverageReader::readIntMax(uint64_t &Result,
48 if (auto Err = readULEB128(Result))
50 if (Result >= MaxPlus1)
51 return error(instrprof_error::malformed);
55 std::error_code RawCoverageReader::readSize(uint64_t &Result) {
56 if (auto Err = readULEB128(Result))
58 // Sanity check the number.
59 if (Result > Data.size())
60 return error(instrprof_error::malformed);
64 std::error_code RawCoverageReader::readString(StringRef &Result) {
66 if (auto Err = readSize(Length))
68 Result = Data.substr(0, Length);
69 Data = Data.substr(Length);
73 std::error_code RawCoverageFilenamesReader::read() {
74 uint64_t NumFilenames;
75 if (auto Err = readSize(NumFilenames))
77 for (size_t I = 0; I < NumFilenames; ++I) {
79 if (auto Err = readString(Filename))
81 Filenames.push_back(Filename);
86 std::error_code RawCoverageMappingReader::decodeCounter(unsigned Value,
88 auto Tag = Value & Counter::EncodingTagMask;
91 C = Counter::getZero();
93 case Counter::CounterValueReference:
94 C = Counter::getCounter(Value >> Counter::EncodingTagBits);
99 Tag -= Counter::Expression;
101 case CounterExpression::Subtract:
102 case CounterExpression::Add: {
103 auto ID = Value >> Counter::EncodingTagBits;
104 if (ID >= Expressions.size())
105 return error(instrprof_error::malformed);
106 Expressions[ID].Kind = CounterExpression::ExprKind(Tag);
107 C = Counter::getExpression(ID);
111 return error(instrprof_error::malformed);
116 std::error_code RawCoverageMappingReader::readCounter(Counter &C) {
117 uint64_t EncodedCounter;
119 readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max()))
121 if (auto Err = decodeCounter(EncodedCounter, C))
126 static const unsigned EncodingExpansionRegionBit = 1
127 << Counter::EncodingTagBits;
129 /// \brief Read the sub-array of regions for the given inferred file id.
130 /// \param NumFileIDs the number of file ids that are defined for this
132 std::error_code RawCoverageMappingReader::readMappingRegionsSubArray(
133 std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID,
136 if (auto Err = readSize(NumRegions))
138 unsigned LineStart = 0;
139 for (size_t I = 0; I < NumRegions; ++I) {
141 CounterMappingRegion::RegionKind Kind = CounterMappingRegion::CodeRegion;
143 // Read the combined counter + region kind.
144 uint64_t EncodedCounterAndRegion;
145 if (auto Err = readIntMax(EncodedCounterAndRegion,
146 std::numeric_limits<unsigned>::max()))
148 unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
149 uint64_t ExpandedFileID = 0;
150 if (Tag != Counter::Zero) {
151 if (auto Err = decodeCounter(EncodedCounterAndRegion, C))
154 // Is it an expansion region?
155 if (EncodedCounterAndRegion & EncodingExpansionRegionBit) {
156 Kind = CounterMappingRegion::ExpansionRegion;
157 ExpandedFileID = EncodedCounterAndRegion >>
158 Counter::EncodingCounterTagAndExpansionRegionTagBits;
159 if (ExpandedFileID >= NumFileIDs)
160 return error(instrprof_error::malformed);
162 switch (EncodedCounterAndRegion >>
163 Counter::EncodingCounterTagAndExpansionRegionTagBits) {
164 case CounterMappingRegion::CodeRegion:
165 // Don't do anything when we have a code region with a zero counter.
167 case CounterMappingRegion::SkippedRegion:
168 Kind = CounterMappingRegion::SkippedRegion;
171 return error(instrprof_error::malformed);
176 // Read the source range.
177 uint64_t LineStartDelta, ColumnStart, NumLines, ColumnEnd;
179 readIntMax(LineStartDelta, std::numeric_limits<unsigned>::max()))
181 if (auto Err = readULEB128(ColumnStart))
183 if (ColumnStart > std::numeric_limits<unsigned>::max())
184 return error(instrprof_error::malformed);
185 if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max()))
187 if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max()))
189 LineStart += LineStartDelta;
190 // Adjust the column locations for the empty regions that are supposed to
191 // cover whole lines. Those regions should be encoded with the
192 // column range (1 -> std::numeric_limits<unsigned>::max()), but because
193 // the encoded std::numeric_limits<unsigned>::max() is several bytes long,
194 // we set the column range to (0 -> 0) to ensure that the column start and
195 // column end take up one byte each.
196 // The std::numeric_limits<unsigned>::max() is used to represent a column
197 // position at the end of the line without knowing the length of that line.
198 if (ColumnStart == 0 && ColumnEnd == 0) {
200 ColumnEnd = std::numeric_limits<unsigned>::max();
204 dbgs() << "Counter in file " << InferredFileID << " " << LineStart << ":"
205 << ColumnStart << " -> " << (LineStart + NumLines) << ":"
206 << ColumnEnd << ", ";
207 if (Kind == CounterMappingRegion::ExpansionRegion)
208 dbgs() << "Expands to file " << ExpandedFileID;
210 CounterMappingContext(Expressions).dump(C, dbgs());
214 MappingRegions.push_back(CounterMappingRegion(
215 C, InferredFileID, ExpandedFileID, LineStart, ColumnStart,
216 LineStart + NumLines, ColumnEnd, Kind));
221 std::error_code RawCoverageMappingReader::read() {
223 // Read the virtual file mapping.
224 llvm::SmallVector<unsigned, 8> VirtualFileMapping;
225 uint64_t NumFileMappings;
226 if (auto Err = readSize(NumFileMappings))
228 for (size_t I = 0; I < NumFileMappings; ++I) {
229 uint64_t FilenameIndex;
230 if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size()))
232 VirtualFileMapping.push_back(FilenameIndex);
235 // Construct the files using unique filenames and virtual file mapping.
236 for (auto I : VirtualFileMapping) {
237 Filenames.push_back(TranslationUnitFilenames[I]);
240 // Read the expressions.
241 uint64_t NumExpressions;
242 if (auto Err = readSize(NumExpressions))
244 // Create an array of dummy expressions that get the proper counters
245 // when the expressions are read, and the proper kinds when the counters
249 CounterExpression(CounterExpression::Subtract, Counter(), Counter()));
250 for (size_t I = 0; I < NumExpressions; ++I) {
251 if (auto Err = readCounter(Expressions[I].LHS))
253 if (auto Err = readCounter(Expressions[I].RHS))
257 // Read the mapping regions sub-arrays.
258 for (unsigned InferredFileID = 0, S = VirtualFileMapping.size();
259 InferredFileID < S; ++InferredFileID) {
260 if (auto Err = readMappingRegionsSubArray(MappingRegions, InferredFileID,
261 VirtualFileMapping.size()))
265 // Set the counters for the expansion regions.
266 // i.e. Counter of expansion region = counter of the first region
267 // from the expanded file.
268 // Perform multiple passes to correctly propagate the counters through
269 // all the nested expansion regions.
270 SmallVector<CounterMappingRegion *, 8> FileIDExpansionRegionMapping;
271 FileIDExpansionRegionMapping.resize(VirtualFileMapping.size(), nullptr);
272 for (unsigned Pass = 1, S = VirtualFileMapping.size(); Pass < S; ++Pass) {
273 for (auto &R : MappingRegions) {
274 if (R.Kind != CounterMappingRegion::ExpansionRegion)
276 assert(!FileIDExpansionRegionMapping[R.ExpandedFileID]);
277 FileIDExpansionRegionMapping[R.ExpandedFileID] = &R;
279 for (auto &R : MappingRegions) {
280 if (FileIDExpansionRegionMapping[R.FileID]) {
281 FileIDExpansionRegionMapping[R.FileID]->Count = R.Count;
282 FileIDExpansionRegionMapping[R.FileID] = nullptr;
291 /// \brief The coverage mapping data for a single function.
292 /// It points to the function's name.
293 template <typename IntPtrT> struct CoverageMappingFunctionRecord {
294 IntPtrT FunctionNamePtr;
295 uint32_t FunctionNameSize;
296 uint32_t CoverageMappingSize;
297 uint64_t FunctionHash;
300 /// \brief The coverage mapping data for a single translation unit.
301 /// It points to the array of function coverage mapping records and the encoded
303 template <typename IntPtrT> struct CoverageMappingTURecord {
304 uint32_t FunctionRecordsSize;
305 uint32_t FilenamesSize;
306 uint32_t CoverageMappingsSize;
310 /// \brief A helper structure to access the data from a section
311 /// in an object file.
316 std::error_code load(SectionRef &Section) {
317 if (auto Err = Section.getContents(Data))
319 Address = Section.getAddress();
320 return instrprof_error::success;
323 std::error_code get(uint64_t Pointer, size_t Size, StringRef &Result) {
324 if (Pointer < Address)
325 return instrprof_error::malformed;
326 auto Offset = Pointer - Address;
327 if (Offset + Size > Data.size())
328 return instrprof_error::malformed;
329 Result = Data.substr(Pointer - Address, Size);
330 return instrprof_error::success;
335 template <typename T>
336 std::error_code readCoverageMappingData(
337 SectionData &ProfileNames, StringRef Data,
338 std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records,
339 std::vector<StringRef> &Filenames) {
340 llvm::DenseSet<T> UniqueFunctionMappingData;
342 // Read the records in the coverage data section.
343 while (!Data.empty()) {
344 if (Data.size() < sizeof(CoverageMappingTURecord<T>))
345 return instrprof_error::malformed;
346 auto TU = reinterpret_cast<const CoverageMappingTURecord<T> *>(Data.data());
347 Data = Data.substr(sizeof(CoverageMappingTURecord<T>));
348 switch (TU->Version) {
349 case CoverageMappingVersion1:
352 return instrprof_error::unsupported_version;
354 auto Version = CoverageMappingVersion(TU->Version);
356 // Get the function records.
357 auto FunctionRecords =
358 reinterpret_cast<const CoverageMappingFunctionRecord<T> *>(Data.data());
360 sizeof(CoverageMappingFunctionRecord<T>) * TU->FunctionRecordsSize)
361 return instrprof_error::malformed;
362 Data = Data.substr(sizeof(CoverageMappingFunctionRecord<T>) *
363 TU->FunctionRecordsSize);
365 // Get the filenames.
366 if (Data.size() < TU->FilenamesSize)
367 return instrprof_error::malformed;
368 auto RawFilenames = Data.substr(0, TU->FilenamesSize);
369 Data = Data.substr(TU->FilenamesSize);
370 size_t FilenamesBegin = Filenames.size();
371 RawCoverageFilenamesReader Reader(RawFilenames, Filenames);
372 if (auto Err = Reader.read())
375 // Get the coverage mappings.
376 if (Data.size() < TU->CoverageMappingsSize)
377 return instrprof_error::malformed;
378 auto CoverageMappings = Data.substr(0, TU->CoverageMappingsSize);
379 Data = Data.substr(TU->CoverageMappingsSize);
381 for (unsigned I = 0; I < TU->FunctionRecordsSize; ++I) {
382 auto &MappingRecord = FunctionRecords[I];
384 // Get the coverage mapping.
385 if (CoverageMappings.size() < MappingRecord.CoverageMappingSize)
386 return instrprof_error::malformed;
388 CoverageMappings.substr(0, MappingRecord.CoverageMappingSize);
390 CoverageMappings.substr(MappingRecord.CoverageMappingSize);
392 // Ignore this record if we already have a record that points to the same
394 // This is useful to ignore the redundant records for the functions
396 if (!UniqueFunctionMappingData.insert(MappingRecord.FunctionNamePtr)
399 StringRef FunctionName;
401 ProfileNames.get(MappingRecord.FunctionNamePtr,
402 MappingRecord.FunctionNameSize, FunctionName))
404 Records.push_back(BinaryCoverageReader::ProfileMappingRecord(
405 Version, FunctionName, MappingRecord.FunctionHash, Mapping,
406 FilenamesBegin, Filenames.size() - FilenamesBegin));
410 return instrprof_error::success;
413 static const char *TestingFormatMagic = "llvmcovmtestdata";
415 static std::error_code loadTestingFormat(StringRef Data,
416 SectionData &ProfileNames,
417 StringRef &CoverageMapping,
418 uint8_t &BytesInAddress) {
421 Data = Data.substr(StringRef(TestingFormatMagic).size());
423 return instrprof_error::truncated;
425 auto ProfileNamesSize =
426 decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
428 return instrprof_error::malformed;
429 Data = Data.substr(N);
431 return instrprof_error::truncated;
433 ProfileNames.Address =
434 decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
436 return instrprof_error::malformed;
437 Data = Data.substr(N);
438 if (Data.size() < ProfileNamesSize)
439 return instrprof_error::malformed;
440 ProfileNames.Data = Data.substr(0, ProfileNamesSize);
441 CoverageMapping = Data.substr(ProfileNamesSize);
442 return instrprof_error::success;
445 static std::error_code loadBinaryFormat(MemoryBufferRef ObjectBuffer,
446 SectionData &ProfileNames,
447 StringRef &CoverageMapping,
448 uint8_t &BytesInAddress,
449 Triple::ArchType Arch) {
450 auto BinOrErr = object::createBinary(ObjectBuffer);
451 if (std::error_code EC = BinOrErr.getError())
453 auto Bin = std::move(BinOrErr.get());
454 std::unique_ptr<ObjectFile> OF;
455 if (auto *Universal = dyn_cast<object::MachOUniversalBinary>(Bin.get())) {
456 // If we have a universal binary, try to look up the object for the
457 // appropriate architecture.
458 auto ObjectFileOrErr = Universal->getObjectForArch(Arch);
459 if (std::error_code EC = ObjectFileOrErr.getError())
461 OF = std::move(ObjectFileOrErr.get());
462 } else if (isa<object::ObjectFile>(Bin.get())) {
463 // For any other object file, upcast and take ownership.
464 OF.reset(cast<object::ObjectFile>(Bin.release()));
465 // If we've asked for a particular arch, make sure they match.
466 if (Arch != Triple::ArchType::UnknownArch && OF->getArch() != Arch)
467 return object_error::arch_not_found;
469 // We can only handle object files.
470 return instrprof_error::malformed;
472 // The coverage uses native pointer sizes for the object it's written in.
473 BytesInAddress = OF->getBytesInAddress();
475 // Look for the sections that we are interested in.
476 int FoundSectionCount = 0;
477 SectionRef NamesSection, CoverageSection;
478 for (const auto &Section : OF->sections()) {
480 if (auto Err = Section.getName(Name))
482 if (Name == "__llvm_prf_names") {
483 NamesSection = Section;
484 } else if (Name == "__llvm_covmap") {
485 CoverageSection = Section;
490 if (FoundSectionCount != 2)
491 return instrprof_error::bad_header;
493 // Get the contents of the given sections.
494 if (std::error_code EC = CoverageSection.getContents(CoverageMapping))
496 if (std::error_code EC = ProfileNames.load(NamesSection))
499 return std::error_code();
502 ErrorOr<std::unique_ptr<BinaryCoverageReader>>
503 BinaryCoverageReader::create(std::unique_ptr<MemoryBuffer> &ObjectBuffer,
504 Triple::ArchType Arch) {
505 std::unique_ptr<BinaryCoverageReader> Reader(new BinaryCoverageReader());
509 uint8_t BytesInAddress;
511 if (ObjectBuffer->getBuffer().startswith(TestingFormatMagic))
512 // This is a special format used for testing.
513 EC = loadTestingFormat(ObjectBuffer->getBuffer(), Profile, Coverage,
516 EC = loadBinaryFormat(ObjectBuffer->getMemBufferRef(), Profile, Coverage,
517 BytesInAddress, Arch);
521 if (BytesInAddress == 4)
522 EC = readCoverageMappingData<uint32_t>(
523 Profile, Coverage, Reader->MappingRecords, Reader->Filenames);
524 else if (BytesInAddress == 8)
525 EC = readCoverageMappingData<uint64_t>(
526 Profile, Coverage, Reader->MappingRecords, Reader->Filenames);
528 return instrprof_error::malformed;
531 return std::move(Reader);
535 BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) {
536 if (CurrentRecord >= MappingRecords.size())
537 return instrprof_error::eof;
539 FunctionsFilenames.clear();
541 MappingRegions.clear();
542 auto &R = MappingRecords[CurrentRecord];
543 RawCoverageMappingReader Reader(
545 makeArrayRef(Filenames).slice(R.FilenamesBegin, R.FilenamesSize),
546 FunctionsFilenames, Expressions, MappingRegions);
547 if (auto Err = Reader.read())
550 Record.FunctionName = R.FunctionName;
551 Record.FunctionHash = R.FunctionHash;
552 Record.Filenames = FunctionsFilenames;
553 Record.Expressions = Expressions;
554 Record.MappingRegions = MappingRegions;
557 return std::error_code();