Skip to content

Commit

Permalink
Revert "Always allocate linear memory using mmap (bytecodealliance#3052
Browse files Browse the repository at this point in the history
…)"

This reverts commit a27ddec.
  • Loading branch information
no1wudi committed Feb 4, 2024
1 parent 529fa9d commit 6981ac7
Show file tree
Hide file tree
Showing 25 changed files with 421 additions and 380 deletions.
1 change: 1 addition & 0 deletions .github/workflows/compilation_on_android_ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ jobs:
"-DWAMR_BUILD_SIMD=1",
"-DWAMR_BUILD_TAIL_CALL=1",
"-DWAMR_DISABLE_HW_BOUND_CHECK=1",
"-DWAMR_ENABLE_SHARED_MEMORY_MMAP=1",
]
os: [ubuntu-22.04]
platform: [android, linux]
Expand Down
6 changes: 6 additions & 0 deletions build-scripts/config_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ if (WAMR_BUILD_SHARED_MEMORY EQUAL 1)
else ()
add_definitions (-DWASM_ENABLE_SHARED_MEMORY=0)
endif ()
if (WAMR_ENABLE_SHARED_MEMORY_MMAP EQUAL 1)
add_definitions (-DWASM_ENABLE_SHARED_MEMORY_MMAP=1)
message (" Shared memory allocated using mmap enabled")
else ()
add_definitions (-DWASM_ENABLE_SHARED_MEMORY_MMAP=0)
endif ()
if (WAMR_BUILD_THREAD_MGR EQUAL 1)
message (" Thread manager enabled")
endif ()
Expand Down
9 changes: 5 additions & 4 deletions core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,6 @@
#define WASM_ENABLE_APP_FRAMEWORK 0
#endif

#ifndef WASM_HAVE_MREMAP
#define WASM_HAVE_MREMAP 0
#endif

/* Bulk memory operation */
#ifndef WASM_ENABLE_BULK_MEMORY
#define WASM_ENABLE_BULK_MEMORY 0
Expand Down Expand Up @@ -525,4 +521,9 @@
#define WASM_ENABLE_QUICK_AOT_ENTRY 1
#endif

/* Disable mmap based shared memory by default */
#ifndef WASM_ENABLE_SHARED_MEMORY_MMAP
#define WASM_ENABLE_SHARED_MEMORY_MMAP 0
#endif

#endif /* end of _CONFIG_H_ */
90 changes: 79 additions & 11 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
static void
memories_deinstantiate(AOTModuleInstance *module_inst)
{
#ifdef WASM_LINEAR_MEMORY_MMAP
uint64 map_size;
#endif
uint32 i;
AOTMemoryInstance *memory_inst;

Expand All @@ -361,7 +364,23 @@ memories_deinstantiate(AOTModuleInstance *module_inst)
}

if (memory_inst->memory_data) {
wasm_deallocate_linear_memory(memory_inst);
#ifndef OS_ENABLE_HW_BOUND_CHECK
#ifdef WASM_LINEAR_MEMORY_MMAP
if (shared_memory_is_shared(memory_inst)) {
map_size = (uint64)memory_inst->num_bytes_per_page
* memory_inst->max_page_count;
wasm_munmap_linear_memory(memory_inst->memory_data,
map_size, map_size);
}
else
#endif
wasm_runtime_free(memory_inst->memory_data);
#else
map_size = (uint64)memory_inst->num_bytes_per_page
* memory_inst->cur_page_count;
wasm_munmap_linear_memory(memory_inst->memory_data, map_size,
8 * (uint64)BH_GB);
#endif
}
}
}
Expand All @@ -383,10 +402,14 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
uint32 heap_offset = num_bytes_per_page * init_page_count;
uint64 memory_data_size, max_memory_data_size;
uint8 *p = NULL, *global_addr;
#ifdef WASM_LINEAR_MEMORY_MMAP
uint8 *mapped_mem = NULL;
uint64 map_size;
#endif

bool is_shared_memory = false;
#if WASM_ENABLE_SHARED_MEMORY != 0
is_shared_memory = memory->memory_flags & 0x02 ? true : false;
bool is_shared_memory = memory->memory_flags & 0x02 ? true : false;

/* Shared memory */
if (is_shared_memory && parent != NULL) {
AOTMemoryInstance *shared_memory_instance;
Expand Down Expand Up @@ -496,18 +519,55 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
module->aux_stack_size);
LOG_VERBOSE(" heap offset: %u, heap size: %d\n", heap_offset, heap_size);

memory_data_size = (uint64)num_bytes_per_page * init_page_count;
max_memory_data_size = (uint64)num_bytes_per_page * max_page_count;
bh_assert(memory_data_size <= UINT32_MAX);
bh_assert(max_memory_data_size <= 4 * (uint64)BH_GB);
(void)max_memory_data_size;

if (wasm_allocate_linear_memory(&p, is_shared_memory, num_bytes_per_page,
init_page_count, max_page_count,
&memory_data_size)
!= BHT_OK) {
set_error_buf(error_buf, error_buf_size,
"allocate linear memory failed");
#ifndef OS_ENABLE_HW_BOUND_CHECK
#if WASM_ENABLE_SHARED_MEMORY != 0
if (is_shared_memory) {
#if WASM_ENABLE_SHARED_MEMORY_MMAP != 0
map_size = max_memory_data_size;
if (max_memory_data_size > 0
&& !(p = mapped_mem =
wasm_mmap_linear_memory(map_size, &max_memory_data_size,
error_buf, error_buf_size))) {
return NULL;
}
#else
/* Allocate maximum memory size when memory is shared */
if (max_memory_data_size > 0
&& !(p = runtime_malloc(max_memory_data_size, error_buf,
error_buf_size))) {
return NULL;
}
#endif
}
else
#endif /* end of WASM_ENABLE_SHARED_MEMORY != 0 */
{
/* Allocate initial memory size when memory is not shared */
if (memory_data_size > 0
&& !(p = runtime_malloc(memory_data_size, error_buf,
error_buf_size))) {
return NULL;
}
}
#else /* else of OS_ENABLE_HW_BOUND_CHECK */
/* Totally 8G is mapped, the opcode load/store address range is 0 to 8G:
* ea = i + memarg.offset
* both i and memarg.offset are u32 in range 0 to 4G
* so the range of ea is 0 to 8G
*/
map_size = 8 * (uint64)BH_GB;
if (!(p = mapped_mem = wasm_mmap_linear_memory(
map_size, &memory_data_size, error_buf, error_buf_size))) {
set_error_buf(error_buf, error_buf_size, "mmap memory failed");
return NULL;
}
#endif /* end of OS_ENABLE_HW_BOUND_CHECK */

memory_inst->module_type = Wasm_Module_AoT;
memory_inst->num_bytes_per_page = num_bytes_per_page;
Expand Down Expand Up @@ -557,8 +617,16 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
if (heap_size > 0)
wasm_runtime_free(memory_inst->heap_handle);
fail1:
wasm_deallocate_linear_memory(memory_inst);

#ifdef WASM_LINEAR_MEMORY_MMAP
if (mapped_mem)
wasm_munmap_linear_memory(mapped_mem, memory_data_size, map_size);
else
#endif
{
if (memory_inst->memory_data)
wasm_runtime_free(memory_inst->memory_data);
}
memory_inst->memory_data = NULL;
return NULL;
}

Expand Down
Loading

0 comments on commit 6981ac7

Please sign in to comment.