Lines Matching +full:- +full:g +full:-
1 //===-- asan_globals.cpp --------------------------------------------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
12 //===----------------------------------------------------------------------===//
34 const Global *g;
43 Global g;
47 // Lazy-initialized and never deleted.
58 ALWAYS_INLINE void PoisonShadowForGlobal(const Global *g, u8 value) {
59 FastPoisonShadow(g->beg, g->size_with_redzone, value);
62 ALWAYS_INLINE void PoisonRedZones(const Global &g) {
63 uptr aligned_size = RoundUpTo(g.size, ASAN_SHADOW_GRANULARITY);
64 FastPoisonShadow(g.beg + aligned_size, g.size_with_redzone - aligned_size,
66 if (g.size != aligned_size) {
68 g.beg + RoundDownTo(g.size, ASAN_SHADOW_GRANULARITY),
69 g.size % ASAN_SHADOW_GRANULARITY, ASAN_SHADOW_GRANULARITY,
76 static 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;
82 static void ReportGlobal(const Global &g, const char *prefix) {
84 bool symbolized = Symbolizer::GetOrInit()->SymbolizeData(g.beg, &info);
89 prefix, (void *)&g, (void *)g.beg, g.size, g.size_with_redzone, g.name,
90 g.module_name, (symbolized ? info.module : "?"), g.has_dynamic_init,
91 (void *)g.odr_indicator);
95 } else if (g.gcc_location != 0) {
97 Report(" location: name=%s, %d\n", g.gcc_location->filename, g.gcc_location->line_no);
101 static u32 FindRegistrationSite(const Global *g) {
104 for (uptr i = 0, n = global_registration_site_vector->size(); i < n; i++) {
106 if (g >= grs.g_first && g <= grs.g_last)
114 if (!flags()->report_globals) return 0;
117 for (ListOfGlobals *l = list_of_all_globals; l; l = l->next) {
118 const Global &g = *l->g;
119 if (flags()->report_globals >= 2)
120 ReportGlobal(g, "Search");
121 if (IsAddressNearGlobal(addr, g)) {
122 internal_memcpy(&globals[res], &g, sizeof(g));
124 reg_sites[res] = FindRegistrationSite(&g);
138 // Check ODR violation for given global G via special ODR indicator. We use
141 static void CheckODRViolationViaIndicator(const Global *g) {
143 if (g->odr_indicator == UINTPTR_MAX)
145 u8 *odr_indicator = reinterpret_cast<u8 *>(g->odr_indicator);
152 for (ListOfGlobals *l = list_of_all_globals; l; l = l->next) {
153 if (g->odr_indicator == l->g->odr_indicator &&
154 (flags()->detect_odr_violation >= 2 || g->size != l->g->size) &&
155 !IsODRViolationSuppressed(g->name))
156 ReportODRViolation(g, FindRegistrationSite(g),
157 l->g, FindRegistrationSite(l->g));
161 // Check ODR violation for given global G by checking if it's already poisoned.
164 static void CheckODRViolationViaPoisoning(const Global *g) {
165 if (__asan_region_is_poisoned(g->beg, g->size_with_redzone)) {
168 for (ListOfGlobals *l = list_of_all_globals; l; l = l->next) {
169 if (g->beg == l->g->beg &&
170 (flags()->detect_odr_violation >= 2 || g->size != l->g->size) &&
171 !IsODRViolationSuppressed(g->name))
172 ReportODRViolation(g, FindRegistrationSite(g),
173 l->g, FindRegistrationSite(l->g));
187 // 1) Non-zero value. In this case, odr_indicator is an address of
194 static inline bool UseODRIndicator(const Global *g) {
195 return g->odr_indicator > 0;
201 static void RegisterGlobal(const Global *g) {
203 if (flags()->report_globals >= 2)
204 ReportGlobal(*g, "Added");
205 CHECK(flags()->report_globals);
206 CHECK(AddrIsInMem(g->beg));
207 if (!AddrIsAlignedByGranularity(g->beg)) {
210 Report("resides in another non-instrumented module.\n");
211 Report("Or the global comes from a C file built w/o -fno-common.\n");
214 ReportODRViolation(g, FindRegistrationSite(g), g, FindRegistrationSite(g));
215 CHECK(AddrIsAlignedByGranularity(g->beg));
217 CHECK(AddrIsAlignedByGranularity(g->size_with_redzone));
218 if (flags()->detect_odr_violation) {
221 if (UseODRIndicator(g))
222 CheckODRViolationViaIndicator(g);
224 CheckODRViolationViaPoisoning(g);
227 PoisonRedZones(*g);
229 l->g = g;
230 l->next = list_of_all_globals;
232 if (g->has_dynamic_init) {
235 dynamic_init_globals->reserve(kDynamicInitGlobalsInitialCapacity);
237 DynInitGlobal dyn_global = { *g, false };
238 dynamic_init_globals->push_back(dyn_global);
242 static void UnregisterGlobal(const Global *g) {
244 if (flags()->report_globals >= 2)
245 ReportGlobal(*g, "Removed");
246 CHECK(flags()->report_globals);
247 CHECK(AddrIsInMem(g->beg));
248 CHECK(AddrIsAlignedByGranularity(g->beg));
249 CHECK(AddrIsAlignedByGranularity(g->size_with_redzone));
251 PoisonShadowForGlobal(g, 0);
257 if (UseODRIndicator(g) && g->odr_indicator != UINTPTR_MAX) {
258 u8 *odr_indicator = reinterpret_cast<u8 *>(g->odr_indicator);
265 if (!flags()->check_initialization_order || !dynamic_init_globals)
267 flags()->check_initialization_order = false;
268 for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {
270 const Global *g = &dyn_g.g;
272 PoisonShadowForGlobal(g, 0);
274 PoisonRedZones(*g);
289 return should_demangle ? Symbolizer::GetOrInit()->Demangle(name) : name;
292 // Check if the global is a zero-terminated ASCII string. If so, print it.
293 void PrintGlobalNameIfASCII(InternalScopedString *str, const __asan_global &g) {
294 for (uptr p = g.beg; p < g.beg + g.size - 1; p++) {
298 if (*(char *)(g.beg + g.size - 1) != '\0') return;
299 str->AppendF(" '%s' is ascii string '%s'\n", MaybeDemangleGlobalName(g.name),
300 (char *)g.beg);
303 void PrintGlobalLocation(InternalScopedString *str, const __asan_global &g,
306 if (Symbolizer::GetOrInit()->SymbolizeData(g.beg, &info) && info.line != 0) {
307 str->AppendF("%s:%d", info.file, static_cast<int>(info.line));
308 } else if (g.gcc_location != 0) {
310 str->AppendF("%s", g.gcc_location->filename ? g.gcc_location->filename
311 : g.module_name);
312 if (g.gcc_location->line_no)
313 str->AppendF(":%d", g.gcc_location->line_no);
314 if (g.gcc_location->column_no)
315 str->AppendF(":%d", g.gcc_location->column_no);
317 str->AppendF("%s", g.module_name);
320 str->AppendF(" in %s", info.module);
325 // ---------------------- Interface ---------------- {{{1
349 CHECK_EQ(0, ((uptr)stop - (uptr)start) % sizeof(__asan_global));
352 __asan_register_globals(globals_start, globals_stop - globals_start);
359 CHECK_EQ(0, ((uptr)stop - (uptr)start) % sizeof(__asan_global));
362 __asan_unregister_globals(globals_start, globals_stop - globals_start);
368 if (!flags()->report_globals) return;
375 global_registration_site_vector->reserve(128);
377 GlobalRegistrationSite site = {stack_id, &globals[0], &globals[n - 1]};
378 global_registration_site_vector->push_back(site);
379 if (flags()->report_globals >= 2) {
382 (void *)&globals[n - 1]);
391 (sizeof(__asan_global) & (sizeof(__asan_global) - 1)) == 0,
410 if (!flags()->report_globals) return;
430 if (!flags()->check_initialization_order ||
434 bool strict_init_order = flags()->strict_init_order;
438 if (flags()->report_globals >= 3)
440 for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {
442 const Global *g = &dyn_g.g;
445 if (g->module_name != module_name)
446 PoisonShadowForGlobal(g, kAsanInitializationOrderMagic);
456 if (!flags()->check_initialization_order ||
463 for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {
465 const Global *g = &dyn_g.g;
468 PoisonShadowForGlobal(g, 0);
470 PoisonRedZones(*g);