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/Endian.h"
21 #include "llvm/Support/LEB128.h"
22 #include "llvm/Support/raw_ostream.h"
25 using namespace coverage;
26 using namespace object;
28 #define DEBUG_TYPE "coverage-mapping"
30 void CoverageMappingIterator::increment() {
31 // Check if all the records were read or if an error occurred while reading
33 if (Reader->readNextRecord(Record))
34 *this = CoverageMappingIterator();
37 std::error_code RawCoverageReader::readULEB128(uint64_t &Result) {
39 return error(instrprof_error::truncated);
41 Result = decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
43 return error(instrprof_error::malformed);
44 Data = Data.substr(N);
48 std::error_code RawCoverageReader::readIntMax(uint64_t &Result,
50 if (auto Err = readULEB128(Result))
52 if (Result >= MaxPlus1)
53 return error(instrprof_error::malformed);
57 std::error_code RawCoverageReader::readSize(uint64_t &Result) {
58 if (auto Err = readULEB128(Result))
60 // Sanity check the number.
61 if (Result > Data.size())
62 return error(instrprof_error::malformed);
66 std::error_code RawCoverageReader::readString(StringRef &Result) {
68 if (auto Err = readSize(Length))
70 Result = Data.substr(0, Length);
71 Data = Data.substr(Length);
75 std::error_code RawCoverageFilenamesReader::read() {
76 uint64_t NumFilenames;
77 if (auto Err = readSize(NumFilenames))
79 for (size_t I = 0; I < NumFilenames; ++I) {
81 if (auto Err = readString(Filename))
83 Filenames.push_back(Filename);
88 std::error_code RawCoverageMappingReader::decodeCounter(unsigned Value,
90 auto Tag = Value & Counter::EncodingTagMask;
93 C = Counter::getZero();
95 case Counter::CounterValueReference:
96 C = Counter::getCounter(Value >> Counter::EncodingTagBits);
101 Tag -= Counter::Expression;
103 case CounterExpression::Subtract:
104 case CounterExpression::Add: {
105 auto ID = Value >> Counter::EncodingTagBits;
106 if (ID >= Expressions.size())
107 return error(instrprof_error::malformed);
108 Expressions[ID].Kind = CounterExpression::ExprKind(Tag);
109 C = Counter::getExpression(ID);
113 return error(instrprof_error::malformed);
118 std::error_code RawCoverageMappingReader::readCounter(Counter &C) {
119 uint64_t EncodedCounter;
121 readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max()))
123 if (auto Err = decodeCounter(EncodedCounter, C))
128 static const unsigned EncodingExpansionRegionBit = 1
129 << Counter::EncodingTagBits;
131 /// \brief Read the sub-array of regions for the given inferred file id.
132 /// \param NumFileIDs the number of file ids that are defined for this
134 std::error_code RawCoverageMappingReader::readMappingRegionsSubArray(
135 std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID,
138 if (auto Err = readSize(NumRegions))
140 unsigned LineStart = 0;
141 for (size_t I = 0; I < NumRegions; ++I) {
143 CounterMappingRegion::RegionKind Kind = CounterMappingRegion::CodeRegion;
145 // Read the combined counter + region kind.
146 uint64_t EncodedCounterAndRegion;
147 if (auto Err = readIntMax(EncodedCounterAndRegion,
148 std::numeric_limits<unsigned>::max()))
150 unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
151 uint64_t ExpandedFileID = 0;
152 if (Tag != Counter::Zero) {
153 if (auto Err = decodeCounter(EncodedCounterAndRegion, C))
156 // Is it an expansion region?
157 if (EncodedCounterAndRegion & EncodingExpansionRegionBit) {
158 Kind = CounterMappingRegion::ExpansionRegion;
159 ExpandedFileID = EncodedCounterAndRegion >>
160 Counter::EncodingCounterTagAndExpansionRegionTagBits;
161 if (ExpandedFileID >= NumFileIDs)
162 return error(instrprof_error::malformed);
164 switch (EncodedCounterAndRegion >>
165 Counter::EncodingCounterTagAndExpansionRegionTagBits) {
166 case CounterMappingRegion::CodeRegion:
167 // Don't do anything when we have a code region with a zero counter.
169 case CounterMappingRegion::SkippedRegion:
170 Kind = CounterMappingRegion::SkippedRegion;
173 return error(instrprof_error::malformed);
178 // Read the source range.
179 uint64_t LineStartDelta, ColumnStart, NumLines, ColumnEnd;
181 readIntMax(LineStartDelta, std::numeric_limits<unsigned>::max()))
183 if (auto Err = readULEB128(ColumnStart))
185 if (ColumnStart > std::numeric_limits<unsigned>::max())
186 return error(instrprof_error::malformed);
187 if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max()))
189 if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max()))
191 LineStart += LineStartDelta;
192 // Adjust the column locations for the empty regions that are supposed to
193 // cover whole lines. Those regions should be encoded with the
194 // column range (1 -> std::numeric_limits<unsigned>::max()), but because
195 // the encoded std::numeric_limits<unsigned>::max() is several bytes long,
196 // we set the column range to (0 -> 0) to ensure that the column start and
197 // column end take up one byte each.
198 // The std::numeric_limits<unsigned>::max() is used to represent a column
199 // position at the end of the line without knowing the length of that line.
200 if (ColumnStart == 0 && ColumnEnd == 0) {
202 ColumnEnd = std::numeric_limits<unsigned>::max();
206 dbgs() << "Counter in file " << InferredFileID << " " << LineStart << ":"
207 << ColumnStart << " -> " << (LineStart + NumLines) << ":"
208 << ColumnEnd << ", ";
209 if (Kind == CounterMappingRegion::ExpansionRegion)
210 dbgs() << "Expands to file " << ExpandedFileID;
212 CounterMappingContext(Expressions).dump(C, dbgs());
216 MappingRegions.push_back(CounterMappingRegion(
217 C, InferredFileID, ExpandedFileID, LineStart, ColumnStart,
218 LineStart + NumLines, ColumnEnd, Kind));
223 std::error_code RawCoverageMappingReader::read() {
225 // Read the virtual file mapping.
226 llvm::SmallVector<unsigned, 8> VirtualFileMapping;
227 uint64_t NumFileMappings;
228 if (auto Err = readSize(NumFileMappings))
230 for (size_t I = 0; I < NumFileMappings; ++I) {
231 uint64_t FilenameIndex;
232 if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size()))
234 VirtualFileMapping.push_back(FilenameIndex);
237 // Construct the files using unique filenames and virtual file mapping.
238 for (auto I : VirtualFileMapping) {
239 Filenames.push_back(TranslationUnitFilenames[I]);
242 // Read the expressions.
243 uint64_t NumExpressions;
244 if (auto Err = readSize(NumExpressions))
246 // Create an array of dummy expressions that get the proper counters
247 // when the expressions are read, and the proper kinds when the counters
251 CounterExpression(CounterExpression::Subtract, Counter(), Counter()));
252 for (size_t I = 0; I < NumExpressions; ++I) {
253 if (auto Err = readCounter(Expressions[I].LHS))
255 if (auto Err = readCounter(Expressions[I].RHS))
259 // Read the mapping regions sub-arrays.
260 for (unsigned InferredFileID = 0, S = VirtualFileMapping.size();
261 InferredFileID < S; ++InferredFileID) {
262 if (auto Err = readMappingRegionsSubArray(MappingRegions, InferredFileID,
263 VirtualFileMapping.size()))
267 // Set the counters for the expansion regions.
268 // i.e. Counter of expansion region = counter of the first region
269 // from the expanded file.
270 // Perform multiple passes to correctly propagate the counters through
271 // all the nested expansion regions.
272 SmallVector<CounterMappingRegion *, 8> FileIDExpansionRegionMapping;
273 FileIDExpansionRegionMapping.resize(VirtualFileMapping.size(), nullptr);
274 for (unsigned Pass = 1, S = VirtualFileMapping.size(); Pass < S; ++Pass) {
275 for (auto &R : MappingRegions) {
276 if (R.Kind != CounterMappingRegion::ExpansionRegion)
278 assert(!FileIDExpansionRegionMapping[R.ExpandedFileID]);
279 FileIDExpansionRegionMapping[R.ExpandedFileID] = &R;
281 for (auto &R : MappingRegions) {
282 if (FileIDExpansionRegionMapping[R.FileID]) {
283 FileIDExpansionRegionMapping[R.FileID]->Count = R.Count;
284 FileIDExpansionRegionMapping[R.FileID] = nullptr;
294 /// \brief A helper structure to access the data from a section
295 /// in an object file.
300 std::error_code load(SectionRef &Section) {
301 if (auto Err = Section.getContents(Data))
303 Address = Section.getAddress();
304 return instrprof_error::success;
307 std::error_code get(uint64_t Pointer, size_t Size, StringRef &Result) {
308 if (Pointer < Address)
309 return instrprof_error::malformed;
310 auto Offset = Pointer - Address;
311 if (Offset + Size > Data.size())
312 return instrprof_error::malformed;
313 Result = Data.substr(Pointer - Address, Size);
314 return instrprof_error::success;
319 template <typename T, support::endianness Endian>
320 std::error_code readCoverageMappingData(
321 SectionData &ProfileNames, StringRef Data,
322 std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records,
323 std::vector<StringRef> &Filenames) {
324 using namespace support;
325 llvm::DenseSet<T> UniqueFunctionMappingData;
327 // Read the records in the coverage data section.
328 for (const char *Buf = Data.data(), *End = Buf + Data.size(); Buf < End;) {
329 if (Buf + 4 * sizeof(uint32_t) > End)
330 return instrprof_error::malformed;
331 uint32_t NRecords = endian::readNext<uint32_t, Endian, unaligned>(Buf);
332 uint32_t FilenamesSize = endian::readNext<uint32_t, Endian, unaligned>(Buf);
333 uint32_t CoverageSize = endian::readNext<uint32_t, Endian, unaligned>(Buf);
334 uint32_t Version = endian::readNext<uint32_t, Endian, unaligned>(Buf);
337 case CoverageMappingVersion1:
340 return instrprof_error::unsupported_version;
343 // Skip past the function records, saving the start and end for later.
344 const char *FunBuf = Buf;
345 Buf += NRecords * (sizeof(T) + 2 * sizeof(uint32_t) + sizeof(uint64_t));
346 const char *FunEnd = Buf;
348 // Get the filenames.
349 if (Buf + FilenamesSize > End)
350 return instrprof_error::malformed;
351 size_t FilenamesBegin = Filenames.size();
352 RawCoverageFilenamesReader Reader(StringRef(Buf, FilenamesSize), Filenames);
353 if (auto Err = Reader.read())
355 Buf += FilenamesSize;
357 // We'll read the coverage mapping records in the loop below.
358 const char *CovBuf = Buf;
360 const char *CovEnd = Buf;
362 return instrprof_error::malformed;
364 while (FunBuf < FunEnd) {
365 // Read the function information
366 T NamePtr = endian::readNext<T, Endian, unaligned>(FunBuf);
367 uint32_t NameSize = endian::readNext<uint32_t, Endian, unaligned>(FunBuf);
368 uint32_t DataSize = endian::readNext<uint32_t, Endian, unaligned>(FunBuf);
369 uint64_t FuncHash = endian::readNext<uint64_t, Endian, unaligned>(FunBuf);
371 // Now use that to read the coverage data.
372 if (CovBuf + DataSize > CovEnd)
373 return instrprof_error::malformed;
374 auto Mapping = StringRef(CovBuf, DataSize);
377 // Ignore this record if we already have a record that points to the same
378 // function name. This is useful to ignore the redundant records for the
379 // functions with ODR linkage.
380 if (!UniqueFunctionMappingData.insert(NamePtr).second)
383 // Finally, grab the name and create a record.
385 if (std::error_code EC = ProfileNames.get(NamePtr, NameSize, FuncName))
387 Records.push_back(BinaryCoverageReader::ProfileMappingRecord(
388 CoverageMappingVersion(Version), FuncName, FuncHash, Mapping,
389 FilenamesBegin, Filenames.size() - FilenamesBegin));
393 return instrprof_error::success;
396 static const char *TestingFormatMagic = "llvmcovmtestdata";
398 static std::error_code loadTestingFormat(StringRef Data,
399 SectionData &ProfileNames,
400 StringRef &CoverageMapping,
401 uint8_t &BytesInAddress,
402 support::endianness &Endian) {
404 Endian = support::endianness::little;
406 Data = Data.substr(StringRef(TestingFormatMagic).size());
408 return instrprof_error::truncated;
410 auto ProfileNamesSize =
411 decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
413 return instrprof_error::malformed;
414 Data = Data.substr(N);
416 return instrprof_error::truncated;
418 ProfileNames.Address =
419 decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
421 return instrprof_error::malformed;
422 Data = Data.substr(N);
423 if (Data.size() < ProfileNamesSize)
424 return instrprof_error::malformed;
425 ProfileNames.Data = Data.substr(0, ProfileNamesSize);
426 CoverageMapping = Data.substr(ProfileNamesSize);
427 return instrprof_error::success;
430 static std::error_code loadBinaryFormat(MemoryBufferRef ObjectBuffer,
431 SectionData &ProfileNames,
432 StringRef &CoverageMapping,
433 uint8_t &BytesInAddress,
434 support::endianness &Endian,
435 Triple::ArchType Arch) {
436 auto BinOrErr = object::createBinary(ObjectBuffer);
437 if (std::error_code EC = BinOrErr.getError())
439 auto Bin = std::move(BinOrErr.get());
440 std::unique_ptr<ObjectFile> OF;
441 if (auto *Universal = dyn_cast<object::MachOUniversalBinary>(Bin.get())) {
442 // If we have a universal binary, try to look up the object for the
443 // appropriate architecture.
444 auto ObjectFileOrErr = Universal->getObjectForArch(Arch);
445 if (std::error_code EC = ObjectFileOrErr.getError())
447 OF = std::move(ObjectFileOrErr.get());
448 } else if (isa<object::ObjectFile>(Bin.get())) {
449 // For any other object file, upcast and take ownership.
450 OF.reset(cast<object::ObjectFile>(Bin.release()));
451 // If we've asked for a particular arch, make sure they match.
452 if (Arch != Triple::ArchType::UnknownArch && OF->getArch() != Arch)
453 return object_error::arch_not_found;
455 // We can only handle object files.
456 return instrprof_error::malformed;
458 // The coverage uses native pointer sizes for the object it's written in.
459 BytesInAddress = OF->getBytesInAddress();
460 Endian = OF->isLittleEndian() ? support::endianness::little
461 : support::endianness::big;
463 // Look for the sections that we are interested in.
464 int FoundSectionCount = 0;
465 SectionRef NamesSection, CoverageSection;
466 for (const auto &Section : OF->sections()) {
468 if (auto Err = Section.getName(Name))
470 if (Name == "__llvm_prf_names") {
471 NamesSection = Section;
472 } else if (Name == "__llvm_covmap") {
473 CoverageSection = Section;
478 if (FoundSectionCount != 2)
479 return instrprof_error::bad_header;
481 // Get the contents of the given sections.
482 if (std::error_code EC = CoverageSection.getContents(CoverageMapping))
484 if (std::error_code EC = ProfileNames.load(NamesSection))
487 return std::error_code();
490 ErrorOr<std::unique_ptr<BinaryCoverageReader>>
491 BinaryCoverageReader::create(std::unique_ptr<MemoryBuffer> &ObjectBuffer,
492 Triple::ArchType Arch) {
493 std::unique_ptr<BinaryCoverageReader> Reader(new BinaryCoverageReader());
497 uint8_t BytesInAddress;
498 support::endianness Endian;
500 if (ObjectBuffer->getBuffer().startswith(TestingFormatMagic))
501 // This is a special format used for testing.
502 EC = loadTestingFormat(ObjectBuffer->getBuffer(), Profile, Coverage,
503 BytesInAddress, Endian);
505 EC = loadBinaryFormat(ObjectBuffer->getMemBufferRef(), Profile, Coverage,
506 BytesInAddress, Endian, Arch);
510 if (BytesInAddress == 4 && Endian == support::endianness::little)
511 EC = readCoverageMappingData<uint32_t, support::endianness::little>(
512 Profile, Coverage, Reader->MappingRecords, Reader->Filenames);
513 else if (BytesInAddress == 4 && Endian == support::endianness::big)
514 EC = readCoverageMappingData<uint32_t, support::endianness::big>(
515 Profile, Coverage, Reader->MappingRecords, Reader->Filenames);
516 else if (BytesInAddress == 8 && Endian == support::endianness::little)
517 EC = readCoverageMappingData<uint64_t, support::endianness::little>(
518 Profile, Coverage, Reader->MappingRecords, Reader->Filenames);
519 else if (BytesInAddress == 8 && Endian == support::endianness::big)
520 EC = readCoverageMappingData<uint64_t, support::endianness::big>(
521 Profile, Coverage, Reader->MappingRecords, Reader->Filenames);
523 return instrprof_error::malformed;
526 return std::move(Reader);
530 BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) {
531 if (CurrentRecord >= MappingRecords.size())
532 return instrprof_error::eof;
534 FunctionsFilenames.clear();
536 MappingRegions.clear();
537 auto &R = MappingRecords[CurrentRecord];
538 RawCoverageMappingReader Reader(
540 makeArrayRef(Filenames).slice(R.FilenamesBegin, R.FilenamesSize),
541 FunctionsFilenames, Expressions, MappingRegions);
542 if (auto Err = Reader.read())
545 Record.FunctionName = R.FunctionName;
546 Record.FunctionHash = R.FunctionHash;
547 Record.Filenames = FunctionsFilenames;
548 Record.Expressions = Expressions;
549 Record.MappingRegions = MappingRegions;
552 return std::error_code();