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// Test case for
5// https://github.com/google/sanitizers/issues/487
6
7#ifdef BUILD_SO
8
9#include <stdio.h>
10
11extern "C"
12void 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
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_SO
52
53// CHECK: HELLO FROM SO
54// CHECK-NOT: Inconsistency detected by ld.so
55// CHECK: CLOSED SO
56
57