]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/asan/asan_globals.cc
Import libc++ trunk r224926. This fixes a number of bugs, completes
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / asan / asan_globals.cc
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 #include "sanitizer_common/sanitizer_stackdepot.h"
26
27 namespace __asan {
28
29 typedef __asan_global Global;
30
31 struct ListOfGlobals {
32   const Global *g;
33   ListOfGlobals *next;
34 };
35
36 static BlockingMutex mu_for_globals(LINKER_INITIALIZED);
37 static LowLevelAllocator allocator_for_globals;
38 static ListOfGlobals *list_of_all_globals;
39
40 static const int kDynamicInitGlobalsInitialCapacity = 512;
41 struct DynInitGlobal {
42   Global g;
43   bool initialized;
44 };
45 typedef InternalMmapVector<DynInitGlobal> VectorOfGlobals;
46 // Lazy-initialized and never deleted.
47 static VectorOfGlobals *dynamic_init_globals;
48
49 // We want to remember where a certain range of globals was registered.
50 struct GlobalRegistrationSite {
51   u32 stack_id;
52   Global *g_first, *g_last;
53 };
54 typedef InternalMmapVector<GlobalRegistrationSite> GlobalRegistrationSiteVector;
55 static GlobalRegistrationSiteVector *global_registration_site_vector;
56
57 ALWAYS_INLINE void PoisonShadowForGlobal(const Global *g, u8 value) {
58   FastPoisonShadow(g->beg, g->size_with_redzone, value);
59 }
60
61 ALWAYS_INLINE void PoisonRedZones(const Global &g) {
62   uptr aligned_size = RoundUpTo(g.size, SHADOW_GRANULARITY);
63   FastPoisonShadow(g.beg + aligned_size, g.size_with_redzone - aligned_size,
64                    kAsanGlobalRedzoneMagic);
65   if (g.size != aligned_size) {
66     FastPoisonShadowPartialRightRedzone(
67         g.beg + RoundDownTo(g.size, SHADOW_GRANULARITY),
68         g.size % SHADOW_GRANULARITY,
69         SHADOW_GRANULARITY,
70         kAsanGlobalRedzoneMagic);
71   }
72 }
73
74 const uptr kMinimalDistanceFromAnotherGlobal = 64;
75
76 bool IsAddressNearGlobal(uptr addr, const __asan_global &g) {
77   if (addr <= g.beg - kMinimalDistanceFromAnotherGlobal) return false;
78   if (addr >= g.beg + g.size_with_redzone) return false;
79   return true;
80 }
81
82 static void ReportGlobal(const Global &g, const char *prefix) {
83   Report("%s Global[%p]: beg=%p size=%zu/%zu name=%s module=%s dyn_init=%zu\n",
84          prefix, &g, (void *)g.beg, g.size, g.size_with_redzone, g.name,
85          g.module_name, g.has_dynamic_init);
86   if (g.location) {
87     Report("  location (%p): name=%s[%p], %d %d\n", g.location,
88            g.location->filename, g.location->filename, g.location->line_no,
89            g.location->column_no);
90   }
91 }
92
93 static bool DescribeOrGetInfoIfGlobal(uptr addr, uptr size, bool print,
94                                       Global *output_global) {
95   if (!flags()->report_globals) return false;
96   BlockingMutexLock lock(&mu_for_globals);
97   bool res = false;
98   for (ListOfGlobals *l = list_of_all_globals; l; l = l->next) {
99     const Global &g = *l->g;
100     if (print) {
101       if (flags()->report_globals >= 2)
102         ReportGlobal(g, "Search");
103       res |= DescribeAddressRelativeToGlobal(addr, size, g);
104     } else {
105       if (IsAddressNearGlobal(addr, g)) {
106         CHECK(output_global);
107         *output_global = g;
108         return true;
109       }
110     }
111   }
112   return res;
113 }
114
115 bool DescribeAddressIfGlobal(uptr addr, uptr size) {
116   return DescribeOrGetInfoIfGlobal(addr, size, /* print */ true,
117                                    /* output_global */ nullptr);
118 }
119
120 bool GetInfoForAddressIfGlobal(uptr addr, AddressDescription *descr) {
121   Global g = {};
122   if (DescribeOrGetInfoIfGlobal(addr, /* size */ 1, /* print */ false, &g)) {
123     internal_strncpy(descr->name, g.name, descr->name_size);
124     descr->region_address = g.beg;
125     descr->region_size = g.size;
126     descr->region_kind = "global";
127     return true;
128   }
129   return false;
130 }
131
132 u32 FindRegistrationSite(const Global *g) {
133   CHECK(global_registration_site_vector);
134   for (uptr i = 0, n = global_registration_site_vector->size(); i < n; i++) {
135     GlobalRegistrationSite &grs = (*global_registration_site_vector)[i];
136     if (g >= grs.g_first && g <= grs.g_last)
137       return grs.stack_id;
138   }
139   return 0;
140 }
141
142 // Register a global variable.
143 // This function may be called more than once for every global
144 // so we store the globals in a map.
145 static void RegisterGlobal(const Global *g) {
146   CHECK(asan_inited);
147   if (flags()->report_globals >= 2)
148     ReportGlobal(*g, "Added");
149   CHECK(flags()->report_globals);
150   CHECK(AddrIsInMem(g->beg));
151   CHECK(AddrIsAlignedByGranularity(g->beg));
152   CHECK(AddrIsAlignedByGranularity(g->size_with_redzone));
153   if (flags()->detect_odr_violation) {
154     // Try detecting ODR (One Definition Rule) violation, i.e. the situation
155     // where two globals with the same name are defined in different modules.
156     if (__asan_region_is_poisoned(g->beg, g->size_with_redzone)) {
157       // This check may not be enough: if the first global is much larger
158       // the entire redzone of the second global may be within the first global.
159       for (ListOfGlobals *l = list_of_all_globals; l; l = l->next) {
160         if (g->beg == l->g->beg &&
161             (flags()->detect_odr_violation >= 2 || g->size != l->g->size))
162           ReportODRViolation(g, FindRegistrationSite(g),
163                              l->g, FindRegistrationSite(l->g));
164       }
165     }
166   }
167   if (flags()->poison_heap)
168     PoisonRedZones(*g);
169   ListOfGlobals *l = new(allocator_for_globals) ListOfGlobals;
170   l->g = g;
171   l->next = list_of_all_globals;
172   list_of_all_globals = l;
173   if (g->has_dynamic_init) {
174     if (dynamic_init_globals == 0) {
175       dynamic_init_globals = new(allocator_for_globals)
176           VectorOfGlobals(kDynamicInitGlobalsInitialCapacity);
177     }
178     DynInitGlobal dyn_global = { *g, false };
179     dynamic_init_globals->push_back(dyn_global);
180   }
181 }
182
183 static void UnregisterGlobal(const Global *g) {
184   CHECK(asan_inited);
185   CHECK(flags()->report_globals);
186   CHECK(AddrIsInMem(g->beg));
187   CHECK(AddrIsAlignedByGranularity(g->beg));
188   CHECK(AddrIsAlignedByGranularity(g->size_with_redzone));
189   if (flags()->poison_heap)
190     PoisonShadowForGlobal(g, 0);
191   // We unpoison the shadow memory for the global but we do not remove it from
192   // the list because that would require O(n^2) time with the current list
193   // implementation. It might not be worth doing anyway.
194 }
195
196 void StopInitOrderChecking() {
197   BlockingMutexLock lock(&mu_for_globals);
198   if (!flags()->check_initialization_order || !dynamic_init_globals)
199     return;
200   flags()->check_initialization_order = false;
201   for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {
202     DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];
203     const Global *g = &dyn_g.g;
204     // Unpoison the whole global.
205     PoisonShadowForGlobal(g, 0);
206     // Poison redzones back.
207     PoisonRedZones(*g);
208   }
209 }
210
211 }  // namespace __asan
212
213 // ---------------------- Interface ---------------- {{{1
214 using namespace __asan;  // NOLINT
215
216 // Register an array of globals.
217 void __asan_register_globals(__asan_global *globals, uptr n) {
218   if (!flags()->report_globals) return;
219   GET_STACK_TRACE_FATAL_HERE;
220   u32 stack_id = StackDepotPut(stack);
221   BlockingMutexLock lock(&mu_for_globals);
222   if (!global_registration_site_vector)
223     global_registration_site_vector =
224         new(allocator_for_globals) GlobalRegistrationSiteVector(128);
225   GlobalRegistrationSite site = {stack_id, &globals[0], &globals[n - 1]};
226   global_registration_site_vector->push_back(site);
227   if (flags()->report_globals >= 2) {
228     PRINT_CURRENT_STACK();
229     Printf("=== ID %d; %p %p\n", stack_id, &globals[0], &globals[n - 1]);
230   }
231   for (uptr i = 0; i < n; i++) {
232     RegisterGlobal(&globals[i]);
233   }
234 }
235
236 // Unregister an array of globals.
237 // We must do this when a shared objects gets dlclosed.
238 void __asan_unregister_globals(__asan_global *globals, uptr n) {
239   if (!flags()->report_globals) return;
240   BlockingMutexLock lock(&mu_for_globals);
241   for (uptr i = 0; i < n; i++) {
242     UnregisterGlobal(&globals[i]);
243   }
244 }
245
246 // This method runs immediately prior to dynamic initialization in each TU,
247 // when all dynamically initialized globals are unpoisoned.  This method
248 // poisons all global variables not defined in this TU, so that a dynamic
249 // initializer can only touch global variables in the same TU.
250 void __asan_before_dynamic_init(const char *module_name) {
251   if (!flags()->check_initialization_order ||
252       !flags()->poison_heap)
253     return;
254   bool strict_init_order = flags()->strict_init_order;
255   CHECK(dynamic_init_globals);
256   CHECK(module_name);
257   CHECK(asan_inited);
258   BlockingMutexLock lock(&mu_for_globals);
259   if (flags()->report_globals >= 3)
260     Printf("DynInitPoison module: %s\n", module_name);
261   for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {
262     DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];
263     const Global *g = &dyn_g.g;
264     if (dyn_g.initialized)
265       continue;
266     if (g->module_name != module_name)
267       PoisonShadowForGlobal(g, kAsanInitializationOrderMagic);
268     else if (!strict_init_order)
269       dyn_g.initialized = true;
270   }
271 }
272
273 // This method runs immediately after dynamic initialization in each TU, when
274 // all dynamically initialized globals except for those defined in the current
275 // TU are poisoned.  It simply unpoisons all dynamically initialized globals.
276 void __asan_after_dynamic_init() {
277   if (!flags()->check_initialization_order ||
278       !flags()->poison_heap)
279     return;
280   CHECK(asan_inited);
281   BlockingMutexLock lock(&mu_for_globals);
282   // FIXME: Optionally report that we're unpoisoning globals from a module.
283   for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {
284     DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];
285     const Global *g = &dyn_g.g;
286     if (!dyn_g.initialized) {
287       // Unpoison the whole global.
288       PoisonShadowForGlobal(g, 0);
289       // Poison redzones back.
290       PoisonRedZones(*g);
291     }
292   }
293 }