wasm_exec_env.h 8.2 KB

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