xref: /aosp_15_r20/external/compiler-rt/test/tsan/dlclose.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clangxx_tsan -O1 %s -DBUILD_SO -fPIC -shared -o %t-so.so
2*7c3d14c8STreehugger Robot // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
3*7c3d14c8STreehugger Robot 
4*7c3d14c8STreehugger Robot // Test case for
5*7c3d14c8STreehugger Robot // https://github.com/google/sanitizers/issues/487
6*7c3d14c8STreehugger Robot 
7*7c3d14c8STreehugger Robot #ifdef BUILD_SO
8*7c3d14c8STreehugger Robot 
9*7c3d14c8STreehugger Robot #include <stdio.h>
10*7c3d14c8STreehugger Robot 
11*7c3d14c8STreehugger Robot extern "C"
sofunc()12*7c3d14c8STreehugger Robot void sofunc() {
13*7c3d14c8STreehugger Robot   fprintf(stderr, "HELLO FROM SO\n");
14*7c3d14c8STreehugger Robot }
15*7c3d14c8STreehugger Robot 
16*7c3d14c8STreehugger Robot #else  // BUILD_SO
17*7c3d14c8STreehugger Robot 
18*7c3d14c8STreehugger Robot #include <dlfcn.h>
19*7c3d14c8STreehugger Robot #include <stdio.h>
20*7c3d14c8STreehugger Robot #include <stddef.h>
21*7c3d14c8STreehugger Robot #include <unistd.h>
22*7c3d14c8STreehugger Robot #include <string>
23*7c3d14c8STreehugger Robot 
24*7c3d14c8STreehugger Robot void *lib;
25*7c3d14c8STreehugger Robot void *lib2;
26*7c3d14c8STreehugger Robot 
27*7c3d14c8STreehugger Robot struct Closer {
~CloserCloser28*7c3d14c8STreehugger Robot   ~Closer() {
29*7c3d14c8STreehugger Robot     dlclose(lib);
30*7c3d14c8STreehugger Robot     fprintf(stderr, "CLOSED SO\n");
31*7c3d14c8STreehugger Robot   }
32*7c3d14c8STreehugger Robot };
33*7c3d14c8STreehugger Robot static Closer c;
34*7c3d14c8STreehugger Robot 
main(int argc,char * argv[])35*7c3d14c8STreehugger Robot int main(int argc, char *argv[]) {
36*7c3d14c8STreehugger Robot   lib = dlopen((std::string(argv[0]) + std::string("-so.so")).c_str(),
37*7c3d14c8STreehugger Robot       RTLD_NOW|RTLD_NODELETE);
38*7c3d14c8STreehugger Robot   if (lib == 0) {
39*7c3d14c8STreehugger Robot     printf("error in dlopen: %s\n", dlerror());
40*7c3d14c8STreehugger Robot     return 1;
41*7c3d14c8STreehugger Robot   }
42*7c3d14c8STreehugger Robot   void *f = dlsym(lib, "sofunc");
43*7c3d14c8STreehugger Robot   if (f == 0) {
44*7c3d14c8STreehugger Robot     printf("error in dlsym: %s\n", dlerror());
45*7c3d14c8STreehugger Robot     return 1;
46*7c3d14c8STreehugger Robot   }
47*7c3d14c8STreehugger Robot   ((void(*)())f)();
48*7c3d14c8STreehugger Robot   return 0;
49*7c3d14c8STreehugger Robot }
50*7c3d14c8STreehugger Robot 
51*7c3d14c8STreehugger Robot #endif  // BUILD_SO
52*7c3d14c8STreehugger Robot 
53*7c3d14c8STreehugger Robot // CHECK: HELLO FROM SO
54*7c3d14c8STreehugger Robot // CHECK-NOT: Inconsistency detected by ld.so
55*7c3d14c8STreehugger Robot // CHECK: CLOSED SO
56*7c3d14c8STreehugger Robot 
57