Skip to content

Commit

Permalink
Extend os_mmap to support map file from fd (bytecodealliance#2763)
Browse files Browse the repository at this point in the history
Add an extra argument `os_file_handle file` for `os_mmap` to support
mapping file from a file fd, and remove `os_get_invalid_handle` from
`posix_file.c` and `win_file.c`, instead, add it in the `platform_internal.h`
files to remove the dependency on libc-wasi.

Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
  • Loading branch information
no1wudi authored Nov 16, 2023
1 parent f713ba6 commit 84513bd
Show file tree
Hide file tree
Showing 35 changed files with 127 additions and 45 deletions.
2 changes: 1 addition & 1 deletion core/app-mgr/app-manager/module_wasm_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,7 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch, int request_total_size,
if (total_size >= UINT32_MAX
|| !(section->section_body =
os_mmap(NULL, (uint32)total_size, map_prot,
map_flags))) {
map_flags, os_get_invalid_handle()))) {
app_manager_printf(
"Allocate executable memory failed!\n");
SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
Expand Down
16 changes: 10 additions & 6 deletions core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1574,8 +1574,9 @@ load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end,

/* Allocate memory for data */
if (data_sections[i].size > 0
&& !(data_sections[i].data = os_mmap(NULL, data_sections[i].size,
map_prot, map_flags))) {
&& !(data_sections[i].data =
os_mmap(NULL, data_sections[i].size, map_prot, map_flags,
os_get_invalid_handle()))) {
set_error_buf(error_buf, error_buf_size, "allocate memory failed");
return false;
}
Expand Down Expand Up @@ -2470,7 +2471,8 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,

if (size > UINT32_MAX
|| !(module->extra_plt_data =
os_mmap(NULL, (uint32)size, map_prot, map_flags))) {
os_mmap(NULL, (uint32)size, map_prot, map_flags,
os_get_invalid_handle()))) {
set_error_buf(error_buf, error_buf_size, "mmap memory failed");
goto fail;
}
Expand Down Expand Up @@ -2593,7 +2595,8 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,
size = (uint64)sizeof(void *) * got_item_count;
if (size > UINT32_MAX
|| !(module->got_func_ptrs =
os_mmap(NULL, (uint32)size, map_prot, map_flags))) {
os_mmap(NULL, (uint32)size, map_prot, map_flags,
os_get_invalid_handle()))) {
set_error_buf(error_buf, error_buf_size, "mmap memory failed");
goto fail;
}
Expand Down Expand Up @@ -3106,8 +3109,9 @@ create_sections(AOTModule *module, const uint8 *buf, uint32 size,
(uint64)section_size + aot_get_plt_table_size();
total_size = (total_size + 3) & ~((uint64)3);
if (total_size >= UINT32_MAX
|| !(aot_text = os_mmap(NULL, (uint32)total_size,
map_prot, map_flags))) {
|| !(aot_text =
os_mmap(NULL, (uint32)total_size, map_prot,
map_flags, os_get_invalid_handle()))) {
wasm_runtime_free(section);
set_error_buf(error_buf, error_buf_size,
"mmap memory failed");
Expand Down
4 changes: 2 additions & 2 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,8 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
* both i and memarg.offset are u32 in range 0 to 4G
* so the range of ea is 0 to 8G
*/
if (!(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, os_get_invalid_handle()))) {
set_error_buf(error_buf, error_buf_size, "mmap memory failed");
return NULL;
}
Expand Down
3 changes: 2 additions & 1 deletion core/iwasm/common/wasm_exec_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ wasm_exec_env_create_internal(struct WASMModuleInstanceCommon *module_inst,

#ifdef OS_ENABLE_HW_BOUND_CHECK
if (!(exec_env->exce_check_guard_page =
os_mmap(NULL, os_getpagesize(), MMAP_PROT_NONE, MMAP_MAP_NONE)))
os_mmap(NULL, os_getpagesize(), MMAP_PROT_NONE, MMAP_MAP_NONE,
os_get_invalid_handle())))
goto fail5;
#endif

Expand Down
4 changes: 2 additions & 2 deletions core/iwasm/fast-jit/jit_codecache.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ jit_code_cache_init(uint32 code_cache_size)
int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
int map_flags = MMAP_MAP_NONE;

