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

Aberta
#472 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 2 dias

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
5/5
Tempo estimado
Mais de uma semana
Facilidade para iniciantes
25/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
c, macos

Direção de pesquisa

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.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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.

Linguagem predominante
C
Estrelas
287
Forks
33
Merge médio
2d 17h
PRs com merge (30d)
18

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de sysprog21/elfuse

Todas as issues de sysprog21/elfuse

Issues semelhantes

Mais issues de C

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.