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 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 4 | // Test case for |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 5 | // https://github.com/google/sanitizers/issues/487 |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 6 | |
| 7 | #ifdef BUILD_SO |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | |
| 11 | extern "C" |
| 12 | void sofunc() { |
| 13 | fprintf(stderr, "HELLO FROM SO\n"); |
| 14 | } |
| 15 | |
| 16 | #else // BUILD_SO |
| 17 | |
| 18 | #include <dlfcn.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stddef.h> |
| 21 | #include <unistd.h> |
| 22 | #include <string> |
| 23 | |
| 24 | void *lib; |
| 25 | void *lib2; |
| 26 | |
| 27 | struct Closer { |
| 28 | ~Closer() { |
| 29 | dlclose(lib); |
| 30 | fprintf(stderr, "CLOSED SO\n"); |
| 31 | } |
| 32 | }; |
| 33 | static Closer c; |
| 34 | |
| 35 | int 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_SO |
| 52 | |
| 53 | // CHECK: HELLO FROM SO |
| 54 | // CHECK-NOT: Inconsistency detected by ld.so |
| 55 | // CHECK: CLOSED SO |
| 56 | |