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 "sanitizer_common/sanitizer_flags.h"
20
21namespace __asan {
22
23static struct AsanDeactivatedFlags {
24  int quarantine_size;
25  int max_redzone;
26  int malloc_context_size;
27  bool poison_heap;
28} asan_deactivated_flags;
29
30static bool asan_is_deactivated;
31
32void AsanStartDeactivated() {
33  VReport(1, "Deactivating ASan\n");
34  // Save flag values.
35  asan_deactivated_flags.quarantine_size = flags()->quarantine_size;
36  asan_deactivated_flags.max_redzone = flags()->max_redzone;
37  asan_deactivated_flags.poison_heap = flags()->poison_heap;
38  asan_deactivated_flags.malloc_context_size =
39      common_flags()->malloc_context_size;
40
41  flags()->quarantine_size = 0;
42  flags()->max_redzone = 16;
43  flags()->poison_heap = false;
44  common_flags()->malloc_context_size = 0;
45
46  asan_is_deactivated = true;
47}
48
49void AsanActivate() {
50  if (!asan_is_deactivated) return;
51  VReport(1, "Activating ASan\n");
52
53  // Restore flag values.
54  // FIXME: this is not atomic, and there may be other threads alive.
55  flags()->quarantine_size = asan_deactivated_flags.quarantine_size;
56  flags()->max_redzone = asan_deactivated_flags.max_redzone;
57  flags()->poison_heap = asan_deactivated_flags.poison_heap;
58  common_flags()->malloc_context_size =
59      asan_deactivated_flags.malloc_context_size;
60
61  ParseExtraActivationFlags();
62
63  ReInitializeAllocator();
64
65  asan_is_deactivated = false;
66  VReport(
67      1,
68      "quarantine_size %d, max_redzone %d, poison_heap %d, malloc_context_size "
69      "%d\n",
70      flags()->quarantine_size, flags()->max_redzone, flags()->poison_heap,
71      common_flags()->malloc_context_size);
72}
73
74}  // namespace __asan
75