Merge main into dev/socket

This commit is contained in:
Wenyong Huang
2022-09-10 22:12:43 +08:00
74 changed files with 1478 additions and 646 deletions

View File

@ -107,9 +107,6 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
the actual main function. Directly calling main function
may cause exception thrown. */
if ((func = wasm_runtime_lookup_wasi_start_function(module_inst))) {
#if WASM_ENABLE_DEBUG_INTERP != 0
wasm_runtime_start_debug_instance(exec_env);
#endif
return wasm_runtime_call_wasm(exec_env, func, 0, NULL);
}
#endif /* end of WASM_ENABLE_LIBC_WASI */
@ -191,10 +188,6 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
(uint32)wasm_runtime_addr_native_to_app(module_inst, argv_offsets);
}
#if WASM_ENABLE_DEBUG_INTERP != 0
wasm_runtime_start_debug_instance(exec_env);
#endif
ret = wasm_runtime_call_wasm(exec_env, func, argc1, argv1);
if (ret && func_type->result_count > 0 && argc > 0 && argv)
/* copy the return value */
@ -240,113 +233,10 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
return (ret && !wasm_runtime_get_exception(module_inst)) ? true : false;
}
#if WASM_ENABLE_MULTI_MODULE != 0
static WASMModuleInstance *
get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
const char *sub_module_name)
{
WASMSubModInstNode *node =
bh_list_first_elem(parent_module_inst->sub_module_inst_list);
while (node && strcmp(node->module_name, sub_module_name)) {
node = bh_list_elem_next(node);
}
return node ? node->module_inst : NULL;
}
static bool
parse_function_name(char *orig_function_name, char **p_module_name,
char **p_function_name)
{
if (orig_function_name[0] != '$') {
*p_module_name = NULL;
*p_function_name = orig_function_name;
return true;
}
/**
* $module_name$function_name\0
* ===>
* module_name\0function_name\0
* ===>
* module_name
* function_name
*/
char *p1 = orig_function_name;
char *p2 = strchr(p1 + 1, '$');
if (!p2) {
LOG_DEBUG("can not parse the incoming function name");
return false;
}
*p_module_name = p1 + 1;
*p2 = '\0';
*p_function_name = p2 + 1;
return strlen(*p_module_name) && strlen(*p_function_name);
}
#endif
/**
* Implementation of wasm_application_execute_func()
*/
static bool
resolve_function(WASMModuleInstanceCommon *module_inst, const char *name,
WASMFunctionInstanceCommon **out_func,
WASMModuleInstanceCommon **out_module_inst)
{
WASMFunctionInstanceCommon *target_func = NULL;
WASMModuleInstanceCommon *target_inst = NULL;
#if WASM_ENABLE_MULTI_MODULE != 0
char *function_name = NULL;
char *orig_name = NULL;
char *sub_module_name = NULL;
uint32 length = (uint32)(strlen(name) + 1);
orig_name = runtime_malloc(sizeof(char) * length, NULL, NULL, 0);
if (!orig_name) {
goto LEAVE;
}
strncpy(orig_name, name, length);
if (!parse_function_name(orig_name, &sub_module_name, &function_name)) {
goto LEAVE;
}
LOG_DEBUG("%s -> %s and %s", name, sub_module_name, function_name);
if (sub_module_name) {
target_inst = (WASMModuleInstanceCommon *)get_sub_module_inst(
(WASMModuleInstance *)module_inst, sub_module_name);
if (!target_inst) {
LOG_DEBUG("can not find a sub module named %s", sub_module_name);
goto LEAVE;
}
}
else {
target_inst = module_inst;
}
#else
const char *function_name = name;
target_inst = module_inst;
#endif
target_func =
wasm_runtime_lookup_function(target_inst, function_name, NULL);
#if WASM_ENABLE_MULTI_MODULE != 0
LEAVE:
if (orig_name)
wasm_runtime_free(orig_name);
#endif
*out_func = target_func;
*out_module_inst = target_inst;
return target_func;
}
union ieee754_float {
float f;
@ -393,7 +283,6 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
int32 argc, char *argv[])
{
WASMFunctionInstanceCommon *target_func;
WASMModuleInstanceCommon *target_inst;
WASMType *type = NULL;
WASMExecEnv *exec_env = NULL;
uint32 argc1, *argv1 = NULL, cell_num = 0, j, k = 0;
@ -408,13 +297,14 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
bh_assert(argc >= 0);
LOG_DEBUG("call a function \"%s\" with %d arguments", name, argc);
if (!resolve_function(module_inst, name, &target_func, &target_inst)) {
if (!(target_func =
wasm_runtime_lookup_function(module_inst, name, NULL))) {
snprintf(buf, sizeof(buf), "lookup function %s failed", name);
wasm_runtime_set_exception(module_inst, buf);
goto fail;
}
module_type = target_inst->module_type;
module_type = module_inst->module_type;
type = wasm_runtime_get_function_type(target_func, module_type);
if (!type) {
@ -446,7 +336,7 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
#endif
total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
if ((!(argv1 = runtime_malloc((uint32)total_size, target_inst, NULL, 0)))) {
if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst, NULL, 0)))) {
goto fail;
}
@ -618,10 +508,6 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
goto fail;
}
#if WASM_ENABLE_DEBUG_INTERP != 0
wasm_runtime_start_debug_instance(exec_env);
#endif
if (!wasm_runtime_call_wasm(exec_env, target_func, argc1, argv1)) {
goto fail;
}

