Skip to content

Commit

Permalink
aot compiler: Fix the length type passed to aot_memmove/aot_memset (b…
Browse files Browse the repository at this point in the history
…ytecodealliance#3378)

The current length type of aot_memmove/aot_memset is size_t, and on
a 64 bit host it is uint64, while what the aot code passes to it is uint32,
this might lead to unexpected behavior.

ps. bytecodealliance#3376.

Signed-off-by: victoryang00 <victoryang00@ucsc.edu>
  • Loading branch information
wenyongh authored and victoryang00 committed May 2, 2024
1 parent 52d05a3 commit 4dbed20
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions core/iwasm/compilation/aot_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ set_local_gc_ref(AOTCompFrame *frame, int n, LLVMValueRef value, uint8 ref_type)
#define INT8_TYPE comp_ctx->basic_types.int8_type
#define INT16_TYPE comp_ctx->basic_types.int16_type
#define INTPTR_T_TYPE comp_ctx->basic_types.intptr_t_type
#define SIZE_T_TYPE comp_ctx->basic_types.size_t_type
#define MD_TYPE comp_ctx->basic_types.meta_data_type
#define INT8_PTR_TYPE comp_ctx->basic_types.int8_ptr_type
#define INT16_PTR_TYPE comp_ctx->basic_types.int16_ptr_type
Expand Down
22 changes: 20 additions & 2 deletions core/iwasm/compilation/aot_emit_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -1090,14 +1090,23 @@ aot_compile_op_memory_copy(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
if (!(dst_addr = check_bulk_memory_overflow(comp_ctx, func_ctx, dst, len)))
return false;

if (comp_ctx->pointer_size == sizeof(uint64)) {
/* zero extend to uint64 if the target is 64-bit */
len = LLVMBuildZExt(comp_ctx->builder, len, I64_TYPE, "len64");
if (!len) {
aot_set_last_error("llvm build zero extend failed.");
return false;
}
}

call_aot_memmove = comp_ctx->is_indirect_mode || comp_ctx->is_jit_mode;
if (call_aot_memmove) {
LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
LLVMValueRef func, params[3];

param_types[0] = INT8_PTR_TYPE;
param_types[1] = INT8_PTR_TYPE;
param_types[2] = I32_TYPE;
param_types[2] = SIZE_T_TYPE;
ret_type = INT8_PTR_TYPE;

if (!(func_type = LLVMFunctionType(ret_type, param_types, 3, false))) {
Expand Down Expand Up @@ -1172,9 +1181,18 @@ aot_compile_op_memory_fill(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
if (!(dst_addr = check_bulk_memory_overflow(comp_ctx, func_ctx, dst, len)))
return false;

if (comp_ctx->pointer_size == sizeof(uint64)) {
/* zero extend to uint64 if the target is 64-bit */
len = LLVMBuildZExt(comp_ctx->builder, len, I64_TYPE, "len64");
if (!len) {
aot_set_last_error("llvm build zero extend failed.");
return false;
}
}

param_types[0] = INT8_PTR_TYPE;
param_types[1] = I32_TYPE;
param_types[2] = I32_TYPE;
param_types[2] = SIZE_T_TYPE;
ret_type = INT8_PTR_TYPE;

if (!(func_type = LLVMFunctionType(ret_type, param_types, 3, false))) {
Expand Down
2 changes: 2 additions & 0 deletions core/iwasm/compilation/aot_llvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1975,10 +1975,12 @@ aot_set_llvm_basic_types(AOTLLVMTypes *basic_types, LLVMContextRef context,
if (pointer_size == 4) {
basic_types->intptr_t_type = basic_types->int32_type;
basic_types->intptr_t_ptr_type = basic_types->int32_ptr_type;
basic_types->size_t_type = basic_types->int32_type;
}
else {
basic_types->intptr_t_type = basic_types->int64_type;
basic_types->intptr_t_ptr_type = basic_types->int64_ptr_type;
basic_types->size_t_type = basic_types->int64_type;
}

basic_types->gc_ref_type = basic_types->int8_ptr_type;
Expand Down
1 change: 1 addition & 0 deletions core/iwasm/compilation/aot_llvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ typedef struct AOTLLVMTypes {
LLVMTypeRef int32_type;
LLVMTypeRef int64_type;
LLVMTypeRef intptr_t_type;
LLVMTypeRef size_t_type;
LLVMTypeRef float32_type;
LLVMTypeRef float64_type;
LLVMTypeRef void_type;
Expand Down

0 comments on commit 4dbed20

Please sign in to comment.