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
This commit is contained in:
Kiyoshi Nakao 2025-10-09 12:24:12 +09:00 committed by GitHub
parent 3f4145e691
commit 49ac85472d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 7 deletions

View File

@ -131,8 +131,9 @@ os_is_handle_valid(os_file_handle *handle)
/* implemented in posix_file.c */ /* implemented in posix_file.c */
#endif #endif
void * static void *
os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file) os_mmap_internal(void *hint, size_t size, int prot, int flags,
os_file_handle file, bool clear)
{ {
int mprot = 0; int mprot = 0;
uint64 aligned_size, page_size; 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; return NULL;
} }
if (clear) {
memset(ret, 0, aligned_size);
}
if (prot & MMAP_PROT_READ) if (prot & MMAP_PROT_READ)
mprot |= SGX_PROT_READ; mprot |= SGX_PROT_READ;
if (prot & MMAP_PROT_WRITE) 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; 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 void
os_munmap(void *addr, size_t size) os_munmap(void *addr, size_t size)
{ {
@ -216,8 +245,10 @@ os_mprotect(void *addr, size_t size, int prot)
void void
os_dcache_flush(void) os_dcache_flush(void)
{} {
}
void void
os_icache_flush(void *start, size_t len) os_icache_flush(void *start, size_t len)
{} {
}

View File

@ -37,9 +37,6 @@ else()
set(source_all ${source_all} ${PLATFORM_COMMON_LIBC_UTIL_SOURCE}) set(source_all ${source_all} ${PLATFORM_COMMON_LIBC_UTIL_SOURCE})
endif() 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) file (GLOB source_all_untrusted ${PLATFORM_SHARED_DIR}/untrusted/*.c)
set (PLATFORM_SHARED_SOURCE ${source_all}) set (PLATFORM_SHARED_SOURCE ${source_all})