[Git][ghc/ghc][wip/freebsd] rts/linker: Fix mapping wrap-around logic

Ben Gamari (@bgamari) gitlab at gitlab.haskell.org
Sun Nov 17 21:29:59 UTC 2024



Ben Gamari pushed to branch wip/freebsd at Glasgow Haskell Compiler / GHC


Commits:
fd3686e4 by Ben Gamari at 2024-11-17T16:29:47-05:00
rts/linker: Fix mapping wrap-around logic

Previously the structure of `mmapInRegion` concealed a subtle bug
concerning handling of `mmap` returning mappings below the beginning of
the desired region. Specifically, we would reset `p = result + bytes`
and then again reset `p = region->start` before looping around for
another iteration. This resulted in an infinite loop on FreeBSD.

Fixes #25492.

- - - - -


1 changed file:

- rts/linker/MMap.c


Changes:

=====================================
rts/linker/MMap.c
=====================================
@@ -345,12 +345,7 @@ mmapInRegion (
         if (result == NULL) {
             // The mapping failed
             return NULL;
-        } else if (result < region->start) {
-            // Uh oh, we assume that mmap() will only give us a
-            // an address at or after the requested address.
-            // Try again.
-            p = (uint8_t *) result + bytes;
-        } else if (result < region->end) {
+        } else if (result >= region->start && result < region->end) {
             // Success!
             region->last = (uint8_t *) result + bytes;
             return result;
@@ -358,17 +353,23 @@ mmapInRegion (
             // We failed to find a suitable mapping
             munmap(result, bytes);
             reportMemoryMap();
-            errorBelch("mmapForLinker: failed to mmap() memory below 2Gb; "
+            errorBelch("mmapForLinker: failed to mmap() memory between %p and %p; "
                        "asked for %zu bytes at %p. "
                        "Try specifying an address with +RTS -xm<addr> -RTS",
-                       bytes, p);
+                       region->start, region->end, bytes, p);
             return NULL;
-        }
+        } else if (result < region->start) {
+            // Uh oh, we assume that mmap() will only give us a
+            // an address at or after the requested address.
+            // Try again.
+            p = (uint8_t *) result + bytes;
+        } else if (result >= region->end) {
+            // mmap() gave us too high an address; wrap around and try again
+            wrapped = true;
+            p = region->start;
+       }
 
-        // mmap() gave us too high an address; wrap around and try again
         munmap(result, bytes);
-        wrapped = true;
-        p = region->start;
     }
 }
 



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/fd3686e410fdd66d37bc797b0dc157304af6faab

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/fd3686e410fdd66d37bc797b0dc157304af6faab
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20241117/e154c0ea/attachment-0001.html>


More information about the ghc-commits mailing list