InstrProf: Remove CoverageMapping::HasCodeBefore, it isn't used
[oota-llvm.git] / lib / ProfileData / CoverageMappingReader.cpp
1 //=-- CoverageMappingReader.cpp - Code coverage mapping reader ----*- C++ -*-=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for reading coverage mapping data for
11 // instrumentation based coverage.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
20
21 using namespace llvm;
22 using namespace coverage;
23 using namespace object;
24
25 #define DEBUG_TYPE "coverage-mapping"
26
27 void CoverageMappingIterator::increment() {
28   // Check if all the records were read or if an error occurred while reading
29   // the next record.
30   if (Reader->readNextRecord(Record))
31     *this = CoverageMappingIterator();
32 }
33
34 std::error_code RawCoverageReader::readULEB128(uint64_t &Result) {
35   if (Data.size() < 1)
36     return error(instrprof_error::truncated);
37   unsigned N = 0;
38   Result = decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
39   if (N > Data.size())
40     return error(instrprof_error::malformed);
41   Data = Data.substr(N);
42   return success();
43 }
44
45 std::error_code RawCoverageReader::readIntMax(uint64_t &Result,
46                                               uint64_t MaxPlus1) {
47   if (auto Err = readULEB128(Result))
48     return Err;
49   if (Result >= MaxPlus1)
50     return error(instrprof_error::malformed);
51   return success();
52 }
53
54 std::error_code RawCoverageReader::readSize(uint64_t &Result) {
55   if (auto Err = readULEB128(Result))
56     return Err;
57   // Sanity check the number.
58   if (Result > Data.size())
59     return error(instrprof_error::malformed);
60   return success();
61 }
62
63 std::error_code RawCoverageReader::readString(StringRef &Result) {
64   uint64_t Length;
65   if (auto Err = readSize(Length))
66     return Err;
67   Result = Data.substr(0, Length);
68   Data = Data.substr(Length);
69   return success();
70 }
71
72 std::error_code RawCoverageFilenamesReader::read() {
73   uint64_t NumFilenames;
74   if (auto Err = readSize(NumFilenames))
75     return Err;
76   for (size_t I = 0; I < NumFilenames; ++I) {
77     StringRef Filename;
78     if (auto Err = readString(Filename))
79       return Err;
80     Filenames.push_back(Filename);
81   }
82   return success();
83 }
84
85 std::error_code RawCoverageMappingReader::decodeCounter(unsigned Value,
86                                                         Counter &C) {
87   auto Tag = Value & Counter::EncodingTagMask;
88   switch (Tag) {
89   case Counter::Zero:
90     C = Counter::getZero();
91     return success();
92   case Counter::CounterValueReference:
93     C = Counter::getCounter(Value >> Counter::EncodingTagBits);
94     return success();
95   default:
96     break;
97   }
98   Tag -= Counter::Expression;
99   switch (Tag) {
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);
107     break;
108   }
109   default:
110     return error(instrprof_error::malformed);
111   }
112   return success();
113 }
114
115 std::error_code RawCoverageMappingReader::readCounter(Counter &C) {
116   uint64_t EncodedCounter;
117   if (auto Err =
118           readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max()))
119     return Err;
120   if (auto Err = decodeCounter(EncodedCounter, C))
121     return Err;
122   return success();
123 }
124
125 static const unsigned EncodingExpansionRegionBit = 1
126                                                    << Counter::EncodingTagBits;
127
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
130 /// function.
131 std::error_code RawCoverageMappingReader::readMappingRegionsSubArray(
132     std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID,
133     size_t NumFileIDs) {
134   uint64_t NumRegions;
135   if (auto Err = readSize(NumRegions))
136     return Err;
137   unsigned LineStart = 0;
138   for (size_t I = 0; I < NumRegions; ++I) {
139     Counter C;
140     CounterMappingRegion::RegionKind Kind = CounterMappingRegion::CodeRegion;
141
142     // Read the combined counter + region kind.
143     uint64_t EncodedCounterAndRegion;
144     if (auto Err = readIntMax(EncodedCounterAndRegion,
145                               std::numeric_limits<unsigned>::max()))
146       return Err;
147     unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
148     uint64_t ExpandedFileID = 0;
149     if (Tag != Counter::Zero) {
150       if (auto Err = decodeCounter(EncodedCounterAndRegion, C))
151         return Err;
152     } else {
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);
160       } else {
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.
165           break;
166         case CounterMappingRegion::SkippedRegion:
167           Kind = CounterMappingRegion::SkippedRegion;
168           break;
169         default:
170           return error(instrprof_error::malformed);
171         }
172       }
173     }
174
175     // Read the source range.
176     uint64_t LineStartDelta, ColumnStart, NumLines, ColumnEnd;
177     if (auto Err =
178             readIntMax(LineStartDelta, std::numeric_limits<unsigned>::max()))
179       return Err;
180     if (auto Err = readULEB128(ColumnStart))
181       return Err;
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()))
185       return Err;
186     if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max()))
187       return Err;
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) {
198       ColumnStart = 1;
199       ColumnEnd = std::numeric_limits<unsigned>::max();
200     }
201
202     DEBUG({
203       dbgs() << "Counter in file " << InferredFileID << " " << LineStart << ":"
204              << ColumnStart << " -> " << (LineStart + NumLines) << ":"
205              << ColumnEnd << ", ";
206       if (Kind == CounterMappingRegion::ExpansionRegion)
207         dbgs() << "Expands to file " << ExpandedFileID;
208       else
209         CounterMappingContext(Expressions).dump(C, dbgs());
210       dbgs() << "\n";
211     });
212
213     MappingRegions.push_back(
214         CounterMappingRegion(C, InferredFileID, LineStart, ColumnStart,
215                              LineStart + NumLines, ColumnEnd, Kind));
216     MappingRegions.back().ExpandedFileID = ExpandedFileID;
217   }
218   return success();
219 }
220
221 std::error_code RawCoverageMappingReader::read() {
222
223   // Read the virtual file mapping.
224   llvm::SmallVector<unsigned, 8> VirtualFileMapping;
225   uint64_t NumFileMappings;
226   if (auto Err = readSize(NumFileMappings))
227     return Err;
228   for (size_t I = 0; I < NumFileMappings; ++I) {
229     uint64_t FilenameIndex;
230     if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size()))
231       return Err;
232     VirtualFileMapping.push_back(FilenameIndex);
233   }
234
235   // Construct the files using unique filenames and virtual file mapping.
236   for (auto I : VirtualFileMapping) {
237     Filenames.push_back(TranslationUnitFilenames[I]);
238   }
239
240   // Read the expressions.
241   uint64_t NumExpressions;
242   if (auto Err = readSize(NumExpressions))
243     return Err;
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
246   // are decoded.
247   Expressions.resize(
248       NumExpressions,
249       CounterExpression(CounterExpression::Subtract, Counter(), Counter()));
250   for (size_t I = 0; I < NumExpressions; ++I) {
251     if (auto Err = readCounter(Expressions[I].LHS))
252       return Err;
253     if (auto Err = readCounter(Expressions[I].RHS))
254       return Err;
255   }
256
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()))
262       return Err;
263   }
264
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)
275         continue;
276       assert(!FileIDExpansionRegionMapping[R.ExpandedFileID]);
277       FileIDExpansionRegionMapping[R.ExpandedFileID] = &R;
278     }
279     for (auto &R : MappingRegions) {
280       if (FileIDExpansionRegionMapping[R.FileID]) {
281         FileIDExpansionRegionMapping[R.FileID]->Count = R.Count;
282         FileIDExpansionRegionMapping[R.FileID] = nullptr;
283       }
284     }
285   }
286
287   return success();
288 }
289
290 ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader(
291     StringRef FileName)
292     : CurrentRecord(0) {
293   auto File = llvm::object::ObjectFile::createObjectFile(FileName);
294   if (!File)
295     error(File.getError());
296   else
297     Object = std::move(File.get());
298 }
299
300 namespace {
301 /// \brief The coverage mapping data for a single function.
302 /// It points to the function's name.
303 template <typename IntPtrT> struct CoverageMappingFunctionRecord {
304   IntPtrT FunctionNamePtr;
305   uint32_t FunctionNameSize;
306   uint32_t CoverageMappingSize;
307   uint64_t FunctionHash;
308 };
309
310 /// \brief The coverage mapping data for a single translation unit.
311 /// It points to the array of function coverage mapping records and the encoded
312 /// filenames array.
313 template <typename IntPtrT> struct CoverageMappingTURecord {
314   uint32_t FunctionRecordsSize;
315   uint32_t FilenamesSize;
316   uint32_t CoverageMappingsSize;
317   uint32_t Version;
318 };
319
320 /// \brief A helper structure to access the data from a section
321 /// in an object file.
322 struct SectionData {
323   StringRef Data;
324   uint64_t Address;
325
326   std::error_code load(SectionRef &Section) {
327     if (auto Err = Section.getContents(Data))
328       return Err;
329     Address = Section.getAddress();
330     return instrprof_error::success;
331   }
332
333   std::error_code get(uint64_t Pointer, size_t Size, StringRef &Result) {
334     if (Pointer < Address)
335       return instrprof_error::malformed;
336     auto Offset = Pointer - Address;
337     if (Offset + Size > Data.size())
338       return instrprof_error::malformed;
339     Result = Data.substr(Pointer - Address, Size);
340     return instrprof_error::success;
341   }
342 };
343 }
344
345 template <typename T>
346 std::error_code readCoverageMappingData(
347     SectionData &ProfileNames, StringRef Data,
348     std::vector<ObjectFileCoverageMappingReader::ProfileMappingRecord> &Records,
349     std::vector<StringRef> &Filenames) {
350   llvm::DenseSet<T> UniqueFunctionMappingData;
351
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:
360       break;
361     default:
362       return instrprof_error::unsupported_version;
363     }
364     auto Version = CoverageMappingVersion(TU->Version);
365
366     // Get the function records.
367     auto FunctionRecords =
368         reinterpret_cast<const CoverageMappingFunctionRecord<T> *>(Data.data());
369     if (Data.size() <
370         sizeof(CoverageMappingFunctionRecord<T>) * TU->FunctionRecordsSize)
371       return instrprof_error::malformed;
372     Data = Data.substr(sizeof(CoverageMappingFunctionRecord<T>) *
373                        TU->FunctionRecordsSize);
374
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())
383       return Err;
384
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);
390
391     for (unsigned I = 0; I < TU->FunctionRecordsSize; ++I) {
392       auto &MappingRecord = FunctionRecords[I];
393
394       // Get the coverage mapping.
395       if (CoverageMappings.size() < MappingRecord.CoverageMappingSize)
396         return instrprof_error::malformed;
397       auto Mapping =
398           CoverageMappings.substr(0, MappingRecord.CoverageMappingSize);
399       CoverageMappings =
400           CoverageMappings.substr(MappingRecord.CoverageMappingSize);
401
402       // Ignore this record if we already have a record that points to the same
403       // function name.
404       // This is useful to ignore the redundant records for the functions
405       // with ODR linkage.
406       if (!UniqueFunctionMappingData.insert(MappingRecord.FunctionNamePtr)
407                .second)
408         continue;
409       StringRef FunctionName;
410       if (auto Err =
411               ProfileNames.get(MappingRecord.FunctionNamePtr,
412                                MappingRecord.FunctionNameSize, FunctionName))
413         return Err;
414       Records.push_back(ObjectFileCoverageMappingReader::ProfileMappingRecord(
415           Version, FunctionName, MappingRecord.FunctionHash, Mapping,
416           FilenamesBegin, Filenames.size() - FilenamesBegin));
417     }
418   }
419
420   return instrprof_error::success;
421 }
422
423 static const char *TestingFormatMagic = "llvmcovmtestdata";
424
425 static std::error_code decodeTestingFormat(StringRef Data,
426                                            SectionData &ProfileNames,
427                                            StringRef &CoverageMapping) {
428   Data = Data.substr(StringRef(TestingFormatMagic).size());
429   if (Data.size() < 1)
430     return instrprof_error::truncated;
431   unsigned N = 0;
432   auto ProfileNamesSize =
433       decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
434   if (N > Data.size())
435     return instrprof_error::malformed;
436   Data = Data.substr(N);
437   if (Data.size() < 1)
438     return instrprof_error::truncated;
439   N = 0;
440   ProfileNames.Address =
441       decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
442   if (N > Data.size())
443     return instrprof_error::malformed;
444   Data = Data.substr(N);
445   if (Data.size() < ProfileNamesSize)
446     return instrprof_error::malformed;
447   ProfileNames.Data = Data.substr(0, ProfileNamesSize);
448   CoverageMapping = Data.substr(ProfileNamesSize);
449   return instrprof_error::success;
450 }
451
452 ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader(
453     std::unique_ptr<MemoryBuffer> &ObjectBuffer, sys::fs::file_magic Type)
454     : CurrentRecord(0) {
455   if (ObjectBuffer->getBuffer().startswith(TestingFormatMagic)) {
456     // This is a special format used for testing.
457     SectionData ProfileNames;
458     StringRef CoverageMapping;
459     if (auto Err = decodeTestingFormat(ObjectBuffer->getBuffer(), ProfileNames,
460                                        CoverageMapping)) {
461       error(Err);
462       return;
463     }
464     error(readCoverageMappingData<uint64_t>(ProfileNames, CoverageMapping,
465                                             MappingRecords, Filenames));
466     Object = OwningBinary<ObjectFile>(std::unique_ptr<ObjectFile>(),
467                                       std::move(ObjectBuffer));
468     return;
469   }
470
471   auto File = object::ObjectFile::createObjectFile(
472       ObjectBuffer->getMemBufferRef(), Type);
473   if (!File)
474     error(File.getError());
475   else
476     Object = OwningBinary<ObjectFile>(std::move(File.get()),
477                                       std::move(ObjectBuffer));
478 }
479
480 std::error_code ObjectFileCoverageMappingReader::readHeader() {
481   const ObjectFile *OF = Object.getBinary();
482   if (!OF)
483     return getError();
484   auto BytesInAddress = OF->getBytesInAddress();
485   if (BytesInAddress != 4 && BytesInAddress != 8)
486     return error(instrprof_error::malformed);
487
488   // Look for the sections that we are interested in.
489   int FoundSectionCount = 0;
490   SectionRef ProfileNames, CoverageMapping;
491   for (const auto &Section : OF->sections()) {
492     StringRef Name;
493     if (auto Err = Section.getName(Name))
494       return Err;
495     if (Name == "__llvm_prf_names") {
496       ProfileNames = Section;
497     } else if (Name == "__llvm_covmap") {
498       CoverageMapping = Section;
499     } else
500       continue;
501     ++FoundSectionCount;
502   }
503   if (FoundSectionCount != 2)
504     return error(instrprof_error::bad_header);
505
506   // Get the contents of the given sections.
507   StringRef Data;
508   if (auto Err = CoverageMapping.getContents(Data))
509     return Err;
510   SectionData ProfileNamesData;
511   if (auto Err = ProfileNamesData.load(ProfileNames))
512     return Err;
513
514   // Load the data from the found sections.
515   std::error_code Err;
516   if (BytesInAddress == 4)
517     Err = readCoverageMappingData<uint32_t>(ProfileNamesData, Data,
518                                             MappingRecords, Filenames);
519   else
520     Err = readCoverageMappingData<uint64_t>(ProfileNamesData, Data,
521                                             MappingRecords, Filenames);
522   if (Err)
523     return error(Err);
524
525   return success();
526 }
527
528 std::error_code
529 ObjectFileCoverageMappingReader::readNextRecord(CoverageMappingRecord &Record) {
530   if (CurrentRecord >= MappingRecords.size())
531     return error(instrprof_error::eof);
532
533   FunctionsFilenames.clear();
534   Expressions.clear();
535   MappingRegions.clear();
536   auto &R = MappingRecords[CurrentRecord];
537   RawCoverageMappingReader Reader(
538       R.CoverageMapping,
539       makeArrayRef(Filenames).slice(R.FilenamesBegin, R.FilenamesSize),
540       FunctionsFilenames, Expressions, MappingRegions);
541   if (auto Err = Reader.read())
542     return Err;
543
544   Record.FunctionName = R.FunctionName;
545   Record.FunctionHash = R.FunctionHash;
546   Record.Filenames = FunctionsFilenames;
547   Record.Expressions = Expressions;
548   Record.MappingRegions = MappingRegions;
549
550   ++CurrentRecord;
551   return success();
552 }