Enable register native with iwasm (#1120)
Enable register native with iwasm application, add sample and update document.
This commit is contained in:
@ -8,6 +8,9 @@
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE)
|
||||
#include <dlfcn.h>
|
||||
#endif
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include "bh_read_file.h"
|
||||
@ -39,13 +42,18 @@ print_help()
|
||||
printf(" --dir=<dir> Grant wasi access to the given host directories\n");
|
||||
printf(" to the program, for example:\n");
|
||||
printf(" --dir=<dir1> --dir=<dir2>\n");
|
||||
printf(" --addr-pool= Grant wasi access to the given network addresses in\n");
|
||||
printf(" --addr-pool=<addrs> Grant wasi access to the given network addresses in\n");
|
||||
printf(" CIRD notation to the program, seperated with ',',\n");
|
||||
printf(" for example:\n");
|
||||
printf(" --addr-pool=1.2.3.4/15,2.3.4.5/16\n");
|
||||
#endif
|
||||
#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE)
|
||||
printf(" --native-lib=<lib> Register native libraries to the WASM module, which\n");
|
||||
printf(" are shared object (.so) files, for example:\n");
|
||||
printf(" --native-lib=test1.so --native-lib=test2.so\n");
|
||||
#endif
|
||||
#if WASM_ENABLE_MULTI_MODULE != 0
|
||||
printf(" --module-path= Indicate a module search path. default is current\n"
|
||||
printf(" --module-path=<path> Indicate a module search path. default is current\n"
|
||||
" directory('./')\n");
|
||||
#endif
|
||||
#if WASM_ENABLE_LIB_PTHREAD != 0
|
||||
@ -174,13 +182,57 @@ validate_env_str(char *env)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
|
||||
#ifdef __NuttX__
|
||||
static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE * BH_KB] = { 0 };
|
||||
#else
|
||||
static char global_heap_buf[10 * 1024 * 1024] = { 0 };
|
||||
#endif
|
||||
#endif
|
||||
#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE)
|
||||
typedef uint32 (*get_native_lib_func)(char **p_module_name,
|
||||
NativeSymbol **p_native_symbols);
|
||||
|
||||
static uint32
|
||||
load_and_register_native_libs(const char **native_lib_list,
|
||||
uint32 native_lib_count,
|
||||
void **native_handle_list)
|
||||
{
|
||||
uint32 i, native_handle_count = 0, n_native_symbols;
|
||||
NativeSymbol *native_symbols;
|
||||
char *module_name;
|
||||
void *handle;
|
||||
|
||||
for (i = 0; i < native_lib_count; i++) {
|
||||
/* open the native library */
|
||||
if (!(handle = dlopen(native_lib_list[i], RTLD_NOW | RTLD_GLOBAL))
|
||||
&& !(handle = dlopen(native_lib_list[i], RTLD_LAZY))) {
|
||||
LOG_WARNING("warning: failed to load native library %s",
|
||||
native_lib_list[i]);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* lookup get_native_lib func */
|
||||
get_native_lib_func get_native_lib = dlsym(handle, "get_native_lib");
|
||||
if (!get_native_lib) {
|
||||
LOG_WARNING("warning: failed to lookup `get_native_lib` function "
|
||||
"from native lib %s",
|
||||
native_lib_list[i]);
|
||||
dlclose(handle);
|
||||
continue;
|
||||
}
|
||||
|
||||
n_native_symbols = get_native_lib(&module_name, &native_symbols);
|
||||
|
||||
/* register native symbols */
|
||||
if (!(n_native_symbols > 0 && module_name && native_symbols
|
||||
&& wasm_runtime_register_natives(module_name, native_symbols,
|
||||
n_native_symbols))) {
|
||||
LOG_WARNING("warning: failed to register native lib %s",
|
||||
native_lib_list[i]);
|
||||
dlclose(handle);
|
||||
continue;
|
||||
}
|
||||
|
||||
native_handle_list[native_handle_count++] = handle;
|
||||
}
|
||||
|
||||
return native_handle_count;
|
||||
}
|
||||
#endif /* end of defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) */
|
||||
|
||||
#if WASM_ENABLE_MULTI_MODULE != 0
|
||||
static char *
|
||||
@ -224,6 +276,14 @@ moudle_destroyer(uint8 *buffer, uint32 size)
|
||||
}
|
||||
#endif /* WASM_ENABLE_MULTI_MODULE */
|
||||
|
||||
#if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
|
||||
#ifdef __NuttX__
|
||||
static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE * BH_KB] = { 0 };
|
||||
#else
|
||||
static char global_heap_buf[10 * 1024 * 1024] = { 0 };
|
||||
#endif
|
||||
#endif
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
@ -249,6 +309,12 @@ main(int argc, char *argv[])
|
||||
const char *addr_pool[8] = { NULL };
|
||||
uint32 addr_pool_size = 0;
|
||||
#endif
|
||||
#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE)
|
||||
const char *native_lib_list[8] = { NULL };
|
||||
uint32 native_lib_count = 0;
|
||||
void *native_handle_list[8] = { NULL };
|
||||
uint32 native_handle_count = 0, native_handle_idx;
|
||||
#endif
|
||||
#if WASM_ENABLE_DEBUG_INTERP != 0
|
||||
char *ip_addr = NULL;
|
||||
/* int platform_port = 0; */
|
||||
@ -337,6 +403,18 @@ main(int argc, char *argv[])
|
||||
}
|
||||
}
|
||||
#endif /* WASM_ENABLE_LIBC_WASI */
|
||||
#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE)
|
||||
else if (!strncmp(argv[0], "--native-lib=", 13)) {
|
||||
if (argv[0][13] == '\0')
|
||||
return print_help();
|
||||
if (native_lib_count >= sizeof(native_lib_list) / sizeof(char *)) {
|
||||
printf("Only allow max native lib number %d\n",
|
||||
(int)(sizeof(native_lib_list) / sizeof(char *)));
|
||||
return -1;
|
||||
}
|
||||
native_lib_list[native_lib_count++] = argv[0] + 13;
|
||||
}
|
||||
#endif
|
||||
#if WASM_ENABLE_MULTI_MODULE != 0
|
||||
else if (!strncmp(argv[0],
|
||||
"--module-path=", strlen("--module-path="))) {
|
||||
@ -407,6 +485,11 @@ main(int argc, char *argv[])
|
||||
bh_log_set_verbose_level(log_verbose_level);
|
||||
#endif
|
||||
|
||||
#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE)
|
||||
native_handle_count = load_and_register_native_libs(
|
||||
native_lib_list, native_lib_count, native_handle_list);
|
||||
#endif
|
||||
|
||||
/* load WASM byte buffer from WASM bin file */
|
||||
if (!(wasm_file_buf =
|
||||
(uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size)))
|
||||
@ -481,6 +564,13 @@ fail2:
|
||||
os_munmap(wasm_file_buf, wasm_file_size);
|
||||
|
||||
fail1:
|
||||
#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE)
|
||||
/* unload the native libraries */
|
||||
for (native_handle_idx = 0; native_handle_idx < native_handle_count;
|
||||
native_handle_idx++)
|
||||
dlclose(native_handle_list[native_handle_idx]);
|
||||
#endif
|
||||
|
||||
/* destroy runtime environment */
|
||||
wasm_runtime_destroy();
|
||||
return 0;
|
||||
|
||||
@ -40,7 +40,7 @@ print_help()
|
||||
printf(" --dir=<dir1> --dir=<dir2>\n");
|
||||
#endif
|
||||
#if WASM_ENABLE_MULTI_MODULE != 0
|
||||
printf(" --module-path= Indicate a module search path. default is current\n"
|
||||
printf(" --module-path=<path> Indicate a module search path. default is current\n"
|
||||
" directory('./')\n");
|
||||
#endif
|
||||
#if WASM_ENABLE_LIB_PTHREAD != 0
|
||||
|
||||
Reference in New Issue
Block a user