mirror of
				https://github.com/bytecodealliance/wasm-micro-runtime.git
				synced 2025-10-31 05:11:19 +00:00 
			
		
		
		
	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.
This commit is contained in:
		
							parent
							
								
									b554a9d05d
								
							
						
					
					
						commit
						0f1ce9ef3d
					
				
							
								
								
									
										78
									
								
								.github/workflows/linux.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										78
									
								
								.github/workflows/linux.yml
									
									
									
									
										vendored
									
									
								
							|  | @ -6,13 +6,16 @@ name: Linux | ||||||
| # Controls when the action will run. Triggers the workflow on push or pull request | # Controls when the action will run. Triggers the workflow on push or pull request | ||||||
| # events but only for the main branch | # events but only for the main branch | ||||||
| on: | on: | ||||||
|  |   # triggers on every branch | ||||||
|   push: |   push: | ||||||
|     branches: [ main ] |     paths-ignore: | ||||||
|  |     - 'doc/**' | ||||||
|  |   # triggers on every PR | ||||||
|   pull_request: |   pull_request: | ||||||
|     branches: [ main ] |     paths-ignore: | ||||||
|  |     - 'doc/**' | ||||||
| 
 | 
 | ||||||
| jobs: | jobs: | ||||||
| 
 |  | ||||||
