| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901 |
- /*
- * Copyright (C) 2019 Intel Corporation. All rights reserved.
- * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- */
- #include "wasm_runtime_common.h"
- #include "../interpreter/wasm_runtime.h"
- #include "../aot/aot_runtime.h"
- #include "bh_platform.h"
- #include "mem_alloc.h"
- #include "wasm_memory.h"
- #if WASM_ENABLE_SHARED_MEMORY != 0
- #include "../common/wasm_shared_memory.h"
- #endif
- typedef enum Memory_Mode {
- MEMORY_MODE_UNKNOWN = 0,
- MEMORY_MODE_POOL,
- MEMORY_MODE_ALLOCATOR,
- MEMORY_MODE_SYSTEM_ALLOCATOR
- } Memory_Mode;
- static Memory_Mode memory_mode = MEMORY_MODE_UNKNOWN;
- static mem_allocator_t pool_allocator = NULL;
- static enlarge_memory_error_callback_t enlarge_memory_error_cb;
- static void *enlarge_memory_error_user_data;
- #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
- static void *allocator_user_data = NULL;
- static void *(*malloc_func)(void *user_data, unsigned int size) = NULL;
- static void *(*realloc_func)(void *user_data, void *ptr,
- unsigned int size) = NULL;
- static void (*free_func)(void *user_data, void *ptr) = NULL;
- #else
- static void *(*malloc_func)(unsigned int size) = NULL;
- static void *(*realloc_func)(void *ptr, unsigned int size) = NULL;
- static void (*free_func)(void *ptr) = NULL;
- #endif
- static unsigned int global_pool_size;
- static bool
- wasm_memory_init_with_pool(void *mem, unsigned int bytes)
- {
- mem_allocator_t _allocator = mem_allocator_create(mem, bytes);
- if (_allocator) {
- memory_mode = MEMORY_MODE_POOL;
- pool_allocator = _allocator;
- global_pool_size = bytes;
- return true;
- }
- LOG_ERROR("Init memory with pool (%p, %u) failed.\n", mem, bytes);
- return false;
- }
- #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
- static bool
- wasm_memory_init_with_allocator(void *_user_data, void *_malloc_func,
- void *_realloc_func, void *_free_func)
- {
- if (_malloc_func && _free_func && _malloc_func != _free_func) {
- memory_mode = MEMORY_MODE_ALLOCATOR;
- allocator_user_data = _user_data;
- malloc_func = _malloc_func;
- realloc_func = _realloc_func;
- free_func = _free_func;
- return true;
- }
- LOG_ERROR("Init memory with allocator (%p, %p, %p, %p) failed.\n",
- _user_data, _malloc_func, _realloc_func, _free_func);
- return false;
- }
- #else
- static bool
- wasm_memory_init_with_allocator(void *_malloc_func, void *_realloc_func,
- void *_free_func)
- {
- if (_malloc_func && _free_func && _malloc_func != _free_func) {
- memory_mode = MEMORY_MODE_ALLOCATOR;
- malloc_func = _malloc_func;
- realloc_func = _realloc_func;
- free_func = _free_func;
- return true;
- }
- LOG_ERROR("Init memory with allocator (%p, %p, %p) failed.\n", _malloc_func,
- _realloc_func, _free_func);
- return false;
- }
- #endif
- static inline bool
- is_bounds_checks_enabled(WASMModuleInstanceCommon *module_inst)
- {
- #if WASM_CONFIGUABLE_BOUNDS_CHECKS != 0
- return wasm_runtime_is_bounds_checks_enabled(module_inst);
- #else
- return true;
- #endif
- }
- bool
- wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
- const MemAllocOption *alloc_option)
- {
- if (mem_alloc_type == Alloc_With_Pool) {
- return wasm_memory_init_with_pool(alloc_option->pool.heap_buf,
- alloc_option->pool.heap_size);
- }
- else if (mem_alloc_type == Alloc_With_Allocator) {
- #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
- return wasm_memory_init_with_allocator(
- alloc_option->allocator.user_data,
- alloc_option->allocator.malloc_func,
- alloc_option->allocator.realloc_func,
- alloc_option->allocator.free_func);
- #else
- return wasm_memory_init_with_allocator(
- alloc_option->allocator.malloc_func,
- alloc_option->allocator.realloc_func,
- alloc_option->allocator.free_func);
- #endif
- }
- else if (mem_alloc_type == Alloc_With_System_Allocator) {
- memory_mode = MEMORY_MODE_SYSTEM_ALLOCATOR;
- return true;
- }
- else {
- return false;
- }
- }
- void
- wasm_runtime_memory_destroy()
- {
- if (memory_mode == MEMORY_MODE_POOL) {
- #if BH_ENABLE_GC_VERIFY == 0
- (void)mem_allocator_destroy(pool_allocator);
- #else
- int ret = mem_allocator_destroy(pool_allocator);
- if (ret != 0) {
- /* Memory leak detected */
- exit(-1);
- }
- #endif
- }
- memory_mode = MEMORY_MODE_UNKNOWN;
- }
- unsigned
- wasm_runtime_memory_pool_size()
- {
- if (memory_mode == MEMORY_MODE_POOL)
- return global_pool_size;
- else
- return UINT32_MAX;
- }
- static inline void *
- wasm_runtime_malloc_internal(unsigned int size)
- {
- if (memory_mode == MEMORY_MODE_UNKNOWN) {
- LOG_WARNING(
- "wasm_runtime_malloc failed: memory hasn't been initialize.\n");
- return NULL;
- }
- else if (memory_mode == MEMORY_MODE_POOL) {
- return mem_allocator_malloc(pool_allocator, size);
- }
- else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
- #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
- return malloc_func(allocator_user_data, size);
- #else
- return malloc_func(size);
- #endif
- }
- else {
- return os_malloc(size);
- }
- }
- static inline void *
- wasm_runtime_realloc_internal(void *ptr, unsigned int size)
- {
- if (memory_mode == MEMORY_MODE_UNKNOWN) {
- LOG_WARNING(
- "wasm_runtime_realloc failed: memory hasn't been initialize.\n");
- return NULL;
- }
- else if (memory_mode == MEMORY_MODE_POOL) {
- return mem_allocator_realloc(pool_allocator, ptr, size);
- }
- else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
- if (realloc_func)
- #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
- return realloc_func(allocator_user_data, ptr, size);
- #else
- return realloc_func(ptr, size);
- #endif
- else
- return NULL;
- }
- else {
- return os_realloc(ptr, size);
- }
- }
- static inline void
- wasm_runtime_free_internal(void *ptr)
- {
- if (!ptr) {
- LOG_WARNING("warning: wasm_runtime_free with NULL pointer\n");
- #if BH_ENABLE_GC_VERIFY != 0
- exit(-1);
- #endif
- return;
- }
- if (memory_mode == MEMORY_MODE_UNKNOWN) {
- LOG_WARNING("warning: wasm_runtime_free failed: "
- "memory hasn't been initialize.\n");
- }
- else if (memory_mode == MEMORY_MODE_POOL) {
- mem_allocator_free(pool_allocator, ptr);
- }
- else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
- #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
- free_func(allocator_user_data, ptr);
- #else
- free_func(ptr);
- #endif
- }
- else {
- os_free(ptr);
- }
- }
- void *
- wasm_runtime_malloc(unsigned int size)
- {
- if (size == 0) {
- LOG_WARNING("warning: wasm_runtime_malloc with size zero\n");
- /* At lease alloc 1 byte to avoid malloc failed */
- size = 1;
- #if BH_ENABLE_GC_VERIFY != 0
- exit(-1);
- #endif
- }
- return wasm_runtime_malloc_internal(size);
- }
- void *
- wasm_runtime_realloc(void *ptr, unsigned int size)
- {
- return wasm_runtime_realloc_internal(ptr, size);
- }
- void
- wasm_runtime_free(void *ptr)
- {
- wasm_runtime_free_internal(ptr);
- }
- bool
- wasm_runtime_get_mem_alloc_info(mem_alloc_info_t *mem_alloc_info)
- {
- if (memory_mode == MEMORY_MODE_POOL) {
- return mem_allocator_get_alloc_info(pool_allocator, mem_alloc_info);
- }
- return false;
- }
- bool
- wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst_comm,
- uint32 app_offset, uint32 size)
- {
- WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
- WASMMemoryInstance *memory_inst;
- bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
- || module_inst_comm->module_type == Wasm_Module_AoT);
- if (!is_bounds_checks_enabled(module_inst_comm)) {
- return true;
- }
- memory_inst = wasm_get_default_memory(module_inst);
- if (!memory_inst) {
- goto fail;
- }
- /* integer overflow check */
- if (app_offset > UINT32_MAX - size) {
- goto fail;
- }
- SHARED_MEMORY_LOCK(memory_inst);
- if (app_offset + size <= memory_inst->memory_data_size) {
- SHARED_MEMORY_UNLOCK(memory_inst);
- return true;
- }
- SHARED_MEMORY_UNLOCK(memory_inst);
- fail:
- wasm_set_exception(module_inst, "out of bounds memory access");
- return false;
- }
- bool
- wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst_comm,
- uint32 app_str_offset)
- {
- WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
- uint32 app_end_offset;
- char *str, *str_end;
- bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
- || module_inst_comm->module_type == Wasm_Module_AoT);
- if (!is_bounds_checks_enabled(module_inst_comm)) {
- return true;
- }
- if (!wasm_runtime_get_app_addr_range(module_inst_comm, app_str_offset, NULL,
- &app_end_offset))
- goto fail;
- str = wasm_runtime_addr_app_to_native(module_inst_comm, app_str_offset);
- str_end = str + (app_end_offset - app_str_offset);
- while (str < str_end && *str != '\0')
- str++;
- if (str == str_end)
- goto fail;
- return true;
- fail:
- wasm_set_exception(module_inst, "out of bounds memory access");
- return false;
- }
- bool
- wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst_comm,
- void *native_ptr, uint32 size)
- {
- WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
- WASMMemoryInstance *memory_inst;
- uint8 *addr = (uint8 *)native_ptr;
- bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
- || module_inst_comm->module_type == Wasm_Module_AoT);
- if (!is_bounds_checks_enabled(module_inst_comm)) {
- return true;
- }
- memory_inst = wasm_get_default_memory(module_inst);
- if (!memory_inst) {
- goto fail;
- }
- /* integer overflow check */
- if ((uintptr_t)addr > UINTPTR_MAX - size) {
- goto fail;
- }
- SHARED_MEMORY_LOCK(memory_inst);
- if (memory_inst->memory_data <= addr
- && addr + size <= memory_inst->memory_data_end) {
- SHARED_MEMORY_UNLOCK(memory_inst);
- return true;
- }
- SHARED_MEMORY_UNLOCK(memory_inst);
- fail:
- wasm_set_exception(module_inst, "out of bounds memory access");
- return false;
- }
- void *
- wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst_comm,
- uint32 app_offset)
- {
- WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
- WASMMemoryInstance *memory_inst;
- uint8 *addr;
- bool bounds_checks;
- bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
- || module_inst_comm->module_type == Wasm_Module_AoT);
- bounds_checks = is_bounds_checks_enabled(module_inst_comm);
- memory_inst = wasm_get_default_memory(module_inst);
- if (!memory_inst) {
- return NULL;
- }
- SHARED_MEMORY_LOCK(memory_inst);
- addr = memory_inst->memory_data + app_offset;
- if (bounds_checks) {
- if (memory_inst->memory_data <= addr
- && addr < memory_inst->memory_data_end) {
- SHARED_MEMORY_UNLOCK(memory_inst);
- return addr;
- }
- }
- /* If bounds checks is disabled, return the address directly */
- else if (app_offset != 0) {
- SHARED_MEMORY_UNLOCK(memory_inst);
- return addr;
- }
- SHARED_MEMORY_UNLOCK(memory_inst);
- return NULL;
- }
- uint32
- wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst_comm,
- void *native_ptr)
- {
- WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
- WASMMemoryInstance *memory_inst;
- uint8 *addr = (uint8 *)native_ptr;
- bool bounds_checks;
- uint32 ret;
- bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
- || module_inst_comm->module_type == Wasm_Module_AoT);
- bounds_checks = is_bounds_checks_enabled(module_inst_comm);
- memory_inst = wasm_get_default_memory(module_inst);
- if (!memory_inst) {
- return 0;
- }
- SHARED_MEMORY_LOCK(memory_inst);
- if (bounds_checks) {
- if (memory_inst->memory_data <= addr
- && addr < memory_inst->memory_data_end) {
- ret = (uint32)(addr - memory_inst->memory_data);
- SHARED_MEMORY_UNLOCK(memory_inst);
- return ret;
- }
- }
- /* If bounds checks is disabled, return the offset directly */
- else if (addr != NULL) {
- ret = (uint32)(addr - memory_inst->memory_data);
- SHARED_MEMORY_UNLOCK(memory_inst);
- return ret;
- }
- SHARED_MEMORY_UNLOCK(memory_inst);
- return 0;
- }
- bool
- wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst_comm,
- uint32 app_offset, uint32 *p_app_start_offset,
- uint32 *p_app_end_offset)
- {
- WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
- WASMMemoryInstance *memory_inst;
- uint32 memory_data_size;
- bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
- || module_inst_comm->module_type == Wasm_Module_AoT);
- memory_inst = wasm_get_default_memory(module_inst);
- if (!memory_inst) {
- return false;
- }
- SHARED_MEMORY_LOCK(memory_inst);
- memory_data_size = memory_inst->memory_data_size;
- if (app_offset < memory_data_size) {
- if (p_app_start_offset)
- *p_app_start_offset = 0;
- if (p_app_end_offset)
- *p_app_end_offset = memory_data_size;
- SHARED_MEMORY_UNLOCK(memory_inst);
- return true;
- }
- SHARED_MEMORY_UNLOCK(memory_inst);
- return false;
- }
- bool
- wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst_comm,
- uint8 *native_ptr,
- uint8 **p_native_start_addr,
- uint8 **p_native_end_addr)
- {
- WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
- WASMMemoryInstance *memory_inst;
- uint8 *addr = (uint8 *)native_ptr;
- bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
- || module_inst_comm->module_type == Wasm_Module_AoT);
- memory_inst = wasm_get_default_memory(module_inst);
- if (!memory_inst) {
- return false;
- }
- SHARED_MEMORY_LOCK(memory_inst);
- if (memory_inst->memory_data <= addr
- && addr < memory_inst->memory_data_end) {
- if (p_native_start_addr)
- *p_native_start_addr = memory_inst->memory_data;
- if (p_native_end_addr)
- *p_native_end_addr = memory_inst->memory_data_end;
- SHARED_MEMORY_UNLOCK(memory_inst);
- return true;
- }
- SHARED_MEMORY_UNLOCK(memory_inst);
- return false;
- }
- bool
- wasm_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
- uint32 app_buf_addr, uint32 app_buf_size,
- void **p_native_addr)
- {
- WASMMemoryInstance *memory_inst = wasm_get_default_memory(module_inst);
- uint8 *native_addr;
- bool bounds_checks;
- if (!memory_inst) {
- wasm_set_exception(module_inst, "out of bounds memory access");
- return false;
- }
- SHARED_MEMORY_LOCK(memory_inst);
- native_addr = memory_inst->memory_data + app_buf_addr;
- bounds_checks = is_bounds_checks_enabled((wasm_module_inst_t)module_inst);
- if (!bounds_checks) {
- if (app_buf_addr == 0) {
- native_addr = NULL;
- }
- goto success;
- }
- /* No need to check the app_offset and buf_size if memory access
- boundary check with hardware trap is enabled */
- #ifndef OS_ENABLE_HW_BOUND_CHECK
- if (app_buf_addr >= memory_inst->memory_data_size) {
- goto fail;
- }
- if (!is_str) {
- if (app_buf_size > memory_inst->memory_data_size - app_buf_addr) {
- goto fail;
- }
- }
- else {
- const char *str, *str_end;
- /* The whole string must be in the linear memory */
- str = (const char *)native_addr;
- str_end = (const char *)memory_inst->memory_data_end;
- while (str < str_end && *str != '\0')
- str++;
- if (str == str_end)
- goto fail;
- }
- #endif
- SHARED_MEMORY_UNLOCK(memory_inst);
- success:
- *p_native_addr = (void *)native_addr;
- return true;
- #ifndef OS_ENABLE_HW_BOUND_CHECK
- fail:
- SHARED_MEMORY_UNLOCK(memory_inst);
- wasm_set_exception(module_inst, "out of bounds memory access");
- return false;
- #endif
- }
- WASMMemoryInstance *
- wasm_get_default_memory(WASMModuleInstance *module_inst)
- {
- if (module_inst->memories)
- return module_inst->memories[0];
- else
- return NULL;
- }
- void
- wasm_runtime_set_mem_bound_check_bytes(WASMMemoryInstance *memory,
- uint64 memory_data_size)
- {
- #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 || WASM_ENABLE_AOT != 0
- #if UINTPTR_MAX == UINT64_MAX
- memory->mem_bound_check_1byte.u64 = memory_data_size - 1;
- memory->mem_bound_check_2bytes.u64 = memory_data_size - 2;
- memory->mem_bound_check_4bytes.u64 = memory_data_size - 4;
- memory->mem_bound_check_8bytes.u64 = memory_data_size - 8;
- memory->mem_bound_check_16bytes.u64 = memory_data_size - 16;
- #else
- memory->mem_bound_check_1byte.u32[0] = (uint32)memory_data_size - 1;
- memory->mem_bound_check_2bytes.u32[0] = (uint32)memory_data_size - 2;
- memory->mem_bound_check_4bytes.u32[0] = (uint32)memory_data_size - 4;
- memory->mem_bound_check_8bytes.u32[0] = (uint32)memory_data_size - 8;
- memory->mem_bound_check_16bytes.u32[0] = (uint32)memory_data_size - 16;
- #endif
- #endif
- }
- #ifndef OS_ENABLE_HW_BOUND_CHECK
- bool
- wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
- {
- WASMMemoryInstance *memory = wasm_get_default_memory(module);
- uint8 *memory_data_old, *memory_data_new, *heap_data_old;
- uint32 num_bytes_per_page, heap_size, total_size_old = 0;
- uint32 cur_page_count, max_page_count, total_page_count;
- uint64 total_size_new;
- bool ret = true;
- enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR;
- if (!memory) {
- ret = false;
- goto return_func;
- }
- heap_data_old = memory->heap_data;
- heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
- memory_data_old = memory->memory_data;
- total_size_old = memory->memory_data_size;
- 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 */
- return true;
- if (total_page_count < cur_page_count) { /* integer overflow */
- ret = false;
- goto return_func;
- }
- if (total_page_count > max_page_count) {
- failure_reason = MAX_SIZE_REACHED;
- ret = false;
- goto return_func;
- }
- 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 (shared_memory_is_shared(memory)) {
- 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 = memory->memory_data + (uint32)total_size_new;
- wasm_runtime_set_mem_bound_check_bytes(memory, total_size_new);
- return true;
- }
- #endif
- if (heap_size > 0) {
- if (mem_allocator_is_heap_corrupted(memory->heap_handle)) {
- wasm_runtime_show_app_heap_corrupted_prompt();
- ret = false;
- goto return_func;
- }
- }
- 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))) {
- ret = false;
- goto return_func;
- }
- if (memory_data_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_new + total_size_old, 0,
- (uint32)total_size_new - total_size_old);
- if (heap_size > 0) {
- if (mem_allocator_migrate(memory->heap_handle,
- (char *)heap_data_old
- + (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;
- }
- }
- memory->heap_data = memory_data_new + (heap_data_old - memory_data_old);
- memory->heap_data_end = memory->heap_data + 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_size = (uint32)total_size_new;
- memory->memory_data = memory_data_new;
- memory->memory_data_end = memory_data_new + (uint32)total_size_new;
- wasm_runtime_set_mem_bound_check_bytes(memory, total_size_new);
- #if defined(os_writegsbase)
- /* write base addr of linear memory to GS segment register */
- os_writegsbase(memory_data_new);
- #endif
- return_func:
- if (!ret && enlarge_memory_error_cb) {
- WASMExecEnv *exec_env = NULL;
- #if WASM_ENABLE_INTERP != 0
- if (module->module_type == Wasm_Module_Bytecode)
- exec_env =
- ((WASMModuleInstanceExtra *)module->e)->common.cur_exec_env;
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module->module_type == Wasm_Module_AoT)
- exec_env =
- ((AOTModuleInstanceExtra *)module->e)->common.cur_exec_env;
- #endif
- enlarge_memory_error_cb(inc_page_count, total_size_old, 0,
- failure_reason,
- (WASMModuleInstanceCommon *)module, exec_env,
- enlarge_memory_error_user_data);
- }
- return ret;
- }
- #else
- bool
- wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
- {
- WASMMemoryInstance *memory = wasm_get_default_memory(module);
- uint32 num_bytes_per_page, total_size_old = 0;
- uint32 cur_page_count, max_page_count, total_page_count;
- uint64 total_size_new;
- bool ret = true;
- enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR;
- if (!memory) {
- ret = false;
- goto return_func;
- }
- 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 */
- return true;
- if (total_page_count < cur_page_count) { /* integer overflow */
- ret = false;
- goto return_func;
- }
- if (total_page_count > max_page_count) {
- failure_reason = MAX_SIZE_REACHED;
- ret = false;
- goto return_func;
- }
- 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->memory_data_end,
- (uint32)total_size_new - total_size_old,
- MMAP_PROT_READ | MMAP_PROT_WRITE)) {
- ret = false;
- goto return_func;
- }
- #endif
- if (os_mprotect(memory->memory_data_end,
- (uint32)total_size_new - total_size_old,
- MMAP_PROT_READ | MMAP_PROT_WRITE)
- != 0) {
- #ifdef BH_PLATFORM_WINDOWS
- os_mem_decommit(memory->memory_data_end,
- (uint32)total_size_new - total_size_old);
- #endif
- ret = false;
- goto return_func;
- }
- /* The increased pages are filled with zero by the OS when os_mmap,
- no need to memset it again here */
- 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 = memory->memory_data + (uint32)total_size_new;
- wasm_runtime_set_mem_bound_check_bytes(memory, total_size_new);
- return_func:
- if (!ret && enlarge_memory_error_cb) {
- WASMExecEnv *exec_env = NULL;
- #if WASM_ENABLE_INTERP != 0
- if (module->module_type == Wasm_Module_Bytecode)
- exec_env =
- ((WASMModuleInstanceExtra *)module->e)->common.cur_exec_env;
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module->module_type == Wasm_Module_AoT)
- exec_env =
- ((AOTModuleInstanceExtra *)module->e)->common.cur_exec_env;
- #endif
- enlarge_memory_error_cb(inc_page_count, total_size_old, 0,
- failure_reason,
- (WASMModuleInstanceCommon *)module, exec_env,
- enlarge_memory_error_user_data);
- }
- return ret;
- }
- #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
- void
- wasm_runtime_set_enlarge_mem_error_callback(
- const enlarge_memory_error_callback_t callback, void *user_data)
- {
- enlarge_memory_error_cb = callback;
- enlarge_memory_error_user_data = user_data;
- }
- bool
- wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
- {
- bool ret = false;
- #if WASM_ENABLE_SHARED_MEMORY != 0
- if (module->memory_count > 0)
- shared_memory_lock(module->memories[0]);
- #endif
- ret = wasm_enlarge_memory_internal(module, inc_page_count);
- #if WASM_ENABLE_SHARED_MEMORY != 0
- if (module->memory_count > 0)
- shared_memory_unlock(module->memories[0]);
- #endif
- return ret;
- }
|