Skip to content

Commit 236ea1f

Browse files
committed
Add import functions callback
Signed-off-by: zhenweijin <[email protected]>
1 parent 6253bd1 commit 236ea1f

File tree

8 files changed

+274
-0
lines changed

8 files changed

+274
-0
lines changed

.github/workflows/compilation_on_android_ubuntu.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,14 @@ jobs:
621621
./shared_heap_test
622622
./shared_heap_test --aot
623623
624+
- name: Build Sample [import-func-callback]
625+
run: |
626+
cd samples/import-func-callback
627+
mkdir build && cd build
628+
cmake ..
629+
cmake --build . --config Release --parallel 4
630+
./import-func-callback
631+
624632
test:
625633
needs:
626634
[

.github/workflows/compilation_on_macos.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,3 +422,11 @@ jobs:
422422
cmake --build . --config Debug --parallel 4
423423
./shared_heap_test
424424
./shared_heap_test --aot
425+
426+
- name: Build Sample [import-func-callback]
427+
run: |
428+
cd samples/import-func-callback
429+
mkdir build && cd build
430+
cmake ..
431+
cmake --build . --config Release --parallel 4
432+
./import-func-callback

.github/workflows/nightly_run.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,14 @@ jobs:
568568
./shared_heap_test
569569
./shared_heap_test --aot
570570
571+
- name: Build Sample [import-func-callback]
572+
run: |
573+
cd samples/import-func-callback
574+
mkdir build && cd build
575+
cmake ..
576+
cmake --build . --config Release --parallel 4
577+
./import-func-callback
578+
571579
test:
572580
needs: [build_iwasm, build_llvm_libraries_on_ubuntu, build_wamrc]
573581
runs-on: ${{ matrix.os }}

core/iwasm/include/wasm_export.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,14 @@ wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
15261526
/**
15271527
* Get the number of import items for a WASM module
15281528
*
1529+
* Typical usage scenario:
1530+
* Combine this function with wasm_runtime_get_import_count() to traverse
1531+
* all import items in a module. Use import_type.kind to filter and identify
1532+
* different types of import items.
1533+
*
1534+
* Example usage (as wasm_runtime_for_each_import_func() in
1535+
* samples/import-func-callback)
1536+
*
15291537
* @param module the WASM module
15301538
*
15311539
* @return the number of imports (zero for none), or -1 for failure
@@ -1536,6 +1544,14 @@ wasm_runtime_get_import_count(const wasm_module_t module);
15361544
/**
15371545
* Get information about a specific WASM module import
15381546
*
1547+
* Typical usage scenario:
1548+
* Combine this function with wasm_runtime_get_import_count() to traverse
1549+
* all import items in a module. Use import_type.kind to filter and identify
1550+
* different types of import items.
1551+
*
1552+
* Example usage (as wasm_runtime_for_each_import_func() in
1553+
* samples/import-func-callback)
1554+
*
15391555
* @param module the WASM module
15401556
* @param import_index the desired import index
15411557
* @param import_type the location to store information about the import
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
include(ExternalProject)
8+
9+
project (import-func-callback)
10+
11+
set (CMAKE_CXX_STANDARD 17)
12+
13+
################ runtime settings ################
14+
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
15+
if (APPLE)
16+
add_definitions(-DBH_PLATFORM_DARWIN)
17+
endif ()
18+
19+
# Reset default linker flags
20+
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
21+
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
22+
23+
# WAMR features switch
24+
25+
# Set WAMR_BUILD_TARGET, currently values supported:
26+
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
27+
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
28+
if (NOT DEFINED WAMR_BUILD_TARGET)
29+
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
30+
set (WAMR_BUILD_TARGET "AARCH64")
31+
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
32+
set (WAMR_BUILD_TARGET "RISCV64")
33+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
34+
# Build as X86_64 by default in 64-bit platform
35+
set (WAMR_BUILD_TARGET "X86_64")
36+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
37+
# Build as X86_32 by default in 32-bit platform
38+
set (WAMR_BUILD_TARGET "X86_32")
39+
else ()
40+
message(SEND_ERROR "Unsupported build target platform!")
41+
endif ()
42+
endif ()
43+
44+
if (NOT CMAKE_BUILD_TYPE)
45+
set (CMAKE_BUILD_TYPE Debug)
46+
endif ()
47+
48+
set (WAMR_BUILD_INTERP 1)
49+
50+
if (NOT MSVC)
51+
# linker flags
52+
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
53+
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
54+
endif ()
55+
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
56+
if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
57+
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
58+
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
59+
endif ()
60+
endif ()
61+
endif ()
62+
63+
# build out vmlib
64+
set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
65+
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
66+
67+
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
68+
69+
################ wasm application ################
70+
if (NOT DEFINED WASI_SDK_DIR)
71+
set (WASI_SDK_DIR "/opt/wasi-sdk")
72+
endif ()
73+
74+
ExternalProject_Add(wasm_app
75+
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps
76+
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/wasm-apps
77+
CONFIGURE_COMMAND ""
78+
BUILD_COMMAND ${CMAKE_COMMAND} -E env
79+
${WASI_SDK_DIR}/bin/clang
80+
-nostdlib
81+
--target=wasm32
82+
-Wl,--no-entry
83+
-Wl,--export=test
84+
-Wl,--allow-undefined
85+
-o ${CMAKE_CURRENT_BINARY_DIR}/wasm-apps/test.wasm
86+
${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps/test.c
87+
BUILD_ALWAYS TRUE
88+
INSTALL_COMMAND ""
89+
)
90+
91+
################ application related ################
92+
include_directories(${CMAKE_CURRENT_LIST_DIR}/src)
93+
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
94+
95+
add_executable (import-func-callback src/main.c ${UNCOMMON_SHARED_SOURCE})
96+
97+
add_dependencies(import-func-callback wasm_app)
98+
99+
check_pie_supported()
100+
set_target_properties (import-func-callback PROPERTIES POSITION_INDEPENDENT_CODE ON)
101+
102+
if (APPLE)
103+
target_link_libraries (import-func-callback vmlib -lm -ldl -lpthread ${LLVM_AVAILABLE_LIBS})
104+
else ()
105+
target_link_libraries (import-func-callback vmlib -lm -ldl -lpthread -lrt ${LLVM_AVAILABLE_LIBS})
106+
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 and run the sample
8+
9+
```bash
10+
mkdir build && cd build
11+
cmake ..
12+
cmake --build . --config Release
13+
./import-func-callback
14+
```
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
#include "assert.h"
10+
11+
typedef void (*wasm_func_type_callback_t)(const wasm_import_t *import_type);
12+
13+
const char *import_func_names[] = { "import_func1", "import_func2" };
14+
15+
void
16+
import_func_type_callback(const wasm_import_t *import_type)
17+
{
18+
int ret = 0;
19+
for (uint32_t i = 0;
20+
i < sizeof(import_func_names) / sizeof(import_func_names[0]); i++) {
21+
if (strcmp(import_type->name, import_func_names[i]) == 0) {
22+
ret = 1;
23+
break;
24+
}
25+
}
26+
assert(ret == 1);
27+
return;
28+
}
29+
30+
/* Iterate over all import functions in the module */
31+
void
32+
wasm_runtime_for_each_import_func(const wasm_module_t module,
33+
wasm_func_type_callback_t callback)
34+
{
35+
int32_t import_count = wasm_runtime_get_import_count(module);
36+
if (import_count <= 0)
37+
return;
38+
if (callback == NULL)
39+
return;
40+
41+
for (int32_t i = 0; i < import_count; ++i) {
42+
wasm_import_t import_type;
43+
wasm_runtime_get_import_type(module, i, &import_type);
44+
45+
if (import_type.kind != WASM_IMPORT_EXPORT_KIND_FUNC) {
46+
continue;
47+
}
48+
49+
callback(&import_type);
50+
}
51+
}
52+
53+
int
54+
main(int argc, char *argv_main[])
55+
{
56+
static char global_heap_buf[512 * 1024];
57+
wasm_module_t module = NULL;
58+
uint32 buf_size;
59+
char *buffer = NULL;
60+
const char *wasm_path = "wasm-apps/test.wasm";
61+
char error_buf[128];
62+
63+
RuntimeInitArgs init_args;
64+
memset(&init_args, 0, sizeof(RuntimeInitArgs));
65+
66+
init_args.mem_alloc_type = Alloc_With_Pool;
67+
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
68+
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
69+
70+
if (!wasm_runtime_full_init(&init_args)) {
71+
printf("Init runtime environment failed.\n");
72+
return -1;
73+
}
74+
buffer = bh_read_file_to_buffer(wasm_path, &buf_size);
75+
76+
if (!buffer) {
77+
printf("Open wasm app file [%s] failed.\n", wasm_path);
78+
goto fail;
79+
}
80+
81+
module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf,
82+
sizeof(error_buf));
83+
if (!module) {
84+
printf("Load wasm app file [%s] failed.\n", wasm_path);
85+
goto fail;
86+
}
87+
88+
wasm_runtime_for_each_import_func(module, import_func_type_callback);
89+
90+
fail:
91+
if (module)
92+
wasm_runtime_unload(module);
93+
if (buffer)
94+
BH_FREE(buffer);
95+
wasm_runtime_destroy();
96+
return 0;
97+
}
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)