Engine.h 7.5 KB

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