2 import core
.stdc
.stdio
;
3 import core
.stdc
.string
;
6 version (linux
) import core
.sys
.linux
.dlfcn
;
7 else version (FreeBSD
) import core
.sys
.freebsd
.dlfcn
;
8 else version (NetBSD
) import core
.sys
.netbsd
.dlfcn
;
9 else static assert(0, "unimplemented");
11 void loadSym(T
)(void* handle
, ref T val
, const char* mangle
)
13 val
= cast(T
).dlsym(handle
, mangle
);
16 void* openLib(string s
)
18 auto h
= Runtime
.loadLibrary(s
);
21 loadSym(h
, libThrowException
, "_D3lib14throwExceptionFZv");
22 loadSym(h
, libCollectException
, "_D3lib16collectExceptionFDFZvZC9Exception");
24 loadSym(h
, libAlloc
, "_D3lib5allocFZv");
25 loadSym(h
, libTlsAlloc
, "_D3lib9tls_allocFZv");
26 loadSym(h
, libAccess
, "_D3lib6accessFZv");
27 loadSym(h
, libTlsAccess
, "_D3lib10tls_accessFZv");
28 loadSym(h
, libFree
, "_D3lib4freeFZv");
29 loadSym(h
, libTlsFree
, "_D3lib8tls_freeFZv");
31 loadSym(h
, libSharedStaticCtor
, "_D3lib18shared_static_ctorOk");
32 loadSym(h
, libSharedStaticDtor
, "_D3lib18shared_static_dtorOk");
33 loadSym(h
, libStaticCtor
, "_D3lib11static_ctorOk");
34 loadSym(h
, libStaticDtor
, "_D3lib11static_dtorOk");
39 void closeLib(void* h
)
41 Runtime
.unloadLibrary(h
);
46 void function() libThrowException
;
47 Exception
function(void delegate()) libCollectException
;
49 void function() libAlloc
;
50 void function() libTlsAlloc
;
51 void function() libAccess
;
52 void function() libTlsAccess
;
53 void function() libFree
;
54 void function() libTlsFree
;
56 shared uint* libSharedStaticCtor
;
57 shared uint* libSharedStaticDtor
;
58 shared uint* libStaticCtor
;
59 shared uint* libStaticDtor
;
69 assert(passed
); passed
= false;
71 assert(libCollectException({throw new Exception(null);}) !is null);
72 assert(libCollectException({libThrowException();}) !is null);
92 assert(*libStaticCtor
== 1);
93 assert(*libStaticDtor
== 0);
96 assert(*libSharedStaticCtor
== 1);
97 assert(*libSharedStaticDtor
== 0);
98 assert(*libStaticCtor
== 2);
99 assert(*libStaticDtor
== 0);
101 auto thr
= new Thread(&run
);
104 assert(*libSharedStaticCtor
== 1);
105 assert(*libSharedStaticDtor
== 0);
106 assert(*libStaticCtor
== 2);
107 assert(*libStaticDtor
== 1);
110 const(ModuleInfo
)* findModuleInfo(string name
)
112 foreach (m
; ModuleInfo
)
113 if (m
.name
== name
) return m
;
117 void runTests(string libName
)
119 assert(findModuleInfo("lib") is null);
120 auto handle
= openLib(libName
);
121 assert(findModuleInfo("lib") !is null);
128 assert(findModuleInfo("lib") is null);
131 void main(string
[] args
)
133 auto name
= args
[0] ~ '\0';
134 const pathlen
= strrchr(name
.ptr
, '/') - name
.ptr
+ 1;
135 name
= name
[0 .. pathlen
] ~ "lib.so";
139 // lib is no longer resident
141 assert(.dlopen(name
.ptr
, RTLD_LAZY | RTLD_NOLOAD
) is null);
142 name
= name
[0 .. $-1];
144 auto thr
= new Thread({runTests(name
);});