Engine.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. #pragma once
  2. #include <stack>
  3. #include "../CommonDef.h"
  4. #include "gc/GarbageCollector.h"
  5. #include "vm/Exception.h"
  6. #include "vm/StackTrace.h"
  7. #include "../metadata/MetadataUtil.h"
  8. #include "../RuntimeConfig.h"
  9. #include "InterpreterDefs.h"
  10. #include "MemoryUtil.h"
  11. #include "MethodBridge.h"
  12. //#if DEBUG
  13. //#define PUSH_STACK_FRAME(method) do { \
  14. // Il2CppStackFrameInfo stackFrameInfo = { method, (uintptr_t)method->methodPointer }; \
  15. // il2cpp::vm::StackTrace::PushFrame(stackFrameInfo); \
  16. //} while(0)
  17. //
  18. //#define POP_STACK_FRAME() do { il2cpp::vm::StackTrace::PopFrame(); } while(0)
  19. //
  20. //#else
  21. #define PUSH_STACK_FRAME(method)
  22. #define POP_STACK_FRAME()
  23. //#endif
  24. namespace hybridclr
  25. {
  26. namespace interpreter
  27. {
  28. class MachineState
  29. {
  30. public:
  31. MachineState()
  32. {
  33. _stackSize = -1;
  34. _stackBase = nullptr;
  35. _stackTopIdx = 0;
  36. _localPoolBottomIdx = -1;
  37. _frameBase = nullptr;
  38. _frameCount = -1;
  39. _frameTopIdx = 0;
  40. _exceptionFlowBase = nullptr;
  41. _exceptionFlowCount = -1;
  42. _exceptionFlowTopIdx = 0;
  43. }
  44. ~MachineState()
  45. {
  46. if (_stackBase)
  47. {
  48. //il2cpp::gc::GarbageCollector::FreeFixed(_stackBase);
  49. il2cpp::gc::GarbageCollector::UnregisterDynamicRoot(this);
  50. HYBRIDCLR_FREE(_stackBase);
  51. }
  52. if (_frameBase)
  53. {
  54. HYBRIDCLR_FREE(_frameBase);
  55. }
  56. if (_exceptionFlowBase)
  57. {
  58. HYBRIDCLR_FREE(_exceptionFlowBase);
  59. }
  60. }
  61. static std::pair<char*, size_t> GetGCRootData(void* root)
  62. {
  63. MachineState* machineState = (MachineState*)root;
  64. if (machineState->_stackBase && machineState->_stackTopIdx > 0)
  65. {
  66. return std::make_pair((char*)machineState->_stackBase, machineState->_stackTopIdx * sizeof(StackObject));
  67. }
  68. else
  69. {
  70. return std::make_pair(nullptr, 0);
  71. }
  72. }
  73. StackObject* AllocArgments(int32_t argCount)
  74. {
  75. return AllocStackSlot(argCount);
  76. }
  77. StackObject* GetStackBasePtr() const
  78. {
  79. return _stackBase;
  80. }
  81. int32_t GetStackTop() const
  82. {
  83. return _stackTopIdx;
  84. }
  85. StackObject* AllocStackSlot(int32_t slotNum)
  86. {
  87. if (_stackTopIdx + slotNum > _localPoolBottomIdx)
  88. {
  89. if (!_stackBase)
  90. {
  91. InitEvalStack();
  92. }
  93. if (_stackTopIdx + slotNum > _localPoolBottomIdx)
  94. {
  95. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetStackOverflowException("AllocStackSlot"));
  96. }
  97. }
  98. StackObject* dataPtr = _stackBase + _stackTopIdx;
  99. _stackTopIdx += slotNum;
  100. #if DEBUG
  101. std::memset(dataPtr, 0xEA, slotNum * sizeof(StackObject));
  102. #endif
  103. return dataPtr;
  104. }
  105. void* AllocLocalloc(size_t size)
  106. {
  107. IL2CPP_ASSERT(size % 8 == 0);
  108. int32_t slotNum = (int32_t)(size / 8);
  109. IL2CPP_ASSERT(slotNum > 0);
  110. if (_stackTopIdx + slotNum > _localPoolBottomIdx)
  111. {
  112. if (!_stackBase)
  113. {
  114. InitEvalStack();
  115. }
  116. if (_stackTopIdx + slotNum > _localPoolBottomIdx)
  117. {
  118. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetStackOverflowException("AllocLocalloc"));
  119. }
  120. }
  121. _localPoolBottomIdx -= slotNum;
  122. return _stackBase + _localPoolBottomIdx;
  123. }
  124. void SetStackTop(int32_t oldTop)
  125. {
  126. _stackTopIdx = oldTop;
  127. }
  128. uint32_t GetFrameTopIdx() const
  129. {
  130. return _frameTopIdx;
  131. }
  132. int32_t GetLocalPoolBottomIdx() const
  133. {
  134. return _localPoolBottomIdx;
  135. }
  136. void SetLocalPoolBottomIdx(int32_t idx)
  137. {
  138. _localPoolBottomIdx = idx;
  139. }
  140. InterpFrame* PushFrame()
  141. {
  142. if (_frameTopIdx >= _frameCount)
  143. {
  144. if (!_frameBase)
  145. {
  146. InitFrames();
  147. }
  148. else
  149. {
  150. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetStackOverflowException("AllocFrame"));
  151. }
  152. }
  153. return _frameBase + _frameTopIdx++;
  154. }
  155. void PopFrame()
  156. {
  157. IL2CPP_ASSERT(_frameTopIdx > 0);
  158. --_frameTopIdx;
  159. }
  160. void PopFrameN(int32_t count)
  161. {
  162. IL2CPP_ASSERT(count > 0 && _frameTopIdx >= count);
  163. _frameTopIdx -= count;
  164. }
  165. InterpFrame* GetTopFrame() const
  166. {
  167. if (_frameTopIdx > 0)
  168. {
  169. return _frameBase + _frameTopIdx - 1;
  170. }
  171. else
  172. {
  173. return nullptr;
  174. }
  175. }
  176. ExceptionFlowInfo* AllocExceptionFlow(int32_t count)
  177. {
  178. if (_exceptionFlowTopIdx + count >= _exceptionFlowCount)
  179. {
  180. if (!_exceptionFlowBase)
  181. {
  182. InitExceptionFlows();
  183. }
  184. if (_exceptionFlowTopIdx + count >= _exceptionFlowCount)
  185. {
  186. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetExecutionEngineException("AllocExceptionFlowZero"));
  187. }
  188. }
  189. ExceptionFlowInfo* efi = _exceptionFlowBase + _exceptionFlowTopIdx;
  190. _exceptionFlowTopIdx += count;
  191. return efi;
  192. }
  193. uint32_t GetExceptionFlowTopIdx() const
  194. {
  195. return _exceptionFlowTopIdx;
  196. }
  197. void SetExceptionFlowTopIdx(uint32_t exTopIdx)
  198. {
  199. _exceptionFlowTopIdx = exTopIdx;
  200. }
  201. void SetExceptionFlowTop(ExceptionFlowInfo* top)
  202. {
  203. _exceptionFlowTopIdx = (int32_t)(top - _exceptionFlowBase);
  204. IL2CPP_ASSERT(_exceptionFlowTopIdx >= 0 && _exceptionFlowTopIdx <= _exceptionFlowCount);
  205. }
  206. void PushExecutingImage(const Il2CppImage* image)
  207. {
  208. _executingImageStack.push(image);
  209. }
  210. void PopExecutingImage()
  211. {
  212. _executingImageStack.pop();
  213. }
  214. const Il2CppImage* GetTopExecutingImage() const
  215. {
  216. if (_executingImageStack.empty())
  217. {
  218. return nullptr;
  219. }
  220. else
  221. {
  222. return _executingImageStack.top();
  223. }
  224. }
  225. void CollectFrames(il2cpp::vm::StackFrames* stackFrames)
  226. {
  227. if (_frameTopIdx <= 0)
  228. {
  229. return;
  230. }
  231. stackFrames->insert(stackFrames->begin(), _frameTopIdx, Il2CppStackFrameInfo());
  232. for (int32_t i = 0; i < _frameTopIdx; i++)
  233. {
  234. InterpFrame* frame = _frameBase + i;
  235. const MethodInfo* method = frame->method->method;
  236. (*stackFrames)[i] = {
  237. method
  238. #if HYBRIDCLR_UNITY_2020_OR_NEW
  239. , (uintptr_t)method->methodPointer
  240. #endif
  241. };
  242. }
  243. }
  244. private:
  245. void InitEvalStack()
  246. {
  247. _stackSize = (int32_t)RuntimeConfig::GetInterpreterThreadObjectStackSize();
  248. _stackBase = (StackObject*)HYBRIDCLR_MALLOC_ZERO(RuntimeConfig::GetInterpreterThreadObjectStackSize() * sizeof(StackObject));
  249. _stackTopIdx = 0;
  250. _localPoolBottomIdx = _stackSize;
  251. il2cpp::gc::GarbageCollector::RegisterDynamicRoot(this, GetGCRootData);
  252. }
  253. void InitFrames()
  254. {
  255. _frameBase = (InterpFrame*)HYBRIDCLR_CALLOC(RuntimeConfig::GetInterpreterThreadFrameStackSize(), sizeof(InterpFrame));
  256. _frameCount = (int32_t)RuntimeConfig::GetInterpreterThreadFrameStackSize();
  257. _frameTopIdx = 0;
  258. }
  259. void InitExceptionFlows()
  260. {
  261. _exceptionFlowBase = (ExceptionFlowInfo*)HYBRIDCLR_CALLOC(RuntimeConfig::GetInterpreterThreadExceptionFlowSize(), sizeof(ExceptionFlowInfo));
  262. _exceptionFlowCount = (int32_t)RuntimeConfig::GetInterpreterThreadExceptionFlowSize();
  263. _exceptionFlowTopIdx = 0;
  264. }
  265. StackObject* _stackBase;
  266. int32_t _stackSize;
  267. int32_t _stackTopIdx;
  268. int32_t _localPoolBottomIdx;
  269. InterpFrame* _frameBase;
  270. int32_t _frameTopIdx;
  271. int32_t _frameCount;
  272. ExceptionFlowInfo* _exceptionFlowBase;
  273. int32_t _exceptionFlowTopIdx;
  274. int32_t _exceptionFlowCount;
  275. std::stack<const Il2CppImage*> _executingImageStack;
  276. };
  277. class ExecutingInterpImageScope
  278. {
  279. public:
  280. ExecutingInterpImageScope(MachineState& state, const Il2CppImage* image) : _state(state)
  281. {
  282. _state.PushExecutingImage(image);
  283. }
  284. ~ExecutingInterpImageScope()
  285. {
  286. _state.PopExecutingImage();
  287. }
  288. private:
  289. MachineState& _state;
  290. };
  291. class InterpFrameGroup
  292. {
  293. public:
  294. InterpFrameGroup(MachineState& ms) : _machineState(ms), _stackBaseIdx(ms.GetStackTop()), _frameBaseIdx(ms.GetFrameTopIdx())
  295. {
  296. }
  297. void CleanUpFrames()
  298. {
  299. IL2CPP_ASSERT(_machineState.GetFrameTopIdx() >= _frameBaseIdx);
  300. uint32_t n = _machineState.GetFrameTopIdx() - _frameBaseIdx;
  301. if (n > 0)
  302. {
  303. for (uint32_t i = 0; i < n; i++)
  304. {
  305. LeaveFrame();
  306. }
  307. }
  308. }
  309. InterpFrame* EnterFrameFromInterpreter(const InterpMethodInfo* imi, StackObject* argBase);
  310. InterpFrame* EnterFrameFromNative(const InterpMethodInfo* imi, StackObject* argBase);
  311. InterpFrame* LeaveFrame();
  312. void* AllocLoc(size_t originSize, bool fillZero)
  313. {
  314. if (originSize == 0)
  315. {
  316. return nullptr;
  317. }
  318. size_t size = (originSize + 7) & ~(size_t)7;
  319. void* data = _machineState.AllocLocalloc(size);
  320. if (fillZero)
  321. {
  322. std::memset(data, 0, size);
  323. }
  324. return data;
  325. }
  326. size_t GetFrameCount() const { return _machineState.GetFrameTopIdx() - _frameBaseIdx; }
  327. private:
  328. MachineState& _machineState;
  329. int32_t _stackBaseIdx;
  330. uint32_t _frameBaseIdx;
  331. };
  332. class StackObjectAllocScope
  333. {
  334. private:
  335. MachineState& _state;
  336. const int32_t _originStackTop;
  337. const int32_t _count;
  338. StackObject* _data;
  339. public:
  340. StackObjectAllocScope(MachineState& state, int32_t count) : _state(state), _count(count), _originStackTop(_state.GetStackTop())
  341. {
  342. _data = state.AllocStackSlot(count);
  343. }
  344. ~StackObjectAllocScope()
  345. {
  346. IL2CPP_ASSERT(_state.GetStackTop() > _originStackTop);
  347. _state.SetStackTop(_originStackTop);
  348. }
  349. StackObject* GetData() const { return _data; }
  350. };
  351. }
  352. }