From 02f0156475bf20416e6446cdb03d3a2085bae7fb Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Tue, 9 May 2023 11:10:40 +0800 Subject: [PATCH] GC: Change table_elem_type_t to pointer width to simplify the related handlings (#2178) --- .gitignore | 1 + core/iwasm/aot/aot_loader.c | 2 +- core/iwasm/aot/aot_runtime.c | 33 +++++++++++--------- core/iwasm/common/wasm_c_api.c | 4 +-- core/iwasm/common/wasm_runtime_common.c | 6 ++-- core/iwasm/compilation/aot.c | 15 ++++----- core/iwasm/compilation/aot.h | 2 +- core/iwasm/compilation/aot_compiler.h | 2 ++ core/iwasm/compilation/aot_emit_aot_file.c | 33 +++++++++++++------- core/iwasm/compilation/aot_emit_function.c | 18 +++++++---- core/iwasm/compilation/aot_emit_table.c | 20 +++++++----- core/iwasm/compilation/aot_llvm.c | 18 +++++++++-- core/iwasm/compilation/aot_llvm.h | 2 ++ core/iwasm/fast-jit/fe/jit_emit_function.c | 4 ++- core/iwasm/fast-jit/fe/jit_emit_table.c | 28 ++++++++++------- core/iwasm/fast-jit/jit_frontend.c | 4 +-- core/iwasm/interpreter/wasm.h | 4 +-- core/iwasm/interpreter/wasm_interp_classic.c | 20 ++++++------ core/iwasm/interpreter/wasm_interp_fast.c | 20 ++++++------ core/iwasm/interpreter/wasm_loader.c | 4 +-- core/iwasm/interpreter/wasm_mini_loader.c | 4 +-- core/iwasm/interpreter/wasm_runtime.c | 22 +++++++------ 22 files changed, 161 insertions(+), 105 deletions(-) diff --git a/.gitignore b/.gitignore index a4889fb7f..74596ef70 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ *.obj *.a *.so +.clangd .DS_Store core/deps/** diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index 5345fb2d7..f5e8c320f 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -1037,7 +1037,7 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end, read_uint64(buf, buf_end, init_expr_value); read_uint32(buf, buf_end, func_index_count); - size1 = sizeof(uint32) * (uint64)func_index_count; + size1 = sizeof(uintptr_t) * (uint64)func_index_count; size = offsetof(AOTTableInitData, func_indexes) + size1; if (!(data_list[i] = loader_malloc(size, error_buf, error_buf_size))) { return false; diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index c909d29d4..be93ef331 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -228,12 +228,14 @@ tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module, } /* Set all elements to -1 to mark them as uninitialized elements */ - memset(tbl_inst->elems, 0xff, sizeof(uint32) * tbl_inst->max_size); + memset(tbl_inst->elems, 0xff, + sizeof(table_elem_type_t) * tbl_inst->max_size); module_inst->tables[i] = tbl_inst; tbl_inst = (AOTTableInstance *)((uint8 *)tbl_inst + offsetof(AOTTableInstance, elems) - + sizeof(uint32) * tbl_inst->max_size); + + sizeof(table_elem_type_t) + * tbl_inst->max_size); } /* fill table with element segment content */ @@ -316,9 +318,10 @@ tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module, * Check function index in the current module inst for now. * will check the linked table inst owner in future */ - bh_memcpy_s(tbl_inst->elems + base_offset, - (tbl_inst->max_size - base_offset) * sizeof(uint32), - table_seg->func_indexes, length * sizeof(uint32)); + bh_memcpy_s( + tbl_inst->elems + base_offset, + (tbl_inst->max_size - base_offset) * sizeof(table_elem_type_t), + table_seg->func_indexes, length * sizeof(table_elem_type_t)); } return true; @@ -1112,7 +1115,7 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, WASMExecEnv *exec_env_main, */ for (i = 0; i != module->import_table_count; ++i) { table_size += offsetof(AOTTableInstance, elems); - table_size += (uint64)sizeof(uint32) + table_size += (uint64)sizeof(table_elem_type_t) * (uint64)aot_get_imp_tbl_data_slots( module->import_tables + i, false); } @@ -1120,7 +1123,7 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, WASMExecEnv *exec_env_main, for (i = 0; i != module->table_count; ++i) { table_size += offsetof(AOTTableInstance, elems); table_size += - (uint64)sizeof(uint32) + (uint64)sizeof(table_elem_type_t) * (uint64)aot_get_tbl_data_slots(module->tables + i, false); } total_size += table_size; @@ -2507,9 +2510,10 @@ aot_table_init(AOTModuleInstance *module_inst, uint32 tbl_idx, } bh_memcpy_s((uint8 *)tbl_inst + offsetof(AOTTableInstance, elems) - + dst_offset * sizeof(uint32), - (tbl_inst->cur_size - dst_offset) * sizeof(uint32), - tbl_seg->func_indexes + src_offset, length * sizeof(uint32)); + + dst_offset * sizeof(table_elem_type_t), + (tbl_inst->cur_size - dst_offset) * sizeof(table_elem_type_t), + tbl_seg->func_indexes + src_offset, + length * sizeof(table_elem_type_t)); } void @@ -2535,11 +2539,12 @@ aot_table_copy(AOTModuleInstance *module_inst, uint32 src_tbl_idx, /* if src_offset < dst_offset, copy from back to front */ /* merge all together */ bh_memmove_s((uint8 *)dst_tbl_inst + offsetof(AOTTableInstance, elems) - + dst_offset * sizeof(uint32), - (dst_tbl_inst->cur_size - dst_offset) * sizeof(uint32), + + dst_offset * sizeof(table_elem_type_t), + (dst_tbl_inst->cur_size - dst_offset) + * sizeof(table_elem_type_t), (uint8 *)src_tbl_inst + offsetof(AOTTableInstance, elems) - + src_offset * sizeof(uint32), - length * sizeof(uint32)); + + src_offset * sizeof(table_elem_type_t), + length * sizeof(table_elem_type_t)); } void diff --git a/core/iwasm/common/wasm_c_api.c b/core/iwasm/common/wasm_c_api.c index dc36adec6..c6d727d11 100644 --- a/core/iwasm/common/wasm_c_api.c +++ b/core/iwasm/common/wasm_c_api.c @@ -3984,7 +3984,7 @@ wasm_table_set(wasm_table_t *table, wasm_table_size_t index, return false; } - p_ref_idx = table_interp->elems + index; + p_ref_idx = (uint32 *)(table_interp->elems + index); function_count = ((WASMModuleInstance *)table->inst_comm_rt)->e->function_count; } @@ -4000,7 +4000,7 @@ wasm_table_set(wasm_table_t *table, wasm_table_size_t index, return false; } - p_ref_idx = table_aot->elems + index; + p_ref_idx = (uint32 *)(table_aot->elems + index); function_count = module_aot->func_count; } #endif diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 7db1265f3..505963ca1 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -4935,7 +4935,8 @@ mark_externref(uint32 externref_idx) static void interp_mark_all_externrefs(WASMModuleInstance *module_inst) { - uint32 i, j, externref_idx, *table_data; + uint32 i, j, externref_idx; + table_elem_type_t *table_data; uint8 *global_data = module_inst->global_data; WASMGlobalInstance *global; WASMTableInstance *table; @@ -5455,7 +5456,8 @@ results_to_argv(WASMModuleInstanceCommon *module_inst, uint32 *out_argv, #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0 case VALUE_TYPE_EXTERNREF: if (!wasm_externref_obj2ref(module_inst, - (void *)result->of.foreign, argv)) { + (void *)result->of.foreign, + (uint32 *)argv)) { return false; } argv++; diff --git a/core/iwasm/compilation/aot.c b/core/iwasm/compilation/aot.c index e836df28f..fde7805dc 100644 --- a/core/iwasm/compilation/aot.c +++ b/core/iwasm/compilation/aot.c @@ -114,9 +114,9 @@ aot_create_table_init_data_list(const WASMModule *module) /* Create each table data segment */ for (i = 0; i < module->table_seg_count; i++) { - size = - offsetof(AOTTableInitData, func_indexes) - + sizeof(uint32) * (uint64)module->table_segments[i].function_count; + size = offsetof(AOTTableInitData, func_indexes) + + sizeof(uintptr_t) + * (uint64)module->table_segments[i].function_count; if (size >= UINT32_MAX || !(data_list[i] = wasm_runtime_malloc((uint32)size))) { aot_set_last_error("allocate memory failed."); @@ -136,10 +136,11 @@ aot_create_table_init_data_list(const WASMModule *module) sizeof(AOTInitExpr)); data_list[i]->func_index_count = module->table_segments[i].function_count; - bh_memcpy_s(data_list[i]->func_indexes, - sizeof(uint32) * module->table_segments[i].function_count, - module->table_segments[i].func_indexes, - sizeof(uint32) * module->table_segments[i].function_count); + bh_memcpy_s( + data_list[i]->func_indexes, + sizeof(uintptr_t) * module->table_segments[i].function_count, + module->table_segments[i].func_indexes, + sizeof(uintptr_t) * module->table_segments[i].function_count); } return data_list; diff --git a/core/iwasm/compilation/aot.h b/core/iwasm/compilation/aot.h index 76a32c615..ccc1b0d97 100644 --- a/core/iwasm/compilation/aot.h +++ b/core/iwasm/compilation/aot.h @@ -132,7 +132,7 @@ typedef struct AOTTableInitData { /* Function index count */ uint32 func_index_count; /* Function index array */ - uint32 func_indexes[1]; + uintptr_t func_indexes[1]; } AOTTableInitData; /** diff --git a/core/iwasm/compilation/aot_compiler.h b/core/iwasm/compilation/aot_compiler.h index e6031ab89..05af5d7b3 100644 --- a/core/iwasm/compilation/aot_compiler.h +++ b/core/iwasm/compilation/aot_compiler.h @@ -229,11 +229,13 @@ check_type_compatible(uint8 src_type, uint8 dst_type) #define INT1_TYPE comp_ctx->basic_types.int1_type #define INT8_TYPE comp_ctx->basic_types.int8_type #define INT16_TYPE comp_ctx->basic_types.int16_type +#define INTPTR_TYPE comp_ctx->basic_types.intptr_type #define MD_TYPE comp_ctx->basic_types.meta_data_type #define INT8_PTR_TYPE comp_ctx->basic_types.int8_ptr_type #define INT16_PTR_TYPE comp_ctx->basic_types.int16_ptr_type #define INT32_PTR_TYPE comp_ctx->basic_types.int32_ptr_type #define INT64_PTR_TYPE comp_ctx->basic_types.int64_ptr_type +#define INTPTR_PTR_TYPE comp_ctx->basic_types.intptr_ptr_type #define F32_PTR_TYPE comp_ctx->basic_types.float32_ptr_type #define F64_PTR_TYPE comp_ctx->basic_types.float64_ptr_type #define FUNC_REF_TYPE comp_ctx->basic_types.funcref_type diff --git a/core/iwasm/compilation/aot_emit_aot_file.c b/core/iwasm/compilation/aot_emit_aot_file.c index 62bb809da..2be831ae3 100644 --- a/core/iwasm/compilation/aot_emit_aot_file.c +++ b/core/iwasm/compilation/aot_emit_aot_file.c @@ -251,7 +251,8 @@ get_mem_info_size(AOTCompData *comp_data) } static uint32 -get_table_init_data_size(AOTTableInitData *table_init_data) +get_table_init_data_size(AOTCompContext *comp_ctx, + AOTTableInitData *table_init_data) { /* * mode (4 bytes), elem_type (4 bytes), do not need is_dropped field @@ -262,11 +263,13 @@ get_table_init_data_size(AOTTableInitData *table_init_data) */ return (uint32)(sizeof(uint32) * 2 + sizeof(uint32) + sizeof(uint32) + sizeof(uint64) + sizeof(uint32) - + sizeof(uint32) * table_init_data->func_index_count); + + comp_ctx->pointer_size + * table_init_data->func_index_count); } static uint32 -get_table_init_data_list_size(AOTTableInitData **table_init_data_list, +get_table_init_data_list_size(AOTCompContext *comp_ctx, + AOTTableInitData **table_init_data_list, uint32 table_init_data_count) { /* @@ -279,7 +282,7 @@ get_table_init_data_list_size(AOTTableInitData **table_init_data_list, * | | U32 offset.init_expr_type * | | U64 offset.u.i64 * | | U32 func_index_count - * | | U32[func_index_count] + * | | U32/U64 [func_index_count] * ------------------------------ */ AOTTableInitData **table_init_data = table_init_data_list; @@ -289,7 +292,7 @@ get_table_init_data_list_size(AOTTableInitData **table_init_data_list, for (i = 0; i < table_init_data_count; i++, table_init_data++) { size = align_uint(size, 4); - size += get_table_init_data_size(*table_init_data); + size += get_table_init_data_size(comp_ctx, *table_init_data); } return size; } @@ -331,7 +334,7 @@ get_table_size(AOTCompData *comp_data) } static uint32 -get_table_info_size(AOTCompData *comp_data) +get_table_info_size(AOTCompContext *comp_ctx, AOTCompData *comp_data) { /* * ------------------------------ @@ -355,7 +358,8 @@ get_table_info_size(AOTCompData *comp_data) * ------------------------------ */ return get_import_table_size(comp_data) + get_table_size(comp_data) - + get_table_init_data_list_size(comp_data->table_init_data_list, + + get_table_init_data_list_size(comp_ctx, + comp_data->table_init_data_list, comp_data->table_init_data_count); } @@ -530,7 +534,7 @@ get_init_data_section_size(AOTCompContext *comp_ctx, AOTCompData *comp_data, size += get_mem_info_size(comp_data); size = align_uint(size, 4); - size += get_table_info_size(comp_data); + size += get_table_info_size(comp_ctx, comp_data); size = align_uint(size, 4); size += get_func_type_info_size(comp_data); @@ -1470,11 +1474,18 @@ aot_emit_table_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset, EMIT_U32(init_datas[i]->offset.init_expr_type); EMIT_U64(init_datas[i]->offset.u.i64); EMIT_U32(init_datas[i]->func_index_count); - for (j = 0; j < init_datas[i]->func_index_count; j++) - EMIT_U32(init_datas[i]->func_indexes[j]); + for (j = 0; j < init_datas[i]->func_index_count; j++) { + + if (comp_ctx->pointer_size == 4) { + EMIT_U32(init_datas[i]->func_indexes[j]); + } + else { + EMIT_U64(init_datas[i]->func_indexes[j]); + } + } } - if (offset - *p_offset != get_table_info_size(comp_data)) { + if (offset - *p_offset != get_table_info_size(comp_ctx, comp_data)) { aot_set_last_error("emit table info failed."); return false; } diff --git a/core/iwasm/compilation/aot_emit_function.c b/core/iwasm/compilation/aot_emit_function.c index 9ba8baa24..b7169e732 100644 --- a/core/iwasm/compilation/aot_emit_function.c +++ b/core/iwasm/compilation/aot_emit_function.c @@ -1210,9 +1210,9 @@ aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, are equal (the type index of call_indirect opcode and callee func), we don't need to check whether the whole function types are equal, including param types and result types. */ - type_idx = wasm_get_smallest_type_idx(comp_ctx->comp_data->func_types, - comp_ctx->comp_data->func_type_count, - type_idx); + type_idx = wasm_get_smallest_type_idx( + (WASMTypePtr *)comp_ctx->comp_data->func_types, + comp_ctx->comp_data->func_type_count, type_idx); ftype_idx_const = I32_CONST(type_idx); CHECK_LLVM_CONST(ftype_idx_const); @@ -1285,25 +1285,31 @@ aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, } if (!(table_elem = LLVMBuildBitCast(comp_ctx->builder, table_elem, - INT32_PTR_TYPE, "table_elem_i32p"))) { + INTPTR_PTR_TYPE, "table_elem_ptr"))) { HANDLE_FAILURE("LLVMBuildBitCast"); goto fail; } /* Load function index */ if (!(table_elem = - LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE, table_elem, + LLVMBuildInBoundsGEP2(comp_ctx->builder, INTPTR_TYPE, table_elem, &elem_idx, 1, "table_elem"))) { HANDLE_FAILURE("LLVMBuildNUWAdd"); goto fail; } - if (!(func_idx = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, table_elem, + if (!(func_idx = LLVMBuildLoad2(comp_ctx->builder, INTPTR_TYPE, table_elem, "func_idx"))) { aot_set_last_error("llvm build load failed."); goto fail; } + if (!(func_idx = LLVMBuildIntCast2(comp_ctx->builder, func_idx, I32_TYPE, + true, "func_idx_i32"))) { + aot_set_last_error("llvm build int cast failed."); + goto fail; + } + /* Check if func_idx == -1 */ if (!(cmp_func_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, func_idx, I32_NEG_ONE, "cmp_func_idx"))) { diff --git a/core/iwasm/compilation/aot_emit_table.c b/core/iwasm/compilation/aot_emit_table.c index d8a5efd91..ef10fa33d 100644 --- a/core/iwasm/compilation/aot_emit_table.c +++ b/core/iwasm/compilation/aot_emit_table.c @@ -24,7 +24,7 @@ get_tbl_inst_offset(const AOTCompContext *comp_ctx, offset += offsetof(AOTTableInstance, elems); /* avoid loading from current AOTTableInstance */ offset += - sizeof(uint32) + comp_ctx->pointer_size * aot_get_imp_tbl_data_slots(imp_tbls + i, comp_ctx->is_jit_mode); ++i; } @@ -38,7 +38,7 @@ get_tbl_inst_offset(const AOTCompContext *comp_ctx, while (i < tbl_idx && i < comp_ctx->comp_data->table_count) { offset += offsetof(AOTTableInstance, elems); /* avoid loading from current AOTTableInstance */ - offset += sizeof(uint32) + offset += comp_ctx->pointer_size * aot_get_tbl_data_slots(tbls + i, comp_ctx->is_jit_mode); ++i; } @@ -194,25 +194,31 @@ aot_compile_op_table_get(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, } if (!(table_elem = LLVMBuildBitCast(comp_ctx->builder, table_elem, - INT32_PTR_TYPE, "table_elem_i32p"))) { + INTPTR_PTR_TYPE, "table_elem_i32p"))) { HANDLE_FAILURE("LLVMBuildBitCast"); goto fail; } /* Load function index */ if (!(table_elem = - LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE, table_elem, + LLVMBuildInBoundsGEP2(comp_ctx->builder, INTPTR_TYPE, table_elem, &elem_idx, 1, "table_elem"))) { HANDLE_FAILURE("LLVMBuildNUWAdd"); goto fail; } - if (!(func_idx = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, table_elem, + if (!(func_idx = LLVMBuildLoad2(comp_ctx->builder, INTPTR_TYPE, table_elem, "func_idx"))) { HANDLE_FAILURE("LLVMBuildLoad"); goto fail; } + if (!(func_idx = LLVMBuildIntCast2(comp_ctx->builder, func_idx, I32_TYPE, + true, "func_idx_i32"))) { + HANDLE_FAILURE("LLVMBuildIntCast"); + goto fail; + } + PUSH_I32(func_idx); return true; @@ -248,14 +254,14 @@ aot_compile_op_table_set(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, } if (!(table_elem = LLVMBuildBitCast(comp_ctx->builder, table_elem, - INT32_PTR_TYPE, "table_elem_i32p"))) { + INTPTR_PTR_TYPE, "table_elem_i32p"))) { HANDLE_FAILURE("LLVMBuildBitCast"); goto fail; } /* Load function index */ if (!(table_elem = - LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE, table_elem, + LLVMBuildInBoundsGEP2(comp_ctx->builder, INTPTR_TYPE, table_elem, &elem_idx, 1, "table_elem"))) { HANDLE_FAILURE("LLVMBuildInBoundsGEP"); goto fail; diff --git a/core/iwasm/compilation/aot_llvm.c b/core/iwasm/compilation/aot_llvm.c index 27550560f..012e8981b 100644 --- a/core/iwasm/compilation/aot_llvm.c +++ b/core/iwasm/compilation/aot_llvm.c @@ -1088,7 +1088,8 @@ aot_create_func_contexts(AOTCompData *comp_data, AOTCompContext *comp_ctx) } static bool -aot_set_llvm_basic_types(AOTLLVMTypes *basic_types, LLVMContextRef context) +aot_set_llvm_basic_types(AOTLLVMTypes *basic_types, LLVMContextRef context, + int pointer_size) { basic_types->int1_type = LLVMInt1TypeInContext(context); basic_types->int8_type = LLVMInt8TypeInContext(context); @@ -1131,9 +1132,19 @@ aot_set_llvm_basic_types(AOTLLVMTypes *basic_types, LLVMContextRef context) basic_types->funcref_type = LLVMInt32TypeInContext(context); basic_types->externref_type = LLVMInt32TypeInContext(context); + if (pointer_size == 4) { + basic_types->intptr_type = basic_types->int32_type; + basic_types->intptr_ptr_type = basic_types->int32_ptr_type; + } + else { + basic_types->intptr_type = basic_types->int64_type; + basic_types->intptr_ptr_type = basic_types->int64_ptr_type; + } + return (basic_types->int8_ptr_type && basic_types->int8_pptr_type && basic_types->int16_ptr_type && basic_types->int32_ptr_type - && basic_types->int64_ptr_type && basic_types->float32_ptr_type + && basic_types->int64_ptr_type && basic_types->intptr_type + && basic_types->intptr_ptr_type && basic_types->float32_ptr_type && basic_types->float64_ptr_type && basic_types->i8x16_vec_type && basic_types->i16x8_vec_type && basic_types->i32x4_vec_type && basic_types->i64x2_vec_type && basic_types->f32x4_vec_type @@ -2112,7 +2123,8 @@ aot_create_comp_context(AOTCompData *comp_data, aot_comp_option_t option) goto fail; } - if (!aot_set_llvm_basic_types(&comp_ctx->basic_types, comp_ctx->context)) { + if (!aot_set_llvm_basic_types(&comp_ctx->basic_types, comp_ctx->context, + comp_ctx->pointer_size)) { aot_set_last_error("create LLVM basic types failed."); goto fail; } diff --git a/core/iwasm/compilation/aot_llvm.h b/core/iwasm/compilation/aot_llvm.h index b982e8083..aa3af1b2b 100644 --- a/core/iwasm/compilation/aot_llvm.h +++ b/core/iwasm/compilation/aot_llvm.h @@ -193,6 +193,7 @@ typedef struct AOTLLVMTypes { LLVMTypeRef int16_type; LLVMTypeRef int32_type; LLVMTypeRef int64_type; + LLVMTypeRef intptr_type; LLVMTypeRef float32_type; LLVMTypeRef float64_type; LLVMTypeRef void_type; @@ -202,6 +203,7 @@ typedef struct AOTLLVMTypes { LLVMTypeRef int16_ptr_type; LLVMTypeRef int32_ptr_type; LLVMTypeRef int64_ptr_type; + LLVMTypeRef intptr_ptr_type; LLVMTypeRef float32_ptr_type; LLVMTypeRef float64_ptr_type; diff --git a/core/iwasm/fast-jit/fe/jit_emit_function.c b/core/iwasm/fast-jit/fe/jit_emit_function.c index a89f2ff59..2fc17b9c8 100644 --- a/core/iwasm/fast-jit/fe/jit_emit_function.c +++ b/core/iwasm/fast-jit/fe/jit_emit_function.c @@ -480,7 +480,9 @@ jit_compile_op_call_indirect(JitCompContext *cc, uint32 type_idx, if (UINTPTR_MAX == UINT64_MAX) { offset_i32 = jit_cc_new_reg_I32(cc); offset = jit_cc_new_reg_I64(cc); - GEN_INSN(SHL, offset_i32, elem_idx, NEW_CONST(I32, 2)); + /* Calculate offset by pointer size (elem_idx * + * sizeof(table_elem_type_t)) */ + GEN_INSN(SHL, offset_i32, elem_idx, NEW_CONST(I32, 3)); GEN_INSN(I32TOI64, offset, offset_i32); } else { diff --git a/core/iwasm/fast-jit/fe/jit_emit_table.c b/core/iwasm/fast-jit/fe/jit_emit_table.c index 9fb61931f..9584eaeb3 100644 --- a/core/iwasm/fast-jit/fe/jit_emit_table.c +++ b/core/iwasm/fast-jit/fe/jit_emit_table.c @@ -45,7 +45,8 @@ jit_compile_op_table_get(JitCompContext *cc, uint32 tbl_idx) GEN_INSN(I32TOI64, elem_idx_long, elem_idx); offset = jit_cc_new_reg_I64(cc); - GEN_INSN(MUL, offset, elem_idx_long, NEW_CONST(I64, sizeof(uint32))); + GEN_INSN(MUL, offset, elem_idx_long, + NEW_CONST(I64, sizeof(table_elem_type_t))); res = jit_cc_new_reg_I32(cc); tbl_elems = get_table_elems_reg(cc->jit_frame, tbl_idx); @@ -76,7 +77,8 @@ jit_compile_op_table_set(JitCompContext *cc, uint32 tbl_idx) GEN_INSN(I32TOI64, elem_idx_long, elem_idx); offset = jit_cc_new_reg_I64(cc); - GEN_INSN(MUL, offset, elem_idx_long, NEW_CONST(I64, sizeof(uint32))); + GEN_INSN(MUL, offset, elem_idx_long, + NEW_CONST(I64, sizeof(table_elem_type_t))); tbl_elems = get_table_elems_reg(cc->jit_frame, tbl_idx); GEN_INSN(STI32, elem_val, tbl_elems, offset); @@ -106,9 +108,10 @@ wasm_init_table(WASMModuleInstance *inst, uint32 tbl_idx, uint32 elem_idx, goto out_of_bounds; bh_memcpy_s((uint8 *)tbl + offsetof(WASMTableInstance, elems) - + dst * sizeof(uint32), - (uint32)((tbl_sz - dst) * sizeof(uint32)), - elem->func_indexes + src, (uint32)(len * sizeof(uint32))); + + dst * sizeof(table_elem_type_t), + (uint32)((tbl_sz - dst) * sizeof(table_elem_type_t)), + elem->func_indexes + src, + (uint32)(len * sizeof(table_elem_type_t))); return 0; out_of_bounds: @@ -167,12 +170,13 @@ wasm_copy_table(WASMModuleInstance *inst, uint32 src_tbl_idx, if (dst_offset > dst_tbl_sz || dst_tbl_sz - dst_offset < len) goto out_of_bounds; - bh_memmove_s((uint8 *)dst_tbl + offsetof(WASMTableInstance, elems) - + dst_offset * sizeof(uint32), - (uint32)((dst_tbl_sz - dst_offset) * sizeof(uint32)), - (uint8 *)src_tbl + offsetof(WASMTableInstance, elems) - + src_offset * sizeof(uint32), - (uint32)(len * sizeof(uint32))); + bh_memmove_s( + (uint8 *)dst_tbl + offsetof(WASMTableInstance, elems) + + dst_offset * sizeof(table_elem_type_t), + (uint32)((dst_tbl_sz - dst_offset) * sizeof(table_elem_type_t)), + (uint8 *)src_tbl + offsetof(WASMTableInstance, elems) + + src_offset * sizeof(table_elem_type_t), + (uint32)(len * sizeof(table_elem_type_t))); return 0; out_of_bounds: @@ -264,7 +268,7 @@ fail: static int wasm_fill_table(WASMModuleInstance *inst, uint32 tbl_idx, uint32 dst, - uint32 val, uint32 len) + uintptr_t val, uint32 len) { WASMTableInstance *tbl; uint32 tbl_sz; diff --git a/core/iwasm/fast-jit/jit_frontend.c b/core/iwasm/fast-jit/jit_frontend.c index 5ca829645..a63c1fbf2 100644 --- a/core/iwasm/fast-jit/jit_frontend.c +++ b/core/iwasm/fast-jit/jit_frontend.c @@ -97,9 +97,9 @@ jit_frontend_get_table_inst_offset(const WASMModule *module, uint32 tbl_idx) offset += (uint32)offsetof(WASMTableInstance, elems); #if WASM_ENABLE_MULTI_MODULE != 0 - offset += (uint32)sizeof(uint32) * table->max_size; + offset += (uint32)sizeof(table_elem_type_t) * table->max_size; #else - offset += (uint32)sizeof(uint32) + offset += (uint32)sizeof(table_elem_type_t) * (table->possible_grow ? table->max_size : table->init_size); #endif diff --git a/core/iwasm/interpreter/wasm.h b/core/iwasm/interpreter/wasm.h index 2af97b8ae..f85834f4e 100644 --- a/core/iwasm/interpreter/wasm.h +++ b/core/iwasm/interpreter/wasm.h @@ -79,7 +79,7 @@ extern "C" { #define DEFAULT_MAX_PAGES 65536 #if WASM_ENABLE_GC == 0 -typedef uint32 table_elem_type_t; +typedef uintptr_t table_elem_type_t; #define NULL_REF (0xFFFFFFFF) #else typedef void *table_elem_type_t; @@ -610,7 +610,7 @@ typedef struct WASMTableSeg { uint32 table_index; InitializerExpression base_offset; uint32 function_count; - uint32 *func_indexes; + uintptr_t *func_indexes; } WASMTableSeg; typedef struct WASMDataSeg { diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index ff4bc44b2..940fe84bc 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -4222,7 +4222,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, WASMTableInstance *tbl_inst; #if WASM_ENABLE_GC != 0 void **table_elems; - uint32 *func_indexes; + uintptr_t *func_indexes; #endif read_leb_uint32(frame_ip, frame_ip_end, elem_idx); @@ -4267,15 +4267,15 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, } #if WASM_ENABLE_GC == 0 - bh_memcpy_s( - (uint8 *)tbl_inst - + offsetof(WASMTableInstance, elems) - + d * sizeof(uint32), - (uint32)((tbl_inst->cur_size - d) * sizeof(uint32)), - module->module->table_segments[elem_idx] - .func_indexes - + s, - (uint32)(n * sizeof(uint32))); + bh_memcpy_s((uint8 *)tbl_inst + + offsetof(WASMTableInstance, elems) + + d * sizeof(table_elem_type_t), + (uint32)((tbl_inst->cur_size - d) + * sizeof(table_elem_type_t)), + module->module->table_segments[elem_idx] + .func_indexes + + s, + (uint32)(n * sizeof(table_elem_type_t))); #else SYNC_ALL_TO_FRAME(); table_elems = tbl_inst->elems + d; diff --git a/core/iwasm/interpreter/wasm_interp_fast.c b/core/iwasm/interpreter/wasm_interp_fast.c index 1704e6641..791d35ab4 100644 --- a/core/iwasm/interpreter/wasm_interp_fast.c +++ b/core/iwasm/interpreter/wasm_interp_fast.c @@ -4026,7 +4026,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, WASMTableInstance *tbl_inst; #if WASM_ENABLE_GC != 0 void **table_elems; - uint32 *func_indexes; + uintptr_t *func_indexes; uint64 i; #endif @@ -4070,15 +4070,15 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, } #if WASM_ENABLE_GC == 0 - bh_memcpy_s( - (uint8 *)tbl_inst - + offsetof(WASMTableInstance, elems) - + d * sizeof(uint32), - (uint32)((tbl_inst->cur_size - d) * sizeof(uint32)), - module->module->table_segments[elem_idx] - .func_indexes - + s, - (uint32)(n * sizeof(uint32))); + bh_memcpy_s((uint8 *)tbl_inst + + offsetof(WASMTableInstance, elems) + + d * sizeof(table_elem_type_t), + (uint32)((tbl_inst->cur_size - d) + * sizeof(table_elem_type_t)), + module->module->table_segments[elem_idx] + .func_indexes + + s, + (uint32)(n * sizeof(table_elem_type_t))); #else SYNC_ALL_TO_FRAME(); table_elems = tbl_inst->elems + d; diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index 63a03be99..025a51dc6 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -3468,9 +3468,9 @@ load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end, read_leb_uint32(p, p_end, function_count); table_segment->function_count = function_count; - total_size = sizeof(uint32) * (uint64)function_count; + total_size = sizeof(uintptr_t) * (uint64)function_count; if (total_size > 0 - && !(table_segment->func_indexes = (uint32 *)loader_malloc( + && !(table_segment->func_indexes = (uintptr_t *)loader_malloc( total_size, error_buf, error_buf_size))) { return false; } diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index 15f4cf3b3..eadd8c7ae 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -1345,9 +1345,9 @@ load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end, read_leb_uint32(p, p_end, function_count); table_segment->function_count = function_count; - total_size = sizeof(uint32) * (uint64)function_count; + total_size = sizeof(uintptr_t) * (uint64)function_count; if (total_size > 0 - && !(table_segment->func_indexes = (uint32 *)loader_malloc( + && !(table_segment->func_indexes = (uintptr_t *)loader_malloc( total_size, error_buf, error_buf_size))) { return false; } diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 5d11711ae..70274b952 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -640,7 +640,7 @@ tables_instantiate(const WASMModule *module, WASMModuleInstance *module_inst, #endif #if WASM_ENABLE_GC == 0 /* Store function indexes */ - total_size += sizeof(uint32) * (uint64)max_size_fixed; + total_size += sizeof(uintptr_t) * (uint64)max_size_fixed; #else /* Store object pointers */ total_size += sizeof(uintptr_t) * (uint64)max_size_fixed; @@ -2313,8 +2313,8 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, bh_memcpy_s( table_data + table_seg->base_offset.u.i32, (uint32)((table->cur_size - (uint32)table_seg->base_offset.u.i32) - * sizeof(uint32)), - table_seg->func_indexes, (uint32)(length * sizeof(uint32))); + * sizeof(table_elem_type_t)), + table_seg->func_indexes, (uint32)(length * sizeof(uintptr_t))); #else for (j = 0; j < length; j++) { WASMFuncObjectRef func_obj; @@ -3628,10 +3628,11 @@ llvm_jit_table_init(WASMModuleInstance *module_inst, uint32 tbl_idx, } bh_memcpy_s((uint8 *)tbl_inst + offsetof(WASMTableInstance, elems) - + dst_offset * sizeof(uint32), - (uint32)sizeof(uint32) * (tbl_inst->cur_size - dst_offset), + + dst_offset * sizeof(table_elem_type_t), + (uint32)sizeof(table_elem_type_t) + * (tbl_inst->cur_size - dst_offset), tbl_seg->func_indexes + src_offset, - (uint32)(length * sizeof(uint32))); + (uint32)(length * sizeof(table_elem_type_t))); } void @@ -3665,11 +3666,12 @@ llvm_jit_table_copy(WASMModuleInstance *module_inst, uint32 src_tbl_idx, /* if src_offset < dst_offset, copy from back to front */ /* merge all together */ bh_memmove_s((uint8 *)dst_tbl_inst + offsetof(WASMTableInstance, elems) - + sizeof(uint32) * dst_offset, - (uint32)sizeof(uint32) * (dst_tbl_inst->cur_size - dst_offset), + + sizeof(table_elem_type_t) * dst_offset, + (uint32)sizeof(table_elem_type_t) + * (dst_tbl_inst->cur_size - dst_offset), (uint8 *)src_tbl_inst + offsetof(WASMTableInstance, elems) - + sizeof(uint32) * src_offset, - (uint32)sizeof(uint32) * length); + + sizeof(table_elem_type_t) * src_offset, + (uint32)sizeof(table_elem_type_t) * length); } void