Path: blob/main/contrib/llvm-project/compiler-rt/lib/asan/asan_globals.cpp
35233 views
//===-- asan_globals.cpp --------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of AddressSanitizer, an address sanity checker.9//10// Handle globals.11//===----------------------------------------------------------------------===//1213#include "asan_interceptors.h"14#include "asan_internal.h"15#include "asan_mapping.h"16#include "asan_poisoning.h"17#include "asan_report.h"18#include "asan_stack.h"19#include "asan_stats.h"20#include "asan_suppressions.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#include "sanitizer_common/sanitizer_symbolizer.h"2728namespace __asan {2930typedef __asan_global Global;3132struct ListOfGlobals {33const Global *g;34ListOfGlobals *next;35};3637static Mutex mu_for_globals;38static ListOfGlobals *list_of_all_globals;3940static const int kDynamicInitGlobalsInitialCapacity = 512;41struct DynInitGlobal {42Global g;43bool initialized;44};45typedef InternalMmapVector<DynInitGlobal> VectorOfGlobals;46// Lazy-initialized and never deleted.47static VectorOfGlobals *dynamic_init_globals;4849// We want to remember where a certain range of globals was registered.50struct GlobalRegistrationSite {51u32 stack_id;52Global *g_first, *g_last;53};54typedef InternalMmapVector<GlobalRegistrationSite> GlobalRegistrationSiteVector;55static GlobalRegistrationSiteVector *global_registration_site_vector;5657ALWAYS_INLINE void PoisonShadowForGlobal(const Global *g, u8 value) {58FastPoisonShadow(g->beg, g->size_with_redzone, value);59}6061ALWAYS_INLINE void PoisonRedZones(const Global &g) {62uptr aligned_size = RoundUpTo(g.size, ASAN_SHADOW_GRANULARITY);63FastPoisonShadow(g.beg + aligned_size, g.size_with_redzone - aligned_size,64kAsanGlobalRedzoneMagic);65if (g.size != aligned_size) {66FastPoisonShadowPartialRightRedzone(67g.beg + RoundDownTo(g.size, ASAN_SHADOW_GRANULARITY),68g.size % ASAN_SHADOW_GRANULARITY, ASAN_SHADOW_GRANULARITY,69kAsanGlobalRedzoneMagic);70}71}7273const uptr kMinimalDistanceFromAnotherGlobal = 64;7475static bool IsAddressNearGlobal(uptr addr, const __asan_global &g) {76if (addr <= g.beg - kMinimalDistanceFromAnotherGlobal) return false;77if (addr >= g.beg + g.size_with_redzone) return false;78return true;79}8081static void ReportGlobal(const Global &g, const char *prefix) {82DataInfo info;83bool symbolized = Symbolizer::GetOrInit()->SymbolizeData(g.beg, &info);84Report(85"%s Global[%p]: beg=%p size=%zu/%zu name=%s source=%s module=%s "86"dyn_init=%zu "87"odr_indicator=%p\n",88prefix, (void *)&g, (void *)g.beg, g.size, g.size_with_redzone, g.name,89g.module_name, (symbolized ? info.module : "?"), g.has_dynamic_init,90(void *)g.odr_indicator);9192if (symbolized && info.line != 0) {93Report(" location: name=%s, %d\n", info.file, static_cast<int>(info.line));94} else if (g.gcc_location != 0) {95// Fallback to Global::gcc_location96Report(" location: name=%s, %d\n", g.gcc_location->filename, g.gcc_location->line_no);97}98}99100static u32 FindRegistrationSite(const Global *g) {101mu_for_globals.CheckLocked();102CHECK(global_registration_site_vector);103for (uptr i = 0, n = global_registration_site_vector->size(); i < n; i++) {104GlobalRegistrationSite &grs = (*global_registration_site_vector)[i];105if (g >= grs.g_first && g <= grs.g_last)106return grs.stack_id;107}108return 0;109}110111int GetGlobalsForAddress(uptr addr, Global *globals, u32 *reg_sites,112int max_globals) {113if (!flags()->report_globals) return 0;114Lock lock(&mu_for_globals);115int res = 0;116for (ListOfGlobals *l = list_of_all_globals; l; l = l->next) {117const Global &g = *l->g;118if (flags()->report_globals >= 2)119ReportGlobal(g, "Search");120if (IsAddressNearGlobal(addr, g)) {121internal_memcpy(&globals[res], &g, sizeof(g));122if (reg_sites)123reg_sites[res] = FindRegistrationSite(&g);124res++;125if (res == max_globals)126break;127}128}129return res;130}131132enum GlobalSymbolState {133UNREGISTERED = 0,134REGISTERED = 1135};136137// Check ODR violation for given global G via special ODR indicator. We use138// this method in case compiler instruments global variables through their139// local aliases.140static void CheckODRViolationViaIndicator(const Global *g) {141// Instrumentation requests to skip ODR check.142if (g->odr_indicator == UINTPTR_MAX)143return;144u8 *odr_indicator = reinterpret_cast<u8 *>(g->odr_indicator);145if (*odr_indicator == UNREGISTERED) {146*odr_indicator = REGISTERED;147return;148}149// If *odr_indicator is DEFINED, some module have already registered150// externally visible symbol with the same name. This is an ODR violation.151for (ListOfGlobals *l = list_of_all_globals; l; l = l->next) {152if (g->odr_indicator == l->g->odr_indicator &&153(flags()->detect_odr_violation >= 2 || g->size != l->g->size) &&154!IsODRViolationSuppressed(g->name))155ReportODRViolation(g, FindRegistrationSite(g),156l->g, FindRegistrationSite(l->g));157}158}159160// Check ODR violation for given global G by checking if it's already poisoned.161// We use this method in case compiler doesn't use private aliases for global162// variables.163static void CheckODRViolationViaPoisoning(const Global *g) {164if (__asan_region_is_poisoned(g->beg, g->size_with_redzone)) {165// This check may not be enough: if the first global is much larger166// the entire redzone of the second global may be within the first global.167for (ListOfGlobals *l = list_of_all_globals; l; l = l->next) {168if (g->beg == l->g->beg &&169(flags()->detect_odr_violation >= 2 || g->size != l->g->size) &&170!IsODRViolationSuppressed(g->name))171ReportODRViolation(g, FindRegistrationSite(g),172l->g, FindRegistrationSite(l->g));173}174}175}176177// Clang provides two different ways for global variables protection:178// it can poison the global itself or its private alias. In former179// case we may poison same symbol multiple times, that can help us to180// cheaply detect ODR violation: if we try to poison an already poisoned181// global, we have ODR violation error.182// In latter case, we poison each symbol exactly once, so we use special183// indicator symbol to perform similar check.184// In either case, compiler provides a special odr_indicator field to Global185// structure, that can contain two kinds of values:186// 1) Non-zero value. In this case, odr_indicator is an address of187// corresponding indicator variable for given global.188// 2) Zero. This means that we don't use private aliases for global variables189// and can freely check ODR violation with the first method.190//191// This routine chooses between two different methods of ODR violation192// detection.193static inline bool UseODRIndicator(const Global *g) {194return g->odr_indicator > 0;195}196197// Register a global variable.198// This function may be called more than once for every global199// so we store the globals in a map.200static void RegisterGlobal(const Global *g) {201CHECK(AsanInited());202if (flags()->report_globals >= 2)203ReportGlobal(*g, "Added");204CHECK(flags()->report_globals);205CHECK(AddrIsInMem(g->beg));206if (!AddrIsAlignedByGranularity(g->beg)) {207Report("The following global variable is not properly aligned.\n");208Report("This may happen if another global with the same name\n");209Report("resides in another non-instrumented module.\n");210Report("Or the global comes from a C file built w/o -fno-common.\n");211Report("In either case this is likely an ODR violation bug,\n");212Report("but AddressSanitizer can not provide more details.\n");213ReportODRViolation(g, FindRegistrationSite(g), g, FindRegistrationSite(g));214CHECK(AddrIsAlignedByGranularity(g->beg));215}216CHECK(AddrIsAlignedByGranularity(g->size_with_redzone));217if (flags()->detect_odr_violation) {218// Try detecting ODR (One Definition Rule) violation, i.e. the situation219// where two globals with the same name are defined in different modules.220if (UseODRIndicator(g))221CheckODRViolationViaIndicator(g);222else223CheckODRViolationViaPoisoning(g);224}225if (CanPoisonMemory())226PoisonRedZones(*g);227ListOfGlobals *l = new (GetGlobalLowLevelAllocator()) ListOfGlobals;228l->g = g;229l->next = list_of_all_globals;230list_of_all_globals = l;231if (g->has_dynamic_init) {232if (!dynamic_init_globals) {233dynamic_init_globals = new (GetGlobalLowLevelAllocator()) VectorOfGlobals;234dynamic_init_globals->reserve(kDynamicInitGlobalsInitialCapacity);235}236DynInitGlobal dyn_global = { *g, false };237dynamic_init_globals->push_back(dyn_global);238}239}240241static void UnregisterGlobal(const Global *g) {242CHECK(AsanInited());243if (flags()->report_globals >= 2)244ReportGlobal(*g, "Removed");245CHECK(flags()->report_globals);246CHECK(AddrIsInMem(g->beg));247CHECK(AddrIsAlignedByGranularity(g->beg));248CHECK(AddrIsAlignedByGranularity(g->size_with_redzone));249if (CanPoisonMemory())250PoisonShadowForGlobal(g, 0);251// We unpoison the shadow memory for the global but we do not remove it from252// the list because that would require O(n^2) time with the current list253// implementation. It might not be worth doing anyway.254255// Release ODR indicator.256if (UseODRIndicator(g) && g->odr_indicator != UINTPTR_MAX) {257u8 *odr_indicator = reinterpret_cast<u8 *>(g->odr_indicator);258*odr_indicator = UNREGISTERED;259}260}261262void StopInitOrderChecking() {263Lock lock(&mu_for_globals);264if (!flags()->check_initialization_order || !dynamic_init_globals)265return;266flags()->check_initialization_order = false;267for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {268DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];269const Global *g = &dyn_g.g;270// Unpoison the whole global.271PoisonShadowForGlobal(g, 0);272// Poison redzones back.273PoisonRedZones(*g);274}275}276277static bool IsASCII(unsigned char c) { return /*0x00 <= c &&*/ c <= 0x7F; }278279const char *MaybeDemangleGlobalName(const char *name) {280// We can spoil names of globals with C linkage, so use an heuristic281// approach to check if the name should be demangled.282bool should_demangle = false;283if (name[0] == '_' && name[1] == 'Z')284should_demangle = true;285else if (SANITIZER_WINDOWS && name[0] == '\01' && name[1] == '?')286should_demangle = true;287288return should_demangle ? Symbolizer::GetOrInit()->Demangle(name) : name;289}290291// Check if the global is a zero-terminated ASCII string. If so, print it.292void PrintGlobalNameIfASCII(InternalScopedString *str, const __asan_global &g) {293for (uptr p = g.beg; p < g.beg + g.size - 1; p++) {294unsigned char c = *(unsigned char *)p;295if (c == '\0' || !IsASCII(c)) return;296}297if (*(char *)(g.beg + g.size - 1) != '\0') return;298str->AppendF(" '%s' is ascii string '%s'\n", MaybeDemangleGlobalName(g.name),299(char *)g.beg);300}301302void PrintGlobalLocation(InternalScopedString *str, const __asan_global &g,303bool print_module_name) {304DataInfo info;305if (Symbolizer::GetOrInit()->SymbolizeData(g.beg, &info) && info.line != 0) {306str->AppendF("%s:%d", info.file, static_cast<int>(info.line));307} else if (g.gcc_location != 0) {308// Fallback to Global::gcc_location309str->AppendF("%s", g.gcc_location->filename ? g.gcc_location->filename310: g.module_name);311if (g.gcc_location->line_no)312str->AppendF(":%d", g.gcc_location->line_no);313if (g.gcc_location->column_no)314str->AppendF(":%d", g.gcc_location->column_no);315} else {316str->AppendF("%s", g.module_name);317}318if (print_module_name && info.module)319str->AppendF(" in %s", info.module);320}321322} // namespace __asan323324// ---------------------- Interface ---------------- {{{1325using namespace __asan;326327// Apply __asan_register_globals to all globals found in the same loaded328// executable or shared library as `flag'. The flag tracks whether globals have329// already been registered or not for this image.330void __asan_register_image_globals(uptr *flag) {331if (*flag)332return;333AsanApplyToGlobals(__asan_register_globals, flag);334*flag = 1;335}336337// This mirrors __asan_register_image_globals.338void __asan_unregister_image_globals(uptr *flag) {339if (!*flag)340return;341AsanApplyToGlobals(__asan_unregister_globals, flag);342*flag = 0;343}344345void __asan_register_elf_globals(uptr *flag, void *start, void *stop) {346if (*flag || start == stop)347return;348CHECK_EQ(0, ((uptr)stop - (uptr)start) % sizeof(__asan_global));349__asan_global *globals_start = (__asan_global*)start;350__asan_global *globals_stop = (__asan_global*)stop;351__asan_register_globals(globals_start, globals_stop - globals_start);352*flag = 1;353}354355void __asan_unregister_elf_globals(uptr *flag, void *start, void *stop) {356if (!*flag || start == stop)357return;358CHECK_EQ(0, ((uptr)stop - (uptr)start) % sizeof(__asan_global));359__asan_global *globals_start = (__asan_global*)start;360__asan_global *globals_stop = (__asan_global*)stop;361__asan_unregister_globals(globals_start, globals_stop - globals_start);362*flag = 0;363}364365// Register an array of globals.366void __asan_register_globals(__asan_global *globals, uptr n) {367if (!flags()->report_globals) return;368GET_STACK_TRACE_MALLOC;369u32 stack_id = StackDepotPut(stack);370Lock lock(&mu_for_globals);371if (!global_registration_site_vector) {372global_registration_site_vector =373new (GetGlobalLowLevelAllocator()) GlobalRegistrationSiteVector;374global_registration_site_vector->reserve(128);375}376GlobalRegistrationSite site = {stack_id, &globals[0], &globals[n - 1]};377global_registration_site_vector->push_back(site);378if (flags()->report_globals >= 2) {379PRINT_CURRENT_STACK();380Printf("=== ID %d; %p %p\n", stack_id, (void *)&globals[0],381(void *)&globals[n - 1]);382}383for (uptr i = 0; i < n; i++) {384if (SANITIZER_WINDOWS && globals[i].beg == 0) {385// The MSVC incremental linker may pad globals out to 256 bytes. As long386// as __asan_global is less than 256 bytes large and its size is a power387// of two, we can skip over the padding.388static_assert(389sizeof(__asan_global) < 256 &&390(sizeof(__asan_global) & (sizeof(__asan_global) - 1)) == 0,391"sizeof(__asan_global) incompatible with incremental linker padding");392// If these are padding bytes, the rest of the global should be zero.393CHECK(globals[i].size == 0 && globals[i].size_with_redzone == 0 &&394globals[i].name == nullptr && globals[i].module_name == nullptr &&395globals[i].odr_indicator == 0);396continue;397}398RegisterGlobal(&globals[i]);399}400401// Poison the metadata. It should not be accessible to user code.402PoisonShadow(reinterpret_cast<uptr>(globals), n * sizeof(__asan_global),403kAsanGlobalRedzoneMagic);404}405406// Unregister an array of globals.407// We must do this when a shared objects gets dlclosed.408void __asan_unregister_globals(__asan_global *globals, uptr n) {409if (!flags()->report_globals) return;410Lock lock(&mu_for_globals);411for (uptr i = 0; i < n; i++) {412if (SANITIZER_WINDOWS && globals[i].beg == 0) {413// Skip globals that look like padding from the MSVC incremental linker.414// See comment in __asan_register_globals.415continue;416}417UnregisterGlobal(&globals[i]);418}419420// Unpoison the metadata.421PoisonShadow(reinterpret_cast<uptr>(globals), n * sizeof(__asan_global), 0);422}423424// This method runs immediately prior to dynamic initialization in each TU,425// when all dynamically initialized globals are unpoisoned. This method426// poisons all global variables not defined in this TU, so that a dynamic427// initializer can only touch global variables in the same TU.428void __asan_before_dynamic_init(const char *module_name) {429if (!flags()->check_initialization_order ||430!CanPoisonMemory() ||431!dynamic_init_globals)432return;433bool strict_init_order = flags()->strict_init_order;434CHECK(module_name);435CHECK(AsanInited());436Lock lock(&mu_for_globals);437if (flags()->report_globals >= 3)438Printf("DynInitPoison module: %s\n", module_name);439for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {440DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];441const Global *g = &dyn_g.g;442if (dyn_g.initialized)443continue;444if (g->module_name != module_name)445PoisonShadowForGlobal(g, kAsanInitializationOrderMagic);446else if (!strict_init_order)447dyn_g.initialized = true;448}449}450451// This method runs immediately after dynamic initialization in each TU, when452// all dynamically initialized globals except for those defined in the current453// TU are poisoned. It simply unpoisons all dynamically initialized globals.454void __asan_after_dynamic_init() {455if (!flags()->check_initialization_order ||456!CanPoisonMemory() ||457!dynamic_init_globals)458return;459CHECK(AsanInited());460Lock lock(&mu_for_globals);461// FIXME: Optionally report that we're unpoisoning globals from a module.462for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {463DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];464const Global *g = &dyn_g.g;465if (!dyn_g.initialized) {466// Unpoison the whole global.467PoisonShadowForGlobal(g, 0);468// Poison redzones back.469PoisonRedZones(*g);470}471}472}473474475