瀏覽代碼

Fix memory grow on SGX platform (#4651)

* SGX: zero-initialize reserved memory in os_mmap after allocation
* Add SGX-specific os_mremap to zero-clear remaining memory after memcpy
* Modify core/shared/platform/linux-sgx/shared_platform.cmake not to include platform_api_memory.cmake
* Modify core/shared/platform/linux-sgx/shared_platform.cmake to remove unnecessary PLATFORM_COMMON_MEMORY_SOURCE
Kiyoshi Nakao 3 月之前
父節點
當前提交
49ac85472d
共有 2 個文件被更改,包括 35 次插入7 次删除
  1. 35 4
      core/shared/platform/linux-sgx/sgx_platform.c
  2. 0 3
      core/shared/platform/linux-sgx/shared_platform.cmake

+ 35 - 4
core/shared/platform/linux-sgx/sgx_platform.c

@@ -131,8 +131,9 @@ os_is_handle_valid(os_file_handle *handle)
 /* implemented in posix_file.c */
 #endif
 
-void *
-os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
+static void *
+os_mmap_internal(void *hint, size_t size, int prot, int flags,
+                 os_file_handle file, bool clear)
 {
     int mprot = 0;
     uint64 aligned_size, page_size;
@@ -161,6 +162,10 @@ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
         return NULL;
     }
 
+    if (clear) {
+        memset(ret, 0, aligned_size);
+    }
+
     if (prot & MMAP_PROT_READ)
         mprot |= SGX_PROT_READ;
     if (prot & MMAP_PROT_WRITE)
@@ -179,6 +184,30 @@ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
     return ret;
 }
 
+void *
+os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
+{
+    return os_mmap_internal(hint, size, prot, flags, file, true);
+}
+
+void *
+os_mremap(void *old_addr, size_t old_size, size_t new_size)
+{
+    void *new_memory =
+        os_mmap_internal(NULL, new_size, MMAP_PROT_WRITE | MMAP_PROT_READ, 0,
+                         os_get_invalid_handle(), false);
+    if (!new_memory) {
+        return NULL;
+    }
+    size_t copy_size = new_size < old_size ? new_size : old_size;
+    memcpy(new_memory, old_addr, copy_size);
+    if (new_size > copy_size) {
+        memset((char *)new_memory + copy_size, 0, new_size - copy_size);
+    }
+    os_munmap(old_addr, old_size);
+    return new_memory;
+}
+
 void
 os_munmap(void *addr, size_t size)
 {
@@ -216,8 +245,10 @@ os_mprotect(void *addr, size_t size, int prot)
 
 void
 os_dcache_flush(void)
-{}
+{
+}
 
 void
 os_icache_flush(void *start, size_t len)
-{}
+{
+}

+ 0 - 3
core/shared/platform/linux-sgx/shared_platform.cmake

@@ -37,9 +37,6 @@ else()
   set(source_all ${source_all} ${PLATFORM_COMMON_LIBC_UTIL_SOURCE})
 endif()
 
-include (${CMAKE_CURRENT_LIST_DIR}/../common/memory/platform_api_memory.cmake)
-set (source_all ${source_all} ${PLATFORM_COMMON_MEMORY_SOURCE})
-
 file (GLOB source_all_untrusted ${PLATFORM_SHARED_DIR}/untrusted/*.c)
 
 set (PLATFORM_SHARED_SOURCE ${source_all})