|   build: |   build: | ||||||
|     runs-on: ${{ matrix.os }} |     runs-on: ${{ matrix.os }} | ||||||
|     strategy: |     strategy: | ||||||
|  | @ -84,27 +87,80 @@ jobs: | ||||||
|         cmake .. -DWAMR_BUILD_CUSTOM_NAME_SECTION=1 |         cmake .. -DWAMR_BUILD_CUSTOM_NAME_SECTION=1 | ||||||
|         make |         make | ||||||
|         cd .. && rm -rf build |         cd .. && rm -rf build | ||||||
|  |     - name: Cache LLVM libraries | ||||||
|  |       uses: actions/cache@v2 | ||||||
|  |       id: cache_llvm | ||||||
|  |       env: | ||||||
|  |         cache-name: llvm_libraries | ||||||
|  |       with: | ||||||
|  |         path: ./core/deps/llvm | ||||||
|  |         key: ${{ runner.os }}-build-${{env.cache-name}} | ||||||
|  |         restore-keys: ${{ runner.os }}-build-${{env.cache-name}} | ||||||
|  |     - name: Build llvm and clang from source | ||||||
|  |       if: steps.cache_llvm.outputs.cache-hit != 'true' | ||||||
|  |       run: | | ||||||
|  |         cd wamr-compiler | ||||||
|  |         ./build_llvm.sh | ||||||
|  |     - name: Build wamrc | ||||||
|  |       run: | | ||||||
|  |         cd wamr-compiler | ||||||
|  |         mkdir build && cd build | ||||||
|  |         cmake .. | ||||||
|  |         make | ||||||
|  |         cd .. | ||||||
|     - name: download and install wasi-sdk |     - name: download and install wasi-sdk | ||||||
|       run: | |       run: | | ||||||
|         cd /opt |         cd /opt | ||||||
|         wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-8/wasi-sdk-8.0-linux.tar.gz |         wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-12/wasi-sdk-12.0-linux.tar.gz | ||||||
|         tar -xzf wasi-sdk-8.0-linux.tar.gz |         tar -xzf wasi-sdk-12.0-linux.tar.gz | ||||||
|         mv wasi-sdk-8.0 wasi-sdk |         mv wasi-sdk-12.0 wasi-sdk | ||||||
|  |         rm wasi-sdk-12.0-linux.tar.gz | ||||||
|     - name: download and install wabt |     - name: download and install wabt | ||||||
|       run: | |       run: | | ||||||
|         cd /opt |         cd /opt | ||||||
|         wget https://github.com/WebAssembly/wabt/releases/download/1.0.19/wabt-1.0.19-ubuntu.tar.gz |         wget https://github.com/WebAssembly/wabt/releases/download/1.0.23/wabt-1.0.23-ubuntu.tar.gz | ||||||
|         tar -xzf wabt-1.0.19-ubuntu.tar.gz |         tar -xzf wabt-1.0.23-ubuntu.tar.gz | ||||||
|         mv wabt-1.0.19 wabt |         mv wabt-1.0.23 wabt | ||||||
|  |         rm wabt-1.0.23-ubuntu.tar.gz | ||||||
|     - name: Build Sample [wasm-c-api] |     - name: Build Sample [wasm-c-api] | ||||||
|       run: | |       run: | | ||||||
|         cd samples/wasm-c-api |         cd samples/wasm-c-api | ||||||
|         mkdir build && cd build |         mkdir build && cd build | ||||||
|         cmake .. |         cmake .. | ||||||
|         make |         make | ||||||
|         ./hello |  | ||||||
|         ./global |  | ||||||
|         ./callback |         ./callback | ||||||
|  |         ./callback_chain | ||||||
|  |         ./global | ||||||
|  |         ./hello | ||||||
|  |         ./reflect | ||||||
|  |         ./trap | ||||||
|  |         cd .. && rm -r build | ||||||
|  |     - name: Build Sample [wasm-c-api] [Jit] | ||||||
|  |       run: | | ||||||
|  |         cd samples/wasm-c-api | ||||||
|  |         mkdir build && cd build | ||||||
|  |         cmake -DWAMR_BUILD_INTERP=0 -DWAMR_BUILD_JIT=1 -DWAMR_BUILD_AOT=1 .. | ||||||
|  |         make | ||||||
|  |         ./callback | ||||||
|  |         ./callback_chain | ||||||
|  |         ./global | ||||||
|  |         ./hello | ||||||
|  |         ./reflect | ||||||
|  |         ./trap | ||||||
|  |         cd .. && rm -r build | ||||||
|  |     - name: Build Sample [wasm-c-api] [Aot] | ||||||
|  |       run: | | ||||||
|  |         cd samples/wasm-c-api | ||||||
|  |         mkdir build && cd build | ||||||
|  |         cmake -DWAMR_BUILD_INTERP=0 -DWAMR_BUILD_JIT=0 -DWAMR_BUILD_AOT=1 .. | ||||||
|  |         make | ||||||
|  |         ./callback | ||||||
|  |         ./callback_chain | ||||||
|  |         ./global | ||||||
|  |         ./hello | ||||||
|  |         ./reflect | ||||||
|  |         ./trap | ||||||
|  |         cd .. && rm -r build | ||||||
|     - name: Build Sample [basic] |     - name: Build Sample [basic] | ||||||
|       run: | |       run: | | ||||||
|         cd samples/basic |         cd samples/basic | ||||||
|  |  | ||||||
|  | @ -1061,6 +1061,13 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, | ||||||
|     } |     } | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
|  | #if WASM_ENABLE_DUMP_CALL_STACK != 0 | ||||||
|  |     if (!(module_inst->frames.ptr = | ||||||
|  |             runtime_malloc(sizeof(Vector), error_buf, error_buf_size))) { | ||||||
|  |         goto fail; | ||||||
|  |     } | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
|     /* Execute __post_instantiate function and start function*/ |     /* Execute __post_instantiate function and start function*/ | ||||||
|     if (!execute_post_inst_function(module_inst) |     if (!execute_post_inst_function(module_inst) | ||||||
|         || !execute_start_function(module_inst)) { |         || !execute_start_function(module_inst)) { | ||||||
|  | @ -1130,6 +1137,14 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst) | ||||||
|         wasm_runtime_free(module_inst->func_perf_profilings.ptr); |         wasm_runtime_free(module_inst->func_perf_profilings.ptr); | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
|  | #if WASM_ENABLE_DUMP_CALL_STACK != 0 | ||||||
|  |     if (module_inst->frames.ptr) { | ||||||
|  |         bh_vector_destroy(module_inst->frames.ptr); | ||||||
|  |         wasm_runtime_free(module_inst->frames.ptr); | ||||||
|  |         module_inst->frames.ptr = NULL; | ||||||
|  |     } | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
|     if (module_inst->memories.ptr) |     if (module_inst->memories.ptr) | ||||||
|         memories_deinstantiate(module_inst); |         memories_deinstantiate(module_inst); | ||||||
| 
 | 
 | ||||||
|  | @ -2902,7 +2917,8 @@ aot_free_frame(WASMExecEnv *exec_env) | ||||||
| void | void | ||||||
| aot_dump_call_stack(WASMExecEnv *exec_env) | aot_dump_call_stack(WASMExecEnv *exec_env) | ||||||
| { | { | ||||||
|     AOTFrame *cur_frame = (AOTFrame *)exec_env->cur_frame; |     AOTFrame *cur_frame = (AOTFrame *)exec_env->cur_frame, | ||||||
|  |              *first_frame = cur_frame; | ||||||
|     AOTModuleInstance *module_inst = |     AOTModuleInstance *module_inst = | ||||||
|             (AOTModuleInstance *)exec_env->module_inst; |             (AOTModuleInstance *)exec_env->module_inst; | ||||||
|     const char *func_name; |     const char *func_name; | ||||||
|  | @ -2925,6 +2941,29 @@ aot_dump_call_stack(WASMExecEnv *exec_env) | ||||||
|         n++; |         n++; | ||||||
|     } |     } | ||||||
|     os_printf("\n"); |     os_printf("\n"); | ||||||
|  | 
 | ||||||
|  |     /* release previous stack frames and create new ones */ | ||||||
|  |     if (!bh_vector_destroy(module_inst->frames.ptr) | ||||||
|  |         || !bh_vector_init(module_inst->frames.ptr, n, | ||||||
|  |                            sizeof(WASMCApiFrame))) { | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     cur_frame = first_frame; | ||||||
|  |     while (cur_frame) { | ||||||
|  |         WASMCApiFrame frame = { 0 }; | ||||||
|  |         frame.instance = module_inst; | ||||||
|  |         frame.module_offset = 0; | ||||||
|  |         frame.func_index = cur_frame->func_index; | ||||||
|  |         frame.func_offset = 0; | ||||||
|  | 
 | ||||||
|  |         if (!bh_vector_append(module_inst->frames.ptr, &frame)) { | ||||||
|  |             bh_vector_destroy(module_inst->frames.ptr); | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         cur_frame = cur_frame->prev_frame; | ||||||
|  |     } | ||||||
| } | } | ||||||
| #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */ | #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */ | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -353,8 +353,11 @@ typedef struct AOTModuleInstance { | ||||||
|     uint32 llvm_stack; |     uint32 llvm_stack; | ||||||
|     uint32 default_wasm_stack_size; |     uint32 default_wasm_stack_size; | ||||||
| 
 | 
 | ||||||
|  |     uint32 _padding; | ||||||
|  |     /* store stacktrace information */ | ||||||
|  |     AOTPointer frames; | ||||||
|     /* reserved */ |     /* reserved */ | ||||||
|     uint32 reserved[9]; |     uint32 reserved[6]; | ||||||
| 
 | 
 | ||||||
|    /*
 |    /*
 | ||||||
|     * +------------------------------+ <-- memories.ptr |     * +------------------------------+ <-- memories.ptr | ||||||
|  |  | ||||||
|  | @ -1435,6 +1435,12 @@ wasm_trap_new_internal(WASMModuleInstanceCommon *inst_comm_rt, | ||||||
|         trap->frames = ((WASMModuleInstance *)inst_comm_rt)->frames; |         trap->frames = ((WASMModuleInstance *)inst_comm_rt)->frames; | ||||||
|     } |     } | ||||||
| #endif | #endif | ||||||
|  | 
 | ||||||
|  | #if WASM_ENABLE_AOT != 0 | ||||||
|  |     if (inst_comm_rt->module_type == Wasm_Module_AoT) { | ||||||
|  |         trap->frames = ((AOTModuleInstance *)inst_comm_rt)->frames.ptr; | ||||||
|  |     } | ||||||
|  | #endif | ||||||
| #endif /* WASM_ENABLE_DUMP_CALL_STACK != 0 */ | #endif /* WASM_ENABLE_DUMP_CALL_STACK != 0 */ | ||||||
| 
 | 
 | ||||||
|     /* allow a NULL frames list */ |     /* allow a NULL frames list */ | ||||||
|  |  | ||||||
|  | @ -86,13 +86,6 @@ struct wasm_ref_t { | ||||||
|     uint32 obj; |     uint32 obj; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| struct wasm_frame_t { |  | ||||||
|     wasm_instance_t *instance; |  | ||||||
|     uint32 module_offset; |  | ||||||
|     uint32 func_index; |  | ||||||
|     uint32 func_offset; |  | ||||||
| }; |  | ||||||
| 
 |  | ||||||
| struct wasm_trap_t { | struct wasm_trap_t { | ||||||
|     wasm_byte_vec_t *message; |     wasm_byte_vec_t *message; | ||||||
|     Vector *frames; |     Vector *frames; | ||||||
|  |  | ||||||
|  | @ -336,6 +336,14 @@ typedef struct WASMMemoryInstanceCommon { | ||||||
| typedef package_type_t PackageType; | typedef package_type_t PackageType; | ||||||
| typedef wasm_section_t WASMSection, AOTSection; | typedef wasm_section_t WASMSection, AOTSection; | ||||||
| 
 | 
 | ||||||
|  | typedef struct wasm_frame_t { | ||||||
|  |     /*  wasm_instance_t */ | ||||||
|  |     void *instance; | ||||||
|  |     uint32 module_offset; | ||||||
|  |     uint32 func_index; | ||||||
|  |     uint32 func_offset; | ||||||
|  | } WASMCApiFrame; | ||||||
|  | 
 | ||||||
| /* See wasm_export.h for description */ | /* See wasm_export.h for description */ | ||||||
| WASM_RUNTIME_API_EXTERN bool | WASM_RUNTIME_API_EXTERN bool | ||||||
| wasm_runtime_init(void); | wasm_runtime_init(void); | ||||||
|  |  | ||||||
|  | @ -1159,6 +1159,13 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, | ||||||
|     } |     } | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
|  | #if WASM_ENABLE_DUMP_CALL_STACK != 0 | ||||||
|  |     if (!(module_inst->frames = runtime_malloc((uint64)sizeof(Vector), | ||||||
|  |                                                error_buf, error_buf_size))) { | ||||||
|  |         goto fail; | ||||||
|  |     } | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
|     /* Instantiate global firstly to get the mutable data size */ |     /* Instantiate global firstly to get the mutable data size */ | ||||||
|     global_count = module->import_global_count + module->global_count; |     global_count = module->import_global_count + module->global_count; | ||||||
|     if (global_count |     if (global_count | ||||||
|  | @ -2434,13 +2441,6 @@ wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env) | ||||||
|       *cur_frame = wasm_exec_env_get_cur_frame(exec_env); |       *cur_frame = wasm_exec_env_get_cur_frame(exec_env); | ||||||
|     uint32 n = 0; |     uint32 n = 0; | ||||||
| 
 | 
 | ||||||
|     /* release previous stack frame */ |  | ||||||
|     if (module_inst->frames) { |  | ||||||
|         bh_vector_destroy(module_inst->frames); |  | ||||||
|         wasm_runtime_free(module_inst->frames); |  | ||||||
|         module_inst->frames = NULL; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     /* count frames includes a function */ |     /* count frames includes a function */ | ||||||
|     first_frame = cur_frame; |     first_frame = cur_frame; | ||||||
|     while (cur_frame) { |     while (cur_frame) { | ||||||
|  | @ -2450,14 +2450,9 @@ wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env) | ||||||
|         cur_frame = cur_frame->prev_frame; |         cur_frame = cur_frame->prev_frame; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     if (!(module_inst->frames = runtime_malloc( |     /* release previous stack frames and create new ones */ | ||||||
|             (uint64)sizeof(Vector), module_inst->cur_exception, 128))) { |     if (!bh_vector_destroy(module_inst->frames) | ||||||
|         return; |         || !bh_vector_init(module_inst->frames, n, sizeof(WASMCApiFrame))) { | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     if (!bh_vector_init(module_inst->frames, n, sizeof(struct WASMFrame))) { |  | ||||||
|         wasm_runtime_free(module_inst->frames); |  | ||||||
|         module_inst->frames = NULL; |  | ||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | @ -2465,7 +2460,7 @@ wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env) | ||||||
|     n = 0; |     n = 0; | ||||||
|     os_printf("\n"); |     os_printf("\n"); | ||||||
|     while (cur_frame) { |     while (cur_frame) { | ||||||
|         struct WASMFrame frame = { 0 }; |         WASMCApiFrame frame = { 0 }; | ||||||
|         WASMFunctionInstance *func_inst = cur_frame->function; |         WASMFunctionInstance *func_inst = cur_frame->function; | ||||||
|         const char *func_name = NULL; |         const char *func_name = NULL; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -150,15 +150,6 @@ typedef struct WASMExportMemInstance { | ||||||
| } WASMExportMemInstance; | } WASMExportMemInstance; | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
| #if WASM_ENABLE_DUMP_CALL_STACK != 0 |  | ||||||
| struct WASMFrame { |  | ||||||
|     void *instance; |  | ||||||
|     uint32 module_offset; |  | ||||||
|     uint32 func_index; |  | ||||||
|     uint32 func_offset; |  | ||||||
| }; |  | ||||||
| #endif |  | ||||||
| 
 |  | ||||||
| struct WASMModuleInstance { | struct WASMModuleInstance { | ||||||
|     /* Module instance type, for module instance loaded from
 |     /* Module instance type, for module instance loaded from
 | ||||||
|        WASM bytecode binary, this field is Wasm_Module_Bytecode; |        WASM bytecode binary, this field is Wasm_Module_Bytecode; | ||||||
|  |  | ||||||
|  | @ -31,7 +31,7 @@ if(NOT DEFINED WAMR_BUILD_INTERP) | ||||||
| endif() | endif() | ||||||
| 
 | 
 | ||||||
| if(NOT DEFINED WAMR_BUILD_AOT) | if(NOT DEFINED WAMR_BUILD_AOT) | ||||||
|   set(WAMR_BUILD_AOT 1) |   set(WAMR_BUILD_AOT 0) | ||||||
| endif() | endif() | ||||||
| 
 | 
 | ||||||
| if(NOT DEFINED WAMR_BUILD_JIT) | if(NOT DEFINED WAMR_BUILD_JIT) | ||||||
|  | @ -62,7 +62,6 @@ if (NOT MSVC) | ||||||
| endif() | endif() | ||||||
| # build out vmlib | # build out vmlib | ||||||
| set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..) | set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..) | ||||||
| set(WAMRC ${WAMR_ROOT_DIR}/wamr-compiler/build/wamrc) |  | ||||||
| include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake) | include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake) | ||||||
| 
 | 
 | ||||||
| add_library(vmlib STATIC ${WAMR_RUNTIME_LIB_SOURCE}) | add_library(vmlib STATIC ${WAMR_RUNTIME_LIB_SOURCE}) | ||||||
|  | @ -84,6 +83,19 @@ if(NOT WAT2WASM) | ||||||
|   message(SEND_ERROR "can not find wat2wasm") |   message(SEND_ERROR "can not find wat2wasm") | ||||||
| endif() | endif() | ||||||
| 
 | 
 | ||||||
|  | if(${WAMR_BUILD_AOT} EQUAL 1) | ||||||
|  |   ## locate wamrc | ||||||
|  |   find_program(WAMRC | ||||||
|  |     wamrc | ||||||
|  |     PATHS ${WAMR_ROOT_DIR}/wamr-compiler/build/ | ||||||
|  |   ) | ||||||
|  | 
 | ||||||
|  |   if(NOT WAMRC) | ||||||
|  |     message(SEND_ERROR "can not find wamrc.  refer to \ | ||||||
|  |         https://github.com/bytecodealliance/wasm-micro-runtime#build-wamrc-aot-compiler" | ||||||
|  |     ) | ||||||
|  |   endif() | ||||||
|  | endif() | ||||||
| include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake) | include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake) | ||||||
| 
 | 
 | ||||||
| set(MM_UTIL src/utils/multi_module_utils.c) | set(MM_UTIL src/utils/multi_module_utils.c) | ||||||
|  | @ -120,7 +132,6 @@ foreach(EX ${EXAMPLES}) | ||||||
| 
 | 
 | ||||||
|   # generate .aot file |   # generate .aot file | ||||||
|   if(${WAMR_BUILD_AOT} EQUAL 1) |   if(${WAMR_BUILD_AOT} EQUAL 1) | ||||||
|     if(EXISTS ${WAMRC}) |  | ||||||
|     add_custom_target(${EX}_AOT |     add_custom_target(${EX}_AOT | ||||||
|       COMMAND ${WAMRC} -o ${PROJECT_BINARY_DIR}/${EX}.aot |       COMMAND ${WAMRC} -o ${PROJECT_BINARY_DIR}/${EX}.aot | ||||||
|         ${PROJECT_BINARY_DIR}/${EX}.wasm |         ${PROJECT_BINARY_DIR}/${EX}.wasm | ||||||
|  | @ -131,7 +142,6 @@ foreach(EX ${EXAMPLES}) | ||||||
|     ) |     ) | ||||||
|     add_dependencies(${EX} ${EX}_AOT) |     add_dependencies(${EX} ${EX}_AOT) | ||||||
|   endif() |   endif() | ||||||
|   endif() |  | ||||||
| endforeach() | endforeach() | ||||||
| 
 | 
 | ||||||
| if (CMAKE_BUILD_TYPE STREQUAL "Debug") | if (CMAKE_BUILD_TYPE STREQUAL "Debug") | ||||||
|  |  | ||||||
|  | @ -112,3 +112,4 @@ int main(int argc, const char* argv[]) { | ||||||
|   printf("Done.\n"); |   printf("Done.\n"); | ||||||
|   return 0; |   return 0; | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Wenyong Huang
						Wenyong Huang