Skip to content

Commit 49ac854

Browse files
authored
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
1 parent 3f4145e commit 49ac854

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

core/shared/platform/linux-sgx/sgx_platform.c

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ os_is_handle_valid(os_file_handle *handle)
131131
/* implemented in posix_file.c */
132132
#endif
133133

134-
void *
135-
os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
134+
static void *
135+
os_mmap_internal(void *hint, size_t size, int prot, int flags,
136+
os_file_handle file, bool clear)
136137
{
137138
int mprot = 0;
138139
uint64 aligned_size, page_size;
@@ -161,6 +162,10 @@ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
161162
return NULL;
162163
}
163164

165+
if (clear) {
166+
memset(ret, 0, aligned_size);
167+
}
168+
164169
if (prot & MMAP_PROT_READ)
165170
mprot |= SGX_PROT_READ;
166171
if (prot & MMAP_PROT_WRITE)
@@ -179,6 +184,30 @@ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
179184
return ret;
180185
}
181186

187+
void *
188+
os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
189+
{
190+
return os_mmap_internal(hint, size, prot, flags, file, true);
191+
}
192+
193+
void *
194+
os_mremap(void *old_addr, size_t old_size, size_t new_size)
195+
{
196+
void *new_memory =
197+
os_mmap_internal(NULL, new_size, MMAP_PROT_WRITE | MMAP_PROT_READ, 0,
198+
os_get_invalid_handle(), false);
199+
if (!new_memory) {
200+
return NULL;
201+
}
202+
size_t copy_size = new_size < old_size ? new_size : old_size;
203+
memcpy(new_memory, old_addr, copy_size);
204+
if (new_size > copy_size) {
205+
memset((char *)new_memory + copy_size, 0, new_size - copy_size);
206+
}
207+
os_munmap(old_addr, old_size);
208+
return new_memory;
209+
}
210+
182211
void
183212
os_munmap(void *addr, size_t size)
184213
{
@@ -216,8 +245,10 @@ os_mprotect(void *addr, size_t size, int prot)
216245

217246
void
218247
os_dcache_flush(void)
219-
{}
248+
{
249+
}
220250

221251
void
222252
os_icache_flush(void *start, size_t len)
223-
{}
253+
{
254+
}

core/shared/platform/linux-sgx/shared_platform.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ else()
3737
set(source_all ${source_all} ${PLATFORM_COMMON_LIBC_UTIL_SOURCE})
3838
endif()
3939

40-
include (${CMAKE_CURRENT_LIST_DIR}/../common/memory/platform_api_memory.cmake)
41-
set (source_all ${source_all} ${PLATFORM_COMMON_MEMORY_SOURCE})
42-
4340
file (GLOB source_all_untrusted ${PLATFORM_SHARED_DIR}/untrusted/*.c)
4441

4542
set (PLATFORM_SHARED_SOURCE ${source_all})

0 commit comments

Comments
 (0)