wasm_exec_env.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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_INTERP != 0
  8. #include "../interpreter/wasm_runtime.h"
  9. #endif
  10. #if WASM_ENABLE_AOT != 0
  11. #include "../aot/aot_runtime.h"
  12. #endif
  13. #if WASM_ENABLE_AOT != 0
  14. #include "aot_runtime.h"
  15. #endif
  16. #if WASM_ENABLE_THREAD_MGR != 0
  17. #include "../libraries/thread-mgr/thread_manager.h"
  18. #endif
  19. WASMExecEnv *
  20. wasm_exec_env_create_internal(struct WASMModuleInstanceCommon *module_inst,
  21. uint32 stack_size)
  22. {
  23. uint64 total_size = offsetof(WASMExecEnv, wasm_stack.s.bottom)
  24. + (uint64)stack_size;
  25. WASMExecEnv *exec_env;
  26. if (total_size >= UINT32_MAX
  27. || !(exec_env = wasm_runtime_malloc((uint32)total_size)))
  28. return NULL;
  29. memset(exec_env, 0, (uint32)total_size);
  30. #if WASM_ENABLE_AOT != 0
  31. if (!(exec_env->argv_buf = wasm_runtime_malloc(sizeof(uint32) * 64))) {
  32. goto fail1;
  33. }
  34. #endif
  35. #if WASM_ENABLE_THREAD_MGR != 0
  36. if (os_mutex_init(&exec_env->wait_lock) != 0)
  37. goto fail2;
  38. if (os_cond_init(&exec_env->wait_cond) != 0)
  39. goto fail3;
  40. #endif
  41. exec_env->module_inst = module_inst;
  42. exec_env->wasm_stack_size = stack_size;
  43. exec_env->wasm_stack.s.top_boundary =
  44. exec_env->wasm_stack.s.bottom + stack_size;
  45. exec_env->wasm_stack.s.top = exec_env->wasm_stack.s.bottom;
  46. #if WASM_ENABLE_AOT != 0
  47. if (module_inst->module_type == Wasm_Module_AoT) {
  48. AOTModuleInstance *i = (AOTModuleInstance *)module_inst;
  49. AOTModule *m = (AOTModule *)i->aot_module.ptr;
  50. exec_env->native_symbol = m->native_symbol_list;
  51. }
  52. #endif
  53. #if WASM_ENABLE_MEMORY_TRACING != 0
  54. wasm_runtime_dump_exec_env_mem_consumption(exec_env);
  55. #endif
  56. return exec_env;
  57. #if WASM_ENABLE_THREAD_MGR != 0
  58. fail3:
  59. os_mutex_destroy(&exec_env->wait_lock);
  60. fail2:
  61. #endif
  62. #if WASM_ENABLE_AOT != 0
  63. wasm_runtime_free(exec_env->argv_buf);
  64. fail1:
  65. #endif
  66. wasm_runtime_free(exec_env);
  67. return NULL;
  68. }
  69. void
  70. wasm_exec_env_destroy_internal(WASMExecEnv *exec_env)
  71. {
  72. #if WASM_ENABLE_THREAD_MGR != 0
  73. os_mutex_destroy(&exec_env->wait_lock);
  74. os_cond_destroy(&exec_env->wait_cond);
  75. #endif
  76. #if WASM_ENABLE_AOT != 0
  77. wasm_runtime_free(exec_env->argv_buf);
  78. #endif
  79. wasm_runtime_free(exec_env);
  80. }
  81. WASMExecEnv *
  82. wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
  83. uint32 stack_size)
  84. {
  85. #if WASM_ENABLE_THREAD_MGR != 0
  86. WASMCluster *cluster;
  87. #endif
  88. WASMExecEnv *exec_env =
  89. wasm_exec_env_create_internal(module_inst, stack_size);
  90. if (!exec_env)
  91. return NULL;
  92. /* Set the aux_stack_boundary and aux_stack_bottom */
  93. #if WASM_ENABLE_INTERP != 0
  94. if (module_inst->module_type == Wasm_Module_Bytecode) {
  95. WASMModule *module = ((WASMModuleInstance *)module_inst)->module;
  96. exec_env->aux_stack_bottom.bottom = module->aux_stack_bottom;
  97. exec_env->aux_stack_boundary.boundary = module->aux_stack_bottom
  98. - module->aux_stack_size;
  99. }
  100. #endif
  101. #if WASM_ENABLE_AOT != 0
  102. if (module_inst->module_type == Wasm_Module_AoT) {
  103. AOTModule *module =
  104. (AOTModule *)(((AOTModuleInstance *)module_inst)->aot_module.ptr);
  105. exec_env->aux_stack_bottom.bottom = module->aux_stack_bottom;
  106. exec_env->aux_stack_boundary.boundary = module->aux_stack_bottom
  107. - module->aux_stack_size;
  108. }
  109. #endif
  110. #if WASM_ENABLE_THREAD_MGR != 0
  111. /* Create a new cluster for this exec_env */
  112. if (!(cluster = wasm_cluster_create(exec_env))) {
  113. wasm_exec_env_destroy_internal(exec_env);
  114. return NULL;
  115. }
  116. #endif
  117. return exec_env;
  118. }
  119. void
  120. wasm_exec_env_destroy(WASMExecEnv *exec_env)
  121. {
  122. #if WASM_ENABLE_THREAD_MGR != 0
  123. /* Terminate all sub-threads */
  124. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  125. if (cluster) {
  126. wasm_cluster_terminate_all_except_self(cluster, exec_env);
  127. wasm_cluster_del_exec_env(cluster, exec_env);
  128. }
  129. #endif
  130. wasm_exec_env_destroy_internal(exec_env);
  131. }
  132. WASMModuleInstanceCommon *
  133. wasm_exec_env_get_module_inst(WASMExecEnv *exec_env)
  134. {
  135. return exec_env->module_inst;
  136. }
  137. void
  138. wasm_exec_env_set_thread_info(WASMExecEnv *exec_env)
  139. {
  140. exec_env->handle = os_self_thread();
  141. exec_env->native_stack_boundary = os_thread_get_stack_boundary()
  142. + RESERVED_BYTES_TO_NATIVE_STACK_BOUNDARY;
  143. }
  144. #if WASM_ENABLE_THREAD_MGR != 0
  145. void *
  146. wasm_exec_env_get_thread_arg(WASMExecEnv *exec_env)
  147. {
  148. return exec_env->thread_arg;
  149. }
  150. void
  151. wasm_exec_env_set_thread_arg(WASMExecEnv *exec_env, void *thread_arg)
  152. {
  153. exec_env->thread_arg = thread_arg;
  154. }
  155. #endif
  156. #ifdef OS_ENABLE_HW_BOUND_CHECK
  157. void
  158. wasm_exec_env_push_jmpbuf(WASMExecEnv *exec_env, WASMJmpBuf *jmpbuf)
  159. {
  160. jmpbuf->prev = exec_env->jmpbuf_stack_top;
  161. exec_env->jmpbuf_stack_top = jmpbuf;
  162. }
  163. WASMJmpBuf *
  164. wasm_exec_env_pop_jmpbuf(WASMExecEnv *exec_env)
  165. {
  166. WASMJmpBuf *stack_top = exec_env->jmpbuf_stack_top;
  167. if (stack_top) {
  168. exec_env->jmpbuf_stack_top = stack_top->prev;
  169. return stack_top;
  170. }
  171. return NULL;
  172. }
  173. #endif