Skip to content

Commit

Permalink
do runtime bounds check when normal check failed
Browse files Browse the repository at this point in the history
Signed-off-by: wenlingyun1 <wenlingyun1@xiaomi.com>
  • Loading branch information
WenLY1 committed Jul 4, 2024
1 parent 8331426 commit 7cd8b1a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 29 deletions.
8 changes: 1 addition & 7 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -3117,13 +3117,7 @@ uint64
aot_bounds_check(AOTModuleInstance *module_inst, uint64 offset, uint32 bytes)
#endif
{
WASMMemoryInstance *memory = aot_get_default_memory(module_inst);
uint64 linear_memory_size = memory->memory_data_size;

if (offset + bytes <= linear_memory_size) {
return memory->memory_data + offset;
}
return NULL;
return offset;
}

void *
Expand Down
98 changes: 76 additions & 22 deletions core/iwasm/compilation/aot_emit_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
{
LLVMValueRef offset_const =
MEMORY64_COND_VALUE(I64_CONST(offset), I32_CONST(offset));
LLVMValueRef addr, maddr, offset1, cmp1, cmp2, cmp;
LLVMValueRef addr, maddr, offset1, cmp1, cmp2, cmp, maddr_ret;
LLVMValueRef mem_base_addr, mem_check_bound;
LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
LLVMBasicBlockRef check_succ;
LLVMBasicBlockRef check_succ, runtime_bounds_check, merge_block;
AOTValue *aot_value_top;
uint32 local_idx_of_aot_value = 0;
uint64 const_value;
Expand Down Expand Up @@ -329,21 +329,82 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
}

/* Add basic blocks */
ADD_BASIC_BLOCK(check_succ, "check_succ");
LLVMMoveBasicBlockAfter(check_succ, block_curr);

if (!aot_emit_exception(comp_ctx, func_ctx,
EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp,
check_succ)) {
goto fail;
if (!comp_ctx->enable_runtime_bound_check) {
ADD_BASIC_BLOCK(check_succ, "check_succ");
LLVMMoveBasicBlockAfter(check_succ, block_curr);
if (!aot_emit_exception(comp_ctx, func_ctx,
EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp,
check_succ)) {
goto fail;
}
SET_BUILD_POS(check_succ);
if (is_local_of_aot_value) {
if (!aot_checked_addr_list_add(func_ctx, local_idx_of_aot_value,
offset, bytes))
goto fail;
}
}
else {
maddr_ret = LLVMBuildAlloca(comp_ctx->builder,
LLVMPointerType(LLVMInt8Type(), 0),
"maddr_ret");

ADD_BASIC_BLOCK(merge_block, "merge_block");
LLVMMoveBasicBlockAfter(merge_block, block_curr);
ADD_BASIC_BLOCK(runtime_bounds_check, "runtime_bounds_check");
LLVMMoveBasicBlockAfter(runtime_bounds_check, merge_block);
ADD_BASIC_BLOCK(check_succ, "check_succ");
LLVMMoveBasicBlockAfter(check_succ, runtime_bounds_check);

LLVMBuildCondBr(comp_ctx->builder, cmp, runtime_bounds_check,
check_succ);

SET_BUILD_POS(runtime_bounds_check);
maddr = aot_call_runtime_bounds_check(comp_ctx, func_ctx, offset1,
bytes);
LLVMBuildStore(comp_ctx->builder, maddr, maddr_ret);
LLVMBuildBr(comp_ctx->builder, merge_block);

SET_BUILD_POS(check_succ);
SET_BUILD_POS(check_succ);
if (is_local_of_aot_value) {
if (!aot_checked_addr_list_add(func_ctx, local_idx_of_aot_value,
offset, bytes))
goto fail;
}
if (!enable_segue) {
/* maddr = mem_base_addr + offset1 */
if (!(maddr = LLVMBuildInBoundsGEP2(comp_ctx->builder,
INT8_TYPE, mem_base_addr,
&offset1, 1, "maddr"))) {
aot_set_last_error("llvm build add failed.");
goto fail;
}
}
else {
LLVMValueRef maddr_base;

if (is_local_of_aot_value) {
if (!aot_checked_addr_list_add(func_ctx, local_idx_of_aot_value,
offset, bytes))
goto fail;
if (!(maddr_base =
LLVMBuildIntToPtr(comp_ctx->builder, addr,
INT8_PTR_TYPE_GS, "maddr_base"))) {
aot_set_last_error("llvm build int to ptr failed.");
goto fail;
}
if (!(maddr = LLVMBuildInBoundsGEP2(
comp_ctx->builder, INT8_TYPE, maddr_base,
&offset_const, 1, "maddr"))) {
aot_set_last_error("llvm build inboundgep failed.");
goto fail;
}
}
LLVMBuildStore(comp_ctx->builder, maddr, maddr_ret);
LLVMBuildBr(comp_ctx->builder, merge_block);

SET_BUILD_POS(merge_block);
maddr = LLVMBuildLoad2(comp_ctx->builder,
LLVMPointerType(LLVMInt8Type(), 0),
maddr_ret, "maddr_final");

return maddr;
}
}
if (!enable_segue) {
Expand Down Expand Up @@ -373,14 +434,7 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,

return maddr;
fail:
if (comp_ctx->enable_bound_check && comp_ctx->enable_runtime_bound_check) {
maddr =
aot_call_runtime_bounds_check(comp_ctx, func_ctx, offset1, bytes);
return maddr;
}
else {
return NULL;
}
return NULL;
}

#define BUILD_PTR_CAST(ptr_type) \
Expand Down

0 comments on commit 7cd8b1a

Please sign in to comment.