Skip to content

1.8.x: track mmap page offset without over-allocating apr_mmap_t; don't allocate on failure - #78

Open
notroj wants to merge 2 commits into
apache:1.8.xfrom
notroj:1.8.x-mmap-offset
Open

notroj wants to merge 2 commits into
apache:1.8.xfrom
notroj:1.8.x-mmap-offset

Conversation

@notroj

@notroj notroj commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Two changes to apr_mmap_create() on 1.8.x.

Track the page offset without over-allocating apr_mmap_t

Since r1887497 (PR 65158), apr_mmap_create() on Unix maps from the start of the page containing the requested offset. Because apr_mmap_t is a complete public type, the offset within the page was stored after the end of the struct, in a private struct mm_layout wrapper.

For an apr_mmap_t which APR did not allocate, apr_mmap_dup() and apr_mmap_delete() then read beyond the end of the struct and pass the result to munmap(). That change was reverted from 1.7.x in r1902372 after an objection on dev@ to altering the size of the structure: https://lists.apache.org/thread/jt7by9hhvsnpo5ztjmbq5rrpnq2n0b8v

The start of a mapping is always page-aligned, so the offset can be derived from mm->mm when the region is unmapped. This removes struct mm_layout and its helpers:

  • apr_mmap_t is allocated and copied at its public size again.
  • The mmap() and munmap() calls made are unchanged.
  • No 1.8.x release has shipped with the mm_layout form.

The same form would be suitable for 1.7.x, since it changes neither the struct nor its allocation.

Merge r1940313 from trunk

Allocate the apr_mmap_t only once the file has been mapped, so a failed call does not consume memory from the pool. Partial fix for httpd PR 70167. The source changes are adapted by hand: 1.8.x differs from trunk in the BeOS path and in the Windows struct members.

Tests

  • test_mmap_dup (new): dup into another pool, delete the original, read through the dup, delete the dup; run at each test offset including the unaligned one.
  • test_mmap_create_fail (from trunk): a failed apr_mmap_create() does not allocate from the pool. Only effective in a pool-debug build.

On Linux x86_64 the full test suite passes with the first commit, and testmmap passes with both, in normal and pool-debug builds. An unmodified APR-util 1.6.x run against this library reads a FILE bucket at an unaligned offset as MMAP buckets.

The Windows and BeOS changes are not compiled or tested.

🤖 Generated with Claude Code

notroj and others added 2 commits November 15, 2026 18:07
apr_mmap_create() maps from the start of the page containing the
requested offset, since r1887497 (with follow-up r1887508), which this
branch inherited from 1.7.x.  That change stored the offset of the
data within the page after the end of the apr_mmap_t, in a private
structure wrapping it.  For an apr_mmap_t which was not allocated by
APR, apr_mmap_dup() and apr_mmap_delete() would then read beyond the
end of the structure and pass the result to munmap().

r1887497 and r1887508 were reverted from 1.7.x in r1902372, after an
objection on dev@ to altering the size of the structure:

  https://lists.apache.org/thread/jt7by9hhvsnpo5ztjmbq5rrpnq2n0b8v

The start of a mapping is always page-aligned, so the offset can
instead be derived from the ->mm pointer when the region is unmapped,
with no change to the size or allocation of apr_mmap_t.  The mmap()
and munmap() calls made are unchanged.

* mmap/unix/mmap.c (struct mm_layout, alloc_with_poffset, set_poffset,
  get_poffset): Remove.
  (page_offset): New function.
  (mmap_cleanup): Use it to derive the start of the mapping.
  (apr_mmap_create): Use it, and allocate only an apr_mmap_t.
  (apr_mmap_dup): Copy only an apr_mmap_t.

* test/testmmap.c (test_mmap_dup): New test.
  (testmmap): Run it.

PR: 65158
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Avoid allocating from the pool when apr_mmap_create() fails.

A caller which repeatedly fails to map a file, such as a FILE bucket
being read in chunks, otherwise consumes memory from the pool for each
attempt, for the lifetime of that pool.

This is a partial fix for httpd PR 70167: it removes the memory growth
per failed call, but does not stop the FILE bucket retrying a failed
mmap for each read.

* mmap/unix/mmap.c, mmap/win32/mmap.c (apr_mmap_create): Allocate the
  apr_mmap_t only once the file has been mapped.

* test/testmmap.c (test_mmap_create_fail): New test.
  (testmmap): Run it.

PR: 70167
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.

1 participant