Skip to content

Commit c7148a6

Browse files
authored
Fix potential integer overflow issues (#4429)
It is reported as "Multiplication result converted to larger type". And "Multiplication result may overflow 'Type A' before it is converted to 'Type B'." Type A is a larger type than Type B. Since the conversion applies after the multiplication, arithmetic overflow may still occur. > The rule flags every multiplication of two non-constant integer expressions > that is (explicitly or implicitly) converted to a larger integer type. The > conversion is an indication that the expression would produce a result that > would be too large to fit in the smaller integer type.
1 parent 8949797 commit c7148a6

File tree

5 files changed

+8
-6
lines changed

5 files changed

+8
-6
lines changed

core/iwasm/aot/aot_runtime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3639,7 +3639,7 @@ aot_get_module_inst_mem_consumption(const AOTModuleInstance *module_inst,
36393639
for (i = 0; i < module_inst->memory_count; i++) {
36403640
AOTMemoryInstance *mem_inst = module_inst->memories[i];
36413641
mem_conspn->memories_size +=
3642-
mem_inst->num_bytes_per_page * mem_inst->cur_page_count;
3642+
(uint64)mem_inst->num_bytes_per_page * mem_inst->cur_page_count;
36433643
mem_conspn->app_heap_size =
36443644
mem_inst->heap_data_end - mem_inst->heap_data;
36453645
/* size of app heap structure */

core/iwasm/compilation/aot_emit_aot_file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,8 @@ get_init_expr_size(const AOTCompContext *comp_ctx, const AOTCompData *comp_data,
302302

303303
/* array_elem_type + type_index + len + elems */
304304
size += sizeof(uint32) * 3
305-
+ wasm_value_type_size_internal(array_type->elem_type,
306-
comp_ctx->pointer_size)
305+
+ (uint64)wasm_value_type_size_internal(
306+
array_type->elem_type, comp_ctx->pointer_size)
307307
* value_count;
308308
break;
309309
}

core/iwasm/compilation/aot_emit_function.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,8 @@ call_aot_invoke_c_api_native(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
347347

348348
/* Get &c_api_func_imports[func_idx], note size of CApiFuncImport
349349
is pointer_size * 3 */
350-
offset = I32_CONST((comp_ctx->pointer_size * 3) * import_func_idx);
350+
offset = I32_CONST((unsigned long long)comp_ctx->pointer_size * 3
351+
* import_func_idx);
351352
CHECK_LLVM_CONST(offset);
352353
c_api_func_import =
353354
LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, c_api_func_imports,

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4161,7 +4161,7 @@ wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
41614161
sizeof(WASMMemoryInstance *) * module_inst->memory_count;
41624162
for (i = 0; i < module_inst->memory_count; i++) {
41634163
WASMMemoryInstance *memory = module_inst->memories[i];
4164-
size = memory->num_bytes_per_page * memory->cur_page_count;
4164+
size = (uint64)memory->num_bytes_per_page * memory->cur_page_count;
41654165
mem_conspn->memories_size += size;
41664166
mem_conspn->app_heap_size += memory->heap_data_end - memory->heap_data;
41674167
/* size of app heap structure */

core/iwasm/libraries/thread-mgr/thread_manager.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ wasm_cluster_create(WASMExecEnv *exec_env)
301301
aux_stack_start -= cluster->stack_size;
302302

303303
for (i = 0; i < cluster_max_thread_num; i++) {
304-
cluster->stack_tops[i] = aux_stack_start - cluster->stack_size * i;
304+
cluster->stack_tops[i] =
305+
aux_stack_start - (uint64)cluster->stack_size * i;
305306
}
306307
}
307308
#endif

0 commit comments

Comments
 (0)