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 | |
| 15 | #include "llvm/ProfileData/CoverageMappingReader.h" |
| 16 | #include "llvm/ADT/DenseSet.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" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | using namespace coverage; |
| 26 | using namespace object; |
| 27 | |
Justin Bogner | f584649 | 2014-09-20 15:31:51 +0000 | [diff] [blame] | 28 | #define DEBUG_TYPE "coverage-mapping" |
| 29 | |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 30 | void CoverageMappingIterator::increment() { |
| 31 | // Check if all the records were read or if an error occurred while reading |
| 32 | // the next record. |
| 33 | if (Reader->readNextRecord(Record)) |
| 34 | *this = CoverageMappingIterator(); |
| 35 | } |
| 36 | |
| 37 | std::error_code RawCoverageReader::readULEB128(uint64_t &Result) { |
| 38 | if (Data.size() < 1) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 39 | return coveragemap_error::truncated; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 40 | unsigned N = 0; |
| 41 | Result = decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N); |
| 42 | if (N > Data.size()) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 43 | return coveragemap_error::malformed; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 44 | Data = Data.substr(N); |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 45 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | std::error_code RawCoverageReader::readIntMax(uint64_t &Result, |
| 49 | uint64_t MaxPlus1) { |
| 50 | if (auto Err = readULEB128(Result)) |
| 51 | return Err; |
| 52 | if (Result >= MaxPlus1) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 53 | return coveragemap_error::malformed; |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 54 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | std::error_code RawCoverageReader::readSize(uint64_t &Result) { |
| 58 | if (auto Err = readULEB128(Result)) |
| 59 | return Err; |
| 60 | // Sanity check the number. |
| 61 | if (Result > Data.size()) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 62 | return coveragemap_error::malformed; |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 63 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | std::error_code RawCoverageReader::readString(StringRef &Result) { |
| 67 | uint64_t Length; |
| 68 | if (auto Err = readSize(Length)) |
| 69 | return Err; |
| 70 | Result = Data.substr(0, Length); |
| 71 | Data = Data.substr(Length); |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 72 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | std::error_code RawCoverageFilenamesReader::read() { |
| 76 | uint64_t NumFilenames; |
| 77 | if (auto Err = readSize(NumFilenames)) |
| 78 | return Err; |
| 79 | for (size_t I = 0; I < NumFilenames; ++I) { |
| 80 | StringRef Filename; |
| 81 | if (auto Err = readString(Filename)) |
| 82 | return Err; |
| 83 | Filenames.push_back(Filename); |
| 84 | } |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 85 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | std::error_code RawCoverageMappingReader::decodeCounter(unsigned Value, |
| 89 | Counter &C) { |
| 90 | auto Tag = Value & Counter::EncodingTagMask; |
| 91 | switch (Tag) { |
| 92 | case Counter::Zero: |
| 93 | C = Counter::getZero(); |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 94 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 95 | case Counter::CounterValueReference: |
| 96 | C = Counter::getCounter(Value >> Counter::EncodingTagBits); |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 97 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 98 | default: |
| 99 | break; |
| 100 | } |
| 101 | Tag -= Counter::Expression; |
| 102 | switch (Tag) { |
| 103 | case CounterExpression::Subtract: |
| 104 | case CounterExpression::Add: { |
| 105 | auto ID = Value >> Counter::EncodingTagBits; |
| 106 | if (ID >= Expressions.size()) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 107 | return coveragemap_error::malformed; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 108 | Expressions[ID].Kind = CounterExpression::ExprKind(Tag); |
| 109 | C = Counter::getExpression(ID); |
| 110 | break; |
| 111 | } |
| 112 | default: |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 113 | return coveragemap_error::malformed; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 114 | } |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 115 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | std::error_code RawCoverageMappingReader::readCounter(Counter &C) { |
| 119 | uint64_t EncodedCounter; |
| 120 | if (auto Err = |
| 121 | readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max())) |
| 122 | return Err; |
| 123 | if (auto Err = decodeCounter(EncodedCounter, C)) |
| 124 | return Err; |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 125 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static const unsigned EncodingExpansionRegionBit = 1 |
| 129 | << Counter::EncodingTagBits; |
| 130 | |
| 131 | /// \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] | 132 | /// \param NumFileIDs the number of file ids that are defined for this |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 133 | /// function. |
| 134 | std::error_code RawCoverageMappingReader::readMappingRegionsSubArray( |
| 135 | std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID, |
| 136 | size_t NumFileIDs) { |
| 137 | uint64_t NumRegions; |
| 138 | if (auto Err = readSize(NumRegions)) |
| 139 | return Err; |
| 140 | unsigned LineStart = 0; |
| 141 | for (size_t I = 0; I < NumRegions; ++I) { |
| 142 | Counter C; |
| 143 | CounterMappingRegion::RegionKind Kind = CounterMappingRegion::CodeRegion; |
| 144 | |
| 145 | // Read the combined counter + region kind. |
| 146 | uint64_t EncodedCounterAndRegion; |
| 147 | if (auto Err = readIntMax(EncodedCounterAndRegion, |
| 148 | std::numeric_limits<unsigned>::max())) |
| 149 | return Err; |
| 150 | unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask; |
| 151 | uint64_t ExpandedFileID = 0; |
| 152 | if (Tag != Counter::Zero) { |
| 153 | if (auto Err = decodeCounter(EncodedCounterAndRegion, C)) |
| 154 | return Err; |
| 155 | } else { |
| 156 | // Is it an expansion region? |
| 157 | if (EncodedCounterAndRegion & EncodingExpansionRegionBit) { |
| 158 | Kind = CounterMappingRegion::ExpansionRegion; |
| 159 | ExpandedFileID = EncodedCounterAndRegion >> |
| 160 | Counter::EncodingCounterTagAndExpansionRegionTagBits; |
| 161 | if (ExpandedFileID >= NumFileIDs) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 162 | return coveragemap_error::malformed; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 163 | } else { |
| 164 | switch (EncodedCounterAndRegion >> |
| 165 | Counter::EncodingCounterTagAndExpansionRegionTagBits) { |
| 166 | case CounterMappingRegion::CodeRegion: |
| 167 | // Don't do anything when we have a code region with a zero counter. |
| 168 | break; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 169 | case CounterMappingRegion::SkippedRegion: |
| 170 | Kind = CounterMappingRegion::SkippedRegion; |
| 171 | break; |
| 172 | default: |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 173 | return coveragemap_error::malformed; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Read the source range. |
Justin Bogner | de15817 | 2015-02-03 21:35:36 +0000 | [diff] [blame] | 179 | uint64_t LineStartDelta, ColumnStart, NumLines, ColumnEnd; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 180 | if (auto Err = |
| 181 | readIntMax(LineStartDelta, std::numeric_limits<unsigned>::max())) |
| 182 | return Err; |
Justin Bogner | de15817 | 2015-02-03 21:35:36 +0000 | [diff] [blame] | 183 | if (auto Err = readULEB128(ColumnStart)) |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 184 | return Err; |
Alex Lorenz | 1193b5e | 2014-08-04 18:00:51 +0000 | [diff] [blame] | 185 | if (ColumnStart > std::numeric_limits<unsigned>::max()) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 186 | return coveragemap_error::malformed; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 187 | if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max())) |
| 188 | return Err; |
| 189 | if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max())) |
| 190 | return Err; |
| 191 | LineStart += LineStartDelta; |
| 192 | // Adjust the column locations for the empty regions that are supposed to |
| 193 | // cover whole lines. Those regions should be encoded with the |
| 194 | // column range (1 -> std::numeric_limits<unsigned>::max()), but because |
| 195 | // the encoded std::numeric_limits<unsigned>::max() is several bytes long, |
| 196 | // we set the column range to (0 -> 0) to ensure that the column start and |
| 197 | // column end take up one byte each. |
| 198 | // The std::numeric_limits<unsigned>::max() is used to represent a column |
| 199 | // position at the end of the line without knowing the length of that line. |
| 200 | if (ColumnStart == 0 && ColumnEnd == 0) { |
| 201 | ColumnStart = 1; |
| 202 | ColumnEnd = std::numeric_limits<unsigned>::max(); |
| 203 | } |
Justin Bogner | f584649 | 2014-09-20 15:31:51 +0000 | [diff] [blame] | 204 | |
| 205 | DEBUG({ |
| 206 | dbgs() << "Counter in file " << InferredFileID << " " << LineStart << ":" |
| 207 | << ColumnStart << " -> " << (LineStart + NumLines) << ":" |
| 208 | << ColumnEnd << ", "; |
| 209 | if (Kind == CounterMappingRegion::ExpansionRegion) |
| 210 | dbgs() << "Expands to file " << ExpandedFileID; |
| 211 | else |
| 212 | CounterMappingContext(Expressions).dump(C, dbgs()); |
| 213 | dbgs() << "\n"; |
| 214 | }); |
| 215 | |
Justin Bogner | 26b3142 | 2015-02-03 23:59:33 +0000 | [diff] [blame] | 216 | MappingRegions.push_back(CounterMappingRegion( |
| 217 | C, InferredFileID, ExpandedFileID, LineStart, ColumnStart, |
| 218 | LineStart + NumLines, ColumnEnd, Kind)); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 219 | } |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 220 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Justin Bogner | 195a4f0 | 2015-02-03 00:20:11 +0000 | [diff] [blame] | 223 | std::error_code RawCoverageMappingReader::read() { |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 224 | |
| 225 | // Read the virtual file mapping. |
| 226 | llvm::SmallVector<unsigned, 8> VirtualFileMapping; |
| 227 | uint64_t NumFileMappings; |
| 228 | if (auto Err = readSize(NumFileMappings)) |
| 229 | return Err; |
| 230 | for (size_t I = 0; I < NumFileMappings; ++I) { |
| 231 | uint64_t FilenameIndex; |
| 232 | if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size())) |
| 233 | return Err; |
| 234 | VirtualFileMapping.push_back(FilenameIndex); |
| 235 | } |
| 236 | |
| 237 | // Construct the files using unique filenames and virtual file mapping. |
| 238 | for (auto I : VirtualFileMapping) { |
| 239 | Filenames.push_back(TranslationUnitFilenames[I]); |
| 240 | } |
| 241 | |
| 242 | // Read the expressions. |
| 243 | uint64_t NumExpressions; |
| 244 | if (auto Err = readSize(NumExpressions)) |
| 245 | return Err; |
| 246 | // Create an array of dummy expressions that get the proper counters |
| 247 | // when the expressions are read, and the proper kinds when the counters |
| 248 | // are decoded. |
| 249 | Expressions.resize( |
| 250 | NumExpressions, |
| 251 | CounterExpression(CounterExpression::Subtract, Counter(), Counter())); |
| 252 | for (size_t I = 0; I < NumExpressions; ++I) { |
| 253 | if (auto Err = readCounter(Expressions[I].LHS)) |
| 254 | return Err; |
| 255 | if (auto Err = readCounter(Expressions[I].RHS)) |
| 256 | return Err; |
| 257 | } |
| 258 | |
| 259 | // Read the mapping regions sub-arrays. |
| 260 | for (unsigned InferredFileID = 0, S = VirtualFileMapping.size(); |
| 261 | InferredFileID < S; ++InferredFileID) { |
| 262 | if (auto Err = readMappingRegionsSubArray(MappingRegions, InferredFileID, |
| 263 | VirtualFileMapping.size())) |
| 264 | return Err; |
| 265 | } |
| 266 | |
| 267 | // Set the counters for the expansion regions. |
| 268 | // i.e. Counter of expansion region = counter of the first region |
| 269 | // from the expanded file. |
| 270 | // Perform multiple passes to correctly propagate the counters through |
| 271 | // all the nested expansion regions. |
Alex Lorenz | 251b3e3 | 2014-07-29 21:42:24 +0000 | [diff] [blame] | 272 | SmallVector<CounterMappingRegion *, 8> FileIDExpansionRegionMapping; |
| 273 | FileIDExpansionRegionMapping.resize(VirtualFileMapping.size(), nullptr); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 274 | for (unsigned Pass = 1, S = VirtualFileMapping.size(); Pass < S; ++Pass) { |
Alex Lorenz | 251b3e3 | 2014-07-29 21:42:24 +0000 | [diff] [blame] | 275 | for (auto &R : MappingRegions) { |
| 276 | if (R.Kind != CounterMappingRegion::ExpansionRegion) |
| 277 | continue; |
| 278 | assert(!FileIDExpansionRegionMapping[R.ExpandedFileID]); |
| 279 | FileIDExpansionRegionMapping[R.ExpandedFileID] = &R; |
| 280 | } |
| 281 | for (auto &R : MappingRegions) { |
| 282 | if (FileIDExpansionRegionMapping[R.FileID]) { |
| 283 | FileIDExpansionRegionMapping[R.FileID]->Count = R.Count; |
| 284 | FileIDExpansionRegionMapping[R.FileID] = nullptr; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 289 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 292 | namespace { |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 293 | |
| 294 | /// \brief A helper structure to access the data from a section |
| 295 | /// in an object file. |
| 296 | struct SectionData { |
| 297 | StringRef Data; |
| 298 | uint64_t Address; |
| 299 | |
| 300 | std::error_code load(SectionRef &Section) { |
| 301 | if (auto Err = Section.getContents(Data)) |
| 302 | return Err; |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 303 | Address = Section.getAddress(); |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 304 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | std::error_code get(uint64_t Pointer, size_t Size, StringRef &Result) { |
| 308 | if (Pointer < Address) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 309 | return coveragemap_error::malformed; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 310 | auto Offset = Pointer - Address; |
| 311 | if (Offset + Size > Data.size()) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 312 | return coveragemap_error::malformed; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 313 | Result = Data.substr(Pointer - Address, Size); |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 314 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 315 | } |
| 316 | }; |
| 317 | } |
| 318 | |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 319 | template <typename T, support::endianness Endian> |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 320 | std::error_code readCoverageMappingData( |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 321 | SectionData &ProfileNames, StringRef Data, |
Justin Bogner | e84891a | 2015-02-26 20:06:24 +0000 | [diff] [blame] | 322 | std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records, |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 323 | std::vector<StringRef> &Filenames) { |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 324 | using namespace support; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 325 | llvm::DenseSet<T> UniqueFunctionMappingData; |
| 326 | |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 327 | // Read the records in the coverage data section. |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 328 | for (const char *Buf = Data.data(), *End = Buf + Data.size(); Buf < End;) { |
| 329 | if (Buf + 4 * sizeof(uint32_t) > End) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 330 | return coveragemap_error::malformed; |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 331 | uint32_t NRecords = endian::readNext<uint32_t, Endian, unaligned>(Buf); |
| 332 | uint32_t FilenamesSize = endian::readNext<uint32_t, Endian, unaligned>(Buf); |
| 333 | uint32_t CoverageSize = endian::readNext<uint32_t, Endian, unaligned>(Buf); |
| 334 | uint32_t Version = endian::readNext<uint32_t, Endian, unaligned>(Buf); |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 335 | |
| 336 | switch (Version) { |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 337 | case CoverageMappingVersion1: |
| 338 | break; |
| 339 | default: |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 340 | return coveragemap_error::unsupported_version; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 341 | } |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 342 | |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 343 | // Skip past the function records, saving the start and end for later. |
| 344 | const char *FunBuf = Buf; |
| 345 | Buf += NRecords * (sizeof(T) + 2 * sizeof(uint32_t) + sizeof(uint64_t)); |
| 346 | const char *FunEnd = Buf; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 347 | |
| 348 | // Get the filenames. |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 349 | if (Buf + FilenamesSize > End) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 350 | return coveragemap_error::malformed; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 351 | size_t FilenamesBegin = Filenames.size(); |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 352 | RawCoverageFilenamesReader Reader(StringRef(Buf, FilenamesSize), Filenames); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 353 | if (auto Err = Reader.read()) |
| 354 | return Err; |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 355 | Buf += FilenamesSize; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 356 | |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 357 | // We'll read the coverage mapping records in the loop below. |
| 358 | const char *CovBuf = Buf; |
| 359 | Buf += CoverageSize; |
| 360 | const char *CovEnd = Buf; |
| 361 | if (Buf > End) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 362 | return coveragemap_error::malformed; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 363 | |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 364 | while (FunBuf < FunEnd) { |
| 365 | // Read the function information |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 366 | T NamePtr = endian::readNext<T, Endian, unaligned>(FunBuf); |
| 367 | uint32_t NameSize = endian::readNext<uint32_t, Endian, unaligned>(FunBuf); |
| 368 | uint32_t DataSize = endian::readNext<uint32_t, Endian, unaligned>(FunBuf); |
| 369 | uint64_t FuncHash = endian::readNext<uint64_t, Endian, unaligned>(FunBuf); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 370 | |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 371 | // Now use that to read the coverage data. |
| 372 | if (CovBuf + DataSize > CovEnd) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 373 | return coveragemap_error::malformed; |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 374 | auto Mapping = StringRef(CovBuf, DataSize); |
| 375 | CovBuf += DataSize; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 376 | |
| 377 | // Ignore this record if we already have a record that points to the same |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 378 | // function name. This is useful to ignore the redundant records for the |
| 379 | // functions with ODR linkage. |
| 380 | if (!UniqueFunctionMappingData.insert(NamePtr).second) |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 381 | continue; |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 382 | |
| 383 | // Finally, grab the name and create a record. |
| 384 | StringRef FuncName; |
| 385 | if (std::error_code EC = ProfileNames.get(NamePtr, NameSize, FuncName)) |
| 386 | return EC; |
Justin Bogner | e84891a | 2015-02-26 20:06:24 +0000 | [diff] [blame] | 387 | Records.push_back(BinaryCoverageReader::ProfileMappingRecord( |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 388 | CoverageMappingVersion(Version), FuncName, FuncHash, Mapping, |
Alex Lorenz | 936b99c | 2014-08-21 19:23:25 +0000 | [diff] [blame] | 389 | FilenamesBegin, Filenames.size() - FilenamesBegin)); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 393 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 396 | static const char *TestingFormatMagic = "llvmcovmtestdata"; |
| 397 | |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 398 | static std::error_code loadTestingFormat(StringRef Data, |
| 399 | SectionData &ProfileNames, |
| 400 | StringRef &CoverageMapping, |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 401 | uint8_t &BytesInAddress, |
| 402 | support::endianness &Endian) { |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 403 | BytesInAddress = 8; |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 404 | Endian = support::endianness::little; |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 405 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 406 | Data = Data.substr(StringRef(TestingFormatMagic).size()); |
| 407 | if (Data.size() < 1) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 408 | return coveragemap_error::truncated; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 409 | unsigned N = 0; |
| 410 | auto ProfileNamesSize = |
| 411 | decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N); |
| 412 | if (N > Data.size()) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 413 | return coveragemap_error::malformed; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 414 | Data = Data.substr(N); |
| 415 | if (Data.size() < 1) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 416 | return coveragemap_error::truncated; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 417 | N = 0; |
| 418 | ProfileNames.Address = |
| 419 | decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N); |
| 420 | if (N > Data.size()) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 421 | return coveragemap_error::malformed; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 422 | Data = Data.substr(N); |
| 423 | if (Data.size() < ProfileNamesSize) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 424 | return coveragemap_error::malformed; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 425 | ProfileNames.Data = Data.substr(0, ProfileNamesSize); |
| 426 | CoverageMapping = Data.substr(ProfileNamesSize); |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 427 | return std::error_code(); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Justin Bogner | 5a5c381 | 2015-05-07 00:31:58 +0000 | [diff] [blame] | 430 | static ErrorOr<SectionRef> lookupSection(ObjectFile &OF, StringRef Name) { |
| 431 | StringRef FoundName; |
| 432 | for (const auto &Section : OF.sections()) { |
| 433 | if (auto EC = Section.getName(FoundName)) |
| 434 | return EC; |
| 435 | if (FoundName == Name) |
| 436 | return Section; |
| 437 | } |
| 438 | return coveragemap_error::no_data_found; |
| 439 | } |
| 440 | |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 441 | static std::error_code loadBinaryFormat(MemoryBufferRef ObjectBuffer, |
| 442 | SectionData &ProfileNames, |
| 443 | StringRef &CoverageMapping, |
Justin Bogner | 4379535 | 2015-03-11 02:30:51 +0000 | [diff] [blame] | 444 | uint8_t &BytesInAddress, |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 445 | support::endianness &Endian, |
Justin Bogner | 4379535 | 2015-03-11 02:30:51 +0000 | [diff] [blame] | 446 | Triple::ArchType Arch) { |
| 447 | auto BinOrErr = object::createBinary(ObjectBuffer); |
| 448 | if (std::error_code EC = BinOrErr.getError()) |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 449 | return EC; |
Justin Bogner | 4379535 | 2015-03-11 02:30:51 +0000 | [diff] [blame] | 450 | auto Bin = std::move(BinOrErr.get()); |
| 451 | std::unique_ptr<ObjectFile> OF; |
| 452 | if (auto *Universal = dyn_cast<object::MachOUniversalBinary>(Bin.get())) { |
| 453 | // If we have a universal binary, try to look up the object for the |
| 454 | // appropriate architecture. |
| 455 | auto ObjectFileOrErr = Universal->getObjectForArch(Arch); |
| 456 | if (std::error_code EC = ObjectFileOrErr.getError()) |
| 457 | return EC; |
| 458 | OF = std::move(ObjectFileOrErr.get()); |
| 459 | } else if (isa<object::ObjectFile>(Bin.get())) { |
| 460 | // For any other object file, upcast and take ownership. |
| 461 | OF.reset(cast<object::ObjectFile>(Bin.release())); |
| 462 | // If we've asked for a particular arch, make sure they match. |
| 463 | if (Arch != Triple::ArchType::UnknownArch && OF->getArch() != Arch) |
| 464 | return object_error::arch_not_found; |
| 465 | } else |
| 466 | // We can only handle object files. |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 467 | return coveragemap_error::malformed; |
Justin Bogner | 4379535 | 2015-03-11 02:30:51 +0000 | [diff] [blame] | 468 | |
| 469 | // 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] | 470 | BytesInAddress = OF->getBytesInAddress(); |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 471 | Endian = OF->isLittleEndian() ? support::endianness::little |
| 472 | : support::endianness::big; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 473 | |
| 474 | // Look for the sections that we are interested in. |
Justin Bogner | 5a5c381 | 2015-05-07 00:31:58 +0000 | [diff] [blame] | 475 | auto NamesSection = lookupSection(*OF, "__llvm_prf_names"); |
| 476 | if (auto EC = NamesSection.getError()) |
| 477 | return EC; |
| 478 | auto CoverageSection = lookupSection(*OF, "__llvm_covmap"); |
| 479 | if (auto EC = CoverageSection.getError()) |
| 480 | return EC; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 481 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 482 | // Get the contents of the given sections. |
Justin Bogner | 5a5c381 | 2015-05-07 00:31:58 +0000 | [diff] [blame] | 483 | if (std::error_code EC = CoverageSection->getContents(CoverageMapping)) |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 484 | return EC; |
Justin Bogner | 5a5c381 | 2015-05-07 00:31:58 +0000 | [diff] [blame] | 485 | if (std::error_code EC = ProfileNames.load(*NamesSection)) |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 486 | return EC; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 487 | |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 488 | return std::error_code(); |
| 489 | } |
| 490 | |
| 491 | ErrorOr<std::unique_ptr<BinaryCoverageReader>> |
Justin Bogner | 4379535 | 2015-03-11 02:30:51 +0000 | [diff] [blame] | 492 | BinaryCoverageReader::create(std::unique_ptr<MemoryBuffer> &ObjectBuffer, |
| 493 | Triple::ArchType Arch) { |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 494 | std::unique_ptr<BinaryCoverageReader> Reader(new BinaryCoverageReader()); |
| 495 | |
| 496 | SectionData Profile; |
| 497 | StringRef Coverage; |
| 498 | uint8_t BytesInAddress; |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 499 | support::endianness Endian; |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 500 | std::error_code EC; |
| 501 | if (ObjectBuffer->getBuffer().startswith(TestingFormatMagic)) |
| 502 | // This is a special format used for testing. |
| 503 | EC = loadTestingFormat(ObjectBuffer->getBuffer(), Profile, Coverage, |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 504 | BytesInAddress, Endian); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 505 | else |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 506 | EC = loadBinaryFormat(ObjectBuffer->getMemBufferRef(), Profile, Coverage, |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 507 | BytesInAddress, Endian, Arch); |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 508 | if (EC) |
| 509 | return EC; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 510 | |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 511 | if (BytesInAddress == 4 && Endian == support::endianness::little) |
| 512 | EC = readCoverageMappingData<uint32_t, support::endianness::little>( |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 513 | Profile, Coverage, Reader->MappingRecords, Reader->Filenames); |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 514 | else if (BytesInAddress == 4 && Endian == support::endianness::big) |
| 515 | EC = readCoverageMappingData<uint32_t, support::endianness::big>( |
| 516 | Profile, Coverage, Reader->MappingRecords, Reader->Filenames); |
| 517 | else if (BytesInAddress == 8 && Endian == support::endianness::little) |
| 518 | EC = readCoverageMappingData<uint64_t, support::endianness::little>( |
| 519 | Profile, Coverage, Reader->MappingRecords, Reader->Filenames); |
| 520 | else if (BytesInAddress == 8 && Endian == support::endianness::big) |
| 521 | EC = readCoverageMappingData<uint64_t, support::endianness::big>( |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 522 | Profile, Coverage, Reader->MappingRecords, Reader->Filenames); |
| 523 | else |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 524 | return coveragemap_error::malformed; |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 525 | if (EC) |
| 526 | return EC; |
| 527 | return std::move(Reader); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | std::error_code |
Justin Bogner | e84891a | 2015-02-26 20:06:24 +0000 | [diff] [blame] | 531 | BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) { |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 532 | if (CurrentRecord >= MappingRecords.size()) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 533 | return coveragemap_error::eof; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 534 | |
| 535 | FunctionsFilenames.clear(); |
| 536 | Expressions.clear(); |
| 537 | MappingRegions.clear(); |
| 538 | auto &R = MappingRecords[CurrentRecord]; |
| 539 | RawCoverageMappingReader Reader( |
Justin Bogner | 195a4f0 | 2015-02-03 00:20:11 +0000 | [diff] [blame] | 540 | R.CoverageMapping, |
Justin Bogner | 346359d | 2015-02-03 00:00:00 +0000 | [diff] [blame] | 541 | makeArrayRef(Filenames).slice(R.FilenamesBegin, R.FilenamesSize), |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 542 | FunctionsFilenames, Expressions, MappingRegions); |
Justin Bogner | 195a4f0 | 2015-02-03 00:20:11 +0000 | [diff] [blame] | 543 | if (auto Err = Reader.read()) |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 544 | return Err; |
Justin Bogner | 195a4f0 | 2015-02-03 00:20:11 +0000 | [diff] [blame] | 545 | |
| 546 | Record.FunctionName = R.FunctionName; |
Alex Lorenz | 936b99c | 2014-08-21 19:23:25 +0000 | [diff] [blame] | 547 | Record.FunctionHash = R.FunctionHash; |
Justin Bogner | 195a4f0 | 2015-02-03 00:20:11 +0000 | [diff] [blame] | 548 | Record.Filenames = FunctionsFilenames; |
| 549 | Record.Expressions = Expressions; |
| 550 | Record.MappingRegions = MappingRegions; |
| 551 | |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 552 | ++CurrentRecord; |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 553 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 554 | } |