See attached patch. When the shared memory segment cannot be mapped,
for example because GFORTRAN_SHARED_MEMORY_SIZE does not fit under the
address-space limit, a caf_shmem program dies with a segmentation fault
and gives no hint of the cause.  shared_memory_init tested the mmap
result for NULL, which mmap does not return on failure.  The patch
tests for MAP_FAILED first, so the supervisor and the images report the
mmap error and exit with status 1.

The change is one hunk in shared_memory_init in
libgfortran/caf/shmem/shared_memory.c.  It also drops the old NULL
test, which a failed mmap never triggered.

There is no new test: caf.exp runs every test under each variant, and
the failure needs a resource limit.  To reproduce:

  program t
    integer :: x[*]
    x = this_image ()
    sync all
    if (this_image () == 1) print *, 'images:', num_images ()
  end program

  $ gfortran -fcoarray=lib t.f90 -lcaf_shmem -o t
  $ (ulimit -v 8000000; GFORTRAN_NUM_IMAGES=4 \
     GFORTRAN_SHARED_MEMORY_SIZE=64G ./t; echo $?)

Without the patch this ends in a segmentation fault (exit status 139).
With it the program prints "mmap failed: Cannot allocate memory" and
exits with status 1.

Regression tested on x86_64.

I plan to commit as simple if not obvious.

Regards,

Jerry

---

libgfortran: caf_shmem: check mmap against MAP_FAILED

shared_memory_init compared the result of mmap with NULL, but mmap
reports failure with MAP_FAILED.  A failed mmap in the supervisor went
unnoticed until the first access to the shared memory segfaulted.  In an
image it looked like a mapping at the wrong address, so the supervisor
restarted the image until it gave up with "no common state on shared
memory could be reached".  Check for MAP_FAILED before comparing the
address, and exit with a message.

Assisted-by: Claude Opus 5.5

libgfortran/ChangeLog:

        * caf/shmem/shared_memory.c (shared_memory_init): Check the result
        of mmap against MAP_FAILED before comparing it with the requested
        address.
>From b1f1a62d2e6ba98ab247bf55e072dc8b4e1c9da1 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Tue, 17 Nov 2026 17:09:34 -0800
Subject: [PATCH] libgfortran: caf_shmem: check mmap against MAP_FAILED

shared_memory_init compared the result of mmap with NULL, but mmap
reports failure with MAP_FAILED.  A failed mmap in the supervisor went
unnoticed until the first access to the shared memory segfaulted.  In an
image it looked like a mapping at the wrong address, so the supervisor
restarted the image until it gave up with "no common state on shared
memory could be reached".  Check for MAP_FAILED before comparing the
address, and exit with a message.

Assisted-by: Claude Opus 5.5

libgfortran/ChangeLog:

	* caf/shmem/shared_memory.c (shared_memory_init): Check the result
	of mmap against MAP_FAILED before comparing it with the requested
	address.
---
 libgfortran/caf/shmem/shared_memory.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/libgfortran/caf/shmem/shared_memory.c b/libgfortran/caf/shmem/shared_memory.c
index 281336c80bc..dc78f66e820 100644
--- a/libgfortran/caf/shmem/shared_memory.c
+++ b/libgfortran/caf/shmem/shared_memory.c
@@ -256,6 +256,11 @@ shared_memory_init (shared_memory_act *mem, size_t size)
 #ifdef HAVE_MMAP
   mem->glbl.base
     = mmap (base_ptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, mem->shm_fd, 0);
+  if (mem->glbl.base == MAP_FAILED)
+    {
+      perror ("mmap failed");
+      exit (1);
+    }
   if (base_ptr && mem->glbl.base != base_ptr)
     {
       /* The supervisor will start us again.  */
@@ -263,11 +268,6 @@ shared_memory_init (shared_memory_act *mem, size_t size)
       free (local);
       exit (210);
     }
-  else if (!base_ptr && !mem->glbl.base)
-    {
-      perror ("mmap failed");
-      exit (1);
-    }
 #ifdef MADV_DONTDUMP
   /* Keep the unlinked object, which counts as anonymous shared memory, out
      of core dumps.  */
-- 
2.56.0

Reply via email to