Skip to content

Commit ab0d448

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

File tree

9 files changed

+266
-0
lines changed

9 files changed

+266
-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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,8 @@ 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+
* Example usage (as shown in samples/import-func-callback)
1530+
*
15291531
* @param module the WASM module
15301532
*
15311533
* @return the number of imports (zero for none), or -1 for failure
@@ -1536,6 +1538,8 @@ wasm_runtime_get_import_count(const wasm_module_t module);
15361538
/**
15371539
* Get information about a specific WASM module import
15381540
*
1541+
* Example usage (as shown in samples/import-func-callback)
1542+
*
15391543
* @param module the WASM module
15401544
* @param import_index the desired import index
15411545
* @param import_type the location to store information about the import
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
################ wasm application ################
69+
add_subdirectory(wasm-apps)
70+
71+
################ application related ################
72+
include_directories(${CMAKE_CURRENT_LIST_DIR}/src)
73+
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
74+
75+
add_executable (import-func-callback src/main.c ${UNCOMMON_SHARED_SOURCE})
76+
77+
check_pie_supported()
78+
set_target_properties (import-func-callback PROPERTIES POSITION_INDEPENDENT_CODE ON)
79+
80+
if (APPLE)
81+
target_link_libraries (import-func-callback vmlib -lm -ldl -lpthread ${LLVM_AVAILABLE_LIBS})
82+
else ()
83+
target_link_libraries (import-func-callback vmlib -lm -ldl -lpthread -lrt ${LLVM_AVAILABLE_LIBS})
84+
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: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
typedef void (*wasm_func_type_callback_t)(const wasm_import_t *import_type);
11+
12+
void
13+
import_func_type_callback(const wasm_import_t *import_type)
14+
{
15+
if (strcmp(import_type->name, "import_func1") == 0) {
16+
printf("import_func1 has been found\n");
17+
}
18+
if (strcmp(import_type->name, "import_func2") == 0) {
19+
printf("import_func2 has been found\n");
20+
}
21+
}
22+
23+
/* Iterate over all import functions in the module */
24+
void
25+
wasm_runtime_for_each_import_func(const wasm_module_t module,
26+
wasm_func_type_callback_t callback)
27+
{
28+
int32_t import_count = wasm_runtime_get_import_count(module);
29+
if (import_count <= 0)
30+
return;
31+
if (callback == NULL)
32+
return;
33+
34+
for (int32_t i = 0; i < import_count; ++i) {
35+
wasm_import_t import_type;
36+
wasm_runtime_get_import_type(module, i, &import_type);
37+
38+
if (import_type.kind != WASM_IMPORT_EXPORT_KIND_FUNC) {
39+
continue;
40+
}
41+
42+
callback(&import_type);
43+
}
44+
}
45+
46+
int
47+
main(int argc, char *argv_main[])
48+
{
49+
static char global_heap_buf[512 * 1024];
50+
wasm_module_t module = NULL;
51+
uint32 buf_size;
52+
char *buffer = NULL;
53+
const char *wasm_path = "wasm-apps/test.wasm";
54+
char error_buf[128];
55+
56+
RuntimeInitArgs init_args;
57+
memset(&init_args, 0, sizeof(RuntimeInitArgs));
58+
59+
init_args.mem_alloc_type = Alloc_With_Pool;
60+
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
61+
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
62+
63+
if (!wasm_runtime_full_init(&init_args)) {
64+
printf("Init runtime environment failed.\n");
65+
return -1;
66+
}
67+
printf("Init runtime environment success. %s\n", wasm_path);
68+
buffer = bh_read_file_to_buffer(wasm_path, &buf_size);
69+
70+
if (!buffer) {
71+
printf("Open wasm app file [%s] failed.\n", wasm_path);
72+
goto fail;
73+
}
74+
75+
module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf,
76+
sizeof(error_buf));
77+
if (!module) {
78+
printf("Load wasm app file [%s] failed.\n", wasm_path);
79+
goto fail;
80+
}
81+
82+
wasm_runtime_for_each_import_func(module, import_func_type_callback);
83+
84+
fail:
85+
if (module)
86+
wasm_runtime_unload(module);
87+
if (buffer)
88+
BH_FREE(buffer);
89+
wasm_runtime_destroy();
90+
return 0;
91+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
project(wasm-apps)
6+
7+
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
8+
9+
if (APPLE)
10+
set (HAVE_FLAG_SEARCH_PATHS_FIRST 0)
11+
set (CMAKE_C_LINK_FLAGS "")
12+
set (CMAKE_CXX_LINK_FLAGS "")
13+
endif ()
14+
set(CMAKE_SYSTEM_PROCESSOR wasm32)
15+
16+
if (NOT DEFINED WASI_SDK_DIR)
17+
set (WASI_SDK_DIR "/opt/wasi-sdk")
18+
endif ()
19+
20+
set (CMAKE_C_FLAGS "-nostdlib")
21+
set (CMAKE_C_COMPILER_TARGET "wasm32")
22+
set (CMAKE_C_COMPILER "${WASI_SDK_DIR}/bin/clang")
23+
24+
set (CMAKE_EXE_LINKER_FLAGS
25+
"-Wl,--no-entry, \
26+
-Wl,--export=test, \
27+
-Wl,--allow-undefined"
28+
)
29+
30+
add_executable(test.wasm test.c)
31+
target_link_libraries(test.wasm)
32+
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)