57 lines · cpp
1// RUN: %clangxx_tsan -O1 %s -DBUILD_SO -fPIC -shared -o %t-so.so2// RUN: %clangxx_tsan -O1 %s %link_libcxx_tsan -o %t && %run %t 2>&1 | FileCheck %s3 4// Test case for5// https://github.com/google/sanitizers/issues/4876 7#ifdef BUILD_SO8 9#include <stdio.h>10 11extern "C"12void sofunc() {13 fprintf(stderr, "HELLO FROM SO\n");14}15 16#else // BUILD_SO17 18#include <dlfcn.h>19#include <stdio.h>20#include <stddef.h>21#include <unistd.h>22#include <string>23 24void *lib;25void *lib2;26 27struct Closer {28 ~Closer() {29 dlclose(lib);30 fprintf(stderr, "CLOSED SO\n");31 }32};33static Closer c;34 35int main(int argc, char *argv[]) {36 lib = dlopen((std::string(argv[0]) + std::string("-so.so")).c_str(),37 RTLD_NOW|RTLD_NODELETE);38 if (lib == 0) {39 printf("error in dlopen: %s\n", dlerror());40 return 1;41 }42 void *f = dlsym(lib, "sofunc");43 if (f == 0) {44 printf("error in dlsym: %s\n", dlerror());45 return 1;46 }47 ((void(*)())f)();48 return 0;49}50 51#endif // BUILD_SO52 53// CHECK: HELLO FROM SO54// CHECK-NOT: Inconsistency detected by ld.so55// CHECK: CLOSED SO56 57