View File

@ -75,8 +75,17 @@ wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
void
wasm_runtime_memory_destroy()
{
if (memory_mode == MEMORY_MODE_POOL)
mem_allocator_destroy(pool_allocator);
if (memory_mode == MEMORY_MODE_POOL) {
#if BH_ENABLE_GC_VERIFY == 0
(void)mem_allocator_destroy(pool_allocator);
#else
int ret = mem_allocator_destroy(pool_allocator);
if (ret != 0) {
/* Memory leak detected */
exit(-1);
}
#endif
}
memory_mode = MEMORY_MODE_UNKNOWN;
}
@ -167,3 +176,12 @@ wasm_runtime_free(void *ptr)
{
wasm_runtime_free_internal(ptr);
}
bool
wasm_runtime_get_mem_alloc_info(mem_alloc_info_t *mem_alloc_info)
{
if (memory_mode == MEMORY_MODE_POOL) {
return mem_allocator_get_alloc_info(pool_allocator, mem_alloc_info);
}
return false;
}

View File

@ -53,6 +53,9 @@ get_lib_pthread_export_apis(NativeSymbol **p_lib_pthread_apis);
uint32
get_libc_emcc_export_apis(NativeSymbol **p_libc_emcc_apis);
uint32
get_lib_rats_export_apis(NativeSymbol **p_lib_rats_apis);
static bool
compare_type_with_signautre(uint8 type, const char signature)
{
@ -359,24 +362,24 @@ wasm_native_init()
#if WASM_ENABLE_LIBC_BUILTIN != 0
n_native_symbols = get_libc_builtin_export_apis(&native_symbols);
if (!wasm_native_register_natives("env", native_symbols, n_native_symbols))
return false;
goto fail;
#endif /* WASM_ENABLE_LIBC_BUILTIN */
#if WASM_ENABLE_SPEC_TEST
n_native_symbols = get_spectest_export_apis(&native_symbols);
if (!wasm_native_register_natives("spectest", native_symbols,
n_native_symbols))
return false;
goto fail;
#endif /* WASM_ENABLE_SPEC_TEST */
#if WASM_ENABLE_LIBC_WASI != 0
n_native_symbols = get_libc_wasi_export_apis(&native_symbols);
if (!wasm_native_register_natives("wasi_unstable", native_symbols,
n_native_symbols))
return false;
goto fail;
if (!wasm_native_register_natives("wasi_snapshot_preview1", native_symbols,
n_native_symbols))
return false;
goto fail;
#endif
#if WASM_ENABLE_BASE_LIB != 0
@ -384,7 +387,7 @@ wasm_native_init()
if (n_native_symbols > 0
&& !wasm_native_register_natives("env", native_symbols,
n_native_symbols))
return false;
goto fail;
#endif
#if WASM_ENABLE_APP_FRAMEWORK != 0
@ -392,18 +395,18 @@ wasm_native_init()
if (n_native_symbols > 0
&& !wasm_native_register_natives("env", native_symbols,
n_native_symbols))
return false;
goto fail;
#endif
#if WASM_ENABLE_LIB_PTHREAD != 0
if (!lib_pthread_init())
return false;
goto fail;
n_native_symbols = get_lib_pthread_export_apis(&native_symbols);
if (n_native_symbols > 0
&& !wasm_native_register_natives("env", native_symbols,
n_native_symbols))
return false;
goto fail;
#endif
#if WASM_ENABLE_LIBC_EMCC != 0
@ -411,10 +414,21 @@ wasm_native_init()
if (n_native_symbols > 0
&& !wasm_native_register_natives("env", native_symbols,
n_native_symbols))
return false;
goto fail;
#endif /* WASM_ENABLE_LIBC_EMCC */
#if WASM_ENABLE_LIB_RATS != 0
n_native_symbols = get_lib_rats_export_apis(&native_symbols);
if (n_native_symbols > 0
&& !wasm_native_register_natives("env", native_symbols,
n_native_symbols))
goto fail;
#endif /* WASM_ENABLE_LIB_RATS */
return true;
fail:
wasm_native_destroy();
return false;
}
void

