1 // RUN: %clangxx -g -O0 %s -o %t
2 
3 // Check that trying to dlopen() the ASan dylib fails.
4 // We explictly set `abort_on_error=0` because
5 // - By default the lit config sets this but we don't want this
6 //   test to implicitly depend on this.
7 // - It avoids requiring `--crash` to be passed to `not`.
8 // RUN: APPLE_ASAN_INIT_FOR_DLOPEN=0 %env_asan_opts=abort_on_error=0 not \
9 // RUN:   %run %t %shared_libasan 2>&1 | \
10 // RUN:   FileCheck -check-prefix=CHECK-DL-OPEN-FAIL %s
11 // RUN: env -u APPLE_ASAN_INIT_FOR_DLOPEN %env_asan_opts=abort_on_error=0 not \
12 // RUN:   %run %t %shared_libasan 2>&1 | \
13 // RUN:   FileCheck -check-prefix=CHECK-DL-OPEN-FAIL %s
14 
15 // Check that we can successfully dlopen the ASan dylib when we set the right
16 // environment variable.
17 // RUN: env APPLE_ASAN_INIT_FOR_DLOPEN=1 %run %t %shared_libasan 2>&1 | \
18 // RUN:   FileCheck -check-prefix=CHECK-DL-OPEN-SUCCESS %s
19 
20 #include <dlfcn.h>
21 #include <stdio.h>
22 
23 // CHECK-DL-OPEN-FAIL: ERROR: Interceptors are not working
24 // CHECK-SAME-DL-OPEN-FAIL: Please launch the executable with: DYLD_INSERT_LIBRARIES={{.+}}/libclang_rt.asan_{{.+}}_dynamic.dylib
25 
26 int main(int argc, char **argv) {
27   if (argc != 2) {
28     fprintf(stderr, "Usage: %s <dylib_path>\n", argv[0]);
29     return 1;
30   }
31   const char *dylib_path = argv[1];
32   void *handle = dlopen(dylib_path, RTLD_LAZY);
33   if (!handle) {
34     fprintf(stderr, "Failed to dlopen: %s\n", dlerror());
35     return 1;
36   }
37   // Make sure we can find a function we expect to be in the dylib.
38   void *fn = dlsym(handle, "__sanitizer_mz_size");
39   if (!fn) {
40     fprintf(stderr, "Failed to get symbol: %s\n", dlerror());
41     return 1;
42   }
43   // TODO(dliew): Actually call a function from the dylib that is safe to call.
44   // CHECK-DL-OPEN-SUCCESS: DONE
45   printf("DONE\n");
46   return 0;
47 }
48