if (!(code_cache_pool =
os_mmap(NULL, code_cache_size, map_prot, map_flags))) {
if (!(code_cache_pool = os_mmap(NULL, code_cache_size, map_prot, map_flags,
os_get_invalid_handle()))) {
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion core/iwasm/interpreter/wasm_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ memory_instantiate(WASMModuleInstance *module_inst, WASMModuleInstance *parent,
* so the range of ea is 0 to 8G
*/
if (!(memory->memory_data = mapped_mem =
os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) {
os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE,
os_get_invalid_handle()))) {
set_error_buf(error_buf, error_buf_size, "mmap memory failed");
goto fail1;
}
Expand Down
2 changes: 1 addition & 1 deletion core/shared/platform/alios/alios_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ os_dumps_proc_mem_info(char *out, unsigned int size)
}

void *
os_mmap(void *hint, size_t size, int prot, int flags)
os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
{
if ((uint64)size >= UINT32_MAX)
return NULL;
Expand Down
6 changes: 6 additions & 0 deletions core/shared/platform/alios/platform_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,10 @@ typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;

static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}

#endif /* end of _BH_PLATFORM_H */
6 changes: 6 additions & 0 deletions core/shared/platform/android/platform_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;

static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}

#ifdef __cplusplus
}
#endif
Expand Down
6 changes: 0 additions & 6 deletions core/shared/platform/common/posix/posix_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -978,12 +978,6 @@ os_closedir(os_dir_stream dir_stream)
return __WASI_ESUCCESS;
}

os_file_handle
os_get_invalid_handle()
{
return -1;
}

os_dir_stream
os_get_invalid_dir_stream()
{
Expand Down
8 changes: 4 additions & 4 deletions core/shared/platform/common/posix/posix_memmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ round_down(uintptr_t v, uintptr_t b)
#endif

void *
os_mmap(void *hint, size_t size, int prot, int flags)
os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
{
int map_prot = PROT_NONE;
#if (defined(__APPLE__) || defined(__MACH__)) && defined(__arm64__)
Expand Down Expand Up @@ -114,7 +114,7 @@ os_mmap(void *hint, size_t size, int prot, int flags)
/* try 10 times, step with 1MB each time */
for (i = 0; i < 10 && hint_addr < (uint8 *)(uintptr_t)(2ULL * BH_GB);
i++) {
addr = mmap(hint_addr, request_size, map_prot, map_flags, -1, 0);
addr = mmap(hint_addr, request_size, map_prot, map_flags, file, 0);
if (addr != MAP_FAILED) {
if (addr > (uint8 *)(uintptr_t)(2ULL * BH_GB)) {
/* unmap and try again if the mapped address doesn't
Expand All @@ -136,7 +136,7 @@ os_mmap(void *hint, size_t size, int prot, int flags)
if (addr == MAP_FAILED) {
/* try 5 times */
for (i = 0; i < 5; i++) {
addr = mmap(hint, request_size, map_prot, map_flags, -1, 0);
addr = mmap(hint, request_size, map_prot, map_flags, file, 0);
if (addr != MAP_FAILED)
break;
}
Expand Down Expand Up @@ -266,4 +266,4 @@ os_icache_flush(void *start, size_t len)
#if (defined(__APPLE__) || defined(__MACH__)) && defined(__arm64__)
sys_icache_invalidate(start, len);
#endif
}
}
2 changes: 1 addition & 1 deletion core/shared/platform/common/posix/posix_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ os_thread_signal_init(os_signal_handler handler)

/* Initialize memory for signal alternate stack of current thread */
if (!(map_addr = os_mmap(NULL, map_size, MMAP_PROT_READ | MMAP_PROT_WRITE,
MMAP_MAP_NONE))) {
MMAP_MAP_NONE, os_get_invalid_handle()))) {
os_printf("Failed to mmap memory for alternate stack\n");
goto fail1;
}
Expand Down
6 changes: 6 additions & 0 deletions core/shared/platform/darwin/platform_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;

static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion core/shared/platform/esp-idf/espidf_memmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static portMUX_TYPE s_spinlock = portMUX_INITIALIZER_UNLOCKED;
#endif

void *
os_mmap(void *hint, size_t size, int prot, int flags)
os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
{
if (prot & MMAP_PROT_EXEC) {
#if (WASM_MEM_DUAL_BUS_MIRROR != 0)
Expand Down
6 changes: 6 additions & 0 deletions core/shared/platform/esp-idf/platform_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;

static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}

#ifdef __cplusplus
}
#endif
Expand Down
8 changes: 8 additions & 0 deletions core/shared/platform/freebsd/platform_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ os_sigreturn();
void
os_set_signal_number_for_blocking_op(int signo);

typedef int os_file_handle;

static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion core/shared/platform/include/platform_api_vmcore.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ enum {
};

void *
os_mmap(void *hint, size_t size, int prot, int flags);
os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file);
void
os_munmap(void *addr, size_t size);
int
Expand Down
6 changes: 6 additions & 0 deletions core/shared/platform/linux-sgx/platform_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;

