From a550f4d9f7a346bc217efe3e89e4f55a44466315 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 10 Aug 2023 10:26:52 +0900 Subject: [PATCH] iwasm: call native lib init/deinit if exists (#2439) --- product-mini/platforms/posix/main.c | 20 ++++++++++++++++++++ samples/native-lib/README.md | 4 +++- samples/native-lib/test_hello2.c | 13 +++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/product-mini/platforms/posix/main.c b/product-mini/platforms/posix/main.c index 45214243c..0cf41909d 100644 --- a/product-mini/platforms/posix/main.c +++ b/product-mini/platforms/posix/main.c @@ -284,6 +284,8 @@ validate_env_str(char *env) #if BH_HAS_DLFCN typedef uint32 (*get_native_lib_func)(char **p_module_name, NativeSymbol **p_native_symbols); +typedef int (*init_native_lib_func)(void); +typedef void (*deinit_native_lib_func)(void); static uint32 load_and_register_native_libs(const char **native_lib_list, @@ -304,6 +306,18 @@ load_and_register_native_libs(const char **native_lib_list, continue; } + init_native_lib_func init_native_lib = dlsym(handle, "init_native_lib"); + if (init_native_lib) { + int ret = init_native_lib(); + if (ret != 0) { + LOG_WARNING("warning: `init_native_lib` function from native " + "lib %s failed with %d", + native_lib_list[i], ret); + dlclose(handle); + continue; + } + } + /* lookup get_native_lib func */ get_native_lib_func get_native_lib = dlsym(handle, "get_native_lib"); if (!get_native_lib) { @@ -368,6 +382,12 @@ unregister_and_unload_native_libs(uint32 native_lib_count, continue; } + deinit_native_lib_func deinit_native_lib = + dlsym(handle, "deinit_native_lib"); + if (deinit_native_lib) { + deinit_native_lib(); + } + dlclose(handle); } } diff --git a/samples/native-lib/README.md b/samples/native-lib/README.md index 2bf65814d..80500ade0 100644 --- a/samples/native-lib/README.md +++ b/samples/native-lib/README.md @@ -62,6 +62,7 @@ cd build The output is: ```bash +init_native_lib in test_hello2.c called Hello World! 10 + 20 = 30 sqrt(10, 20) = 500 @@ -72,5 +73,6 @@ Message from test_hello: Hello, main. This is test_hello_wrapper! test_hello2("main", 0x0, 0) = 85 malloc(86) = 0x24e8 test_hello2("main", 0x24e8, 86) = 85 -Message from test_hello2: Hello, main. This is test_hello2_wrapper! Your wasm_module_inst_t is 0x7fd443704990. +Message from test_hello2: Hello, main. This is test_hello2_wrapper! Your wasm_module_inst_t is 0x7fe0e6804280. +deinit_native_lib in test_hello2.c called ``` diff --git a/samples/native-lib/test_hello2.c b/samples/native-lib/test_hello2.c index 2c8f69ed6..5dae79ca4 100644 --- a/samples/native-lib/test_hello2.c +++ b/samples/native-lib/test_hello2.c @@ -57,3 +57,16 @@ get_native_lib(char **p_module_name, NativeSymbol **p_native_symbols) *p_native_symbols = native_symbols; return sizeof(native_symbols) / sizeof(NativeSymbol); } + +int +init_native_lib() +{ + printf("%s in test_hello2.c called\n", __func__); + return 0; +} + +void +deinit_native_lib() +{ + printf("%s in test_hello2.c called\n", __func__); +}