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