xref: /aosp_15_r20/external/compiler-rt/test/tsan/Darwin/dlopen.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // Checks that on OS X 10.11+ (where we do not re-exec anymore, because
2*7c3d14c8STreehugger Robot // interceptors work automatically), dlopen'ing a TSanified library from a
3*7c3d14c8STreehugger Robot // non-instrumented program exits with a user-friendly message.
4*7c3d14c8STreehugger Robot 
5*7c3d14c8STreehugger Robot // REQUIRES: osx-autointerception
6*7c3d14c8STreehugger Robot 
7*7c3d14c8STreehugger Robot // RUN: %clangxx_tsan %s -o %t.so -shared -DSHARED_LIB
8*7c3d14c8STreehugger Robot // RUN: %clangxx_tsan -fno-sanitize=thread %s -o %t
9*7c3d14c8STreehugger Robot 
10*7c3d14c8STreehugger Robot // RUN: TSAN_DYLIB_PATH=`%clangxx_tsan %s -### 2>&1 \
11*7c3d14c8STreehugger Robot // RUN:   | grep "libclang_rt.tsan_osx_dynamic.dylib" \
12*7c3d14c8STreehugger Robot // RUN:   | sed -e 's/.*"\(.*libclang_rt.tsan_osx_dynamic.dylib\)".*/\1/'`
13*7c3d14c8STreehugger Robot 
14*7c3d14c8STreehugger Robot // Launching a non-instrumented binary that dlopen's an instrumented library should fail.
15*7c3d14c8STreehugger Robot // RUN: not %run %t %t.so 2>&1 | FileCheck %s --check-prefix=CHECK-FAIL
16*7c3d14c8STreehugger Robot // Launching a non-instrumented binary with an explicit DYLD_INSERT_LIBRARIES should work.
17*7c3d14c8STreehugger Robot // RUN: DYLD_INSERT_LIBRARIES=$TSAN_DYLIB_PATH %run %t %t.so 2>&1 | FileCheck %s
18*7c3d14c8STreehugger Robot 
19*7c3d14c8STreehugger Robot #include <dlfcn.h>
20*7c3d14c8STreehugger Robot #include <pthread.h>
21*7c3d14c8STreehugger Robot #include <stdio.h>
22*7c3d14c8STreehugger Robot 
23*7c3d14c8STreehugger Robot #if defined(SHARED_LIB)
foo()24*7c3d14c8STreehugger Robot extern "C" void foo() {
25*7c3d14c8STreehugger Robot   fprintf(stderr, "Hello world.\n");
26*7c3d14c8STreehugger Robot }
27*7c3d14c8STreehugger Robot #else  // defined(SHARED_LIB)
main(int argc,char * argv[])28*7c3d14c8STreehugger Robot int main(int argc, char *argv[]) {
29*7c3d14c8STreehugger Robot   void *handle = dlopen(argv[1], RTLD_NOW);
30*7c3d14c8STreehugger Robot   fprintf(stderr, "handle = %p\n", handle);
31*7c3d14c8STreehugger Robot   void (*foo)() = (void (*)())dlsym(handle, "foo");
32*7c3d14c8STreehugger Robot   fprintf(stderr, "foo = %p\n", foo);
33*7c3d14c8STreehugger Robot   foo();
34*7c3d14c8STreehugger Robot }
35*7c3d14c8STreehugger Robot #endif  // defined(SHARED_LIB)
36*7c3d14c8STreehugger Robot 
37*7c3d14c8STreehugger Robot // CHECK: Hello world.
38*7c3d14c8STreehugger Robot // CHECK-NOT: ERROR: Interceptors are not working.
39*7c3d14c8STreehugger Robot 
40*7c3d14c8STreehugger Robot // CHECK-FAIL-NOT: Hello world.
41*7c3d14c8STreehugger Robot // CHECK-FAIL: ERROR: Interceptors are not working.
42