static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}

#ifdef __cplusplus
}
#endif
Expand Down
11 changes: 9 additions & 2 deletions core/shared/platform/linux-sgx/sgx_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,20 @@ strcpy(char *dest, const char *src)
}

void *
os_mmap(void *hint, size_t size, int prot, int flags)
os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
{
int mprot = 0;
uint64 aligned_size, page_size;
void *ret = NULL;
sgx_status_t st = 0;

if (os_is_handle_valid(&file)) {
os_printf("os_mmap(size=%u, prot=0x%x, file=%x) failed: file is not "
"supported.\n",
size, prot, file);
return NULL;
}

page_size = getpagesize();
aligned_size = (size + page_size - 1) & ~(page_size - 1);

Expand Down Expand Up @@ -198,4 +205,4 @@ os_dcache_flush(void)

void
os_icache_flush(void *start, size_t len)
{}
{}
6 changes: 6 additions & 0 deletions core/shared/platform/linux/platform_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;

static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion core/shared/platform/nuttx/nuttx_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ os_dumps_proc_mem_info(char *out, unsigned int size)
}

void *
os_mmap(void *hint, size_t size, int prot, int flags)
os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
{
#if (WASM_MEM_DUAL_BUS_MIRROR != 0)
void *i_addr, *d_addr;
Expand Down
6 changes: 6 additions & 0 deletions core/shared/platform/nuttx/platform_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;

static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}

#ifdef __cplusplus
}
#endif
Expand Down
6 changes: 6 additions & 0 deletions core/shared/platform/riot/platform_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,10 @@ int isnan(double x);
/* clang-format on */
#endif

static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}

#endif /* end of _BH_PLATFORM_H */
2 changes: 1 addition & 1 deletion core/shared/platform/riot/riot_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ os_dumps_proc_mem_info(char *out, unsigned int size)
}

void *
os_mmap(void *hint, size_t size, int prot, int flags)
os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
{
if (size > ((unsigned)~0))
return NULL;
Expand Down
6 changes: 6 additions & 0 deletions core/shared/platform/rt-thread/platform_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,10 @@ typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;

static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}

#endif /* RTTHREAD_PLATFORM_INTERNAL_H */
2 changes: 1 addition & 1 deletion core/shared/platform/rt-thread/rtt_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ os_cond_wait(korp_cond *cond, korp_mutex *mutex)
}

void *
os_mmap(void *hint, size_t size, int prot, int flags)
os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
{
return rt_malloc(size);
}
Expand Down
6 changes: 6 additions & 0 deletions core/shared/platform/vxworks/platform_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ os_sigreturn();
#endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64 */
#endif /* end of WASM_DISABLE_HW_BOUND_CHECK */

static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}

#ifdef __cplusplus
}
#endif
Expand Down
6 changes: 6 additions & 0 deletions core/shared/platform/windows/platform_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ typedef uint32_t os_raw_file_handle;
#define UWP_DEFAULT_VPRINTF
#endif

static inline os_file_handle
os_get_invalid_handle()
{
return NULL;
}

#ifdef __cplusplus
}
#endif
Expand Down
6 changes: 0 additions & 6 deletions core/shared/platform/windows/win_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1466,12 +1466,6 @@ os_is_dir_stream_valid(os_dir_stream *dir_stream)
return true;
}

os_file_handle
os_get_invalid_handle()
{
return NULL;
}

bool
os_is_handle_valid(os_file_handle *handle)
{
Expand Down
Loading

0 comments on commit 84513bd

Please sign in to comment.