wasm_exec_env.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _WASM_EXEC_ENV_H
  6. #define _WASM_EXEC_ENV_H
  7. #include "bh_assert.h"
  8. #if WASM_ENABLE_INTERP != 0
  9. #include "../interpreter/wasm.h"
  10. #endif
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. struct WASMModuleInstanceCommon;
  15. struct WASMInterpFrame;
  16. #if WASM_ENABLE_THREAD_MGR != 0
  17. typedef struct WASMCluster WASMCluster;
  18. #if WASM_ENABLE_DEBUG_INTERP != 0
  19. typedef struct WASMCurrentEnvStatus WASMCurrentEnvStatus;
  20. #endif
  21. #endif
  22. #ifdef OS_ENABLE_HW_BOUND_CHECK
  23. typedef struct WASMJmpBuf {
  24. struct WASMJmpBuf *prev;
  25. korp_jmpbuf jmpbuf;
  26. } WASMJmpBuf;
  27. #endif
  28. /* Execution environment */
  29. typedef struct WASMExecEnv {
  30. /* Next thread's exec env of a WASM module instance. */
  31. struct WASMExecEnv *next;
  32. /* Previous thread's exec env of a WASM module instance. */
  33. struct WASMExecEnv *prev;
  34. /* Note: field module_inst, argv_buf, native_stack_boundary,
  35. suspend_flags, aux_stack_boundary, aux_stack_bottom, and
  36. native_symbol are used by AOTed code, don't change the
  37. places of them */
  38. /* The WASM module instance of current thread */
  39. struct WASMModuleInstanceCommon *module_inst;
  40. #if WASM_ENABLE_AOT != 0
  41. uint32 *argv_buf;
  42. #endif
  43. /* The boundary of native stack. When runtime detects that native
  44. frame may overrun this boundary, it throws stack overflow
  45. exception. */
  46. uint8 *native_stack_boundary;
  47. /* Used to terminate or suspend current thread
  48. bit 0: need to terminate
  49. bit 1: need to suspend
  50. bit 2: need to go into breakpoint
  51. bit 3: return from pthread_exit */
  52. union {
  53. uint32 flags;
  54. uintptr_t __padding__;
  55. } suspend_flags;
  56. /* Auxiliary stack boundary */
  57. union {
  58. uint32 boundary;
  59. uintptr_t __padding__;
  60. } aux_stack_boundary;
  61. /* Auxiliary stack bottom */
  62. union {
  63. uint32 bottom;
  64. uintptr_t __padding__;
  65. } aux_stack_bottom;
  66. #if WASM_ENABLE_AOT != 0
  67. /* Native symbol list, reserved */
  68. void **native_symbol;
  69. #endif
  70. /*
  71. * The lowest stack pointer value observed.
  72. * Assumption: native stack grows to the lower address.
  73. */
  74. uint8 *native_stack_top_min;
  75. #if WASM_ENABLE_FAST_JIT != 0
  76. /**
  77. * Cache for
  78. * - jit native operations in 32-bit target which hasn't 64-bit
  79. * int/float registers, mainly for the operations of double and int64,
  80. * such as F64TOI64, F32TOI64, I64 MUL/REM, and so on.
  81. * - SSE instructions.
  82. **/
  83. uint64 jit_cache[2];
  84. #endif
  85. #if WASM_ENABLE_THREAD_MGR != 0
  86. /* thread return value */
  87. void *thread_ret_value;
  88. /* Must be provided by thread library */
  89. void *(*thread_start_routine)(void *);
  90. void *thread_arg;
  91. /* pointer to the cluster */
  92. WASMCluster *cluster;
  93. /* used to support debugger */
  94. korp_mutex wait_lock;
  95. korp_cond wait_cond;
  96. /* the count of threads which are joining current thread */
  97. uint32 wait_count;
  98. /* whether current thread is detached */
  99. bool thread_is_detached;
  100. #endif
  101. #if WASM_ENABLE_GC != 0
  102. /* Current local object reference variable */
  103. struct WASMLocalObjectRef *cur_local_object_ref;
  104. #endif
  105. #if WASM_ENABLE_DEBUG_INTERP != 0
  106. WASMCurrentEnvStatus *current_status;
  107. #endif
  108. /* attachment for native function */
  109. void *attachment;
  110. void *user_data;
  111. /* Current interpreter frame of current thread */
  112. struct WASMInterpFrame *cur_frame;
  113. /* The native thread handle of current thread */
  114. korp_tid handle;
  115. #if WASM_ENABLE_INTERP != 0 && WASM_ENABLE_FAST_INTERP == 0
  116. BlockAddr block_addr_cache[BLOCK_ADDR_CACHE_SIZE][BLOCK_ADDR_CONFLICT_SIZE];
  117. #endif
  118. #ifdef OS_ENABLE_HW_BOUND_CHECK
  119. WASMJmpBuf *jmpbuf_stack_top;
  120. /* One guard page for the exception check */
  121. uint8 *exce_check_guard_page;
  122. #endif
  123. #if WASM_ENABLE_MEMORY_PROFILING != 0
  124. uint32 max_wasm_stack_used;
  125. #endif
  126. /* The WASM stack size */
  127. uint32 wasm_stack_size;
  128. /* The WASM stack of current thread */
  129. union {
  130. uint64 __make_it_8_byte_aligned_;
  131. struct {
  132. /* The top boundary of the stack. */
  133. uint8 *top_boundary;
  134. /* Top cell index which is free. */
  135. uint8 *top;
  136. /* The WASM stack. */
  137. uint8 bottom[1];
  138. } s;
  139. } wasm_stack;
  140. } WASMExecEnv;
  141. #if WASM_ENABLE_MEMORY_PROFILING != 0
  142. #define RECORD_STACK_USAGE(e, p) \
  143. do { \
  144. if ((e)->native_stack_top_min > (p)) { \
  145. (e)->native_stack_top_min = (p); \
  146. } \
  147. } while (0)
  148. #else
  149. #define RECORD_STACK_USAGE(e, p) (void)0
  150. #endif
  151. WASMExecEnv *
  152. wasm_exec_env_create_internal(struct WASMModuleInstanceCommon *module_inst,
  153. uint32 stack_size);
  154. void
  155. wasm_exec_env_destroy_internal(WASMExecEnv *exec_env);
  156. WASMExecEnv *
  157. wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
  158. uint32 stack_size);
  159. void
  160. wasm_exec_env_destroy(WASMExecEnv *exec_env);
  161. static inline bool
  162. wasm_exec_env_is_aux_stack_managed_by_runtime(WASMExecEnv *exec_env)
  163. {
  164. return exec_env->aux_stack_boundary.boundary != 0
  165. || exec_env->aux_stack_bottom.bottom != 0;
  166. }
  167. /**
  168. * Allocate a WASM frame from the WASM stack.
  169. *
  170. * @param exec_env the current execution environment
  171. * @param size size of the WASM frame, it must be a multiple of 4
  172. *
  173. * @return the WASM frame if there is enough space in the stack area
  174. * with a protection area, NULL otherwise
  175. */
  176. static inline void *
  177. wasm_exec_env_alloc_wasm_frame(WASMExecEnv *exec_env, unsigned size)
  178. {
  179. uint8 *addr = exec_env->wasm_stack.s.top;
  180. bh_assert(!(size & 3));
  181. /* For classic interpreter, the outs area doesn't contain the const cells,
  182. its size cannot be larger than the frame size, so here checking stack
  183. overflow with multiplying by 2 is enough. For fast interpreter, since
  184. the outs area contains const cells, its size may be larger than current
  185. frame size, we should check again before putting the function arguments
  186. into the outs area. */
  187. if (size * 2
  188. > (uint32)(uintptr_t)(exec_env->wasm_stack.s.top_boundary - addr)) {
  189. /* WASM stack overflow. */
  190. return NULL;
  191. }
  192. exec_env->wasm_stack.s.top += size;
  193. #if WASM_ENABLE_MEMORY_PROFILING != 0
  194. {
  195. uint32 wasm_stack_used =
  196. exec_env->wasm_stack.s.top - exec_env->wasm_stack.s.bottom;
  197. if (wasm_stack_used > exec_env->max_wasm_stack_used)
  198. exec_env->max_wasm_stack_used = wasm_stack_used;
  199. }
  200. #endif
  201. return addr;
  202. }
  203. static inline void
  204. wasm_exec_env_free_wasm_frame(WASMExecEnv *exec_env, void *prev_top)
  205. {
  206. bh_assert((uint8 *)prev_top >= exec_env->wasm_stack.s.bottom);
  207. exec_env->wasm_stack.s.top = (uint8 *)prev_top;
  208. }
  209. /**
  210. * Get the current WASM stack top pointer.
  211. *
  212. * @param exec_env the current execution environment
  213. *
  214. * @return the current WASM stack top pointer
  215. */
  216. static inline void *
  217. wasm_exec_env_wasm_stack_top(WASMExecEnv *exec_env)
  218. {
  219. return exec_env->wasm_stack.s.top;
  220. }
  221. /**
  222. * Set the current frame pointer.
  223. *
  224. * @param exec_env the current execution environment
  225. * @param frame the WASM frame to be set for the current exec env
  226. */
  227. static inline void
  228. wasm_exec_env_set_cur_frame(WASMExecEnv *exec_env,
  229. struct WASMInterpFrame *frame)
  230. {
  231. exec_env->cur_frame = frame;
  232. }
  233. /**
  234. * Get the current frame pointer.
  235. *
  236. * @param exec_env the current execution environment
  237. *
  238. * @return the current frame pointer
  239. */
  240. static inline struct WASMInterpFrame *
  241. wasm_exec_env_get_cur_frame(WASMExecEnv *exec_env)
  242. {
  243. return exec_env->cur_frame;
  244. }
  245. struct WASMModuleInstanceCommon *
  246. wasm_exec_env_get_module_inst(WASMExecEnv *exec_env);
  247. void
  248. wasm_exec_env_set_module_inst(
  249. WASMExecEnv *exec_env, struct WASMModuleInstanceCommon *const module_inst);
  250. void
  251. wasm_exec_env_set_thread_info(WASMExecEnv *exec_env);
  252. #if WASM_ENABLE_THREAD_MGR != 0
  253. void *
  254. wasm_exec_env_get_thread_arg(WASMExecEnv *exec_env);
  255. void
  256. wasm_exec_env_set_thread_arg(WASMExecEnv *exec_env, void *thread_arg);
  257. #endif
  258. #ifdef OS_ENABLE_HW_BOUND_CHECK
  259. void
  260. wasm_exec_env_push_jmpbuf(WASMExecEnv *exec_env, WASMJmpBuf *jmpbuf);
  261. WASMJmpBuf *
  262. wasm_exec_env_pop_jmpbuf(WASMExecEnv *exec_env);
  263. #endif
  264. #ifdef __cplusplus
  265. }
  266. #endif
  267. #endif /* end of _WASM_EXEC_ENV_H */