View File

@ -341,10 +341,6 @@ wasm_runtime_init()
void
wasm_runtime_destroy()
{
#if WASM_ENABLE_FAST_JIT != 0
jit_compiler_destroy();
#endif
#if WASM_ENABLE_REF_TYPES != 0
wasm_externref_map_destroy();
#endif
@ -368,6 +364,14 @@ wasm_runtime_destroy()
os_mutex_destroy(&registered_module_list_lock);
#endif
#if WASM_ENABLE_FAST_JIT != 0
/* Destroy Fast-JIT compiler after destroying the modules
* loaded by multi-module feature, since the Fast JIT's
* code cache allocator may be used by these modules.
*/
jit_compiler_destroy();
#endif
#if WASM_ENABLE_SHARED_MEMORY
wasm_shared_memory_destroy();
#endif
@ -404,7 +408,6 @@ wasm_runtime_full_init(RuntimeInitArgs *init_args)
#if WASM_ENABLE_DEBUG_INTERP != 0
if (strlen(init_args->ip_addr))
if (!wasm_debug_engine_init(init_args->ip_addr,
init_args->platform_port,
init_args->instance_port)) {
wasm_runtime_destroy();
return false;
@ -504,7 +507,7 @@ wasm_runtime_is_xip_file(const uint8 *buf, uint32 size)
#if (WASM_ENABLE_THREAD_MGR != 0) && (WASM_ENABLE_DEBUG_INTERP != 0)
uint32
wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env, int32_t port)
{
WASMModuleInstanceCommon *module_inst =
wasm_runtime_get_module_inst(exec_env);
@ -522,12 +525,18 @@ wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
return cluster->debug_inst->control_thread->port;
}
if (wasm_debug_instance_create(cluster)) {
if (wasm_debug_instance_create(cluster, port)) {
return cluster->debug_inst->control_thread->port;
}
return 0;
}
uint32
wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
{
return wasm_runtime_start_debug_instance_with_port(exec_env, -1);
}
#endif
#if WASM_ENABLE_MULTI_MODULE != 0

View File

@ -565,6 +565,11 @@ wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
uint32 num_args, ...);
#if WASM_ENABLE_DEBUG_INTERP != 0
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN uint32
wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env,
int32_t port);
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN uint32
wasm_runtime_start_debug_instance(WASMExecEnv *exec_env);