StackMapParser.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- StackMapParser.h - StackMap Parsing Support --------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_OBJECT_STACKMAPPARSER_H
  14. #define LLVM_OBJECT_STACKMAPPARSER_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/iterator_range.h"
  17. #include "llvm/Object/ELF.h"
  18. #include "llvm/Support/Endian.h"
  19. #include <cassert>
  20. #include <cstddef>
  21. #include <cstdint>
  22. #include <vector>
  23. namespace llvm {
  24. /// A parser for the latest stackmap format. At the moment, latest=V3.
  25. template <support::endianness Endianness>
  26. class StackMapParser {
  27. public:
  28. template <typename AccessorT>
  29. class AccessorIterator {
  30. public:
  31. AccessorIterator(AccessorT A) : A(A) {}
  32. AccessorIterator& operator++() { A = A.next(); return *this; }
  33. AccessorIterator operator++(int) {
  34. auto tmp = *this;
  35. ++*this;
  36. return tmp;
  37. }
  38. bool operator==(const AccessorIterator &Other) const {
  39. return A.P == Other.A.P;
  40. }
  41. bool operator!=(const AccessorIterator &Other) const {
  42. return !(*this == Other);
  43. }
  44. AccessorT& operator*() { return A; }
  45. AccessorT* operator->() { return &A; }
  46. private:
  47. AccessorT A;
  48. };
  49. /// Accessor for function records.
  50. class FunctionAccessor {
  51. friend class StackMapParser;
  52. public:
  53. /// Get the function address.
  54. uint64_t getFunctionAddress() const {
  55. return read<uint64_t>(P);
  56. }
  57. /// Get the function's stack size.
  58. uint64_t getStackSize() const {
  59. return read<uint64_t>(P + sizeof(uint64_t));
  60. }
  61. /// Get the number of callsite records.
  62. uint64_t getRecordCount() const {
  63. return read<uint64_t>(P + (2 * sizeof(uint64_t)));
  64. }
  65. private:
  66. FunctionAccessor(const uint8_t *P) : P(P) {}
  67. const static int FunctionAccessorSize = 3 * sizeof(uint64_t);
  68. FunctionAccessor next() const {
  69. return FunctionAccessor(P + FunctionAccessorSize);
  70. }
  71. const uint8_t *P;
  72. };
  73. /// Accessor for constants.
  74. class ConstantAccessor {
  75. friend class StackMapParser;
  76. public:
  77. /// Return the value of this constant.
  78. uint64_t getValue() const { return read<uint64_t>(P); }
  79. private:
  80. ConstantAccessor(const uint8_t *P) : P(P) {}
  81. const static int ConstantAccessorSize = sizeof(uint64_t);
  82. ConstantAccessor next() const {
  83. return ConstantAccessor(P + ConstantAccessorSize);
  84. }
  85. const uint8_t *P;
  86. };
  87. enum class LocationKind : uint8_t {
  88. Register = 1, Direct = 2, Indirect = 3, Constant = 4, ConstantIndex = 5
  89. };
  90. /// Accessor for location records.
  91. class LocationAccessor {
  92. friend class StackMapParser;
  93. friend class RecordAccessor;
  94. public:
  95. /// Get the Kind for this location.
  96. LocationKind getKind() const {
  97. return LocationKind(P[KindOffset]);
  98. }
  99. /// Get the Size for this location.
  100. unsigned getSizeInBytes() const {
  101. return read<uint16_t>(P + SizeOffset);
  102. }
  103. /// Get the Dwarf register number for this location.
  104. uint16_t getDwarfRegNum() const {
  105. return read<uint16_t>(P + DwarfRegNumOffset);
  106. }
  107. /// Get the small-constant for this location. (Kind must be Constant).
  108. uint32_t getSmallConstant() const {
  109. assert(getKind() == LocationKind::Constant && "Not a small constant.");
  110. return read<uint32_t>(P + SmallConstantOffset);
  111. }
  112. /// Get the constant-index for this location. (Kind must be ConstantIndex).
  113. uint32_t getConstantIndex() const {
  114. assert(getKind() == LocationKind::ConstantIndex &&
  115. "Not a constant-index.");
  116. return read<uint32_t>(P + SmallConstantOffset);
  117. }
  118. /// Get the offset for this location. (Kind must be Direct or Indirect).
  119. int32_t getOffset() const {
  120. assert((getKind() == LocationKind::Direct ||
  121. getKind() == LocationKind::Indirect) &&
  122. "Not direct or indirect.");
  123. return read<int32_t>(P + SmallConstantOffset);
  124. }
  125. private:
  126. LocationAccessor(const uint8_t *P) : P(P) {}
  127. LocationAccessor next() const {
  128. return LocationAccessor(P + LocationAccessorSize);
  129. }
  130. static const int KindOffset = 0;
  131. static const int SizeOffset = KindOffset + sizeof(uint16_t);
  132. static const int DwarfRegNumOffset = SizeOffset + sizeof(uint16_t);
  133. static const int SmallConstantOffset = DwarfRegNumOffset + sizeof(uint32_t);
  134. static const int LocationAccessorSize = sizeof(uint64_t) + sizeof(uint32_t);
  135. const uint8_t *P;
  136. };
  137. /// Accessor for stackmap live-out fields.
  138. class LiveOutAccessor {
  139. friend class StackMapParser;
  140. friend class RecordAccessor;
  141. public:
  142. /// Get the Dwarf register number for this live-out.
  143. uint16_t getDwarfRegNum() const {
  144. return read<uint16_t>(P + DwarfRegNumOffset);
  145. }
  146. /// Get the size in bytes of live [sub]register.
  147. unsigned getSizeInBytes() const {
  148. return read<uint8_t>(P + SizeOffset);
  149. }
  150. private:
  151. LiveOutAccessor(const uint8_t *P) : P(P) {}
  152. LiveOutAccessor next() const {
  153. return LiveOutAccessor(P + LiveOutAccessorSize);
  154. }
  155. static const int DwarfRegNumOffset = 0;
  156. static const int SizeOffset =
  157. DwarfRegNumOffset + sizeof(uint16_t) + sizeof(uint8_t);
  158. static const int LiveOutAccessorSize = sizeof(uint32_t);
  159. const uint8_t *P;
  160. };
  161. /// Accessor for stackmap records.
  162. class RecordAccessor {
  163. friend class StackMapParser;
  164. public:
  165. using location_iterator = AccessorIterator<LocationAccessor>;
  166. using liveout_iterator = AccessorIterator<LiveOutAccessor>;
  167. /// Get the patchpoint/stackmap ID for this record.
  168. uint64_t getID() const {
  169. return read<uint64_t>(P + PatchpointIDOffset);
  170. }
  171. /// Get the instruction offset (from the start of the containing function)
  172. /// for this record.
  173. uint32_t getInstructionOffset() const {
  174. return read<uint32_t>(P + InstructionOffsetOffset);
  175. }
  176. /// Get the number of locations contained in this record.
  177. uint16_t getNumLocations() const {
  178. return read<uint16_t>(P + NumLocationsOffset);
  179. }
  180. /// Get the location with the given index.
  181. LocationAccessor getLocation(unsigned LocationIndex) const {
  182. unsigned LocationOffset =
  183. LocationListOffset + LocationIndex * LocationSize;
  184. return LocationAccessor(P + LocationOffset);
  185. }
  186. /// Begin iterator for locations.
  187. location_iterator location_begin() const {
  188. return location_iterator(getLocation(0));
  189. }
  190. /// End iterator for locations.
  191. location_iterator location_end() const {
  192. return location_iterator(getLocation(getNumLocations()));
  193. }
  194. /// Iterator range for locations.
  195. iterator_range<location_iterator> locations() const {
  196. return make_range(location_begin(), location_end());
  197. }
  198. /// Get the number of liveouts contained in this record.
  199. uint16_t getNumLiveOuts() const {
  200. return read<uint16_t>(P + getNumLiveOutsOffset());
  201. }
  202. /// Get the live-out with the given index.
  203. LiveOutAccessor getLiveOut(unsigned LiveOutIndex) const {
  204. unsigned LiveOutOffset =
  205. getNumLiveOutsOffset() + sizeof(uint16_t) + LiveOutIndex * LiveOutSize;
  206. return LiveOutAccessor(P + LiveOutOffset);
  207. }
  208. /// Begin iterator for live-outs.
  209. liveout_iterator liveouts_begin() const {
  210. return liveout_iterator(getLiveOut(0));
  211. }
  212. /// End iterator for live-outs.
  213. liveout_iterator liveouts_end() const {
  214. return liveout_iterator(getLiveOut(getNumLiveOuts()));
  215. }
  216. /// Iterator range for live-outs.
  217. iterator_range<liveout_iterator> liveouts() const {
  218. return make_range(liveouts_begin(), liveouts_end());
  219. }
  220. private:
  221. RecordAccessor(const uint8_t *P) : P(P) {}
  222. unsigned getNumLiveOutsOffset() const {
  223. unsigned LocOffset =
  224. ((LocationListOffset + LocationSize * getNumLocations()) + 7) & ~0x7;
  225. return LocOffset + sizeof(uint16_t);
  226. }
  227. unsigned getSizeInBytes() const {
  228. unsigned RecordSize =
  229. getNumLiveOutsOffset() + sizeof(uint16_t) + getNumLiveOuts() * LiveOutSize;
  230. return (RecordSize + 7) & ~0x7;
  231. }
  232. RecordAccessor next() const {
  233. return RecordAccessor(P + getSizeInBytes());
  234. }
  235. static const unsigned PatchpointIDOffset = 0;
  236. static const unsigned InstructionOffsetOffset =
  237. PatchpointIDOffset + sizeof(uint64_t);
  238. static const unsigned NumLocationsOffset =
  239. InstructionOffsetOffset + sizeof(uint32_t) + sizeof(uint16_t);
  240. static const unsigned LocationListOffset =
  241. NumLocationsOffset + sizeof(uint16_t);
  242. static const unsigned LocationSize = sizeof(uint64_t) + sizeof(uint32_t);
  243. static const unsigned LiveOutSize = sizeof(uint32_t);
  244. const uint8_t *P;
  245. };
  246. /// Construct a parser for a version-3 stackmap. StackMap data will be read
  247. /// from the given array.
  248. StackMapParser(ArrayRef<uint8_t> StackMapSection)
  249. : StackMapSection(StackMapSection) {
  250. ConstantsListOffset = FunctionListOffset + getNumFunctions() * FunctionSize;
  251. assert(StackMapSection[0] == 3 &&
  252. "StackMapParser can only parse version 3 stackmaps");
  253. unsigned CurrentRecordOffset =
  254. ConstantsListOffset + getNumConstants() * ConstantSize;
  255. for (unsigned I = 0, E = getNumRecords(); I != E; ++I) {
  256. StackMapRecordOffsets.push_back(CurrentRecordOffset);
  257. CurrentRecordOffset +=
  258. RecordAccessor(&StackMapSection[CurrentRecordOffset]).getSizeInBytes();
  259. }
  260. }
  261. /// Validates the header of the specified stack map section.
  262. static Error validateHeader(ArrayRef<uint8_t> StackMapSection) {
  263. // See the comment for StackMaps::emitStackmapHeader().
  264. if (StackMapSection.size() < 16)
  265. return object::createError(
  266. "the stack map section size (" + Twine(StackMapSection.size()) +
  267. ") is less than the minimum possible size of its header (16)");
  268. unsigned Version = StackMapSection[0];
  269. if (Version != 3)
  270. return object::createError(
  271. "the version (" + Twine(Version) +
  272. ") of the stack map section is unsupported, the "
  273. "supported version is 3");
  274. return Error::success();
  275. }
  276. using function_iterator = AccessorIterator<FunctionAccessor>;
  277. using constant_iterator = AccessorIterator<ConstantAccessor>;
  278. using record_iterator = AccessorIterator<RecordAccessor>;
  279. /// Get the version number of this stackmap. (Always returns 3).
  280. unsigned getVersion() const { return 3; }
  281. /// Get the number of functions in the stack map.
  282. uint32_t getNumFunctions() const {
  283. return read<uint32_t>(&StackMapSection[NumFunctionsOffset]);
  284. }
  285. /// Get the number of large constants in the stack map.
  286. uint32_t getNumConstants() const {
  287. return read<uint32_t>(&StackMapSection[NumConstantsOffset]);
  288. }
  289. /// Get the number of stackmap records in the stackmap.
  290. uint32_t getNumRecords() const {
  291. return read<uint32_t>(&StackMapSection[NumRecordsOffset]);
  292. }
  293. /// Return an FunctionAccessor for the given function index.
  294. FunctionAccessor getFunction(unsigned FunctionIndex) const {
  295. return FunctionAccessor(StackMapSection.data() +
  296. getFunctionOffset(FunctionIndex));
  297. }
  298. /// Begin iterator for functions.
  299. function_iterator functions_begin() const {
  300. return function_iterator(getFunction(0));
  301. }
  302. /// End iterator for functions.
  303. function_iterator functions_end() const {
  304. return function_iterator(
  305. FunctionAccessor(StackMapSection.data() +
  306. getFunctionOffset(getNumFunctions())));
  307. }
  308. /// Iterator range for functions.
  309. iterator_range<function_iterator> functions() const {
  310. return make_range(functions_begin(), functions_end());
  311. }
  312. /// Return the large constant at the given index.
  313. ConstantAccessor getConstant(unsigned ConstantIndex) const {
  314. return ConstantAccessor(StackMapSection.data() +
  315. getConstantOffset(ConstantIndex));
  316. }
  317. /// Begin iterator for constants.
  318. constant_iterator constants_begin() const {
  319. return constant_iterator(getConstant(0));
  320. }
  321. /// End iterator for constants.
  322. constant_iterator constants_end() const {
  323. return constant_iterator(
  324. ConstantAccessor(StackMapSection.data() +
  325. getConstantOffset(getNumConstants())));
  326. }
  327. /// Iterator range for constants.
  328. iterator_range<constant_iterator> constants() const {
  329. return make_range(constants_begin(), constants_end());
  330. }
  331. /// Return a RecordAccessor for the given record index.
  332. RecordAccessor getRecord(unsigned RecordIndex) const {
  333. std::size_t RecordOffset = StackMapRecordOffsets[RecordIndex];
  334. return RecordAccessor(StackMapSection.data() + RecordOffset);
  335. }
  336. /// Begin iterator for records.
  337. record_iterator records_begin() const {
  338. if (getNumRecords() == 0)
  339. return record_iterator(RecordAccessor(nullptr));
  340. return record_iterator(getRecord(0));
  341. }
  342. /// End iterator for records.
  343. record_iterator records_end() const {
  344. // Records need to be handled specially, since we cache the start addresses
  345. // for them: We can't just compute the 1-past-the-end address, we have to
  346. // look at the last record and use the 'next' method.
  347. if (getNumRecords() == 0)
  348. return record_iterator(RecordAccessor(nullptr));
  349. return record_iterator(getRecord(getNumRecords() - 1).next());
  350. }
  351. /// Iterator range for records.
  352. iterator_range<record_iterator> records() const {
  353. return make_range(records_begin(), records_end());
  354. }
  355. private:
  356. template <typename T>
  357. static T read(const uint8_t *P) {
  358. return support::endian::read<T, Endianness, 1>(P);
  359. }
  360. static const unsigned HeaderOffset = 0;
  361. static const unsigned NumFunctionsOffset = HeaderOffset + sizeof(uint32_t);
  362. static const unsigned NumConstantsOffset = NumFunctionsOffset + sizeof(uint32_t);
  363. static const unsigned NumRecordsOffset = NumConstantsOffset + sizeof(uint32_t);
  364. static const unsigned FunctionListOffset = NumRecordsOffset + sizeof(uint32_t);
  365. static const unsigned FunctionSize = 3 * sizeof(uint64_t);
  366. static const unsigned ConstantSize = sizeof(uint64_t);
  367. std::size_t getFunctionOffset(unsigned FunctionIndex) const {
  368. return FunctionListOffset + FunctionIndex * FunctionSize;
  369. }
  370. std::size_t getConstantOffset(unsigned ConstantIndex) const {
  371. return ConstantsListOffset + ConstantIndex * ConstantSize;
  372. }
  373. ArrayRef<uint8_t> StackMapSection;
  374. unsigned ConstantsListOffset;
  375. std::vector<unsigned> StackMapRecordOffsets;
  376. };
  377. } // end namespace llvm
  378. #endif // LLVM_OBJECT_STACKMAPPARSER_H
  379. #ifdef __GNUC__
  380. #pragma GCC diagnostic pop
  381. #endif