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