Extend os_mmap to support map file from fd (#2763)

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>
This commit is contained in:
Huang Qi 2023-11-16 08:28:54 +08:00 committed by GitHub
parent 0b8a904193
commit 24aa1cb408
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
35 changed files with 127 additions and 45 deletions

View File

@ -1467,7 +1467,7 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch, int request_total_size,
if (total_size >= UINT32_MAX if (total_size >= UINT32_MAX
|| !(section->section_body = || !(section->section_body =
os_mmap(NULL, (uint32)total_size, map_prot, os_mmap(NULL, (uint32)total_size, map_prot,
map_flags))) { map_flags, os_get_invalid_handle()))) {
app_manager_printf( app_manager_printf(
"Allocate executable memory failed!\n"); "Allocate executable memory failed!\n");
SEND_ERR_RESPONSE(recv_ctx.message.request_mid, SEND_ERR_RESPONSE(recv_ctx.message.request_mid,

View File

@ -1574,8 +1574,9 @@ load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end,
/* Allocate memory for data */ /* Allocate memory for data */
if (data_sections[i].size > 0 if (data_sections[i].size > 0
&& !(data_sections[i].data = os_mmap(NULL, data_sections[i].size, && !(data_sections[i].data =
map_prot, map_flags))) { 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"); set_error_buf(error_buf, error_buf_size, "allocate memory failed");
return false; return false;
} }
@ -2470,7 +2471,8 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,
if (size > UINT32_MAX if (size > UINT32_MAX
|| !(module->extra_plt_data = || !(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"); set_error_buf(error_buf, error_buf_size, "mmap memory failed");
goto fail; goto fail;
} }
@ -2593,7 +2595,8 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,
size = (uint64)sizeof(void *) * got_item_count; size = (uint64)sizeof(void *) * got_item_count;
if (size > UINT32_MAX if (size > UINT32_MAX
|| !(module->got_func_ptrs = || !(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"); set_error_buf(error_buf, error_buf_size, "mmap memory failed");
goto fail; goto fail;
} }
@ -3106,8 +3109,9 @@ create_sections(AOTModule *module, const uint8 *buf, uint32 size,
(uint64)section_size + aot_get_plt_table_size(); (uint64)section_size + aot_get_plt_table_size();
total_size = (total_size + 3) & ~((uint64)3); total_size = (total_size + 3) & ~((uint64)3);
if (total_size >= UINT32_MAX if (total_size >= UINT32_MAX
|| !(aot_text = os_mmap(NULL, (uint32)total_size, || !(aot_text =
map_prot, map_flags))) { os_mmap(NULL, (uint32)total_size, map_prot,
map_flags, os_get_invalid_handle()))) {
wasm_runtime_free(section); wasm_runtime_free(section);
set_error_buf(error_buf, error_buf_size, set_error_buf(error_buf, error_buf_size,
"mmap memory failed"); "mmap memory failed");

View File

@ -537,8 +537,8 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
* both i and memarg.offset are u32 in range 0 to 4G * both i and memarg.offset are u32 in range 0 to 4G
* so the range of ea is 0 to 8G * so the range of ea is 0 to 8G
*/ */
if (!(p = mapped_mem = if (!(p = mapped_mem = os_mmap(NULL, map_size, MMAP_PROT_NONE,
os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) { MMAP_MAP_NONE, os_get_invalid_handle()))) {
set_error_buf(error_buf, error_buf_size, "mmap memory failed"); set_error_buf(error_buf, error_buf_size, "mmap memory failed");
return NULL; return NULL;
} }

View File

@ -58,7 +58,8 @@ wasm_exec_env_create_internal(struct WASMModuleInstanceCommon *module_inst,
#ifdef OS_ENABLE_HW_BOUND_CHECK #ifdef OS_ENABLE_HW_BOUND_CHECK
if (!(exec_env->exce_check_guard_page = 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; goto fail5;
#endif #endif

View File

@ -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_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
int map_flags = MMAP_MAP_NONE; int map_flags = MMAP_MAP_NONE;
if (!(code_cache_pool = if (!(code_cache_pool = os_mmap(NULL, code_cache_size, map_prot, map_flags,
os_mmap(NULL, code_cache_size, map_prot, map_flags))) { os_get_invalid_handle()))) {
return false; return false;
} }

View File

@ -324,7 +324,8 @@ memory_instantiate(WASMModuleInstance *module_inst, WASMModuleInstance *parent,
* so the range of ea is 0 to 8G * so the range of ea is 0 to 8G
*/ */
if (!(memory->memory_data = mapped_mem = 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"); set_error_buf(error_buf, error_buf_size, "mmap memory failed");
goto fail1; goto fail1;
} }

View File

@ -47,7 +47,7 @@ os_dumps_proc_mem_info(char *out, unsigned int size)
} }
void * 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) if ((uint64)size >= UINT32_MAX)
return NULL; return NULL;

View File

@ -68,4 +68,10 @@ typedef int os_file_handle;
typedef DIR *os_dir_stream; typedef DIR *os_dir_stream;
typedef int os_raw_file_handle; typedef int os_raw_file_handle;
static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}
#endif /* end of _BH_PLATFORM_H */ #endif /* end of _BH_PLATFORM_H */

View File

@ -150,6 +150,12 @@ typedef int os_file_handle;
typedef DIR *os_dir_stream; typedef DIR *os_dir_stream;
typedef int os_raw_file_handle; typedef int os_raw_file_handle;
static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -978,12 +978,6 @@ os_closedir(os_dir_stream dir_stream)
return __WASI_ESUCCESS; return __WASI_ESUCCESS;
} }
os_file_handle
os_get_invalid_handle()
{
return -1;
}
os_dir_stream os_dir_stream
os_get_invalid_dir_stream() os_get_invalid_dir_stream()
{ {

View File

@ -37,7 +37,7 @@ round_down(uintptr_t v, uintptr_t b)
#endif #endif
void * 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; int map_prot = PROT_NONE;
#if (defined(__APPLE__) || defined(__MACH__)) && defined(__arm64__) #if (defined(__APPLE__) || defined(__MACH__)) && defined(__arm64__)
@ -114,7 +114,7 @@ os_mmap(void *hint, size_t size, int prot, int flags)
/* try 10 times, step with 1MB each time */ /* try 10 times, step with 1MB each time */
for (i = 0; i < 10 && hint_addr < (uint8 *)(uintptr_t)(2ULL * BH_GB); for (i = 0; i < 10 && hint_addr < (uint8 *)(uintptr_t)(2ULL * BH_GB);
i++) { 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 != MAP_FAILED) {
if (addr > (uint8 *)(uintptr_t)(2ULL * BH_GB)) { if (addr > (uint8 *)(uintptr_t)(2ULL * BH_GB)) {
/* unmap and try again if the mapped address doesn't /* unmap and try again if the mapped address doesn't
@ -136,7 +136,7 @@ os_mmap(void *hint, size_t size, int prot, int flags)
if (addr == MAP_FAILED) { if (addr == MAP_FAILED) {
/* try 5 times */ /* try 5 times */
for (i = 0; i < 5; i++) { 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) if (addr != MAP_FAILED)
break; break;
} }
@ -266,4 +266,4 @@ os_icache_flush(void *start, size_t len)
#if (defined(__APPLE__) || defined(__MACH__)) && defined(__arm64__) #if (defined(__APPLE__) || defined(__MACH__)) && defined(__arm64__)
sys_icache_invalidate(start, len); sys_icache_invalidate(start, len);
#endif #endif
} }

View File

@ -664,7 +664,7 @@ os_thread_signal_init(os_signal_handler handler)
/* Initialize memory for signal alternate stack of current thread */ /* Initialize memory for signal alternate stack of current thread */
if (!(map_addr = os_mmap(NULL, map_size, MMAP_PROT_READ | MMAP_PROT_WRITE, 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"); os_printf("Failed to mmap memory for alternate stack\n");
goto fail1; goto fail1;
} }

View File

@ -113,6 +113,12 @@ typedef int os_file_handle;
typedef DIR *os_dir_stream; typedef DIR *os_dir_stream;
typedef int os_raw_file_handle; typedef int os_raw_file_handle;
static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -19,7 +19,7 @@ static portMUX_TYPE s_spinlock = portMUX_INITIALIZER_UNLOCKED;
#endif #endif
void * 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 (prot & MMAP_PROT_EXEC) {
#if (WASM_MEM_DUAL_BUS_MIRROR != 0) #if (WASM_MEM_DUAL_BUS_MIRROR != 0)

View File

@ -113,6 +113,12 @@ typedef int os_file_handle;
typedef DIR *os_dir_stream; typedef DIR *os_dir_stream;
typedef int os_raw_file_handle; typedef int os_raw_file_handle;
static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -112,6 +112,14 @@ os_sigreturn();
void void
os_set_signal_number_for_blocking_op(int signo); 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 #ifdef __cplusplus
} }
#endif #endif

View File

@ -130,7 +130,7 @@ enum {
}; };
void * 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 void
os_munmap(void *addr, size_t size); os_munmap(void *addr, size_t size);
int int

View File

@ -73,6 +73,12 @@ typedef int os_file_handle;
typedef DIR *os_dir_stream; typedef DIR *os_dir_stream;
typedef int os_raw_file_handle; typedef int os_raw_file_handle;
static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -120,13 +120,20 @@ strcpy(char *dest, const char *src)
} }
void * 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; int mprot = 0;
uint64 aligned_size, page_size; uint64 aligned_size, page_size;
void *ret = NULL; void *ret = NULL;
sgx_status_t st = 0; 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(); page_size = getpagesize();
aligned_size = (size + page_size - 1) & ~(page_size - 1); aligned_size = (size + page_size - 1) & ~(page_size - 1);
@ -198,4 +205,4 @@ os_dcache_flush(void)
void void
os_icache_flush(void *start, size_t len) os_icache_flush(void *start, size_t len)
{} {}

View File

@ -126,6 +126,12 @@ typedef int os_file_handle;
typedef DIR *os_dir_stream; typedef DIR *os_dir_stream;
typedef int os_raw_file_handle; typedef int os_raw_file_handle;
static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -85,7 +85,7 @@ os_dumps_proc_mem_info(char *out, unsigned int size)
} }
void * 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) #if (WASM_MEM_DUAL_BUS_MIRROR != 0)
void *i_addr, *d_addr; void *i_addr, *d_addr;

View File

@ -134,6 +134,12 @@ typedef int os_file_handle;
typedef DIR *os_dir_stream; typedef DIR *os_dir_stream;
typedef int os_raw_file_handle; typedef int os_raw_file_handle;
static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -80,4 +80,10 @@ int isnan(double x);
/* clang-format on */ /* clang-format on */
#endif #endif
static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}
#endif /* end of _BH_PLATFORM_H */ #endif /* end of _BH_PLATFORM_H */

View File

@ -50,7 +50,7 @@ os_dumps_proc_mem_info(char *out, unsigned int size)
} }
void * 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)) if (size > ((unsigned)~0))
return NULL; return NULL;

View File

@ -49,4 +49,10 @@ typedef int os_file_handle;
typedef DIR *os_dir_stream; typedef DIR *os_dir_stream;
typedef int os_raw_file_handle; typedef int os_raw_file_handle;
static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}
#endif /* RTTHREAD_PLATFORM_INTERNAL_H */ #endif /* RTTHREAD_PLATFORM_INTERNAL_H */

View File

@ -191,7 +191,7 @@ os_cond_wait(korp_cond *cond, korp_mutex *mutex)
} }
void * 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); return rt_malloc(size);
} }

