|
|
@ -515,19 +515,44 @@ int32_t
|
|
|
|
wasm_mremap(int32_t offset, int32_t old_size, int32_t new_size, int32_t flags)
|
|
|
|
wasm_mremap(int32_t offset, int32_t old_size, int32_t new_size, int32_t flags)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
/* Should fit within the 32-bit linear address space */
|
|
|
|
/* Should fit within the 32-bit linear address space */
|
|
|
|
/* TODO: Improve with errno */
|
|
|
|
/* TODO: Improve with errno and handle flags properly */
|
|
|
|
assert(offset + old_size < INT32_MAX);
|
|
|
|
// debuglog("Offset: %d, Old Size: %d, New Size: %d, May Move: %s, Fixed: %s\n", offset, old_size, new_size,
|
|
|
|
assert(new_size < INT32_MAX);
|
|
|
|
// (flags & MREMAP_MAYMOVE) == MREMAP_MAYMOVE ? "true" : "false",
|
|
|
|
|
|
|
|
// (flags & MREMAP_FIXED) == MREMAP_FIXED ? "true" : "false");
|
|
|
|
|
|
|
|
|
|
|
|
/* Not really implemented, so dump out usage to understand requirements */
|
|
|
|
assert(offset >= 0);
|
|
|
|
debuglog("Offset: %d, Old Size: %d, New Size: %d, May Move: %s, Fixed: %s\n", offset, old_size, new_size,
|
|
|
|
assert(offset + old_size <= INT32_MAX);
|
|
|
|
(flags & MREMAP_MAYMOVE) == MREMAP_MAYMOVE ? "true" : "false",
|
|
|
|
|
|
|
|
(flags & MREMAP_FIXED) == MREMAP_FIXED ? "true" : "false");
|
|
|
|
// We do not implement compaction yet, so just return immediately if shrinking
|
|
|
|
|
|
|
|
if (new_size <= old_size) return offset;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If at end of linear memory, just expand and return same address
|
|
|
|
|
|
|
|
if (offset + old_size == local_sandbox_context_cache.linear_memory_size) {
|
|
|
|
|
|
|
|
int32_t amount_to_expand = new_size - old_size;
|
|
|
|
|
|
|
|
int32_t pages_to_allocate = amount_to_expand / WASM_PAGE_SIZE;
|
|
|
|
|
|
|
|
if (amount_to_expand % WASM_PAGE_SIZE > 0) pages_to_allocate++;
|
|
|
|
|
|
|
|
for (int i = 0; i < pages_to_allocate; i++) expand_memory();
|
|
|
|
|
|
|
|
|
|
|
|
/* Return the current offset, hoping for the best */
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Otherwise allocate at end of address space and copy
|
|
|
|
|
|
|
|
int32_t pages_to_allocate = new_size / WASM_PAGE_SIZE;
|
|
|
|
|
|
|
|
if (new_size % WASM_PAGE_SIZE > 0) pages_to_allocate++;
|
|
|
|
|
|
|
|
int32_t new_offset = local_sandbox_context_cache.linear_memory_size;
|
|
|
|
|
|
|
|
for (int i = 0; i < pages_to_allocate; i++) expand_memory();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get pointer of old offset and pointer of new offset
|
|
|
|
|
|
|
|
char *linear_mem = local_sandbox_context_cache.linear_memory_start;
|
|
|
|
|
|
|
|
char *src = &linear_mem[offset];
|
|
|
|
|
|
|
|
char *dest = &linear_mem[new_offset];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Copy Values. We can use memcpy because we don't overlap
|
|
|
|
|
|
|
|
memcpy((void *)dest, (void *)src, old_size);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new_offset;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define SYS_MADVISE 28
|
|
|
|
#define SYS_MADVISE 28
|
|
|
|
|
|
|
|
|
|
|
|
#define SYS_GETPID 39
|
|
|
|
#define SYS_GETPID 39
|
|
|
|