[Git][ghc/ghc][wip/freebsd] rts/linker: Fix out-of-bounds mapping logic

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



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


Commits:
e7306479 by Ben Gamari at 2024-11-17T16:30:18-05:00
rts/linker: Fix out-of-bounds mapping 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/e7306479bfd098f454d6379e0c707f869d943931

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/e7306479bfd098f454d6379e0c707f869d943931
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/6fa8b24b/attachment-0001.html>


More information about the ghc-commits mailing list