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 
main(int argc,char ** argv)25 int main(int argc, char **argv) {
26   if (argc != 2) {
27     fprintf(stderr, "Usage: %s <dylib_path>\n", argv[0]);
28     return 1;
29   }
30   const char *dylib_path = argv[1];
31   void *handle = dlopen(dylib_path, RTLD_LAZY);
32   if (!handle) {
33     fprintf(stderr, "Failed to dlopen: %s\n", dlerror());
34     return 1;
35   }
36   // Make sure we can find a function we expect to be in the dylib.
37   void *fn = dlsym(handle, "__sanitizer_mz_size");
38   if (!fn) {
39     fprintf(stderr, "Failed to get symbol: %s\n", dlerror());
40     return 1;
41   }
42   // TODO(dliew): Actually call a function from the dylib that is safe to call.
43   // CHECK-DL-OPEN-SUCCESS: DONE
44   printf("DONE\n");
45   return 0;
46 }
47