Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 1 | // RUN: %clangxx_tsan -O1 %s -DBUILD_SO -fPIC -shared -o %t-so.so |
| 2 | // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s |
| 3 | |
| 4 | // If we mention TSAN_OPTIONS, the test won't run from test_output.sh script. |
| 5 | |
| 6 | // Test case for |
| 7 | // https://code.google.com/p/thread-sanitizer/issues/detail?id=80 |
| 8 | |
| 9 | #ifdef BUILD_SO |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | |
| 13 | extern "C" |
| 14 | void sofunc() { |
| 15 | fprintf(stderr, "HELLO FROM SO\n"); |
| 16 | } |
| 17 | |
| 18 | #else // BUILD_SO |
| 19 | |
| 20 | #include <dlfcn.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stddef.h> |
| 23 | #include <unistd.h> |
| 24 | #include <string> |
| 25 | |
| 26 | void *lib; |
| 27 | void *lib2; |
| 28 | |
| 29 | struct Closer { |
| 30 | ~Closer() { |
| 31 | dlclose(lib); |
| 32 | fprintf(stderr, "CLOSED SO\n"); |
| 33 | } |
| 34 | }; |
| 35 | static Closer c; |
| 36 | |
| 37 | int main(int argc, char *argv[]) { |
| 38 | lib = dlopen((std::string(argv[0]) + std::string("-so.so")).c_str(), |
| 39 | RTLD_NOW|RTLD_NODELETE); |
| 40 | if (lib == 0) { |
| 41 | printf("error in dlopen: %s\n", dlerror()); |
| 42 | return 1; |
| 43 | } |
| 44 | void *f = dlsym(lib, "sofunc"); |
| 45 | if (f == 0) { |
| 46 | printf("error in dlsym: %s\n", dlerror()); |
| 47 | return 1; |
| 48 | } |
| 49 | ((void(*)())f)(); |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | #endif // BUILD_SO |
| 54 | |
| 55 | // CHECK: HELLO FROM SO |
| 56 | // CHECK-NOT: Inconsistency detected by ld.so |
| 57 | // CHECK: CLOSED SO |
| 58 | |