CoverageMappingReader.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. //===- CoverageMappingReader.cpp - Code coverage mapping reader -----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains support for reading coverage mapping data for
  10. // instrumentation based coverage.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/STLExtras.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/ADT/StringRef.h"
  19. #include "llvm/ADT/Triple.h"
  20. #include "llvm/Object/Binary.h"
  21. #include "llvm/Object/Error.h"
  22. #include "llvm/Object/MachOUniversal.h"
  23. #include "llvm/Object/ObjectFile.h"
  24. #include "llvm/Object/COFF.h"
  25. #include "llvm/ProfileData/InstrProf.h"
  26. #include "llvm/Support/Casting.h"
  27. #include "llvm/Support/Debug.h"
  28. #include "llvm/Support/Endian.h"
  29. #include "llvm/Support/Error.h"
  30. #include "llvm/Support/ErrorHandling.h"
  31. #include "llvm/Support/LEB128.h"
  32. #include "llvm/Support/MathExtras.h"
  33. #include "llvm/Support/raw_ostream.h"
  34. #include <vector>
  35. using namespace llvm;
  36. using namespace coverage;
  37. using namespace object;
  38. #define DEBUG_TYPE "coverage-mapping"
  39. void CoverageMappingIterator::increment() {
  40. if (ReadErr != coveragemap_error::success)
  41. return;
  42. // Check if all the records were read or if an error occurred while reading
  43. // the next record.
  44. if (auto E = Reader->readNextRecord(Record))
  45. handleAllErrors(std::move(E), [&](const CoverageMapError &CME) {
  46. if (CME.get() == coveragemap_error::eof)
  47. *this = CoverageMappingIterator();
  48. else
  49. ReadErr = CME.get();
  50. });
  51. }
  52. Error RawCoverageReader::readULEB128(uint64_t &Result) {
  53. if (Data.empty())
  54. return make_error<CoverageMapError>(coveragemap_error::truncated);
  55. unsigned N = 0;
  56. Result = decodeULEB128(Data.bytes_begin(), &N);
  57. if (N > Data.size())
  58. return make_error<CoverageMapError>(coveragemap_error::malformed);
  59. Data = Data.substr(N);
  60. return Error::success();
  61. }
  62. Error RawCoverageReader::readIntMax(uint64_t &Result, uint64_t MaxPlus1) {
  63. if (auto Err = readULEB128(Result))
  64. return Err;
  65. if (Result >= MaxPlus1)
  66. return make_error<CoverageMapError>(coveragemap_error::malformed);
  67. return Error::success();
  68. }
  69. Error RawCoverageReader::readSize(uint64_t &Result) {
  70. if (auto Err = readULEB128(Result))
  71. return Err;
  72. // Sanity check the number.
  73. if (Result > Data.size())
  74. return make_error<CoverageMapError>(coveragemap_error::malformed);
  75. return Error::success();
  76. }
  77. Error RawCoverageReader::readString(StringRef &Result) {
  78. uint64_t Length;
  79. if (auto Err = readSize(Length))
  80. return Err;
  81. Result = Data.substr(0, Length);
  82. Data = Data.substr(Length);
  83. return Error::success();
  84. }
  85. Error RawCoverageFilenamesReader::read() {
  86. uint64_t NumFilenames;
  87. if (auto Err = readSize(NumFilenames))
  88. return Err;
  89. for (size_t I = 0; I < NumFilenames; ++I) {
  90. StringRef Filename;
  91. if (auto Err = readString(Filename))
  92. return Err;
  93. Filenames.push_back(Filename);
  94. }
  95. return Error::success();
  96. }
  97. Error RawCoverageMappingReader::decodeCounter(unsigned Value, Counter &C) {
  98. auto Tag = Value & Counter::EncodingTagMask;
  99. switch (Tag) {
  100. case Counter::Zero:
  101. C = Counter::getZero();
  102. return Error::success();
  103. case Counter::CounterValueReference:
  104. C = Counter::getCounter(Value >> Counter::EncodingTagBits);
  105. return Error::success();
  106. default:
  107. break;
  108. }
  109. Tag -= Counter::Expression;
  110. switch (Tag) {
  111. case CounterExpression::Subtract:
  112. case CounterExpression::Add: {
  113. auto ID = Value >> Counter::EncodingTagBits;
  114. if (ID >= Expressions.size())
  115. return make_error<CoverageMapError>(coveragemap_error::malformed);
  116. Expressions[ID].Kind = CounterExpression::ExprKind(Tag);
  117. C = Counter::getExpression(ID);
  118. break;
  119. }
  120. default:
  121. return make_error<CoverageMapError>(coveragemap_error::malformed);
  122. }
  123. return Error::success();
  124. }
  125. Error RawCoverageMappingReader::readCounter(Counter &C) {
  126. uint64_t EncodedCounter;
  127. if (auto Err =
  128. readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max()))
  129. return Err;
  130. if (auto Err = decodeCounter(EncodedCounter, C))
  131. return Err;
  132. return Error::success();
  133. }
  134. static const unsigned EncodingExpansionRegionBit = 1
  135. << Counter::EncodingTagBits;
  136. /// Read the sub-array of regions for the given inferred file id.
  137. /// \param NumFileIDs the number of file ids that are defined for this
  138. /// function.
  139. Error RawCoverageMappingReader::readMappingRegionsSubArray(
  140. std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID,
  141. size_t NumFileIDs) {
  142. uint64_t NumRegions;
  143. if (auto Err = readSize(NumRegions))
  144. return Err;
  145. unsigned LineStart = 0;
  146. for (size_t I = 0; I < NumRegions; ++I) {
  147. Counter C;
  148. CounterMappingRegion::RegionKind Kind = CounterMappingRegion::CodeRegion;
  149. // Read the combined counter + region kind.
  150. uint64_t EncodedCounterAndRegion;
  151. if (auto Err = readIntMax(EncodedCounterAndRegion,
  152. std::numeric_limits<unsigned>::max()))
  153. return Err;
  154. unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
  155. uint64_t ExpandedFileID = 0;
  156. if (Tag != Counter::Zero) {
  157. if (auto Err = decodeCounter(EncodedCounterAndRegion, C))
  158. return Err;
  159. } else {
  160. // Is it an expansion region?
  161. if (EncodedCounterAndRegion & EncodingExpansionRegionBit) {
  162. Kind = CounterMappingRegion::ExpansionRegion;
  163. ExpandedFileID = EncodedCounterAndRegion >>
  164. Counter::EncodingCounterTagAndExpansionRegionTagBits;
  165. if (ExpandedFileID >= NumFileIDs)
  166. return make_error<CoverageMapError>(coveragemap_error::malformed);
  167. } else {
  168. switch (EncodedCounterAndRegion >>
  169. Counter::EncodingCounterTagAndExpansionRegionTagBits) {
  170. case CounterMappingRegion::CodeRegion:
  171. // Don't do anything when we have a code region with a zero counter.
  172. break;
  173. case CounterMappingRegion::SkippedRegion:
  174. Kind = CounterMappingRegion::SkippedRegion;
  175. break;
  176. default:
  177. return make_error<CoverageMapError>(coveragemap_error::malformed);
  178. }
  179. }
  180. }
  181. // Read the source range.
  182. uint64_t LineStartDelta, ColumnStart, NumLines, ColumnEnd;
  183. if (auto Err =
  184. readIntMax(LineStartDelta, std::numeric_limits<unsigned>::max()))
  185. return Err;
  186. if (auto Err = readULEB128(ColumnStart))
  187. return Err;
  188. if (ColumnStart > std::numeric_limits<unsigned>::max())
  189. return make_error<CoverageMapError>(coveragemap_error::malformed);
  190. if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max()))
  191. return Err;
  192. if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max()))
  193. return Err;
  194. LineStart += LineStartDelta;
  195. // If the high bit of ColumnEnd is set, this is a gap region.
  196. if (ColumnEnd & (1U << 31)) {
  197. Kind = CounterMappingRegion::GapRegion;
  198. ColumnEnd &= ~(1U << 31);
  199. }
  200. // Adjust the column locations for the empty regions that are supposed to
  201. // cover whole lines. Those regions should be encoded with the
  202. // column range (1 -> std::numeric_limits<unsigned>::max()), but because
  203. // the encoded std::numeric_limits<unsigned>::max() is several bytes long,
  204. // we set the column range to (0 -> 0) to ensure that the column start and
  205. // column end take up one byte each.
  206. // The std::numeric_limits<unsigned>::max() is used to represent a column
  207. // position at the end of the line without knowing the length of that line.
  208. if (ColumnStart == 0 && ColumnEnd == 0) {
  209. ColumnStart = 1;
  210. ColumnEnd = std::numeric_limits<unsigned>::max();
  211. }
  212. LLVM_DEBUG({
  213. dbgs() << "Counter in file " << InferredFileID << " " << LineStart << ":"
  214. << ColumnStart << " -> " << (LineStart + NumLines) << ":"
  215. << ColumnEnd << ", ";
  216. if (Kind == CounterMappingRegion::ExpansionRegion)
  217. dbgs() << "Expands to file " << ExpandedFileID;
  218. else
  219. CounterMappingContext(Expressions).dump(C, dbgs());
  220. dbgs() << "\n";
  221. });
  222. auto CMR = CounterMappingRegion(C, InferredFileID, ExpandedFileID,
  223. LineStart, ColumnStart,
  224. LineStart + NumLines, ColumnEnd, Kind);
  225. if (CMR.startLoc() > CMR.endLoc())
  226. return make_error<CoverageMapError>(coveragemap_error::malformed);
  227. MappingRegions.push_back(CMR);
  228. }
  229. return Error::success();
  230. }
  231. Error RawCoverageMappingReader::read() {
  232. // Read the virtual file mapping.
  233. SmallVector<unsigned, 8> VirtualFileMapping;
  234. uint64_t NumFileMappings;
  235. if (auto Err = readSize(NumFileMappings))
  236. return Err;
  237. for (size_t I = 0; I < NumFileMappings; ++I) {
  238. uint64_t FilenameIndex;
  239. if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size()))
  240. return Err;
  241. VirtualFileMapping.push_back(FilenameIndex);
  242. }
  243. // Construct the files using unique filenames and virtual file mapping.
  244. for (auto I : VirtualFileMapping) {
  245. Filenames.push_back(TranslationUnitFilenames[I]);
  246. }
  247. // Read the expressions.
  248. uint64_t NumExpressions;
  249. if (auto Err = readSize(NumExpressions))
  250. return Err;
  251. // Create an array of dummy expressions that get the proper counters
  252. // when the expressions are read, and the proper kinds when the counters
  253. // are decoded.
  254. Expressions.resize(
  255. NumExpressions,
  256. CounterExpression(CounterExpression::Subtract, Counter(), Counter()));
  257. for (size_t I = 0; I < NumExpressions; ++I) {
  258. if (auto Err = readCounter(Expressions[I].LHS))
  259. return Err;
  260. if (auto Err = readCounter(Expressions[I].RHS))
  261. return Err;
  262. }
  263. // Read the mapping regions sub-arrays.
  264. for (unsigned InferredFileID = 0, S = VirtualFileMapping.size();
  265. InferredFileID < S; ++InferredFileID) {
  266. if (auto Err = readMappingRegionsSubArray(MappingRegions, InferredFileID,
  267. VirtualFileMapping.size()))
  268. return Err;
  269. }
  270. // Set the counters for the expansion regions.
  271. // i.e. Counter of expansion region = counter of the first region
  272. // from the expanded file.
  273. // Perform multiple passes to correctly propagate the counters through
  274. // all the nested expansion regions.
  275. SmallVector<CounterMappingRegion *, 8> FileIDExpansionRegionMapping;
  276. FileIDExpansionRegionMapping.resize(VirtualFileMapping.size(), nullptr);
  277. for (unsigned Pass = 1, S = VirtualFileMapping.size(); Pass < S; ++Pass) {
  278. for (auto &R : MappingRegions) {
  279. if (R.Kind != CounterMappingRegion::ExpansionRegion)
  280. continue;
  281. assert(!FileIDExpansionRegionMapping[R.ExpandedFileID]);
  282. FileIDExpansionRegionMapping[R.ExpandedFileID] = &R;
  283. }
  284. for (auto &R : MappingRegions) {
  285. if (FileIDExpansionRegionMapping[R.FileID]) {
  286. FileIDExpansionRegionMapping[R.FileID]->Count = R.Count;
  287. FileIDExpansionRegionMapping[R.FileID] = nullptr;
  288. }
  289. }
  290. }
  291. return Error::success();
  292. }
  293. Expected<bool> RawCoverageMappingDummyChecker::isDummy() {
  294. // A dummy coverage mapping data consists of just one region with zero count.
  295. uint64_t NumFileMappings;
  296. if (Error Err = readSize(NumFileMappings))
  297. return std::move(Err);
  298. if (NumFileMappings != 1)
  299. return false;
  300. // We don't expect any specific value for the filename index, just skip it.
  301. uint64_t FilenameIndex;
  302. if (Error Err =
  303. readIntMax(FilenameIndex, std::numeric_limits<unsigned>::max()))
  304. return std::move(Err);
  305. uint64_t NumExpressions;
  306. if (Error Err = readSize(NumExpressions))
  307. return std::move(Err);
  308. if (NumExpressions != 0)
  309. return false;
  310. uint64_t NumRegions;
  311. if (Error Err = readSize(NumRegions))
  312. return std::move(Err);
  313. if (NumRegions != 1)
  314. return false;
  315. uint64_t EncodedCounterAndRegion;
  316. if (Error Err = readIntMax(EncodedCounterAndRegion,
  317. std::numeric_limits<unsigned>::max()))
  318. return std::move(Err);
  319. unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
  320. return Tag == Counter::Zero;
  321. }
  322. Error InstrProfSymtab::create(SectionRef &Section) {
  323. if (auto EC = Section.getContents(Data))
  324. return errorCodeToError(EC);
  325. Address = Section.getAddress();
  326. // If this is a linked PE/COFF file, then we have to skip over the null byte
  327. // that is allocated in the .lprfn$A section in the LLVM profiling runtime.
  328. const ObjectFile *Obj = Section.getObject();
  329. if (isa<COFFObjectFile>(Obj) && !Obj->isRelocatableObject())
  330. Data = Data.drop_front(1);
  331. return Error::success();
  332. }
  333. StringRef InstrProfSymtab::getFuncName(uint64_t Pointer, size_t Size) {
  334. if (Pointer < Address)
  335. return StringRef();
  336. auto Offset = Pointer - Address;
  337. if (Offset + Size > Data.size())
  338. return StringRef();
  339. return Data.substr(Pointer - Address, Size);
  340. }
  341. // Check if the mapping data is a dummy, i.e. is emitted for an unused function.
  342. static Expected<bool> isCoverageMappingDummy(uint64_t Hash, StringRef Mapping) {
  343. // The hash value of dummy mapping records is always zero.
  344. if (Hash)
  345. return false;
  346. return RawCoverageMappingDummyChecker(Mapping).isDummy();
  347. }
  348. namespace {
  349. struct CovMapFuncRecordReader {
  350. virtual ~CovMapFuncRecordReader() = default;
  351. // The interface to read coverage mapping function records for a module.
  352. //
  353. // \p Buf points to the buffer containing the \c CovHeader of the coverage
  354. // mapping data associated with the module.
  355. //
  356. // Returns a pointer to the next \c CovHeader if it exists, or a pointer
  357. // greater than \p End if not.
  358. virtual Expected<const char *> readFunctionRecords(const char *Buf,
  359. const char *End) = 0;
  360. template <class IntPtrT, support::endianness Endian>
  361. static Expected<std::unique_ptr<CovMapFuncRecordReader>>
  362. get(CovMapVersion Version, InstrProfSymtab &P,
  363. std::vector<BinaryCoverageReader::ProfileMappingRecord> &R,
  364. std::vector<StringRef> &F);
  365. };
  366. // A class for reading coverage mapping function records for a module.
  367. template <CovMapVersion Version, class IntPtrT, support::endianness Endian>
  368. class VersionedCovMapFuncRecordReader : public CovMapFuncRecordReader {
  369. using FuncRecordType =
  370. typename CovMapTraits<Version, IntPtrT>::CovMapFuncRecordType;
  371. using NameRefType = typename CovMapTraits<Version, IntPtrT>::NameRefType;
  372. // Maps function's name references to the indexes of their records
  373. // in \c Records.
  374. DenseMap<NameRefType, size_t> FunctionRecords;
  375. InstrProfSymtab &ProfileNames;
  376. std::vector<StringRef> &Filenames;
  377. std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records;
  378. // Add the record to the collection if we don't already have a record that
  379. // points to the same function name. This is useful to ignore the redundant
  380. // records for the functions with ODR linkage.
  381. // In addition, prefer records with real coverage mapping data to dummy
  382. // records, which were emitted for inline functions which were seen but
  383. // not used in the corresponding translation unit.
  384. Error insertFunctionRecordIfNeeded(const FuncRecordType *CFR,
  385. StringRef Mapping, size_t FilenamesBegin) {
  386. uint64_t FuncHash = CFR->template getFuncHash<Endian>();
  387. NameRefType NameRef = CFR->template getFuncNameRef<Endian>();
  388. auto InsertResult =
  389. FunctionRecords.insert(std::make_pair(NameRef, Records.size()));
  390. if (InsertResult.second) {
  391. StringRef FuncName;
  392. if (Error Err = CFR->template getFuncName<Endian>(ProfileNames, FuncName))
  393. return Err;
  394. if (FuncName.empty())
  395. return make_error<InstrProfError>(instrprof_error::malformed);
  396. Records.emplace_back(Version, FuncName, FuncHash, Mapping, FilenamesBegin,
  397. Filenames.size() - FilenamesBegin);
  398. return Error::success();
  399. }
  400. // Update the existing record if it's a dummy and the new record is real.
  401. size_t OldRecordIndex = InsertResult.first->second;
  402. BinaryCoverageReader::ProfileMappingRecord &OldRecord =
  403. Records[OldRecordIndex];
  404. Expected<bool> OldIsDummyExpected = isCoverageMappingDummy(
  405. OldRecord.FunctionHash, OldRecord.CoverageMapping);
  406. if (Error Err = OldIsDummyExpected.takeError())
  407. return Err;
  408. if (!*OldIsDummyExpected)
  409. return Error::success();
  410. Expected<bool> NewIsDummyExpected =
  411. isCoverageMappingDummy(FuncHash, Mapping);
  412. if (Error Err = NewIsDummyExpected.takeError())
  413. return Err;
  414. if (*NewIsDummyExpected)
  415. return Error::success();
  416. OldRecord.FunctionHash = FuncHash;
  417. OldRecord.CoverageMapping = Mapping;
  418. OldRecord.FilenamesBegin = FilenamesBegin;
  419. OldRecord.FilenamesSize = Filenames.size() - FilenamesBegin;
  420. return Error::success();
  421. }
  422. public:
  423. VersionedCovMapFuncRecordReader(
  424. InstrProfSymtab &P,
  425. std::vector<BinaryCoverageReader::ProfileMappingRecord> &R,
  426. std::vector<StringRef> &F)
  427. : ProfileNames(P), Filenames(F), Records(R) {}
  428. ~VersionedCovMapFuncRecordReader() override = default;
  429. Expected<const char *> readFunctionRecords(const char *Buf,
  430. const char *End) override {
  431. using namespace support;
  432. if (Buf + sizeof(CovMapHeader) > End)
  433. return make_error<CoverageMapError>(coveragemap_error::malformed);
  434. auto CovHeader = reinterpret_cast<const CovMapHeader *>(Buf);
  435. uint32_t NRecords = CovHeader->getNRecords<Endian>();
  436. uint32_t FilenamesSize = CovHeader->getFilenamesSize<Endian>();
  437. uint32_t CoverageSize = CovHeader->getCoverageSize<Endian>();
  438. assert((CovMapVersion)CovHeader->getVersion<Endian>() == Version);
  439. Buf = reinterpret_cast<const char *>(CovHeader + 1);
  440. // Skip past the function records, saving the start and end for later.
  441. const char *FunBuf = Buf;
  442. Buf += NRecords * sizeof(FuncRecordType);
  443. const char *FunEnd = Buf;
  444. // Get the filenames.
  445. if (Buf + FilenamesSize > End)
  446. return make_error<CoverageMapError>(coveragemap_error::malformed);
  447. size_t FilenamesBegin = Filenames.size();
  448. RawCoverageFilenamesReader Reader(StringRef(Buf, FilenamesSize), Filenames);
  449. if (auto Err = Reader.read())
  450. return std::move(Err);
  451. Buf += FilenamesSize;
  452. // We'll read the coverage mapping records in the loop below.
  453. const char *CovBuf = Buf;
  454. Buf += CoverageSize;
  455. const char *CovEnd = Buf;
  456. if (Buf > End)
  457. return make_error<CoverageMapError>(coveragemap_error::malformed);
  458. // Each coverage map has an alignment of 8, so we need to adjust alignment
  459. // before reading the next map.
  460. Buf += alignmentAdjustment(Buf, 8);
  461. auto CFR = reinterpret_cast<const FuncRecordType *>(FunBuf);
  462. while ((const char *)CFR < FunEnd) {
  463. // Read the function information
  464. uint32_t DataSize = CFR->template getDataSize<Endian>();
  465. // Now use that to read the coverage data.
  466. if (CovBuf + DataSize > CovEnd)
  467. return make_error<CoverageMapError>(coveragemap_error::malformed);
  468. auto Mapping = StringRef(CovBuf, DataSize);
  469. CovBuf += DataSize;
  470. if (Error Err =
  471. insertFunctionRecordIfNeeded(CFR, Mapping, FilenamesBegin))
  472. return std::move(Err);
  473. CFR++;
  474. }
  475. return Buf;
  476. }
  477. };
  478. } // end anonymous namespace
  479. template <class IntPtrT, support::endianness Endian>
  480. Expected<std::unique_ptr<CovMapFuncRecordReader>> CovMapFuncRecordReader::get(
  481. CovMapVersion Version, InstrProfSymtab &P,
  482. std::vector<BinaryCoverageReader::ProfileMappingRecord> &R,
  483. std::vector<StringRef> &F) {
  484. using namespace coverage;
  485. switch (Version) {
  486. case CovMapVersion::Version1:
  487. return llvm::make_unique<VersionedCovMapFuncRecordReader<
  488. CovMapVersion::Version1, IntPtrT, Endian>>(P, R, F);
  489. case CovMapVersion::Version2:
  490. case CovMapVersion::Version3:
  491. // Decompress the name data.
  492. if (Error E = P.create(P.getNameData()))
  493. return std::move(E);
  494. if (Version == CovMapVersion::Version2)
  495. return llvm::make_unique<VersionedCovMapFuncRecordReader<
  496. CovMapVersion::Version2, IntPtrT, Endian>>(P, R, F);
  497. else
  498. return llvm::make_unique<VersionedCovMapFuncRecordReader<
  499. CovMapVersion::Version3, IntPtrT, Endian>>(P, R, F);
  500. }
  501. llvm_unreachable("Unsupported version");
  502. }
  503. template <typename T, support::endianness Endian>
  504. static Error readCoverageMappingData(
  505. InstrProfSymtab &ProfileNames, StringRef Data,
  506. std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records,
  507. std::vector<StringRef> &Filenames) {
  508. using namespace coverage;
  509. // Read the records in the coverage data section.
  510. auto CovHeader =
  511. reinterpret_cast<const CovMapHeader *>(Data.data());
  512. CovMapVersion Version = (CovMapVersion)CovHeader->getVersion<Endian>();
  513. if (Version > CovMapVersion::CurrentVersion)
  514. return make_error<CoverageMapError>(coveragemap_error::unsupported_version);
  515. Expected<std::unique_ptr<CovMapFuncRecordReader>> ReaderExpected =
  516. CovMapFuncRecordReader::get<T, Endian>(Version, ProfileNames, Records,
  517. Filenames);
  518. if (Error E = ReaderExpected.takeError())
  519. return E;
  520. auto Reader = std::move(ReaderExpected.get());
  521. for (const char *Buf = Data.data(), *End = Buf + Data.size(); Buf < End;) {
  522. auto NextHeaderOrErr = Reader->readFunctionRecords(Buf, End);
  523. if (auto E = NextHeaderOrErr.takeError())
  524. return E;
  525. Buf = NextHeaderOrErr.get();
  526. }
  527. return Error::success();
  528. }
  529. static const char *TestingFormatMagic = "llvmcovmtestdata";
  530. static Error loadTestingFormat(StringRef Data, InstrProfSymtab &ProfileNames,
  531. StringRef &CoverageMapping,
  532. uint8_t &BytesInAddress,
  533. support::endianness &Endian) {
  534. BytesInAddress = 8;
  535. Endian = support::endianness::little;
  536. Data = Data.substr(StringRef(TestingFormatMagic).size());
  537. if (Data.empty())
  538. return make_error<CoverageMapError>(coveragemap_error::truncated);
  539. unsigned N = 0;
  540. uint64_t ProfileNamesSize = decodeULEB128(Data.bytes_begin(), &N);
  541. if (N > Data.size())
  542. return make_error<CoverageMapError>(coveragemap_error::malformed);
  543. Data = Data.substr(N);
  544. if (Data.empty())
  545. return make_error<CoverageMapError>(coveragemap_error::truncated);
  546. N = 0;
  547. uint64_t Address = decodeULEB128(Data.bytes_begin(), &N);
  548. if (N > Data.size())
  549. return make_error<CoverageMapError>(coveragemap_error::malformed);
  550. Data = Data.substr(N);
  551. if (Data.size() < ProfileNamesSize)
  552. return make_error<CoverageMapError>(coveragemap_error::malformed);
  553. if (Error E = ProfileNames.create(Data.substr(0, ProfileNamesSize), Address))
  554. return E;
  555. CoverageMapping = Data.substr(ProfileNamesSize);
  556. // Skip the padding bytes because coverage map data has an alignment of 8.
  557. if (CoverageMapping.empty())
  558. return make_error<CoverageMapError>(coveragemap_error::truncated);
  559. size_t Pad = alignmentAdjustment(CoverageMapping.data(), 8);
  560. if (CoverageMapping.size() < Pad)
  561. return make_error<CoverageMapError>(coveragemap_error::malformed);
  562. CoverageMapping = CoverageMapping.substr(Pad);
  563. return Error::success();
  564. }
  565. static Expected<SectionRef> lookupSection(ObjectFile &OF, StringRef Name) {
  566. // On COFF, the object file section name may end in "$M". This tells the
  567. // linker to sort these sections between "$A" and "$Z". The linker removes the
  568. // dollar and everything after it in the final binary. Do the same to match.
  569. bool IsCOFF = isa<COFFObjectFile>(OF);
  570. auto stripSuffix = [IsCOFF](StringRef N) {
  571. return IsCOFF ? N.split('$').first : N;
  572. };
  573. Name = stripSuffix(Name);
  574. StringRef FoundName;
  575. for (const auto &Section : OF.sections()) {
  576. if (auto EC = Section.getName(FoundName))
  577. return errorCodeToError(EC);
  578. if (stripSuffix(FoundName) == Name)
  579. return Section;
  580. }
  581. return make_error<CoverageMapError>(coveragemap_error::no_data_found);
  582. }
  583. static Error loadBinaryFormat(MemoryBufferRef ObjectBuffer,
  584. InstrProfSymtab &ProfileNames,
  585. StringRef &CoverageMapping,
  586. uint8_t &BytesInAddress,
  587. support::endianness &Endian, StringRef Arch) {
  588. auto BinOrErr = createBinary(ObjectBuffer);
  589. if (!BinOrErr)
  590. return BinOrErr.takeError();
  591. auto Bin = std::move(BinOrErr.get());
  592. std::unique_ptr<ObjectFile> OF;
  593. if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin.get())) {
  594. // If we have a universal binary, try to look up the object for the
  595. // appropriate architecture.
  596. auto ObjectFileOrErr = Universal->getObjectForArch(Arch);
  597. if (!ObjectFileOrErr)
  598. return ObjectFileOrErr.takeError();
  599. OF = std::move(ObjectFileOrErr.get());
  600. } else if (isa<ObjectFile>(Bin.get())) {
  601. // For any other object file, upcast and take ownership.
  602. OF.reset(cast<ObjectFile>(Bin.release()));
  603. // If we've asked for a particular arch, make sure they match.
  604. if (!Arch.empty() && OF->getArch() != Triple(Arch).getArch())
  605. return errorCodeToError(object_error::arch_not_found);
  606. } else
  607. // We can only handle object files.
  608. return make_error<CoverageMapError>(coveragemap_error::malformed);
  609. // The coverage uses native pointer sizes for the object it's written in.
  610. BytesInAddress = OF->getBytesInAddress();
  611. Endian = OF->isLittleEndian() ? support::endianness::little
  612. : support::endianness::big;
  613. // Look for the sections that we are interested in.
  614. auto ObjFormat = OF->getTripleObjectFormat();
  615. auto NamesSection =
  616. lookupSection(*OF, getInstrProfSectionName(IPSK_name, ObjFormat,
  617. /*AddSegmentInfo=*/false));
  618. if (auto E = NamesSection.takeError())
  619. return E;
  620. auto CoverageSection =
  621. lookupSection(*OF, getInstrProfSectionName(IPSK_covmap, ObjFormat,
  622. /*AddSegmentInfo=*/false));
  623. if (auto E = CoverageSection.takeError())
  624. return E;
  625. // Get the contents of the given sections.
  626. if (auto EC = CoverageSection->getContents(CoverageMapping))
  627. return errorCodeToError(EC);
  628. if (Error E = ProfileNames.create(*NamesSection))
  629. return E;
  630. return Error::success();
  631. }
  632. Expected<std::unique_ptr<BinaryCoverageReader>>
  633. BinaryCoverageReader::create(std::unique_ptr<MemoryBuffer> &ObjectBuffer,
  634. StringRef Arch) {
  635. std::unique_ptr<BinaryCoverageReader> Reader(new BinaryCoverageReader());
  636. StringRef Coverage;
  637. uint8_t BytesInAddress;
  638. support::endianness Endian;
  639. Error E = Error::success();
  640. consumeError(std::move(E));
  641. if (ObjectBuffer->getBuffer().startswith(TestingFormatMagic))
  642. // This is a special format used for testing.
  643. E = loadTestingFormat(ObjectBuffer->getBuffer(), Reader->ProfileNames,
  644. Coverage, BytesInAddress, Endian);
  645. else
  646. E = loadBinaryFormat(ObjectBuffer->getMemBufferRef(), Reader->ProfileNames,
  647. Coverage, BytesInAddress, Endian, Arch);
  648. if (E)
  649. return std::move(E);
  650. if (BytesInAddress == 4 && Endian == support::endianness::little)
  651. E = readCoverageMappingData<uint32_t, support::endianness::little>(
  652. Reader->ProfileNames, Coverage, Reader->MappingRecords,
  653. Reader->Filenames);
  654. else if (BytesInAddress == 4 && Endian == support::endianness::big)
  655. E = readCoverageMappingData<uint32_t, support::endianness::big>(
  656. Reader->ProfileNames, Coverage, Reader->MappingRecords,
  657. Reader->Filenames);
  658. else if (BytesInAddress == 8 && Endian == support::endianness::little)
  659. E = readCoverageMappingData<uint64_t, support::endianness::little>(
  660. Reader->ProfileNames, Coverage, Reader->MappingRecords,
  661. Reader->Filenames);
  662. else if (BytesInAddress == 8 && Endian == support::endianness::big)
  663. E = readCoverageMappingData<uint64_t, support::endianness::big>(
  664. Reader->ProfileNames, Coverage, Reader->MappingRecords,
  665. Reader->Filenames);
  666. else
  667. return make_error<CoverageMapError>(coveragemap_error::malformed);
  668. if (E)
  669. return std::move(E);
  670. return std::move(Reader);
  671. }
  672. Error BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) {
  673. if (CurrentRecord >= MappingRecords.size())
  674. return make_error<CoverageMapError>(coveragemap_error::eof);
  675. FunctionsFilenames.clear();
  676. Expressions.clear();
  677. MappingRegions.clear();
  678. auto &R = MappingRecords[CurrentRecord];
  679. RawCoverageMappingReader Reader(
  680. R.CoverageMapping,
  681. makeArrayRef(Filenames).slice(R.FilenamesBegin, R.FilenamesSize),
  682. FunctionsFilenames, Expressions, MappingRegions);
  683. if (auto Err = Reader.read())
  684. return Err;
  685. Record.FunctionName = R.FunctionName;
  686. Record.FunctionHash = R.FunctionHash;
  687. Record.Filenames = FunctionsFilenames;
  688. Record.Expressions = Expressions;
  689. Record.MappingRegions = MappingRegions;
  690. ++CurrentRecord;
  691. return Error::success();
  692. }