mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-07-11 15:03:33 +00:00
Compare commits
2 Commits
945951670d
...
8ebfb0716d
Author | SHA1 | Date | |
---|---|---|---|
![]() |
8ebfb0716d | ||
![]() |
b8e57c6ec3 |
|
@ -1625,13 +1625,24 @@ wasm_runtime_instantiate_internal(WASMModuleCommon *module,
|
||||||
WASMModuleInstanceCommon *parent,
|
WASMModuleInstanceCommon *parent,
|
||||||
WASMExecEnv *exec_env_main, uint32 stack_size,
|
WASMExecEnv *exec_env_main, uint32 stack_size,
|
||||||
uint32 heap_size, uint32 max_memory_pages,
|
uint32 heap_size, uint32 max_memory_pages,
|
||||||
char *error_buf, uint32 error_buf_size)
|
bool run_start_function, char *error_buf,
|
||||||
|
uint32 error_buf_size)
|
||||||
{
|
{
|
||||||
#if WASM_ENABLE_INTERP != 0
|
#if WASM_ENABLE_INTERP != 0
|
||||||
if (module->module_type == Wasm_Module_Bytecode)
|
if (module->module_type == Wasm_Module_Bytecode) {
|
||||||
|
if (run_start_function) {
|
||||||
return (WASMModuleInstanceCommon *)wasm_instantiate(
|
return (WASMModuleInstanceCommon *)wasm_instantiate(
|
||||||
(WASMModule *)module, (WASMModuleInstance *)parent, exec_env_main,
|
(WASMModule *)module, (WASMModuleInstance *)parent,
|
||||||
stack_size, heap_size, max_memory_pages, error_buf, error_buf_size);
|
exec_env_main, stack_size, heap_size, max_memory_pages,
|
||||||
|
error_buf, error_buf_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (WASMModuleInstanceCommon *)
|
||||||
|
wasm_instantiate_without_start_function(
|
||||||
|
(WASMModule *)module, (WASMModuleInstance *)parent,
|
||||||
|
exec_env_main, stack_size, heap_size, max_memory_pages,
|
||||||
|
error_buf, error_buf_size);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#if WASM_ENABLE_AOT != 0
|
#if WASM_ENABLE_AOT != 0
|
||||||
if (module->module_type == Wasm_Module_AoT)
|
if (module->module_type == Wasm_Module_AoT)
|
||||||
|
@ -1650,7 +1661,7 @@ wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size,
|
||||||
uint32 error_buf_size)
|
uint32 error_buf_size)
|
||||||
{
|
{
|
||||||
return wasm_runtime_instantiate_internal(module, NULL, NULL, stack_size,
|
return wasm_runtime_instantiate_internal(module, NULL, NULL, stack_size,
|
||||||
heap_size, 0, error_buf,
|
heap_size, 0, true, error_buf,
|
||||||
error_buf_size);
|
error_buf_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1661,8 +1672,8 @@ wasm_runtime_instantiate_ex(WASMModuleCommon *module,
|
||||||
{
|
{
|
||||||
return wasm_runtime_instantiate_internal(
|
return wasm_runtime_instantiate_internal(
|
||||||
module, NULL, NULL, args->default_stack_size,
|
module, NULL, NULL, args->default_stack_size,
|
||||||
args->host_managed_heap_size, args->max_memory_pages, error_buf,
|
args->host_managed_heap_size, args->max_memory_pages,
|
||||||
error_buf_size);
|
args->run_start_function, error_buf, error_buf_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -7618,7 +7629,7 @@ wasm_runtime_sub_module_instantiate(WASMModuleCommon *module,
|
||||||
WASMModuleInstanceCommon *sub_module_inst = NULL;
|
WASMModuleInstanceCommon *sub_module_inst = NULL;
|
||||||
sub_module_inst = wasm_runtime_instantiate_internal(
|
sub_module_inst = wasm_runtime_instantiate_internal(
|
||||||
sub_module, NULL, NULL, stack_size, heap_size, max_memory_pages,
|
sub_module, NULL, NULL, stack_size, heap_size, max_memory_pages,
|
||||||
error_buf, error_buf_size);
|
true, error_buf, error_buf_size);
|
||||||
if (!sub_module_inst) {
|
if (!sub_module_inst) {
|
||||||
LOG_DEBUG("instantiate %s failed",
|
LOG_DEBUG("instantiate %s failed",
|
||||||
sub_module_list_node->module_name);
|
sub_module_list_node->module_name);
|
||||||
|
|
|
@ -681,7 +681,8 @@ wasm_runtime_instantiate_internal(WASMModuleCommon *module,
|
||||||
WASMModuleInstanceCommon *parent,
|
WASMModuleInstanceCommon *parent,
|
||||||
WASMExecEnv *exec_env_main, uint32 stack_size,
|
WASMExecEnv *exec_env_main, uint32 stack_size,
|
||||||
uint32 heap_size, uint32 max_memory_pages,
|
uint32 heap_size, uint32 max_memory_pages,
|
||||||
char *error_buf, uint32 error_buf_size);
|
bool run_start_function, char *error_buf,
|
||||||
|
uint32 error_buf_size);
|
||||||
|
|
||||||
/* Internal API */
|
/* Internal API */
|
||||||
void
|
void
|
||||||
|
|
|
@ -288,6 +288,8 @@ typedef struct InstantiationArgs {
|
||||||
uint32_t default_stack_size;
|
uint32_t default_stack_size;
|
||||||
uint32_t host_managed_heap_size;
|
uint32_t host_managed_heap_size;
|
||||||
uint32_t max_memory_pages;
|
uint32_t max_memory_pages;
|
||||||
|
|
||||||
|
bool run_start_function;
|
||||||
} InstantiationArgs;
|
} InstantiationArgs;
|
||||||
#endif /* INSTANTIATION_ARGS_OPTION_DEFINED */
|
#endif /* INSTANTIATION_ARGS_OPTION_DEFINED */
|
||||||
|
|
||||||
|
@ -734,6 +736,17 @@ wasm_runtime_instantiate_ex(const wasm_module_t module,
|
||||||
const InstantiationArgs *args, char *error_buf,
|
const InstantiationArgs *args, char *error_buf,
|
||||||
uint32_t error_buf_size);
|
uint32_t error_buf_size);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the start function of an otherwise instantiated module. See
|
||||||
|
* InstantiationArgs.run_start_function.
|
||||||
|
*/
|
||||||
|
WASM_RUNTIME_API_EXTERN bool
|
||||||
|
wasm_runtime_instantiate_run_start_func(wasm_module_inst_t module,
|
||||||
|
wasm_module_inst_t parent,
|
||||||
|
wasm_exec_env_t exec_env,
|
||||||
|
char *error_buf,
|
||||||
|
uint32_t error_buf_size);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the running mode of a WASM module instance, override the
|
* Set the running mode of a WASM module instance, override the
|
||||||
* default running mode of the runtime. Note that it only makes sense when
|
* default running mode of the runtime. Note that it only makes sense when
|
||||||
|
|
|
@ -2333,6 +2333,25 @@ wasm_set_running_mode(WASMModuleInstance *module_inst, RunningMode running_mode)
|
||||||
return set_running_mode(module_inst, running_mode, false);
|
return set_running_mode(module_inst, running_mode, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
wasm_runtime_instantiate_run_start_func(wasm_module_inst_t module_inst,
|
||||||
|
wasm_module_inst_t parent,
|
||||||
|
wasm_exec_env_t exec_env,
|
||||||
|
char *error_buf,
|
||||||
|
uint32_t error_buf_size)
|
||||||
|
{
|
||||||
|
const bool is_sub_inst = parent != NULL;
|
||||||
|
if (!execute_post_instantiate_functions((WASMModuleInstance *)module_inst,
|
||||||
|
is_sub_inst, exec_env)) {
|
||||||
|
set_error_buf(error_buf, error_buf_size,
|
||||||
|
((WASMModuleInstance *)module_inst)->cur_exception);
|
||||||
|
wasm_runtime_deinstantiate(module_inst);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiate module
|
* Instantiate module
|
||||||
*/
|
*/
|
||||||
|
@ -2341,6 +2360,41 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
|
||||||
WASMExecEnv *exec_env_main, uint32 stack_size,
|
WASMExecEnv *exec_env_main, uint32 stack_size,
|
||||||
uint32 heap_size, uint32 max_memory_pages, char *error_buf,
|
uint32 heap_size, uint32 max_memory_pages, char *error_buf,
|
||||||
uint32 error_buf_size)
|
uint32 error_buf_size)
|
||||||
|
{
|
||||||
|
WASMModuleInstance *module_inst = wasm_instantiate_without_start_function(
|
||||||
|
module, parent, exec_env_main, stack_size, heap_size, max_memory_pages,
|
||||||
|
error_buf, error_buf_size);
|
||||||
|
|
||||||
|
if (!module_inst) {
|
||||||
|
// wasm_instantiate_without_start_function will deinstantiate on
|
||||||
|
// failure.
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!wasm_runtime_instantiate_run_start_func(
|
||||||
|
(WASMModuleInstanceCommon *)module_inst,
|
||||||
|
(WASMModuleInstanceCommon *)parent, exec_env_main, error_buf,
|
||||||
|
error_buf_size)) {
|
||||||
|
// run_start_func will deinstantiate on failure.
|
||||||
|
set_error_buf(error_buf, error_buf_size, module_inst->cur_exception);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if WASM_ENABLE_MEMORY_TRACING != 0
|
||||||
|
wasm_runtime_dump_module_inst_mem_consumption(
|
||||||
|
(WASMModuleInstanceCommon *)module_inst);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return module_inst;
|
||||||
|
}
|
||||||
|
|
||||||
|
WASMModuleInstance *
|
||||||
|
wasm_instantiate_without_start_function(WASMModule *module,
|
||||||
|
WASMModuleInstance *parent,
|
||||||
|
WASMExecEnv *exec_env_main,
|
||||||
|
uint32 stack_size, uint32 heap_size,
|
||||||
|
uint32 max_memory_pages,
|
||||||
|
char *error_buf, uint32 error_buf_size)
|
||||||
{
|
{
|
||||||
WASMModuleInstance *module_inst;
|
WASMModuleInstance *module_inst;
|
||||||
WASMGlobalInstance *globals = NULL, *global;
|
WASMGlobalInstance *globals = NULL, *global;
|
||||||
|
@ -3257,17 +3311,6 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
|
||||||
&module_inst->e->functions[module->start_function];
|
&module_inst->e->functions[module->start_function];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!execute_post_instantiate_functions(module_inst, is_sub_inst,
|
|
||||||
exec_env_main)) {
|
|
||||||
set_error_buf(error_buf, error_buf_size, module_inst->cur_exception);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if WASM_ENABLE_MEMORY_TRACING != 0
|
|
||||||
wasm_runtime_dump_module_inst_mem_consumption(
|
|
||||||
(WASMModuleInstanceCommon *)module_inst);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
(void)global_data_end;
|
(void)global_data_end;
|
||||||
return module_inst;
|
return module_inst;
|
||||||
|
|
||||||
|
|
|
@ -543,6 +543,14 @@ bool
|
||||||
wasm_resolve_import_func(const WASMModule *module,
|
wasm_resolve_import_func(const WASMModule *module,
|
||||||
WASMFunctionImport *function);
|
WASMFunctionImport *function);
|
||||||
|
|
||||||
|
WASMModuleInstance *
|
||||||
|
wasm_instantiate_without_start_function(WASMModule *module,
|
||||||
|
WASMModuleInstance *parent,
|
||||||
|
WASMExecEnv *exec_env_main,
|
||||||
|
uint32 stack_size, uint32 heap_size,
|
||||||
|
uint32 max_memory_pages,
|
||||||
|
char *error_buf, uint32 error_buf_size);
|
||||||
|
|
||||||
WASMModuleInstance *
|
WASMModuleInstance *
|
||||||
wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
|
wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
|
||||||
WASMExecEnv *exec_env_main, uint32 stack_size,
|
WASMExecEnv *exec_env_main, uint32 stack_size,
|
||||||
|
|
|
@ -580,7 +580,7 @@ pthread_create_wrapper(wasm_exec_env_t exec_env,
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (!(new_module_inst = wasm_runtime_instantiate_internal(
|
if (!(new_module_inst = wasm_runtime_instantiate_internal(
|
||||||
module, module_inst, exec_env, stack_size, 0, 0, NULL, 0)))
|
module, module_inst, exec_env, stack_size, 0, 0, true, NULL, 0)))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
/* Set custom_data to new module instance */
|
/* Set custom_data to new module instance */
|
||||||
|
|
|
@ -87,7 +87,7 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg)
|
||||||
stack_size = ((WASMModuleInstance *)module_inst)->default_wasm_stack_size;
|
stack_size = ((WASMModuleInstance *)module_inst)->default_wasm_stack_size;
|
||||||
|
|
||||||
if (!(new_module_inst = wasm_runtime_instantiate_internal(
|
if (!(new_module_inst = wasm_runtime_instantiate_internal(
|
||||||
module, module_inst, exec_env, stack_size, 0, 0, NULL, 0)))
|
module, module_inst, exec_env, stack_size, 0, 0, true, NULL, 0)))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
wasm_runtime_set_custom_data_internal(
|
wasm_runtime_set_custom_data_internal(
|
||||||
|
|
|
@ -506,7 +506,8 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(new_module_inst = wasm_runtime_instantiate_internal(
|
if (!(new_module_inst = wasm_runtime_instantiate_internal(
|
||||||
module, module_inst, exec_env, stack_size, 0, 0, NULL, 0))) {
|
module, module_inst, exec_env, stack_size, 0, 0, true, NULL,
|
||||||
|
0))) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -949,10 +949,38 @@ main(int argc, char *argv[])
|
||||||
libc_wasi_init(wasm_module, argc, argv, &wasi_parse_ctx);
|
libc_wasi_init(wasm_module, argc, argv, &wasi_parse_ctx);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
InstantiationArgs args = {
|
||||||
|
.default_stack_size = stack_size,
|
||||||
|
.host_managed_heap_size = heap_size,
|
||||||
|
.max_memory_pages = 0,
|
||||||
|
.run_start_function = false,
|
||||||
|
};
|
||||||
|
|
||||||
/* instantiate the module */
|
/* instantiate the module */
|
||||||
if (!(wasm_module_inst =
|
if (!(wasm_module_inst = wasm_runtime_instantiate_ex(
|
||||||
wasm_runtime_instantiate(wasm_module, stack_size, heap_size,
|
wasm_module, &args, error_buf, sizeof(error_buf)))) {
|
||||||
error_buf, sizeof(error_buf)))) {
|
printf("%s\n", error_buf);
|
||||||
|
goto fail3;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if WASM_ENABLE_THREAD_MGR != 0
|
||||||
|
struct timeout_arg timeout_arg;
|
||||||
|
korp_tid timeout_tid;
|
||||||
|
if (timeout_ms >= 0) {
|
||||||
|
timeout_arg.timeout_ms = timeout_ms;
|
||||||
|
timeout_arg.inst = wasm_module_inst;
|
||||||
|
timeout_arg.cancel = false;
|
||||||
|
ret = os_thread_create(&timeout_tid, timeout_thread, &timeout_arg,
|
||||||
|
APP_THREAD_STACK_SIZE_DEFAULT);
|
||||||
|
if (ret != 0) {
|
||||||
|
printf("Failed to start timeout\n");
|
||||||
|
goto fail4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (!wasm_runtime_instantiate_run_start_func(
|
||||||
|
wasm_module_inst, NULL, NULL, error_buf, sizeof(error_buf))) {
|
||||||
printf("%s\n", error_buf);
|
printf("%s\n", error_buf);
|
||||||
goto fail3;
|
goto fail3;
|
||||||
}
|
}
|
||||||
|
@ -970,27 +998,11 @@ main(int argc, char *argv[])
|
||||||
uint32_t debug_port;
|
uint32_t debug_port;
|
||||||
if (exec_env == NULL) {
|
if (exec_env == NULL) {
|
||||||
printf("%s\n", wasm_runtime_get_exception(wasm_module_inst));
|
printf("%s\n", wasm_runtime_get_exception(wasm_module_inst));
|
||||||
goto fail4;
|
goto fail5;
|
||||||
}
|
}
|
||||||
debug_port = wasm_runtime_start_debug_instance(exec_env);
|
debug_port = wasm_runtime_start_debug_instance(exec_env);
|
||||||
if (debug_port == 0) {
|
if (debug_port == 0) {
|
||||||
printf("Failed to start debug instance\n");
|
printf("Failed to start debug instance\n");
|
||||||
goto fail4;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if WASM_ENABLE_THREAD_MGR != 0
|
|
||||||
struct timeout_arg timeout_arg;
|
|
||||||
korp_tid timeout_tid;
|
|
||||||
if (timeout_ms >= 0) {
|
|
||||||
timeout_arg.timeout_ms = timeout_ms;
|
|
||||||
timeout_arg.inst = wasm_module_inst;
|
|
||||||
timeout_arg.cancel = false;
|
|
||||||
ret = os_thread_create(&timeout_tid, timeout_thread, &timeout_arg,
|
|
||||||
APP_THREAD_STACK_SIZE_DEFAULT);
|
|
||||||
if (ret != 0) {
|
|
||||||
printf("Failed to start timeout\n");
|
|
||||||
goto fail5;
|
goto fail5;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1039,10 +1051,10 @@ main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if WASM_ENABLE_THREAD_MGR != 0
|
#if WASM_ENABLE_DEBUG_INTERP != 0
|
||||||
fail5:
|
fail5:
|
||||||
#endif
|
#endif
|
||||||
#if WASM_ENABLE_DEBUG_INTERP != 0
|
#if WASM_ENABLE_THREAD_MGR != 0
|
||||||
fail4:
|
fail4:
|
||||||
#endif
|
#endif
|
||||||
/* destroy the module instance */
|
/* destroy the module instance */
|
||||||
|
|
Loading…
Reference in New Issue
Block a user