1673dc3d4SNico Weber // RUN: %clangxx -g -O0 %s -o %t
2673dc3d4SNico Weber 
3673dc3d4SNico Weber // Check that trying to dlopen() the ASan dylib fails.
4673dc3d4SNico Weber // We explictly set `abort_on_error=0` because
5673dc3d4SNico Weber // - By default the lit config sets this but we don't want this
6673dc3d4SNico Weber //   test to implicitly depend on this.
7673dc3d4SNico Weber // - It avoids requiring `--crash` to be passed to `not`.
8673dc3d4SNico Weber // RUN: APPLE_ASAN_INIT_FOR_DLOPEN=0 %env_asan_opts=abort_on_error=0 not \
9673dc3d4SNico Weber // RUN:   %run %t %shared_libasan 2>&1 | \
10673dc3d4SNico Weber // RUN:   FileCheck -check-prefix=CHECK-DL-OPEN-FAIL %s
11673dc3d4SNico Weber // RUN: env -u APPLE_ASAN_INIT_FOR_DLOPEN %env_asan_opts=abort_on_error=0 not \
12673dc3d4SNico Weber // RUN:   %run %t %shared_libasan 2>&1 | \
13673dc3d4SNico Weber // RUN:   FileCheck -check-prefix=CHECK-DL-OPEN-FAIL %s
14673dc3d4SNico Weber 
15673dc3d4SNico Weber // Check that we can successfully dlopen the ASan dylib when we set the right
16673dc3d4SNico Weber // environment variable.
17673dc3d4SNico Weber // RUN: env APPLE_ASAN_INIT_FOR_DLOPEN=1 %run %t %shared_libasan 2>&1 | \
18673dc3d4SNico Weber // RUN:   FileCheck -check-prefix=CHECK-DL-OPEN-SUCCESS %s
19673dc3d4SNico Weber 
20673dc3d4SNico Weber #include <dlfcn.h>
21673dc3d4SNico Weber #include <stdio.h>
22673dc3d4SNico Weber 
23673dc3d4SNico Weber // CHECK-DL-OPEN-FAIL: ERROR: Interceptors are not working
24*ca50840bSJulian Lettner // CHECK-SAME-DL-OPEN-FAIL: Please launch the executable with: DYLD_INSERT_LIBRARIES={{.+}}/libclang_rt.asan_{{.+}}_dynamic.dylib
25673dc3d4SNico Weber 
main(int argc,char ** argv)26673dc3d4SNico Weber int main(int argc, char **argv) {
27673dc3d4SNico Weber   if (argc != 2) {
28673dc3d4SNico Weber     fprintf(stderr, "Usage: %s <dylib_path>\n", argv[0]);
29673dc3d4SNico Weber     return 1;
30673dc3d4SNico Weber   }
31673dc3d4SNico Weber   const char *dylib_path = argv[1];
32673dc3d4SNico Weber   void *handle = dlopen(dylib_path, RTLD_LAZY);
33673dc3d4SNico Weber   if (!handle) {
34673dc3d4SNico Weber     fprintf(stderr, "Failed to dlopen: %s\n", dlerror());
35673dc3d4SNico Weber     return 1;
36673dc3d4SNico Weber   }
37673dc3d4SNico Weber   // Make sure we can find a function we expect to be in the dylib.
38673dc3d4SNico Weber   void *fn = dlsym(handle, "__sanitizer_mz_size");
39673dc3d4SNico Weber   if (!fn) {
40673dc3d4SNico Weber     fprintf(stderr, "Failed to get symbol: %s\n", dlerror());
41673dc3d4SNico Weber     return 1;
42673dc3d4SNico Weber   }
43673dc3d4SNico Weber   // TODO(dliew): Actually call a function from the dylib that is safe to call.
44673dc3d4SNico Weber   // CHECK-DL-OPEN-SUCCESS: DONE
45673dc3d4SNico Weber   printf("DONE\n");
46673dc3d4SNico Weber   return 0;
47673dc3d4SNico Weber }
48