xref: /llvm-project/compiler-rt/test/rtsan/Darwin/dlopen.cpp (revision 4bdac0851f4d613890558a8254043e21b0479b1e)
1 // Checks that on OS X 10.11+ dlopen'ing a RTsanified library from a
2 // non-instrumented program exits with a user-friendly message.
3 
4 // REQUIRES: osx-autointerception
5 
6 // XFAIL: ios
7 
8 // RUN: %clangxx -fsanitize=realtime %s -o %t.so -shared -DSHARED_LIB
9 // RUN: %clangxx %s -o %t
10 
11 // RUN: RTSAN_DYLIB_PATH=`%clangxx -fsanitize=realtime %s -### 2>&1 \
12 // RUN:   | grep "libclang_rt.rtsan_osx_dynamic.dylib" \
13 // RUN:   | sed -e 's/.*"\(.*libclang_rt.rtsan_osx_dynamic.dylib\)".*/\1/'`
14 
15 // Launching a non-instrumented binary that dlopen's an instrumented library should fail.
16 // RUN: not %run %t %t.so 2>&1 | FileCheck %s --check-prefix=CHECK-FAIL
17 // Launching a non-instrumented binary with an explicit DYLD_INSERT_LIBRARIES should work.
18 // RUN: DYLD_INSERT_LIBRARIES=$RTSAN_DYLIB_PATH %run %t %t.so 2>&1 | FileCheck %s
19 
20 // Launching an instrumented binary with the DYLD_INSERT_LIBRARIES env variable has no error
21 // RUN: %clangxx -fsanitize=realtime %s -o %t
22 // RUN: DYLD_INSERT_LIBRARIES=$RTSAN_DYLIB_PATH %run %t %t.so 2>&1 | FileCheck %s --check-prefix=CHECK-INSTRUMENTED
23 
24 #include <dlfcn.h>
25 #include <stdio.h>
26 
27 #if defined(SHARED_LIB)
28 extern "C" void foo() { fprintf(stderr, "Hello world.\n"); }
29 #else  // defined(SHARED_LIB)
30 int main(int argc, char *argv[]) {
31   void *handle = dlopen(argv[1], RTLD_NOW);
32   void (*foo)() = (void (*)())dlsym(handle, "foo");
33   foo();
34 }
35 #endif // defined(SHARED_LIB)
36 
37 // CHECK: Hello world.
38 // CHECK-NOT: ERROR: Interceptors are not working.
39 
40 // CHECK-FAIL-NOT: Hello world.
41 // CHECK-FAIL: ERROR: Interceptors are not working.
42 
43 // CHECK-INSTRUMENTED-NOT: ERROR: Interceptors are not working
44 // CHECK-INSTRUMENTED: Hello world.
45