Access linear memory size atomically (#2834)

Fixes: https://github.com/bytecodealliance/wasm-micro-runtime/issues/2804
This commit is contained in:
Enrico Loparco 2023-11-29 12:27:17 +00:00 committed by GitHub
parent b81abd01ef
commit 0455071fc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 16 deletions

View File

@ -686,7 +686,7 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
memory->num_bytes_per_page = num_bytes_per_page;
memory->cur_page_count = total_page_count;
memory->max_page_count = max_page_count;
memory->memory_data_size = (uint32)total_size_new;
SET_LINEAR_MEMORY_SIZE(memory, (uint32)total_size_new);
memory->memory_data_end = memory->memory_data + (uint32)total_size_new;
wasm_runtime_set_mem_bound_check_bytes(memory, total_size_new);
@ -844,7 +844,7 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
memory->num_bytes_per_page = num_bytes_per_page;
memory->cur_page_count = total_page_count;
memory->max_page_count = max_page_count;
memory->memory_data_size = (uint32)total_size_new;
SET_LINEAR_MEMORY_SIZE(memory, (uint32)total_size_new);
memory->memory_data_end = memory->memory_data + (uint32)total_size_new;
wasm_runtime_set_mem_bound_check_bytes(memory, total_size_new);

View File

@ -14,6 +14,16 @@
extern "C" {
#endif
#if WASM_ENABLE_SHARED_MEMORY != 0
#define GET_LINEAR_MEMORY_SIZE(memory) \
BH_ATOMIC_32_LOAD(memory->memory_data_size)
#define SET_LINEAR_MEMORY_SIZE(memory, size) \
BH_ATOMIC_32_STORE(memory->memory_data_size, size)
#else
#define GET_LINEAR_MEMORY_SIZE(memory) memory->memory_data_size
#define SET_LINEAR_MEMORY_SIZE(memory, size) memory->memory_data_size = size
#endif
bool
wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
const MemAllocOption *alloc_option);

View File

@ -36,7 +36,7 @@ typedef float64 CellType_F64;
* multi-threading mode since it may be changed by other
* threads in memory.grow
*/
#define get_linear_mem_size() memory->memory_data_size
#define get_linear_mem_size() GET_LINEAR_MEMORY_SIZE(memory)
#endif
#if !defined(OS_ENABLE_HW_BOUND_CHECK) \
@ -1155,7 +1155,13 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
#if !defined(OS_ENABLE_HW_BOUND_CHECK) \
|| WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
|| WASM_ENABLE_BULK_MEMORY != 0
uint32 linear_mem_size = memory ? memory->memory_data_size : 0;
uint32 linear_mem_size = 0;
if (memory)
#if WASM_ENABLE_THREAD_MGR == 0
linear_mem_size = memory->memory_data_size;
#else
linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory);
#endif
#endif
WASMType **wasm_types = module->module->types;
WASMGlobalInstance *globals = module->e->globals, *global;
@ -2143,7 +2149,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
#if !defined(OS_ENABLE_HW_BOUND_CHECK) \
|| WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
|| WASM_ENABLE_BULK_MEMORY != 0
linear_mem_size = memory->memory_data_size;
linear_mem_size = get_linear_mem_size();
#endif
}
@ -3148,7 +3154,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
addr = (uint32)POP_I32();
#if WASM_ENABLE_THREAD_MGR != 0
linear_mem_size = memory->memory_data_size;
linear_mem_size = get_linear_mem_size();
#endif
#ifndef OS_ENABLE_HW_BOUND_CHECK
@ -3199,7 +3205,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
dst = POP_I32();
#if WASM_ENABLE_THREAD_MGR != 0
linear_mem_size = memory->memory_data_size;
linear_mem_size = get_linear_mem_size();
#endif
#ifndef OS_ENABLE_HW_BOUND_CHECK
@ -3230,7 +3236,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
dst = POP_I32();
#if WASM_ENABLE_THREAD_MGR != 0
linear_mem_size = memory->memory_data_size;
linear_mem_size = get_linear_mem_size();
#endif
#ifndef OS_ENABLE_HW_BOUND_CHECK
@ -3907,7 +3913,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|| WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
|| WASM_ENABLE_BULK_MEMORY != 0
if (memory)
linear_mem_size = memory->memory_data_size;
linear_mem_size = get_linear_mem_size();
#endif
if (wasm_copy_exception(module, NULL))
goto got_exception;

View File

@ -27,7 +27,7 @@ typedef float64 CellType_F64;
* multi-threading mode since it may be changed by other
* threads in memory.grow
*/
#define get_linear_mem_size() memory->memory_data_size
#define get_linear_mem_size() GET_LINEAR_MEMORY_SIZE(memory)
#endif
#if !defined(OS_ENABLE_HW_BOUND_CHECK) \
@ -1180,7 +1180,13 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
#if !defined(OS_ENABLE_HW_BOUND_CHECK) \
|| WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
|| WASM_ENABLE_BULK_MEMORY != 0
uint32 linear_mem_size = memory ? memory->memory_data_size : 0;
uint32 linear_mem_size = 0;
if (memory)
#if WASM_ENABLE_THREAD_MGR == 0
linear_mem_size = memory->memory_data_size;
#else
linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory);
#endif
#endif
WASMGlobalInstance *globals = module->e ? module->e->globals : NULL;
WASMGlobalInstance *global;
@ -1911,7 +1917,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
#if !defined(OS_ENABLE_HW_BOUND_CHECK) \
|| WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
|| WASM_ENABLE_BULK_MEMORY != 0
linear_mem_size = memory->memory_data_size;
linear_mem_size = get_linear_mem_size();
#endif
}
@ -2994,7 +3000,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
addr = POP_I32();
#if WASM_ENABLE_THREAD_MGR
linear_mem_size = memory->memory_data_size;
linear_mem_size = get_linear_mem_size();
#endif
#ifndef OS_ENABLE_HW_BOUND_CHECK
@ -3043,7 +3049,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
dst = POP_I32();
#if WASM_ENABLE_THREAD_MGR
linear_mem_size = memory->memory_data_size;
linear_mem_size = get_linear_mem_size();
#endif
#ifndef OS_ENABLE_HW_BOUND_CHECK
@ -3073,7 +3079,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
dst = POP_I32();
#if WASM_ENABLE_THREAD_MGR
linear_mem_size = memory->memory_data_size;
linear_mem_size = get_linear_mem_size();
#endif
#ifndef OS_ENABLE_HW_BOUND_CHECK
@ -3848,7 +3854,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|| WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
|| WASM_ENABLE_BULK_MEMORY != 0
if (memory)
linear_mem_size = memory->memory_data_size;
linear_mem_size = get_linear_mem_size();
#endif
if (wasm_copy_exception(module, NULL))
goto got_exception;