Update README, change wasi primitive lib position and add some exception checks (#146)
Add exception throw when some initial checks fail in executing main or specific function
This commit is contained in:
@ -6,7 +6,7 @@ set (WASM_LIBC_DIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
include_directories(${WASM_LIBC_DIR})
|
||||
|
||||
|
||||
file (GLOB_RECURSE source_all ${WASM_LIBC_DIR}/*.c)
|
||||
file (GLOB source_all ${WASM_LIBC_DIR}/*.c)
|
||||
|
||||
set (WASM_LIBC_SOURCE ${source_all})
|
||||
|
||||
|
||||
@ -61,8 +61,8 @@ if (CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||
endif ()
|
||||
|
||||
if (NOT DEFINED WASM_ENABLE_WASI)
|
||||
# Enable wasi support by default
|
||||
set (WASM_ENABLE_WASI 1)
|
||||
# Disable wasi support by default
|
||||
set (WASM_ENABLE_WASI 0)
|
||||
endif ()
|
||||
|
||||
if (WASM_ENABLE_WASI EQUAL 1)
|
||||
@ -95,7 +95,7 @@ enable_language (ASM)
|
||||
include (../../runtime/platform/${PLATFORM}/platform.cmake)
|
||||
include (../../runtime/utils/utils.cmake)
|
||||
include (../../runtime/vmcore-wasm/vmcore.cmake)
|
||||
include (../../runtime/wasmtime-wasi-c/wasi.cmake)
|
||||
include (../../lib/native/libc/wasmtime-wasi-c/wasi.cmake)
|
||||
include (../../lib/native/base/wasm_lib_base.cmake)
|
||||
include (../../lib/native/libc/wasm_libc.cmake)
|
||||
include (${SHARED_LIB_DIR}/platform/${PLATFORM}/shared_platform.cmake)
|
||||
|
||||
@ -129,6 +129,7 @@ app_instance_repl(wasm_module_inst_t module_inst)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_WASI != 0
|
||||
static bool
|
||||
validate_env_str(char *env)
|
||||
{
|
||||
@ -145,6 +146,7 @@ validate_env_str(char *env)
|
||||
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
#define USE_GLOBAL_HEAP_BUF 0
|
||||
|
||||
|
||||
@ -96,7 +96,7 @@ enable_language (ASM)
|
||||
include (../../runtime/platform/${PLATFORM}/platform.cmake)
|
||||
include (../../runtime/utils/utils.cmake)
|
||||
include (../../runtime/vmcore-wasm/vmcore.cmake)
|
||||
include (../../runtime/wasmtime-wasi-c/wasi.cmake)
|
||||
include (../../lib/native/libc/wasmtime-wasi-c/wasi.cmake)
|
||||
include (../../lib/native/base/wasm_lib_base.cmake)
|
||||
include (../../lib/native/libc/wasm_libc.cmake)
|
||||
include (${SHARED_LIB_DIR}/platform/${PLATFORM}/shared_platform.cmake)
|
||||
|
||||
@ -92,7 +92,7 @@ enable_language (ASM)
|
||||
include (../../runtime/platform/${PLATFORM}/platform.cmake)
|
||||
include (../../runtime/utils/utils.cmake)
|
||||
include (../../runtime/vmcore-wasm/vmcore.cmake)
|
||||
include (../../runtime/wasmtime-wasi-c/wasi.cmake)
|
||||
include (../../lib/native/libc/wasmtime-wasi-c/wasi.cmake)
|
||||
include (../../lib/native/base/wasm_lib_base.cmake)
|
||||
include (../../lib/native/libc/wasm_libc.cmake)
|
||||
include (${SHARED_LIB_DIR}/platform/${PLATFORM}/shared_platform.cmake)
|
||||
|
||||
@ -156,6 +156,7 @@ app_instance_repl(wasm_module_inst_t module_inst)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_WASI != 0
|
||||
static bool
|
||||
validate_env_str(char *env)
|
||||
{
|
||||
@ -172,6 +173,7 @@ validate_env_str(char *env)
|
||||
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
#define USE_GLOBAL_HEAP_BUF 0
|
||||
|
||||
|
||||
@ -78,8 +78,8 @@ if (CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||
endif ()
|
||||
|
||||
if (NOT DEFINED WASM_ENABLE_WASI)
|
||||
# Enable wasi support by default
|
||||
set (WASM_ENABLE_WASI 1)
|
||||
# Disable wasi support by default
|
||||
set (WASM_ENABLE_WASI 0)
|
||||
endif ()
|
||||
|
||||
if (WASM_ENABLE_WASI EQUAL 1)
|
||||
@ -110,7 +110,7 @@ enable_language (ASM)
|
||||
include (../../runtime/platform/${PLATFORM}/platform.cmake)
|
||||
include (../../runtime/utils/utils.cmake)
|
||||
include (../../runtime/vmcore-wasm/vmcore.cmake)
|
||||
include (../../runtime/wasmtime-wasi-c/wasi.cmake)
|
||||
include (../../lib/native/libc/wasmtime-wasi-c/wasi.cmake)
|
||||
include (../../lib/native/base/wasm_lib_base.cmake)
|
||||
include (../../lib/native/libc/wasm_libc.cmake)
|
||||
include (${SHARED_LIB_DIR}/platform/${PLATFORM}/shared_platform.cmake)
|
||||
|
||||
@ -156,6 +156,7 @@ app_instance_repl(wasm_module_inst_t module_inst)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_WASI != 0
|
||||
static bool
|
||||
validate_env_str(char *env)
|
||||
{
|
||||
@ -172,6 +173,7 @@ validate_env_str(char *env)
|
||||
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
static char global_heap_buf[512 * 1024] = { 0 };
|
||||
|
||||
|
||||
@ -105,11 +105,17 @@ wasm_application_execute_main(WASMModuleInstance *module_inst,
|
||||
#endif
|
||||
|
||||
func = resolve_main_function(module_inst);
|
||||
if (!func || func->is_import_func)
|
||||
if (!func || func->is_import_func) {
|
||||
wasm_runtime_set_exception(module_inst,
|
||||
"lookup main function failed.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!check_main_func_type(func->u.func->func_type))
|
||||
if (!check_main_func_type(func->u.func->func_type)) {
|
||||
wasm_runtime_set_exception(module_inst,
|
||||
"invalid function type of main function.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (func->u.func->func_type->param_count) {
|
||||
for (i = 0; i < argc; i++)
|
||||
@ -120,8 +126,11 @@ wasm_application_execute_main(WASMModuleInstance *module_inst,
|
||||
|
||||
if (total_size >= UINT32_MAX
|
||||
|| !(argv_buf_offset =
|
||||
wasm_runtime_module_malloc(module_inst, (uint32)total_size)))
|
||||
wasm_runtime_module_malloc(module_inst, (uint32)total_size))) {
|
||||
wasm_runtime_set_exception(module_inst,
|
||||
"allocate memory failed.");
|
||||
return false;
|
||||
}
|
||||
|
||||
argv_buf = p = wasm_runtime_addr_app_to_native(module_inst, argv_buf_offset);
|
||||
argv_offsets = (int32*)(p + total_argv_size);
|
||||
@ -209,17 +218,20 @@ wasm_application_execute_func(WASMModuleInstance *module_inst,
|
||||
int32 i, p;
|
||||
uint64 total_size;
|
||||
const char *exception;
|
||||
char buf[128];
|
||||
|
||||
wasm_assert(argc >= 0);
|
||||
func = resolve_function(module_inst, name);
|
||||
if (!func || func->is_import_func) {
|
||||
LOG_ERROR("Wasm lookup function %s failed.\n", name);
|
||||
snprintf(buf, sizeof(buf), "lookup function %s failed.", name);
|
||||
wasm_runtime_set_exception(module_inst, buf);
|
||||
return false;
|
||||
}
|
||||
|
||||
type = func->u.func->func_type;
|
||||
if (type->param_count != (uint32)argc) {
|
||||
LOG_ERROR("Wasm prepare param failed: invalid param count.\n");
|
||||
wasm_runtime_set_exception(module_inst,
|
||||
"invalid input argument count.");
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -227,7 +239,7 @@ wasm_application_execute_func(WASMModuleInstance *module_inst,
|
||||
total_size = sizeof(uint32) * (uint64)(argc1 > 2 ? argc1 : 2);
|
||||
if (total_size >= UINT32_MAX
|
||||
|| (!(argv1 = wasm_malloc((uint32)total_size)))) {
|
||||
LOG_ERROR("Wasm prepare param failed: malloc failed.\n");
|
||||
wasm_runtime_set_exception(module_inst, "allocate memory failed.");
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -236,7 +248,8 @@ wasm_application_execute_func(WASMModuleInstance *module_inst,
|
||||
char *endptr = NULL;
|
||||
wasm_assert(argv[i] != NULL);
|
||||
if (argv[i][0] == '\0') {
|
||||
LOG_ERROR("Wasm prepare param failed: invalid num (%s).\n", argv[i]);
|
||||
snprintf(buf, sizeof(buf), "invalid input argument %d.", i);
|
||||
wasm_runtime_set_exception(module_inst, buf);
|
||||
goto fail;
|
||||
}
|
||||
switch (type->types[i]) {
|
||||
@ -303,11 +316,15 @@ wasm_application_execute_func(WASMModuleInstance *module_inst,
|
||||
}
|
||||
}
|
||||
if (endptr && *endptr != '\0' && *endptr != '_') {
|
||||
LOG_ERROR("Wasm prepare param failed: invalid num (%s).\n", argv[i]);
|
||||
snprintf(buf, sizeof(buf), "invalid input argument %d: %s.",
|
||||
i, argv[i]);
|
||||
wasm_runtime_set_exception(module_inst, buf);
|
||||
goto fail;
|
||||
}
|
||||
if (errno != 0) {
|
||||
LOG_ERROR("Wasm prepare param failed: errno %d.\n", errno);
|
||||
snprintf(buf, sizeof(buf),
|
||||
"prepare function argument error, errno: %d.", errno);
|
||||
wasm_runtime_set_exception(module_inst, buf);
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user