Skip to content

Commit 0f1ce9e

Browse files
authored
Implement wasm-c-api frame/trap APIs for AOT mode (#663)
And update CI workflow to build/cache llvm and run wasm-c-api samples.
1 parent b554a9d commit 0f1ce9e

File tree

10 files changed

+158
-56
lines changed

10 files changed

+158
-56
lines changed

.github/workflows/linux.yml

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ name: Linux
66
# Controls when the action will run. Triggers the workflow on push or pull request
77
# events but only for the main branch
88
on:
9+
# triggers on every branch
910
push:
10-
branches: [ main ]
11+
paths-ignore:
12+
- 'doc/**'
13+
# triggers on every PR
1114
pull_request:
12-
branches: [ main ]
15+
paths-ignore:
16+
- 'doc/**'
1317

1418
jobs:
15-
1619
build:
1720
runs-on: ${{ matrix.os }}
1821
strategy:
@@ -84,27 +87,80 @@ jobs:
8487
cmake .. -DWAMR_BUILD_CUSTOM_NAME_SECTION=1
8588
make
8689
cd .. && rm -rf build
90+
- name: Cache LLVM libraries
91+
uses: actions/cache@v2
92+
id: cache_llvm
93+
env:
94+
cache-name: llvm_libraries
95+
with:
96+
path: ./core/deps/llvm
97+
key: ${{ runner.os }}-build-${{env.cache-name}}
98+
restore-keys: ${{ runner.os }}-build-${{env.cache-name}}
99+
- name: Build llvm and clang from source
100+
if: steps.cache_llvm.outputs.cache-hit != 'true'
101+
run: |
102+
cd wamr-compiler
103+
./build_llvm.sh
104+
- name: Build wamrc
105+
run: |
106+
cd wamr-compiler
107+
mkdir build && cd build
108+
cmake ..
109+
make
110+
cd ..
87111
- name: download and install wasi-sdk
88112
run: |
89113
cd /opt
90-
wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-8/wasi-sdk-8.0-linux.tar.gz
91-
tar -xzf wasi-sdk-8.0-linux.tar.gz
92-
mv wasi-sdk-8.0 wasi-sdk
114+
wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-12/wasi-sdk-12.0-linux.tar.gz
115+
tar -xzf wasi-sdk-12.0-linux.tar.gz
116+
mv wasi-sdk-12.0 wasi-sdk
117+
rm wasi-sdk-12.0-linux.tar.gz
93118
- name: download and install wabt
94119
run: |
95120
cd /opt
96-
wget https://github.com/WebAssembly/wabt/releases/download/1.0.19/wabt-1.0.19-ubuntu.tar.gz
97-
tar -xzf wabt-1.0.19-ubuntu.tar.gz
98-
mv wabt-1.0.19 wabt
121+
wget https://github.com/WebAssembly/wabt/releases/download/1.0.23/wabt-1.0.23-ubuntu.tar.gz
122+
tar -xzf wabt-1.0.23-ubuntu.tar.gz
123+
mv wabt-1.0.23 wabt
124+
rm wabt-1.0.23-ubuntu.tar.gz
99125
- name: Build Sample [wasm-c-api]
100126
run: |
101127
cd samples/wasm-c-api
102128
mkdir build && cd build
103129
cmake ..
104130
make
131+
./callback
132+
./callback_chain
133+
./global
105134
./hello
135+
./reflect
136+
./trap
137+
cd .. && rm -r build
138+
- name: Build Sample [wasm-c-api] [Jit]
139+
run: |
140+
cd samples/wasm-c-api
141+
mkdir build && cd build
142+
cmake -DWAMR_BUILD_INTERP=0 -DWAMR_BUILD_JIT=1 -DWAMR_BUILD_AOT=1 ..
143+
make
144+
./callback
145+
./callback_chain
106146
./global
147+
./hello
148+
./reflect
149+
./trap
150+
cd .. && rm -r build
151+
- name: Build Sample [wasm-c-api] [Aot]
152+
run: |
153+
cd samples/wasm-c-api
154+
mkdir build && cd build
155+
cmake -DWAMR_BUILD_INTERP=0 -DWAMR_BUILD_JIT=0 -DWAMR_BUILD_AOT=1 ..
156+
make
107157
./callback
158+
./callback_chain
159+
./global
160+
./hello
161+
./reflect
162+
./trap
163+
cd .. && rm -r build
108164
- name: Build Sample [basic]
109165
run: |
110166
cd samples/basic

core/iwasm/aot/aot_runtime.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,13 @@ aot_instantiate(AOTModule *module, bool is_sub_inst,
10611061
}
10621062
#endif
10631063

1064+
#if WASM_ENABLE_DUMP_CALL_STACK != 0
1065+
if (!(module_inst->frames.ptr =
1066+
runtime_malloc(sizeof(Vector), error_buf, error_buf_size))) {
1067+
goto fail;
1068+
}
1069+
#endif
1070+
10641071
/* Execute __post_instantiate function and start function*/
10651072
if (!execute_post_inst_function(module_inst)
10661073
|| !execute_start_function(module_inst)) {
@@ -1130,6 +1137,14 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst)
11301137
wasm_runtime_free(module_inst->func_perf_profilings.ptr);
11311138
#endif
11321139

1140+
#if WASM_ENABLE_DUMP_CALL_STACK != 0
1141+
if (module_inst->frames.ptr) {
1142+
bh_vector_destroy(module_inst->frames.ptr);
1143+
wasm_runtime_free(module_inst->frames.ptr);
1144+
module_inst->frames.ptr = NULL;
1145+
}
1146+
#endif
1147+
11331148
if (module_inst->memories.ptr)
11341149
memories_deinstantiate(module_inst);
11351150

@@ -2902,7 +2917,8 @@ aot_free_frame(WASMExecEnv *exec_env)
29022917
void
29032918
aot_dump_call_stack(WASMExecEnv *exec_env)
29042919
{
2905-
AOTFrame *cur_frame = (AOTFrame *)exec_env->cur_frame;
2920+
AOTFrame *cur_frame = (AOTFrame *)exec_env->cur_frame,
2921+
*first_frame = cur_frame;
29062922
AOTModuleInstance *module_inst =
29072923
(AOTModuleInstance *)exec_env->module_inst;
29082924
const char *func_name;
@@ -2925,6 +2941,29 @@ aot_dump_call_stack(WASMExecEnv *exec_env)
29252941
n++;
29262942
}
29272943
os_printf("\n");
2944+
2945+
/* release previous stack frames and create new ones */
2946+
if (!bh_vector_destroy(module_inst->frames.ptr)
2947+
|| !bh_vector_init(module_inst->frames.ptr, n,
2948+
sizeof(WASMCApiFrame))) {
2949+
return;
2950+
}
2951+
2952+
cur_frame = first_frame;
2953+
while (cur_frame) {
2954+
WASMCApiFrame frame = { 0 };
2955+
frame.instance = module_inst;
2956+
frame.module_offset = 0;
2957+
frame.func_index = cur_frame->func_index;
2958+
frame.func_offset = 0;
2959+
2960+
if (!bh_vector_append(module_inst->frames.ptr, &frame)) {
2961+
bh_vector_destroy(module_inst->frames.ptr);
2962+
return;
2963+
}
2964+
2965+
cur_frame = cur_frame->prev_frame;
2966+
}
29282967
}
29292968
#endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
29302969

