Re-org thread env init/destroy for HW bound check (#631)

And fix cmake_minimum_required() deprecation warning since envoy is using cmake 3.16 or higher version.
This commit is contained in:
Wenyong Huang
2021-05-14 11:13:12 +08:00
committed by GitHub
parent 17a2167485
commit a14a4487bb
16 changed files with 205 additions and 126 deletions

View File

@ -1254,21 +1254,26 @@ bool
aot_signal_init()
{
#ifndef BH_PLATFORM_WINDOWS
return os_signal_init(aot_signal_handler) == 0 ? true : false;
return os_thread_signal_init(aot_signal_handler) == 0 ? true : false;
#else
return AddVectoredExceptionHandler(1, aot_exception_handler)
? true : false;
if (os_thread_signal_init() != 0)
return false;
if (!AddVectoredExceptionHandler(1, aot_exception_handler)) {
os_thread_signal_destroy();
return false;
}
#endif
return true;
}
void
aot_signal_destroy()
{
#ifndef BH_PLATFORM_WINDOWS
os_signal_destroy();
#else
#ifdef BH_PLATFORM_WINDOWS
RemoveVectoredExceptionHandler(aot_exception_handler);
#endif
os_thread_signal_destroy();
}
static bool
@ -1302,7 +1307,10 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr,
return false;
}
os_thread_init_stack_guard_pages();
if (!os_thread_signal_inited()) {
aot_set_exception(module_inst, "thread signal env not inited");
return false;
}
wasm_exec_env_push_jmpbuf(exec_env, &jmpbuf_node);
@ -1486,11 +1494,11 @@ aot_create_exec_env_and_call_function(AOTModuleInstance *module_inst,
WASMExecEnv *exec_env = NULL, *existing_exec_env = NULL;
bool ret;
#if WASM_ENABLE_THREAD_MGR != 0
#if defined(OS_ENABLE_HW_BOUND_CHECK)
existing_exec_env = exec_env = aot_exec_env;
#elif WASM_ENABLE_THREAD_MGR != 0
existing_exec_env = exec_env = wasm_clusters_search_exec_env(
(WASMModuleInstanceCommon*)module_inst);
#elif defined(OS_ENABLE_HW_BOUND_CHECK)
existing_exec_env = exec_env = aot_exec_env;
#endif
if (!existing_exec_env) {

View File

@ -363,7 +363,13 @@ wasm_store_new(wasm_engine_t *engine)
return NULL;
}
if (!wasm_runtime_init_thread_env()) {
LOG_ERROR("init thread environment failed");
return NULL;
}
if (!(store = malloc_internal(sizeof(wasm_store_t)))) {
wasm_runtime_destroy_thread_env();
return NULL;
}
@ -412,6 +418,8 @@ wasm_store_delete(wasm_store_t *store)
DEINIT_VEC(store->modules, wasm_module_vec_delete);
DEINIT_VEC(store->instances, wasm_instance_vec_delete);
wasm_runtime_free(store);
wasm_runtime_destroy_thread_env();
}
/* Type Representations */
@ -1376,7 +1384,7 @@ module_to_module_ext(wasm_module_t *module)
wasm_module_t *
wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary)
{
char error[128] = { 0 };
char error_buf[128] = { 0 };
wasm_module_ex_t *module_ex = NULL;
bh_assert(singleton_engine);
@ -1393,11 +1401,12 @@ wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary)
INIT_VEC(module_ex->binary, wasm_byte_vec_new, binary->size, binary->data);
module_ex->module_comm_rt = wasm_runtime_load(
(uint8 *)module_ex->binary->data, (uint32)module_ex->binary->size, error,
(uint32)sizeof(error));
module_ex->module_comm_rt =
wasm_runtime_load((uint8 *)module_ex->binary->data,
(uint32)module_ex->binary->size,
error_buf, (uint32)sizeof(error_buf));
if (!(module_ex->module_comm_rt)) {
LOG_ERROR(error);
LOG_ERROR(error_buf);
goto failed;
}
@ -1418,6 +1427,7 @@ static void
wasm_module_delete_internal(wasm_module_t *module)
{
wasm_module_ex_t *module_ex;
if (!module) {
return;
}
@ -1436,7 +1446,7 @@ wasm_module_delete_internal(wasm_module_t *module)
void
wasm_module_delete(wasm_module_t *module)
{
/* will release module when releasing the store */
/* the module will be released when releasing the store */
}
void
@ -3589,7 +3599,7 @@ wasm_instance_new(wasm_store_t *store,
const wasm_extern_t *const imports[],
own wasm_trap_t **traps)
{
char error[128] = { 0 };
char error_buf[128] = { 0 };
const uint32 stack_size = 16 * 1024;
const uint32 heap_size = 16 * 1024;
uint32 import_count = 0;
@ -3650,10 +3660,11 @@ wasm_instance_new(wasm_store_t *store,
#endif
}
instance->inst_comm_rt = wasm_runtime_instantiate(
*module, stack_size, heap_size, error, sizeof(error));
instance->inst_comm_rt =
wasm_runtime_instantiate(*module, stack_size, heap_size,
error_buf, sizeof(error_buf));
if (!instance->inst_comm_rt) {
LOG_ERROR(error);
LOG_ERROR(error_buf);
goto failed;
}

View File

@ -819,6 +819,27 @@ wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env)
wasm_exec_env_destroy(exec_env);
}
bool
wasm_runtime_init_thread_env()
{
#if WASM_ENABLE_AOT != 0
#ifdef OS_ENABLE_HW_BOUND_CHECK
return aot_signal_init();
#endif
#endif
return true;
}
void
wasm_runtime_destroy_thread_env()
{
#if WASM_ENABLE_AOT != 0
#ifdef OS_ENABLE_HW_BOUND_CHECK
return aot_signal_destroy();
#endif
#endif
}
#if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
void
wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module)

View File

@ -392,6 +392,27 @@ wasm_runtime_create_exec_env(wasm_module_inst_t module_inst,
WASM_RUNTIME_API_EXTERN void
wasm_runtime_destroy_exec_env(wasm_exec_env_t exec_env);
/**
* Initialize thread environment.
* Note:
* If developer creates a child thread by himself to call the
* the wasm function in that thread, he should call this API
* firstly before calling the wasm function and then call
* wasm_runtime_destroy_thread_env() after calling the wasm
* function. If the thread is created from the runtime API,
* it is unnecessary to call these two APIs.
*
* @return true if success, false otherwise
*/
WASM_RUNTIME_API_EXTERN bool
wasm_runtime_init_thread_env();
/**
* Destroy thread environment
*/
WASM_RUNTIME_API_EXTERN void
wasm_runtime_destroy_thread_env();
/**
* Get WASM module instance from execution environment
*