View File

@ -100,6 +100,12 @@ os_sigreturn();
#endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64 */ #endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64 */
#endif /* end of WASM_DISABLE_HW_BOUND_CHECK */ #endif /* end of WASM_DISABLE_HW_BOUND_CHECK */
static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -187,6 +187,12 @@ typedef uint32_t os_raw_file_handle;
#define UWP_DEFAULT_VPRINTF #define UWP_DEFAULT_VPRINTF
#endif #endif
static inline os_file_handle
os_get_invalid_handle()
{
return NULL;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -1466,12 +1466,6 @@ os_is_dir_stream_valid(os_dir_stream *dir_stream)
return true; return true;
} }
os_file_handle
os_get_invalid_handle()
{
return NULL;
}
bool bool
os_is_handle_valid(os_file_handle *handle) os_is_handle_valid(os_file_handle *handle)
{ {

View File

@ -29,7 +29,7 @@ access_to_win32_flags(int prot)
} }
void * 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)
{ {
DWORD alloc_type = MEM_RESERVE; DWORD alloc_type = MEM_RESERVE;
DWORD protect; DWORD protect;

View File

@ -152,4 +152,10 @@ typedef int os_file_handle;
typedef DIR *os_dir_stream; typedef DIR *os_dir_stream;
typedef int os_raw_file_handle; typedef int os_raw_file_handle;
static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}
#endif #endif

View File

@ -173,7 +173,7 @@ strcspn(const char *s, const char *reject)
#endif #endif
void * 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) if ((uint64)size >= UINT32_MAX)
return NULL; return NULL;

