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/LEB128.h"
21 using namespace coverage;
22 using namespace object;
24 void CoverageMappingIterator::increment() {
25 // Check if all the records were read or if an error occurred while reading
27 if (Reader->readNextRecord(Record))
28 *this = CoverageMappingIterator();
31 std::error_code RawCoverageReader::readULEB128(uint64_t &Result) {
33 return error(instrprof_error::truncated);
35 Result = decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
37 return error(instrprof_error::malformed);
38 Data = Data.substr(N);
42 std::error_code RawCoverageReader::readIntMax(uint64_t &Result,
44 if (auto Err = readULEB128(Result))
46 if (Result >= MaxPlus1)
47 return error(instrprof_error::malformed);
51 std::error_code RawCoverageReader::readSize(uint64_t &Result) {
52 if (auto Err = readULEB128(Result))
54 // Sanity check the number.
55 if (Result > Data.size())
56 return error(instrprof_error::malformed);
60 std::error_code RawCoverageReader::readString(StringRef &Result) {
62 if (auto Err = readSize(Length))
64 Result = Data.substr(0, Length);
65 Data = Data.substr(Length);
69 std::error_code RawCoverageFilenamesReader::read() {
70 uint64_t NumFilenames;
71 if (auto Err = readSize(NumFilenames))
73 for (size_t I = 0; I < NumFilenames; ++I) {
75 if (auto Err = readString(Filename))
77 Filenames.push_back(Filename);
82 std::error_code RawCoverageMappingReader::decodeCounter(unsigned Value,
84 auto Tag = Value & Counter::EncodingTagMask;
87 C = Counter::getZero();
89 case Counter::CounterValueReference:
90 C = Counter::getCounter(Value >> Counter::EncodingTagBits);
95 Tag -= Counter::Expression;
97 case CounterExpression::Subtract:
98 case CounterExpression::Add: {
99 auto ID = Value >> Counter::EncodingTagBits;
100 if (ID >= Expressions.size())
101 return error(instrprof_error::malformed);
102 Expressions[ID].Kind = CounterExpression::ExprKind(Tag);
103 C = Counter::getExpression(ID);
107 return error(instrprof_error::malformed);
112 std::error_code RawCoverageMappingReader::readCounter(Counter &C) {
113 uint64_t EncodedCounter;
115 readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max()))
117 if (auto Err = decodeCounter(EncodedCounter, C))
122 static const unsigned EncodingExpansionRegionBit = 1
123 << Counter::EncodingTagBits;
125 /// \brief Read the sub-array of regions for the given inferred file id.
126 /// \param NumFileIDs the number of file ids that are defined for this
128 std::error_code RawCoverageMappingReader::readMappingRegionsSubArray(
129 std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID,
132 if (auto Err = readSize(NumRegions))
134 unsigned LineStart = 0;
135 for (size_t I = 0; I < NumRegions; ++I) {
137 CounterMappingRegion::RegionKind Kind = CounterMappingRegion::CodeRegion;
139 // Read the combined counter + region kind.
140 uint64_t EncodedCounterAndRegion;
141 if (auto Err = readIntMax(EncodedCounterAndRegion,
142 std::numeric_limits<unsigned>::max()))
144 unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
145 uint64_t ExpandedFileID = 0;
146 if (Tag != Counter::Zero) {
147 if (auto Err = decodeCounter(EncodedCounterAndRegion, C))
150 // Is it an expansion region?
151 if (EncodedCounterAndRegion & EncodingExpansionRegionBit) {
152 Kind = CounterMappingRegion::ExpansionRegion;
153 ExpandedFileID = EncodedCounterAndRegion >>
154 Counter::EncodingCounterTagAndExpansionRegionTagBits;
155 if (ExpandedFileID >= NumFileIDs)
156 return error(instrprof_error::malformed);
158 switch (EncodedCounterAndRegion >>
159 Counter::EncodingCounterTagAndExpansionRegionTagBits) {
160 case CounterMappingRegion::CodeRegion:
161 // Don't do anything when we have a code region with a zero counter.
163 case CounterMappingRegion::SkippedRegion:
164 Kind = CounterMappingRegion::SkippedRegion;
167 return error(instrprof_error::malformed);
172 // Read the source range.
173 uint64_t LineStartDelta, ColumnStart, NumLines, ColumnEnd;
175 readIntMax(LineStartDelta, std::numeric_limits<unsigned>::max()))
178 readIntMax(ColumnStart, std::numeric_limits<unsigned>::max()))
180 if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max()))
182 if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max()))
184 LineStart += LineStartDelta;
185 // Adjust the column locations for the empty regions that are supposed to
186 // cover whole lines. Those regions should be encoded with the
187 // column range (1 -> std::numeric_limits<unsigned>::max()), but because
188 // the encoded std::numeric_limits<unsigned>::max() is several bytes long,
189 // we set the column range to (0 -> 0) to ensure that the column start and
190 // column end take up one byte each.
191 // The std::numeric_limits<unsigned>::max() is used to represent a column
192 // position at the end of the line without knowing the length of that line.
193 if (ColumnStart == 0 && ColumnEnd == 0) {
195 ColumnEnd = std::numeric_limits<unsigned>::max();
197 MappingRegions.push_back(
198 CounterMappingRegion(C, InferredFileID, LineStart, ColumnStart,
199 LineStart + NumLines, ColumnEnd, Kind));
200 MappingRegions.back().ExpandedFileID = ExpandedFileID;
205 std::error_code RawCoverageMappingReader::read(CoverageMappingRecord &Record) {
207 // Read the virtual file mapping.
208 llvm::SmallVector<unsigned, 8> VirtualFileMapping;
209 uint64_t NumFileMappings;
210 if (auto Err = readSize(NumFileMappings))
212 for (size_t I = 0; I < NumFileMappings; ++I) {
213 uint64_t FilenameIndex;
214 if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size()))
216 VirtualFileMapping.push_back(FilenameIndex);
219 // Construct the files using unique filenames and virtual file mapping.
220 for (auto I : VirtualFileMapping) {
221 Filenames.push_back(TranslationUnitFilenames[I]);
224 // Read the expressions.
225 uint64_t NumExpressions;
226 if (auto Err = readSize(NumExpressions))
228 // Create an array of dummy expressions that get the proper counters
229 // when the expressions are read, and the proper kinds when the counters
233 CounterExpression(CounterExpression::Subtract, Counter(), Counter()));
234 for (size_t I = 0; I < NumExpressions; ++I) {
235 if (auto Err = readCounter(Expressions[I].LHS))
237 if (auto Err = readCounter(Expressions[I].RHS))
241 // Read the mapping regions sub-arrays.
242 for (unsigned InferredFileID = 0, S = VirtualFileMapping.size();
243 InferredFileID < S; ++InferredFileID) {
244 if (auto Err = readMappingRegionsSubArray(MappingRegions, InferredFileID,
245 VirtualFileMapping.size()))
249 // Set the counters for the expansion regions.
250 // i.e. Counter of expansion region = counter of the first region
251 // from the expanded file.
252 // Perform multiple passes to correctly propagate the counters through
253 // all the nested expansion regions.
254 for (unsigned Pass = 1, S = VirtualFileMapping.size(); Pass < S; ++Pass) {
255 for (auto &I : MappingRegions) {
256 if (I.Kind == CounterMappingRegion::ExpansionRegion) {
257 for (const auto &J : MappingRegions) {
258 if (J.FileID == I.ExpandedFileID) {
267 Record.FunctionName = FunctionName;
268 Record.Filenames = Filenames;
269 Record.Expressions = Expressions;
270 Record.MappingRegions = MappingRegions;
274 ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader(
277 auto File = llvm::object::ObjectFile::createObjectFile(FileName);
279 error(File.getError());
281 Object.reset(File.get());
284 ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader(
285 std::unique_ptr<MemoryBuffer> &ObjectBuffer, sys::fs::file_magic Type)
287 auto File = llvm::object::ObjectFile::createObjectFile(ObjectBuffer, Type);
289 error(File.getError());
291 Object.reset(File.get());
295 /// \brief The coverage mapping data for a single function.
296 /// It points to the function's name.
297 template <typename IntPtrT> struct CoverageMappingFunctionRecord {
298 IntPtrT FunctionNamePtr;
299 uint32_t FunctionNameSize;
300 uint32_t CoverageMappingSize;
303 /// \brief The coverage mapping data for a single translation unit.
304 /// It points to the array of function coverage mapping records and the encoded
306 template <typename IntPtrT> struct CoverageMappingTURecord {
307 uint32_t FunctionRecordsSize;
308 uint32_t FilenamesSize;
309 uint32_t CoverageMappingsSize;
313 /// \brief A helper structure to access the data from a section
314 /// in an object file.
319 std::error_code load(SectionRef &Section) {
320 if (auto Err = Section.getContents(Data))
322 return Section.getAddress(Address);
325 std::error_code get(uint64_t Pointer, size_t Size, StringRef &Result) {
326 if (Pointer < Address)
327 return instrprof_error::malformed;
328 auto Offset = Pointer - Address;
329 if (Offset + Size > Data.size())
330 return instrprof_error::malformed;
331 Result = Data.substr(Pointer - Address, Size);
332 return instrprof_error::success;
337 template <typename T>
338 std::error_code readCoverageMappingData(
339 SectionRef &ProfileNames, SectionRef &CoverageMapping,
340 std::vector<ObjectFileCoverageMappingReader::ProfileMappingRecord> &Records,
341 std::vector<StringRef> &Filenames) {
342 llvm::DenseSet<T> UniqueFunctionMappingData;
344 // Get the contents of the given sections.
346 if (auto Err = CoverageMapping.getContents(Data))
348 SectionData ProfileNamesData;
349 if (auto Err = ProfileNamesData.load(ProfileNames))
352 // Read the records in the coverage data section.
353 while (!Data.empty()) {
354 if (Data.size() < sizeof(CoverageMappingTURecord<T>))
355 return instrprof_error::malformed;
356 auto TU = reinterpret_cast<const CoverageMappingTURecord<T> *>(Data.data());
357 Data = Data.substr(sizeof(CoverageMappingTURecord<T>));
358 switch (TU->Version) {
359 case CoverageMappingVersion1:
362 return instrprof_error::unsupported_version;
364 auto Version = CoverageMappingVersion(TU->Version);
366 // Get the function records.
367 auto FunctionRecords =
368 reinterpret_cast<const CoverageMappingFunctionRecord<T> *>(Data.data());
370 sizeof(CoverageMappingFunctionRecord<T>) * TU->FunctionRecordsSize)
371 return instrprof_error::malformed;
372 Data = Data.substr(sizeof(CoverageMappingFunctionRecord<T>) *
373 TU->FunctionRecordsSize);
375 // Get the filenames.
376 if (Data.size() < TU->FilenamesSize)
377 return instrprof_error::malformed;
378 auto RawFilenames = Data.substr(0, TU->FilenamesSize);
379 Data = Data.substr(TU->FilenamesSize);
380 size_t FilenamesBegin = Filenames.size();
381 RawCoverageFilenamesReader Reader(RawFilenames, Filenames);
382 if (auto Err = Reader.read())
385 // Get the coverage mappings.
386 if (Data.size() < TU->CoverageMappingsSize)
387 return instrprof_error::malformed;
388 auto CoverageMappings = Data.substr(0, TU->CoverageMappingsSize);
389 Data = Data.substr(TU->CoverageMappingsSize);
391 for (unsigned I = 0; I < TU->FunctionRecordsSize; ++I) {
392 auto &MappingRecord = FunctionRecords[I];
394 // Get the coverage mapping.
395 if (CoverageMappings.size() < MappingRecord.CoverageMappingSize)
396 return instrprof_error::malformed;
398 CoverageMappings.substr(0, MappingRecord.CoverageMappingSize);
400 CoverageMappings.substr(MappingRecord.CoverageMappingSize);
402 // Ignore this record if we already have a record that points to the same
404 // This is useful to ignore the redundant records for the functions
406 if (UniqueFunctionMappingData.count(MappingRecord.FunctionNamePtr))
408 UniqueFunctionMappingData.insert(MappingRecord.FunctionNamePtr);
409 StringRef FunctionName;
410 if (auto Err = ProfileNamesData.get(MappingRecord.FunctionNamePtr,
411 MappingRecord.FunctionNameSize,
414 Records.push_back(ObjectFileCoverageMappingReader::ProfileMappingRecord(
415 Version, FunctionName, Mapping, FilenamesBegin,
416 Filenames.size() - FilenamesBegin));
420 return instrprof_error::success;
423 std::error_code ObjectFileCoverageMappingReader::readHeader() {
426 auto BytesInAddress = Object->getBytesInAddress();
427 if (BytesInAddress != 4 && BytesInAddress != 8)
428 return error(instrprof_error::malformed);
430 // Look for the sections that we are interested in.
431 int FoundSectionCount = 0;
432 SectionRef ProfileNames, CoverageMapping;
433 for (const auto &Section : Object->sections()) {
435 if (auto Err = Section.getName(Name))
437 if (Name == "__llvm_prf_names") {
438 ProfileNames = Section;
439 } else if (Name == "__llvm_covmap") {
440 CoverageMapping = Section;
445 if (FoundSectionCount != 2)
446 return error(instrprof_error::bad_header);
448 // Load the data from the found sections.
450 if (BytesInAddress == 4)
451 Err = readCoverageMappingData<uint32_t>(ProfileNames, CoverageMapping,
452 MappingRecords, Filenames);
454 Err = readCoverageMappingData<uint64_t>(ProfileNames, CoverageMapping,
455 MappingRecords, Filenames);
463 ObjectFileCoverageMappingReader::readNextRecord(CoverageMappingRecord &Record) {
464 if (CurrentRecord >= MappingRecords.size())
465 return error(instrprof_error::eof);
467 FunctionsFilenames.clear();
469 MappingRegions.clear();
470 auto &R = MappingRecords[CurrentRecord];
471 RawCoverageMappingReader Reader(
472 R.FunctionName, R.CoverageMapping,
473 makeArrayRef(Filenames.data() + R.FilenamesBegin, R.FilenamesSize),
474 FunctionsFilenames, Expressions, MappingRegions);
475 if (auto Err = Reader.read(Record))