StackMapParser.h 14 KB

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