blob: 83c707fa43d0877cfc2de45d10456aacd56518fe [file] [log] [blame]
Alex Lorenza20a5d52014-07-24 23:57:54 +00001//=-- 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/LEB128.h"
19
20using namespace llvm;
21using namespace coverage;
22using namespace object;
23
24void CoverageMappingIterator::increment() {
25 // Check if all the records were read or if an error occurred while reading
26 // the next record.
27 if (Reader->readNextRecord(Record))
28 *this = CoverageMappingIterator();
29}
30
31std::error_code RawCoverageReader::readULEB128(uint64_t &Result) {
32 if (Data.size() < 1)
33 return error(instrprof_error::truncated);
34 unsigned N = 0;
35 Result = decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
36 if (N > Data.size())
37 return error(instrprof_error::malformed);
38 Data = Data.substr(N);
39 return success();
40}
41
42std::error_code RawCoverageReader::readIntMax(uint64_t &Result,
43 uint64_t MaxPlus1) {
44 if (auto Err = readULEB128(Result))
45 return Err;
46 if (Result >= MaxPlus1)
47 return error(instrprof_error::malformed);
48 return success();
49}
50
51std::error_code RawCoverageReader::readSize(uint64_t &Result) {
52 if (auto Err = readULEB128(Result))
53 return Err;
54 // Sanity check the number.
55 if (Result > Data.size())
56 return error(instrprof_error::malformed);
57 return success();
58}
59
60std::error_code RawCoverageReader::readString(StringRef &Result) {
61 uint64_t Length;
62 if (auto Err = readSize(Length))
63 return Err;
64 Result = Data.substr(0, Length);
65 Data = Data.substr(Length);
66 return success();
67}
68
69std::error_code RawCoverageFilenamesReader::read() {
70 uint64_t NumFilenames;
71 if (auto Err = readSize(NumFilenames))
72 return Err;
73 for (size_t I = 0; I < NumFilenames; ++I) {
74 StringRef Filename;
75 if (auto Err = readString(Filename))
76 return Err;
77 Filenames.push_back(Filename);
78 }
79 return success();
80}
81
82std::error_code RawCoverageMappingReader::decodeCounter(unsigned Value,
83 Counter &C) {
84 auto Tag = Value & Counter::EncodingTagMask;
85 switch (Tag) {
86 case Counter::Zero:
87 C = Counter::getZero();
88 return success();
89 case Counter::CounterValueReference:
90 C = Counter::getCounter(Value >> Counter::EncodingTagBits);
91 return success();
92 default:
93 break;
94 }
95 Tag -= Counter::Expression;
96 switch (Tag) {
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);
104 break;
105 }
106 default:
107 return error(instrprof_error::malformed);
108 }
109 return success();
110}
111
112std::error_code RawCoverageMappingReader::readCounter(Counter &C) {
113 uint64_t EncodedCounter;
114 if (auto Err =
115 readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max()))
116 return Err;
117 if (auto Err = decodeCounter(EncodedCounter, C))
118 return Err;
119 return success();
120}
121
122static const unsigned EncodingExpansionRegionBit = 1
123 << Counter::EncodingTagBits;
124
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
127/// function.
128std::error_code RawCoverageMappingReader::readMappingRegionsSubArray(
129 std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID,
130 size_t NumFileIDs) {
131 uint64_t NumRegions;
132 if (auto Err = readSize(NumRegions))
133 return Err;
134 unsigned LineStart = 0;
135 for (size_t I = 0; I < NumRegions; ++I) {
136 Counter C;
137 CounterMappingRegion::RegionKind Kind = CounterMappingRegion::CodeRegion;
138
139 // Read the combined counter + region kind.
140 uint64_t EncodedCounterAndRegion;
141 if (auto Err = readIntMax(EncodedCounterAndRegion,
142 std::numeric_limits<unsigned>::max()))
143 return Err;
144 unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
145 uint64_t ExpandedFileID = 0;
146 if (Tag != Counter::Zero) {
147 if (auto Err = decodeCounter(EncodedCounterAndRegion, C))
148 return Err;
149 } else {
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);
157 } else {
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.
162 break;
163 case CounterMappingRegion::EmptyRegion:
164 Kind = CounterMappingRegion::EmptyRegion;
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 =
181 readIntMax(ColumnStart, std::numeric_limits<unsigned>::max()))
182 return Err;
183 if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max()))
184 return Err;
185 if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max()))
186 return Err;
187 LineStart += LineStartDelta;
188 // Adjust the column locations for the empty regions that are supposed to
189 // cover whole lines. Those regions should be encoded with the
190 // column range (1 -> std::numeric_limits<unsigned>::max()), but because
191 // the encoded std::numeric_limits<unsigned>::max() is several bytes long,
192 // we set the column range to (0 -> 0) to ensure that the column start and
193 // column end take up one byte each.
194 // The std::numeric_limits<unsigned>::max() is used to represent a column
195 // position at the end of the line without knowing the length of that line.
196 if (ColumnStart == 0 && ColumnEnd == 0) {
197 ColumnStart = 1;
198 ColumnEnd = std::numeric_limits<unsigned>::max();
199 }
200 MappingRegions.push_back(
201 CounterMappingRegion(C, InferredFileID, LineStart, ColumnStart,
202 LineStart + NumLines, ColumnEnd, Kind));
203 MappingRegions.back().ExpandedFileID = ExpandedFileID;
204 }
205 return success();
206}
207
208std::error_code RawCoverageMappingReader::read(CoverageMappingRecord &Record) {
209
210 // Read the virtual file mapping.
211 llvm::SmallVector<unsigned, 8> VirtualFileMapping;
212 uint64_t NumFileMappings;
213 if (auto Err = readSize(NumFileMappings))
214 return Err;
215 for (size_t I = 0; I < NumFileMappings; ++I) {
216 uint64_t FilenameIndex;
217 if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size()))
218 return Err;
219 VirtualFileMapping.push_back(FilenameIndex);
220 }
221
222 // Construct the files using unique filenames and virtual file mapping.
223 for (auto I : VirtualFileMapping) {
224 Filenames.push_back(TranslationUnitFilenames[I]);
225 }
226
227 // Read the expressions.
228 uint64_t NumExpressions;
229 if (auto Err = readSize(NumExpressions))
230 return Err;
231 // Create an array of dummy expressions that get the proper counters
232 // when the expressions are read, and the proper kinds when the counters
233 // are decoded.
234 Expressions.resize(
235 NumExpressions,
236 CounterExpression(CounterExpression::Subtract, Counter(), Counter()));
237 for (size_t I = 0; I < NumExpressions; ++I) {
238 if (auto Err = readCounter(Expressions[I].LHS))
239 return Err;
240 if (auto Err = readCounter(Expressions[I].RHS))
241 return Err;
242 }
243
244 // Read the mapping regions sub-arrays.
245 for (unsigned InferredFileID = 0, S = VirtualFileMapping.size();
246 InferredFileID < S; ++InferredFileID) {
247 if (auto Err = readMappingRegionsSubArray(MappingRegions, InferredFileID,
248 VirtualFileMapping.size()))
249 return Err;
250 }
251
252 // Set the counters for the expansion regions.
253 // i.e. Counter of expansion region = counter of the first region
254 // from the expanded file.
255 // Perform multiple passes to correctly propagate the counters through
256 // all the nested expansion regions.
257 for (unsigned Pass = 1, S = VirtualFileMapping.size(); Pass < S; ++Pass) {
258 for (auto &I : MappingRegions) {
259 if (I.Kind == CounterMappingRegion::ExpansionRegion) {
260 for (const auto &J : MappingRegions) {
261 if (J.FileID == I.ExpandedFileID) {
262 I.Count = J.Count;
263 break;
264 }
265 }
266 }
267 }
268 }
269
270 Record.FunctionName = FunctionName;
271 Record.Filenames = Filenames;
272 Record.Expressions = Expressions;
273 Record.MappingRegions = MappingRegions;
274 return success();
275}
276
277ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader(
278 StringRef FileName)
279 : CurrentRecord(0) {
280 auto File = llvm::object::ObjectFile::createObjectFile(FileName);
281 if (!File)
282 error(File.getError());
283 else
284 Object.reset(File.get());
285}
286
287ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader(
288 std::unique_ptr<MemoryBuffer> &ObjectBuffer, sys::fs::file_magic Type)
289 : CurrentRecord(0) {
290 auto File = llvm::object::ObjectFile::createObjectFile(ObjectBuffer, Type);
291 if (!File)
292 error(File.getError());
293 else
294 Object.reset(File.get());
295}
296
297namespace {
298/// \brief The coverage mapping data for a single function.
299/// It points to the function's name.
300template <typename IntPtrT> struct CoverageMappingFunctionRecord {
301 IntPtrT FunctionNamePtr;
302 uint32_t FunctionNameSize;
303 uint32_t CoverageMappingSize;
304};
305
306/// \brief The coverage mapping data for a single translation unit.
307/// It points to the array of function coverage mapping records and the encoded
308/// filenames array.
309template <typename IntPtrT> struct CoverageMappingTURecord {
310 uint32_t FunctionRecordsSize;
311 uint32_t FilenamesSize;
312 uint32_t CoverageMappingsSize;
313 uint32_t Version;
314};
315
316/// \brief A helper structure to access the data from a section
317/// in an object file.
318struct SectionData {
319 StringRef Data;
320 uint64_t Address;
321
322 std::error_code load(SectionRef &Section) {
323 if (auto Err = Section.getContents(Data))
324 return Err;
325 return Section.getAddress(Address);
326 }
327
328 std::error_code get(uint64_t Pointer, size_t Size, StringRef &Result) {
329 if (Pointer < Address)
330 return instrprof_error::malformed;
331 auto Offset = Pointer - Address;
332 if (Offset + Size > Data.size())
333 return instrprof_error::malformed;
334 Result = Data.substr(Pointer - Address, Size);
335 return instrprof_error::success;
336 }
337};
338}
339
340template <typename T>
341std::error_code readCoverageMappingData(
342 SectionRef &ProfileNames, SectionRef &CoverageMapping,
343 std::vector<ObjectFileCoverageMappingReader::ProfileMappingRecord> &Records,
344 std::vector<StringRef> &Filenames) {
345 llvm::DenseSet<T> UniqueFunctionMappingData;
346
347 // Get the contents of the given sections.
348 StringRef Data;
349 if (auto Err = CoverageMapping.getContents(Data))
350 return Err;
351 SectionData ProfileNamesData;
352 if (auto Err = ProfileNamesData.load(ProfileNames))
353 return Err;
354
355 // Read the records in the coverage data section.
356 while (!Data.empty()) {
357 if (Data.size() < sizeof(CoverageMappingTURecord<T>))
358 return instrprof_error::malformed;
359 auto TU = reinterpret_cast<const CoverageMappingTURecord<T> *>(Data.data());
360 Data = Data.substr(sizeof(CoverageMappingTURecord<T>));
361 switch (TU->Version) {
362 case CoverageMappingVersion1:
363 break;
364 default:
365 return instrprof_error::unsupported_version;
366 }
367 auto Version = CoverageMappingVersion(TU->Version);
368
369 // Get the function records.
370 auto FunctionRecords =
371 reinterpret_cast<const CoverageMappingFunctionRecord<T> *>(Data.data());
372 if (Data.size() <
373 sizeof(CoverageMappingFunctionRecord<T>) * TU->FunctionRecordsSize)
374 return instrprof_error::malformed;
375 Data = Data.substr(sizeof(CoverageMappingFunctionRecord<T>) *
376 TU->FunctionRecordsSize);
377
378 // Get the filenames.
379 if (Data.size() < TU->FilenamesSize)
380 return instrprof_error::malformed;
381 auto RawFilenames = Data.substr(0, TU->FilenamesSize);
382 Data = Data.substr(TU->FilenamesSize);
383 size_t FilenamesBegin = Filenames.size();
384 RawCoverageFilenamesReader Reader(RawFilenames, Filenames);
385 if (auto Err = Reader.read())
386 return Err;
387
388 // Get the coverage mappings.
389 if (Data.size() < TU->CoverageMappingsSize)
390 return instrprof_error::malformed;
391 auto CoverageMappings = Data.substr(0, TU->CoverageMappingsSize);
392 Data = Data.substr(TU->CoverageMappingsSize);
393
394 for (unsigned I = 0; I < TU->FunctionRecordsSize; ++I) {
395 auto &MappingRecord = FunctionRecords[I];
396
397 // Get the coverage mapping.
398 if (CoverageMappings.size() < MappingRecord.CoverageMappingSize)
399 return instrprof_error::malformed;
400 auto Mapping =
401 CoverageMappings.substr(0, MappingRecord.CoverageMappingSize);
402 CoverageMappings =
403 CoverageMappings.substr(MappingRecord.CoverageMappingSize);
404
405 // Ignore this record if we already have a record that points to the same
406 // function name.
407 // This is useful to ignore the redundant records for the functions
408 // with ODR linkage.
409 if (UniqueFunctionMappingData.count(MappingRecord.FunctionNamePtr))
410 continue;
411 UniqueFunctionMappingData.insert(MappingRecord.FunctionNamePtr);
412 StringRef FunctionName;
413 if (auto Err = ProfileNamesData.get(MappingRecord.FunctionNamePtr,
414 MappingRecord.FunctionNameSize,
415 FunctionName))
416 return Err;
417 Records.push_back(ObjectFileCoverageMappingReader::ProfileMappingRecord(
418 Version, FunctionName, Mapping, FilenamesBegin,
419 Filenames.size() - FilenamesBegin));
420 }
421 }
422
423 return instrprof_error::success;
424}
425
426std::error_code ObjectFileCoverageMappingReader::readHeader() {
427 if (!Object)
428 return getError();
429 auto BytesInAddress = Object->getBytesInAddress();
430 if (BytesInAddress != 4 && BytesInAddress != 8)
431 return error(instrprof_error::malformed);
432
433 // Look for the sections that we are interested in.
434 int FoundSectionCount = 0;
435 SectionRef ProfileNames, CoverageMapping;
436 for (const auto &Section : Object->sections()) {
437 StringRef Name;
438 if (auto Err = Section.getName(Name))
439 return Err;
440 if (Name == "__llvm_prf_names") {
441 ProfileNames = Section;
442 } else if (Name == "__llvm_covmap") {
443 CoverageMapping = Section;
444 } else
445 continue;
446 ++FoundSectionCount;
447 }
448 if (FoundSectionCount != 2)
449 return error(instrprof_error::bad_header);
450
451 // Load the data from the found sections.
452 std::error_code Err;
453 if (BytesInAddress == 4)
454 Err = readCoverageMappingData<uint32_t>(ProfileNames, CoverageMapping,
455 MappingRecords, Filenames);
456 else
457 Err = readCoverageMappingData<uint64_t>(ProfileNames, CoverageMapping,
458 MappingRecords, Filenames);
459 if (Err)
460 return error(Err);
461
462 return success();
463}
464
465std::error_code
466ObjectFileCoverageMappingReader::readNextRecord(CoverageMappingRecord &Record) {
467 if (CurrentRecord >= MappingRecords.size())
468 return error(instrprof_error::eof);
469
470 FunctionsFilenames.clear();
471 Expressions.clear();
472 MappingRegions.clear();
473 auto &R = MappingRecords[CurrentRecord];
474 RawCoverageMappingReader Reader(
475 R.FunctionName, R.CoverageMapping,
476 makeArrayRef(Filenames.data() + R.FilenamesBegin, R.FilenamesSize),
477 FunctionsFilenames, Expressions, MappingRegions);
478 if (auto Err = Reader.read(Record))
479 return Err;
480 ++CurrentRecord;
481 return success();
482}