wasm_exec_env.h 8.4 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. #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. /* Current interpreter/AOT frame of current thread */
  34. struct WASMInterpFrame *cur_frame;
  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. uintptr_t aux_stack_boundary;
  52. /* Auxiliary stack bottom */
  53. uintptr_t aux_stack_bottom;
  54. #if WASM_ENABLE_AOT != 0
  55. /* Native symbol list, reserved */
  56. void **native_symbol;
  57. #endif
  58. /*
  59. * The lowest stack pointer value observed.
  60. * Assumption: native stack grows to the lower address.
  61. */
  62. uint8 *native_stack_top_min;
  63. struct {
  64. /* The top boundary of the stack. */
  65. uint8 *top_boundary;
  66. /* The top to of the wasm stack which is free. */
  67. uint8 *top;
  68. /* The bottom of the wasm stack. */
  69. uint8 *bottom;
  70. } wasm_stack;
  71. #if WASM_ENABLE_INSTRUCTION_METERING != 0
  72. /* instructions to execute */
  73. int instructions_to_execute;
  74. #endif
  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. /* whether the aux stack is allocated */
  101. bool is_aux_stack_allocated;
  102. #endif
  103. #if WASM_ENABLE_GC != 0
  104. /* Current local object reference variable */
  105. struct WASMLocalObjectRef *cur_local_object_ref;
  106. #endif
  107. #if WASM_ENABLE_DEBUG_INTERP != 0
  108. WASMCurrentEnvStatus *current_status;
  109. #endif
  110. /* attachment for native function */
  111. void *attachment;
  112. void *user_data;
  113. /* The boundary of native stack set by host embedder. It is used
  114. if it is not NULL when calling wasm functions. */
  115. uint8 *user_native_stack_boundary;
  116. /* The native thread handle of current thread */
  117. korp_tid handle;
  118. #if WASM_ENABLE_INTERP != 0 && WASM_ENABLE_FAST_INTERP == 0
  119. BlockAddr block_addr_cache[BLOCK_ADDR_CACHE_SIZE][BLOCK_ADDR_CONFLICT_SIZE];
  120. #endif
  121. #ifdef OS_ENABLE_HW_BOUND_CHECK
  122. WASMJmpBuf *jmpbuf_stack_top;
  123. /* One guard page for the exception check */
  124. uint8 *exce_check_guard_page;
  125. #endif
  126. #if WASM_ENABLE_MEMORY_PROFILING != 0
  127. uint32 max_wasm_stack_used;
  128. #endif
  129. /* The WASM stack size */
  130. uint32 wasm_stack_size;
  131. /* The WASM stack of current thread */
  132. union {
  133. uint64 __make_it_8_byte_aligned_;
  134. /* The WASM stack. */
  135. uint8 bottom[1];
  136. } wasm_stack_u;
  137. } WASMExecEnv;
  138. #if WASM_ENABLE_MEMORY_PROFILING != 0
  139. #define RECORD_STACK_USAGE(e, p) \
  140. do { \
  141. if ((e)->native_stack_top_min > (p)) { \
  142. (e)->native_stack_top_min = (p); \
  143. } \
  144. } while (0)
  145. #else
  146. #define RECORD_STACK_USAGE(e, p) (void)0
  147. #endif
  148. WASMExecEnv *
  149. wasm_exec_env_create_internal(struct WASMModuleInstanceCommon *module_inst,
  150. uint32 stack_size);
  151. void
  152. wasm_exec_env_destroy_internal(WASMExecEnv *exec_env);
  153. WASMExecEnv *
  154. wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
  155. uint32 stack_size);
  156. void
  157. wasm_exec_env_destroy(WASMExecEnv *exec_env);
  158. static inline bool
  159. wasm_exec_env_is_aux_stack_managed_by_runtime(WASMExecEnv *exec_env)
  160. {
  161. return exec_env->aux_stack_boundary != 0 || exec_env->aux_stack_bottom != 0;
  162. }
  163. /**
  164. * Allocate a WASM frame from the WASM stack.
  165. *
  166. * @param exec_env the current execution environment
  167. * @param size size of the WASM frame, it must be a multiple of 4
  168. *
  169. * @return the WASM frame if there is enough space in the stack area
  170. * with a protection area, NULL otherwise
  171. */
  172. static inline void *
  173. wasm_exec_env_alloc_wasm_frame(WASMExecEnv *exec_env, unsigned size)
  174. {
  175. uint8 *addr = exec_env->wasm_stack.top;
  176. bh_assert(!(size & 3));
  177. /* For classic interpreter, the outs area doesn't contain the const cells,
  178. its size cannot be larger than the frame size, so here checking stack
  179. overflow with multiplying by 2 is enough. For fast interpreter, since
  180. the outs area contains const cells, its size may be larger than current
  181. frame size, we should check again before putting the function arguments
  182. into the outs area. */
  183. if (size * 2
  184. > (uint32)(uintptr_t)(exec_env->wasm_stack.top_boundary - addr)) {
  185. /* WASM stack overflow. */
  186. return NULL;
  187. }
  188. exec_env->wasm_stack.top += size;
  189. #if WASM_ENABLE_MEMORY_PROFILING != 0
  190. {
  191. uint32 wasm_stack_used =
  192. exec_env->wasm_stack.top - exec_env->wasm_stack.bottom;
  193. if (wasm_stack_used > exec_env->max_wasm_stack_used)
  194. exec_env->max_wasm_stack_used = wasm_stack_used;
  195. }
  196. #endif
  197. return addr;
  198. }
  199. static inline void
  200. wasm_exec_env_free_wasm_frame(WASMExecEnv *exec_env, void *prev_top)
  201. {
  202. bh_assert((uint8 *)prev_top >= exec_env->wasm_stack.bottom);
  203. exec_env->wasm_stack.top = (uint8 *)prev_top;
  204. }
  205. /**
  206. * Get the current WASM stack top pointer.
  207. *
  208. * @param exec_env the current execution environment
  209. *
  210. * @return the current WASM stack top pointer
  211. */
  212. static inline void *
  213. wasm_exec_env_wasm_stack_top(WASMExecEnv *exec_env)
  214. {
  215. return exec_env->wasm_stack.top;
  216. }
  217. /**
  218. * Set the current frame pointer.
  219. *
  220. * @param exec_env the current execution environment
  221. * @param frame the WASM frame to be set for the current exec env
  222. */
  223. static inline void
  224. wasm_exec_env_set_cur_frame(WASMExecEnv *exec_env,
  225. struct WASMInterpFrame *frame)
  226. {
  227. exec_env->cur_frame = frame;
  228. }
  229. /**
  230. * Get the current frame pointer.
  231. *
  232. * @param exec_env the current execution environment
  233. *
  234. * @return the current frame pointer
  235. */
  236. static inline struct WASMInterpFrame *
  237. wasm_exec_env_get_cur_frame(WASMExecEnv *exec_env)
  238. {
  239. return exec_env->cur_frame;
  240. }
  241. struct WASMModuleInstanceCommon *
  242. wasm_exec_env_get_module_inst(WASMExecEnv *exec_env);
  243. void
  244. wasm_exec_env_set_module_inst(
  245. WASMExecEnv *exec_env, struct WASMModuleInstanceCommon *const module_inst);
  246. void
  247. wasm_exec_env_restore_module_inst(
  248. WASMExecEnv *exec_env, struct WASMModuleInstanceCommon *const module_inst);
  249. void
  250. wasm_exec_env_set_thread_info(WASMExecEnv *exec_env);
  251. #if WASM_ENABLE_THREAD_MGR != 0
  252. void *
  253. wasm_exec_env_get_thread_arg(WASMExecEnv *exec_env);
  254. void
  255. wasm_exec_env_set_thread_arg(WASMExecEnv *exec_env, void *thread_arg);
  256. #endif
  257. #ifdef OS_ENABLE_HW_BOUND_CHECK
  258. void
  259. wasm_exec_env_push_jmpbuf(WASMExecEnv *exec_env, WASMJmpBuf *jmpbuf);
  260. WASMJmpBuf *
  261. wasm_exec_env_pop_jmpbuf(WASMExecEnv *exec_env);
  262. #endif
  263. #ifdef __cplusplus
  264. }
  265. #endif
  266. #endif /* end of _WASM_EXEC_ENV_H */