Skip to content

Commit 5623e4d

Browse files
authored
Fix memory64 handling find_block_addr and execute_main (#3480)
1 parent 1f8a78d commit 5623e4d

4 files changed

Lines changed: 40 additions & 18 deletions

File tree

core/iwasm/common/wasm_application.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,23 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
201201
if (func_type->param_count) {
202202
for (i = 0; i < argc; i++)
203203
total_argv_size += (uint32)(strlen(argv[i]) + 1);
204-
total_argv_size = align_uint(total_argv_size, 4);
204+
#if WASM_ENABLE_MEMORY64 != 0
205+
if (is_memory64)
206+
/* `char **argv` is an array of 64-bit elements in memory64 */
207+
total_argv_size = align_uint(total_argv_size, 8);
208+
else
209+
#endif
210+
total_argv_size = align_uint(total_argv_size, 4);
205211

206-
total_size = (uint64)total_argv_size + sizeof(int32) * (uint64)argc;
212+
#if WASM_ENABLE_MEMORY64 != 0
213+
if (is_memory64)
214+
/* `char **argv` is an array of 64-bit elements in memory64 */
215+
total_size =
216+
(uint64)total_argv_size + sizeof(uint64) * (uint64)argc;
217+
else
218+
#endif
219+
total_size =
220+
(uint64)total_argv_size + sizeof(uint32) * (uint64)argc;
207221

208222
if (total_size >= UINT32_MAX
209223
|| !(argv_buf_offset = wasm_runtime_module_malloc(
@@ -219,7 +233,15 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
219233
for (i = 0; i < argc; i++) {
220234
bh_memcpy_s(p, (uint32)(p_end - p), argv[i],
221235
(uint32)(strlen(argv[i]) + 1));
222-
argv_offsets[i] = (uint32)argv_buf_offset + (uint32)(p - argv_buf);
236+
#if WASM_ENABLE_MEMORY64 != 0
237+
if (is_memory64)
238+
/* `char **argv` is an array of 64-bit elements in memory64 */
239+
((uint64 *)argv_offsets)[i] =
240+
(uint32)argv_buf_offset + (uint32)(p - argv_buf);
241+
else
242+
#endif
243+
argv_offsets[i] =
244+
(uint32)argv_buf_offset + (uint32)(p - argv_buf);
223245
p += strlen(argv[i]) + 1;
224246
}
225247

core/iwasm/interpreter/wasm_interp_fast.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3446,10 +3446,10 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
34463446
{
34473447
/* clang-format off */
34483448
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
3449-
local_offset = *frame_ip++;
3449+
local_offset = *frame_ip++;
34503450
#else
3451-
local_offset = *frame_ip;
3452-
frame_ip += 2;
3451+
local_offset = *frame_ip;
3452+
frame_ip += 2;
34533453
#endif
34543454
/* clang-format on */
34553455
*(uint32 *)(frame_lp + local_offset) =
@@ -3463,10 +3463,10 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
34633463
{
34643464
/* clang-format off */
34653465
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
3466-
local_offset = *frame_ip++;
3466+
local_offset = *frame_ip++;
34673467
#else
3468-
local_offset = *frame_ip;
3469-
frame_ip += 2;
3468+
local_offset = *frame_ip;
3469+
frame_ip += 2;
34703470
#endif
34713471
/* clang-format on */
34723472
PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),

core/iwasm/interpreter/wasm_loader.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6414,7 +6414,7 @@ create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
64146414
char *error_buf, uint32 error_buf_size)
64156415
{
64166416
WASMSection *section_list_end = NULL, *section;
6417-
const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
6417+
const uint8 *p = buf, *p_end = buf + size;
64186418
uint8 section_type, section_index, last_section_index = (uint8)-1;
64196419
uint32 section_size;
64206420

@@ -7658,7 +7658,6 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
76587658
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
76597659
case WASM_OP_SIMD_PREFIX:
76607660
{
7661-
/* TODO: memory64 offset type changes */
76627661
uint32 opcode1;
76637662

76647663
read_leb_uint32(p, p_end, opcode1);
@@ -7683,8 +7682,8 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
76837682
case SIMD_v128_store:
76847683
/* memarg align */
76857684
skip_leb_uint32(p, p_end);
7686-
/* memarg offset*/
7687-
skip_leb_uint32(p, p_end);
7685+
/* memarg offset */
7686+
skip_leb_mem_offset(p, p_end);
76887687
break;
76897688

76907689
case SIMD_v128_const:
@@ -7723,8 +7722,8 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
77237722
case SIMD_v128_store64_lane:
77247723
/* memarg align */
77257724
skip_leb_uint32(p, p_end);
7726-
/* memarg offset*/
7727-
skip_leb_uint32(p, p_end);
7725+
/* memarg offset */
7726+
skip_leb_mem_offset(p, p_end);
77287727
/* ImmLaneId */
77297728
CHECK_BUF(p, p_end, 1);
77307729
p++;
@@ -7734,8 +7733,8 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
77347733
case SIMD_v128_load64_zero:
77357734
/* memarg align */
77367735
skip_leb_uint32(p, p_end);
7737-
/* memarg offset*/
7738-
skip_leb_uint32(p, p_end);
7736+
/* memarg offset */
7737+
skip_leb_mem_offset(p, p_end);
77397738
break;
77407739

77417740
default:

core/iwasm/interpreter/wasm_mini_loader.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6942,7 +6942,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
69426942
uint32 j;
69436943

69446944
for (i = 0; i < module->global_count; i++) {
6945-
if (module->globals[i].type == VALUE_TYPE_FUNCREF
6945+
if (module->globals[i].type.val_type
6946+
== VALUE_TYPE_FUNCREF
69466947
&& module->globals[i].init_expr.init_expr_type
69476948
== INIT_EXPR_TYPE_FUNCREF_CONST
69486949
&& module->globals[i].init_expr.u.u32 == func_idx) {

0 commit comments

Comments
 (0)