mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-02-08 16:05:07 +00:00
![wenyongh](/assets/img/avatar_default.png)
* Implement memory profiler, optimize memory usage, modify code indent * Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default * Add a new extension library: connection * Fix bug of reading magic number and version in big endian platform * Re-org platform APIs: move most platform APIs from iwasm to shared-lib * Enhance wasm loader to fix some security issues * Fix issue about illegal load of EXC_RETURN into PC on stm32 board * Updates that let a restricted version of the interpreter run in SGX * Enable native/app address validation and conversion for wasm app * Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused * Refine binary size and fix several minor issues Optimize interpreter LOAD/STORE opcodes to decrease the binary size Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper Add macros of global heap size, stack size, heap size for Zephyr main.c Clear compile warning of wasm_application.c * Add more strict security checks for libc wrapper API's * Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files * Enhance security of libc strcpy/sprintf wrapper function * Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions * Remove get_module_inst() and fix issue of call native * Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate * Refine interpreter call native process, refine memory boudary check * Fix issues of invokeNative function of arm/mips/general version * Add a switch to build simple sample without gui support * Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code * Re-org shared lib header files, remove unused info; fix compile issues of vxworks * Add build target general * Remove unused files * Update license header * test push * Restore file * Sync up with internal/feature * Sync up with internal/feature * Rename build_wamr_app to build_wasm_app * Fix small issues of README * Enhance malformed wasm file checking Fix issue of print hex int and implement utf8 string check Fix wasi file read/write right issue Fix minor issue of build wasm app doc * Sync up with internal/feature * Sync up with internal/feature: fix interpreter arm issue, fix read leb issue * Sync up with internal/feature * Fix bug of config.h and rename wasi config.h to ssp_config.h * Sync up with internal/feature * Import wamr aot * update document * update document * Update document, disable WASI in 32bit * update document * remove files * update document * Update document * update document * update document * update samples * Sync up with internal repo
185 lines
7.0 KiB
C
185 lines
7.0 KiB
C
/*
|
|
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
|
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
*/
|
|
|
|
#include "aot_emit_exception.h"
|
|
#include "../aot/aot_runtime.h"
|
|
|
|
static char *exce_block_names[] = {
|
|
"exce_unreachable", /* EXCE_UNREACHABLE */
|
|
"exce_out_of_memory", /* EXCE_OUT_OF_MEMORY */
|
|
"exce_out_of_bounds_mem_access",/* EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS */
|
|
"exce_integer_overflow", /* EXCE_INTEGER_OVERFLOW */
|
|
"exce_divide_by_zero", /* EXCE_INTEGER_DIVIDE_BY_ZERO */
|
|
"exce_invalid_convert_to_int", /* EXCE_INVALID_CONVERSION_TO_INTEGER */
|
|
"exce_invalid_func_type_idx", /* EXCE_INVALID_FUNCTION_TYPE_INDEX */
|
|
"exce_invalid_func_idx", /* EXCE_INVALID_FUNCTION_INDEX */
|
|
"exce_undefined_element", /* EXCE_UNDEFINED_ELEMENT */
|
|
"exce_uninit_element", /* EXCE_UNINITIALIZED_ELEMENT */
|
|
"exce_call_unlinked" /* EXCE_CALL_UNLINKED_IMPORT_FUNC */
|
|
};
|
|
|
|
bool
|
|
aot_emit_exception(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
|
|
int32 exception_id,
|
|
bool is_cond_br,
|
|
LLVMValueRef cond_br_if,
|
|
LLVMBasicBlockRef cond_br_else_block)
|
|
{
|
|
LLVMBasicBlockRef exce_block;
|
|
LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
|
|
LLVMValueRef exce_id = I32_CONST((uint32)exception_id), func_const, func;
|
|
LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
|
|
LLVMValueRef param_values[2];
|
|
|
|
bh_assert(exception_id >= 0 && exception_id < EXCE_NUM);
|
|
|
|
CHECK_LLVM_CONST(exce_id);
|
|
|
|
/* Create got_exception block if needed */
|
|
if (!func_ctx->got_exception_block) {
|
|
if (!(func_ctx->got_exception_block =
|
|
LLVMAppendBasicBlockInContext(comp_ctx->context,
|
|
func_ctx->func,
|
|
"got_exception"))) {
|
|
aot_set_last_error("add LLVM basic block failed.");
|
|
return false;
|
|
}
|
|
|
|
LLVMPositionBuilderAtEnd(comp_ctx->builder,
|
|
func_ctx->got_exception_block);
|
|
|
|
/* Create exection id phi */
|
|
if (!(func_ctx->exception_id_phi =
|
|
LLVMBuildPhi(comp_ctx->builder,
|
|
comp_ctx->basic_types.int32_type,
|
|
"exception_id_phi"))) {
|
|
aot_set_last_error("llvm build phi failed.");
|
|
return false;
|
|
}
|
|
|
|
/* Call aot_set_exception_with_id() to throw exception */
|
|
param_types[0] = INT8_PTR_TYPE;
|
|
param_types[1] = I32_TYPE;
|
|
ret_type = VOID_TYPE;
|
|
|
|
/* Create function type */
|
|
if (!(func_type = LLVMFunctionType(ret_type, param_types,
|
|
2, false))) {
|
|
aot_set_last_error("create LLVM function type failed.");
|
|
return false;
|
|
}
|
|
|
|
if (comp_ctx->is_jit_mode) {
|
|
/* Create function type */
|
|
if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
|
|
aot_set_last_error("create LLVM function type failed.");
|
|
return false;
|
|
}
|
|
/* Create LLVM function with const function pointer */
|
|
if (!(func_const =
|
|
I64_CONST((uint64)(uintptr_t)aot_set_exception_with_id))
|
|
|| !(func = LLVMConstIntToPtr(func_const, func_ptr_type))) {
|
|
aot_set_last_error("create LLVM value failed.");
|
|
return false;
|
|
}
|
|
}
|
|
else {
|
|
/* Create LLVM function with external function pointer */
|
|
if (!(func = LLVMGetNamedFunction(comp_ctx->module,
|
|
"aot_set_exception_with_id"))
|
|
&& !(func = LLVMAddFunction(comp_ctx->module,
|
|
"aot_set_exception_with_id",
|
|
func_type))) {
|
|
aot_set_last_error("add LLVM function failed.");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/* Call the aot_set_exception_with_id() function */
|
|
param_values[0] = func_ctx->aot_inst;
|
|
param_values[1] = func_ctx->exception_id_phi;
|
|
if (!LLVMBuildCall(comp_ctx->builder, func, param_values,
|
|
2, "")) {
|
|
aot_set_last_error("llvm build call failed.");
|
|
return false;
|
|
}
|
|
|
|
/* Create return IR */
|
|
AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
|
|
if (aot_func_type->result_count) {
|
|
switch (aot_func_type->types[aot_func_type->param_count]) {
|
|
case VALUE_TYPE_I32:
|
|
LLVMBuildRet(comp_ctx->builder, I32_ZERO);
|
|
break;
|
|
case VALUE_TYPE_I64:
|
|
LLVMBuildRet(comp_ctx->builder, I64_ZERO);
|
|
break;
|
|
case VALUE_TYPE_F32:
|
|
LLVMBuildRet(comp_ctx->builder, F32_ZERO);
|
|
break;
|
|
case VALUE_TYPE_F64:
|
|
LLVMBuildRet(comp_ctx->builder, F64_ZERO);
|
|
break;
|
|
}
|
|
}
|
|
else {
|
|
LLVMBuildRetVoid(comp_ctx->builder);
|
|
}
|
|
|
|
/* Resume the builder position */
|
|
LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
|
|
}
|
|
|
|
/* Create exception block if needed */
|
|
if (!(exce_block = func_ctx->exception_blocks[exception_id])) {
|
|
if (!(func_ctx->exception_blocks[exception_id] = exce_block =
|
|
LLVMAppendBasicBlockInContext(comp_ctx->context,
|
|
func_ctx->func,
|
|
exce_block_names[exception_id]))) {
|
|
aot_set_last_error("add LLVM basic block failed.");
|
|
return false;
|
|
}
|
|
|
|
/* Move before got_exception block */
|
|
LLVMMoveBasicBlockBefore(exce_block, func_ctx->got_exception_block);
|
|
|
|
/* Add phi incoming value to got_exception block */
|
|
LLVMAddIncoming(func_ctx->exception_id_phi, &exce_id, &exce_block, 1);
|
|
|
|
/* Jump to got exception block */
|
|
LLVMPositionBuilderAtEnd(comp_ctx->builder, exce_block);
|
|
if (!LLVMBuildBr(comp_ctx->builder, func_ctx->got_exception_block)) {
|
|
aot_set_last_error("llvm build br failed.");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/* Resume builder position */
|
|
LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
|
|
|
|
if (!is_cond_br) {
|
|
/* not condition br, create br IR */
|
|
if (!LLVMBuildBr(comp_ctx->builder, exce_block)) {
|
|
aot_set_last_error("llvm build br failed.");
|
|
return false;
|
|
}
|
|
}
|
|
else {
|
|
/* Create condition br */
|
|
if (!LLVMBuildCondBr(comp_ctx->builder, cond_br_if,
|
|
exce_block, cond_br_else_block)) {
|
|
aot_set_last_error("llvm build cond br failed.");
|
|
return false;
|
|
}
|
|
/* Start to translate the else block */
|
|
LLVMPositionBuilderAtEnd(comp_ctx->builder, cond_br_else_block);
|
|
}
|
|
|
|
return true;
|
|
fail:
|
|
return false;
|
|
}
|
|
|