Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions src/syscall/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -3897,19 +3897,8 @@ int64_t sys_mremap(guest_t *g,

/* Check if the space after the old region is free (overflow-safe) */
if (grow_off <= g->guest_size && grow_len <= g->guest_size - grow_off) {
bool can_grow = true;
for (int i = 0; i < g->nregions; i++) {
if (g->regions[i].start >= grow_off + grow_len)
break;
if (g->regions[i].end > grow_off &&
g->regions[i].start < grow_off + grow_len) {
/* Skip the region being extended */
if (g->regions[i].start == old_off)
continue;
can_grow = false;
break;
}
}
bool can_grow =
!region_range_overlaps(g, grow_off, grow_off + grow_len);
Comment on lines +3900 to +3901

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refusing in-place growth here sends a source whose region extends past grow_off down the move path, which is right, and it removes the old path's worst case: a memset of grow_len zeros straight into a live MAP_SHARED file overlay, which wrote those zeros to the backing file.

One hole survives the move. cleanup_overlays_in_range rounds its teardown up to the host page (ALIGN_UP(end, hps), 16 KiB on Apple Silicon) while guest ranges are 4 KiB granular, and hvf_restore_slab_backing re-mmaps fresh slab without repopulating from the file, so growing the first 4 KiB of a shared file mapping leaves the next 12 KiB of the surviving suffix reading slab instead of file contents. That was already reachable through a separate mapping at grow_off, so it is a follow-up rather than something this change owes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. The 16 KiB overlay teardown is independent of this change and remains reachable through a separate mapping at grow_off. It needs a separate issue and regression test. Will take time to reproduce the follow-up and open a new issue.


if (can_grow) {
remove_range_t removed = {old_off, old_off + old_size};
Expand Down
93 changes: 92 additions & 1 deletion tests/test-mremap.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
* Syscalls exercised: mmap(222), mremap(216), munmap(215)
*/

#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/wait.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h>

#include "test-harness.h"
Expand Down Expand Up @@ -397,6 +398,94 @@ static void test_source_range_hole(void)
munmap(p, 4096);
}

static void check_grow_occupied_suffix(unsigned char *first,
unsigned char *neighbor)
{
const size_t page = 4096;
memset(first, 0x5A, page);
memset(neighbor, 0xA5, page);

errno = 0;
void *q = mremap(first, page, 2 * page, 0);
if (q != MAP_FAILED) {
FAIL("growth without MAYMOVE succeeded");
munmap(q, 2 * page);
return;
}
if ((errno != ENOMEM) || (first[0] != 0x5A) || (first[page - 1] != 0x5A) ||
(neighbor[0] != 0xA5) || (neighbor[page - 1] != 0xA5)) {
FAIL("failed growth changed mapped pages");
munmap(first, page);
munmap(neighbor, page);
return;
}

q = mremap(first, page, 2 * page, MREMAP_MAYMOVE);
if ((q == MAP_FAILED) || (q == first)) {
FAIL("growth with MAYMOVE did not relocate");
munmap(first, page);
munmap(neighbor, page);
return;
}

unsigned char *grown = q;
bool ok = (neighbor[0] == 0xA5) && (neighbor[page - 1] == 0xA5) &&
(grown[0] == 0x5A) && (grown[page - 1] == 0x5A) &&
(grown[page] == 0) && (grown[2 * page - 1] == 0);
EXPECT_TRUE(ok, "relocation corrupted mapping contents");

munmap(grown, 2 * page);
munmap(neighbor, page);
}

/* Test 10: an adjacent coalesced mapping blocks in-place growth */

static void test_grow_adjacent_mapping(void)
{
TEST("mremap grow preserves adjacent mapping");

const size_t page = 4096;
unsigned char *base = mmap(NULL, 3 * page, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if ((base == MAP_FAILED) || (munmap(base, 3 * page) != 0)) {
FAIL("reserve failed");
return;
}

unsigned char *first = mmap(base, page, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
unsigned char *neighbor =
Comment thread
Ksld154 marked this conversation as resolved.
mmap(base + page, page, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
Comment thread
Ksld154 marked this conversation as resolved.
if ((first != base) || (neighbor != base + page)) {
FAIL("adjacent mmap failed");
if (first == base)
munmap(first, page);
if (neighbor == base + page)
munmap(neighbor, page);
return;
}

check_grow_occupied_suffix(first, neighbor);
}

/* Test 11: a mapped suffix blocks partial-range growth */

static void test_grow_mapped_suffix(void)
{
TEST("mremap grow preserves mapped suffix");

const size_t page = 4096;
unsigned char *base = mmap(NULL, 2 * page, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (base == MAP_FAILED) {
FAIL("mmap failed");
return;
}

check_grow_occupied_suffix(base, base + page);
}

int main(void)
{
printf("test-mremap: mremap syscall tests\n");
Expand All @@ -410,6 +499,8 @@ int main(void)
test_invalid_args();
test_prot_none_mremap();
test_source_range_hole();
test_grow_adjacent_mapping();
test_grow_mapped_suffix();

SUMMARY("test-mremap");
return fails > 0 ? 1 : 0;
Expand Down
Loading