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
35 changed files with 127 additions and 45 deletions

View File

@ -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;

View File

@ -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 */

View File

@ -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

View File

@ -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()
{

View File

@ -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__)
@ -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
@ -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;
}
@ -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
}
}

View File

@ -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;
}

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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);
@ -198,4 +205,4 @@ os_dcache_flush(void)
void
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 int os_raw_file_handle;
static inline os_file_handle
os_get_invalid_handle()
{
return -1;
}
#ifdef __cplusplus
}
#endif

View File

@ -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;

View File

@ -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

View File

@ -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 */

View File

@ -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;

View File

@ -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 */

View File

@ -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);
}

View File

@ -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

View File

@ -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

View File

@ -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)
{

View File

@ -29,7 +29,7 @@ access_to_win32_flags(int prot)
}
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 protect;

View File

@ -152,4 +152,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

View File

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