ecd767c5985d0624a66cfddc0dd5947ffea1ef80
[platform/upstream/linaro-gcc.git] / libsanitizer / asan / asan_activation.cc
1 //===-- asan_activation.cc --------------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
10 // ASan activation/deactivation logic.
11 //===----------------------------------------------------------------------===//
12
13 #include "asan_activation.h"
14 #include "asan_allocator.h"
15 #include "asan_flags.h"
16 #include "asan_internal.h"
17 #include "asan_poisoning.h"
18 #include "asan_stack.h"
19 #include "sanitizer_common/sanitizer_flags.h"
20
21 namespace __asan {
22
23 static struct AsanDeactivatedFlags {
24   AllocatorOptions allocator_options;
25   int malloc_context_size;
26   bool poison_heap;
27   bool coverage;
28   const char *coverage_dir;
29
30   void RegisterActivationFlags(FlagParser *parser, Flags *f, CommonFlags *cf) {
31 #define ASAN_ACTIVATION_FLAG(Type, Name) \
32   RegisterFlag(parser, #Name, "", &f->Name);
33 #define COMMON_ACTIVATION_FLAG(Type, Name) \
34   RegisterFlag(parser, #Name, "", &cf->Name);
35 #include "asan_activation_flags.inc"
36 #undef ASAN_ACTIVATION_FLAG
37 #undef COMMON_ACTIVATION_FLAG
38
39     RegisterIncludeFlags(parser, cf);
40   }
41
42   void OverrideFromActivationFlags() {
43     Flags f;
44     CommonFlags cf;
45     FlagParser parser;
46     RegisterActivationFlags(&parser, &f, &cf);
47
48     cf.SetDefaults();
49     // Copy the current activation flags.
50     allocator_options.CopyTo(&f, &cf);
51     cf.malloc_context_size = malloc_context_size;
52     f.poison_heap = poison_heap;
53     cf.coverage = coverage;
54     cf.coverage_dir = coverage_dir;
55     cf.verbosity = Verbosity();
56     cf.help = false; // this is activation-specific help
57
58     // Check if activation flags need to be overriden.
59     if (const char *env = GetEnv("ASAN_ACTIVATION_OPTIONS")) {
60       parser.ParseString(env);
61     }
62
63     InitializeCommonFlags(&cf);
64
65     if (Verbosity()) ReportUnrecognizedFlags();
66
67     if (cf.help) parser.PrintFlagDescriptions();
68
69     allocator_options.SetFrom(&f, &cf);
70     malloc_context_size = cf.malloc_context_size;
71     poison_heap = f.poison_heap;
72     coverage = cf.coverage;
73     coverage_dir = cf.coverage_dir;
74   }
75
76   void Print() {
77     Report(
78         "quarantine_size_mb %d, max_redzone %d, poison_heap %d, "
79         "malloc_context_size %d, alloc_dealloc_mismatch %d, "
80         "allocator_may_return_null %d, coverage %d, coverage_dir %s\n",
81         allocator_options.quarantine_size_mb, allocator_options.max_redzone,
82         poison_heap, malloc_context_size,
83         allocator_options.alloc_dealloc_mismatch,
84         allocator_options.may_return_null, coverage, coverage_dir);
85   }
86 } asan_deactivated_flags;
87
88 static bool asan_is_deactivated;
89
90 void AsanDeactivate() {
91   CHECK(!asan_is_deactivated);
92   VReport(1, "Deactivating ASan\n");
93
94   // Stash runtime state.
95   GetAllocatorOptions(&asan_deactivated_flags.allocator_options);
96   asan_deactivated_flags.malloc_context_size = GetMallocContextSize();
97   asan_deactivated_flags.poison_heap = CanPoisonMemory();
98   asan_deactivated_flags.coverage = common_flags()->coverage;
99   asan_deactivated_flags.coverage_dir = common_flags()->coverage_dir;
100
101   // Deactivate the runtime.
102   SetCanPoisonMemory(false);
103   SetMallocContextSize(1);
104   ReInitializeCoverage(false, nullptr);
105
106   AllocatorOptions disabled = asan_deactivated_flags.allocator_options;
107   disabled.quarantine_size_mb = 0;
108   disabled.min_redzone = 16;  // Redzone must be at least 16 bytes long.
109   disabled.max_redzone = 16;
110   disabled.alloc_dealloc_mismatch = false;
111   disabled.may_return_null = true;
112   ReInitializeAllocator(disabled);
113
114   asan_is_deactivated = true;
115 }
116
117 void AsanActivate() {
118   if (!asan_is_deactivated) return;
119   VReport(1, "Activating ASan\n");
120
121   UpdateProcessName();
122
123   asan_deactivated_flags.OverrideFromActivationFlags();
124
125   SetCanPoisonMemory(asan_deactivated_flags.poison_heap);
126   SetMallocContextSize(asan_deactivated_flags.malloc_context_size);
127   ReInitializeCoverage(asan_deactivated_flags.coverage,
128                        asan_deactivated_flags.coverage_dir);
129   ReInitializeAllocator(asan_deactivated_flags.allocator_options);
130
131   asan_is_deactivated = false;
132   if (Verbosity()) {
133     Report("Activated with flags:\n");
134     asan_deactivated_flags.Print();
135   }
136 }
137
138 }  // namespace __asan