wasm_exec_env.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #if WASM_ENABLE_THREAD_MGR != 0
  63. os_mutex_destroy(&exec_env->wait_lock);
  64. os_cond_destroy(&exec_env->wait_cond);
  65. #endif
  66. #if WASM_ENABLE_AOT != 0
  67. wasm_runtime_free(exec_env->argv_buf);
  68. #endif
  69. wasm_runtime_free(exec_env);
  70. }
  71. WASMExecEnv *
  72. wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
  73. uint32 stack_size)
  74. {
  75. #if WASM_ENABLE_THREAD_MGR != 0
  76. WASMCluster *cluster;
  77. #endif
  78. WASMExecEnv *exec_env =
  79. wasm_exec_env_create_internal(module_inst, stack_size);
  80. if (!exec_env)
  81. return NULL;
  82. /* Set the aux_stack_boundary and aux_stack_bottom */
  83. #if WASM_ENABLE_INTERP != 0
  84. if (module_inst->module_type == Wasm_Module_Bytecode) {
  85. WASMModule *module = ((WASMModuleInstance *)module_inst)->module;
  86. exec_env->aux_stack_bottom.bottom = module->aux_stack_bottom;
  87. exec_env->aux_stack_boundary.boundary = module->aux_stack_bottom
  88. - module->aux_stack_size;
  89. }
  90. #endif
  91. #if WASM_ENABLE_AOT != 0
  92. if (module_inst->module_type == Wasm_Module_AoT) {
  93. AOTModule *module =
  94. (AOTModule *)(((AOTModuleInstance *)module_inst)->aot_module.ptr);
  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_THREAD_MGR != 0
  101. /* Create a new cluster for this exec_env */
  102. if (!(cluster = wasm_cluster_create(exec_env))) {
  103. wasm_exec_env_destroy_internal(exec_env);
  104. return NULL;
  105. }
  106. #endif
  107. return exec_env;
  108. }
  109. void
  110. wasm_exec_env_destroy(WASMExecEnv *exec_env)
  111. {
  112. #if WASM_ENABLE_THREAD_MGR != 0
  113. /* Terminate all sub-threads */
  114. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  115. if (cluster) {
  116. wasm_cluster_terminate_all_except_self(cluster, exec_env);
  117. wasm_cluster_del_exec_env(cluster, exec_env);
  118. }
  119. #endif
  120. wasm_exec_env_destroy_internal(exec_env);
  121. }
  122. WASMModuleInstanceCommon *
  123. wasm_exec_env_get_module_inst(WASMExecEnv *exec_env)
  124. {
  125. return exec_env->module_inst;
  126. }
  127. void
  128. wasm_exec_env_set_thread_info(WASMExecEnv *exec_env)
  129. {
  130. exec_env->handle = os_self_thread();
  131. exec_env->native_stack_boundary = os_thread_get_stack_boundary()
  132. + RESERVED_BYTES_TO_NATIVE_STACK_BOUNDARY;
  133. }
  134. #if WASM_ENABLE_THREAD_MGR != 0
  135. void *
  136. wasm_exec_env_get_thread_arg(WASMExecEnv *exec_env)
  137. {
  138. return exec_env->thread_arg;
  139. }
  140. void
  141. wasm_exec_env_set_thread_arg(WASMExecEnv *exec_env, void *thread_arg)
  142. {
  143. exec_env->thread_arg = thread_arg;
  144. }
  145. #endif
  146. #ifdef OS_ENABLE_HW_BOUND_CHECK
  147. void
  148. wasm_exec_env_push_jmpbuf(WASMExecEnv *exec_env, WASMJmpBuf *jmpbuf)
  149. {
  150. jmpbuf->prev = exec_env->jmpbuf_stack_top;
  151. exec_env->jmpbuf_stack_top = jmpbuf;
  152. }
  153. WASMJmpBuf *
  154. wasm_exec_env_pop_jmpbuf(WASMExecEnv *exec_env)
  155. {
  156. WASMJmpBuf *stack_top = exec_env->jmpbuf_stack_top;
  157. if (stack_top) {
  158. exec_env->jmpbuf_stack_top = stack_top->prev;
  159. return stack_top;
  160. }
  161. return NULL;
  162. }
  163. #endif