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

Offen
#472 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 2 Tagen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Anfängerfreundlichkeit
25/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
c, macos

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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.

Vorherrschende Sprache
C
Sterne
287
Forks
33
Ø Merge
2 T. 17 Std.
Gemergte PRs (30 T.)
18

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus sysprog21/elfuse

Alle Issues in sysprog21/elfuse

Ähnliche Issues

Weitere Issues zu C

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.