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