Skip to content

Enable the mmap stdlib module on Nanvix#561

Merged
ppenna merged 3 commits into
nanvix/v3.12.3from
feat/301-mmap
Apr 29, 2026
Merged

Enable the mmap stdlib module on Nanvix#561
ppenna merged 3 commits into
nanvix/v3.12.3from
feat/301-mmap

Conversation

@ada-x64
Copy link
Copy Markdown

@ada-x64 ada-x64 commented Apr 29, 2026

Closes #301.

Drops mmap from PY_STDLIB_MOD_SET_NA for the Nanvix arm and adds the one autoconf check needed to make the build link cleanly. Two commits: switch flip, then the HAVE_MSYNC guard that lets the link succeed.

What this enables

The mmap module imports. Most of it works against the existing libposix mmap binding (which today supports MAP_PRIVATE | MAP_ANONYMOUS with fd == -1):

  • import mmap; all constants (PAGESIZE, MAP_*, PROT_*, ACCESS_*, MADV_*).
  • mmap.mmap(-1, size) — anonymous private mappings. CPython's POSIX constructor (mmapmodule.c:1318) treats fd == -1 as "give me anonymous memory" and adds MAP_ANONYMOUS itself, so this hits libposix's supported path end-to-end.
  • All buffer operations on the resulting object: read, write, seek, tell, find/rfind, indexing, slicing, len(), iteration, move, read_byte, write_byte, close, size.
  • Use as a buffer-protocol object in any API that accepts one (struct, pickle, hashlib.update, …).
  • .resize() for anonymous mappings (shrink works; grow uses the upstream unmap+remap fallback since mremap is absent).

What this doesn't enable, and why

Each of these is a missing piece in nanvix/nanvix, not in this PR; tracking issues filed:

Why this shape (the second commit)

Modules/mmapmodule.c:683 calls msync() unconditionally on the UNIX branch with no upstream HAVE_MSYNC guard — even though the surrounding madvise/mremap code is all HAVE_*-gated and mmap/mremap are already in the AC_CHECK_FUNCS list right next door. msync is the anomaly.

Without the guard, the build doesn't link (verified: clean ./z distclean && ./z clean && ./z --mode=standalone setup && ./z build fails with Modules/mmapmodule.c:683: undefined reference to msync; nm on every .a in the freshly-set-up sysroot confirms zero msync definitions). With the guard, link succeeds and only mmap.flush() is affected — falling through to the same ValueError users on any other msync-less Unix already see.

Behaviour on every existing in-tree target is bit-identical: their autoconf detects msync, HAVE_MSYNC is defined, the original branch runs.

Per port convention, configure.ac and the generated configure are hand-edited in parallel rather than regenerated with autoreconf.

Verification

All three modes, full cycle (./z distclean && ./z clean && ./z --mode=$MODE setup && ./z build && ./z test): standalone (116 regrtest modules), single-process (162), multi-process (162) all green. mmap moves from the n/a list into the built-in set (38 → 37 n/a); no other module's status changes.

Out of scope

  • Adding test_mmap to NANVIX_TEST_LIST. The module builds and the anonymous path works; wiring up the test will need an is_nanvix → SkipTest guard for the file-backed and MAP_SHARED cases, analogous to upstream's is_emscripten. Separate PR by convention.
  • _posixshmem (Enable _posixshmem stdlib module (POSIX shared memory) #306) and other related modules.

Copilot AI review requested due to automatic review settings April 29, 2026 17:13
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Enables the mmap stdlib extension module for the Nanvix port by removing it from Nanvix’s “not available” module set, and fixes the resulting link failure by autoconf-detecting msync() and compiling mmap.flush()’s Unix implementation only when msync is available.

Changes:

  • Remove mmap from PY_STDLIB_MOD_SET_NA for Nanvix so it can be built when headers/requirements are present.
  • Add msync to AC_CHECK_FUNCS and introduce HAVE_MSYNC to the config header template.
  • Gate the Unix mmap.flush() implementation behind HAVE_MSYNC, falling back to the existing “flush not supported” behavior when absent.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
pyconfig.h.in Adds HAVE_MSYNC placeholder so autoconf can define it when detected.
configure.ac Checks for msync() and stops forcing mmap to n/a on Nanvix.
configure Mirrors configure.ac updates: emits HAVE_MSYNC and removes Nanvix mmap n/a assignment.
Modules/mmapmodule.c Compiles the Unix msync()-based flush() path only when HAVE_MSYNC is defined.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Empirical enablement per
.vault/designs/stdlib-modules-enablement/design.md.
Drop [mmap] from the Nanvix arm of PY_STDLIB_MOD_SET_NA in
configure.ac and the matching py_cv_module_mmap=n/a in configure.

Closes #301 (pending verification).
Cycle 2 of the empirical enablement of issue #301. Cycle 1 (commit
8dd17c2fde3) flipped the configure switch and confirmed that
Modules/mmapmodule.c:683 calls msync() unconditionally inside
mmap.flush(), and that nanvix's libposix.a does not export msync
(see .vault/designs/stdlib-modules-enablement/findings.md).

This commit:
- adds msync to the AC_CHECK_FUNCS list in configure.ac (alongside
  the existing mmap/mremap entries) and mirrors the change in the
  generated configure script (no autoreconf, per port convention).
- adds HAVE_MSYNC to pyconfig.h.in so the autoconf substitution
  has a target.
- guards the UNIX msync() branch in mmap_flush_method() with
  defined(HAVE_MSYNC); platforms without msync fall through to the
  existing 'flush not supported on this system' ValueError path.

On any platform that has msync (every existing in-tree target),
HAVE_MSYNC will be defined and behaviour is unchanged. On nanvix,
mmap.flush() will raise ValueError instead of failing to link.
@ppenna ppenna self-assigned this Apr 29, 2026
Comment thread configure Outdated
py_cv_module__testmultiphase=n/a
py_cv_module__testsinglephase=n/a
py_cv_module__testclinic=n/a
py_cv_module_=n/a
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why is this required?

Comment thread configure
@ppenna ppenna merged commit 535a4c4 into nanvix/v3.12.3 Apr 29, 2026
12 checks passed
@ppenna ppenna deleted the feat/301-mmap branch April 29, 2026 20:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enable mmap stdlib module (memory-mapped file I/O)

3 participants