Skip to content

Commit bb4044d

Browse files
committed
feat: add checked API sample with Fibonacci example and CMake configuration
1 parent 6a0e38d commit bb4044d

4 files changed

Lines changed: 205 additions & 0 deletions

File tree

samples/checked-api/CMakeLists.txt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
project(checked_api_sample)
7+
8+
if(NOT CMAKE_BUILD_TYPE)
9+
set(CMAKE_BUILD_TYPE Release)
10+
endif()
11+
12+
set(CMAKE_C_STANDARD 23)
13+
14+
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../cmake)
15+
find_package(WASISDK REQUIRED)
16+
17+
################ runtime settings ################
18+
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
19+
include(CheckPIESupported)
20+
21+
# aot and interp by default
22+
set(WAMR_BUILD_AOT 1)
23+
set(WAMR_BUILD_INTERP 1)
24+
set(WAMR_BUILD_JIT 0)
25+
# wasm32-wasi
26+
set(WAMR_BUILD_LIBC_BUILTIN 0)
27+
set(WAMR_BUILD_LIBC_WASI 1)
28+
# mvp
29+
set(WAMR_BUILD_BULK_MEMORY 1)
30+
set(WAMR_BUILD_REF_TYPES 1)
31+
set(WAMR_BUILD_SIMD 1)
32+
set(WAMR_BUILD_TAIL_CALL 1)
33+
# trap information
34+
set(WAMR_BUILD_DUMP_CALL_STACK 1)
35+
36+
# vmlib
37+
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
38+
include(${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
39+
add_library(vmlib SHARED ${WAMR_RUNTIME_LIB_SOURCE})
40+
target_include_directories(vmlib INTERFACE ${WAMR_ROOT_DIR}/core/iwasm/include)
41+
target_link_libraries (vmlib ${LLVM_AVAILABLE_LIBS} -lm -ldl)
42+
43+
################ host ################
44+
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
45+
add_executable(${PROJECT_NAME} src/demo.c ${UNCOMMON_SHARED_SOURCE})
46+
target_link_libraries(${PROJECT_NAME} vmlib)
47+
48+
################ aot + wasm ################
49+
include(ExternalProject)
50+
ExternalProject_Add(wasm
51+
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps"
52+
CONFIGURE_COMMAND ${CMAKE_COMMAND} -S ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps -B build
53+
-DCMAKE_TOOLCHAIN_FILE=${WASISDK_TOOLCHAIN}
54+
BUILD_COMMAND ${CMAKE_COMMAND} --build build
55+
INSTALL_COMMAND ${CMAKE_COMMAND} --install build --prefix ${CMAKE_CURRENT_BINARY_DIR}
56+
)

samples/checked-api/src/demo.c

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
#include "bh_platform.h"
5+
#include "bh_read_file.h"
6+
#include "wasm_export_checked.h"
7+
8+
#define VERIFY_API_RESULT(callee, result, fail_label) \
9+
do { \
10+
if (result.error_code != 0) { \
11+
printf("%s failed with error code: %d\n", #callee, \
12+
result.error_code); \
13+
goto fail_label; \
14+
} \
15+
} while (0)
16+
17+
int
18+
main(int argc, char *argv_main[])
19+
{
20+
Result api_result;
21+
wasm_module_t module = NULL;
22+
uint32 buf_size, stack_size = 8092, heap_size = 8092;
23+
wasm_module_inst_t module_inst = NULL;
24+
wasm_function_inst_t func = NULL;
25+
wasm_exec_env_t exec_env = NULL;
26+
int ret = EXIT_FAILURE;
27+
28+
RuntimeInitArgs init_args;
29+
// 512Kb
30+
static char global_heap_buf[512 * 1024];
31+
char *wasm_path = "fib.wasm";
32+
char *buffer;
33+
char error_buf[128];
34+
35+
memset(&init_args, 0, sizeof(RuntimeInitArgs));
36+
init_args.mem_alloc_type = Alloc_With_Pool;
37+
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
38+
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
39+
40+
api_result = wasm_runtime_full_init_checked(&init_args);
41+
VERIFY_API_RESULT(wasm_runtime_full_init_checked, api_result, fail);
42+
43+
api_result = wasm_runtime_set_log_level_checked(WASM_LOG_LEVEL_VERBOSE);
44+
VERIFY_API_RESULT(wasm_runtime_set_log_level_checked, api_result,
45+
release_runtime);
46+
47+
buffer = bh_read_file_to_buffer(wasm_path, &buf_size);
48+
if (buffer == NULL) {
49+
printf("Open wasm app file [%s] failed.\n", wasm_path);
50+
goto release_runtime;
51+
}
52+
53+
api_result = wasm_runtime_load_checked((uint8 *)buffer, buf_size, error_buf,
54+
sizeof(error_buf));
55+
VERIFY_API_RESULT(wasm_runtime_load_checked, api_result, release_file);
56+
module = api_result.value.wasm_module_t_value;
57+
58+
api_result = wasm_runtime_instantiate_checked(module, stack_size, heap_size,
59+
error_buf, sizeof(error_buf));
60+
VERIFY_API_RESULT(wasm_runtime_instantiate_checked, api_result,
61+
release_module);
62+
module_inst = api_result.value.wasm_module_inst_t_value;
63+
64+
api_result = wasm_runtime_create_exec_env_checked(module_inst, stack_size);
65+
VERIFY_API_RESULT(wasm_runtime_create_exec_env_checked, api_result,
66+
release_instance);
67+
exec_env = api_result.value.wasm_exec_env_t_value;
68+
69+
api_result = wasm_runtime_lookup_function_checked(module_inst, "fib");
70+
VERIFY_API_RESULT(wasm_runtime_lookup_function_checked, api_result,
71+
release_exec_env);
72+
func = api_result.value.wasm_function_inst_t_value;
73+
74+
wasm_val_t result[1] = { { .kind = WASM_I32 } };
75+
wasm_val_t arguments[1] = {
76+
{ .kind = WASM_I32, .of.i32 = 6 },
77+
};
78+
79+
api_result = wasm_runtime_call_wasm_a_checked(exec_env, func, 1, result, 1,
80+
arguments);
81+
VERIFY_API_RESULT(wasm_runtime_call_wasm_a_checked, api_result,
82+
release_runtime);
83+
printf("Native finished calling wasm function: fib, returned: %d\n",
84+
result[0].of.i32);
85+
86+
ret = EXIT_SUCCESS;
87+
88+
release_exec_env:
89+
wasm_runtime_destroy_exec_env_checked(exec_env);
90+
release_instance:
91+
wasm_runtime_deinstantiate_checked(module_inst);
92+
release_module:
93+
wasm_runtime_unload_checked(module);
94+
release_file:
95+
wasm_runtime_free(buffer);
96+
release_runtime:
97+
wasm_runtime_destroy_checked();
98+
fail:
99+
return ret;
100+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
project(checked_api_wasm_apps)
7+
8+
include(CMakePrintHelpers)
9+
10+
if(NOT CMAKE_BUILD_TYPE)
11+
set(CMAKE_BUILD_TYPE Release)
12+
endif()
13+
14+
################ wasm ################
15+
add_executable(fib fib.c)
16+
set_target_properties(fib PROPERTIES SUFFIX .wasm)
17+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/fib.wasm DESTINATION .)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int
5+
fibonacci(int n)
6+
{
7+
if (n <= 0)
8+
return 0;
9+
10+
if (n == 1)
11+
return 1;
12+
13+
return fibonacci(n - 1) + fibonacci(n - 2);
14+
}
15+
16+
__attribute__((export_name("fib"))) int
17+
fib(int n)
18+
{
19+
int result = fibonacci(n);
20+
printf("fibonacci(%d)=%d\n", n, result);
21+
return result;
22+
}
23+
24+
int
25+
main(int argc, char **argv)
26+
{
27+
int n = atoi(argv[1]);
28+
29+
printf("fibonacci(%d)=%d\n", n, fibonacci(n));
30+
31+
return 0;
32+
}

0 commit comments

Comments
 (0)