blob: cf6cd58f953360cd7d2aad649ccf3d9d4ee86613 [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"
Justin Bogner43795352015-03-11 02:30:51 +000017#include "llvm/Object/MachOUniversal.h"
Alex Lorenza20a5d52014-07-24 23:57:54 +000018#include "llvm/Object/ObjectFile.h"
Justin Bognerf5846492014-09-20 15:31:51 +000019#include "llvm/Support/Debug.h"
Justin Bogner7b33cc92015-03-16 06:55:45 +000020#include "llvm/Support/Endian.h"
Alex Lorenza20a5d52014-07-24 23:57:54 +000021#include "llvm/Support/LEB128.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000022#include "llvm/Support/raw_ostream.h"
Alex Lorenza20a5d52014-07-24 23:57:54 +000023
24using namespace llvm;
25using namespace coverage;
26using namespace object;
27
Justin Bognerf5846492014-09-20 15:31:51 +000028#define DEBUG_TYPE "coverage-mapping"
29
Alex Lorenza20a5d52014-07-24 23:57:54 +000030void CoverageMappingIterator::increment() {
31 // Check if all the records were read or if an error occurred while reading
32 // the next record.
33 if (Reader->readNextRecord(Record))
34 *this = CoverageMappingIterator();
35}
36
37std::error_code RawCoverageReader::readULEB128(uint64_t &Result) {
38 if (Data.size() < 1)
Justin Bogner367a9f22015-05-06 23:19:35 +000039 return coveragemap_error::truncated;
Alex Lorenza20a5d52014-07-24 23:57:54 +000040 unsigned N = 0;
41 Result = decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
42 if (N > Data.size())
Justin Bogner367a9f22015-05-06 23:19:35 +000043 return coveragemap_error::malformed;
Alex Lorenza20a5d52014-07-24 23:57:54 +000044 Data = Data.substr(N);
Justin Bogner0b130862015-05-06 23:15:55 +000045 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +000046}
47
48std::error_code RawCoverageReader::readIntMax(uint64_t &Result,
49 uint64_t MaxPlus1) {
50 if (auto Err = readULEB128(Result))
51 return Err;
52 if (Result >= MaxPlus1)
Justin Bogner367a9f22015-05-06 23:19:35 +000053 return coveragemap_error::malformed;
Justin Bogner0b130862015-05-06 23:15:55 +000054 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +000055}
56
57std::error_code RawCoverageReader::readSize(uint64_t &Result) {
58 if (auto Err = readULEB128(Result))
59 return Err;
60 // Sanity check the number.
61 if (Result > Data.size())
Justin Bogner367a9f22015-05-06 23:19:35 +000062 return coveragemap_error::malformed;
Justin Bogner0b130862015-05-06 23:15:55 +000063 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +000064}
65
66std::error_code RawCoverageReader::readString(StringRef &Result) {
67 uint64_t Length;
68 if (auto Err = readSize(Length))
69 return Err;
70 Result = Data.substr(0, Length);
71 Data = Data.substr(Length);
Justin Bogner0b130862015-05-06 23:15:55 +000072 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +000073}
74
75std::error_code RawCoverageFilenamesReader::read() {
76 uint64_t NumFilenames;
77 if (auto Err = readSize(NumFilenames))
78 return Err;
79 for (size_t I = 0; I < NumFilenames; ++I) {
80 StringRef Filename;
81 if (auto Err = readString(Filename))
82 return Err;
83 Filenames.push_back(Filename);
84 }
Justin Bogner0b130862015-05-06 23:15:55 +000085 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +000086}
87
88std::error_code RawCoverageMappingReader::decodeCounter(unsigned Value,
89 Counter &C) {
90 auto Tag = Value & Counter::EncodingTagMask;
91 switch (Tag) {
92 case Counter::Zero:
93 C = Counter::getZero();
Justin Bogner0b130862015-05-06 23:15:55 +000094 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +000095 case Counter::CounterValueReference:
96 C = Counter::getCounter(Value >> Counter::EncodingTagBits);
Justin Bogner0b130862015-05-06 23:15:55 +000097 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +000098 default:
99 break;
100 }
101 Tag -= Counter::Expression;
102 switch (Tag) {
103 case CounterExpression::Subtract:
104 case CounterExpression::Add: {
105 auto ID = Value >> Counter::EncodingTagBits;
106 if (ID >= Expressions.size())
Justin Bogner367a9f22015-05-06 23:19:35 +0000107 return coveragemap_error::malformed;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000108 Expressions[ID].Kind = CounterExpression::ExprKind(Tag);
109 C = Counter::getExpression(ID);
110 break;
111 }
112 default:
Justin Bogner367a9f22015-05-06 23:19:35 +0000113 return coveragemap_error::malformed;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000114 }
Justin Bogner0b130862015-05-06 23:15:55 +0000115 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +0000116}
117
118std::error_code RawCoverageMappingReader::readCounter(Counter &C) {
119 uint64_t EncodedCounter;
120 if (auto Err =
121 readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max()))
122 return Err;
123 if (auto Err = decodeCounter(EncodedCounter, C))
124 return Err;
Justin Bogner0b130862015-05-06 23:15:55 +0000125 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +0000126}
127
128static const unsigned EncodingExpansionRegionBit = 1
129 << Counter::EncodingTagBits;
130
131/// \brief Read the sub-array of regions for the given inferred file id.
Ehsan Akhgari29b61ce2014-07-25 02:51:57 +0000132/// \param NumFileIDs the number of file ids that are defined for this
Alex Lorenza20a5d52014-07-24 23:57:54 +0000133/// function.
134std::error_code RawCoverageMappingReader::readMappingRegionsSubArray(
135 std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID,
136 size_t NumFileIDs) {
137 uint64_t NumRegions;
138 if (auto Err = readSize(NumRegions))
139 return Err;
140 unsigned LineStart = 0;
141 for (size_t I = 0; I < NumRegions; ++I) {
142 Counter C;
143 CounterMappingRegion::RegionKind Kind = CounterMappingRegion::CodeRegion;
144
145 // Read the combined counter + region kind.
146 uint64_t EncodedCounterAndRegion;
147 if (auto Err = readIntMax(EncodedCounterAndRegion,
148 std::numeric_limits<unsigned>::max()))
149 return Err;
150 unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
151 uint64_t ExpandedFileID = 0;
152 if (Tag != Counter::Zero) {
153 if (auto Err = decodeCounter(EncodedCounterAndRegion, C))
154 return Err;
155 } else {
156 // Is it an expansion region?
157 if (EncodedCounterAndRegion & EncodingExpansionRegionBit) {
158 Kind = CounterMappingRegion::ExpansionRegion;
159 ExpandedFileID = EncodedCounterAndRegion >>
160 Counter::EncodingCounterTagAndExpansionRegionTagBits;
161 if (ExpandedFileID >= NumFileIDs)
Justin Bogner367a9f22015-05-06 23:19:35 +0000162 return coveragemap_error::malformed;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000163 } else {
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.
168 break;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000169 case CounterMappingRegion::SkippedRegion:
170 Kind = CounterMappingRegion::SkippedRegion;
171 break;
172 default:
Justin Bogner367a9f22015-05-06 23:19:35 +0000173 return coveragemap_error::malformed;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000174 }
175 }
176 }
177
178 // Read the source range.
Justin Bognerde158172015-02-03 21:35:36 +0000179 uint64_t LineStartDelta, ColumnStart, NumLines, ColumnEnd;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000180 if (auto Err =
181 readIntMax(LineStartDelta, std::numeric_limits<unsigned>::max()))
182 return Err;
Justin Bognerde158172015-02-03 21:35:36 +0000183 if (auto Err = readULEB128(ColumnStart))
Alex Lorenza20a5d52014-07-24 23:57:54 +0000184 return Err;
Alex Lorenz1193b5e2014-08-04 18:00:51 +0000185 if (ColumnStart > std::numeric_limits<unsigned>::max())
Justin Bogner367a9f22015-05-06 23:19:35 +0000186 return coveragemap_error::malformed;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000187 if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max()))
188 return Err;
189 if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max()))
190 return Err;
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) {
201 ColumnStart = 1;
202 ColumnEnd = std::numeric_limits<unsigned>::max();
203 }
Justin Bognerf5846492014-09-20 15:31:51 +0000204
205 DEBUG({
206 dbgs() << "Counter in file " << InferredFileID << " " << LineStart << ":"
207 << ColumnStart << " -> " << (LineStart + NumLines) << ":"
208 << ColumnEnd << ", ";
209 if (Kind == CounterMappingRegion::ExpansionRegion)
210 dbgs() << "Expands to file " << ExpandedFileID;
211 else
212 CounterMappingContext(Expressions).dump(C, dbgs());
213 dbgs() << "\n";
214 });
215
Justin Bogner26b31422015-02-03 23:59:33 +0000216 MappingRegions.push_back(CounterMappingRegion(
217 C, InferredFileID, ExpandedFileID, LineStart, ColumnStart,
218 LineStart + NumLines, ColumnEnd, Kind));
Alex Lorenza20a5d52014-07-24 23:57:54 +0000219 }
Justin Bogner0b130862015-05-06 23:15:55 +0000220 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +0000221}
222
Justin Bogner195a4f02015-02-03 00:20:11 +0000223std::error_code RawCoverageMappingReader::read() {
Alex Lorenza20a5d52014-07-24 23:57:54 +0000224
225 // Read the virtual file mapping.
226 llvm::SmallVector<unsigned, 8> VirtualFileMapping;
227 uint64_t NumFileMappings;
228 if (auto Err = readSize(NumFileMappings))
229 return Err;
230 for (size_t I = 0; I < NumFileMappings; ++I) {
231 uint64_t FilenameIndex;
232 if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size()))
233 return Err;
234 VirtualFileMapping.push_back(FilenameIndex);
235 }
236
237 // Construct the files using unique filenames and virtual file mapping.
238 for (auto I : VirtualFileMapping) {
239 Filenames.push_back(TranslationUnitFilenames[I]);
240 }
241
242 // Read the expressions.
243 uint64_t NumExpressions;
244 if (auto Err = readSize(NumExpressions))
245 return Err;
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
248 // are decoded.
249 Expressions.resize(
250 NumExpressions,
251 CounterExpression(CounterExpression::Subtract, Counter(), Counter()));
252 for (size_t I = 0; I < NumExpressions; ++I) {
253 if (auto Err = readCounter(Expressions[I].LHS))
254 return Err;
255 if (auto Err = readCounter(Expressions[I].RHS))
256 return Err;
257 }
258
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()))
264 return Err;
265 }
266
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.
Alex Lorenz251b3e32014-07-29 21:42:24 +0000272 SmallVector<CounterMappingRegion *, 8> FileIDExpansionRegionMapping;
273 FileIDExpansionRegionMapping.resize(VirtualFileMapping.size(), nullptr);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000274 for (unsigned Pass = 1, S = VirtualFileMapping.size(); Pass < S; ++Pass) {
Alex Lorenz251b3e32014-07-29 21:42:24 +0000275 for (auto &R : MappingRegions) {
276 if (R.Kind != CounterMappingRegion::ExpansionRegion)
277 continue;
278 assert(!FileIDExpansionRegionMapping[R.ExpandedFileID]);
279 FileIDExpansionRegionMapping[R.ExpandedFileID] = &R;
280 }
281 for (auto &R : MappingRegions) {
282 if (FileIDExpansionRegionMapping[R.FileID]) {
283 FileIDExpansionRegionMapping[R.FileID]->Count = R.Count;
284 FileIDExpansionRegionMapping[R.FileID] = nullptr;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000285 }
286 }
287 }
288
Justin Bogner0b130862015-05-06 23:15:55 +0000289 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +0000290}
291
Alex Lorenza20a5d52014-07-24 23:57:54 +0000292namespace {
Alex Lorenza20a5d52014-07-24 23:57:54 +0000293
294/// \brief A helper structure to access the data from a section
295/// in an object file.
296struct SectionData {
297 StringRef Data;
298 uint64_t Address;
299
300 std::error_code load(SectionRef &Section) {
301 if (auto Err = Section.getContents(Data))
302 return Err;
Rafael Espindola80291272014-10-08 15:28:58 +0000303 Address = Section.getAddress();
Justin Bogner367a9f22015-05-06 23:19:35 +0000304 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +0000305 }
306
307 std::error_code get(uint64_t Pointer, size_t Size, StringRef &Result) {
308 if (Pointer < Address)
Justin Bogner367a9f22015-05-06 23:19:35 +0000309 return coveragemap_error::malformed;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000310 auto Offset = Pointer - Address;
311 if (Offset + Size > Data.size())
Justin Bogner367a9f22015-05-06 23:19:35 +0000312 return coveragemap_error::malformed;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000313 Result = Data.substr(Pointer - Address, Size);
Justin Bogner367a9f22015-05-06 23:19:35 +0000314 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +0000315 }
316};
317}
318
Justin Bognera4387172015-03-16 21:40:18 +0000319template <typename T, support::endianness Endian>
Alex Lorenza20a5d52014-07-24 23:57:54 +0000320std::error_code readCoverageMappingData(
Alex Lorenze82d89c2014-08-22 22:56:03 +0000321 SectionData &ProfileNames, StringRef Data,
Justin Bognere84891a2015-02-26 20:06:24 +0000322 std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records,
Alex Lorenza20a5d52014-07-24 23:57:54 +0000323 std::vector<StringRef> &Filenames) {
Justin Bogner7b33cc92015-03-16 06:55:45 +0000324 using namespace support;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000325 llvm::DenseSet<T> UniqueFunctionMappingData;
326
Alex Lorenza20a5d52014-07-24 23:57:54 +0000327 // Read the records in the coverage data section.
Justin Bogner7b33cc92015-03-16 06:55:45 +0000328 for (const char *Buf = Data.data(), *End = Buf + Data.size(); Buf < End;) {
329 if (Buf + 4 * sizeof(uint32_t) > End)
Justin Bogner367a9f22015-05-06 23:19:35 +0000330 return coveragemap_error::malformed;
Justin Bognera4387172015-03-16 21:40:18 +0000331 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);
Justin Bogner7b33cc92015-03-16 06:55:45 +0000335
336 switch (Version) {
Alex Lorenza20a5d52014-07-24 23:57:54 +0000337 case CoverageMappingVersion1:
338 break;
339 default:
Justin Bogner367a9f22015-05-06 23:19:35 +0000340 return coveragemap_error::unsupported_version;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000341 }
Alex Lorenza20a5d52014-07-24 23:57:54 +0000342
Justin Bogner7b33cc92015-03-16 06:55:45 +0000343 // 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;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000347
348 // Get the filenames.
Justin Bogner7b33cc92015-03-16 06:55:45 +0000349 if (Buf + FilenamesSize > End)
Justin Bogner367a9f22015-05-06 23:19:35 +0000350 return coveragemap_error::malformed;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000351 size_t FilenamesBegin = Filenames.size();
Justin Bogner7b33cc92015-03-16 06:55:45 +0000352 RawCoverageFilenamesReader Reader(StringRef(Buf, FilenamesSize), Filenames);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000353 if (auto Err = Reader.read())
354 return Err;
Justin Bogner7b33cc92015-03-16 06:55:45 +0000355 Buf += FilenamesSize;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000356
Justin Bogner7b33cc92015-03-16 06:55:45 +0000357 // We'll read the coverage mapping records in the loop below.
358 const char *CovBuf = Buf;
359 Buf += CoverageSize;
360 const char *CovEnd = Buf;
361 if (Buf > End)
Justin Bogner367a9f22015-05-06 23:19:35 +0000362 return coveragemap_error::malformed;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000363
Justin Bogner7b33cc92015-03-16 06:55:45 +0000364 while (FunBuf < FunEnd) {
365 // Read the function information
Justin Bognera4387172015-03-16 21:40:18 +0000366 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);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000370
Justin Bogner7b33cc92015-03-16 06:55:45 +0000371 // Now use that to read the coverage data.
372 if (CovBuf + DataSize > CovEnd)
Justin Bogner367a9f22015-05-06 23:19:35 +0000373 return coveragemap_error::malformed;
Justin Bogner7b33cc92015-03-16 06:55:45 +0000374 auto Mapping = StringRef(CovBuf, DataSize);
375 CovBuf += DataSize;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000376
377 // Ignore this record if we already have a record that points to the same
Justin Bogner7b33cc92015-03-16 06:55:45 +0000378 // function name. This is useful to ignore the redundant records for the
379 // functions with ODR linkage.
380 if (!UniqueFunctionMappingData.insert(NamePtr).second)
Alex Lorenza20a5d52014-07-24 23:57:54 +0000381 continue;
Justin Bogner7b33cc92015-03-16 06:55:45 +0000382
383 // Finally, grab the name and create a record.
384 StringRef FuncName;
385 if (std::error_code EC = ProfileNames.get(NamePtr, NameSize, FuncName))
386 return EC;
Justin Bognere84891a2015-02-26 20:06:24 +0000387 Records.push_back(BinaryCoverageReader::ProfileMappingRecord(
Justin Bogner7b33cc92015-03-16 06:55:45 +0000388 CoverageMappingVersion(Version), FuncName, FuncHash, Mapping,
Alex Lorenz936b99c2014-08-21 19:23:25 +0000389 FilenamesBegin, Filenames.size() - FilenamesBegin));
Alex Lorenza20a5d52014-07-24 23:57:54 +0000390 }
391 }
392
Justin Bogner367a9f22015-05-06 23:19:35 +0000393 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +0000394}
395
Alex Lorenze82d89c2014-08-22 22:56:03 +0000396static const char *TestingFormatMagic = "llvmcovmtestdata";
397
Justin Bogner43e51632015-02-26 20:06:28 +0000398static std::error_code loadTestingFormat(StringRef Data,
399 SectionData &ProfileNames,
400 StringRef &CoverageMapping,
Justin Bognera4387172015-03-16 21:40:18 +0000401 uint8_t &BytesInAddress,
402 support::endianness &Endian) {
Justin Bogner43e51632015-02-26 20:06:28 +0000403 BytesInAddress = 8;
Justin Bognera4387172015-03-16 21:40:18 +0000404 Endian = support::endianness::little;
Justin Bogner43e51632015-02-26 20:06:28 +0000405
Alex Lorenze82d89c2014-08-22 22:56:03 +0000406 Data = Data.substr(StringRef(TestingFormatMagic).size());
407 if (Data.size() < 1)
Justin Bogner367a9f22015-05-06 23:19:35 +0000408 return coveragemap_error::truncated;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000409 unsigned N = 0;
410 auto ProfileNamesSize =
411 decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
412 if (N > Data.size())
Justin Bogner367a9f22015-05-06 23:19:35 +0000413 return coveragemap_error::malformed;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000414 Data = Data.substr(N);
415 if (Data.size() < 1)
Justin Bogner367a9f22015-05-06 23:19:35 +0000416 return coveragemap_error::truncated;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000417 N = 0;
418 ProfileNames.Address =
419 decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
420 if (N > Data.size())
Justin Bogner367a9f22015-05-06 23:19:35 +0000421 return coveragemap_error::malformed;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000422 Data = Data.substr(N);
423 if (Data.size() < ProfileNamesSize)
Justin Bogner367a9f22015-05-06 23:19:35 +0000424 return coveragemap_error::malformed;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000425 ProfileNames.Data = Data.substr(0, ProfileNamesSize);
426 CoverageMapping = Data.substr(ProfileNamesSize);
Justin Bogner367a9f22015-05-06 23:19:35 +0000427 return std::error_code();
Alex Lorenze82d89c2014-08-22 22:56:03 +0000428}
429
Justin Bogner5a5c3812015-05-07 00:31:58 +0000430static ErrorOr<SectionRef> lookupSection(ObjectFile &OF, StringRef Name) {
431 StringRef FoundName;
432 for (const auto &Section : OF.sections()) {
433 if (auto EC = Section.getName(FoundName))
434 return EC;
435 if (FoundName == Name)
436 return Section;
437 }
438 return coveragemap_error::no_data_found;
439}
440
Justin Bogner43e51632015-02-26 20:06:28 +0000441static std::error_code loadBinaryFormat(MemoryBufferRef ObjectBuffer,
442 SectionData &ProfileNames,
443 StringRef &CoverageMapping,
Justin Bogner43795352015-03-11 02:30:51 +0000444 uint8_t &BytesInAddress,
Justin Bognera4387172015-03-16 21:40:18 +0000445 support::endianness &Endian,
Justin Bogner43795352015-03-11 02:30:51 +0000446 Triple::ArchType Arch) {
447 auto BinOrErr = object::createBinary(ObjectBuffer);
448 if (std::error_code EC = BinOrErr.getError())
Justin Bogner43e51632015-02-26 20:06:28 +0000449 return EC;
Justin Bogner43795352015-03-11 02:30:51 +0000450 auto Bin = std::move(BinOrErr.get());
451 std::unique_ptr<ObjectFile> OF;
452 if (auto *Universal = dyn_cast<object::MachOUniversalBinary>(Bin.get())) {
453 // If we have a universal binary, try to look up the object for the
454 // appropriate architecture.
455 auto ObjectFileOrErr = Universal->getObjectForArch(Arch);
456 if (std::error_code EC = ObjectFileOrErr.getError())
457 return EC;
458 OF = std::move(ObjectFileOrErr.get());
459 } else if (isa<object::ObjectFile>(Bin.get())) {
460 // For any other object file, upcast and take ownership.
461 OF.reset(cast<object::ObjectFile>(Bin.release()));
462 // If we've asked for a particular arch, make sure they match.
463 if (Arch != Triple::ArchType::UnknownArch && OF->getArch() != Arch)
464 return object_error::arch_not_found;
465 } else
466 // We can only handle object files.
Justin Bogner367a9f22015-05-06 23:19:35 +0000467 return coveragemap_error::malformed;
Justin Bogner43795352015-03-11 02:30:51 +0000468
469 // The coverage uses native pointer sizes for the object it's written in.
Justin Bogner43e51632015-02-26 20:06:28 +0000470 BytesInAddress = OF->getBytesInAddress();
Justin Bognera4387172015-03-16 21:40:18 +0000471 Endian = OF->isLittleEndian() ? support::endianness::little
472 : support::endianness::big;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000473
474 // Look for the sections that we are interested in.
Justin Bogner5a5c3812015-05-07 00:31:58 +0000475 auto NamesSection = lookupSection(*OF, "__llvm_prf_names");
476 if (auto EC = NamesSection.getError())
477 return EC;
478 auto CoverageSection = lookupSection(*OF, "__llvm_covmap");
479 if (auto EC = CoverageSection.getError())
480 return EC;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000481
Alex Lorenze82d89c2014-08-22 22:56:03 +0000482 // Get the contents of the given sections.
Justin Bogner5a5c3812015-05-07 00:31:58 +0000483 if (std::error_code EC = CoverageSection->getContents(CoverageMapping))
Justin Bogner43e51632015-02-26 20:06:28 +0000484 return EC;
Justin Bogner5a5c3812015-05-07 00:31:58 +0000485 if (std::error_code EC = ProfileNames.load(*NamesSection))
Justin Bogner43e51632015-02-26 20:06:28 +0000486 return EC;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000487
Justin Bogner43e51632015-02-26 20:06:28 +0000488 return std::error_code();
489}
490
491ErrorOr<std::unique_ptr<BinaryCoverageReader>>
Justin Bogner43795352015-03-11 02:30:51 +0000492BinaryCoverageReader::create(std::unique_ptr<MemoryBuffer> &ObjectBuffer,
493 Triple::ArchType Arch) {
Justin Bogner43e51632015-02-26 20:06:28 +0000494 std::unique_ptr<BinaryCoverageReader> Reader(new BinaryCoverageReader());
495
496 SectionData Profile;
497 StringRef Coverage;
498 uint8_t BytesInAddress;
Justin Bognera4387172015-03-16 21:40:18 +0000499 support::endianness Endian;
Justin Bogner43e51632015-02-26 20:06:28 +0000500 std::error_code EC;
501 if (ObjectBuffer->getBuffer().startswith(TestingFormatMagic))
502 // This is a special format used for testing.
503 EC = loadTestingFormat(ObjectBuffer->getBuffer(), Profile, Coverage,
Justin Bognera4387172015-03-16 21:40:18 +0000504 BytesInAddress, Endian);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000505 else
Justin Bogner43e51632015-02-26 20:06:28 +0000506 EC = loadBinaryFormat(ObjectBuffer->getMemBufferRef(), Profile, Coverage,
Justin Bognera4387172015-03-16 21:40:18 +0000507 BytesInAddress, Endian, Arch);
Justin Bogner43e51632015-02-26 20:06:28 +0000508 if (EC)
509 return EC;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000510
Justin Bognera4387172015-03-16 21:40:18 +0000511 if (BytesInAddress == 4 && Endian == support::endianness::little)
512 EC = readCoverageMappingData<uint32_t, support::endianness::little>(
Justin Bogner43e51632015-02-26 20:06:28 +0000513 Profile, Coverage, Reader->MappingRecords, Reader->Filenames);
Justin Bognera4387172015-03-16 21:40:18 +0000514 else if (BytesInAddress == 4 && Endian == support::endianness::big)
515 EC = readCoverageMappingData<uint32_t, support::endianness::big>(
516 Profile, Coverage, Reader->MappingRecords, Reader->Filenames);
517 else if (BytesInAddress == 8 && Endian == support::endianness::little)
518 EC = readCoverageMappingData<uint64_t, support::endianness::little>(
519 Profile, Coverage, Reader->MappingRecords, Reader->Filenames);
520 else if (BytesInAddress == 8 && Endian == support::endianness::big)
521 EC = readCoverageMappingData<uint64_t, support::endianness::big>(
Justin Bogner43e51632015-02-26 20:06:28 +0000522 Profile, Coverage, Reader->MappingRecords, Reader->Filenames);
523 else
Justin Bogner367a9f22015-05-06 23:19:35 +0000524 return coveragemap_error::malformed;
Justin Bogner43e51632015-02-26 20:06:28 +0000525 if (EC)
526 return EC;
527 return std::move(Reader);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000528}
529
530std::error_code
Justin Bognere84891a2015-02-26 20:06:24 +0000531BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) {
Alex Lorenza20a5d52014-07-24 23:57:54 +0000532 if (CurrentRecord >= MappingRecords.size())
Justin Bogner367a9f22015-05-06 23:19:35 +0000533 return coveragemap_error::eof;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000534
535 FunctionsFilenames.clear();
536 Expressions.clear();
537 MappingRegions.clear();
538 auto &R = MappingRecords[CurrentRecord];
539 RawCoverageMappingReader Reader(
Justin Bogner195a4f02015-02-03 00:20:11 +0000540 R.CoverageMapping,
Justin Bogner346359d2015-02-03 00:00:00 +0000541 makeArrayRef(Filenames).slice(R.FilenamesBegin, R.FilenamesSize),
Alex Lorenza20a5d52014-07-24 23:57:54 +0000542 FunctionsFilenames, Expressions, MappingRegions);
Justin Bogner195a4f02015-02-03 00:20:11 +0000543 if (auto Err = Reader.read())
Alex Lorenza20a5d52014-07-24 23:57:54 +0000544 return Err;
Justin Bogner195a4f02015-02-03 00:20:11 +0000545
546 Record.FunctionName = R.FunctionName;
Alex Lorenz936b99c2014-08-21 19:23:25 +0000547 Record.FunctionHash = R.FunctionHash;
Justin Bogner195a4f02015-02-03 00:20:11 +0000548 Record.Filenames = FunctionsFilenames;
549 Record.Expressions = Expressions;
550 Record.MappingRegions = MappingRegions;
551
Alex Lorenza20a5d52014-07-24 23:57:54 +0000552 ++CurrentRecord;
Justin Bogner43e51632015-02-26 20:06:28 +0000553 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +0000554}