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