Skip to content

Commit 95f506a

Browse files
authored
Merge commit from fork
* fix overflow in check_bulk_memory_overflow * add comment
1 parent e6fe606 commit 95f506a

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

core/iwasm/compilation/aot_emit_memory.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,7 @@ LLVMValueRef
14861486
check_bulk_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
14871487
LLVMValueRef offset, LLVMValueRef bytes)
14881488
{
1489-
LLVMValueRef maddr, max_addr, cmp, cmp1;
1489+
LLVMValueRef maddr, max_addr, cmp, cmp1, offset1;
14901490
LLVMValueRef mem_base_addr;
14911491
LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
14921492
LLVMBasicBlockRef check_succ;
@@ -1539,8 +1539,18 @@ check_bulk_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
15391539
if (mem_data_size > 0 && mem_offset + mem_len <= mem_data_size) {
15401540
/* inside memory space */
15411541
/* maddr = mem_base_addr + moffset */
1542+
/* Perform zero extension in advance to avoid LLVMBuildInBoundsGEP2
1543+
* interpreting a negative address due to sign extension when
1544+
* mem_offset >= 2GiB */
1545+
if (comp_ctx->pointer_size == sizeof(uint64)) {
1546+
offset1 = I64_CONST(mem_offset);
1547+
}
1548+
else {
1549+
offset1 = I32_CONST((uint32)mem_offset);
1550+
}
1551+
CHECK_LLVM_CONST(offset1);
15421552
if (!(maddr = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
1543-
mem_base_addr, &offset, 1,
1553+
mem_base_addr, &offset1, 1,
15441554
"maddr"))) {
15451555
aot_set_last_error("llvm build add failed.");
15461556
goto fail;

0 commit comments

Comments
 (0)