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