1 // Test for ASAN_OPTIONS=start_deactivated=1 mode.
2 // Main executable is uninstrumented, but linked to ASan runtime. The shared
3 // library is instrumented.
4 
5 // RUN: %clangxx_asan -O0 -DSHARED_LIB %s -fPIC -shared -o %t-so.so
6 // RUN: %clangxx -O0 %s -c -o %t.o
7 // RUN: %clangxx_asan -O0 %t.o %libdl -o %t
8 
9 // RUN: rm -f %t.asan.options.activation-options.cpp.tmp
10 // RUN: rm -f %t.asan.options.ABCDE
11 // RUN: echo "help=1" >%t.asan.options.activation-options.cpp.tmp
12 
13 // RUN: %env_asan_opts=start_deactivated=1 \
14 // RUN:   ASAN_ACTIVATION_OPTIONS=include=%t.asan.options.%b %run %t 2>&1 | \
15 // RUN:   FileCheck %s --check-prefix=CHECK-HELP --check-prefix=CHECK-FOUND
16 
17 // RUN: %env_asan_opts=start_deactivated=1 \
18 // RUN:   ASAN_ACTIVATION_OPTIONS=include=%t.asan.options not %run %t 2>&1 | \
19 // RUN:   FileCheck %s --check-prefix=CHECK-NO-HELP --check-prefix=CHECK-MISSING
20 
21 // RUN: %env_asan_opts=start_deactivated=1 \
22 // RUN:   ASAN_ACTIVATION_OPTIONS=include=%t.asan.options.%b not %run %t --fix-name 2>&1 | \
23 // RUN:   FileCheck %s --check-prefix=CHECK-NO-HELP --check-prefix=CHECK-MISSING
24 
25 // RUN: echo "help=1" >%t.asan.options.ABCDE
26 
27 // RUN: %env_asan_opts=start_deactivated=1 \
28 // RUN:   ASAN_ACTIVATION_OPTIONS=include=%t.asan.options.%b %run %t --fix-name 2>&1 | \
29 // RUN:   FileCheck %s --check-prefix=CHECK-HELP --check-prefix=CHECK-FOUND
30 
31 // XFAIL: android
32 
33 #if !defined(SHARED_LIB)
34 #include <assert.h>
35 #include <dlfcn.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include <string>
42 
43 #include "sanitizer/asan_interface.h"
44 
45 typedef void (*Fn)();
46 
47 int main(int argc, char *argv[]) {
48   std::string path = std::string(argv[0]) + "-so.so";
49 
50   if (argc > 1 && strcmp(argv[1], "--fix-name") == 0) {
51     assert(strlen(argv[0]) > 5);
52     strcpy(argv[0], "ABCDE");
53   }
54 
55   void *dso = dlopen(path.c_str(), RTLD_NOW);
56   if (!dso) {
57     fprintf(stderr, "dlopen failed: %s\n", dlerror());
58     return 1;
59   }
60 
61   return 0;
62 }
63 #else  // SHARED_LIB
64 // Empty: all we need is an ASan shared library constructor.
65 #endif  // SHARED_LIB
66 
67 // CHECK-HELP: Available flags for {{.*}}Sanitizer:
68 // CHECK-NO-HELP-NOT: Available flags for {{.*}}Sanitizer:
69 // CHECK-FOUND-NOT: Failed to read options
70 // CHECK-MISSING: Failed to read options
71