2012-06-04 21:50:10 +08:00
|
|
|
//===-- asan_globals.cc ---------------------------------------------------===//
|
2011-11-30 09:07:02 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// Handle globals.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "asan_interceptors.h"
|
|
|
|
#include "asan_internal.h"
|
|
|
|
#include "asan_mapping.h"
|
2013-03-28 23:42:43 +08:00
|
|
|
#include "asan_poisoning.h"
|
2012-08-09 17:27:24 +08:00
|
|
|
#include "asan_report.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
#include "asan_stack.h"
|
|
|
|
#include "asan_stats.h"
|
|
|
|
#include "asan_thread.h"
|
2013-03-28 23:42:43 +08:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
2013-01-14 15:51:39 +08:00
|
|
|
#include "sanitizer_common/sanitizer_mutex.h"
|
2013-03-28 23:42:43 +08:00
|
|
|
#include "sanitizer_common/sanitizer_placement_new.h"
|
2014-06-20 16:24:12 +08:00
|
|
|
#include "sanitizer_common/sanitizer_stackdepot.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
|
|
|
|
typedef __asan_global Global;
|
|
|
|
|
2011-12-03 05:02:20 +08:00
|
|
|
struct ListOfGlobals {
|
|
|
|
const Global *g;
|
|
|
|
ListOfGlobals *next;
|
|
|
|
};
|
|
|
|
|
2013-01-14 15:51:39 +08:00
|
|
|
static BlockingMutex mu_for_globals(LINKER_INITIALIZED);
|
2012-08-27 22:04:54 +08:00
|
|
|
static LowLevelAllocator allocator_for_globals;
|
2012-08-21 22:10:25 +08:00
|
|
|
static ListOfGlobals *list_of_all_globals;
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2013-03-28 23:42:43 +08:00
|
|
|
static const int kDynamicInitGlobalsInitialCapacity = 512;
|
2013-04-19 16:35:16 +08:00
|
|
|
struct DynInitGlobal {
|
|
|
|
Global g;
|
|
|
|
bool initialized;
|
|
|
|
};
|
2013-06-14 17:59:40 +08:00
|
|
|
typedef InternalMmapVector<DynInitGlobal> VectorOfGlobals;
|
2013-03-28 23:42:43 +08:00
|
|
|
// Lazy-initialized and never deleted.
|
|
|
|
static VectorOfGlobals *dynamic_init_globals;
|
|
|
|
|
2014-06-20 16:24:12 +08:00
|
|
|
// We want to remember where a certain range of globals was registered.
|
|
|
|
struct GlobalRegistrationSite {
|
|
|
|
u32 stack_id;
|
|
|
|
Global *g_first, *g_last;
|
|
|
|
};
|
|
|
|
typedef InternalMmapVector<GlobalRegistrationSite> GlobalRegistrationSiteVector;
|
|
|
|
static GlobalRegistrationSiteVector *global_registration_site_vector;
|
|
|
|
|
2013-03-29 02:52:40 +08:00
|
|
|
ALWAYS_INLINE void PoisonShadowForGlobal(const Global *g, u8 value) {
|
2013-03-28 23:42:43 +08:00
|
|
|
FastPoisonShadow(g->beg, g->size_with_redzone, value);
|
|
|
|
}
|
|
|
|
|
2013-03-29 02:52:40 +08:00
|
|
|
ALWAYS_INLINE void PoisonRedZones(const Global &g) {
|
2013-01-23 19:14:21 +08:00
|
|
|
uptr aligned_size = RoundUpTo(g.size, SHADOW_GRANULARITY);
|
2013-03-28 23:42:43 +08:00
|
|
|
FastPoisonShadow(g.beg + aligned_size, g.size_with_redzone - aligned_size,
|
|
|
|
kAsanGlobalRedzoneMagic);
|
2013-01-23 19:14:21 +08:00
|
|
|
if (g.size != aligned_size) {
|
2013-03-28 23:42:43 +08:00
|
|
|
FastPoisonShadowPartialRightRedzone(
|
2013-01-23 19:14:21 +08:00
|
|
|
g.beg + RoundDownTo(g.size, SHADOW_GRANULARITY),
|
|
|
|
g.size % SHADOW_GRANULARITY,
|
|
|
|
SHADOW_GRANULARITY,
|
|
|
|
kAsanGlobalRedzoneMagic);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:15:32 +08:00
|
|
|
const uptr kMinimalDistanceFromAnotherGlobal = 64;
|
|
|
|
|
|
|
|
bool IsAddressNearGlobal(uptr addr, const __asan_global &g) {
|
|
|
|
if (addr <= g.beg - kMinimalDistanceFromAnotherGlobal) return false;
|
|
|
|
if (addr >= g.beg + g.size_with_redzone) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-26 21:06:12 +08:00
|
|
|
static void ReportGlobal(const Global &g, const char *prefix) {
|
2014-06-20 16:24:12 +08:00
|
|
|
Report("%s Global[%p]: beg=%p size=%zu/%zu name=%s module=%s dyn_init=%zu\n",
|
2014-08-02 08:35:50 +08:00
|
|
|
prefix, &g, (void *)g.beg, g.size, g.size_with_redzone, g.name,
|
2013-03-26 21:06:12 +08:00
|
|
|
g.module_name, g.has_dynamic_init);
|
2014-08-02 08:35:50 +08:00
|
|
|
if (g.location) {
|
|
|
|
Report(" location (%p): name=%s[%p], %d %d\n", g.location,
|
|
|
|
g.location->filename, g.location->filename, g.location->line_no,
|
|
|
|
g.location->column_no);
|
|
|
|
}
|
2013-03-26 21:06:12 +08:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:15:32 +08:00
|
|
|
static bool DescribeOrGetInfoIfGlobal(uptr addr, uptr size, bool print,
|
|
|
|
Global *output_global) {
|
2012-07-09 22:36:04 +08:00
|
|
|
if (!flags()->report_globals) return false;
|
2013-01-14 15:51:39 +08:00
|
|
|
BlockingMutexLock lock(&mu_for_globals);
|
2011-11-30 09:07:02 +08:00
|
|
|
bool res = false;
|
2012-08-21 22:10:25 +08:00
|
|
|
for (ListOfGlobals *l = list_of_all_globals; l; l = l->next) {
|
2011-12-03 05:02:20 +08:00
|
|
|
const Global &g = *l->g;
|
2014-09-27 03:15:32 +08:00
|
|
|
if (print) {
|
|
|
|
if (flags()->report_globals >= 2)
|
|
|
|
ReportGlobal(g, "Search");
|
|
|
|
res |= DescribeAddressRelativeToGlobal(addr, size, g);
|
|
|
|
} else {
|
|
|
|
if (IsAddressNearGlobal(addr, g)) {
|
|
|
|
CHECK(output_global);
|
|
|
|
*output_global = g;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:15:32 +08:00
|
|
|
bool DescribeAddressIfGlobal(uptr addr, uptr size) {
|
|
|
|
return DescribeOrGetInfoIfGlobal(addr, size, /* print */ true,
|
|
|
|
/* output_global */ nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetInfoForAddressIfGlobal(uptr addr, AddressDescription *descr) {
|
|
|
|
Global g = {};
|
|
|
|
if (DescribeOrGetInfoIfGlobal(addr, /* size */ 1, /* print */ false, &g)) {
|
|
|
|
internal_strncpy(descr->name, g.name, descr->name_size);
|
|
|
|
descr->region_address = g.beg;
|
|
|
|
descr->region_size = g.size;
|
|
|
|
descr->region_kind = "global";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-20 16:24:12 +08:00
|
|
|
u32 FindRegistrationSite(const Global *g) {
|
|
|
|
CHECK(global_registration_site_vector);
|
|
|
|
for (uptr i = 0, n = global_registration_site_vector->size(); i < n; i++) {
|
|
|
|
GlobalRegistrationSite &grs = (*global_registration_site_vector)[i];
|
|
|
|
if (g >= grs.g_first && g <= grs.g_last)
|
|
|
|
return grs.stack_id;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
// Register a global variable.
|
|
|
|
// This function may be called more than once for every global
|
|
|
|
// so we store the globals in a map.
|
|
|
|
static void RegisterGlobal(const Global *g) {
|
|
|
|
CHECK(asan_inited);
|
2012-08-21 22:10:25 +08:00
|
|
|
if (flags()->report_globals >= 2)
|
2013-03-26 21:06:12 +08:00
|
|
|
ReportGlobal(*g, "Added");
|
2012-07-09 22:36:04 +08:00
|
|
|
CHECK(flags()->report_globals);
|
2011-11-30 09:07:02 +08:00
|
|
|
CHECK(AddrIsInMem(g->beg));
|
2011-12-03 05:02:20 +08:00
|
|
|
CHECK(AddrIsAlignedByGranularity(g->beg));
|
2011-12-16 05:55:34 +08:00
|
|
|
CHECK(AddrIsAlignedByGranularity(g->size_with_redzone));
|
2014-04-25 16:58:28 +08:00
|
|
|
if (flags()->detect_odr_violation) {
|
|
|
|
// Try detecting ODR (One Definition Rule) violation, i.e. the situation
|
|
|
|
// where two globals with the same name are defined in different modules.
|
|
|
|
if (__asan_region_is_poisoned(g->beg, g->size_with_redzone)) {
|
|
|
|
// This check may not be enough: if the first global is much larger
|
|
|
|
// the entire redzone of the second global may be within the first global.
|
|
|
|
for (ListOfGlobals *l = list_of_all_globals; l; l = l->next) {
|
2014-04-28 20:47:58 +08:00
|
|
|
if (g->beg == l->g->beg &&
|
|
|
|
(flags()->detect_odr_violation >= 2 || g->size != l->g->size))
|
2014-06-20 16:24:12 +08:00
|
|
|
ReportODRViolation(g, FindRegistrationSite(g),
|
|
|
|
l->g, FindRegistrationSite(l->g));
|
2014-04-25 16:58:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-17 08:01:02 +08:00
|
|
|
if (CanPoisonMemory())
|
2013-03-28 23:42:43 +08:00
|
|
|
PoisonRedZones(*g);
|
2013-10-24 14:23:39 +08:00
|
|
|
ListOfGlobals *l = new(allocator_for_globals) ListOfGlobals;
|
2011-12-03 05:02:20 +08:00
|
|
|
l->g = g;
|
2012-08-21 22:10:25 +08:00
|
|
|
l->next = list_of_all_globals;
|
|
|
|
list_of_all_globals = l;
|
|
|
|
if (g->has_dynamic_init) {
|
2013-03-28 23:42:43 +08:00
|
|
|
if (dynamic_init_globals == 0) {
|
2013-10-24 14:23:39 +08:00
|
|
|
dynamic_init_globals = new(allocator_for_globals)
|
2013-03-28 23:42:43 +08:00
|
|
|
VectorOfGlobals(kDynamicInitGlobalsInitialCapacity);
|
|
|
|
}
|
2013-04-19 16:35:16 +08:00
|
|
|
DynInitGlobal dyn_global = { *g, false };
|
|
|
|
dynamic_init_globals->push_back(dyn_global);
|
2012-08-21 22:10:25 +08:00
|
|
|
}
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2011-12-16 05:55:34 +08:00
|
|
|
static void UnregisterGlobal(const Global *g) {
|
|
|
|
CHECK(asan_inited);
|
2012-07-09 22:36:04 +08:00
|
|
|
CHECK(flags()->report_globals);
|
2011-12-16 05:55:34 +08:00
|
|
|
CHECK(AddrIsInMem(g->beg));
|
|
|
|
CHECK(AddrIsAlignedByGranularity(g->beg));
|
|
|
|
CHECK(AddrIsAlignedByGranularity(g->size_with_redzone));
|
2014-12-17 08:01:02 +08:00
|
|
|
if (CanPoisonMemory())
|
2013-03-28 23:42:43 +08:00
|
|
|
PoisonShadowForGlobal(g, 0);
|
2011-12-16 05:55:34 +08:00
|
|
|
// We unpoison the shadow memory for the global but we do not remove it from
|
|
|
|
// the list because that would require O(n^2) time with the current list
|
|
|
|
// implementation. It might not be worth doing anyway.
|
|
|
|
}
|
|
|
|
|
2013-05-24 19:46:56 +08:00
|
|
|
void StopInitOrderChecking() {
|
|
|
|
BlockingMutexLock lock(&mu_for_globals);
|
|
|
|
if (!flags()->check_initialization_order || !dynamic_init_globals)
|
|
|
|
return;
|
|
|
|
flags()->check_initialization_order = false;
|
|
|
|
for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {
|
|
|
|
DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];
|
|
|
|
const Global *g = &dyn_g.g;
|
|
|
|
// Unpoison the whole global.
|
|
|
|
PoisonShadowForGlobal(g, 0);
|
|
|
|
// Poison redzones back.
|
|
|
|
PoisonRedZones(*g);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
} // namespace __asan
|
|
|
|
|
|
|
|
// ---------------------- Interface ---------------- {{{1
|
|
|
|
using namespace __asan; // NOLINT
|
|
|
|
|
|
|
|
// Register an array of globals.
|
2012-05-31 22:11:07 +08:00
|
|
|
void __asan_register_globals(__asan_global *globals, uptr n) {
|
2012-07-09 22:36:04 +08:00
|
|
|
if (!flags()->report_globals) return;
|
2014-06-20 16:24:12 +08:00
|
|
|
GET_STACK_TRACE_FATAL_HERE;
|
2014-10-26 14:23:07 +08:00
|
|
|
u32 stack_id = StackDepotPut(stack);
|
2013-01-14 15:51:39 +08:00
|
|
|
BlockingMutexLock lock(&mu_for_globals);
|
2014-06-20 16:24:12 +08:00
|
|
|
if (!global_registration_site_vector)
|
|
|
|
global_registration_site_vector =
|
|
|
|
new(allocator_for_globals) GlobalRegistrationSiteVector(128);
|
|
|
|
GlobalRegistrationSite site = {stack_id, &globals[0], &globals[n - 1]};
|
|
|
|
global_registration_site_vector->push_back(site);
|
|
|
|
if (flags()->report_globals >= 2) {
|
|
|
|
PRINT_CURRENT_STACK();
|
|
|
|
Printf("=== ID %d; %p %p\n", stack_id, &globals[0], &globals[n - 1]);
|
|
|
|
}
|
2012-05-31 22:11:07 +08:00
|
|
|
for (uptr i = 0; i < n; i++) {
|
2011-11-30 09:07:02 +08:00
|
|
|
RegisterGlobal(&globals[i]);
|
|
|
|
}
|
|
|
|
}
|
2011-12-16 05:55:34 +08:00
|
|
|
|
|
|
|
// Unregister an array of globals.
|
2012-08-21 22:10:25 +08:00
|
|
|
// We must do this when a shared objects gets dlclosed.
|
2012-05-31 22:11:07 +08:00
|
|
|
void __asan_unregister_globals(__asan_global *globals, uptr n) {
|
2012-07-09 22:36:04 +08:00
|
|
|
if (!flags()->report_globals) return;
|
2013-01-14 15:51:39 +08:00
|
|
|
BlockingMutexLock lock(&mu_for_globals);
|
2012-05-31 22:11:07 +08:00
|
|
|
for (uptr i = 0; i < n; i++) {
|
2011-12-16 05:55:34 +08:00
|
|
|
UnregisterGlobal(&globals[i]);
|
|
|
|
}
|
|
|
|
}
|
2012-08-21 22:10:25 +08:00
|
|
|
|
|
|
|
// This method runs immediately prior to dynamic initialization in each TU,
|
|
|
|
// when all dynamically initialized globals are unpoisoned. This method
|
|
|
|
// poisons all global variables not defined in this TU, so that a dynamic
|
|
|
|
// initializer can only touch global variables in the same TU.
|
2013-03-26 21:06:12 +08:00
|
|
|
void __asan_before_dynamic_init(const char *module_name) {
|
2013-03-28 23:42:43 +08:00
|
|
|
if (!flags()->check_initialization_order ||
|
2014-12-17 08:01:02 +08:00
|
|
|
!CanPoisonMemory())
|
2013-03-28 23:42:43 +08:00
|
|
|
return;
|
2013-04-19 16:35:16 +08:00
|
|
|
bool strict_init_order = flags()->strict_init_order;
|
2013-03-28 23:42:43 +08:00
|
|
|
CHECK(dynamic_init_globals);
|
2013-03-26 21:06:12 +08:00
|
|
|
CHECK(module_name);
|
2013-03-28 23:42:43 +08:00
|
|
|
CHECK(asan_inited);
|
2013-01-14 15:51:39 +08:00
|
|
|
BlockingMutexLock lock(&mu_for_globals);
|
2013-03-28 23:42:43 +08:00
|
|
|
if (flags()->report_globals >= 3)
|
|
|
|
Printf("DynInitPoison module: %s\n", module_name);
|
|
|
|
for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {
|
2013-04-19 16:35:16 +08:00
|
|
|
DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];
|
|
|
|
const Global *g = &dyn_g.g;
|
|
|
|
if (dyn_g.initialized)
|
|
|
|
continue;
|
2013-03-28 23:42:43 +08:00
|
|
|
if (g->module_name != module_name)
|
|
|
|
PoisonShadowForGlobal(g, kAsanInitializationOrderMagic);
|
2013-04-19 16:35:16 +08:00
|
|
|
else if (!strict_init_order)
|
|
|
|
dyn_g.initialized = true;
|
2012-08-21 22:10:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method runs immediately after dynamic initialization in each TU, when
|
|
|
|
// all dynamically initialized globals except for those defined in the current
|
|
|
|
// TU are poisoned. It simply unpoisons all dynamically initialized globals.
|
|
|
|
void __asan_after_dynamic_init() {
|
2013-03-28 23:42:43 +08:00
|
|
|
if (!flags()->check_initialization_order ||
|
2014-12-17 08:01:02 +08:00
|
|
|
!CanPoisonMemory())
|
2013-03-28 23:42:43 +08:00
|
|
|
return;
|
|
|
|
CHECK(asan_inited);
|
2013-01-14 15:51:39 +08:00
|
|
|
BlockingMutexLock lock(&mu_for_globals);
|
2013-03-28 23:42:43 +08:00
|
|
|
// FIXME: Optionally report that we're unpoisoning globals from a module.
|
|
|
|
for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {
|
2013-04-19 16:35:16 +08:00
|
|
|
DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];
|
|
|
|
const Global *g = &dyn_g.g;
|
|
|
|
if (!dyn_g.initialized) {
|
|
|
|
// Unpoison the whole global.
|
|
|
|
PoisonShadowForGlobal(g, 0);
|
|
|
|
// Poison redzones back.
|
|
|
|
PoisonRedZones(*g);
|
|
|
|
}
|
2013-03-28 23:42:43 +08:00
|
|
|
}
|
2012-08-21 22:10:25 +08:00
|
|
|
}
|