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