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 12, 2024
1 parent 8331426 commit dd88158
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 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
56 changes: 32 additions & 24 deletions core/iwasm/compilation/aot_emit_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ aot_call_runtime_bounds_check(AOTCompContext *comp_ctx,
AOTFuncContext *func_ctx, LLVMValueRef offset,
uint32 bytes)
{
LLVMValueRef param_values[3], value, maddr, func;
LLVMValueRef param_values[3], value, offset2, func;
LLVMTypeRef param_types[3], ret_type = 0, func_type = 0, func_ptr_type = 0;
uint32 argc = 3;

Expand All @@ -118,12 +118,12 @@ aot_call_runtime_bounds_check(AOTCompContext *comp_ctx,

GET_AOT_FUNCTION(aot_bounds_check, argc);

if (!(maddr = LLVMBuildCall2(comp_ctx->builder, func_type, func,
param_values, argc, "maddr"))) {
if (!(offset2 = LLVMBuildCall2(comp_ctx->builder, func_type, func,
param_values, argc, "offset2"))) {
aot_set_last_error("llvm build call failed.");
goto fail;
}
return maddr;
return offset2;
fail:
return NULL;
}
Expand All @@ -135,10 +135,11 @@ 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, offset2, cmp1, cmp2, cmp;
LLVMValueRef mem_base_addr, mem_check_bound;
LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
LLVMBasicBlockRef check_succ;
LLVMBasicBlockRef check_succ, runtime_bounds_check;
LLVMValueRef phi;
AOTValue *aot_value_top;
uint32 local_idx_of_aot_value = 0;
uint64 const_value;
Expand Down Expand Up @@ -204,7 +205,6 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
}

POP_MEM_OFFSET(addr);

/*
* Note: not throw the integer-overflow-exception here since it must
* have been thrown when converting float to integer before
Expand Down Expand Up @@ -330,21 +330,36 @@ 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(runtime_bounds_check, "runtime_bounds_check");
LLVMBuildCondBr(comp_ctx->builder, cmp, runtime_bounds_check,
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 (comp_ctx->enable_runtime_bound_check) {
SET_BUILD_POS(runtime_bounds_check);
offset2 = aot_call_runtime_bounds_check(comp_ctx, func_ctx, offset1,
bytes);
// todo: store offset2 in the local
LLVMBuildBr(comp_ctx->builder, check_succ);
}

SET_BUILD_POS(check_succ);

if (comp_ctx->enable_runtime_bound_check) {
phi = LLVMBuildPhi(comp_ctx->builder,
is_target_64bit ? I64_TYPE : I32_TYPE, "phi");
LLVMValueRef incoming_values[] = { offset1, offset2 };
LLVMBasicBlockRef incoming_blocks[] = { block_curr,
runtime_bounds_check };
LLVMAddIncoming(phi, incoming_values, incoming_blocks, 2);
offset1 = phi;
}
}
if (!enable_segue) {
/* maddr = mem_base_addr + offset1 */
Expand Down Expand Up @@ -373,14 +388,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 dd88158

Please sign in to comment.