wasm_exec_env.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. #endif
  19. #ifdef OS_ENABLE_HW_BOUND_CHECK
  20. typedef struct WASMJmpBuf {
  21. struct WASMJmpBuf *prev;
  22. korp_jmpbuf jmpbuf;
  23. } WASMJmpBuf;
  24. #endif
  25. /* Execution environment */
  26. typedef struct WASMExecEnv {
  27. /* Next thread's exec env of a WASM module instance. */
  28. struct WASMExecEnv *next;
  29. /* Previous thread's exec env of a WASM module instance. */
  30. struct WASMExecEnv *prev;
  31. /* Note: field module_inst, argv_buf and native_stack_boundary
  32. are used by AOTed code, don't change the places of them */
  33. /* The WASM module instance of current thread */
  34. struct WASMModuleInstanceCommon *module_inst;
  35. #if WASM_ENABLE_AOT != 0
  36. uint32 *argv_buf;
  37. #endif
  38. /* The boundary of native stack. When runtime detects that native
  39. frame may overrun this boundary, it throws stack overflow
  40. exception. */
  41. uint8 *native_stack_boundary;
  42. #if WASM_ENABLE_THREAD_MGR != 0
  43. /* Used to terminate or suspend the interpreter
  44. bit 0: need terminate
  45. bit 1: need suspend
  46. bit 2: need to go into breakpoint
  47. bit 3: return from pthread_exit */
  48. union {
  49. uint32 flags;
  50. uintptr_t __padding__;
  51. } suspend_flags;
  52. /* thread return value */
  53. void *thread_ret_value;
  54. /* Must be provided by thread library */
  55. void* (*thread_start_routine)(void *);
  56. void *thread_arg;
  57. /* pointer to the cluster */
  58. WASMCluster *cluster;
  59. /* used to support debugger */
  60. korp_mutex wait_lock;
  61. korp_cond wait_cond;
  62. #endif
  63. /* Aux stack boundary */
  64. uint32 aux_stack_boundary;
  65. /* attachment for native function */
  66. void *attachment;
  67. void *user_data;
  68. /* Current interpreter frame of current thread */
  69. struct WASMInterpFrame *cur_frame;
  70. /* The native thread handle of current thread */
  71. korp_tid handle;
  72. #if WASM_ENABLE_INTERP != 0
  73. BlockAddr block_addr_cache[BLOCK_ADDR_CACHE_SIZE][BLOCK_ADDR_CONFLICT_SIZE];
  74. #endif
  75. #ifdef OS_ENABLE_HW_BOUND_CHECK
  76. WASMJmpBuf *jmpbuf_stack_top;
  77. #endif
  78. /* The WASM stack size */
  79. uint32 wasm_stack_size;
  80. /* The WASM stack of current thread */
  81. union {
  82. uint64 __make_it_8_byte_aligned_;
  83. struct {
  84. /* The top boundary of the stack. */
  85. uint8 *top_boundary;
  86. /* Top cell index which is free. */
  87. uint8 *top;
  88. /* The WASM stack. */
  89. uint8 bottom[1];
  90. } s;
  91. } wasm_stack;
  92. } WASMExecEnv;
  93. WASMExecEnv *
  94. wasm_exec_env_create_internal(struct WASMModuleInstanceCommon *module_inst,
  95. uint32 stack_size);
  96. void
  97. wasm_exec_env_destroy_internal(WASMExecEnv *exec_env);
  98. WASMExecEnv *
  99. wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
  100. uint32 stack_size);
  101. void
  102. wasm_exec_env_destroy(WASMExecEnv *exec_env);
  103. /**
  104. * Allocate a WASM frame from the WASM stack.
  105. *
  106. * @param exec_env the current execution environment
  107. * @param size size of the WASM frame, it must be a multiple of 4
  108. *
  109. * @return the WASM frame if there is enough space in the stack area
  110. * with a protection area, NULL otherwise
  111. */
  112. static inline void *
  113. wasm_exec_env_alloc_wasm_frame(WASMExecEnv *exec_env, unsigned size)
  114. {
  115. uint8 *addr = exec_env->wasm_stack.s.top;
  116. bh_assert(!(size & 3));
  117. /* The outs area size cannot be larger than the frame size, so
  118. multiplying by 2 is enough. */
  119. if (addr + size * 2 > exec_env->wasm_stack.s.top_boundary) {
  120. /* WASM stack overflow. */
  121. /* When throwing SOE, the preserved space must be enough. */
  122. /* bh_assert(!exec_env->throwing_soe);*/
  123. return NULL;
  124. }
  125. exec_env->wasm_stack.s.top += size;
  126. return addr;
  127. }
  128. static inline void
  129. wasm_exec_env_free_wasm_frame(WASMExecEnv *exec_env, void *prev_top)
  130. {
  131. bh_assert((uint8 *)prev_top >= exec_env->wasm_stack.s.bottom);
  132. exec_env->wasm_stack.s.top = (uint8 *)prev_top;
  133. }
  134. /**
  135. * Get the current WASM stack top pointer.
  136. *
  137. * @param exec_env the current execution environment
  138. *
  139. * @return the current WASM stack top pointer
  140. */
  141. static inline void*
  142. wasm_exec_env_wasm_stack_top(WASMExecEnv *exec_env)
  143. {
  144. return exec_env->wasm_stack.s.top;
  145. }
  146. /**
  147. * Set the current frame pointer.
  148. *
  149. * @param exec_env the current execution environment
  150. * @param frame the WASM frame to be set for the current exec env
  151. */
  152. static inline void
  153. wasm_exec_env_set_cur_frame(WASMExecEnv *exec_env,
  154. struct WASMInterpFrame *frame)
  155. {
  156. exec_env->cur_frame = frame;
  157. }
  158. /**
  159. * Get the current frame pointer.
  160. *
  161. * @param exec_env the current execution environment
  162. *
  163. * @return the current frame pointer
  164. */
  165. static inline struct WASMInterpFrame*
  166. wasm_exec_env_get_cur_frame(WASMExecEnv *exec_env)
  167. {
  168. return exec_env->cur_frame;
  169. }
  170. struct WASMModuleInstanceCommon *
  171. wasm_exec_env_get_module_inst(WASMExecEnv *exec_env);
  172. void
  173. wasm_exec_env_set_thread_info(WASMExecEnv *exec_env);
  174. #if WASM_ENABLE_THREAD_MGR != 0
  175. void *
  176. wasm_exec_env_get_thread_arg(WASMExecEnv *exec_env);
  177. void
  178. wasm_exec_env_set_thread_arg(WASMExecEnv *exec_env, void *thread_arg);
  179. #endif
  180. #ifdef OS_ENABLE_HW_BOUND_CHECK
  181. void
  182. wasm_exec_env_push_jmpbuf(WASMExecEnv *exec_env, WASMJmpBuf *jmpbuf);
  183. WASMJmpBuf *
  184. wasm_exec_env_pop_jmpbuf(WASMExecEnv *exec_env);
  185. #endif
  186. #ifdef __cplusplus
  187. }
  188. #endif
  189. #endif /* end of _WASM_EXEC_ENV_H */