Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
/.idea
**/cmake-build-*/
**/*build/
*.obj
*.a
*.so

core/deps/**
core/shared/mem-alloc/tlsf
core/app-framework/wgl
Expand All @@ -12,6 +16,9 @@ wamr-sdk/out/
wamr-sdk/runtime/build_runtime_sdk/
test-tools/host-tool/bin/
product-mini/app-samples/hello-world/test.wasm
product-mini/platforms/linux-sgx/enclave-sample/App/
product-mini/platforms/linux-sgx/enclave-sample/Enclave/
product-mini/platforms/linux-sgx/enclave-sample/iwasm

build_out
tests/wamr-test-suites/workspace
4 changes: 2 additions & 2 deletions core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,7 @@ load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
#if WASM_ENABLE_LIBC_WASI != 0
if (!strcmp(import_funcs[i].module_name, "wasi_unstable")
|| !strcmp(import_funcs[i].module_name, "wasi_snapshot_preview1"))
module->is_wasi_module = true;
module->import_wasi_api = true;
#endif
}

Expand Down Expand Up @@ -2925,7 +2925,7 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
module->comp_data = comp_data;

#if WASM_ENABLE_LIBC_WASI != 0
module->is_wasi_module = comp_data->wasm_module->is_wasi_module;
module->import_wasi_api = comp_data->wasm_module->import_wasi_api;
#endif

return module;
Expand Down
2 changes: 1 addition & 1 deletion core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, uint32 stack_size,

#if WASM_ENABLE_BULK_MEMORY != 0
#if WASM_ENABLE_LIBC_WASI != 0
if (!module->is_wasi_module) {
if (!module->import_wasi_api) {
#endif
/* Only execute the memory init function for main instance because
the data segments will be dropped once initialized.
Expand Down
2 changes: 1 addition & 1 deletion core/iwasm/aot/aot_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ typedef struct AOTModule {

#if WASM_ENABLE_LIBC_WASI != 0
WASIArguments wasi_args;
bool is_wasi_module;
bool import_wasi_api;
#endif
#if WASM_ENABLE_DEBUG_AOT != 0
void *elf_hdr;
Expand Down
137 changes: 53 additions & 84 deletions core/iwasm/common/wasm_application.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ static union {
/**
* Implementation of wasm_application_execute_main()
*/

static WASMFunctionInstanceCommon *
resolve_function(const WASMModuleInstanceCommon *module_inst, const char *name);

