wasm_exec_env.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_exec_env.h"
  6. #include "wasm_runtime_common.h"
  7. #if WASM_ENABLE_GC != 0
  8. #include "mem_alloc.h"
  9. #endif
  10. #if WASM_ENABLE_INTERP != 0
  11. #include "../interpreter/wasm_runtime.h"
  12. #endif
  13. #if WASM_ENABLE_AOT != 0
  14. #include "../aot/aot_runtime.h"
  15. #endif
  16. #if WASM_ENABLE_AOT != 0
  17. #include "aot_runtime.h"
  18. #endif
  19. #if WASM_ENABLE_THREAD_MGR != 0
  20. #include "../libraries/thread-mgr/thread_manager.h"
  21. #if WASM_ENABLE_DEBUG_INTERP != 0
  22. #include "../libraries/debug-engine/debug_engine.h"
  23. #endif
  24. #endif
  25. WASMExecEnv *
  26. wasm_exec_env_create_internal(struct WASMModuleInstanceCommon *module_inst,
  27. uint32 stack_size)
  28. {
  29. uint64 total_size =
  30. offsetof(WASMExecEnv, wasm_stack_u.bottom) + (uint64)stack_size;
  31. WASMExecEnv *exec_env;
  32. if (total_size >= UINT32_MAX
  33. || !(exec_env = wasm_runtime_malloc((uint32)total_size)))
  34. return NULL;
  35. memset(exec_env, 0, (uint32)total_size);
  36. #if WASM_ENABLE_AOT != 0
  37. if (!(exec_env->argv_buf = wasm_runtime_malloc(sizeof(uint32) * 64))) {
  38. goto fail1;
  39. }
  40. #endif
  41. #if WASM_ENABLE_THREAD_MGR != 0
  42. if (os_mutex_init(&exec_env->wait_lock) != 0)
  43. goto fail2;
  44. if (os_cond_init(&exec_env->wait_cond) != 0)
  45. goto fail3;
  46. #if WASM_ENABLE_DEBUG_INTERP != 0
  47. if (!(exec_env->current_status = wasm_cluster_create_exenv_status()))
  48. goto fail4;
  49. #endif
  50. #endif
  51. #ifdef OS_ENABLE_HW_BOUND_CHECK
  52. if (!(exec_env->exce_check_guard_page =
  53. os_mmap(NULL, os_getpagesize(), MMAP_PROT_NONE, MMAP_MAP_NONE,
  54. os_get_invalid_handle())))
  55. goto fail5;
  56. #endif
  57. exec_env->module_inst = module_inst;
  58. exec_env->wasm_stack_size = stack_size;
  59. exec_env->wasm_stack.bottom = exec_env->wasm_stack_u.bottom;
  60. exec_env->wasm_stack.top_boundary =
  61. exec_env->wasm_stack.bottom + stack_size;
  62. exec_env->wasm_stack.top = exec_env->wasm_stack.bottom;
  63. #if WASM_ENABLE_AOT != 0
  64. if (module_inst->module_type == Wasm_Module_AoT) {
  65. AOTModuleInstance *i = (AOTModuleInstance *)module_inst;
  66. AOTModule *m = (AOTModule *)i->module;
  67. exec_env->native_symbol = m->native_symbol_list;
  68. }
  69. #endif
  70. #if WASM_ENABLE_MEMORY_TRACING != 0
  71. wasm_runtime_dump_exec_env_mem_consumption(exec_env);
  72. #endif
  73. #if WASM_ENABLE_INSTRUCTION_METERING != 0
  74. exec_env->instructions_to_execute = -1;
  75. #endif
  76. return exec_env;
  77. #ifdef OS_ENABLE_HW_BOUND_CHECK
  78. fail5:
  79. #if WASM_ENABLE_THREAD_MGR != 0 && WASM_ENABLE_DEBUG_INTERP != 0
  80. wasm_cluster_destroy_exenv_status(exec_env->current_status);
  81. #endif
  82. #endif
  83. #if WASM_ENABLE_THREAD_MGR != 0
  84. #if WASM_ENABLE_DEBUG_INTERP != 0
  85. fail4:
  86. os_cond_destroy(&exec_env->wait_cond);
  87. #endif
  88. fail3:
  89. os_mutex_destroy(&exec_env->wait_lock);
  90. fail2:
  91. #endif
  92. #if WASM_ENABLE_AOT != 0
  93. wasm_runtime_free(exec_env->argv_buf);
  94. fail1:
  95. #endif
  96. wasm_runtime_free(exec_env);
  97. return NULL;
  98. }
  99. void
  100. wasm_exec_env_destroy_internal(WASMExecEnv *exec_env)
  101. {
  102. #ifdef OS_ENABLE_HW_BOUND_CHECK
  103. os_munmap(exec_env->exce_check_guard_page, os_getpagesize());
  104. #endif
  105. #if WASM_ENABLE_THREAD_MGR != 0
  106. os_mutex_destroy(&exec_env->wait_lock);
  107. os_cond_destroy(&exec_env->wait_cond);
  108. #if WASM_ENABLE_DEBUG_INTERP != 0
  109. wasm_cluster_destroy_exenv_status(exec_env->current_status);
  110. #endif
  111. #endif
  112. #if WASM_ENABLE_AOT != 0
  113. wasm_runtime_free(exec_env->argv_buf);
  114. #endif
  115. wasm_runtime_free(exec_env);
  116. }
  117. WASMExecEnv *
  118. wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
  119. uint32 stack_size)
  120. {
  121. #if WASM_ENABLE_THREAD_MGR != 0
  122. WASMCluster *cluster;
  123. #endif
  124. WASMExecEnv *exec_env =
  125. wasm_exec_env_create_internal(module_inst, stack_size);
  126. #if WASM_ENABLE_GC != 0
  127. void *gc_heap_handle = NULL;
  128. #endif
  129. if (!exec_env)
  130. return NULL;
  131. #if WASM_ENABLE_INTERP != 0
  132. /* Set the aux_stack_boundary and aux_stack_bottom */
  133. if (module_inst->module_type == Wasm_Module_Bytecode) {
  134. WASMModule *module = ((WASMModuleInstance *)module_inst)->module;
  135. exec_env->aux_stack_bottom = (uintptr_t)module->aux_stack_bottom;
  136. exec_env->aux_stack_boundary =
  137. (uintptr_t)module->aux_stack_bottom - module->aux_stack_size;
  138. #if WASM_ENABLE_GC != 0
  139. gc_heap_handle =
  140. ((WASMModuleInstance *)module_inst)->e->common.gc_heap_pool;
  141. #endif
  142. }
  143. #endif
  144. #if WASM_ENABLE_AOT != 0
  145. /* Set the aux_stack_boundary and aux_stack_bottom */
  146. if (module_inst->module_type == Wasm_Module_AoT) {
  147. AOTModule *module =
  148. (AOTModule *)((AOTModuleInstance *)module_inst)->module;
  149. exec_env->aux_stack_bottom = (uintptr_t)module->aux_stack_bottom;
  150. exec_env->aux_stack_boundary =
  151. (uintptr_t)module->aux_stack_bottom - module->aux_stack_size;
  152. #if WASM_ENABLE_GC != 0
  153. gc_heap_handle =
  154. ((AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e)
  155. ->common.gc_heap_handle;
  156. #endif
  157. }
  158. #endif
  159. #if WASM_ENABLE_THREAD_MGR != 0
  160. /* Create a new cluster for this exec_env */
  161. if (!(cluster = wasm_cluster_create(exec_env))) {
  162. wasm_exec_env_destroy_internal(exec_env);
  163. return NULL;
  164. }
  165. #if WASM_ENABLE_GC != 0
  166. mem_allocator_enable_gc_reclaim(gc_heap_handle, cluster);
  167. #endif
  168. #else
  169. #if WASM_ENABLE_GC != 0
  170. mem_allocator_enable_gc_reclaim(gc_heap_handle, exec_env);
  171. #endif
  172. #endif /* end of WASM_ENABLE_THREAD_MGR */
  173. return exec_env;
  174. }
  175. void
  176. wasm_exec_env_destroy(WASMExecEnv *exec_env)
  177. {
  178. #if WASM_ENABLE_THREAD_MGR != 0
  179. /* Wait for all sub-threads */
  180. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  181. if (cluster) {
  182. wasm_cluster_wait_for_all_except_self(cluster, exec_env);
  183. #if WASM_ENABLE_DEBUG_INTERP != 0
  184. /* Must fire exit event after other threads exits, otherwise
  185. the stopped thread will be overridden by other threads */
  186. wasm_cluster_thread_exited(exec_env);
  187. #endif
  188. /* We have waited for other threads, this is the only alive thread, so
  189. * we don't acquire cluster->lock because the cluster will be destroyed
  190. * inside this function */
  191. wasm_cluster_del_exec_env(cluster, exec_env);
  192. }
  193. #endif /* end of WASM_ENABLE_THREAD_MGR */
  194. wasm_exec_env_destroy_internal(exec_env);
  195. }
  196. WASMModuleInstanceCommon *
  197. wasm_exec_env_get_module_inst(WASMExecEnv *exec_env)
  198. {
  199. return exec_env->module_inst;
  200. }
  201. void
  202. wasm_exec_env_set_module_inst(WASMExecEnv *exec_env,
  203. WASMModuleInstanceCommon *const module_inst)
  204. {
  205. #if WASM_ENABLE_THREAD_MGR != 0
  206. wasm_cluster_traverse_lock(exec_env);
  207. #endif
  208. exec_env->module_inst = module_inst;
  209. #if WASM_ENABLE_THREAD_MGR != 0
  210. wasm_cluster_traverse_unlock(exec_env);
  211. #endif
  212. }
  213. void
  214. wasm_exec_env_restore_module_inst(
  215. WASMExecEnv *exec_env, WASMModuleInstanceCommon *const module_inst_common)
  216. {
  217. WASMModuleInstanceCommon *old_module_inst_common = exec_env->module_inst;
  218. WASMModuleInstance *old_module_inst =
  219. (WASMModuleInstance *)old_module_inst_common;
  220. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_common;
  221. char cur_exception[EXCEPTION_BUF_LEN];
  222. #if WASM_ENABLE_THREAD_MGR != 0
  223. wasm_cluster_traverse_lock(exec_env);
  224. #endif
  225. exec_env->module_inst = module_inst_common;
  226. /*
  227. * propagate an exception if any.
  228. */
  229. exception_lock(old_module_inst);
  230. if (old_module_inst->cur_exception[0] != '\0') {
  231. bh_memcpy_s(cur_exception, sizeof(cur_exception),
  232. old_module_inst->cur_exception,
  233. sizeof(old_module_inst->cur_exception));
  234. }
  235. else {
  236. cur_exception[0] = '\0';
  237. }
  238. exception_unlock(old_module_inst);
  239. #if WASM_ENABLE_THREAD_MGR != 0
  240. wasm_cluster_traverse_unlock(exec_env);
  241. #endif
  242. if (cur_exception[0] != '\0') {
  243. exception_lock(module_inst);
  244. bh_memcpy_s(module_inst->cur_exception,
  245. sizeof(module_inst->cur_exception), cur_exception,
  246. sizeof(cur_exception));
  247. exception_unlock(module_inst);
  248. }
  249. }
  250. void
  251. wasm_exec_env_set_thread_info(WASMExecEnv *exec_env)
  252. {
  253. #if WASM_ENABLE_THREAD_MGR != 0
  254. os_mutex_lock(&exec_env->wait_lock);
  255. #endif
  256. exec_env->handle = os_self_thread();
  257. if (exec_env->user_native_stack_boundary)
  258. /* WASM_STACK_GUARD_SIZE isn't added for flexibility to developer,
  259. he must ensure that enough guard bytes are kept. */
  260. exec_env->native_stack_boundary = exec_env->user_native_stack_boundary;
  261. else {
  262. uint8 *stack_boundary = os_thread_get_stack_boundary();
  263. exec_env->native_stack_boundary =
  264. stack_boundary ? stack_boundary + WASM_STACK_GUARD_SIZE : NULL;
  265. }
  266. exec_env->native_stack_top_min = (void *)UINTPTR_MAX;
  267. #if WASM_ENABLE_THREAD_MGR != 0
  268. os_mutex_unlock(&exec_env->wait_lock);
  269. #endif
  270. }
  271. #if WASM_ENABLE_THREAD_MGR != 0
  272. void *
  273. wasm_exec_env_get_thread_arg(WASMExecEnv *exec_env)
  274. {
  275. return exec_env->thread_arg;
  276. }
  277. void
  278. wasm_exec_env_set_thread_arg(WASMExecEnv *exec_env, void *thread_arg)
  279. {
  280. exec_env->thread_arg = thread_arg;
  281. }
  282. #endif
  283. #ifdef OS_ENABLE_HW_BOUND_CHECK
  284. void
  285. wasm_exec_env_push_jmpbuf(WASMExecEnv *exec_env, WASMJmpBuf *jmpbuf)
  286. {
  287. jmpbuf->prev = exec_env->jmpbuf_stack_top;
  288. exec_env->jmpbuf_stack_top = jmpbuf;
  289. }
  290. WASMJmpBuf *
  291. wasm_exec_env_pop_jmpbuf(WASMExecEnv *exec_env)
  292. {
  293. WASMJmpBuf *stack_top = exec_env->jmpbuf_stack_top;
  294. if (stack_top) {
  295. exec_env->jmpbuf_stack_top = stack_top->prev;
  296. return stack_top;
  297. }
  298. return NULL;
  299. }
  300. #endif