浏览代码

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>
Huang Qi 2 年之前
父节点
当前提交
24aa1cb408
共有 35 个文件被更改,包括 127 次插入45 次删除
  1. 1 1
      core/app-mgr/app-manager/module_wasm_app.c
  2. 10 6
      core/iwasm/aot/aot_loader.c
  3. 2 2
      core/iwasm/aot/aot_runtime.c
  4. 2 1
      core/iwasm/common/wasm_exec_env.c
  5. 2 2
      core/iwasm/fast-jit/jit_codecache.c
  6. 2 1
      core/iwasm/interpreter/wasm_runtime.c
  7. 1 1
      core/shared/platform/alios/alios_platform.c
  8. 6 0
      core/shared/platform/alios/platform_internal.h
  9. 6 0
      core/shared/platform/android/platform_internal.h
  10. 0 6
      core/shared/platform/common/posix/posix_file.c
  11. 4 4
      core/shared/platform/common/posix/posix_memmap.c
  12. 1 1
      core/shared/platform/common/posix/posix_thread.c
  13. 6 0
      core/shared/platform/darwin/platform_internal.h
  14. 1 1
      core/shared/platform/esp-idf/espidf_memmap.c
  15. 6 0
      core/shared/platform/esp-idf/platform_internal.h
  16. 8 0
      core/shared/platform/freebsd/platform_internal.h
  17. 1 1
      core/shared/platform/include/platform_api_vmcore.h
  18. 6 0
      core/shared/platform/linux-sgx/platform_internal.h
  19. 9 2
      core/shared/platform/linux-sgx/sgx_platform.c
  20. 6 0
      core/shared/platform/linux/platform_internal.h
  21. 1 1
      core/shared/platform/nuttx/nuttx_platform.c
  22. 6 0
      core/shared/platform/nuttx/platform_internal.h
  23. 6 0
      core/shared/platform/riot/platform_internal.h
  24. 1 1
      core/shared/platform/riot/riot_platform.c
  25. 6 0
      core/shared/platform/rt-thread/platform_internal.h
  26. 1 1
      core/shared/platform/rt-thread/rtt_platform.c
  27. 6 0
      core/shared/platform/vxworks/platform_internal.h
  28. 6 0
      core/shared/platform/windows/platform_internal.h
  29. 0 6
      core/shared/platform/windows/win_file.c
  30. 1 1
      core/shared/platform/windows/win_memmap.c
  31. 6 0
      core/shared/platform/zephyr/platform_internal.h
  32. 1 1
      core/shared/platform/zephyr/zephyr_platform.c
  33. 2 1
      product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp
  34. 2 2
      product-mini/platforms/posix/main.c
  35. 2 2
      product-mini/platforms/windows/main.c

+ 1 - 1
core/app-mgr/app-manager/module_wasm_app.c

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

+ 10 - 6
core/iwasm/aot/aot_loader.c

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

+ 2 - 2
core/iwasm/aot/aot_runtime.c

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

+ 2 - 1
core/iwasm/common/wasm_exec_env.c

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

+ 2 - 2
core/iwasm/fast-jit/jit_codecache.c

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

+ 2 - 1
core/iwasm/interpreter/wasm_runtime.c

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

+ 1 - 1
core/shared/platform/alios/alios_platform.c

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

+ 6 - 0
core/shared/platform/alios/platform_internal.h

@@ -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 - 0
core/shared/platform/android/platform_internal.h

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

+ 0 - 6
core/shared/platform/common/posix/posix_file.c

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

+ 4 - 4
core/shared/platform/common/posix/posix_memmap.c

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

+ 1 - 1
core/shared/platform/common/posix/posix_thread.c

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

+ 6 - 0
core/shared/platform/darwin/platform_internal.h

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

+ 1 - 1
core/shared/platform/esp-idf/espidf_memmap.c

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

+ 6 - 0
core/shared/platform/esp-idf/platform_internal.h

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

+ 8 - 0
core/shared/platform/freebsd/platform_internal.h

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

+ 1 - 1
core/shared/platform/include/platform_api_vmcore.h

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

+ 6 - 0
core/shared/platform/linux-sgx/platform_internal.h

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

+ 9 - 2
core/shared/platform/linux-sgx/sgx_platform.c

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

+ 6 - 0
core/shared/platform/linux/platform_internal.h

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

+ 1 - 1
core/shared/platform/nuttx/nuttx_platform.c

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

+ 6 - 0
core/shared/platform/nuttx/platform_internal.h

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

+ 6 - 0
core/shared/platform/riot/platform_internal.h

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

+ 1 - 1
core/shared/platform/riot/riot_platform.c

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

+ 6 - 0
core/shared/platform/rt-thread/platform_internal.h

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

+ 1 - 1
core/shared/platform/rt-thread/rtt_platform.c

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

+ 6 - 0
core/shared/platform/vxworks/platform_internal.h

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

+ 6 - 0
core/shared/platform/windows/platform_internal.h

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

+ 0 - 6
core/shared/platform/windows/win_file.c

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

+ 1 - 1
core/shared/platform/windows/win_memmap.c

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

+ 6 - 0
core/shared/platform/zephyr/platform_internal.h

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

+ 1 - 1
core/shared/platform/zephyr/zephyr_platform.c

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

+ 2 - 1
product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp

@@ -250,7 +250,8 @@ handle_cmd_load_module(uint64 *args, uint32 argc)
 
         if (total_size >= UINT32_MAX
             || !(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,
                           "WASM module load failed: mmap memory failed.");
             *(void **)args_org = NULL;

+ 2 - 2
product-mini/platforms/posix/main.c

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

+ 2 - 2
product-mini/platforms/windows/main.c

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