Skip to content

Commit 8486fcb

Browse files
committed
Add SGX-specific os_mremap to zero-clear remaining memory after memcpy
1 parent f4a491f commit 8486fcb

1 file changed

Lines changed: 28 additions & 3 deletions

File tree

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ 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, os_file_handle file, bool clear)
136136
{
137137
int mprot = 0;
138138
uint64 aligned_size, page_size;
@@ -161,7 +161,9 @@ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
161161
return NULL;
162162
}
163163

164-
memset(ret, 0, aligned_size);
164+
if (clear) {
165+
memset(ret, 0, aligned_size);
166+
}
165167

166168
if (prot & MMAP_PROT_READ)
167169
mprot |= SGX_PROT_READ;
@@ -181,6 +183,29 @@ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
181183
return ret;
182184
}
183185

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

0 commit comments

Comments
 (0)