blob: 9df3b977ea1ba9b431826242775f77013208d1e0 [file] [log] [blame]
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001//===-- 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"
Stephen Hines86277eb2015-03-23 12:06:32 -070019#include "asan_poisoning.h"
20#include "asan_stack.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070021#include "sanitizer_common/sanitizer_flags.h"
22
23namespace __asan {
24
25static struct AsanDeactivatedFlags {
Stephen Hines86277eb2015-03-23 12:06:32 -070026 AllocatorOptions allocator_options;
Stephen Hines2d1fdb22014-05-28 23:58:16 -070027 int malloc_context_size;
28 bool poison_heap;
Stephen Hines86277eb2015-03-23 12:06:32 -070029 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
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080041 RegisterIncludeFlags(parser, cf);
Stephen Hines86277eb2015-03-23 12:06:32 -070042 }
43
44 void OverrideFromActivationFlags() {
45 Flags f;
46 CommonFlags cf;
47 FlagParser parser;
48 RegisterActivationFlags(&parser, &f, &cf);
49
50 // Copy the current activation flags.
51 allocator_options.CopyTo(&f, &cf);
52 cf.malloc_context_size = malloc_context_size;
53 f.poison_heap = poison_heap;
54 cf.coverage = coverage;
55 cf.coverage_dir = coverage_dir;
56 cf.verbosity = Verbosity();
57 cf.help = false; // this is activation-specific help
58
59 // Check if activation flags need to be overriden.
60 if (const char *env = GetEnv("ASAN_ACTIVATION_OPTIONS")) {
61 parser.ParseString(env);
62 }
63
Stephen Hines86277eb2015-03-23 12:06:32 -070064 SetVerbosity(cf.verbosity);
65
66 if (Verbosity()) ReportUnrecognizedFlags();
67
68 if (cf.help) parser.PrintFlagDescriptions();
69
70 allocator_options.SetFrom(&f, &cf);
71 malloc_context_size = cf.malloc_context_size;
72 poison_heap = f.poison_heap;
73 coverage = cf.coverage;
74 coverage_dir = cf.coverage_dir;
75 }
76
77 void Print() {
78 Report(
79 "quarantine_size_mb %d, max_redzone %d, poison_heap %d, "
80 "malloc_context_size %d, alloc_dealloc_mismatch %d, "
81 "allocator_may_return_null %d, coverage %d, coverage_dir %s\n",
82 allocator_options.quarantine_size_mb, allocator_options.max_redzone,
83 poison_heap, malloc_context_size,
84 allocator_options.alloc_dealloc_mismatch,
85 allocator_options.may_return_null, coverage, coverage_dir);
86 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -070087} asan_deactivated_flags;
88
89static bool asan_is_deactivated;
90
Stephen Hines86277eb2015-03-23 12:06:32 -070091void AsanDeactivate() {
92 CHECK(!asan_is_deactivated);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070093 VReport(1, "Deactivating ASan\n");
Stephen Hines2d1fdb22014-05-28 23:58:16 -070094
Stephen Hines86277eb2015-03-23 12:06:32 -070095 // Stash runtime state.
96 GetAllocatorOptions(&asan_deactivated_flags.allocator_options);
97 asan_deactivated_flags.malloc_context_size = GetMallocContextSize();
98 asan_deactivated_flags.poison_heap = CanPoisonMemory();
99 asan_deactivated_flags.coverage = common_flags()->coverage;
100 asan_deactivated_flags.coverage_dir = common_flags()->coverage_dir;
101
102 // Deactivate the runtime.
103 SetCanPoisonMemory(false);
104 SetMallocContextSize(1);
105 ReInitializeCoverage(false, nullptr);
106
107 AllocatorOptions disabled = asan_deactivated_flags.allocator_options;
108 disabled.quarantine_size_mb = 0;
109 disabled.min_redzone = 16; // Redzone must be at least 16 bytes long.
110 disabled.max_redzone = 16;
111 disabled.alloc_dealloc_mismatch = false;
112 disabled.may_return_null = true;
113 ReInitializeAllocator(disabled);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700114
115 asan_is_deactivated = true;
116}
117
118void AsanActivate() {
119 if (!asan_is_deactivated) return;
120 VReport(1, "Activating ASan\n");
121
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800122 UpdateProcessName();
123
Stephen Hines86277eb2015-03-23 12:06:32 -0700124 asan_deactivated_flags.OverrideFromActivationFlags();
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700125
Stephen Hines86277eb2015-03-23 12:06:32 -0700126 SetCanPoisonMemory(asan_deactivated_flags.poison_heap);
127 SetMallocContextSize(asan_deactivated_flags.malloc_context_size);
128 ReInitializeCoverage(asan_deactivated_flags.coverage,
129 asan_deactivated_flags.coverage_dir);
130 ReInitializeAllocator(asan_deactivated_flags.allocator_options);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700131
132 asan_is_deactivated = false;
Stephen Hines86277eb2015-03-23 12:06:32 -0700133 if (Verbosity()) {
134 Report("Activated with flags:\n");
135 asan_deactivated_flags.Print();
136 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700137}
138
139} // namespace __asan