View File

@ -250,7 +250,8 @@ handle_cmd_load_module(uint64 *args, uint32 argc)
if (total_size >= UINT32_MAX if (total_size >= UINT32_MAX
|| !(enclave_module = (EnclaveModule *)os_mmap( || !(enclave_module = (EnclaveModule *)os_mmap(
NULL, (uint32)total_size, map_prot, map_flags))) { NULL, (uint32)total_size, map_prot, map_flags,
os_get_invalid_handle()))) {
set_error_buf(error_buf, error_buf_size, set_error_buf(error_buf, error_buf_size,
"WASM module load failed: mmap memory failed."); "WASM module load failed: mmap memory failed.");
*(void **)args_org = NULL; *(void **)args_org = NULL;

View File

@ -848,8 +848,8 @@ main(int argc, char *argv[])
int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC; int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
int map_flags = MMAP_MAP_32BIT; int map_flags = MMAP_MAP_32BIT;
if (!(wasm_file_mapped = if (!(wasm_file_mapped = os_mmap(NULL, (uint32)wasm_file_size, map_prot,
os_mmap(NULL, (uint32)wasm_file_size, map_prot, map_flags))) { map_flags, os_get_invalid_handle()))) {
printf("mmap memory failed\n"); printf("mmap memory failed\n");
wasm_runtime_free(wasm_file_buf); wasm_runtime_free(wasm_file_buf);
goto fail1; goto fail1;

View File

@ -488,8 +488,8 @@ main(int argc, char *argv[])
int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC; int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
int map_flags = MMAP_MAP_32BIT; int map_flags = MMAP_MAP_32BIT;
if (!(wasm_file_mapped = if (!(wasm_file_mapped = os_mmap(NULL, (uint32)wasm_file_size, map_prot,
os_mmap(NULL, (uint32)wasm_file_size, map_prot, map_flags))) { map_flags, os_get_invalid_handle()))) {
printf("mmap memory failed\n"); printf("mmap memory failed\n");
wasm_runtime_free(wasm_file_buf); wasm_runtime_free(wasm_file_buf);
goto fail1; goto fail1;