@@ -82,6 +82,7 @@ check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
8282 LLVMValueRef mem_base_addr , mem_check_bound , total_mem_size ;
8383 LLVMBasicBlockRef block_curr = LLVMGetInsertBlock (comp_ctx -> builder );
8484 LLVMBasicBlockRef check_succ , check_mem_space ;
85+ AOTValue * aot_value ;
8586
8687 CHECK_LLVM_CONST (offset_const );
8788 CHECK_LLVM_CONST (bytes_const );
@@ -100,6 +101,8 @@ check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
100101 }
101102 }
102103
104+ aot_value = func_ctx -> block_stack .block_list_end -> value_stack .value_list_end ;
105+
103106 POP_I32 (addr );
104107 /* offset1 = offset + addr; */
105108 BUILD_OP (Add , offset_const , addr , offset1 , "offset1" );
@@ -152,27 +155,37 @@ check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
152155 SET_BUILD_POS (check_mem_space );
153156 }
154157
155- /* offset2 = offset1 - heap_base_offset; */
156- BUILD_OP (Sub , offset1 , heap_base_offset , offset2 , "offset2" );
158+ if (!(aot_value -> is_local
159+ && aot_checked_addr_list_find (func_ctx , aot_value -> local_idx ,
160+ offset , bytes ))) {
161+ /* offset2 = offset1 - heap_base_offset; */
162+ BUILD_OP (Sub , offset1 , heap_base_offset , offset2 , "offset2" );
157163
158- if (!(mem_check_bound =
159- get_memory_check_bound (comp_ctx , func_ctx , bytes ))) {
160- goto fail ;
161- }
164+ if (!(mem_check_bound =
165+ get_memory_check_bound (comp_ctx , func_ctx , bytes ))) {
166+ goto fail ;
167+ }
162168
163- /* Add basic blocks */
164- ADD_BASIC_BLOCK (check_succ , "check_succ" );
165- LLVMMoveBasicBlockAfter (check_succ , block_curr );
169+ /* Add basic blocks */
170+ ADD_BASIC_BLOCK (check_succ , "check_succ" );
171+ LLVMMoveBasicBlockAfter (check_succ , block_curr );
166172
167- /* offset2 > bound ? */
168- BUILD_ICMP (LLVMIntUGT , offset2 , mem_check_bound , cmp , "cmp" );
169- if (!aot_emit_exception (comp_ctx , func_ctx ,
170- EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS ,
171- true, cmp , check_succ )) {
172- goto fail ;
173- }
173+ /* offset2 > bound ? */
174+ BUILD_ICMP (LLVMIntUGT , offset2 , mem_check_bound , cmp , "cmp" );
175+ if (!aot_emit_exception (comp_ctx , func_ctx ,
176+ EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS ,
177+ true, cmp , check_succ )) {
178+ goto fail ;
179+ }
180+
181+ SET_BUILD_POS (check_succ );
174182
175- SET_BUILD_POS (check_succ );
183+ if (aot_value -> is_local ) {
184+ if (!aot_checked_addr_list_add (func_ctx , aot_value -> local_idx ,
185+ offset , bytes ))
186+ goto fail ;
187+ }
188+ }
176189
177190 /* maddr = mem_base_addr + offset1 */
178191 if (!(maddr = LLVMBuildInBoundsGEP (comp_ctx -> builder , mem_base_addr ,
0 commit comments