Skip to content

Commit 4dbed20

Browse files
wenyonghvictoryang00
authored andcommitted
aot compiler: Fix the length type passed to aot_memmove/aot_memset (#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. #3376. Signed-off-by: victoryang00 <[email protected]>
1 parent 52d05a3 commit 4dbed20

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

core/iwasm/compilation/aot_compiler.h

+1
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ set_local_gc_ref(AOTCompFrame *frame, int n, LLVMValueRef value, uint8 ref_type)
638638
#define INT8_TYPE comp_ctx->basic_types.int8_type
639639
#define INT16_TYPE comp_ctx->basic_types.int16_type
640640
#define INTPTR_T_TYPE comp_ctx->basic_types.intptr_t_type
641+
#define SIZE_T_TYPE comp_ctx->basic_types.size_t_type
641642
#define MD_TYPE comp_ctx->basic_types.meta_data_type
642643
#define INT8_PTR_TYPE comp_ctx->basic_types.int8_ptr_type
643644
#define INT16_PTR_TYPE comp_ctx->basic_types.int16_ptr_type

core/iwasm/compilation/aot_emit_memory.c

+20-2
Original file line numberDiff line numberDiff line change
@@ -1090,14 +1090,23 @@ aot_compile_op_memory_copy(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
10901090
if (!(dst_addr = check_bulk_memory_overflow(comp_ctx, func_ctx, dst, len)))
10911091
return false;
10921092

1093+
if (comp_ctx->pointer_size == sizeof(uint64)) {
1094+
/* zero extend to uint64 if the target is 64-bit */
1095+
len = LLVMBuildZExt(comp_ctx->builder, len, I64_TYPE, "len64");
1096+
if (!len) {
1097+
aot_set_last_error("llvm build zero extend failed.");
1098+
return false;
1099+
}
1100+
}
1101+
10931102
call_aot_memmove = comp_ctx->is_indirect_mode || comp_ctx->is_jit_mode;
10941103
if (call_aot_memmove) {
10951104
LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
10961105
LLVMValueRef func, params[3];
10971106

10981107
param_types[0] = INT8_PTR_TYPE;
10991108
param_types[1] = INT8_PTR_TYPE;
1100-
param_types[2] = I32_TYPE;
1109+
param_types[2] = SIZE_T_TYPE;
11011110
ret_type = INT8_PTR_TYPE;
11021111

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

1184+
if (comp_ctx->pointer_size == sizeof(uint64)) {
1185+
/* zero extend to uint64 if the target is 64-bit */
1186+
len = LLVMBuildZExt(comp_ctx->builder, len, I64_TYPE, "len64");
1187+
if (!len) {
1188+
aot_set_last_error("llvm build zero extend failed.");
1189+
return false;
1190+
}
1191+
}
1192+
11751193
param_types[0] = INT8_PTR_TYPE;
11761194
param_types[1] = I32_TYPE;
1177-
param_types[2] = I32_TYPE;
1195+
param_types[2] = SIZE_T_TYPE;
11781196
ret_type = INT8_PTR_TYPE;
11791197

11801198
if (!(func_type = LLVMFunctionType(ret_type, param_types, 3, false))) {

core/iwasm/compilation/aot_llvm.c

+2
Original file line numberDiff line numberDiff line change
@@ -1975,10 +1975,12 @@ aot_set_llvm_basic_types(AOTLLVMTypes *basic_types, LLVMContextRef context,
19751975
if (pointer_size == 4) {
19761976
basic_types->intptr_t_type = basic_types->int32_type;
19771977
basic_types->intptr_t_ptr_type = basic_types->int32_ptr_type;
1978+
basic_types->size_t_type = basic_types->int32_type;
19781979
}
19791980
else {
19801981
basic_types->intptr_t_type = basic_types->int64_type;
19811982
basic_types->intptr_t_ptr_type = basic_types->int64_ptr_type;
1983+
basic_types->size_t_type = basic_types->int64_type;
19821984
}
19831985

19841986
basic_types->gc_ref_type = basic_types->int8_ptr_type;

core/iwasm/compilation/aot_llvm.h

+1
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ typedef struct AOTLLVMTypes {
266266
LLVMTypeRef int32_type;
267267
LLVMTypeRef int64_type;
268268
LLVMTypeRef intptr_t_type;
269+
LLVMTypeRef size_t_type;
269270
LLVMTypeRef float32_type;
270271
LLVMTypeRef float64_type;
271272
LLVMTypeRef void_type;

0 commit comments

Comments
 (0)