1 //===-- asan_globals.cc ---------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
11 //===----------------------------------------------------------------------===//
12 #include "asan_interceptors.h"
13 #include "asan_internal.h"
14 #include "asan_mapping.h"
15 #include "asan_poisoning.h"
16 #include "asan_report.h"
17 #include "asan_stack.h"
18 #include "asan_stats.h"
19 #include "asan_thread.h"
20 #include "sanitizer_common/sanitizer_common.h"
21 #include "sanitizer_common/sanitizer_mutex.h"
22 #include "sanitizer_common/sanitizer_placement_new.h"
23 #include "sanitizer_common/sanitizer_stackdepot.h"
27 typedef __asan_global Global
;
29 struct ListOfGlobals
{
34 static BlockingMutex
mu_for_globals(LINKER_INITIALIZED
);
35 static LowLevelAllocator allocator_for_globals
;
36 static ListOfGlobals
*list_of_all_globals
;
38 static const int kDynamicInitGlobalsInitialCapacity
= 512;
39 struct DynInitGlobal
{
43 typedef InternalMmapVector
<DynInitGlobal
> VectorOfGlobals
;
44 // Lazy-initialized and never deleted.
45 static VectorOfGlobals
*dynamic_init_globals
;
47 // We want to remember where a certain range of globals was registered.
48 struct GlobalRegistrationSite
{
50 Global
*g_first
, *g_last
;
52 typedef InternalMmapVector
<GlobalRegistrationSite
> GlobalRegistrationSiteVector
;
53 static GlobalRegistrationSiteVector
*global_registration_site_vector
;
55 ALWAYS_INLINE
void PoisonShadowForGlobal(const Global
*g
, u8 value
) {
56 FastPoisonShadow(g
->beg
, g
->size_with_redzone
, value
);
59 ALWAYS_INLINE
void PoisonRedZones(const Global
&g
) {
60 uptr aligned_size
= RoundUpTo(g
.size
, SHADOW_GRANULARITY
);
61 FastPoisonShadow(g
.beg
+ aligned_size
, g
.size_with_redzone
- aligned_size
,
62 kAsanGlobalRedzoneMagic
);
63 if (g
.size
!= aligned_size
) {
64 FastPoisonShadowPartialRightRedzone(
65 g
.beg
+ RoundDownTo(g
.size
, SHADOW_GRANULARITY
),
66 g
.size
% SHADOW_GRANULARITY
,
68 kAsanGlobalRedzoneMagic
);
72 const uptr kMinimalDistanceFromAnotherGlobal
= 64;
74 bool IsAddressNearGlobal(uptr addr
, const __asan_global
&g
) {
75 if (addr
<= g
.beg
- kMinimalDistanceFromAnotherGlobal
) return false;
76 if (addr
>= g
.beg
+ g
.size_with_redzone
) return false;
80 static void ReportGlobal(const Global
&g
, const char *prefix
) {
81 Report("%s Global[%p]: beg=%p size=%zu/%zu name=%s module=%s dyn_init=%zu\n",
82 prefix
, &g
, (void *)g
.beg
, g
.size
, g
.size_with_redzone
, g
.name
,
83 g
.module_name
, g
.has_dynamic_init
);
85 Report(" location (%p): name=%s[%p], %d %d\n", g
.location
,
86 g
.location
->filename
, g
.location
->filename
, g
.location
->line_no
,
87 g
.location
->column_no
);
91 static bool DescribeOrGetInfoIfGlobal(uptr addr
, uptr size
, bool print
,
92 Global
*output_global
) {
93 if (!flags()->report_globals
) return false;
94 BlockingMutexLock
lock(&mu_for_globals
);
96 for (ListOfGlobals
*l
= list_of_all_globals
; l
; l
= l
->next
) {
97 const Global
&g
= *l
->g
;
99 if (flags()->report_globals
>= 2)
100 ReportGlobal(g
, "Search");
101 res
|= DescribeAddressRelativeToGlobal(addr
, size
, g
);
103 if (IsAddressNearGlobal(addr
, g
)) {
104 CHECK(output_global
);
113 bool DescribeAddressIfGlobal(uptr addr
, uptr size
) {
114 return DescribeOrGetInfoIfGlobal(addr
, size
, /* print */ true,
115 /* output_global */ nullptr);
118 bool GetInfoForAddressIfGlobal(uptr addr
, AddressDescription
*descr
) {
120 if (DescribeOrGetInfoIfGlobal(addr
, /* size */ 1, /* print */ false, &g
)) {
121 internal_strncpy(descr
->name
, g
.name
, descr
->name_size
);
122 descr
->region_address
= g
.beg
;
123 descr
->region_size
= g
.size
;
124 descr
->region_kind
= "global";
130 u32
FindRegistrationSite(const Global
*g
) {
131 CHECK(global_registration_site_vector
);
132 for (uptr i
= 0, n
= global_registration_site_vector
->size(); i
< n
; i
++) {
133 GlobalRegistrationSite
&grs
= (*global_registration_site_vector
)[i
];
134 if (g
>= grs
.g_first
&& g
<= grs
.g_last
)
140 // Register a global variable.
141 // This function may be called more than once for every global
142 // so we store the globals in a map.
143 static void RegisterGlobal(const Global
*g
) {
145 if (flags()->report_globals
>= 2)
146 ReportGlobal(*g
, "Added");
147 CHECK(flags()->report_globals
);
148 CHECK(AddrIsInMem(g
->beg
));
149 CHECK(AddrIsAlignedByGranularity(g
->beg
));
150 CHECK(AddrIsAlignedByGranularity(g
->size_with_redzone
));
151 // This "ODR violation" detection is fundamentally incompatible with
152 // how GCC registers globals. Disable as useless until rewritten upstream.
153 if (0 && 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
));
167 if (flags()->poison_heap
)
169 ListOfGlobals
*l
= new(allocator_for_globals
) ListOfGlobals
;
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
);
178 DynInitGlobal dyn_global
= { *g
, false };
179 dynamic_init_globals
->push_back(dyn_global
);
183 static void UnregisterGlobal(const Global
*g
) {
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.
196 void StopInitOrderChecking() {
197 BlockingMutexLock
lock(&mu_for_globals
);
198 if (!flags()->check_initialization_order
|| !dynamic_init_globals
)
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.
211 } // namespace __asan
213 // ---------------------- Interface ---------------- {{{1
214 using namespace __asan
; // NOLINT
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]);
231 for (uptr i
= 0; i
< n
; i
++) {
232 RegisterGlobal(&globals
[i
]);
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
]);
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
)
254 bool strict_init_order
= flags()->strict_init_order
;
255 CHECK(dynamic_init_globals
);
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
)
266 if (g
->module_name
!= module_name
)
267 PoisonShadowForGlobal(g
, kAsanInitializationOrderMagic
);
268 else if (!strict_init_order
)
269 dyn_g
.initialized
= true;
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
)
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.