Fix linear memory page count issues (#1380)
Fix issue reported in #1289 and #1371. Enable to set the max page count to 65536.
This commit is contained in:
@ -412,18 +412,27 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
|
||||
if (init_page_count == max_page_count && init_page_count == 1) {
|
||||
/* If only one page and at most one page, we just append
|
||||
the app heap to the end of linear memory, enlarge the
|
||||
num_bytes_per_page, and don't change the page count*/
|
||||
num_bytes_per_page, and don't change the page count */
|
||||
heap_offset = num_bytes_per_page;
|
||||
num_bytes_per_page += heap_size;
|
||||
if (num_bytes_per_page < heap_size) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"memory size must be at most 65536 pages (4GiB)");
|
||||
"failed to insert app heap into linear memory, "
|
||||
"try using `--heap_size=0` option");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
else if (heap_size > 0) {
|
||||
if (module->aux_heap_base_global_index != (uint32)-1
|
||||
&& module->aux_heap_base < num_bytes_per_page * init_page_count) {
|
||||
if (init_page_count == max_page_count && init_page_count == 0) {
|
||||
/* If the memory data size is always 0, we resize it to
|
||||
one page for app heap */
|
||||
num_bytes_per_page = heap_size;
|
||||
heap_offset = 0;
|
||||
inc_page_count = 1;
|
||||
}
|
||||
else if (module->aux_heap_base_global_index != (uint32)-1
|
||||
&& module->aux_heap_base
|
||||
< num_bytes_per_page * init_page_count) {
|
||||
/* Insert app heap before __heap_base */
|
||||
aux_heap_base = module->aux_heap_base;
|
||||
bytes_of_last_page = aux_heap_base % num_bytes_per_page;
|
||||
@ -464,13 +473,18 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
|
||||
}
|
||||
init_page_count += inc_page_count;
|
||||
max_page_count += inc_page_count;
|
||||
if (init_page_count > 65536) {
|
||||
if (init_page_count > DEFAULT_MAX_PAGES) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"memory size must be at most 65536 pages (4GiB)");
|
||||
"failed to insert app heap into linear memory, "
|
||||
"try using `--heap_size=0` option");
|
||||
return NULL;
|
||||
}
|
||||
if (max_page_count > 65536)
|
||||
max_page_count = 65536;
|
||||
else if (init_page_count == DEFAULT_MAX_PAGES) {
|
||||
num_bytes_per_page = UINT32_MAX;
|
||||
init_page_count = max_page_count = 1;
|
||||
}
|
||||
if (max_page_count > DEFAULT_MAX_PAGES)
|
||||
max_page_count = DEFAULT_MAX_PAGES;
|
||||
}
|
||||
|
||||
LOG_VERBOSE("Memory instantiate:");
|
||||
@ -487,6 +501,7 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
|
||||
total_size = (uint64)num_bytes_per_page * max_page_count;
|
||||
}
|
||||
#endif
|
||||
bh_assert(total_size <= UINT32_MAX);
|
||||
|
||||
#ifndef OS_ENABLE_HW_BOUND_CHECK
|
||||
/* Allocate memory */
|
||||
@ -502,9 +517,8 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
|
||||
* both i and memarg.offset are u32 in range 0 to 4G
|
||||
* so the range of ea is 0 to 8G
|
||||
*/
|
||||
if (total_size >= UINT32_MAX
|
||||
|| !(p = mapped_mem =
|
||||
os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) {
|
||||
if (!(p = mapped_mem =
|
||||
os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) {
|
||||
set_error_buf(error_buf, error_buf_size, "mmap memory failed");
|
||||
return NULL;
|
||||
}
|
||||
@ -529,15 +543,18 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
|
||||
* again here */
|
||||
#endif /* end of OS_ENABLE_HW_BOUND_CHECK */
|
||||
|
||||
if (total_size > UINT32_MAX)
|
||||
total_size = UINT32_MAX;
|
||||
|
||||
memory_inst->module_type = Wasm_Module_AoT;
|
||||
memory_inst->num_bytes_per_page = num_bytes_per_page;
|
||||
memory_inst->cur_page_count = init_page_count;
|
||||
memory_inst->max_page_count = max_page_count;
|
||||
memory_inst->memory_data_size = (uint32)total_size;
|
||||
|
||||
/* Init memory info */
|
||||
memory_inst->memory_data.ptr = p;
|
||||
memory_inst->memory_data_end.ptr = p + (uint32)total_size;
|
||||
memory_inst->memory_data_size = (uint32)total_size;
|
||||
|
||||
/* Initialize heap info */
|
||||
memory_inst->heap_data.ptr = p + heap_offset;
|
||||
@ -561,21 +578,19 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
|
||||
}
|
||||
|
||||
if (total_size > 0) {
|
||||
if (sizeof(uintptr_t) == sizeof(uint64)) {
|
||||
memory_inst->mem_bound_check_1byte.u64 = total_size - 1;
|
||||
memory_inst->mem_bound_check_2bytes.u64 = total_size - 2;
|
||||
memory_inst->mem_bound_check_4bytes.u64 = total_size - 4;
|
||||
memory_inst->mem_bound_check_8bytes.u64 = total_size - 8;
|
||||
memory_inst->mem_bound_check_16bytes.u64 = total_size - 16;
|
||||
}
|
||||
else {
|
||||
memory_inst->mem_bound_check_1byte.u32[0] = (uint32)total_size - 1;
|
||||
memory_inst->mem_bound_check_2bytes.u32[0] = (uint32)total_size - 2;
|
||||
memory_inst->mem_bound_check_4bytes.u32[0] = (uint32)total_size - 4;
|
||||
memory_inst->mem_bound_check_8bytes.u32[0] = (uint32)total_size - 8;
|
||||
memory_inst->mem_bound_check_16bytes.u32[0] =
|
||||
(uint32)total_size - 16;
|
||||
}
|
||||
#if UINTPTR_MAX == UINT64_MAX
|
||||
memory_inst->mem_bound_check_1byte.u64 = total_size - 1;
|
||||
memory_inst->mem_bound_check_2bytes.u64 = total_size - 2;
|
||||
memory_inst->mem_bound_check_4bytes.u64 = total_size - 4;
|
||||
memory_inst->mem_bound_check_8bytes.u64 = total_size - 8;
|
||||
memory_inst->mem_bound_check_16bytes.u64 = total_size - 16;
|
||||
#else
|
||||
memory_inst->mem_bound_check_1byte.u32[0] = (uint32)total_size - 1;
|
||||
memory_inst->mem_bound_check_2bytes.u32[0] = (uint32)total_size - 2;
|
||||
memory_inst->mem_bound_check_4bytes.u32[0] = (uint32)total_size - 4;
|
||||
memory_inst->mem_bound_check_8bytes.u32[0] = (uint32)total_size - 8;
|
||||
memory_inst->mem_bound_check_16bytes.u32[0] = (uint32)total_size - 16;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_SHARED_MEMORY != 0
|
||||
@ -2032,26 +2047,29 @@ aot_get_native_addr_range(AOTModuleInstance *module_inst, uint8 *native_ptr,
|
||||
bool
|
||||
aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
|
||||
{
|
||||
AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
|
||||
uint32 num_bytes_per_page, cur_page_count, max_page_count;
|
||||
uint32 total_page_count, total_size_old, heap_size;
|
||||
uint64 total_size;
|
||||
uint8 *memory_data_old, *heap_data_old, *memory_data, *heap_data;
|
||||
AOTMemoryInstance *memory = aot_get_default_memory(module_inst);
|
||||
uint8 *memory_data_old, *memory_data_new, *heap_data_old;
|
||||
uint32 num_bytes_per_page, heap_size, total_size_old;
|
||||
uint32 cur_page_count, max_page_count, total_page_count;
|
||||
uint64 total_size_new;
|
||||
bool ret = true;
|
||||
|
||||
if (!memory_inst)
|
||||
if (!memory)
|
||||
return false;
|
||||
|
||||
num_bytes_per_page = memory_inst->num_bytes_per_page;
|
||||
cur_page_count = memory_inst->cur_page_count;
|
||||
max_page_count = memory_inst->max_page_count;
|
||||
total_page_count = cur_page_count + inc_page_count;
|
||||
total_size_old = memory_inst->memory_data_size;
|
||||
total_size = (uint64)num_bytes_per_page * total_page_count;
|
||||
heap_size = (uint32)((uint8 *)memory_inst->heap_data_end.ptr
|
||||
- (uint8 *)memory_inst->heap_data.ptr);
|
||||
memory_data_old = (uint8 *)memory_inst->memory_data.ptr;
|
||||
heap_data_old = (uint8 *)memory_inst->heap_data.ptr;
|
||||
heap_data_old = (uint8 *)memory->heap_data.ptr;
|
||||
heap_size = (uint32)((uint8 *)memory->heap_data_end.ptr
|
||||
- (uint8 *)memory->heap_data.ptr);
|
||||
|
||||
memory_data_old = (uint8 *)memory->memory_data.ptr;
|
||||
total_size_old =
|
||||
(uint32)((uint8 *)memory->memory_data_end.ptr - memory_data_old);
|
||||
|
||||
num_bytes_per_page = memory->num_bytes_per_page;
|
||||
cur_page_count = memory->cur_page_count;
|
||||
max_page_count = memory->max_page_count;
|
||||
total_page_count = inc_page_count + cur_page_count;
|
||||
total_size_new = num_bytes_per_page * (uint64)total_page_count;
|
||||
|
||||
if (inc_page_count <= 0)
|
||||
/* No need to enlarge memory */
|
||||
@ -2062,94 +2080,103 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (total_size >= UINT32_MAX) {
|
||||
return false;
|
||||
bh_assert(total_size_new <= 4 * (uint64)BH_GB);
|
||||
if (total_size_new > UINT32_MAX) {
|
||||
/* Resize to 1 page with size 4G-1 */
|
||||
num_bytes_per_page = UINT32_MAX;
|
||||
total_page_count = max_page_count = 1;
|
||||
total_size_new = UINT32_MAX;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_SHARED_MEMORY != 0
|
||||
if (memory_inst->is_shared) {
|
||||
/* For shared memory, we have reserved the maximum spaces during
|
||||
instantiate, only change the cur_page_count here */
|
||||
memory_inst->cur_page_count = total_page_count;
|
||||
if (memory->is_shared) {
|
||||
memory->num_bytes_per_page = UINT32_MAX;
|
||||
memory->cur_page_count = total_page_count;
|
||||
memory->max_page_count = max_page_count;
|
||||
memory->memory_data_size = (uint32)total_size_new;
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (heap_size > 0) {
|
||||
if (mem_allocator_is_heap_corrupted(memory_inst->heap_handle.ptr)) {
|
||||
if (mem_allocator_is_heap_corrupted(memory->heap_handle.ptr)) {
|
||||
wasm_runtime_show_app_heap_corrupted_prompt();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!(memory_data =
|
||||
wasm_runtime_realloc(memory_data_old, (uint32)total_size))) {
|
||||
if (!(memory_data = wasm_runtime_malloc((uint32)total_size))) {
|
||||
if (!(memory_data_new =
|
||||
wasm_runtime_realloc(memory_data_old, (uint32)total_size_new))) {
|
||||
if (!(memory_data_new = wasm_runtime_malloc((uint32)total_size_new))) {
|
||||
return false;
|
||||
}
|
||||
if (memory_data_old) {
|
||||
bh_memcpy_s(memory_data, (uint32)total_size, memory_data_old,
|
||||
total_size_old);
|
||||
bh_memcpy_s(memory_data_new, (uint32)total_size_new,
|
||||
memory_data_old, total_size_old);
|
||||
wasm_runtime_free(memory_data_old);
|
||||
}
|
||||
}
|
||||
|
||||
memset(memory_data + total_size_old, 0,
|
||||
(uint32)total_size - total_size_old);
|
||||
|
||||
memory_inst->cur_page_count = total_page_count;
|
||||
memory_inst->memory_data_size = (uint32)total_size;
|
||||
memory_inst->memory_data.ptr = memory_data;
|
||||
memory_inst->memory_data_end.ptr = memory_data + total_size;
|
||||
memset(memory_data_new + total_size_old, 0,
|
||||
(uint32)total_size_new - total_size_old);
|
||||
|
||||
if (heap_size > 0) {
|
||||
if (mem_allocator_migrate(memory_inst->heap_handle.ptr,
|
||||
if (mem_allocator_migrate(memory->heap_handle.ptr,
|
||||
(char *)heap_data_old
|
||||
+ (memory_data - memory_data_old),
|
||||
heap_size)) {
|
||||
+ (memory_data_new - memory_data_old),
|
||||
heap_size)
|
||||
!= 0) {
|
||||
/* Don't return here as memory->memory_data is obsolete and
|
||||
must be updated to be correctly used later. */
|
||||
ret = false;
|
||||
}
|
||||
}
|
||||
|
||||
heap_data = memory_data + (heap_data_old - memory_data_old);
|
||||
memory_inst->heap_data.ptr = heap_data;
|
||||
memory_inst->heap_data_end.ptr = heap_data + heap_size;
|
||||
memory->heap_data.ptr = memory_data_new + (heap_data_old - memory_data_old);
|
||||
memory->heap_data_end.ptr = (uint8 *)memory->heap_data.ptr + heap_size;
|
||||
|
||||
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.ptr = memory_data_new;
|
||||
memory->memory_data_end.ptr = memory_data_new + (uint32)total_size_new;
|
||||
memory->memory_data_size = (uint32)total_size_new;
|
||||
|
||||
#if UINTPTR_MAX == UINT64_MAX
|
||||
memory->mem_bound_check_1byte.u64 = total_size_new - 1;
|
||||
memory->mem_bound_check_2bytes.u64 = total_size_new - 2;
|
||||
memory->mem_bound_check_4bytes.u64 = total_size_new - 4;
|
||||
memory->mem_bound_check_8bytes.u64 = total_size_new - 8;
|
||||
memory->mem_bound_check_16bytes.u64 = total_size_new - 16;
|
||||
#else
|
||||
memory->mem_bound_check_1byte.u32[0] = (uint32)total_size_new - 1;
|
||||
memory->mem_bound_check_2bytes.u32[0] = (uint32)total_size_new - 2;
|
||||
memory->mem_bound_check_4bytes.u32[0] = (uint32)total_size_new - 4;
|
||||
memory->mem_bound_check_8bytes.u32[0] = (uint32)total_size_new - 8;
|
||||
memory->mem_bound_check_16bytes.u32[0] = (uint32)total_size_new - 16;
|
||||
#endif
|
||||
|
||||
if (sizeof(uintptr_t) == sizeof(uint64)) {
|
||||
memory_inst->mem_bound_check_1byte.u64 = total_size - 1;
|
||||
memory_inst->mem_bound_check_2bytes.u64 = total_size - 2;
|
||||
memory_inst->mem_bound_check_4bytes.u64 = total_size - 4;
|
||||
memory_inst->mem_bound_check_8bytes.u64 = total_size - 8;
|
||||
memory_inst->mem_bound_check_16bytes.u64 = total_size - 16;
|
||||
}
|
||||
else {
|
||||
memory_inst->mem_bound_check_1byte.u32[0] = (uint32)total_size - 1;
|
||||
memory_inst->mem_bound_check_2bytes.u32[0] = (uint32)total_size - 2;
|
||||
memory_inst->mem_bound_check_4bytes.u32[0] = (uint32)total_size - 4;
|
||||
memory_inst->mem_bound_check_8bytes.u32[0] = (uint32)total_size - 8;
|
||||
memory_inst->mem_bound_check_16bytes.u32[0] = (uint32)total_size - 16;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
#else /* else of OS_ENABLE_HW_BOUND_CHECK */
|
||||
bool
|
||||
aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
|
||||
{
|
||||
AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
|
||||
uint32 num_bytes_per_page, cur_page_count, max_page_count;
|
||||
uint32 total_page_count;
|
||||
uint64 total_size;
|
||||
AOTMemoryInstance *memory = aot_get_default_memory(module_inst);
|
||||
uint32 num_bytes_per_page, total_size_old;
|
||||
uint32 cur_page_count, max_page_count, total_page_count;
|
||||
uint64 total_size_new;
|
||||
|
||||
if (!memory_inst)
|
||||
if (!memory)
|
||||
return false;
|
||||
|
||||
num_bytes_per_page = memory_inst->num_bytes_per_page;
|
||||
cur_page_count = memory_inst->cur_page_count;
|
||||
max_page_count = memory_inst->max_page_count;
|
||||
total_page_count = cur_page_count + inc_page_count;
|
||||
total_size = (uint64)num_bytes_per_page * total_page_count;
|
||||
num_bytes_per_page = memory->num_bytes_per_page;
|
||||
cur_page_count = memory->cur_page_count;
|
||||
max_page_count = memory->max_page_count;
|
||||
total_size_old = num_bytes_per_page * cur_page_count;
|
||||
total_page_count = inc_page_count + cur_page_count;
|
||||
total_size_new = num_bytes_per_page * (uint64)total_page_count;
|
||||
|
||||
if (inc_page_count <= 0)
|
||||
/* No need to enlarge memory */
|
||||
@ -2160,21 +2187,29 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
|
||||
return false;
|
||||
}
|
||||
|
||||
bh_assert(total_size_new <= 4 * (uint64)BH_GB);
|
||||
if (total_size_new > UINT32_MAX) {
|
||||
/* Resize to 1 page with size 4G-1 */
|
||||
num_bytes_per_page = UINT32_MAX;
|
||||
total_page_count = max_page_count = 1;
|
||||
total_size_new = UINT32_MAX;
|
||||
}
|
||||
|
||||
#ifdef BH_PLATFORM_WINDOWS
|
||||
if (!os_mem_commit(memory_inst->memory_data_end.ptr,
|
||||
num_bytes_per_page * inc_page_count,
|
||||
if (!os_mem_commit(memory->memory_data_end.ptr,
|
||||
(uint32)total_size_new - total_size_old,
|
||||
MMAP_PROT_READ | MMAP_PROT_WRITE)) {
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (os_mprotect(memory_inst->memory_data_end.ptr,
|
||||
num_bytes_per_page * inc_page_count,
|
||||
if (os_mprotect(memory->memory_data_end.ptr,
|
||||
(uint32)total_size_new - total_size_old,
|
||||
MMAP_PROT_READ | MMAP_PROT_WRITE)
|
||||
!= 0) {
|
||||
#ifdef BH_PLATFORM_WINDOWS
|
||||
os_mem_decommit(memory_inst->memory_data_end.ptr,
|
||||
num_bytes_per_page * inc_page_count);
|
||||
os_mem_decommit(memory->memory_data_end.ptr,
|
||||
(uint32)total_size_new - total_size_old);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
@ -2182,25 +2217,19 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
|
||||
/* The increased pages are filled with zero by the OS when os_mmap,
|
||||
no need to memset it again here */
|
||||
|
||||
memory_inst->cur_page_count = total_page_count;
|
||||
memory_inst->memory_data_size = (uint32)total_size;
|
||||
memory_inst->memory_data_end.ptr =
|
||||
(uint8 *)memory_inst->memory_data.ptr + (uint32)total_size;
|
||||
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;
|
||||
memory->memory_data_end.ptr =
|
||||
(uint8 *)memory->memory_data.ptr + (uint32)total_size_new;
|
||||
|
||||
memory->mem_bound_check_1byte.u64 = total_size_new - 1;
|
||||
memory->mem_bound_check_2bytes.u64 = total_size_new - 2;
|
||||
memory->mem_bound_check_4bytes.u64 = total_size_new - 4;
|
||||
memory->mem_bound_check_8bytes.u64 = total_size_new - 8;
|
||||
memory->mem_bound_check_16bytes.u64 = total_size_new - 16;
|
||||
|
||||
if (sizeof(uintptr_t) == sizeof(uint64)) {
|
||||
memory_inst->mem_bound_check_1byte.u64 = total_size - 1;
|
||||
memory_inst->mem_bound_check_2bytes.u64 = total_size - 2;
|
||||
memory_inst->mem_bound_check_4bytes.u64 = total_size - 4;
|
||||
memory_inst->mem_bound_check_8bytes.u64 = total_size - 8;
|
||||
memory_inst->mem_bound_check_16bytes.u64 = total_size - 16;
|
||||
}
|
||||
else {
|
||||
memory_inst->mem_bound_check_1byte.u32[0] = (uint32)total_size - 1;
|
||||
memory_inst->mem_bound_check_2bytes.u32[0] = (uint32)total_size - 2;
|
||||
memory_inst->mem_bound_check_4bytes.u32[0] = (uint32)total_size - 4;
|
||||
memory_inst->mem_bound_check_8bytes.u32[0] = (uint32)total_size - 8;
|
||||
memory_inst->mem_bound_check_16bytes.u32[0] = (uint32)total_size - 16;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif /* end of OS_ENABLE_HW_BOUND_CHECK */
|
||||
|
||||
Reference in New Issue
Block a user