{
  "slug": "memory-mapped-file",
  "term": "Memory-mapped file",
  "aliases": [
    "file-backed mmap",
    "mmap file mapping"
  ],
  "definition": "A memory-mapped file exposes a file region through a process's virtual address space so its contents can be accessed with memory loads and stores.",
  "note": "Mapping mode: MAP_SHARED permits file-backed updates; MAP_PRIVATE keeps modifications private through copy-on-write but does not promise a snapshot of later external changes. Closing the original descriptor leaves the mapping valid. [mmap-api]\n\nFile extent: mapping length does not grow the file. Whole pages beyond the object can raise SIGBUS; stores in the last partial page's unused tail do not extend it. [posix-mmap] [mmap-api]\n\nPersistence: background writeback can precede msync; synchronization is distinct from an atomic transaction. [posix-msync]\n\nSealed objects: memfd seals can restrict writes or resizing, but FUTURE_WRITE permits earlier shared writable mappings to keep writing. [seals-api]\n\nPartial-page tail: Linux warns that out-of-file bytes can remain visible after close and unmap; msync does not clear this tail on tmpfs. A Linux 6.8 probe with ext4 and tmpfs controls reproduced this without changing file contents or length. This is not evidence of persistence after reboot or portable tail clearing. [mmap-api] [hesela-mmap]\n\nChange detection: a controlled shared-mapping test changed bytes without IN_MODIFY before final close; a writable-close event also occurred without changing bytes. [hesela-inotify]\n\nDiscard advice: native MADV_DONTNEED discarded private edits in controlled Linux tests, while POSIX_MADV_DONTNEED in glibc retained them. Interface and mapping mode determine the meaning of success; the tests measure bytes, not globally cold cache state or persistence. [hesela-advice]\n\nRetained access: on one Linux 6.8 host (ext4/tmpfs), an O_RDWR handle admitted a shared writable mapping after chmod 000 denied fresh opens; it wrote after descriptor close. This shows visibility, not crash durability. [lifetime-study]",
  "status": "shipping",
  "supersedes": [],
  "supersededBy": [],
  "firstSeen": null,
  "lastSeen": null,
  "updated": "2026-12-01",
  "advantages": [
    "Allows a file region to be addressed without an explicit read call for each access."
  ],
  "limitations": [
    "Concurrent access and file resizing require an application-level coordination protocol.",
    "MAP_PRIVATE and prefaulting do not establish a consistent file version; the external-writer experiment is a single-host observation, not a portable visibility guarantee."
  ],
  "related": [
    "msync",
    "file-truncation",
    "page-cache",
    "copy-on-write",
    "lmdb",
    "file-sealing",
    "memfd-create",
    "fastmap",
    "inotify",
    "madv-dontneed",
    "posix-madv-dontneed",
    "mlock",
    "tmpfs",
    "flush-view-of-file",
    "flush-file-buffers",
    "flashmap",
    "madv-populate-read",
    "madv-populate-write"
  ],
  "citations": [
    {
      "id": "mmap-api",
      "title": "Linux man-pages: mmap(2), mapping modes and lifetime",
      "url": "https://man7.org/linux/man-pages/man2/mmap.2.html",
      "accessed": "2026-10-18"
    },
    {
      "id": "posix-mmap",
      "title": "POSIX.1-2017: mmap(), object size and mapping boundaries",
      "url": "https://man7.org/linux/man-pages/man3/mmap.3p.html",
      "accessed": "2026-10-17"
    },
    {
      "id": "posix-msync",
      "title": "POSIX.1-2017: msync(), completion and background writeback",
      "url": "https://man7.org/linux/man-pages/man3/msync.3p.html",
      "accessed": "2026-10-17"
    },
    {
      "id": "hesela-mmap",
      "title": "Hesela: Memory-mapped files: visibility, size and synchronization",
      "url": "https://hesela.com/analyses/mmap-visibility-size-durability/#tail-remapping",
      "accessed": "2026-10-18"
    },
    {
      "id": "seals-api",
      "title": "Linux man-pages: F_GET_SEALS / F_ADD_SEALS",
      "url": "https://man7.org/linux/man-pages/man2/F_ADD_SEALS.2const.html",
      "accessed": "2026-10-17"
    },
    {
      "id": "hesela-inotify",
      "title": "Hesela: What inotify misses: mapped writes and collapsed change events",
      "url": "https://hesela.com/analyses/inotify-change-detection/",
      "accessed": "2026-10-19"
    },
    {
      "id": "hesela-advice",
      "title": "Hesela: When don't need discards private edits",
      "url": "https://hesela.com/analyses/mmap-visibility-size-durability/#discard-advice",
      "accessed": "2026-10-20"
    },
    {
      "id": "hesela-private-history",
      "title": "Hesela: Private page history is not a file version, six ordered Linux controls",
      "url": "https://hesela.com/analyses/mmap-visibility-size-durability/#private-history",
      "accessed": "2026-10-26"
    },
    {
      "id": "cpython-mmap-constructor",
      "title": "CPython v3.12.3: Unix new_mmap_object access-mode dispatch",
      "url": "https://github.com/python/cpython/blob/v3.12.3/Modules/mmapmodule.c#L1157-L1189",
      "accessed": "2026-10-26"
    },
    {
      "id": "hesela-private-compiler",
      "title": "Hesela: compiler-dependent private-mapping reproduction, source, disassembly and two captures",
      "url": "https://hesela.dev/research/map-private-history/compiler-control.zip",
      "accessed": "2026-10-27"
    },
    {
      "id": "gcc-volatile",
      "title": "GCC: volatile accesses and limits of memory ordering",
      "url": "https://gcc.gnu.org/onlinedocs/gcc/Volatiles.html",
      "accessed": "2026-10-27"
    },
    {
      "id": "lifetime-kernel-mmap",
      "title": "Linux v6.8: do_mmap and FMODE_WRITE",
      "url": "https://raw.githubusercontent.com/torvalds/linux/v6.8/mm/mmap.c",
      "accessed": "2026-11-05"
    },
    {
      "id": "lifetime-kernel-security",
      "title": "Linux v6.8: vm_mmap_pgoff security hook",
      "url": "https://raw.githubusercontent.com/torvalds/linux/v6.8/mm/util.c",
      "accessed": "2026-11-05"
    },
    {
      "id": "lifetime-study",
      "title": "Hesela: retained local access after permission changes, native evidence and limits",
      "url": "https://hesela.com/analyses/posix-acl-restore-permissions/#retained-access",
      "accessed": "2026-11-05"
    },
    {
      "id": "lifetime-evidence",
      "title": "Hesela: permission-lifetime native observations, source receipts and MIT replay bundle",
      "url": "https://hesela.dev/research/permission-lifetime/permission-lifetime.zip",
      "accessed": "2026-11-05"
    }
  ],
  "provenance": [
    "The POSIX citations refer to the 2017 edition; Linux-specific details are identified separately.",
    "The companion experiment checks API behavior on one host, not power-loss survival.",
    "The private-history supplement uses six controls on one Linux host. Prefaulting and read-only protection did not freeze clean mappings; pathname replacement preserved old-inode identity, not immutability. This is not a portable snapshot recipe or a database-engine bug.",
    "CPython 3.12.3 on Unix maps ACCESS_READ to MAP_SHARED with PROT_READ; ACCESS_COPY selects private read/write mapping. This probe explicitly uses MAP_PRIVATE and PROT_READ; convenience-mode behavior was not separately measured. [cpython-mmap-constructor]",
    "Compiler supplement: 24 repeated observations, two captures on Linux 6.8/ext4, GCC 13.3.0; not independent samples. The original writer helpers are reused. No universal compiler behavior or kernel fault tracing is claimed. [hesela-private-compiler]",
    "Reproduction: in GCC 13.3.0 controls, plain C self-assignment lost its store at -O2 and read BB, while a volatile byte store retained AB. Emitted accesses matter; volatile supplies neither a snapshot nor synchronization. [hesela-private-compiler] [gcc-volatile]",
    "Permission-lifetime evidence: nine scenarios, 108 recorded executions and 108 matching replay records; two compiler configurations, three repetitions per mount and one host, not independent fleet samples. No changed LSM policy, concurrent writer or power-loss experiment. [lifetime-evidence]",
    "Version consistency: after an ordered writer exited, a Linux probe read AB from a private mapping although file history was AA -> BA -> BB. A same-value store retained page 0; page 1 followed the file. [hesela-private-history]",
    "Shared writable mapping admission checks file->f_mode and additional restrictions. The mapping security hook still applies; the experiment did not change LSM policy or test NFS. [lifetime-kernel-mmap] [lifetime-kernel-security]"
  ]
}
