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

Ben Gamari (@bgamari) gitlab at gitlab.haskell.org
Thu Nov 21 19:09:55 UTC 2024



Ben Gamari pushed to branch wip/linker-fixes at Glasgow Haskell Compiler / GHC


Commits:
c0ab3ac7 by Ben Gamari at 2024-11-21T14:09:45-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.

- - - - -
2ecfccff by Ben Gamari at 2024-11-21T14:09:45-05:00
rts/linker: Clarify debug output

- - - - -


2 changed files:

- rts/Linker.c
- rts/linker/MMap.c


Changes:

=====================================
rts/Linker.c
=====================================
@@ -1118,7 +1118,7 @@ freePreloadObjectFile (ObjectCode *oc)
  */
 void freeObjectCode (ObjectCode *oc)
 {
-    IF_DEBUG(linker, ocDebugBelch(oc, "start\n"));
+    IF_DEBUG(linker, ocDebugBelch(oc, "freeObjectCode: start\n"));
 
     // Run finalizers
     if (oc->type == STATIC_OBJECT &&


=====================================
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 bump forward by a bit and try again.
+            p = (uint8_t *) p + 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/-/compare/acb1982db98821ef54022cc140ca9f4b3ef1bf66...2ecfccffcd39fc724997bebb45c4bab86c28790a

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/acb1982db98821ef54022cc140ca9f4b3ef1bf66...2ecfccffcd39fc724997bebb45c4bab86c28790a
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/20241121/17ce2f3b/attachment-0001.html>


More information about the ghc-commits mailing list