blob: 334a3f51ec9e5762122890dba46009f7d1ec9a49 [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"
Justin Bognerd49d8ee2015-06-05 01:23:42 +000022#include "llvm/Support/MathExtras.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000023#include "llvm/Support/raw_ostream.h"
Alex Lorenza20a5d52014-07-24 23:57:54 +000024
25using namespace llvm;
26using namespace coverage;
27using namespace object;
28
Justin Bognerf5846492014-09-20 15:31:51 +000029#define DEBUG_TYPE "coverage-mapping"
30
Alex Lorenza20a5d52014-07-24 23:57:54 +000031void CoverageMappingIterator::increment() {
32 // Check if all the records were read or if an error occurred while reading
33 // the next record.
34 if (Reader->readNextRecord(Record))
35 *this = CoverageMappingIterator();
36}
37
38std::error_code RawCoverageReader::readULEB128(uint64_t &Result) {
39 if (Data.size() < 1)
Justin Bogner367a9f22015-05-06 23:19:35 +000040 return coveragemap_error::truncated;
Alex Lorenza20a5d52014-07-24 23:57:54 +000041 unsigned N = 0;
42 Result = decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
43 if (N > Data.size())
Justin Bogner367a9f22015-05-06 23:19:35 +000044 return coveragemap_error::malformed;
Alex Lorenza20a5d52014-07-24 23:57:54 +000045 Data = Data.substr(N);
Justin Bogner0b130862015-05-06 23:15:55 +000046 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +000047}
48
49std::error_code RawCoverageReader::readIntMax(uint64_t &Result,
50 uint64_t MaxPlus1) {
51 if (auto Err = readULEB128(Result))
52 return Err;
53 if (Result >= MaxPlus1)
Justin Bogner367a9f22015-05-06 23:19:35 +000054 return coveragemap_error::malformed;
Justin Bogner0b130862015-05-06 23:15:55 +000055 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +000056}
57
58std::error_code RawCoverageReader::readSize(uint64_t &Result) {
59 if (auto Err = readULEB128(Result))
60 return Err;
61 // Sanity check the number.
62 if (Result > Data.size())
Justin Bogner367a9f22015-05-06 23:19:35 +000063 return coveragemap_error::malformed;
Justin Bogner0b130862015-05-06 23:15:55 +000064 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +000065}
66
67std::error_code RawCoverageReader::readString(StringRef &Result) {
68 uint64_t Length;
69 if (auto Err = readSize(Length))
70 return Err;
71 Result = Data.substr(0, Length);
72 Data = Data.substr(Length);
Justin Bogner0b130862015-05-06 23:15:55 +000073 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +000074}
75
76std::error_code RawCoverageFilenamesReader::read() {
77 uint64_t NumFilenames;
78 if (auto Err = readSize(NumFilenames))
79 return Err;
80 for (size_t I = 0; I < NumFilenames; ++I) {
81 StringRef Filename;
82 if (auto Err = readString(Filename))
83 return Err;
84 Filenames.push_back(Filename);
85 }
Justin Bogner0b130862015-05-06 23:15:55 +000086 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +000087}
88
89std::error_code RawCoverageMappingReader::decodeCounter(unsigned Value,
90 Counter &C) {
91 auto Tag = Value & Counter::EncodingTagMask;
92 switch (Tag) {
93 case Counter::Zero:
94 C = Counter::getZero();
Justin Bogner0b130862015-05-06 23:15:55 +000095 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +000096 case Counter::CounterValueReference:
97 C = Counter::getCounter(Value >> Counter::EncodingTagBits);
Justin Bogner0b130862015-05-06 23:15:55 +000098 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +000099 default:
100 break;
101 }
102 Tag -= Counter::Expression;
103 switch (Tag) {
104 case CounterExpression::Subtract:
105 case CounterExpression::Add: {
106 auto ID = Value >> Counter::EncodingTagBits;
107 if (ID >= Expressions.size())
Justin Bogner367a9f22015-05-06 23:19:35 +0000108 return coveragemap_error::malformed;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000109 Expressions[ID].Kind = CounterExpression::ExprKind(Tag);
110 C = Counter::getExpression(ID);
111 break;
112 }
113 default:
Justin Bogner367a9f22015-05-06 23:19:35 +0000114 return coveragemap_error::malformed;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000115 }
Justin Bogner0b130862015-05-06 23:15:55 +0000116 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +0000117}
118
119std::error_code RawCoverageMappingReader::readCounter(Counter &C) {
120 uint64_t EncodedCounter;
121 if (auto Err =
122 readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max()))
123 return Err;
124 if (auto Err = decodeCounter(EncodedCounter, C))
125 return Err;
Justin Bogner0b130862015-05-06 23:15:55 +0000126 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +0000127}
128
129static const unsigned EncodingExpansionRegionBit = 1
130 << Counter::EncodingTagBits;
131
132/// \brief Read the sub-array of regions for the given inferred file id.
Ehsan Akhgari29b61ce2014-07-25 02:51:57 +0000133/// \param NumFileIDs the number of file ids that are defined for this
Alex Lorenza20a5d52014-07-24 23:57:54 +0000134/// function.
135std::error_code RawCoverageMappingReader::readMappingRegionsSubArray(
136 std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID,
137 size_t NumFileIDs) {
138 uint64_t NumRegions;
139 if (auto Err = readSize(NumRegions))
140 return Err;
141 unsigned LineStart = 0;
142 for (size_t I = 0; I < NumRegions; ++I) {
143 Counter C;
144 CounterMappingRegion::RegionKind Kind = CounterMappingRegion::CodeRegion;
145
146 // Read the combined counter + region kind.
147 uint64_t EncodedCounterAndRegion;
148 if (auto Err = readIntMax(EncodedCounterAndRegion,
149 std::numeric_limits<unsigned>::max()))
150 return Err;
151 unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
152 uint64_t ExpandedFileID = 0;
153 if (Tag != Counter::Zero) {
154 if (auto Err = decodeCounter(EncodedCounterAndRegion, C))
155 return Err;
156 } else {
157 // Is it an expansion region?
158 if (EncodedCounterAndRegion & EncodingExpansionRegionBit) {
159 Kind = CounterMappingRegion::ExpansionRegion;
160 ExpandedFileID = EncodedCounterAndRegion >>
161 Counter::EncodingCounterTagAndExpansionRegionTagBits;
162 if (ExpandedFileID >= NumFileIDs)
Justin Bogner367a9f22015-05-06 23:19:35 +0000163 return coveragemap_error::malformed;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000164 } else {
165 switch (EncodedCounterAndRegion >>
166 Counter::EncodingCounterTagAndExpansionRegionTagBits) {
167 case CounterMappingRegion::CodeRegion:
168 // Don't do anything when we have a code region with a zero counter.
169 break;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000170 case CounterMappingRegion::SkippedRegion:
171 Kind = CounterMappingRegion::SkippedRegion;
172 break;
173 default:
Justin Bogner367a9f22015-05-06 23:19:35 +0000174 return coveragemap_error::malformed;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000175 }
176 }
177 }
178
179 // Read the source range.
Justin Bognerde158172015-02-03 21:35:36 +0000180 uint64_t LineStartDelta, ColumnStart, NumLines, ColumnEnd;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000181 if (auto Err =
182 readIntMax(LineStartDelta, std::numeric_limits<unsigned>::max()))
183 return Err;
Justin Bognerde158172015-02-03 21:35:36 +0000184 if (auto Err = readULEB128(ColumnStart))
Alex Lorenza20a5d52014-07-24 23:57:54 +0000185 return Err;
Alex Lorenz1193b5e2014-08-04 18:00:51 +0000186 if (ColumnStart > std::numeric_limits<unsigned>::max())
Justin Bogner367a9f22015-05-06 23:19:35 +0000187 return coveragemap_error::malformed;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000188 if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max()))
189 return Err;
190 if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max()))
191 return Err;
192 LineStart += LineStartDelta;
193 // Adjust the column locations for the empty regions that are supposed to
194 // cover whole lines. Those regions should be encoded with the
195 // column range (1 -> std::numeric_limits<unsigned>::max()), but because
196 // the encoded std::numeric_limits<unsigned>::max() is several bytes long,
197 // we set the column range to (0 -> 0) to ensure that the column start and
198 // column end take up one byte each.
199 // The std::numeric_limits<unsigned>::max() is used to represent a column
200 // position at the end of the line without knowing the length of that line.
201 if (ColumnStart == 0 && ColumnEnd == 0) {
202 ColumnStart = 1;
203 ColumnEnd = std::numeric_limits<unsigned>::max();
204 }
Justin Bognerf5846492014-09-20 15:31:51 +0000205
206 DEBUG({
207 dbgs() << "Counter in file " << InferredFileID << " " << LineStart << ":"
208 << ColumnStart << " -> " << (LineStart + NumLines) << ":"
209 << ColumnEnd << ", ";
210 if (Kind == CounterMappingRegion::ExpansionRegion)
211 dbgs() << "Expands to file " << ExpandedFileID;
212 else
213 CounterMappingContext(Expressions).dump(C, dbgs());
214 dbgs() << "\n";
215 });
216
Justin Bogner26b31422015-02-03 23:59:33 +0000217 MappingRegions.push_back(CounterMappingRegion(
218 C, InferredFileID, ExpandedFileID, LineStart, ColumnStart,
219 LineStart + NumLines, ColumnEnd, Kind));
Alex Lorenza20a5d52014-07-24 23:57:54 +0000220 }
Justin Bogner0b130862015-05-06 23:15:55 +0000221 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +0000222}
223
Justin Bogner195a4f02015-02-03 00:20:11 +0000224std::error_code RawCoverageMappingReader::read() {
Alex Lorenza20a5d52014-07-24 23:57:54 +0000225
226 // Read the virtual file mapping.
227 llvm::SmallVector<unsigned, 8> VirtualFileMapping;
228 uint64_t NumFileMappings;
229 if (auto Err = readSize(NumFileMappings))
230 return Err;
231 for (size_t I = 0; I < NumFileMappings; ++I) {
232 uint64_t FilenameIndex;
233 if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size()))
234 return Err;
235 VirtualFileMapping.push_back(FilenameIndex);
236 }
237
238 // Construct the files using unique filenames and virtual file mapping.
239 for (auto I : VirtualFileMapping) {
240 Filenames.push_back(TranslationUnitFilenames[I]);
241 }
242
243 // Read the expressions.
244 uint64_t NumExpressions;
245 if (auto Err = readSize(NumExpressions))
246 return Err;
247 // Create an array of dummy expressions that get the proper counters
248 // when the expressions are read, and the proper kinds when the counters
249 // are decoded.
250 Expressions.resize(
251 NumExpressions,
252 CounterExpression(CounterExpression::Subtract, Counter(), Counter()));
253 for (size_t I = 0; I < NumExpressions; ++I) {
254 if (auto Err = readCounter(Expressions[I].LHS))
255 return Err;
256 if (auto Err = readCounter(Expressions[I].RHS))
257 return Err;
258 }
259
260 // Read the mapping regions sub-arrays.
261 for (unsigned InferredFileID = 0, S = VirtualFileMapping.size();
262 InferredFileID < S; ++InferredFileID) {
263 if (auto Err = readMappingRegionsSubArray(MappingRegions, InferredFileID,
264 VirtualFileMapping.size()))
265 return Err;
266 }
267
268 // Set the counters for the expansion regions.
269 // i.e. Counter of expansion region = counter of the first region
270 // from the expanded file.
271 // Perform multiple passes to correctly propagate the counters through
272 // all the nested expansion regions.
Alex Lorenz251b3e32014-07-29 21:42:24 +0000273 SmallVector<CounterMappingRegion *, 8> FileIDExpansionRegionMapping;
274 FileIDExpansionRegionMapping.resize(VirtualFileMapping.size(), nullptr);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000275 for (unsigned Pass = 1, S = VirtualFileMapping.size(); Pass < S; ++Pass) {
Alex Lorenz251b3e32014-07-29 21:42:24 +0000276 for (auto &R : MappingRegions) {
277 if (R.Kind != CounterMappingRegion::ExpansionRegion)
278 continue;
279 assert(!FileIDExpansionRegionMapping[R.ExpandedFileID]);
280 FileIDExpansionRegionMapping[R.ExpandedFileID] = &R;
281 }
282 for (auto &R : MappingRegions) {
283 if (FileIDExpansionRegionMapping[R.FileID]) {
284 FileIDExpansionRegionMapping[R.FileID]->Count = R.Count;
285 FileIDExpansionRegionMapping[R.FileID] = nullptr;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000286 }
287 }
288 }
289
Justin Bogner0b130862015-05-06 23:15:55 +0000290 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +0000291}
292
Alex Lorenza20a5d52014-07-24 23:57:54 +0000293namespace {
Alex Lorenza20a5d52014-07-24 23:57:54 +0000294
295/// \brief A helper structure to access the data from a section
296/// in an object file.
297struct SectionData {
298 StringRef Data;
299 uint64_t Address;
300
301 std::error_code load(SectionRef &Section) {
302 if (auto Err = Section.getContents(Data))
303 return Err;
Rafael Espindola80291272014-10-08 15:28:58 +0000304 Address = Section.getAddress();
Justin Bogner367a9f22015-05-06 23:19:35 +0000305 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +0000306 }
307
308 std::error_code get(uint64_t Pointer, size_t Size, StringRef &Result) {
309 if (Pointer < Address)
Justin Bogner367a9f22015-05-06 23:19:35 +0000310 return coveragemap_error::malformed;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000311 auto Offset = Pointer - Address;
312 if (Offset + Size > Data.size())
Justin Bogner367a9f22015-05-06 23:19:35 +0000313 return coveragemap_error::malformed;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000314 Result = Data.substr(Pointer - Address, Size);
Justin Bogner367a9f22015-05-06 23:19:35 +0000315 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +0000316 }
317};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000318}
Alex Lorenza20a5d52014-07-24 23:57:54 +0000319
Justin Bognera4387172015-03-16 21:40:18 +0000320template <typename T, support::endianness Endian>
Alex Lorenza20a5d52014-07-24 23:57:54 +0000321std::error_code readCoverageMappingData(
Alex Lorenze82d89c2014-08-22 22:56:03 +0000322 SectionData &ProfileNames, StringRef Data,
Justin Bognere84891a2015-02-26 20:06:24 +0000323 std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records,
Alex Lorenza20a5d52014-07-24 23:57:54 +0000324 std::vector<StringRef> &Filenames) {
Justin Bogner7b33cc92015-03-16 06:55:45 +0000325 using namespace support;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000326 llvm::DenseSet<T> UniqueFunctionMappingData;
327
Alex Lorenza20a5d52014-07-24 23:57:54 +0000328 // Read the records in the coverage data section.
Justin Bogner7b33cc92015-03-16 06:55:45 +0000329 for (const char *Buf = Data.data(), *End = Buf + Data.size(); Buf < End;) {
330 if (Buf + 4 * sizeof(uint32_t) > End)
Justin Bogner367a9f22015-05-06 23:19:35 +0000331 return coveragemap_error::malformed;
Justin Bognera4387172015-03-16 21:40:18 +0000332 uint32_t NRecords = endian::readNext<uint32_t, Endian, unaligned>(Buf);
333 uint32_t FilenamesSize = endian::readNext<uint32_t, Endian, unaligned>(Buf);
334 uint32_t CoverageSize = endian::readNext<uint32_t, Endian, unaligned>(Buf);
335 uint32_t Version = endian::readNext<uint32_t, Endian, unaligned>(Buf);
Justin Bogner7b33cc92015-03-16 06:55:45 +0000336
337 switch (Version) {
Alex Lorenza20a5d52014-07-24 23:57:54 +0000338 case CoverageMappingVersion1:
339 break;
340 default:
Justin Bogner367a9f22015-05-06 23:19:35 +0000341 return coveragemap_error::unsupported_version;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000342 }
Alex Lorenza20a5d52014-07-24 23:57:54 +0000343
Justin Bogner7b33cc92015-03-16 06:55:45 +0000344 // Skip past the function records, saving the start and end for later.
345 const char *FunBuf = Buf;
346 Buf += NRecords * (sizeof(T) + 2 * sizeof(uint32_t) + sizeof(uint64_t));
347 const char *FunEnd = Buf;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000348
349 // Get the filenames.
Justin Bogner7b33cc92015-03-16 06:55:45 +0000350 if (Buf + FilenamesSize > End)
Justin Bogner367a9f22015-05-06 23:19:35 +0000351 return coveragemap_error::malformed;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000352 size_t FilenamesBegin = Filenames.size();
Justin Bogner7b33cc92015-03-16 06:55:45 +0000353 RawCoverageFilenamesReader Reader(StringRef(Buf, FilenamesSize), Filenames);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000354 if (auto Err = Reader.read())
355 return Err;
Justin Bogner7b33cc92015-03-16 06:55:45 +0000356 Buf += FilenamesSize;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000357
Justin Bogner7b33cc92015-03-16 06:55:45 +0000358 // We'll read the coverage mapping records in the loop below.
359 const char *CovBuf = Buf;
360 Buf += CoverageSize;
361 const char *CovEnd = Buf;
Justin Bognerd49d8ee2015-06-05 01:23:42 +0000362
Justin Bogner7b33cc92015-03-16 06:55:45 +0000363 if (Buf > End)
Justin Bogner367a9f22015-05-06 23:19:35 +0000364 return coveragemap_error::malformed;
Justin Bognerd49d8ee2015-06-05 01:23:42 +0000365 // Each coverage map has an alignment of 8, so we need to adjust alignment
366 // before reading the next map.
367 Buf += alignmentAdjustment(Buf, 8);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000368
Justin Bogner7b33cc92015-03-16 06:55:45 +0000369 while (FunBuf < FunEnd) {
370 // Read the function information
Justin Bognera4387172015-03-16 21:40:18 +0000371 T NamePtr = endian::readNext<T, Endian, unaligned>(FunBuf);
372 uint32_t NameSize = endian::readNext<uint32_t, Endian, unaligned>(FunBuf);
373 uint32_t DataSize = endian::readNext<uint32_t, Endian, unaligned>(FunBuf);
374 uint64_t FuncHash = endian::readNext<uint64_t, Endian, unaligned>(FunBuf);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000375
Justin Bogner7b33cc92015-03-16 06:55:45 +0000376 // Now use that to read the coverage data.
377 if (CovBuf + DataSize > CovEnd)
Justin Bogner367a9f22015-05-06 23:19:35 +0000378 return coveragemap_error::malformed;
Justin Bogner7b33cc92015-03-16 06:55:45 +0000379 auto Mapping = StringRef(CovBuf, DataSize);
380 CovBuf += DataSize;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000381
382 // Ignore this record if we already have a record that points to the same
Justin Bogner7b33cc92015-03-16 06:55:45 +0000383 // function name. This is useful to ignore the redundant records for the
384 // functions with ODR linkage.
385 if (!UniqueFunctionMappingData.insert(NamePtr).second)
Alex Lorenza20a5d52014-07-24 23:57:54 +0000386 continue;
Justin Bogner7b33cc92015-03-16 06:55:45 +0000387
388 // Finally, grab the name and create a record.
389 StringRef FuncName;
390 if (std::error_code EC = ProfileNames.get(NamePtr, NameSize, FuncName))
391 return EC;
Justin Bognere84891a2015-02-26 20:06:24 +0000392 Records.push_back(BinaryCoverageReader::ProfileMappingRecord(
Justin Bogner7b33cc92015-03-16 06:55:45 +0000393 CoverageMappingVersion(Version), FuncName, FuncHash, Mapping,
Alex Lorenz936b99c2014-08-21 19:23:25 +0000394 FilenamesBegin, Filenames.size() - FilenamesBegin));
Alex Lorenza20a5d52014-07-24 23:57:54 +0000395 }
396 }
397
Justin Bogner367a9f22015-05-06 23:19:35 +0000398 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +0000399}
400
Alex Lorenze82d89c2014-08-22 22:56:03 +0000401static const char *TestingFormatMagic = "llvmcovmtestdata";
402
Justin Bogner43e51632015-02-26 20:06:28 +0000403static std::error_code loadTestingFormat(StringRef Data,
404 SectionData &ProfileNames,
405 StringRef &CoverageMapping,
Justin Bognera4387172015-03-16 21:40:18 +0000406 uint8_t &BytesInAddress,
407 support::endianness &Endian) {
Justin Bogner43e51632015-02-26 20:06:28 +0000408 BytesInAddress = 8;
Justin Bognera4387172015-03-16 21:40:18 +0000409 Endian = support::endianness::little;
Justin Bogner43e51632015-02-26 20:06:28 +0000410
Alex Lorenze82d89c2014-08-22 22:56:03 +0000411 Data = Data.substr(StringRef(TestingFormatMagic).size());
412 if (Data.size() < 1)
Justin Bogner367a9f22015-05-06 23:19:35 +0000413 return coveragemap_error::truncated;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000414 unsigned N = 0;
415 auto ProfileNamesSize =
416 decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
417 if (N > Data.size())
Justin Bogner367a9f22015-05-06 23:19:35 +0000418 return coveragemap_error::malformed;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000419 Data = Data.substr(N);
420 if (Data.size() < 1)
Justin Bogner367a9f22015-05-06 23:19:35 +0000421 return coveragemap_error::truncated;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000422 N = 0;
423 ProfileNames.Address =
424 decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
425 if (N > Data.size())
Justin Bogner367a9f22015-05-06 23:19:35 +0000426 return coveragemap_error::malformed;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000427 Data = Data.substr(N);
428 if (Data.size() < ProfileNamesSize)
Justin Bogner367a9f22015-05-06 23:19:35 +0000429 return coveragemap_error::malformed;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000430 ProfileNames.Data = Data.substr(0, ProfileNamesSize);
431 CoverageMapping = Data.substr(ProfileNamesSize);
Justin Bogner367a9f22015-05-06 23:19:35 +0000432 return std::error_code();
Alex Lorenze82d89c2014-08-22 22:56:03 +0000433}
434
Justin Bogner5a5c3812015-05-07 00:31:58 +0000435static ErrorOr<SectionRef> lookupSection(ObjectFile &OF, StringRef Name) {
436 StringRef FoundName;
437 for (const auto &Section : OF.sections()) {
438 if (auto EC = Section.getName(FoundName))
439 return EC;
440 if (FoundName == Name)
441 return Section;
442 }
443 return coveragemap_error::no_data_found;
444}
445
Justin Bogner43e51632015-02-26 20:06:28 +0000446static std::error_code loadBinaryFormat(MemoryBufferRef ObjectBuffer,
447 SectionData &ProfileNames,
448 StringRef &CoverageMapping,
Justin Bogner43795352015-03-11 02:30:51 +0000449 uint8_t &BytesInAddress,
Justin Bognera4387172015-03-16 21:40:18 +0000450 support::endianness &Endian,
Frederic Rissebc162a2015-06-22 21:33:24 +0000451 StringRef Arch) {
Justin Bogner43795352015-03-11 02:30:51 +0000452 auto BinOrErr = object::createBinary(ObjectBuffer);
453 if (std::error_code EC = BinOrErr.getError())
Justin Bogner43e51632015-02-26 20:06:28 +0000454 return EC;
Justin Bogner43795352015-03-11 02:30:51 +0000455 auto Bin = std::move(BinOrErr.get());
456 std::unique_ptr<ObjectFile> OF;
457 if (auto *Universal = dyn_cast<object::MachOUniversalBinary>(Bin.get())) {
458 // If we have a universal binary, try to look up the object for the
459 // appropriate architecture.
460 auto ObjectFileOrErr = Universal->getObjectForArch(Arch);
461 if (std::error_code EC = ObjectFileOrErr.getError())
462 return EC;
463 OF = std::move(ObjectFileOrErr.get());
464 } else if (isa<object::ObjectFile>(Bin.get())) {
465 // For any other object file, upcast and take ownership.
466 OF.reset(cast<object::ObjectFile>(Bin.release()));
467 // If we've asked for a particular arch, make sure they match.
Frederic Rissebc162a2015-06-22 21:33:24 +0000468 if (!Arch.empty() && OF->getArch() != Triple(Arch).getArch())
Justin Bogner43795352015-03-11 02:30:51 +0000469 return object_error::arch_not_found;
470 } else
471 // We can only handle object files.
Justin Bogner367a9f22015-05-06 23:19:35 +0000472 return coveragemap_error::malformed;
Justin Bogner43795352015-03-11 02:30:51 +0000473
474 // The coverage uses native pointer sizes for the object it's written in.
Justin Bogner43e51632015-02-26 20:06:28 +0000475 BytesInAddress = OF->getBytesInAddress();
Justin Bognera4387172015-03-16 21:40:18 +0000476 Endian = OF->isLittleEndian() ? support::endianness::little
477 : support::endianness::big;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000478
479 // Look for the sections that we are interested in.
Justin Bogner5a5c3812015-05-07 00:31:58 +0000480 auto NamesSection = lookupSection(*OF, "__llvm_prf_names");
481 if (auto EC = NamesSection.getError())
482 return EC;
483 auto CoverageSection = lookupSection(*OF, "__llvm_covmap");
484 if (auto EC = CoverageSection.getError())
485 return EC;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000486
Alex Lorenze82d89c2014-08-22 22:56:03 +0000487 // Get the contents of the given sections.
Justin Bogner5a5c3812015-05-07 00:31:58 +0000488 if (std::error_code EC = CoverageSection->getContents(CoverageMapping))
Justin Bogner43e51632015-02-26 20:06:28 +0000489 return EC;
Justin Bogner5a5c3812015-05-07 00:31:58 +0000490 if (std::error_code EC = ProfileNames.load(*NamesSection))
Justin Bogner43e51632015-02-26 20:06:28 +0000491 return EC;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000492
Justin Bogner43e51632015-02-26 20:06:28 +0000493 return std::error_code();
494}
495
496ErrorOr<std::unique_ptr<BinaryCoverageReader>>
Justin Bogner43795352015-03-11 02:30:51 +0000497BinaryCoverageReader::create(std::unique_ptr<MemoryBuffer> &ObjectBuffer,
Frederic Rissebc162a2015-06-22 21:33:24 +0000498 StringRef Arch) {
Justin Bogner43e51632015-02-26 20:06:28 +0000499 std::unique_ptr<BinaryCoverageReader> Reader(new BinaryCoverageReader());
500
501 SectionData Profile;
502 StringRef Coverage;
503 uint8_t BytesInAddress;
Justin Bognera4387172015-03-16 21:40:18 +0000504 support::endianness Endian;
Justin Bogner43e51632015-02-26 20:06:28 +0000505 std::error_code EC;
506 if (ObjectBuffer->getBuffer().startswith(TestingFormatMagic))
507 // This is a special format used for testing.
508 EC = loadTestingFormat(ObjectBuffer->getBuffer(), Profile, Coverage,
Justin Bognera4387172015-03-16 21:40:18 +0000509 BytesInAddress, Endian);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000510 else
Justin Bogner43e51632015-02-26 20:06:28 +0000511 EC = loadBinaryFormat(ObjectBuffer->getMemBufferRef(), Profile, Coverage,
Justin Bognera4387172015-03-16 21:40:18 +0000512 BytesInAddress, Endian, Arch);
Justin Bogner43e51632015-02-26 20:06:28 +0000513 if (EC)
514 return EC;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000515
Justin Bognera4387172015-03-16 21:40:18 +0000516 if (BytesInAddress == 4 && Endian == support::endianness::little)
517 EC = readCoverageMappingData<uint32_t, support::endianness::little>(
Justin Bogner43e51632015-02-26 20:06:28 +0000518 Profile, Coverage, Reader->MappingRecords, Reader->Filenames);
Justin Bognera4387172015-03-16 21:40:18 +0000519 else if (BytesInAddress == 4 && Endian == support::endianness::big)
520 EC = readCoverageMappingData<uint32_t, support::endianness::big>(
521 Profile, Coverage, Reader->MappingRecords, Reader->Filenames);
522 else if (BytesInAddress == 8 && Endian == support::endianness::little)
523 EC = readCoverageMappingData<uint64_t, support::endianness::little>(
524 Profile, Coverage, Reader->MappingRecords, Reader->Filenames);
525 else if (BytesInAddress == 8 && Endian == support::endianness::big)
526 EC = readCoverageMappingData<uint64_t, support::endianness::big>(
Justin Bogner43e51632015-02-26 20:06:28 +0000527 Profile, Coverage, Reader->MappingRecords, Reader->Filenames);
528 else
Justin Bogner367a9f22015-05-06 23:19:35 +0000529 return coveragemap_error::malformed;
Justin Bogner43e51632015-02-26 20:06:28 +0000530 if (EC)
531 return EC;
532 return std::move(Reader);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000533}
534
535std::error_code
Justin Bognere84891a2015-02-26 20:06:24 +0000536BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) {
Alex Lorenza20a5d52014-07-24 23:57:54 +0000537 if (CurrentRecord >= MappingRecords.size())
Justin Bogner367a9f22015-05-06 23:19:35 +0000538 return coveragemap_error::eof;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000539
540 FunctionsFilenames.clear();
541 Expressions.clear();
542 MappingRegions.clear();
543 auto &R = MappingRecords[CurrentRecord];
544 RawCoverageMappingReader Reader(
Justin Bogner195a4f02015-02-03 00:20:11 +0000545 R.CoverageMapping,
Justin Bogner346359d2015-02-03 00:00:00 +0000546 makeArrayRef(Filenames).slice(R.FilenamesBegin, R.FilenamesSize),
Alex Lorenza20a5d52014-07-24 23:57:54 +0000547 FunctionsFilenames, Expressions, MappingRegions);
Justin Bogner195a4f02015-02-03 00:20:11 +0000548 if (auto Err = Reader.read())
Alex Lorenza20a5d52014-07-24 23:57:54 +0000549 return Err;
Justin Bogner195a4f02015-02-03 00:20:11 +0000550
551 Record.FunctionName = R.FunctionName;
Alex Lorenz936b99c2014-08-21 19:23:25 +0000552 Record.FunctionHash = R.FunctionHash;
Justin Bogner195a4f02015-02-03 00:20:11 +0000553 Record.Filenames = FunctionsFilenames;
554 Record.Expressions = Expressions;
555 Record.MappingRegions = MappingRegions;
556
Alex Lorenza20a5d52014-07-24 23:57:54 +0000557 ++CurrentRecord;
Justin Bogner43e51632015-02-26 20:06:28 +0000558 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +0000559}