core/iwasm/aot/aot_runtime.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,11 @@ typedef struct AOTModuleInstance {
353353
uint32 llvm_stack;
354354
uint32 default_wasm_stack_size;
355355

356+
uint32 _padding;
357+
/* store stacktrace information */
358+
AOTPointer frames;
356359
/* reserved */
357-
uint32 reserved[9];
360+
uint32 reserved[6];
358361

359362
/*
360363
* +------------------------------+ <-- memories.ptr

core/iwasm/common/wasm_c_api.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,12 @@ wasm_trap_new_internal(WASMModuleInstanceCommon *inst_comm_rt,
14351435
trap->frames = ((WASMModuleInstance *)inst_comm_rt)->frames;
14361436
}
14371437
#endif
1438+
1439+
#if WASM_ENABLE_AOT != 0
1440+
if (inst_comm_rt->module_type == Wasm_Module_AoT) {
1441+
trap->frames = ((AOTModuleInstance *)inst_comm_rt)->frames.ptr;
1442+
}
1443+
#endif
14381444
#endif /* WASM_ENABLE_DUMP_CALL_STACK != 0 */
14391445

14401446
/* allow a NULL frames list */

core/iwasm/common/wasm_c_api_internal.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,6 @@ struct wasm_ref_t {
8686
uint32 obj;
8787
};
8888

89-
struct wasm_frame_t {
90-
wasm_instance_t *instance;
91-
uint32 module_offset;
92-
uint32 func_index;
93-
uint32 func_offset;
94-
};
95-
9689
struct wasm_trap_t {
9790
wasm_byte_vec_t *message;
9891
Vector *frames;

core/iwasm/common/wasm_runtime_common.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,14 @@ typedef struct WASMMemoryInstanceCommon {
336336
typedef package_type_t PackageType;
337337
typedef wasm_section_t WASMSection, AOTSection;
338338

339+
typedef struct wasm_frame_t {
340+
/* wasm_instance_t */
341+
void *instance;
342+
uint32 module_offset;
343+
uint32 func_index;
344+
uint32 func_offset;
345+
} WASMCApiFrame;
346+
339347
/* See wasm_export.h for description */
340348
WASM_RUNTIME_API_EXTERN bool
341349
wasm_runtime_init(void);

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,13 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
11591159
}
11601160
#endif
11611161

1162+
#if WASM_ENABLE_DUMP_CALL_STACK != 0
1163+
if (!(module_inst->frames = runtime_malloc((uint64)sizeof(Vector),
1164+
error_buf, error_buf_size))) {
1165+
goto fail;
1166+
}
1167+
#endif
1168+
11621169
/* Instantiate global firstly to get the mutable data size */
11631170
global_count = module->import_global_count + module->global_count;
11641171
if (global_count
@@ -2434,13 +2441,6 @@ wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env)
24342441
*cur_frame = wasm_exec_env_get_cur_frame(exec_env);
24352442
uint32 n = 0;
24362443

2437-
/* release previous stack frame */
2438-
if (module_inst->frames) {
2439-
bh_vector_destroy(module_inst->frames);
2440-
wasm_runtime_free(module_inst->frames);
2441-
module_inst->frames = NULL;
2442-
}
2443-
24442444
/* count frames includes a function */
24452445
first_frame = cur_frame;
24462446
while (cur_frame) {
@@ -2450,22 +2450,17 @@ wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env)
24502450
cur_frame = cur_frame->prev_frame;
24512451
}
24522452

2453-
if (!(module_inst->frames = runtime_malloc(
2454-
(uint64)sizeof(Vector), module_inst->cur_exception, 128))) {
2455-
return;
2456-
}
2457-
2458-
if (!bh_vector_init(module_inst->frames, n, sizeof(struct WASMFrame))) {
2459-
wasm_runtime_free(module_inst->frames);
2460-
module_inst->frames = NULL;
2453+
/* release previous stack frames and create new ones */
2454+
if (!bh_vector_destroy(module_inst->frames)
2455+
|| !bh_vector_init(module_inst->frames, n, sizeof(WASMCApiFrame))) {
24612456
return;
24622457
}
24632458

24642459
cur_frame = first_frame;
24652460
n = 0;
24662461
os_printf("\n");
24672462
while (cur_frame) {
2468-
struct WASMFrame frame = { 0 };
2463+
WASMCApiFrame frame = { 0 };
24692464
WASMFunctionInstance *func_inst = cur_frame->function;
24702465
const char *func_name = NULL;
24712466

core/iwasm/interpreter/wasm_runtime.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,6 @@ typedef struct WASMExportMemInstance {
150150
} WASMExportMemInstance;
151151
#endif
152152

153-
#if WASM_ENABLE_DUMP_CALL_STACK != 0
154-
struct WASMFrame {
155-
void *instance;
156-
uint32 module_offset;
157-
uint32 func_index;
158-
uint32 func_offset;
159-
};
160-
#endif
161-
162153
struct WASMModuleInstance {
163154
/* Module instance type, for module instance loaded from
164155
WASM bytecode binary, this field is Wasm_Module_Bytecode;

samples/wasm-c-api/CMakeLists.txt

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ if(NOT DEFINED WAMR_BUILD_INTERP)
3131
endif()
3232

3333
if(NOT DEFINED WAMR_BUILD_AOT)
34-
set(WAMR_BUILD_AOT 1)
34+
set(WAMR_BUILD_AOT 0)
3535
endif()
3636

3737
if(NOT DEFINED WAMR_BUILD_JIT)
@@ -62,7 +62,6 @@ if (NOT MSVC)
6262
endif()
6363
# build out vmlib
6464
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
65-
set(WAMRC ${WAMR_ROOT_DIR}/wamr-compiler/build/wamrc)
6665
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
6766

6867
add_library(vmlib STATIC ${WAMR_RUNTIME_LIB_SOURCE})
@@ -84,6 +83,19 @@ if(NOT WAT2WASM)
8483
message(SEND_ERROR "can not find wat2wasm")
8584
endif()
8685

86+
if(${WAMR_BUILD_AOT} EQUAL 1)
87+
## locate wamrc
88+
find_program(WAMRC
89+
wamrc
90+
PATHS ${WAMR_ROOT_DIR}/wamr-compiler/build/
91+
)
92+
93+
if(NOT WAMRC)
94+
message(SEND_ERROR "can not find wamrc. refer to \
95+
https://github.com/bytecodealliance/wasm-micro-runtime#build-wamrc-aot-compiler"
96+
)
97+
endif()
98+
endif()
8799
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
88100

89101
set(MM_UTIL src/utils/multi_module_utils.c)
@@ -120,17 +132,15 @@ foreach(EX ${EXAMPLES})
120132

121133
# generate .aot file
122134
if(${WAMR_BUILD_AOT} EQUAL 1)
123-
if(EXISTS ${WAMRC})
124-
add_custom_target(${EX}_AOT
125-
COMMAND ${WAMRC} -o ${PROJECT_BINARY_DIR}/${EX}.aot
126-
${PROJECT_BINARY_DIR}/${EX}.wasm
127-
DEPENDS ${EX}_WASM
128-
BYPRODUCTS ${PROJECT_BINARY_DIR}/${EX}.aot
129-
VERBATIM
130-
COMMENT "generate a aot file ${PROJECT_BINARY_DIR}/${EX}.aot"
131-
)
132-
add_dependencies(${EX} ${EX}_AOT)
133-
endif()
135+
add_custom_target(${EX}_AOT
136+
COMMAND ${WAMRC} -o ${PROJECT_BINARY_DIR}/${EX}.aot
137+
${PROJECT_BINARY_DIR}/${EX}.wasm
138+
DEPENDS ${EX}_WASM
139+
BYPRODUCTS ${PROJECT_BINARY_DIR}/${EX}.aot
140+
VERBATIM
141+
COMMENT "generate a aot file ${PROJECT_BINARY_DIR}/${EX}.aot"
142+
)
143+
add_dependencies(${EX} ${EX}_AOT)
134144
endif()
135145
endforeach()
136146

samples/wasm-c-api/src/hello.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,4 @@ int main(int argc, const char* argv[]) {
112112
printf("Done.\n");
113113
return 0;
114114
}
115+

0 commit comments

Comments
 (0)