Skip to content

Commit 62aca17

Browse files
Check possible integer overflow in aot memory boundary check (#3920)
Check possible integer overflow in aot memory boundary check when the wasm memory is 64-bit.
1 parent f1d03db commit 62aca17

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

core/iwasm/compilation/aot_emit_memory.c

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,24 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
273273
}
274274

275275
/* offset1 = offset + addr; */
276-
/* TODO: check whether integer overflow occurs when memory is 64-bit
277-
and boundary check is enabled */
278276
BUILD_OP(Add, offset_const, addr, offset1, "offset1");
279277

278+
if (is_memory64 && comp_ctx->enable_bound_check) {
279+
/* Check whether integer overflow occurs in offset + addr */
280+
LLVMBasicBlockRef check_integer_overflow_end;
281+
ADD_BASIC_BLOCK(check_integer_overflow_end,
282+
"check_integer_overflow_end");
283+
LLVMMoveBasicBlockAfter(check_integer_overflow_end, block_curr);
284+
285+
BUILD_ICMP(LLVMIntULT, offset1, offset_const, cmp1, "cmp1");
286+
if (!aot_emit_exception(comp_ctx, func_ctx,
287+
EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp1,
288+
check_integer_overflow_end)) {
289+
goto fail;
290+
}
291+
SET_BUILD_POS(check_integer_overflow_end);
292+
}
293+
280294
if (comp_ctx->enable_shared_heap /* TODO: && mem_idx == 0 */) {
281295
LLVMBasicBlockRef app_addr_in_shared_heap, app_addr_in_linear_mem;
282296
LLVMValueRef is_in_shared_heap, shared_heap_check_bound = NULL;
@@ -303,7 +317,7 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
303317
LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
304318

305319
if (!is_target_64bit) {
306-
/* Check whether interger overflow occurs in addr + offset */
320+
/* Check whether integer overflow occurs in addr + offset */
307321
LLVMBasicBlockRef check_integer_overflow_end;
308322
ADD_BASIC_BLOCK(check_integer_overflow_end,
309323
"check_integer_overflow_end");
@@ -1215,10 +1229,24 @@ check_bulk_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
12151229
goto fail;
12161230
}
12171231

1218-
/* TODO: check whether integer overflow occurs when memory is 64-bit
1219-
and boundary check is enabled */
12201232
BUILD_OP(Add, offset, bytes, max_addr, "max_addr");
12211233

1234+
if (is_memory64 && comp_ctx->enable_bound_check) {
1235+
/* Check whether integer overflow occurs in offset + addr */
1236+
LLVMBasicBlockRef check_integer_overflow_end;
1237+
ADD_BASIC_BLOCK(check_integer_overflow_end,
1238+
"check_integer_overflow_end");
1239+
LLVMMoveBasicBlockAfter(check_integer_overflow_end, block_curr);
1240+
1241+
BUILD_ICMP(LLVMIntULT, max_addr, offset, cmp, "cmp");
1242+
if (!aot_emit_exception(comp_ctx, func_ctx,
1243+
EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp,
1244+
check_integer_overflow_end)) {
1245+
goto fail;
1246+
}
1247+
SET_BUILD_POS(check_integer_overflow_end);
1248+
}
1249+
12221250
if (comp_ctx->enable_shared_heap /* TODO: && mem_idx == 0 */) {
12231251
LLVMBasicBlockRef app_addr_in_shared_heap, app_addr_in_linear_mem;
12241252
LLVMValueRef shared_heap_start_off, shared_heap_check_bound;

0 commit comments

Comments
 (0)