Skip to content

Commit

Permalink
Re-org thread env init/destroy for HW bound check (bytecodealliance#631)
Browse files Browse the repository at this point in the history
And fix cmake_minimum_required() deprecation warning since envoy is using cmake 3.16 or higher version.
  • Loading branch information
wenyongh authored May 14, 2021
1 parent 17a2167 commit a14a448
Show file tree
Hide file tree
Showing 16 changed files with 204 additions and 125 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

cmake_minimum_required (VERSION 2.8)
cmake_minimum_required (VERSION 2.8...3.16)

project (iwasm)
# set (CMAKE_VERBOSE_MAKEFILE 1)
Expand Down
28 changes: 18 additions & 10 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down
31 changes: 21 additions & 10 deletions core/iwasm/common/wasm_c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand All @@ -1418,6 +1427,7 @@ static void
wasm_module_delete_internal(wasm_module_t *module)
{
wasm_module_ex_t *module_ex;

if (!module) {
return;
}
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
21 changes: 21 additions & 0 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions core/iwasm/include/wasm_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
3 changes: 0 additions & 3 deletions core/shared/platform/android/platform_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ bh_platform_init()
void
bh_platform_destroy()
{
#ifdef OS_ENABLE_HW_BOUND_CHECK
os_thread_destroy_stack_guard_pages();
#endif
}

int os_printf(const char *fmt, ...)
Expand Down
10 changes: 4 additions & 6 deletions core/shared/platform/android/platform_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,13 @@ typedef jmp_buf korp_jmpbuf;

#define os_getpagesize getpagesize

bool os_thread_init_stack_guard_pages();

void os_thread_destroy_stack_guard_pages();

typedef void (*os_signal_handler)(void *sig_addr);

int os_signal_init(os_signal_handler handler);
int os_thread_signal_init(os_signal_handler handler);

void os_thread_signal_destroy();

void os_signal_destroy();
bool os_thread_signal_inited();

void os_signal_unmask();

Expand Down
Loading

0 comments on commit a14a448

Please sign in to comment.