Partial teardown of a MAP_SHARED overlay detaches the surviving pages from the file

Open
#472 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c, macos

Research direction

Start with the failing cases in tests/test-msync.c and trace how partial munmap, MAP_FIXED hole reuse, and mremap update backing IPAs and HVF segments. The report says the fix also affects fork and guest address mapping. Done means the surviving shared pages retain file contents and two way coherence while a replacement anonymous page stays private; all four listed cases should pass.

Written by the indexing model from the issue text.

Description

bug

Problem

On Apple Silicon the host page is 16 KiB and the guest page is 4 KiB, so one host file overlay covers four guest pages. Removing only part of that host page (munmap of one guest page, mremap shrink, or moving part of a source range) tears down the whole overlay and restores slab backing under it. The guest pages that are still mapped keep valid PTEs but now read slab memory: they lose the file contents, a host pwrite is no longer visible through them, and their stores no longer reach the file.

Reproduction

char name[] = "/dev/shm/partial-XXXXXX";
int fd = mkstemp(name);
unlink(name);
ftruncate(fd, 16384);
for (int i = 0; i < 4; i++) {
    unsigned char c = 0x31 + i;
    pwrite(fd, &c, 1, i * 4096);
}
unsigned char *p = mmap(NULL, 16384, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
munmap(p, 4096);
/* Linux: p[4096], p[8192], p[12288] read 0x32 0x33 0x34 */

Expected, as on Linux: the removed page faults, the remaining pages read 0x32, 0x33 and 0x34, a pwrite at offset 8192 is visible at p[8192], and a store to p[12288] lands in the file.

Actual on elfuse: the remaining pages read 0x00, and neither direction of coherence holds. The file itself is not modified. mremap shrinking a shared mapping, or moving only part of one, shows the same result.

MAP_FIXED into the hole

A second case remains even if the removal paths keep the overlay alive. After the munmap above, map a different backing into the hole:

unsigned char *q = mmap(p, 4096, PROT_READ | PROT_WRITE,
                        MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
q[0] = 'A';

The anonymous page must not write into the file, and its three neighbors must stay file-backed. One 16 KiB host range cannot carry both backings at once, so keeping the overlay corrupts the file and dropping it detaches the neighbors. Fixing this needs the surviving guest pages moved onto a separate host-aligned backing, which touches how guest addresses map to backing IPAs, HVF segments, fork and mremap.

Environment

  • macOS 27.0.1 (26A434), SDK 27.0, Apple M5
  • elfuse main at e9fdbbb
  • Partial munmap, hole reuse, partial mremap and mremap shrink cases in tests/test-msync.c: all four fail on main

Follow-up to the discussion in #391.

Dominant language
C
Stars
287
Forks
33
Avg merge
2d 17h
Merged PRs (30d)
18

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sysprog21/elfuse

All issues in sysprog21/elfuse

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.