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