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

Stian Halseth stian@itx.no
Tue Nov 3 08:51:07 GMT 2026


allocate_big reserves the region with an 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.

Over-allocate the reservation and align its start, and make the stride
a multiple of SHMLBA so that every shared alias is aligned too.

Tested on sparc64 and x86_64.

Signed-off-by: Stian Halseth <stian@itx.no>
---
 support/blob_repeat.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/support/blob_repeat.c b/support/blob_repeat.c
index 6f3fe64cec..317098a065 100644
--- a/support/blob_repeat.c
+++ b/support/blob_repeat.c
@@ -18,6 +18,7 @@
 
 #include <errno.h>
 #include <fcntl.h>
+#include <libc-pointer-arith.h>
 #include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
@@ -27,6 +28,7 @@
 #include <support/support.h>
 #include <support/xunistd.h>
 #include <sys/mman.h>
+#include <sys/shm.h>
 #include <unistd.h>
 #include <wchar.h>
 
@@ -145,16 +147,27 @@ allocate_big (size_t total_size, const void *element, size_t element_size,
     stride_size
       = ((maximum_small_size + stride_size - 1) / stride_size) * stride_size;
 
+  /* The file mappings below are shared and must be aligned to SHMLBA.  */
+  while (stride_size % SHMLBA != 0)
+    stride_size += minimum_stride_size (page_size, element_size);
+
   if (stride_size > total_size)
     /* The mmap optimization would not save anything.  */
     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.  */
-  void *target = mmap (NULL, total_size, PROT_NONE,
-                       MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
-  if (target == MAP_FAILED)
+  size_t reserve_size = total_size + SHMLBA;
+  char *reserve = mmap (NULL, reserve_size, PROT_NONE,
+                        MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+  if (reserve == MAP_FAILED)
     return (struct support_blob_repeat) {};
+  char *target = PTR_ALIGN_UP (reserve, SHMLBA);
+  char *end = target + ALIGN_UP (total_size, page_size);
+  if (target > reserve)
+    xmunmap (reserve, target - reserve);
+  if (end < reserve + reserve_size)
+    xmunmap (end, reserve + reserve_size - end);
 
   /* Create the backing file for the repeated mapping.  Call mkstemp
      directly to remove the resources backing the temporary file
-- 
2.43.0



More information about the Libc-alpha mailing list