asan_globals.cpp revision 1.1 1 1.1 mrg //===-- asan_globals.cpp --------------------------------------------------===//
2 1.1 mrg //
3 1.1 mrg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 1.1 mrg // See https://llvm.org/LICENSE.txt for license information.
5 1.1 mrg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 1.1 mrg //
7 1.1 mrg //===----------------------------------------------------------------------===//
8 1.1 mrg //
9 1.1 mrg // This file is a part of AddressSanitizer, an address sanity checker.
10 1.1 mrg //
11 1.1 mrg // Handle globals.
12 1.1 mrg //===----------------------------------------------------------------------===//
13 1.1 mrg
14 1.1 mrg #include "asan_interceptors.h"
15 1.1 mrg #include "asan_internal.h"
16 1.1 mrg #include "asan_mapping.h"
17 1.1 mrg #include "asan_poisoning.h"
18 1.1 mrg #include "asan_report.h"
19 1.1 mrg #include "asan_stack.h"
20 1.1 mrg #include "asan_stats.h"
21 1.1 mrg #include "asan_suppressions.h"
22 1.1 mrg #include "asan_thread.h"
23 1.1 mrg #include "sanitizer_common/sanitizer_common.h"
24 1.1 mrg #include "sanitizer_common/sanitizer_mutex.h"
25 1.1 mrg #include "sanitizer_common/sanitizer_placement_new.h"
26 1.1 mrg #include "sanitizer_common/sanitizer_stackdepot.h"
27 1.1 mrg #include "sanitizer_common/sanitizer_symbolizer.h"
28 1.1 mrg
29 1.1 mrg namespace __asan {
30 1.1 mrg
31 1.1 mrg typedef __asan_global Global;
32 1.1 mrg
33 1.1 mrg struct ListOfGlobals {
34 1.1 mrg const Global *g;
35 1.1 mrg ListOfGlobals *next;
36 1.1 mrg };
37 1.1 mrg
38 1.1 mrg static BlockingMutex mu_for_globals(LINKER_INITIALIZED);
39 1.1 mrg static LowLevelAllocator allocator_for_globals;
40 1.1 mrg static ListOfGlobals *list_of_all_globals;
41 1.1 mrg
42 1.1 mrg static const int kDynamicInitGlobalsInitialCapacity = 512;
43 1.1 mrg struct DynInitGlobal {
44 1.1 mrg Global g;
45 1.1 mrg bool initialized;
46 1.1 mrg };
47 1.1 mrg typedef InternalMmapVector<DynInitGlobal> VectorOfGlobals;
48 1.1 mrg // Lazy-initialized and never deleted.
49 1.1 mrg static VectorOfGlobals *dynamic_init_globals;
50 1.1 mrg
51 1.1 mrg // We want to remember where a certain range of globals was registered.
52 1.1 mrg struct GlobalRegistrationSite {
53 1.1 mrg u32 stack_id;
54 1.1 mrg Global *g_first, *g_last;
55 1.1 mrg };
56 1.1 mrg typedef InternalMmapVector<GlobalRegistrationSite> GlobalRegistrationSiteVector;
57 1.1 mrg static GlobalRegistrationSiteVector *global_registration_site_vector;
58 1.1 mrg
59 1.1 mrg ALWAYS_INLINE void PoisonShadowForGlobal(const Global *g, u8 value) {
60 1.1 mrg FastPoisonShadow(g->beg, g->size_with_redzone, value);
61 1.1 mrg }
62 1.1 mrg
63 1.1 mrg ALWAYS_INLINE void PoisonRedZones(const Global &g) {
64 1.1 mrg uptr aligned_size = RoundUpTo(g.size, SHADOW_GRANULARITY);
65 1.1 mrg FastPoisonShadow(g.beg + aligned_size, g.size_with_redzone - aligned_size,
66 1.1 mrg kAsanGlobalRedzoneMagic);
67 1.1 mrg if (g.size != aligned_size) {
68 1.1 mrg FastPoisonShadowPartialRightRedzone(
69 1.1 mrg g.beg + RoundDownTo(g.size, SHADOW_GRANULARITY),
70 1.1 mrg g.size % SHADOW_GRANULARITY,
71 1.1 mrg SHADOW_GRANULARITY,
72 1.1 mrg kAsanGlobalRedzoneMagic);
73 1.1 mrg }
74 1.1 mrg }
75 1.1 mrg
76 1.1 mrg const uptr kMinimalDistanceFromAnotherGlobal = 64;
77 1.1 mrg
78 1.1 mrg static bool IsAddressNearGlobal(uptr addr, const __asan_global &g) {
79 1.1 mrg if (addr <= g.beg - kMinimalDistanceFromAnotherGlobal) return false;
80 1.1 mrg if (addr >= g.beg + g.size_with_redzone) return false;
81 1.1 mrg return true;
82 1.1 mrg }
83 1.1 mrg
84 1.1 mrg static void ReportGlobal(const Global &g, const char *prefix) {
85 1.1 mrg Report(
86 1.1 mrg "%s Global[%p]: beg=%p size=%zu/%zu name=%s module=%s dyn_init=%zu "
87 1.1 mrg "odr_indicator=%p\n",
88 1.1 mrg prefix, &g, (void *)g.beg, g.size, g.size_with_redzone, g.name,
89 1.1 mrg g.module_name, g.has_dynamic_init, (void *)g.odr_indicator);
90 1.1 mrg if (g.location) {
91 1.1 mrg Report(" location (%p): name=%s[%p], %d %d\n", g.location,
92 1.1 mrg g.location->filename, g.location->filename, g.location->line_no,
93 1.1 mrg g.location->column_no);
94 1.1 mrg }
95 1.1 mrg }
96 1.1 mrg
97 1.1 mrg static u32 FindRegistrationSite(const Global *g) {
98 1.1 mrg mu_for_globals.CheckLocked();
99 1.1 mrg CHECK(global_registration_site_vector);
100 1.1 mrg for (uptr i = 0, n = global_registration_site_vector->size(); i < n; i++) {
101 1.1 mrg GlobalRegistrationSite &grs = (*global_registration_site_vector)[i];
102 1.1 mrg if (g >= grs.g_first && g <= grs.g_last)
103 1.1 mrg return grs.stack_id;
104 1.1 mrg }
105 1.1 mrg return 0;
106 1.1 mrg }
107 1.1 mrg
108 1.1 mrg int GetGlobalsForAddress(uptr addr, Global *globals, u32 *reg_sites,
109 1.1 mrg int max_globals) {
110 1.1 mrg if (!flags()->report_globals) return 0;
111 1.1 mrg BlockingMutexLock lock(&mu_for_globals);
112 1.1 mrg int res = 0;
113 1.1 mrg for (ListOfGlobals *l = list_of_all_globals; l; l = l->next) {
114 1.1 mrg const Global &g = *l->g;
115 1.1 mrg if (flags()->report_globals >= 2)
116 1.1 mrg ReportGlobal(g, "Search");
117 1.1 mrg if (IsAddressNearGlobal(addr, g)) {
118 1.1 mrg internal_memcpy(&globals[res], &g, sizeof(g));
119 1.1 mrg if (reg_sites)
120 1.1 mrg reg_sites[res] = FindRegistrationSite(&g);
121 1.1 mrg res++;
122 1.1 mrg if (res == max_globals)
123 1.1 mrg break;
124 1.1 mrg }
125 1.1 mrg }
126 1.1 mrg return res;
127 1.1 mrg }
128 1.1 mrg
129 1.1 mrg enum GlobalSymbolState {
130 1.1 mrg UNREGISTERED = 0,
131 1.1 mrg REGISTERED = 1
132 1.1 mrg };
133 1.1 mrg
134 1.1 mrg // Check ODR violation for given global G via special ODR indicator. We use
135 1.1 mrg // this method in case compiler instruments global variables through their
136 1.1 mrg // local aliases.
137 1.1 mrg static void CheckODRViolationViaIndicator(const Global *g) {
138 1.1 mrg // Instrumentation requests to skip ODR check.
139 1.1 mrg if (g->odr_indicator == UINTPTR_MAX)
140 1.1 mrg return;
141 1.1 mrg u8 *odr_indicator = reinterpret_cast<u8 *>(g->odr_indicator);
142 1.1 mrg if (*odr_indicator == UNREGISTERED) {
143 1.1 mrg *odr_indicator = REGISTERED;
144 1.1 mrg return;
145 1.1 mrg }
146 1.1 mrg // If *odr_indicator is DEFINED, some module have already registered
147 1.1 mrg // externally visible symbol with the same name. This is an ODR violation.
148 1.1 mrg for (ListOfGlobals *l = list_of_all_globals; l; l = l->next) {
149 1.1 mrg if (g->odr_indicator == l->g->odr_indicator &&
150 1.1 mrg (flags()->detect_odr_violation >= 2 || g->size != l->g->size) &&
151 1.1 mrg !IsODRViolationSuppressed(g->name))
152 1.1 mrg ReportODRViolation(g, FindRegistrationSite(g),
153 1.1 mrg l->g, FindRegistrationSite(l->g));
154 1.1 mrg }
155 1.1 mrg }
156 1.1 mrg
157 1.1 mrg // Clang provides two different ways for global variables protection:
158 1.1 mrg // it can poison the global itself or its private alias. In former
159 1.1 mrg // case we may poison same symbol multiple times, that can help us to
160 1.1 mrg // cheaply detect ODR violation: if we try to poison an already poisoned
161 1.1 mrg // global, we have ODR violation error.
162 1.1 mrg // In latter case, we poison each symbol exactly once, so we use special
163 1.1 mrg // indicator symbol to perform similar check.
164 1.1 mrg // In either case, compiler provides a special odr_indicator field to Global
165 1.1 mrg // structure, that can contain two kinds of values:
166 1.1 mrg // 1) Non-zero value. In this case, odr_indicator is an address of
167 1.1 mrg // corresponding indicator variable for given global.
168 1.1 mrg // 2) Zero. This means that we don't use private aliases for global variables
169 1.1 mrg // and can freely check ODR violation with the first method.
170 1.1 mrg //
171 1.1 mrg // This routine chooses between two different methods of ODR violation
172 1.1 mrg // detection.
173 1.1 mrg static inline bool UseODRIndicator(const Global *g) {
174 1.1 mrg return g->odr_indicator > 0;
175 1.1 mrg }
176 1.1 mrg
177 1.1 mrg // Register a global variable.
178 1.1 mrg // This function may be called more than once for every global
179 1.1 mrg // so we store the globals in a map.
180 1.1 mrg static void RegisterGlobal(const Global *g) {
181 1.1 mrg CHECK(asan_inited);
182 1.1 mrg if (flags()->report_globals >= 2)
183 1.1 mrg ReportGlobal(*g, "Added");
184 1.1 mrg CHECK(flags()->report_globals);
185 1.1 mrg CHECK(AddrIsInMem(g->beg));
186 1.1 mrg if (!AddrIsAlignedByGranularity(g->beg)) {
187 1.1 mrg Report("The following global variable is not properly aligned.\n");
188 1.1 mrg Report("This may happen if another global with the same name\n");
189 1.1 mrg Report("resides in another non-instrumented module.\n");
190 1.1 mrg Report("Or the global comes from a C file built w/o -fno-common.\n");
191 1.1 mrg Report("In either case this is likely an ODR violation bug,\n");
192 1.1 mrg Report("but AddressSanitizer can not provide more details.\n");
193 1.1 mrg ReportODRViolation(g, FindRegistrationSite(g), g, FindRegistrationSite(g));
194 1.1 mrg CHECK(AddrIsAlignedByGranularity(g->beg));
195 1.1 mrg }
196 1.1 mrg CHECK(AddrIsAlignedByGranularity(g->size_with_redzone));
197 1.1 mrg if (flags()->detect_odr_violation) {
198 1.1 mrg // Try detecting ODR (One Definition Rule) violation, i.e. the situation
199 1.1 mrg // where two globals with the same name are defined in different modules.
200 1.1 mrg if (UseODRIndicator(g))
201 1.1 mrg CheckODRViolationViaIndicator(g);
202 1.1 mrg }
203 1.1 mrg if (CanPoisonMemory())
204 1.1 mrg PoisonRedZones(*g);
205 1.1 mrg ListOfGlobals *l = new(allocator_for_globals) ListOfGlobals;
206 1.1 mrg l->g = g;
207 1.1 mrg l->next = list_of_all_globals;
208 1.1 mrg list_of_all_globals = l;
209 1.1 mrg if (g->has_dynamic_init) {
210 1.1 mrg if (!dynamic_init_globals) {
211 1.1 mrg dynamic_init_globals = new (allocator_for_globals) VectorOfGlobals;
212 1.1 mrg dynamic_init_globals->reserve(kDynamicInitGlobalsInitialCapacity);
213 1.1 mrg }
214 1.1 mrg DynInitGlobal dyn_global = { *g, false };
215 1.1 mrg dynamic_init_globals->push_back(dyn_global);
216 1.1 mrg }
217 1.1 mrg }
218 1.1 mrg
219 1.1 mrg static void UnregisterGlobal(const Global *g) {
220 1.1 mrg CHECK(asan_inited);
221 1.1 mrg if (flags()->report_globals >= 2)
222 1.1 mrg ReportGlobal(*g, "Removed");
223 1.1 mrg CHECK(flags()->report_globals);
224 1.1 mrg CHECK(AddrIsInMem(g->beg));
225 1.1 mrg CHECK(AddrIsAlignedByGranularity(g->beg));
226 1.1 mrg CHECK(AddrIsAlignedByGranularity(g->size_with_redzone));
227 1.1 mrg if (CanPoisonMemory())
228 1.1 mrg PoisonShadowForGlobal(g, 0);
229 1.1 mrg // We unpoison the shadow memory for the global but we do not remove it from
230 1.1 mrg // the list because that would require O(n^2) time with the current list
231 1.1 mrg // implementation. It might not be worth doing anyway.
232 1.1 mrg
233 1.1 mrg // Release ODR indicator.
234 1.1 mrg if (UseODRIndicator(g) && g->odr_indicator != UINTPTR_MAX) {
235 1.1 mrg u8 *odr_indicator = reinterpret_cast<u8 *>(g->odr_indicator);
236 1.1 mrg *odr_indicator = UNREGISTERED;
237 1.1 mrg }
238 1.1 mrg }
239 1.1 mrg
240 1.1 mrg void StopInitOrderChecking() {
241 1.1 mrg BlockingMutexLock lock(&mu_for_globals);
242 1.1 mrg if (!flags()->check_initialization_order || !dynamic_init_globals)
243 1.1 mrg return;
244 1.1 mrg flags()->check_initialization_order = false;
245 1.1 mrg for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {
246 1.1 mrg DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];
247 1.1 mrg const Global *g = &dyn_g.g;
248 1.1 mrg // Unpoison the whole global.
249 1.1 mrg PoisonShadowForGlobal(g, 0);
250 1.1 mrg // Poison redzones back.
251 1.1 mrg PoisonRedZones(*g);
252 1.1 mrg }
253 1.1 mrg }
254 1.1 mrg
255 1.1 mrg static bool IsASCII(unsigned char c) { return /*0x00 <= c &&*/ c <= 0x7F; }
256 1.1 mrg
257 1.1 mrg const char *MaybeDemangleGlobalName(const char *name) {
258 1.1 mrg // We can spoil names of globals with C linkage, so use an heuristic
259 1.1 mrg // approach to check if the name should be demangled.
260 1.1 mrg bool should_demangle = false;
261 1.1 mrg if (name[0] == '_' && name[1] == 'Z')
262 1.1 mrg should_demangle = true;
263 1.1 mrg else if (SANITIZER_WINDOWS && name[0] == '\01' && name[1] == '?')
264 1.1 mrg should_demangle = true;
265 1.1 mrg
266 1.1 mrg return should_demangle ? Symbolizer::GetOrInit()->Demangle(name) : name;
267 1.1 mrg }
268 1.1 mrg
269 1.1 mrg // Check if the global is a zero-terminated ASCII string. If so, print it.
270 1.1 mrg void PrintGlobalNameIfASCII(InternalScopedString *str, const __asan_global &g) {
271 1.1 mrg for (uptr p = g.beg; p < g.beg + g.size - 1; p++) {
272 1.1 mrg unsigned char c = *(unsigned char *)p;
273 1.1 mrg if (c == '\0' || !IsASCII(c)) return;
274 1.1 mrg }
275 1.1 mrg if (*(char *)(g.beg + g.size - 1) != '\0') return;
276 1.1 mrg str->append(" '%s' is ascii string '%s'\n", MaybeDemangleGlobalName(g.name),
277 1.1 mrg (char *)g.beg);
278 1.1 mrg }
279 1.1 mrg
280 1.1 mrg static const char *GlobalFilename(const __asan_global &g) {
281 1.1 mrg const char *res = g.module_name;
282 1.1 mrg // Prefer the filename from source location, if is available.
283 1.1 mrg if (g.location) res = g.location->filename;
284 1.1 mrg CHECK(res);
285 1.1 mrg return res;
286 1.1 mrg }
287 1.1 mrg
288 1.1 mrg void PrintGlobalLocation(InternalScopedString *str, const __asan_global &g) {
289 1.1 mrg str->append("%s", GlobalFilename(g));
290 1.1 mrg if (!g.location) return;
291 1.1 mrg if (g.location->line_no) str->append(":%d", g.location->line_no);
292 1.1 mrg if (g.location->column_no) str->append(":%d", g.location->column_no);
293 1.1 mrg }
294 1.1 mrg
295 1.1 mrg } // namespace __asan
296 1.1 mrg
297 1.1 mrg // ---------------------- Interface ---------------- {{{1
298 1.1 mrg using namespace __asan;
299 1.1 mrg
300 1.1 mrg // Apply __asan_register_globals to all globals found in the same loaded
301 1.1 mrg // executable or shared library as `flag'. The flag tracks whether globals have
302 1.1 mrg // already been registered or not for this image.
303 1.1 mrg void __asan_register_image_globals(uptr *flag) {
304 1.1 mrg if (*flag)
305 1.1 mrg return;
306 1.1 mrg AsanApplyToGlobals(__asan_register_globals, flag);
307 1.1 mrg *flag = 1;
308 1.1 mrg }
309 1.1 mrg
310 1.1 mrg // This mirrors __asan_register_image_globals.
311 1.1 mrg void __asan_unregister_image_globals(uptr *flag) {
312 1.1 mrg if (!*flag)
313 1.1 mrg return;
314 1.1 mrg AsanApplyToGlobals(__asan_unregister_globals, flag);
315 1.1 mrg *flag = 0;
316 1.1 mrg }
317 1.1 mrg
318 1.1 mrg void __asan_register_elf_globals(uptr *flag, void *start, void *stop) {
319 1.1 mrg if (*flag) return;
320 1.1 mrg if (!start) return;
321 1.1 mrg CHECK_EQ(0, ((uptr)stop - (uptr)start) % sizeof(__asan_global));
322 1.1 mrg __asan_global *globals_start = (__asan_global*)start;
323 1.1 mrg __asan_global *globals_stop = (__asan_global*)stop;
324 1.1 mrg __asan_register_globals(globals_start, globals_stop - globals_start);
325 1.1 mrg *flag = 1;
326 1.1 mrg }
327 1.1 mrg
328 1.1 mrg void __asan_unregister_elf_globals(uptr *flag, void *start, void *stop) {
329 1.1 mrg if (!*flag) return;
330 1.1 mrg if (!start) return;
331 1.1 mrg CHECK_EQ(0, ((uptr)stop - (uptr)start) % sizeof(__asan_global));
332 1.1 mrg __asan_global *globals_start = (__asan_global*)start;
333 1.1 mrg __asan_global *globals_stop = (__asan_global*)stop;
334 1.1 mrg __asan_unregister_globals(globals_start, globals_stop - globals_start);
335 1.1 mrg *flag = 0;
336 1.1 mrg }
337 1.1 mrg
338 1.1 mrg // Register an array of globals.
339 1.1 mrg void __asan_register_globals(__asan_global *globals, uptr n) {
340 1.1 mrg if (!flags()->report_globals) return;
341 1.1 mrg GET_STACK_TRACE_MALLOC;
342 1.1 mrg u32 stack_id = StackDepotPut(stack);
343 1.1 mrg BlockingMutexLock lock(&mu_for_globals);
344 1.1 mrg if (!global_registration_site_vector) {
345 1.1 mrg global_registration_site_vector =
346 1.1 mrg new (allocator_for_globals) GlobalRegistrationSiteVector;
347 1.1 mrg global_registration_site_vector->reserve(128);
348 1.1 mrg }
349 1.1 mrg GlobalRegistrationSite site = {stack_id, &globals[0], &globals[n - 1]};
350 1.1 mrg global_registration_site_vector->push_back(site);
351 1.1 mrg if (flags()->report_globals >= 2) {
352 1.1 mrg PRINT_CURRENT_STACK();
353 1.1 mrg Printf("=== ID %d; %p %p\n", stack_id, &globals[0], &globals[n - 1]);
354 1.1 mrg }
355 1.1 mrg for (uptr i = 0; i < n; i++) {
356 1.1 mrg if (SANITIZER_WINDOWS && globals[i].beg == 0) {
357 1.1 mrg // The MSVC incremental linker may pad globals out to 256 bytes. As long
358 1.1 mrg // as __asan_global is less than 256 bytes large and its size is a power
359 1.1 mrg // of two, we can skip over the padding.
360 1.1 mrg static_assert(
361 1.1 mrg sizeof(__asan_global) < 256 &&
362 1.1 mrg (sizeof(__asan_global) & (sizeof(__asan_global) - 1)) == 0,
363 1.1 mrg "sizeof(__asan_global) incompatible with incremental linker padding");
364 1.1 mrg // If these are padding bytes, the rest of the global should be zero.
365 1.1 mrg CHECK(globals[i].size == 0 && globals[i].size_with_redzone == 0 &&
366 1.1 mrg globals[i].name == nullptr && globals[i].module_name == nullptr &&
367 1.1 mrg globals[i].odr_indicator == 0);
368 1.1 mrg continue;
369 1.1 mrg }
370 1.1 mrg RegisterGlobal(&globals[i]);
371 1.1 mrg }
372 1.1 mrg
373 1.1 mrg // Poison the metadata. It should not be accessible to user code.
374 1.1 mrg PoisonShadow(reinterpret_cast<uptr>(globals), n * sizeof(__asan_global),
375 1.1 mrg kAsanGlobalRedzoneMagic);
376 1.1 mrg }
377 1.1 mrg
378 1.1 mrg // Unregister an array of globals.
379 1.1 mrg // We must do this when a shared objects gets dlclosed.
380 1.1 mrg void __asan_unregister_globals(__asan_global *globals, uptr n) {
381 1.1 mrg if (!flags()->report_globals) return;
382 1.1 mrg BlockingMutexLock lock(&mu_for_globals);
383 1.1 mrg for (uptr i = 0; i < n; i++) {
384 1.1 mrg if (SANITIZER_WINDOWS && globals[i].beg == 0) {
385 1.1 mrg // Skip globals that look like padding from the MSVC incremental linker.
386 1.1 mrg // See comment in __asan_register_globals.
387 1.1 mrg continue;
388 1.1 mrg }
389 1.1 mrg UnregisterGlobal(&globals[i]);
390 1.1 mrg }
391 1.1 mrg
392 1.1 mrg // Unpoison the metadata.
393 1.1 mrg PoisonShadow(reinterpret_cast<uptr>(globals), n * sizeof(__asan_global), 0);
394 1.1 mrg }
395 1.1 mrg
396 1.1 mrg // This method runs immediately prior to dynamic initialization in each TU,
397 1.1 mrg // when all dynamically initialized globals are unpoisoned. This method
398 1.1 mrg // poisons all global variables not defined in this TU, so that a dynamic
399 1.1 mrg // initializer can only touch global variables in the same TU.
400 1.1 mrg void __asan_before_dynamic_init(const char *module_name) {
401 1.1 mrg if (!flags()->check_initialization_order ||
402 1.1 mrg !CanPoisonMemory() ||
403 1.1 mrg !dynamic_init_globals)
404 1.1 mrg return;
405 1.1 mrg bool strict_init_order = flags()->strict_init_order;
406 1.1 mrg CHECK(module_name);
407 1.1 mrg CHECK(asan_inited);
408 1.1 mrg BlockingMutexLock lock(&mu_for_globals);
409 1.1 mrg if (flags()->report_globals >= 3)
410 1.1 mrg Printf("DynInitPoison module: %s\n", module_name);
411 1.1 mrg for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {
412 1.1 mrg DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];
413 1.1 mrg const Global *g = &dyn_g.g;
414 1.1 mrg if (dyn_g.initialized)
415 1.1 mrg continue;
416 1.1 mrg if (g->module_name != module_name)
417 1.1 mrg PoisonShadowForGlobal(g, kAsanInitializationOrderMagic);
418 1.1 mrg else if (!strict_init_order)
419 1.1 mrg dyn_g.initialized = true;
420 1.1 mrg }
421 1.1 mrg }
422 1.1 mrg
423 1.1 mrg // This method runs immediately after dynamic initialization in each TU, when
424 1.1 mrg // all dynamically initialized globals except for those defined in the current
425 1.1 mrg // TU are poisoned. It simply unpoisons all dynamically initialized globals.
426 1.1 mrg void __asan_after_dynamic_init() {
427 1.1 mrg if (!flags()->check_initialization_order ||
428 1.1 mrg !CanPoisonMemory() ||
429 1.1 mrg !dynamic_init_globals)
430 1.1 mrg return;
431 1.1 mrg CHECK(asan_inited);
432 1.1 mrg BlockingMutexLock lock(&mu_for_globals);
433 1.1 mrg // FIXME: Optionally report that we're unpoisoning globals from a module.
434 1.1 mrg for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {
435 1.1 mrg DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];
436 1.1 mrg const Global *g = &dyn_g.g;
437 1.1 mrg if (!dyn_g.initialized) {
438 1.1 mrg // Unpoison the whole global.
439 1.1 mrg PoisonShadowForGlobal(g, 0);
440 1.1 mrg // Poison redzones back.
441 1.1 mrg PoisonRedZones(*g);
442 1.1 mrg }
443 1.1 mrg }
444 1.1 mrg }
445