[Git][ghc/ghc][wip/unloading] Add loadNativeObj and unloadNativeObj
Ben Gamari
gitlab at gitlab.haskell.org
Sun Aug 9 20:01:32 UTC 2020
Ben Gamari pushed to branch wip/unloading at Glasgow Haskell Compiler / GHC
Commits:
2cfcbcfa by Ray Shih at 2020-08-09T16:01:21-04:00
Add loadNativeObj and unloadNativeObj
Summary:
(This change is originally written by niteria)
This adds two functions:
* `loadNativeObj`
* `unloadNativeObj`
and implements them for Linux.
They are useful if you want to load a shared object with Haskell code
using the system linker and have GHC call dlclose() after the
code is no longer referenced from the heap.
Using the system linker allows you to load the shared object
above outside the lowmem region. It also loads the DWARF sections
in a way that `perf` understands.
`dl_iterate_phdr` is what makes this implementation Linux specific.
NOTE: this should be replaceable by whatever D4263 becomes
Test Plan: manual testing
Reviewers: simonmar, Phyx, bgamari, erikd
Reviewed By: niteria
Subscribers: rwbarton, thomie, carter
Differential Revision: https://phabricator.haskell.org/D4263
- - - - -
11 changed files:
- includes/rts/Linker.h
- includes/rts/storage/GC.h
- rts/CheckUnload.c
- rts/Linker.c
- rts/LinkerInternals.h
- rts/linker/LoadArchive.c
- rts/sm/Storage.c
- testsuite/tests/rts/linker/Makefile
- testsuite/tests/rts/linker/all.T
- + testsuite/tests/rts/linker/linker_unload_native.c
- + testsuite/tests/rts/linker/linker_unload_native.stdout
Changes:
=====================================
includes/rts/Linker.h
=====================================
@@ -76,6 +76,19 @@ HsInt loadArchive( pathchar *path );
/* resolve all the currently unlinked objects in memory */
HsInt resolveObjs( void );
+/* Load an .so using the system linker.
+ Returns a handle that can be passed to dlsym() or NULL on error.
+
+ In the case of error, stores the error message in errmsg. The caller
+ is responsible for freeing it. */
+void *loadNativeObj( pathchar *path, char **errmsg );
+
+/* Mark the .so loaded with the system linker for unloading.
+ The RTS will unload it when all the references to the .so disappear from
+ the heap.
+ Takes the handle returned from loadNativeObj() as an argument. */
+HsInt unloadNativeObj( void *handle );
+
/* load a dynamic library */
const char *addDLL( pathchar* dll_name );
=====================================
includes/rts/storage/GC.h
=====================================
@@ -230,6 +230,10 @@ void revertCAFs (void);
// (preferably use RtsConfig.keep_cafs instead)
void setKeepCAFs (void);
+// Let the runtime know that all the CAFs in high mem are not
+// to be retained. Useful in conjunction with loadNativeObj
+void setHighMemDynamic (void);
+
/* -----------------------------------------------------------------------------
This is the write barrier for MUT_VARs, a.k.a. IORefs. A
MUT_VAR_CLEAN object is not on the mutable list; a MUT_VAR_DIRTY
=====================================
rts/CheckUnload.c
=====================================
@@ -238,21 +238,45 @@ static void reserveOCSectionIndices(OCSectionIndices *s_indices, int len)
// state.
void insertOCSectionIndices(ObjectCode *oc)
{
- reserveOCSectionIndices(global_s_indices, oc->n_sections);
+ // after we finish the section table will no longer be sorted.
global_s_indices->sorted = false;
- int s_i = global_s_indices->n_sections;
- for (int i = 0; i < oc->n_sections; i++) {
- if (oc->sections[i].kind != SECTIONKIND_OTHER) {
- global_s_indices->indices[s_i].start = (W_)oc->sections[i].start;
- global_s_indices->indices[s_i].end = (W_)oc->sections[i].start
- + oc->sections[i].size;
- global_s_indices->indices[s_i].oc = oc;
+ if (oc->type == DYNAMIC_OBJECT) {
+ // First count the ranges
+ int n_ranges = 0;
+ for (NativeCodeRange *ncr = oc->nc_ranges; ncr != NULL; ncr = ncr->next) {
+ n_ranges++;
+ }
+
+ // Next reserve the appropriate number of table entries...
+ reserveOCSectionIndices(global_s_indices, n_ranges);
+
+ // Now insert the new ranges...
+ int s_i = global_s_indices->n_sections;
+ for (NativeCodeRange *ncr = oc->nc_ranges; ncr != NULL; ncr = ncr->next) {
+ OCSectionIndex *ent = &global_s_indices->indices[s_i];
+ ent->start = (W_)ncr->start;
+ ent->end = (W_)ncr->end;
+ ent->oc = oc;
s_i++;
}
- }
- global_s_indices->n_sections = s_i;
+ global_s_indices->n_sections = s_i;
+ } else {
+ reserveOCSectionIndices(global_s_indices, oc->n_sections);
+ int s_i = global_s_indices->n_sections;
+ for (int i = 0; i < oc->n_sections; i++) {
+ if (oc->sections[i].kind != SECTIONKIND_OTHER) {
+ OCSectionIndex *ent = &global_s_indices->indices[s_i];
+ ent->start = (W_)oc->sections[i].start;
+ ent->end = (W_)oc->sections[i].start + oc->sections[i].size;
+ ent->oc = oc;
+ s_i++;
+ }
+ }
+
+ global_s_indices->n_sections = s_i;
+ }
// Add object to 'objects' list
if (objects != NULL) {
@@ -443,6 +467,7 @@ void checkUnload()
ObjectCode *next = NULL;
for (ObjectCode *oc = old_objects; oc != NULL; oc = next) {
next = oc->next;
+ ASSERT(oc->status == OBJECT_UNLOADED);
removeOCSectionIndices(s_indices, oc);
=====================================
rts/Linker.c
=====================================
@@ -63,6 +63,7 @@
# include "linker/Elf.h"
# include <regex.h> // regex is already used by dlopen() so this is OK
// to use here without requiring an additional lib
+# include <link.h>
#elif defined(OBJFORMAT_PEi386)
# include "linker/PEi386.h"
# include <windows.h>
@@ -169,6 +170,8 @@ Mutex linker_mutex;
/* Generic wrapper function to try and Resolve and RunInit oc files */
int ocTryLoad( ObjectCode* oc );
+static void freeNativeCode_ELF (ObjectCode *nc);
+
/* Link objects into the lower 2Gb on x86_64 and AArch64. GHC assumes the
* small memory model on this architecture (see gcc docs,
* -mcmodel=small).
@@ -980,7 +983,9 @@ SymbolAddr* lookupSymbol( SymbolName* lbl )
again in unloadObj().
-------------------------------------------------------------------------- */
-static ObjectCode *loading_obj = NULL;
+// volatile works around
+// https://sourceware.org/ml/libc-alpha/2013-08/msg00465.html
+static ForeignExportStablePtr ** volatile loading_fe_stable_ptr = NULL;
StgStablePtr foreignExportStablePtr (StgPtr p)
{
@@ -989,12 +994,12 @@ StgStablePtr foreignExportStablePtr (StgPtr p)
sptr = getStablePtr(p);
- if (loading_obj != NULL) {
+ if (loading_fe_stable_ptr != NULL) {
fe_sptr = stgMallocBytes(sizeof(ForeignExportStablePtr),
"foreignExportStablePtr");
fe_sptr->stable_ptr = sptr;
- fe_sptr->next = loading_obj->stable_ptrs;
- loading_obj->stable_ptrs = fe_sptr;
+ fe_sptr->next = *loading_fe_stable_ptr;
+ *loading_fe_stable_ptr = fe_sptr;
}
return sptr;
@@ -1267,18 +1272,18 @@ static void removeOcSymbols (ObjectCode *oc)
* Release StablePtrs and free oc->stable_ptrs.
* This operation is idempotent.
*/
-static void freeOcStablePtrs (ObjectCode *oc)
+static void freeFEStablePtrs (ForeignExportStablePtr **code_stable_ptrs)
{
// Release any StablePtrs that were created when this
// object module was initialized.
ForeignExportStablePtr *fe_ptr, *next;
- for (fe_ptr = oc->stable_ptrs; fe_ptr != NULL; fe_ptr = next) {
+ for (fe_ptr = *code_stable_ptrs; fe_ptr != NULL; fe_ptr = next) {
next = fe_ptr->next;
freeStablePtr(fe_ptr->stable_ptr);
stgFree(fe_ptr);
}
- oc->stable_ptrs = NULL;
+ *code_stable_ptrs = NULL;
}
static void
@@ -1308,6 +1313,16 @@ freePreloadObjectFile (ObjectCode *oc)
*/
void freeObjectCode (ObjectCode *oc)
{
+ if (oc->type == DYNAMIC_OBJECT) {
+#if defined(OBJFORMAT_ELF)
+ ACQUIRE_LOCK(&dl_mutex);
+ freeNativeCode_ELF(oc);
+ RELEASE_LOCK(&dl_mutex);
+#else
+ barf("This shouldn't happen")
+#endif
+ }
+
freePreloadObjectFile(oc);
if (oc->symbols != NULL) {
@@ -1390,7 +1405,7 @@ void freeObjectCode (ObjectCode *oc)
}
ObjectCode*
-mkOc( pathchar *path, char *image, int imageSize,
+mkOc( ObjectType type, pathchar *path, char *image, int imageSize,
bool mapped, pathchar *archiveMemberName, int misalignment ) {
ObjectCode* oc;
@@ -1398,6 +1413,7 @@ mkOc( pathchar *path, char *image, int imageSize,
oc = stgMallocBytes(sizeof(ObjectCode), "mkOc(oc)");
oc->info = NULL;
+ oc->type = type;
# if defined(OBJFORMAT_ELF)
oc->formatName = "ELF";
@@ -1458,6 +1474,10 @@ mkOc( pathchar *path, char *image, int imageSize,
oc->rx_m32 = m32_allocator_new(true);
#endif
+ oc->l_addr = NULL;
+ oc->nc_ranges = NULL;
+ oc->dlopen_handle = NULL;
+
IF_DEBUG(linker, debugBelch("mkOc: done\n"));
return oc;
}
@@ -1585,7 +1605,7 @@ preloadObjectFile (pathchar *path)
IF_DEBUG(linker, debugBelch("loadObj: preloaded image at %p\n", (void *) image));
/* FIXME (AP): =mapped= parameter unconditionally set to true */
- oc = mkOc(path, image, fileSize, true, NULL, misalignment);
+ oc = mkOc(STATIC_OBJECT, path, image, fileSize, true, NULL, misalignment);
#if defined(OBJFORMAT_MACHO)
if (ocVerifyImage_MachO( oc ))
@@ -1620,7 +1640,7 @@ static HsInt loadObj_ (pathchar *path)
if (! loadOc(oc)) {
// failed; free everything we've allocated
removeOcSymbols(oc);
- // no need to freeOcStablePtrs, they aren't created until resolveObjs()
+ // no need to freeFEStablePtrs, they aren't created until resolveObjs()
freeObjectCode(oc);
return 0;
}
@@ -1794,7 +1814,8 @@ int ocTryLoad (ObjectCode* oc) {
IF_DEBUG(linker, debugBelch("ocTryLoad: ocRunInit start\n"));
- loading_obj = oc; // tells foreignExportStablePtr what to do
+ loading_fe_stable_ptr = &oc->stable_ptrs;
+ // tells foreignExportStablePtr what to do
#if defined(OBJFORMAT_ELF)
r = ocRunInit_ELF ( oc );
#elif defined(OBJFORMAT_PEi386)
@@ -1804,7 +1825,7 @@ int ocTryLoad (ObjectCode* oc) {
#else
barf("ocTryLoad: initializers not implemented on this platform");
#endif
- loading_obj = NULL;
+ loading_fe_stable_ptr = NULL;
if (!r) { return r; }
@@ -1871,7 +1892,7 @@ static HsInt unloadObj_ (pathchar *path, bool just_purge)
// These are both idempotent, so in just_purge mode we can later
// call unloadObj() to really unload the object.
removeOcSymbols(oc);
- freeOcStablePtrs(oc);
+ freeFEStablePtrs(&oc->stable_ptrs);
unloadedAnyObj = true;
@@ -2003,6 +2024,185 @@ addSection (Section *s, SectionKind kind, SectionAlloc alloc,
size, kind ));
}
+
+# if defined(OBJFORMAT_ELF)
+static int loadNativeObjCb_(struct dl_phdr_info *info,
+ size_t _size GNUC3_ATTRIBUTE(__unused__), void *data) {
+ ObjectCode* nc = (ObjectCode*) data;
+
+ // This logic mimicks _dl_addr_inside_object from glibc
+ // For reference:
+ // int
+ // internal_function
+ // _dl_addr_inside_object (struct link_map *l, const ElfW(Addr) addr)
+ // {
+ // int n = l->l_phnum;
+ // const ElfW(Addr) reladdr = addr - l->l_addr;
+ //
+ // while (--n >= 0)
+ // if (l->l_phdr[n].p_type == PT_LOAD
+ // && reladdr - l->l_phdr[n].p_vaddr >= 0
+ // && reladdr - l->l_phdr[n].p_vaddr < l->l_phdr[n].p_memsz)
+ // return 1;
+ // return 0;
+ // }
+
+ if ((void*) info->dlpi_addr == nc->l_addr) {
+ int n = info->dlpi_phnum;
+ while (--n >= 0) {
+ if (info->dlpi_phdr[n].p_type == PT_LOAD) {
+ NativeCodeRange* ncr =
+ stgMallocBytes(sizeof(NativeCodeRange), "loadNativeObjCb_");
+ ncr->start = (void*) ((char*) nc->l_addr + info->dlpi_phdr[n].p_vaddr);
+ ncr->end = (void*) ((char*) ncr->start + info->dlpi_phdr[n].p_memsz);
+
+ ncr->next = nc->nc_ranges;
+ nc->nc_ranges = ncr;
+ }
+ }
+ }
+ return 0;
+}
+
+static void copyErrmsg(char** errmsg_dest, char* errmsg) {
+ if (errmsg == NULL) errmsg = "loadNativeObj_ELF: unknown error";
+ *errmsg_dest = stgMallocBytes(strlen(errmsg)+1, "loadNativeObj_ELF");
+ strcpy(*errmsg_dest, errmsg);
+}
+
+// need dl_mutex
+static void freeNativeCode_ELF (ObjectCode *nc) {
+ dlclose(nc->dlopen_handle);
+
+ NativeCodeRange *ncr = nc->nc_ranges;
+ while (ncr) {
+ NativeCodeRange* last_ncr = ncr;
+ ncr = ncr->next;
+ stgFree(last_ncr);
+ }
+}
+
+static void * loadNativeObj_ELF (pathchar *path, char **errmsg)
+{
+ ObjectCode* nc;
+ void *hdl, *retval;
+
+ IF_DEBUG(linker, debugBelch("loadNativeObj_ELF %" PATH_FMT "\n", path));
+
+ retval = NULL;
+ ACQUIRE_LOCK(&dl_mutex);
+
+ ForeignExportStablePtr *fe_sptr = NULL;
+ loading_fe_stable_ptr = &fe_sptr;
+ // dlopen will run foreignExportStablePtr adding StablePtrs from this DLL
+ // to fe_sptr
+ hdl = dlopen(path, RTLD_NOW|RTLD_LOCAL);
+ loading_fe_stable_ptr = NULL;
+ if (hdl == NULL) {
+ /* dlopen failed; save the message in errmsg */
+ copyErrmsg(errmsg, dlerror());
+ goto dlopen_fail;
+ }
+
+ struct link_map *map;
+ if (dlinfo(hdl, RTLD_DI_LINKMAP, &map) == -1) {
+ /* dlinfo failed; save the message in errmsg */
+ copyErrmsg(errmsg, dlerror());
+ goto dlinfo_fail;
+ }
+
+ nc = mkOc(DYNAMIC_OBJECT, path, NULL, 0, true, NULL, 0);
+ nc->l_addr = (void*) map->l_addr;
+ nc->dlopen_handle = hdl;
+ hdl = NULL; // pass handle ownership to nc
+ nc->stable_ptrs = fe_sptr;
+ fe_sptr = NULL; // pass the ownership to nc
+
+ dl_iterate_phdr(loadNativeObjCb_, nc);
+ if (!nc->nc_ranges) {
+ copyErrmsg(errmsg, "dl_iterate_phdr failed to find obj");
+ goto dl_iterate_phdr_fail;
+ }
+
+ insertOCSectionIndices(nc);
+
+ nc->next_loaded_object = loaded_objects;
+ loaded_objects = nc;
+
+ retval = nc->dlopen_handle;
+ goto success;
+
+dl_iterate_phdr_fail:
+ // already have dl_mutex
+ freeNativeCode_ELF(nc);
+dlinfo_fail:
+ if (hdl) dlclose(hdl);
+dlopen_fail:
+ freeFEStablePtrs(&fe_sptr);
+success:
+
+ RELEASE_LOCK(&dl_mutex);
+ IF_DEBUG(linker, debugBelch("loadNativeObj_ELF result=%p\n", retval));
+
+ return retval;
+}
+
+# endif
+
+#define UNUSED(x) (void)(x)
+
+void * loadNativeObj (pathchar *path, char **errmsg)
+{
+#if defined(OBJFORMAT_ELF)
+ ACQUIRE_LOCK(&linker_mutex);
+ void *r = loadNativeObj_ELF(path, errmsg);
+ RELEASE_LOCK(&linker_mutex);
+ return r;
+#else
+ UNUSED(path);
+ UNUSED(errmsg);
+ barf("loadNativeObj: not implemented on this platform");
+#endif
+}
+
+HsInt unloadNativeObj (void *handle)
+{
+ bool unloadedAnyObj = false;
+
+ IF_DEBUG(linker, debugBelch("unloadNativeObj: %p\n", handle));
+
+ ObjectCode *prev = NULL, *next;
+ for (ObjectCode *nc = loaded_objects; nc; nc = next) {
+ next = nc->next_loaded_object; // we might move nc
+
+ if (nc->type == DYNAMIC_OBJECT && nc->dlopen_handle == handle) {
+ nc->status = OBJECT_UNLOADED;
+ n_unloaded_objects += 1;
+
+ // dynamic objects have no symbols
+ ASSERT(nc->symbols == NULL);
+ freeFEStablePtrs(&nc->stable_ptrs);
+
+ // Remove object code from root set
+ if (prev == NULL) {
+ loaded_objects = nc->next_loaded_object;
+ } else {
+ prev->next_loaded_object = nc->next_loaded_object;
+ }
+ unloadedAnyObj = true;
+ } else {
+ prev = nc;
+ }
+ }
+
+ if (unloadedAnyObj) {
+ return 1;
+ } else {
+ errorBelch("unloadObjNativeObj_ELF: can't find `%p' to unload", handle);
+ return 0;
+ }
+}
+
/* -----------------------------------------------------------------------------
* Segment management
*/
=====================================
rts/LinkerInternals.h
=====================================
@@ -32,6 +32,13 @@ typedef struct _Symbol
SymbolAddr *addr;
} Symbol_t;
+typedef struct NativeCodeRange_ {
+ void *start, *end;
+
+ /* Allow a chain of these things */
+ struct NativeCodeRange_ *next;
+} NativeCodeRange;
+
/* Indication of section kinds for loaded objects. Needed by
the GC for deciding whether or not a pointer on the stack
is a code pointer.
@@ -169,6 +176,13 @@ typedef struct {
#endif
} SymbolExtra;
+typedef enum {
+ /* Objects that were loaded by this linker */
+ STATIC_OBJECT,
+
+ /* Objects that were loaded by dlopen */
+ DYNAMIC_OBJECT,
+} ObjectType;
/* Top-level structure for an object module. One of these is allocated
* for each object file in use.
@@ -177,7 +191,8 @@ typedef struct _ObjectCode {
OStatus status;
pathchar *fileName;
int fileSize; /* also mapped image size when using mmap() */
- char* formatName; /* eg "ELF32", "DLL", "COFF", etc. */
+ char* formatName; /* e.g. "ELF32", "DLL", "COFF", etc. */
+ ObjectType type; /* who loaded this object? */
/* If this object is a member of an archive, archiveMemberName is
* like "libarchive.a(object.o)". Otherwise it's NULL.
@@ -278,6 +293,19 @@ typedef struct _ObjectCode {
* (read-only/executable) code. */
m32_allocator *rw_m32, *rx_m32;
#endif
+
+ /*
+ * The following are only valid if .type == DYNAMIC_OBJECT
+ */
+
+ /* handle returned from dlopen */
+ void *dlopen_handle;
+
+ /* base virtual address of the loaded code */
+ void *l_addr;
+
+ /* virtual memory ranges of loaded code */
+ NativeCodeRange *nc_ranges;
} ObjectCode;
#define OC_INFORMATIVE_FILENAME(OC) \
@@ -286,6 +314,7 @@ typedef struct _ObjectCode {
(OC)->fileName \
)
+
#if defined(THREADED_RTS)
extern Mutex linker_mutex;
#endif
@@ -371,7 +400,7 @@ resolveSymbolAddr (pathchar* buffer, int size,
HsInt isAlreadyLoaded( pathchar *path );
HsInt loadOc( ObjectCode* oc );
-ObjectCode* mkOc( pathchar *path, char *image, int imageSize,
+ObjectCode* mkOc( ObjectType type, pathchar *path, char *image, int imageSize,
bool mapped, pathchar *archiveMemberName,
int misalignment
);
=====================================
rts/linker/LoadArchive.c
=====================================
@@ -521,7 +521,7 @@ static HsInt loadArchive_ (pathchar *path)
pathprintf(archiveMemberName, size, WSTR("%" PATH_FMT "(%.*s)"),
path, (int)thisFileNameSize, fileName);
- ObjectCode *oc = mkOc(path, image, memberSize, false, archiveMemberName,
+ ObjectCode *oc = mkOc(STATIC_OBJECT, path, image, memberSize, false, archiveMemberName,
misalignment);
#if defined(OBJFORMAT_MACHO)
ocInit_MachO( oc );
=====================================
rts/sm/Storage.c
=====================================
@@ -45,6 +45,7 @@ StgIndStatic *dyn_caf_list = NULL;
StgIndStatic *debug_caf_list = NULL;
StgIndStatic *revertible_caf_list = NULL;
bool keepCAFs;
+bool highMemDynamic;
W_ large_alloc_lim; /* GC if n_large_blocks in any nursery
* reaches this. */
@@ -519,7 +520,7 @@ newCAF(StgRegTable *reg, StgIndStatic *caf)
bh = lockCAF(reg, caf);
if (!bh) return NULL;
- if(keepCAFs)
+ if(keepCAFs && !(highMemDynamic && (void*) caf > (void*) 0x80000000))
{
// Note [dyn_caf_list]
// If we are in GHCi _and_ we are using dynamic libraries,
@@ -573,6 +574,12 @@ setKeepCAFs (void)
keepCAFs = 1;
}
+void
+setHighMemDynamic (void)
+{
+ highMemDynamic = 1;
+}
+
// An alternate version of newCAF which is used for dynamically loaded
// object code in GHCi. In this case we want to retain *all* CAFs in
// the object code, because they might be demanded at any time from an
=====================================
testsuite/tests/rts/linker/Makefile
=====================================
@@ -58,6 +58,23 @@ linker_unload:
# -rtsopts causes a warning
"$(TEST_HC)" LinkerUnload.hs -package ghc $(filter-out -rtsopts, $(TEST_HC_OPTS)) linker_unload.c -o linker_unload -no-hs-main -optc-Werror
./linker_unload "`'$(TEST_HC)' $(TEST_HC_OPTS) --print-libdir | tr -d '\r'`"
+
+.PHONY: linker_unload_native
+linker_unload_native:
+ $(RM) Test.o Test.hi Test.a Test.so Test2.so
+ "$(TEST_HC)" $(TEST_HC_OPTS) -c Test.hs -v0 -dynamic -fPIC -o Test.a
+ # only libraries without DT_NEEDED are supported
+ "$(CC)" -shared -Wl,-Bsymbolic -nostdlib -o Test.so -Wl,-nostdlib \
+ -Wl,--whole-archive Test.a
+ cp Test.so Test2.so
+
+ # -rtsopts causes a warning
+ "$(TEST_HC)" LinkerUnload.hs -optl-Wl,--export-dynamic -package ghc \
+ $(filter-out -rtsopts, $(TEST_HC_OPTS)) linker_unload_native.c \
+ -o linker_unload_native -no-hs-main -optc-Werror
+ ./linker_unload_native \
+ "`'$(TEST_HC)' $(TEST_HC_OPTS) --print-libdir | tr -d '\r'`"
+
# -----------------------------------------------------------------------------
# Testing failures in the RTS linker. We should be able to repeatedly
=====================================
testsuite/tests/rts/linker/all.T
=====================================
@@ -84,6 +84,11 @@ test('T5435_dyn_gcc', extra_files(['T5435.hs', 'T5435_gcc.c']) , makefile_test,
test('linker_unload',
[extra_files(['LinkerUnload.hs', 'Test.hs']), req_rts_linker],
makefile_test, ['linker_unload'])
+
+test('linker_unload_native',
+ [extra_files(['LinkerUnload.hs', 'Test.hs']), req_rts_linker],
+ makefile_test, ['linker_unload_native'])
+
######################################
test('linker_error1', [extra_files(['linker_error.c']),
=====================================
testsuite/tests/rts/linker/linker_unload_native.c
=====================================
@@ -0,0 +1,93 @@
+#include "ghcconfig.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include "Rts.h"
+#include <string.h>
+#include <dlfcn.h>
+
+// poke into linker internals
+extern void *objects;
+
+#define ITERATIONS 1000
+
+#if defined(mingw32_HOST_OS)
+#define OBJPATH L"Test.so"
+#define OBJPATH2 L"Test2.so"
+#else
+#define OBJPATH "./Test.so"
+#define OBJPATH2 "./Test2.so"
+#endif
+
+typedef int testfun(int);
+
+extern void loadPackages(void);
+
+int main (int argc, char *argv[])
+{
+ testfun *f, *f2;
+ int i, r;
+
+ RtsConfig conf = defaultRtsConfig;
+ conf.rts_opts_enabled = RtsOptsAll;
+ // we want to preserve static CAFs and unload dynamic CAFs
+ conf.keep_cafs = true;
+ setHighMemDynamic();
+ hs_init_ghc(&argc, &argv, conf);
+
+ initLinker_(0);
+
+ loadPackages();
+
+ for (i=0; i < ITERATIONS; i++) {
+ char* errmsg;
+ // load 2 libraries at once
+ void* handle = loadNativeObj(OBJPATH, &errmsg);
+ if (!handle) {
+ errorBelch("loadNativeObj(%s) failed: %s", OBJPATH, errmsg);
+ free(errmsg);
+ exit(1);
+ }
+
+ void* handle2 = loadNativeObj(OBJPATH2, &errmsg);
+ if (!handle2) {
+ errorBelch("loadNativeObj(%s) failed: %s", OBJPATH2, errmsg);
+ free(errmsg);
+ exit(1);
+ }
+#if LEADING_UNDERSCORE
+ f = dlsym(handle, "_f");
+ f2 = dlsym(handle2, "_f");
+#else
+ f = dlsym(handle, "f");
+ f2 = dlsym(handle2, "f");
+#endif
+ if (!f) {
+ errorBelch("dlsym failed");
+ exit(1);
+ }
+ r = f(3);
+ if (r != 4) {
+ errorBelch("call failed; %d", r);
+ exit(1);
+ }
+ if (!f2) {
+ errorBelch("dlsym failed");
+ exit(1);
+ }
+ r = f2(3);
+ if (r != 4) {
+ errorBelch("call failed; %d", r);
+ exit(1);
+ }
+ unloadNativeObj(handle);
+ unloadNativeObj(handle2);
+ performMajorGC();
+ printf("%d ", i);
+ fflush(stdout);
+ }
+
+ // Verify that Test.so isn't still loaded.
+ int res = getObjectLoadStatus("Test.so") != OBJECT_NOT_LOADED;
+ hs_exit();
+ exit(res);
+}
=====================================
testsuite/tests/rts/linker/linker_unload_native.stdout
=====================================
@@ -0,0 +1,3 @@
+[1 of 1] Compiling LinkerUnload ( LinkerUnload.hs, LinkerUnload.o )
+Linking linker_unload_native ...
+0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
\ No newline at end of file
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/2cfcbcfa90dd5cda84c7bdd3b4ef36786ea508c8
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/2cfcbcfa90dd5cda84c7bdd3b4ef36786ea508c8
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/20200809/ae66769e/attachment-0001.html>
More information about the ghc-commits
mailing list