xref: /llvm-project/compiler-rt/test/asan/TestCases/Linux/activation-options.cpp (revision 895878f4568d3c5fe470bd811c2dfdbbe285e5aa)
1673dc3d4SNico Weber // Test for ASAN_OPTIONS=start_deactivated=1 mode.
2673dc3d4SNico Weber // Main executable is uninstrumented, but linked to ASan runtime. The shared
3673dc3d4SNico Weber // library is instrumented.
4*895878f4SAlex Richardson // Fails with debug checks: https://bugs.llvm.org/show_bug.cgi?id=46862
5*895878f4SAlex Richardson // XFAIL: !compiler-rt-optimized
6673dc3d4SNico Weber 
7673dc3d4SNico Weber // RUN: %clangxx_asan -O0 -DSHARED_LIB %s -fPIC -shared -o %t-so.so
8673dc3d4SNico Weber // RUN: %clangxx -O0 %s -c -o %t.o
9673dc3d4SNico Weber // RUN: %clangxx_asan -O0 %t.o %libdl -o %t
10673dc3d4SNico Weber 
11673dc3d4SNico Weber // RUN: rm -f %t.asan.options.activation-options.cpp.tmp
12673dc3d4SNico Weber // RUN: rm -f %t.asan.options.ABCDE
13673dc3d4SNico Weber // RUN: echo "help=1" >%t.asan.options.activation-options.cpp.tmp
14673dc3d4SNico Weber 
15673dc3d4SNico Weber // RUN: %env_asan_opts=start_deactivated=1 \
16673dc3d4SNico Weber // RUN:   ASAN_ACTIVATION_OPTIONS=include=%t.asan.options.%b %run %t 2>&1 | \
17673dc3d4SNico Weber // RUN:   FileCheck %s --check-prefix=CHECK-HELP --check-prefix=CHECK-FOUND
18673dc3d4SNico Weber 
19673dc3d4SNico Weber // RUN: %env_asan_opts=start_deactivated=1 \
20673dc3d4SNico Weber // RUN:   ASAN_ACTIVATION_OPTIONS=include=%t.asan.options not %run %t 2>&1 | \
21673dc3d4SNico Weber // RUN:   FileCheck %s --check-prefix=CHECK-NO-HELP --check-prefix=CHECK-MISSING
22673dc3d4SNico Weber 
23673dc3d4SNico Weber // RUN: %env_asan_opts=start_deactivated=1 \
24673dc3d4SNico Weber // RUN:   ASAN_ACTIVATION_OPTIONS=include=%t.asan.options.%b not %run %t --fix-name 2>&1 | \
25673dc3d4SNico Weber // RUN:   FileCheck %s --check-prefix=CHECK-NO-HELP --check-prefix=CHECK-MISSING
26673dc3d4SNico Weber 
27673dc3d4SNico Weber // RUN: echo "help=1" >%t.asan.options.ABCDE
28673dc3d4SNico Weber 
29673dc3d4SNico Weber // RUN: %env_asan_opts=start_deactivated=1 \
30673dc3d4SNico Weber // RUN:   ASAN_ACTIVATION_OPTIONS=include=%t.asan.options.%b %run %t --fix-name 2>&1 | \
31673dc3d4SNico Weber // RUN:   FileCheck %s --check-prefix=CHECK-HELP --check-prefix=CHECK-FOUND
32673dc3d4SNico Weber 
33673dc3d4SNico Weber // XFAIL: android
34673dc3d4SNico Weber 
35673dc3d4SNico Weber #if !defined(SHARED_LIB)
36673dc3d4SNico Weber #include <assert.h>
37673dc3d4SNico Weber #include <dlfcn.h>
38673dc3d4SNico Weber #include <stdio.h>
39673dc3d4SNico Weber #include <stdlib.h>
40673dc3d4SNico Weber #include <string.h>
41673dc3d4SNico Weber #include <unistd.h>
42673dc3d4SNico Weber 
43673dc3d4SNico Weber #include <string>
44673dc3d4SNico Weber 
45673dc3d4SNico Weber #include "sanitizer/asan_interface.h"
46673dc3d4SNico Weber 
47673dc3d4SNico Weber typedef void (*Fn)();
48673dc3d4SNico Weber 
main(int argc,char * argv[])49673dc3d4SNico Weber int main(int argc, char *argv[]) {
50673dc3d4SNico Weber   std::string path = std::string(argv[0]) + "-so.so";
51673dc3d4SNico Weber 
52673dc3d4SNico Weber   if (argc > 1 && strcmp(argv[1], "--fix-name") == 0) {
53673dc3d4SNico Weber     assert(strlen(argv[0]) > 5);
54673dc3d4SNico Weber     strcpy(argv[0], "ABCDE");
55673dc3d4SNico Weber   }
56673dc3d4SNico Weber 
57673dc3d4SNico Weber   void *dso = dlopen(path.c_str(), RTLD_NOW);
58673dc3d4SNico Weber   if (!dso) {
59673dc3d4SNico Weber     fprintf(stderr, "dlopen failed: %s\n", dlerror());
60673dc3d4SNico Weber     return 1;
61673dc3d4SNico Weber   }
62673dc3d4SNico Weber 
63673dc3d4SNico Weber   return 0;
64673dc3d4SNico Weber }
65673dc3d4SNico Weber #else  // SHARED_LIB
66673dc3d4SNico Weber // Empty: all we need is an ASan shared library constructor.
67673dc3d4SNico Weber #endif  // SHARED_LIB
68673dc3d4SNico Weber 
69673dc3d4SNico Weber // CHECK-HELP: Available flags for {{.*}}Sanitizer:
70673dc3d4SNico Weber // CHECK-NO-HELP-NOT: Available flags for {{.*}}Sanitizer:
71673dc3d4SNico Weber // CHECK-FOUND-NOT: Failed to read options
72673dc3d4SNico Weber // CHECK-MISSING: Failed to read options
73