Fix memory64 handling find_block_addr and execute_main (#3480)

This commit is contained in:
Wenyong Huang 2024-05-30 16:14:12 +08:00 committed by GitHub
parent 1f8a78d61a
commit 5623e4d22a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 40 additions and 18 deletions

View File

@ -201,9 +201,23 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
if (func_type->param_count) {
for (i = 0; i < argc; i++)
total_argv_size += (uint32)(strlen(argv[i]) + 1);
total_argv_size = align_uint(total_argv_size, 4);
#if WASM_ENABLE_MEMORY64 != 0
if (is_memory64)
/* `char **argv` is an array of 64-bit elements in memory64 */
total_argv_size = align_uint(total_argv_size, 8);
else
#endif
total_argv_size = align_uint(total_argv_size, 4);
total_size = (uint64)total_argv_size + sizeof(int32) * (uint64)argc;
#if WASM_ENABLE_MEMORY64 != 0
if (is_memory64)
/* `char **argv` is an array of 64-bit elements in memory64 */
total_size =
(uint64)total_argv_size + sizeof(uint64) * (uint64)argc;
else
#endif
total_size =
(uint64)total_argv_size + sizeof(uint32) * (uint64)argc;
if (total_size >= UINT32_MAX
|| !(argv_buf_offset = wasm_runtime_module_malloc(
@ -219,7 +233,15 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
for (i = 0; i < argc; i++) {
bh_memcpy_s(p, (uint32)(p_end - p), argv[i],
(uint32)(strlen(argv[i]) + 1));
argv_offsets[i] = (uint32)argv_buf_offset + (uint32)(p - argv_buf);
#if WASM_ENABLE_MEMORY64 != 0
if (is_memory64)
/* `char **argv` is an array of 64-bit elements in memory64 */
((uint64 *)argv_offsets)[i] =
(uint32)argv_buf_offset + (uint32)(p - argv_buf);
else
#endif
argv_offsets[i] =
(uint32)argv_buf_offset + (uint32)(p - argv_buf);
p += strlen(argv[i]) + 1;
}

View File

@ -3446,10 +3446,10 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
{
/* clang-format off */
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
local_offset = *frame_ip++;
local_offset = *frame_ip++;
#else
local_offset = *frame_ip;
frame_ip += 2;
local_offset = *frame_ip;
frame_ip += 2;
#endif
/* clang-format on */
*(uint32 *)(frame_lp + local_offset) =
@ -3463,10 +3463,10 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
{
/* clang-format off */
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
local_offset = *frame_ip++;
local_offset = *frame_ip++;
#else
local_offset = *frame_ip;
frame_ip += 2;
local_offset = *frame_ip;
frame_ip += 2;
#endif
/* clang-format on */
PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),

View File

@ -6414,7 +6414,7 @@ create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
char *error_buf, uint32 error_buf_size)
{
WASMSection *section_list_end = NULL, *section;
const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
const uint8 *p = buf, *p_end = buf + size;
uint8 section_type, section_index, last_section_index = (uint8)-1;
uint32 section_size;
@ -7658,7 +7658,6 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
case WASM_OP_SIMD_PREFIX:
{
/* TODO: memory64 offset type changes */
uint32 opcode1;
read_leb_uint32(p, p_end, opcode1);
@ -7683,8 +7682,8 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
case SIMD_v128_store:
/* memarg align */
skip_leb_uint32(p, p_end);
/* memarg offset*/
skip_leb_uint32(p, p_end);
/* memarg offset */
skip_leb_mem_offset(p, p_end);
break;
case SIMD_v128_const:
@ -7723,8 +7722,8 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
case SIMD_v128_store64_lane:
/* memarg align */
skip_leb_uint32(p, p_end);
/* memarg offset*/
skip_leb_uint32(p, p_end);
/* memarg offset */
skip_leb_mem_offset(p, p_end);
/* ImmLaneId */
CHECK_BUF(p, p_end, 1);
p++;
@ -7734,8 +7733,8 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
case SIMD_v128_load64_zero:
/* memarg align */
skip_leb_uint32(p, p_end);
/* memarg offset*/
skip_leb_uint32(p, p_end);
/* memarg offset */
skip_leb_mem_offset(p, p_end);
break;
default:

View File

@ -6942,7 +6942,8 @@ re_scan:
uint32 j;
for (i = 0; i < module->global_count; i++) {
if (module->globals[i].type == VALUE_TYPE_FUNCREF
if (module->globals[i].type.val_type
== VALUE_TYPE_FUNCREF
&& module->globals[i].init_expr.init_expr_type
== INIT_EXPR_TYPE_FUNCREF_CONST
&& module->globals[i].init_expr.u.u32 == func_idx) {