1 //===-- asan_activation.cc --------------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
10 // ASan activation/deactivation logic.
11 //===----------------------------------------------------------------------===//
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"
23 static struct AsanDeactivatedFlags
{
24 AllocatorOptions allocator_options
;
25 int malloc_context_size
;
28 const char *coverage_dir
;
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
39 RegisterIncludeFlags(parser
, cf
);
42 void OverrideFromActivationFlags() {
46 RegisterActivationFlags(&parser
, &f
, &cf
);
48 // Copy the current activation flags.
49 allocator_options
.CopyTo(&f
, &cf
);
50 cf
.malloc_context_size
= malloc_context_size
;
51 f
.poison_heap
= poison_heap
;
52 cf
.coverage
= coverage
;
53 cf
.coverage_dir
= coverage_dir
;
54 cf
.verbosity
= Verbosity();
55 cf
.help
= false; // this is activation-specific help
57 // Check if activation flags need to be overriden.
58 if (const char *env
= GetEnv("ASAN_ACTIVATION_OPTIONS")) {
59 parser
.ParseString(env
);
62 // Override from getprop asan.options.
64 GetExtraActivationFlags(buf
, sizeof(buf
));
65 parser
.ParseString(buf
);
67 SetVerbosity(cf
.verbosity
);
69 if (Verbosity()) ReportUnrecognizedFlags();
71 if (cf
.help
) parser
.PrintFlagDescriptions();
73 allocator_options
.SetFrom(&f
, &cf
);
74 malloc_context_size
= cf
.malloc_context_size
;
75 poison_heap
= f
.poison_heap
;
76 coverage
= cf
.coverage
;
77 coverage_dir
= cf
.coverage_dir
;
82 "quarantine_size_mb %d, max_redzone %d, poison_heap %d, "
83 "malloc_context_size %d, alloc_dealloc_mismatch %d, "
84 "allocator_may_return_null %d, coverage %d, coverage_dir %s\n",
85 allocator_options
.quarantine_size_mb
, allocator_options
.max_redzone
,
86 poison_heap
, malloc_context_size
,
87 allocator_options
.alloc_dealloc_mismatch
,
88 allocator_options
.may_return_null
, coverage
, coverage_dir
);
90 } asan_deactivated_flags
;
92 static bool asan_is_deactivated
;
94 void AsanDeactivate() {
95 CHECK(!asan_is_deactivated
);
96 VReport(1, "Deactivating ASan\n");
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
;
105 // Deactivate the runtime.
106 SetCanPoisonMemory(false);
107 SetMallocContextSize(1);
108 ReInitializeCoverage(false, nullptr);
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
);
118 asan_is_deactivated
= true;
121 void AsanActivate() {
122 if (!asan_is_deactivated
) return;
123 VReport(1, "Activating ASan\n");
127 asan_deactivated_flags
.OverrideFromActivationFlags();
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
);
135 asan_is_deactivated
= false;
137 Report("Activated with flags:\n");
138 asan_deactivated_flags
.Print();
142 } // namespace __asan