wasm_exec_env.c 5.1 KB

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