static bool
check_main_func_type(const WASMType *type)
{
Expand Down Expand Up @@ -96,23 +92,29 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
bool ret, is_import_func = true;

#if WASM_ENABLE_LIBC_WASI != 0
if (wasm_runtime_is_wasi_mode(module_inst)) {
/* In wasi mode, we should call function named "_start"
which initializes the wasi envrionment and then calls
the actual main function. Directly call main function
may cause exception thrown. */
if ((func = wasm_runtime_lookup_wasi_start_function(module_inst)))
return wasm_runtime_create_exec_env_and_call_wasm(module_inst, func,
0, NULL);
/* If no start function was found, we execute
the main function as normal */
/* In wasi mode, we should call function named "_start"
which initializes the wasi envrionment and then calls
the actual main function. Directly call main function
may cause exception thrown. */
if ((func = wasm_runtime_lookup_wasi_start_function(module_inst))) {
return wasm_runtime_create_exec_env_and_call_wasm(module_inst, func, 0,
NULL);
}
#endif /* end of WASM_ENABLE_LIBC_WASI */

if (!(func = resolve_function(module_inst, "main"))
&& !(func = resolve_function(module_inst, "__main_argc_argv"))
&& !(func = resolve_function(module_inst, "_main"))) {
wasm_runtime_set_exception(module_inst, "lookup main function failed");
if (!(func = wasm_runtime_lookup_function(module_inst, "main", NULL))
&& !(func = wasm_runtime_lookup_function(module_inst,
"__main_argc_argv", NULL))
&& !(func = wasm_runtime_lookup_function(module_inst, "_main", NULL))) {
#if WASM_ENABLE_LIBC_WASI != 0
wasm_runtime_set_exception(
module_inst, "lookup the entry point symbol (like _start, main, "
"_main, __main_argc_argv) failed");
#else
wasm_runtime_set_exception(module_inst,
"lookup the entry point symbol (like main, "
"_main, __main_argc_argv) failed");
#endif
return false;
}

Expand Down Expand Up @@ -238,21 +240,23 @@ parse_function_name(char *orig_function_name, char **p_module_name,
* Implementation of wasm_application_execute_func()
*/

static WASMFunctionInstanceCommon *
resolve_function(const WASMModuleInstanceCommon *module_inst, const char *name)
static bool
resolve_function(WASMModuleInstanceCommon *module_inst, const char *name,
WASMFunctionInstanceCommon **out_func,
WASMModuleInstanceCommon **out_module_inst)
{
uint32 i = 0;
WASMFunctionInstanceCommon *ret = NULL;
WASMFunctionInstanceCommon *target_func = NULL;
WASMModuleInstanceCommon *target_inst = NULL;

#if WASM_ENABLE_MULTI_MODULE != 0
WASMModuleInstance *sub_module_inst = NULL;
char *function_name = NULL;
char *orig_name = NULL;
char *sub_module_name = NULL;
char *function_name = NULL;
uint32 length = (uint32)(strlen(name) + 1);

orig_name = runtime_malloc(sizeof(char) * length, NULL, NULL, 0);
if (!orig_name) {
return NULL;
goto LEAVE;
}

strncpy(orig_name, name, length);
Expand All @@ -264,53 +268,33 @@ resolve_function(const WASMModuleInstanceCommon *module_inst, const char *name)
LOG_DEBUG("%s -> %s and %s", name, sub_module_name, function_name);

if (sub_module_name) {
sub_module_inst = get_sub_module_inst((WASMModuleInstance *)module_inst,
sub_module_name);
if (!sub_module_inst) {
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

#if WASM_ENABLE_INTERP != 0
if (module_inst->module_type == Wasm_Module_Bytecode) {
WASMModuleInstance *wasm_inst = (WASMModuleInstance *)module_inst;

#if WASM_ENABLE_MULTI_MODULE != 0
wasm_inst = sub_module_inst ? sub_module_inst : wasm_inst;
#endif /* WASM_ENABLE_MULTI_MODULE */

for (i = 0; i < wasm_inst->export_func_count; i++) {
if (!strcmp(wasm_inst->export_functions[i].name, function_name)) {
ret = wasm_inst->export_functions[i].function;
break;
}
}
}
#endif /* WASM_ENABLE_INTERP */

#if WASM_ENABLE_AOT != 0
if (module_inst->module_type == Wasm_Module_AoT) {
AOTModuleInstance *aot_inst = (AOTModuleInstance *)module_inst;
AOTFunctionInstance *export_funcs =
(AOTFunctionInstance *)aot_inst->export_funcs.ptr;
for (i = 0; i < aot_inst->export_func_count; i++) {
if (!strcmp(export_funcs[i].func_name, function_name)) {
ret = &export_funcs[i];
break;
}
}
}
#endif
target_func =
wasm_runtime_lookup_function(target_inst, function_name, NULL);

#if WASM_ENABLE_MULTI_MODULE != 0
LEAVE:
wasm_runtime_free(orig_name);
if (orig_name)
wasm_runtime_free(orig_name);
#endif
return ret;

*out_func = target_func;
*out_module_inst = target_inst;
return target_func;
}

union ieee754_float {
Expand Down Expand Up @@ -358,7 +342,8 @@ bool
wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
const char *name, int32 argc, char *argv[])
{
WASMFunctionInstanceCommon *func;
WASMFunctionInstanceCommon *target_func;
WASMModuleInstanceCommon *target_inst;
WASMType *type = NULL;
uint32 argc1, *argv1 = NULL, cell_num = 0, j, k = 0;
int32 i, p, module_type;
Expand All @@ -368,31 +353,15 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,

bh_assert(argc >= 0);
LOG_DEBUG("call a function \"%s\" with %d arguments", name, argc);
func = resolve_function(module_inst, name);

if (!func) {
if (!resolve_function(module_inst, name, &target_func, &target_inst)) {
snprintf(buf, sizeof(buf), "lookup function %s failed", name);
wasm_runtime_set_exception(module_inst, buf);
goto fail;
}

#if WASM_ENABLE_INTERP != 0
if (module_inst->module_type == Wasm_Module_Bytecode) {
WASMFunctionInstance *wasm_func = (WASMFunctionInstance *)func;
if (wasm_func->is_import_func
#if WASM_ENABLE_MULTI_MODULE != 0
&& !wasm_func->import_func_inst
#endif
) {
snprintf(buf, sizeof(buf), "lookup function %s failed", name);
wasm_runtime_set_exception(module_inst, buf);
goto fail;
}
}
#endif

module_type = module_inst->module_type;
type = wasm_runtime_get_function_type(func, module_type);
module_type = target_inst->module_type;
type = wasm_runtime_get_function_type(target_func, module_type);

if (!type) {
LOG_ERROR("invalid module instance type");
Expand All @@ -408,7 +377,7 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
cell_num = (argc1 > type->ret_cell_num) ? argc1 : type->ret_cell_num;

total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst, NULL, 0)))) {
if ((!(argv1 = runtime_malloc((uint32)total_size, target_inst, NULL, 0)))) {
goto fail;
}

Expand Down Expand Up @@ -538,7 +507,7 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
void *extern_obj = (void *)(uintptr_t)val;
uint32 externref_idx;

if (!wasm_externref_obj2ref(module_inst, extern_obj,
if (!wasm_externref_obj2ref(target_inst, extern_obj,
&externref_idx)) {
wasm_runtime_set_exception(
module_inst, "map extern object to ref failed");
Expand All @@ -563,8 +532,8 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
bh_assert(p == (int32)argc1);

wasm_runtime_set_exception(module_inst, NULL);
if (!wasm_runtime_create_exec_env_and_call_wasm(module_inst, func, argc1,
argv1)) {
if (!wasm_runtime_create_exec_env_and_call_wasm(target_inst, target_func,
argc1, argv1)) {
goto fail;
}

Expand Down
4 changes: 2 additions & 2 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -2294,13 +2294,13 @@ wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst)
{
#if WASM_ENABLE_INTERP != 0
if (module_inst->module_type == Wasm_Module_Bytecode
&& ((WASMModuleInstance *)module_inst)->module->is_wasi_module)
&& ((WASMModuleInstance *)module_inst)->module->import_wasi_api)
return true;
#endif
#if WASM_ENABLE_AOT != 0
if (module_inst->module_type == Wasm_Module_AoT
&& ((AOTModule *)((AOTModuleInstance *)module_inst)->aot_module.ptr)
->is_wasi_module)
->import_wasi_api)
return true;
#endif
return false;
Expand Down
2 changes: 1 addition & 1 deletion core/iwasm/interpreter/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ struct WASMModule {

#if WASM_ENABLE_LIBC_WASI != 0
WASIArguments wasi_args;
bool is_wasi_module;
bool import_wasi_api;
#endif

#if WASM_ENABLE_MULTI_MODULE != 0
Expand Down
Loading