Skip to content

Commit c97840c

Browse files
committed
Add import functions callback
Signed-off-by: zhenweijin <zhenwei.jin@intel.com>
1 parent 6253bd1 commit c97840c

6 files changed

Lines changed: 240 additions & 0 deletions

File tree

core/iwasm/common/wasm_runtime_common.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4562,6 +4562,28 @@ wasm_runtime_get_export_type(WASMModuleCommon *const module, int32 export_index,
45624562
#endif
45634563
}
45644564

4565+
void
4566+
wasm_runtime_for_each_import_func(WASMModuleCommon *const module,
4567+
wasm_func_type_callback_t callback)
4568+
{
4569+
int32_t import_count = wasm_runtime_get_import_count(module);
4570+
if (import_count <= 0)
4571+
return;
4572+
if (callback == NULL)
4573+
return;
4574+
4575+
for (int32_t i = 0; i < import_count; ++i) {
4576+
wasm_import_t import_type;
4577+
wasm_runtime_get_import_type(module, i, &import_type);
4578+
4579+
if (import_type.kind != WASM_IMPORT_EXPORT_KIND_FUNC) {
4580+
continue;
4581+
}
4582+
4583+
callback(&import_type, i);
4584+
}
4585+
}
4586+
45654587
uint32
45664588
wasm_func_type_get_param_count(WASMFuncType *const func_type)
45674589
{

core/iwasm/include/wasm_export.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,9 @@ typedef struct SharedHeapInitArgs {
354354
void *pre_allocated_addr;
355355
} SharedHeapInitArgs;
356356

357+
typedef void (*wasm_func_type_callback_t)(const wasm_import_t *import_type,
358+
int32_t import_index);
359+
357360
/**
358361
* Initialize the WASM runtime environment, and also initialize
359362
* the memory allocator with system allocator, which calls os_malloc
@@ -2433,6 +2436,17 @@ wasm_runtime_shared_heap_malloc(wasm_module_inst_t module_inst, uint64_t size,
24332436
WASM_RUNTIME_API_EXTERN void
24342437
wasm_runtime_shared_heap_free(wasm_module_inst_t module_inst, uint64_t ptr);
24352438

2439+
/**
2440+
* Iterate over all import functions in the module
2441+
*
2442+
* @param module the module
2443+
* @param callback the callback function
2444+
*/
2445+
WASM_RUNTIME_API_EXTERN
2446+
void
2447+
wasm_runtime_for_each_import_func(const wasm_module_t module,
2448+
wasm_func_type_callback_t callback);
2449+
24362450
#ifdef __cplusplus
24372451
}
24382452
#endif
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Copyright (C) 2019 Intel Corporation. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
cmake_minimum_required (VERSION 3.14)
5+
6+
include(CheckPIESupported)
7+
8+
project (import-func-callback)
9+
10+
set (CMAKE_CXX_STANDARD 17)
11+
12+
################ runtime settings ################
13+
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
14+
if (APPLE)
15+
add_definitions(-DBH_PLATFORM_DARWIN)
16+
endif ()
17+
18+
# Reset default linker flags
19+
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
20+
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
21+
22+
# WAMR features switch
23+
24+
# Set WAMR_BUILD_TARGET, currently values supported:
25+
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
26+
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
27+
if (NOT DEFINED WAMR_BUILD_TARGET)
28+
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
29+
set (WAMR_BUILD_TARGET "AARCH64")
30+
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
31+
set (WAMR_BUILD_TARGET "RISCV64")
32+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
33+
# Build as X86_64 by default in 64-bit platform
34+
set (WAMR_BUILD_TARGET "X86_64")
35+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
36+
# Build as X86_32 by default in 32-bit platform
37+
set (WAMR_BUILD_TARGET "X86_32")
38+
else ()
39+
message(SEND_ERROR "Unsupported build target platform!")
40+
endif ()
41+
endif ()
42+
43+
if (NOT CMAKE_BUILD_TYPE)
44+
set (CMAKE_BUILD_TYPE Debug)
45+
endif ()
46+
47+
set (WAMR_BUILD_INTERP 1)
48+
49+
if (NOT MSVC)
50+
# linker flags
51+
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
52+
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
53+
endif ()
54+
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
55+
if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
56+
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
57+
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
58+
endif ()
59+
endif ()
60+
endif ()
61+
62+
# build out vmlib
63+
set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
64+
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
65+
66+
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
67+
68+
################ application related ################
69+
include_directories(${CMAKE_CURRENT_LIST_DIR}/src)
70+
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
71+
72+
add_executable (import-func-callback src/main.c ${UNCOMMON_SHARED_SOURCE})
73+
74+
check_pie_supported()
75+
set_target_properties (import-func-callback PROPERTIES POSITION_INDEPENDENT_CODE ON)
76+
77+
if (APPLE)
78+
target_link_libraries (import-func-callback vmlib -lm -ldl -lpthread ${LLVM_AVAILABLE_LIBS})
79+
else ()
80+
target_link_libraries (import-func-callback vmlib -lm -ldl -lpthread -lrt ${LLVM_AVAILABLE_LIBS})
81+
endif ()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# "import function callback" sample introduction
2+
3+
This sample demonstrates how to use import function callbacks to handle WebAssembly modules that import external functions. The sample shows how to register callback functions for imported functions and execute them when the WASM module loads these imported functions.
4+
5+
The sample includes a WASM module that imports external functions and a host application that provides callback `import_func_type_callback` for these imported functions.
6+
7+
## Build the sample
8+
9+
```bash
10+
mkdir build
11+
cd build
12+
cmake ..
13+
make
14+
```
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
#include "wasm_export.h"
7+
#include "bh_read_file.h"
8+
#include "bh_getopt.h"
9+
10+
void
11+
print_usage(void)
12+
{
13+
fprintf(stdout, "Options:\r\n");
14+
fprintf(stdout, " -f [path of wasm file] \n");
15+
}
16+
17+
void
18+
import_func_type_callback(const wasm_import_t *import_type,
19+
int32_t import_index)
20+
{
21+
if (strcmp(import_type->name, "import_func1") == 0) {
22+
printf("import_func1 has been found\n");
23+
}
24+
if (strcmp(import_type->name, "import_func2") == 0) {
25+
printf("import_func2 has been found\n");
26+
}
27+
}
28+
29+
int
30+
main(int argc, char *argv_main[])
31+
{
32+
static char global_heap_buf[512 * 1024];
33+
int opt;
34+
wasm_module_t module = NULL;
35+
uint32 buf_size;
36+
char *buffer = NULL;
37+
const char *wasm_path = NULL;
38+
char error_buf[128];
39+
40+
RuntimeInitArgs init_args;
41+
memset(&init_args, 0, sizeof(RuntimeInitArgs));
42+
43+
while ((opt = getopt(argc, argv_main, "hf:")) != -1) {
44+
switch (opt) {
45+
case 'f':
46+
wasm_path = optarg;
47+
break;
48+
case 'h':
49+
print_usage();
50+
return 0;
51+
case '?':
52+
print_usage();
53+
return 0;
54+
}
55+
}
56+
if (optind == 1) {
57+
print_usage();
58+
return 0;
59+
}
60+
init_args.mem_alloc_type = Alloc_With_Pool;
61+
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
62+
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
63+
64+
if (!wasm_runtime_full_init(&init_args)) {
65+
printf("Init runtime environment failed.\n");
66+
return -1;
67+
}
68+
69+
buffer = bh_read_file_to_buffer(wasm_path, &buf_size);
70+
71+
if (!buffer) {
72+
printf("Open wasm app file [%s] failed.\n", wasm_path);
73+
goto fail;
74+
}
75+
76+
module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf,
77+
sizeof(error_buf));
78+
if (!module) {
79+
printf("Load wasm app file [%s] failed.\n", wasm_path);
80+
goto fail;
81+
}
82+
83+
wasm_runtime_for_each_import_func(module, import_func_type_callback);
84+
85+
fail:
86+
if (module)
87+
wasm_runtime_unload(module);
88+
if (buffer)
89+
BH_FREE(buffer);
90+
wasm_runtime_destroy();
91+
return 0;
92+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
extern int
7+
import_func1(int a, int b);
8+
extern int
9+
import_func2(int a);
10+
11+
int
12+
test()
13+
{
14+
int a = import_func1(1, 2);
15+
int b = import_func2(3);
16+
return a + b;
17+
}

0 commit comments

Comments
 (0)