File: dlclose.cc

package info (click to toggle)
llvm-toolchain-9 1%3A9.0.1-16.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 882,388 kB
  • sloc: cpp: 4,167,636; ansic: 714,256; asm: 457,610; python: 155,927; objc: 65,094; sh: 42,856; lisp: 26,908; perl: 7,786; pascal: 7,722; makefile: 6,881; ml: 5,581; awk: 3,648; cs: 2,027; xml: 888; javascript: 381; ruby: 156
file content (56 lines) | stat: -rw-r--r-- 1,058 bytes parent folder | download | duplicates (33)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// RUN: %clangxx_tsan -O1 %s -DBUILD_SO -fPIC -shared -o %t-so.so
// RUN: %clangxx_tsan -O1 %s %link_libcxx_tsan -o %t && %run %t 2>&1 | FileCheck %s

// Test case for
// https://github.com/google/sanitizers/issues/487

#ifdef BUILD_SO

#include <stdio.h>

extern "C"
void sofunc() {
  fprintf(stderr, "HELLO FROM SO\n");
}

#else  // BUILD_SO

#include <dlfcn.h>
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include <string>

void *lib;
void *lib2;

struct Closer {
  ~Closer() {
    dlclose(lib);
    fprintf(stderr, "CLOSED SO\n");
  }
};
static Closer c;

int main(int argc, char *argv[]) {
  lib = dlopen((std::string(argv[0]) + std::string("-so.so")).c_str(),
      RTLD_NOW|RTLD_NODELETE);
  if (lib == 0) {
    printf("error in dlopen: %s\n", dlerror());
    return 1;
  }
  void *f = dlsym(lib, "sofunc");
  if (f == 0) {
    printf("error in dlsym: %s\n", dlerror());
    return 1;
  }
  ((void(*)())f)();
  return 0;
}

#endif  // BUILD_SO

// CHECK: HELLO FROM SO
// CHECK-NOT: Inconsistency detected by ld.so
// CHECK: CLOSED SO