From 145fb8f6cd3b8112eb84128cda804c4451d8463c Mon Sep 17 00:00:00 2001 From: Joe Orton Date: Sun, 15 Nov 2026 18:03:39 +0000 Subject: [PATCH 1/2] Track the page offset of an mmap without over-allocating apr_mmap_t. 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 --- mmap/unix/mmap.c | 66 +++++++++++++++++------------------------------- test/testmmap.c | 34 +++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 43 deletions(-) diff --git a/mmap/unix/mmap.c b/mmap/unix/mmap.c index 95afb0754ce..33bd4f09802 100644 --- a/mmap/unix/mmap.c +++ b/mmap/unix/mmap.c @@ -48,30 +48,23 @@ #if APR_HAS_MMAP || defined(BEOS) #ifndef BEOS -struct mm_layout { - apr_mmap_t mm; - apr_off_t poffset; -}; - -static APR_INLINE -apr_mmap_t *alloc_with_poffset(apr_pool_t *p) +/* Returns the offset of ADDR from the start of the page containing + * it. The start of a mapping is always page-aligned, so for the + * ->mm pointer of an apr_mmap_t this is the offset of the data from + * the start of the underlying mapping; no state beyond apr_mmap_t + * (the size of which cannot change) is needed to track it. */ +static apr_size_t page_offset(apr_uintptr_t addr) { - struct mm_layout *layout = apr_pcalloc(p, sizeof(*layout)); - return &layout->mm; -} +#if defined(_SC_PAGESIZE) + long psize = sysconf(_SC_PAGESIZE); -static APR_INLINE -void set_poffset(apr_mmap_t *mm, apr_off_t poffset) -{ - struct mm_layout *layout = (struct mm_layout *)mm; - layout->poffset = poffset; -} + /* the page size should be a power of two */ + assert(psize > 0 && (psize & (psize - 1)) == 0); -static APR_INLINE -apr_off_t get_poffset(apr_mmap_t *mm) -{ - struct mm_layout *layout = (struct mm_layout *)mm; - return layout->poffset; + return (apr_size_t)(addr & (apr_uintptr_t)(psize - 1)); +#else + return 0; +#endif } #endif @@ -94,7 +87,11 @@ static apr_status_t mmap_cleanup(void *themmap) #ifdef BEOS rv = delete_area(mm->area); #else - rv = munmap((char *)mm->mm - get_poffset(mm), mm->size + get_poffset(mm)); + { + apr_size_t poffset = page_offset((apr_uintptr_t)mm->mm); + + rv = munmap((char *)mm->mm - poffset, mm->size + poffset); + } #endif mm->mm = (void *)-1; @@ -114,8 +111,7 @@ APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new, area_id aid = -1; uint32 pages = 0; #else - static long psize; - apr_off_t poffset = 0; + apr_size_t poffset; apr_int32_t native_flags = 0; #endif @@ -132,11 +128,9 @@ APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new, if (file == NULL || file->filedes == -1 || file->buffered) return APR_EBADF; + (*new) = (apr_mmap_t *)apr_pcalloc(cont, sizeof(apr_mmap_t)); #ifdef BEOS - - *new = (apr_mmap_t *)apr_pcalloc(cont, sizeof(apr_mmap_t)); - /* XXX: mmap shouldn't really change the seek offset */ apr_file_seek(file, APR_SET, &offset); @@ -158,9 +152,7 @@ APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new, read(file->filedes, mm, size); (*new)->area = aid; - #else - *new = alloc_with_poffset(cont); if (flag & APR_MMAP_WRITE) { native_flags |= PROT_WRITE; @@ -169,15 +161,8 @@ APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new, native_flags |= PROT_READ; } -#if defined(_SC_PAGESIZE) - if (psize == 0) { - psize = sysconf(_SC_PAGESIZE); - /* the page size should be a power of two */ - assert(psize > 0 && (psize & (psize - 1)) == 0); - } - poffset = offset & (apr_off_t)(psize - 1); - set_poffset(*new, poffset); -#endif + /* Map from the start of the page containing the offset. */ + poffset = page_offset((apr_uintptr_t)offset); mm = mmap(NULL, size + poffset, native_flags, MAP_SHARED, @@ -207,12 +192,7 @@ APR_DECLARE(apr_status_t) apr_mmap_dup(apr_mmap_t **new_mmap, apr_mmap_t *old_mmap, apr_pool_t *p) { -#ifdef BEOS *new_mmap = (apr_mmap_t *)apr_pmemdup(p, old_mmap, sizeof(apr_mmap_t)); -#else - *new_mmap = (apr_mmap_t *)apr_pmemdup(p, old_mmap, - sizeof(struct mm_layout)); -#endif (*new_mmap)->cntxt = p; APR_RING_INSERT_AFTER(old_mmap, *new_mmap, link); diff --git a/test/testmmap.c b/test/testmmap.c index bebeceed946..2262d71d85e 100644 --- a/test/testmmap.c +++ b/test/testmmap.c @@ -213,6 +213,39 @@ static void test_mmap_contents(abts_case *tc, void *data) } +static void test_mmap_dup(abts_case *tc, void *data) +{ + apr_off_t *offset = data; + apr_pool_t *subp; + apr_mmap_t *dup; + apr_status_t rv; + + if (!themmap) { + ABTS_SKIP(tc, data, "MMap not open, skipping mmap dup."); + return; + } + + apr_pool_create(&subp, ptest); + rv = apr_mmap_dup(&dup, themmap, subp); + ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); + + /* Deleting the original leaves the region mapped via the dup. */ + rv = apr_mmap_delete(themmap); + ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); + themmap = NULL; + ABTS_STR_NEQUAL(tc, dup->mm, thisfdata, thisfsize); + + /* Deleting the last reference must unmap the region. */ + rv = apr_mmap_delete(dup); + ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); + apr_pool_destroy(subp); + + /* Map the file again for the remaining tests. */ + rv = apr_mmap_create(&themmap, thefile, *offset, thisfsize, + APR_MMAP_READ, ptest); + ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); +} + static void test_mmap_delete(abts_case *tc, void *data) { apr_status_t rv; @@ -276,6 +309,7 @@ abts_suite *testmmap(abts_suite *suite) abts_run_test(suite, test_mmap_create, &test_set[i].offset); abts_run_test(suite, test_mmap_contents, &test_set[i].offset); abts_run_test(suite, test_mmap_offset, &test_set[i].offset); + abts_run_test(suite, test_mmap_dup, &test_set[i].offset); abts_run_test(suite, test_mmap_delete, NULL); abts_run_test(suite, test_file_close, NULL); apr_pool_clear(ptest); From 1397f10c0da97094b69dd0045ed2e967e1ef4bf3 Mon Sep 17 00:00:00 2001 From: Joe Orton Date: Sun, 15 Nov 2026 18:13:44 +0000 Subject: [PATCH 2/2] Merge r1940313 from trunk: 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 --- mmap/unix/mmap.c | 9 ++++++--- mmap/win32/mmap.c | 43 ++++++++++++++++++++++++++++--------------- test/testmmap.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 18 deletions(-) diff --git a/mmap/unix/mmap.c b/mmap/unix/mmap.c index 33bd4f09802..2857e34c37e 100644 --- a/mmap/unix/mmap.c +++ b/mmap/unix/mmap.c @@ -128,7 +128,6 @@ APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new, if (file == NULL || file->filedes == -1 || file->buffered) return APR_EBADF; - (*new) = (apr_mmap_t *)apr_pcalloc(cont, sizeof(apr_mmap_t)); #ifdef BEOS /* XXX: mmap shouldn't really change the seek offset */ @@ -150,8 +149,6 @@ APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new, if (aid >= B_NO_ERROR) read(file->filedes, mm, size); - - (*new)->area = aid; #else if (flag & APR_MMAP_WRITE) { @@ -177,6 +174,12 @@ APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new, mm = (char *)mm + poffset; #endif + /* Allocate only on success, so that a caller which repeatedly + * fails does not consume memory from the pool. */ + (*new) = (apr_mmap_t *)apr_pcalloc(cont, sizeof(apr_mmap_t)); +#ifdef BEOS + (*new)->area = aid; +#endif (*new)->mm = mm; (*new)->size = size; (*new)->cntxt = cont; diff --git a/mmap/win32/mmap.c b/mmap/win32/mmap.c index 8fbb2f25bb0..7a32ddf1a81 100644 --- a/mmap/win32/mmap.c +++ b/mmap/win32/mmap.c @@ -74,6 +74,12 @@ APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new, apr_file_t *file, DWORD mvaccess = 0; DWORD offlo; DWORD offhi; + /** The physical start, size and offset */ + apr_off_t pstart; + apr_size_t psize; + apr_off_t poffset; + HANDLE mhandle = NULL; + void *mv; if (size == 0) return APR_EINVAL; @@ -99,10 +105,9 @@ APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new, apr_file_t *file, memblock = si.dwAllocationGranularity; } - *new = apr_pcalloc(cont, sizeof(apr_mmap_t)); - (*new)->pstart = (offset / memblock) * memblock; - (*new)->poffset = offset - (*new)->pstart; - (*new)->psize = (apr_size_t)((*new)->poffset) + size; + pstart = (offset / memblock) * memblock; + poffset = offset - pstart; + psize = (apr_size_t)poffset + size; /* The size of the CreateFileMapping object is the current size * of the size of the mmap object (e.g. file size), not the size * of the mapped region! @@ -111,35 +116,43 @@ APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new, apr_file_t *file, #if APR_HAS_UNICODE_FS IF_WIN_OS_IS_UNICODE { - (*new)->mhandle = CreateFileMappingW(file->filehand, NULL, fmaccess, - 0, 0, NULL); + mhandle = CreateFileMappingW(file->filehand, NULL, fmaccess, + 0, 0, NULL); } #endif #if APR_HAS_ANSI_FS ELSE_WIN_OS_IS_ANSI { - (*new)->mhandle = CreateFileMappingA(file->filehand, NULL, fmaccess, - 0, 0, NULL); + mhandle = CreateFileMappingA(file->filehand, NULL, fmaccess, + 0, 0, NULL); } #endif - if (!(*new)->mhandle || (*new)->mhandle == INVALID_HANDLE_VALUE) + if (!mhandle || mhandle == INVALID_HANDLE_VALUE) { *new = NULL; return apr_get_os_error(); } - offlo = (DWORD)(*new)->pstart; - offhi = (DWORD)((*new)->pstart >> 32); - (*new)->mv = MapViewOfFile((*new)->mhandle, mvaccess, offhi, - offlo, (*new)->psize); - if (!(*new)->mv) + offlo = (DWORD)pstart; + offhi = (DWORD)(pstart >> 32); + mv = MapViewOfFile(mhandle, mvaccess, offhi, offlo, psize); + if (!mv) { apr_status_t rv = apr_get_os_error(); - CloseHandle((*new)->mhandle); + CloseHandle(mhandle); *new = NULL; return rv; } + /* Allocate only on success, so that a caller which repeatedly + * fails does not consume memory from the pool. */ + *new = apr_pcalloc(cont, sizeof(apr_mmap_t)); + (*new)->pstart = pstart; + (*new)->poffset = poffset; + (*new)->psize = psize; + (*new)->mhandle = mhandle; + (*new)->mv = mv; + (*new)->mm = (char*)((*new)->mv) + (*new)->poffset; (*new)->size = size; (*new)->cntxt = cont; diff --git a/test/testmmap.c b/test/testmmap.c index 2262d71d85e..e4879172a4c 100644 --- a/test/testmmap.c +++ b/test/testmmap.c @@ -292,6 +292,50 @@ static void test_mmap_offset(abts_case *tc, void *data) } +/* A failed apr_mmap_create() must not consume memory from the pool; + * callers such as the FILE bucket may retry many times within the + * lifetime of one pool. */ +static void test_mmap_create_fail(abts_case *tc, void *data) +{ + static const char fname[] = "data/mmap_wronly.txt"; + apr_pool_t *subp; + apr_file_t *f; + apr_mmap_t *mm = (void *)tc; /* any non-NULL value */ + apr_status_t rv; + apr_size_t before; + + apr_pool_create(&subp, ptest); + + /* A file which is open only for writing cannot be mapped for + * reading. */ + rv = apr_file_open(&f, fname, + APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_TRUNCATE, + APR_FPROT_OS_DEFAULT, subp); + APR_ASSERT_SUCCESS(tc, "open file for writing", rv); + if (rv != APR_SUCCESS) { + return; + } + APR_ASSERT_SUCCESS(tc, "write to file", apr_file_puts("hello, world", f)); + + /* apr_pool_num_bytes() always returns zero unless built with + * pool debugging, so this is only checked in such builds. */ + before = apr_pool_num_bytes(subp, 0); + + rv = apr_mmap_create(&mm, f, 0, 5, APR_MMAP_READ, subp); + ABTS_TRUE(tc, rv != APR_SUCCESS); + ABTS_PTR_EQUAL(tc, NULL, mm); + + ABTS_ASSERT(tc, "failed apr_mmap_create() allocated from the pool", + apr_pool_num_bytes(subp, 0) == before); + + if (mm) { + apr_mmap_delete(mm); + } + apr_file_close(f); + apr_file_remove(fname, subp); + apr_pool_destroy(subp); +} + #endif abts_suite *testmmap(abts_suite *suite) @@ -314,6 +358,7 @@ abts_suite *testmmap(abts_suite *suite) abts_run_test(suite, test_file_close, NULL); apr_pool_clear(ptest); } + abts_run_test(suite, test_mmap_create_fail, NULL); apr_pool_destroy(ptest); #else abts_run_test(suite, not_implemented, NULL);