[PATCH v3] support: Align the repeated blob mappings to SHMLBA

Stian Halseth stian@itx.no
Tue Nov 3 12:00:52 GMT 2026


allocate_big reserves the region with a private anonymous mapping,
which is only page aligned, and then maps the backing file over it
with MAP_FIXED | MAP_SHARED.  A shared mapping has to be aligned to
SHMLBA, which is 16 KiB on sparc64 against an 8 KiB page, so the
mapping fails with EINVAL whenever the reservation lands on an odd
page.  stdlib/test-bz22786 then crashes on the NULL start, and
stdlib/tst-strtod-overflow and string/tst-memmove-overflow report
UNSUPPORTED.

Reserve the region with MAP_SHARED, which the kernel aligns to SHMLBA,
and compute the stride from SHMLBA when it exceeds the page size, so
that every shared alias is aligned too.

Tested on sparc64 and x86_64.

Signed-off-by: Stian Halseth <stian@itx.no>
---
v3: Compute the stride from SHMLBA directly (Andreas).
v2: Reserve with MAP_SHARED instead of over-allocating (Florian).

 support/blob_repeat.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/support/blob_repeat.c b/support/blob_repeat.c
index 6f3fe64cec..0cacb4caf6 100644
--- a/support/blob_repeat.c
+++ b/support/blob_repeat.c
@@ -27,6 +27,8 @@
 #include <support/support.h>
 #include <support/xunistd.h>
 #include <sys/mman.h>
+#include <sys/param.h>
+#include <sys/shm.h>
 #include <unistd.h>
 #include <wchar.h>
 
@@ -132,7 +134,9 @@ allocate_big (size_t total_size, const void *element, size_t element_size,
               size_t count, bool shared)
 {
   unsigned long page_size = xsysconf (_SC_PAGESIZE);
-  size_t stride_size = minimum_stride_size (page_size, element_size);
+  /* The file mappings below are shared and must be aligned to SHMLBA.  */
+  size_t stride_size = minimum_stride_size (MAX (page_size, SHMLBA),
+                                            element_size);
   if (stride_size == 0)
     {
       errno = EOVERFLOW;
@@ -150,9 +154,10 @@ allocate_big (size_t total_size, const void *element, size_t element_size,
     return allocate_malloc (total_size, element, element_size, count);
 
   /* Reserve the memory region.  If we cannot create the mapping,
-     there is no reason to set up the backing file.  */
+     there is no reason to set up the backing file.  MAP_SHARED makes
+     the kernel align the reservation to SHMLBA.  */
   void *target = mmap (NULL, total_size, PROT_NONE,
-                       MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+                       MAP_ANONYMOUS | MAP_SHARED | MAP_NORESERVE, -1, 0);
   if (target == MAP_FAILED)
     return (struct support_blob_repeat) {};
 
-- 
2.43.0



More information about the Libc-alpha mailing list