[Git][ghc/ghc][wip/T24512] rts/linker: Don't unload native objects when dlinfo isn't available

Ben Gamari (@bgamari) gitlab at gitlab.haskell.org
Sat Mar 9 16:43:09 UTC 2024



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


Commits:
7eeef6b8 by Ben Gamari at 2024-03-09T11:42:25-05:00
rts/linker: Don't unload native objects when dlinfo isn't available

To do so is unsafe as we have no way of identifying references to
symbols provided by the object.

Fixes #24513. Fixes #23993.

- - - - -


5 changed files:

- rts/CheckUnload.c
- rts/Linker.c
- rts/LinkerInternals.h
- rts/linker/Elf.c
- testsuite/tests/rts/linker/unload_multiple_objs/linker_unload_multiple_objs.c


Changes:

=====================================
rts/CheckUnload.c
=====================================
@@ -492,8 +492,6 @@ void checkUnload(void)
         next = oc->next;
         ASSERT(oc->status == OBJECT_UNLOADED);
 
-        removeOCSectionIndices(s_indices, oc);
-
         // Symbols should be removed by unloadObj_.
         // NB (osa): If this assertion doesn't hold then freeObjectCode below
         // will corrupt symhash as keys of that table live in ObjectCodes. If
@@ -501,8 +499,17 @@ void checkUnload(void)
         // RTS) then it's probably because this assertion did not hold.
         ASSERT(oc->symbols == NULL);
 
-        freeObjectCode(oc);
-        n_unloaded_objects -= 1;
+        if (oc->unloadable) {
+            removeOCSectionIndices(s_indices, oc);
+            freeObjectCode(oc);
+            n_unloaded_objects -= 1;
+        } else {
+            // If we don't have enough information to
+            // accurately determine the reachability of
+            // the object then hold onto it.
+            oc->next = objects;
+            objects = oc;
+        }
     }
 
     old_objects = NULL;


=====================================
rts/Linker.c
=====================================
@@ -1385,6 +1385,8 @@ mkOc( ObjectType type, pathchar *path, char *image, int imageSize,
    oc->prev              = NULL;
    oc->next_loaded_object = NULL;
    oc->mark              = object_code_mark_bit;
+   /* this will get cleared by the caller if object is not safely unloadable */
+   oc->unloadable        = true;
    oc->dependencies      = allocHashSet();
 
 #if defined(NEED_M32)


=====================================
rts/LinkerInternals.h
=====================================
@@ -313,8 +313,14 @@ struct _ObjectCode {
     struct _ObjectCode *next_loaded_object;
 
     // Mark bit
+    // N.B. This is a full word as we CAS it.
     StgWord mark;
 
+    // Can this object be safely unloaded? Not true for
+    // dynamic objects when dlinfo is not available as
+    // we cannot determine liveness.
+    bool unloadable;
+
     // Set of dependencies (ObjectCode*) of the object file. Traverse
     // dependencies using `iterHashTable`.
     //
@@ -376,7 +382,9 @@ struct _ObjectCode {
     /* handle returned from dlopen */
     void *dlopen_handle;
 
-    /* virtual memory ranges of loaded code */
+    /* virtual memory ranges of loaded code. NULL if no range information is
+     * available (e.g. if dlinfo is unavailable on the current platform).
+     */
     NativeCodeRange *nc_ranges;
 };
 


=====================================
rts/linker/Elf.c
=====================================
@@ -2190,6 +2190,10 @@ void * loadNativeObj_ELF (pathchar *path, char **errmsg)
      copyErrmsg(errmsg, "dl_iterate_phdr failed to find obj");
      goto dl_iterate_phdr_fail;
    }
+   nc->unloadable = true;
+#else
+   nc->nc_ranges = NULL;
+   nc->unloadable = false;
 #endif /* defined (HAVE_DLINFO) */
 
    insertOCSectionIndices(nc);


=====================================
testsuite/tests/rts/linker/unload_multiple_objs/linker_unload_multiple_objs.c
=====================================
@@ -64,7 +64,7 @@ void check_object_freed(char *obj_path) {
     OStatus st;
     st = getObjectLoadStatus(toPathchar(obj_path));
     if (st != OBJECT_NOT_LOADED) {
-        errorBelch("object %s status != OBJECT_NOT_LOADED", obj_path);
+        errorBelch("object %s status != OBJECT_NOT_LOADED, is %d instead", obj_path, st);
         exit(1);
     }
 }



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

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/7eeef6b8d719fe8886cc521003a8d6fa379ee32c
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/20240309/4f24b77e/attachment-0001.html>


More information about the ghc-commits mailing list