asan_globals.cc revision dfeef67b23ba92bbee598293164ee20078f633a1
1//===-- asan_globals.cc ---------------------------------------------------===//
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// Handle globals.
13//===----------------------------------------------------------------------===//
14#include "asan_interceptors.h"
15#include "asan_internal.h"
16#include "asan_mapping.h"
17#include "asan_poisoning.h"
18#include "asan_report.h"
19#include "asan_stack.h"
20#include "asan_stats.h"
21#include "asan_thread.h"
22#include "sanitizer_common/sanitizer_common.h"
23#include "sanitizer_common/sanitizer_mutex.h"
24#include "sanitizer_common/sanitizer_placement_new.h"
25
26namespace __asan {
27
28typedef __asan_global Global;
29
30struct ListOfGlobals {
31  const Global *g;
32  ListOfGlobals *next;
33};
34
35static BlockingMutex mu_for_globals(LINKER_INITIALIZED);
36static LowLevelAllocator allocator_for_globals;
37static ListOfGlobals *list_of_all_globals;
38
39static const int kDynamicInitGlobalsInitialCapacity = 512;
40struct DynInitGlobal {
41  Global g;
42  bool initialized;
43};
44typedef InternalVector<DynInitGlobal> VectorOfGlobals;
45// Lazy-initialized and never deleted.
46static VectorOfGlobals *dynamic_init_globals;
47
48ALWAYS_INLINE void PoisonShadowForGlobal(const Global *g, u8 value) {
49  FastPoisonShadow(g->beg, g->size_with_redzone, value);
50}
51
52ALWAYS_INLINE void PoisonRedZones(const Global &g) {
53  uptr aligned_size = RoundUpTo(g.size, SHADOW_GRANULARITY);
54  FastPoisonShadow(g.beg + aligned_size, g.size_with_redzone - aligned_size,
55                   kAsanGlobalRedzoneMagic);
56  if (g.size != aligned_size) {
57    FastPoisonShadowPartialRightRedzone(
58        g.beg + RoundDownTo(g.size, SHADOW_GRANULARITY),
59        g.size % SHADOW_GRANULARITY,
60        SHADOW_GRANULARITY,
61        kAsanGlobalRedzoneMagic);
62  }
63}
64
65static void ReportGlobal(const Global &g, const char *prefix) {
66  Report("%s Global: beg=%p size=%zu/%zu name=%s module=%s dyn_init=%zu\n",
67         prefix, (void*)g.beg, g.size, g.size_with_redzone, g.name,
68         g.module_name, g.has_dynamic_init);
69}
70
71bool DescribeAddressIfGlobal(uptr addr, uptr size) {
72  if (!flags()->report_globals) return false;
73  BlockingMutexLock lock(&mu_for_globals);
74  bool res = false;
75  for (ListOfGlobals *l = list_of_all_globals; l; l = l->next) {
76    const Global &g = *l->g;
77    if (flags()->report_globals >= 2)
78      ReportGlobal(g, "Search");
79    res |= DescribeAddressRelativeToGlobal(addr, size, g);
80  }
81  return res;
82}
83
84// Register a global variable.
85// This function may be called more than once for every global
86// so we store the globals in a map.
87static void RegisterGlobal(const Global *g) {
88  CHECK(asan_inited);
89  if (flags()->report_globals >= 2)
90    ReportGlobal(*g, "Added");
91  CHECK(flags()->report_globals);
92  CHECK(AddrIsInMem(g->beg));
93  CHECK(AddrIsAlignedByGranularity(g->beg));
94  CHECK(AddrIsAlignedByGranularity(g->size_with_redzone));
95  if (flags()->poison_heap)
96    PoisonRedZones(*g);
97  ListOfGlobals *l =
98      (ListOfGlobals*)allocator_for_globals.Allocate(sizeof(ListOfGlobals));
99  l->g = g;
100  l->next = list_of_all_globals;
101  list_of_all_globals = l;
102  if (g->has_dynamic_init) {
103    if (dynamic_init_globals == 0) {
104      void *mem = allocator_for_globals.Allocate(sizeof(VectorOfGlobals));
105      dynamic_init_globals = new(mem)
106          VectorOfGlobals(kDynamicInitGlobalsInitialCapacity);
107    }
108    DynInitGlobal dyn_global = { *g, false };
109    dynamic_init_globals->push_back(dyn_global);
110  }
111}
112
113static void UnregisterGlobal(const Global *g) {
114  CHECK(asan_inited);
115  CHECK(flags()->report_globals);
116  CHECK(AddrIsInMem(g->beg));
117  CHECK(AddrIsAlignedByGranularity(g->beg));
118  CHECK(AddrIsAlignedByGranularity(g->size_with_redzone));
119  if (flags()->poison_heap)
120    PoisonShadowForGlobal(g, 0);
121  // We unpoison the shadow memory for the global but we do not remove it from
122  // the list because that would require O(n^2) time with the current list
123  // implementation. It might not be worth doing anyway.
124}
125
126}  // namespace __asan
127
128// ---------------------- Interface ---------------- {{{1
129using namespace __asan;  // NOLINT
130
131// Register an array of globals.
132void __asan_register_globals(__asan_global *globals, uptr n) {
133  if (!flags()->report_globals) return;
134  BlockingMutexLock lock(&mu_for_globals);
135  for (uptr i = 0; i < n; i++) {
136    RegisterGlobal(&globals[i]);
137  }
138}
139
140// Unregister an array of globals.
141// We must do this when a shared objects gets dlclosed.
142void __asan_unregister_globals(__asan_global *globals, uptr n) {
143  if (!flags()->report_globals) return;
144  BlockingMutexLock lock(&mu_for_globals);
145  for (uptr i = 0; i < n; i++) {
146    UnregisterGlobal(&globals[i]);
147  }
148}
149
150// This method runs immediately prior to dynamic initialization in each TU,
151// when all dynamically initialized globals are unpoisoned.  This method
152// poisons all global variables not defined in this TU, so that a dynamic
153// initializer can only touch global variables in the same TU.
154void __asan_before_dynamic_init(const char *module_name) {
155  if (!flags()->check_initialization_order ||
156      !flags()->poison_heap)
157    return;
158  bool strict_init_order = flags()->strict_init_order;
159  CHECK(dynamic_init_globals);
160  CHECK(module_name);
161  CHECK(asan_inited);
162  BlockingMutexLock lock(&mu_for_globals);
163  if (flags()->report_globals >= 3)
164    Printf("DynInitPoison module: %s\n", module_name);
165  for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {
166    DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];
167    const Global *g = &dyn_g.g;
168    if (dyn_g.initialized)
169      continue;
170    if (g->module_name != module_name)
171      PoisonShadowForGlobal(g, kAsanInitializationOrderMagic);
172    else if (!strict_init_order)
173      dyn_g.initialized = true;
174  }
175}
176
177// This method runs immediately after dynamic initialization in each TU, when
178// all dynamically initialized globals except for those defined in the current
179// TU are poisoned.  It simply unpoisons all dynamically initialized globals.
180void __asan_after_dynamic_init() {
181  if (!flags()->check_initialization_order ||
182      !flags()->poison_heap)
183    return;
184  CHECK(asan_inited);
185  BlockingMutexLock lock(&mu_for_globals);
186  // FIXME: Optionally report that we're unpoisoning globals from a module.
187  for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {
188    DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];
189    const Global *g = &dyn_g.g;
190    if (!dyn_g.initialized) {
191      // Unpoison the whole global.
192      PoisonShadowForGlobal(g, 0);
193      // Poison redzones back.
194      PoisonRedZones(*g);
195    }
196  }
197}
198