Skip to content

Commit

Permalink
Add wasm_runtime_unregister_natives (#1647)
Browse files Browse the repository at this point in the history
Allow to unregister (or unload) the previously registered native libs,
so that no need to restart the whole engine by using
`wasm_runtime_destroy/wasm_runtime_init`.
  • Loading branch information
yamt authored Oct 28, 2022
1 parent bc58778 commit 77ff7da
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 4 deletions.
20 changes: 20 additions & 0 deletions core/iwasm/common/wasm_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,26 @@ wasm_native_register_natives_raw(const char *module_name,
true);
}

bool
wasm_native_unregister_natives(const char *module_name,
NativeSymbol *native_symbols)
{
NativeSymbolsNode **prevp;
NativeSymbolsNode *node;

prevp = &g_native_symbols_list;
while ((node = *prevp) != NULL) {
if (node->native_symbols == native_symbols
&& !strcmp(node->module_name, module_name)) {
*prevp = node->next;
wasm_runtime_free(node);
return true;
}
prevp = &node->next;
}
return false;
}

bool
wasm_native_init()
{
Expand Down
4 changes: 4 additions & 0 deletions core/iwasm/common/wasm_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ wasm_native_register_natives_raw(const char *module_name,
NativeSymbol *native_symbols,
uint32 n_native_symbols);

bool
wasm_native_unregister_natives(const char *module_name,
NativeSymbol *native_symbols);

bool
wasm_native_init();

Expand Down
7 changes: 7 additions & 0 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -2904,6 +2904,13 @@ wasm_runtime_register_natives_raw(const char *module_name,
n_native_symbols);
}

bool
wasm_runtime_unregister_natives(const char *module_name,
NativeSymbol *native_symbols)
{
return wasm_native_unregister_natives(module_name, native_symbols);
}

bool
wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
const WASMType *func_type, const char *signature,
Expand Down
5 changes: 5 additions & 0 deletions core/iwasm/common/wasm_runtime_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,11 @@ wasm_runtime_register_natives_raw(const char *module_name,
NativeSymbol *native_symbols,
uint32 n_native_symbols);

/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN bool
wasm_runtime_unregister_natives(const char *module_name,
NativeSymbol *native_symbols);

bool
wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
const WASMType *func_type, const char *signature,
Expand Down
18 changes: 18 additions & 0 deletions core/iwasm/include/wasm_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,24 @@ wasm_runtime_register_natives_raw(const char *module_name,
NativeSymbol *native_symbols,
uint32_t n_native_symbols);


/**
* Undo wasm_runtime_register_natives or wasm_runtime_register_natives_raw
*
* @param module_name Should be the same as the corresponding
* wasm_runtime_register_natives.
* (Same in term of strcmp.)
*
* @param native_symbols Should be the same as the corresponding
* wasm_runtime_register_natives.
* (Same in term of pointer comparison.)
*
* @return true if success, false otherwise
*/
WASM_RUNTIME_API_EXTERN bool
wasm_runtime_unregister_natives(const char *module_name,
NativeSymbol *native_symbols);

/**
* Get attachment of native function from execution environment
*
Expand Down
46 changes: 42 additions & 4 deletions product-mini/platforms/posix/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,46 @@ load_and_register_native_libs(const char **native_lib_list,

return native_handle_count;
}

static void
unregister_and_unload_native_libs(uint32 native_lib_count,
void **native_handle_list)
{
uint32 i, n_native_symbols;
NativeSymbol *native_symbols;
char *module_name;
void *handle;

for (i = 0; i < native_lib_count; i++) {
handle = native_handle_list[i];

/* 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 %p",
handle);
continue;
}

n_native_symbols = get_native_lib(&module_name, &native_symbols);
if (n_native_symbols == 0 || module_name == NULL
|| native_symbols == NULL) {
LOG_WARNING("warning: get_native_lib returned different values for "
"native lib %p",
handle);
continue;
}

/* unregister native symbols */
if (!wasm_runtime_unregister_natives(module_name, native_symbols)) {
LOG_WARNING("warning: failed to unregister native lib %p", handle);
continue;
}

dlclose(handle);
}
}
#endif /* BH_HAS_DLFCN */

#if WASM_ENABLE_MULTI_MODULE != 0
Expand Down Expand Up @@ -328,7 +368,7 @@ main(int argc, char *argv[])
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;
uint32 native_handle_count = 0;
#endif
#if WASM_ENABLE_DEBUG_INTERP != 0
char *ip_addr = NULL;
Expand Down Expand Up @@ -632,9 +672,7 @@ main(int argc, char *argv[])
fail1:
#if BH_HAS_DLFCN
/* 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]);
unregister_and_unload_native_libs(native_handle_count, native_handle_list);
#endif

/* destroy runtime environment */
Expand Down

0 comments on commit 77ff7da

Please sign in to comment.