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_lock.h"
|
|
|
|
#include "asan_mapping.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"
|
2012-08-29 10:27:54 +08:00
|
|
|
#include "sanitizer/asan_interface.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;
|
|
|
|
};
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
static AsanLock 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;
|
|
|
|
static ListOfGlobals *list_of_dynamic_init_globals;
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
void PoisonRedZones(const Global &g) {
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr shadow_rz_size = kGlobalAndStackRedzone >> SHADOW_SCALE;
|
2011-12-01 02:50:23 +08:00
|
|
|
CHECK(shadow_rz_size == 1 || shadow_rz_size == 2 || shadow_rz_size == 4);
|
2011-11-30 09:07:02 +08:00
|
|
|
// full right redzone
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr g_aligned_size = kGlobalAndStackRedzone *
|
2011-11-30 09:07:02 +08:00
|
|
|
((g.size + kGlobalAndStackRedzone - 1) / kGlobalAndStackRedzone);
|
2011-12-01 02:50:23 +08:00
|
|
|
PoisonShadow(g.beg + g_aligned_size,
|
|
|
|
kGlobalAndStackRedzone, kAsanGlobalRedzoneMagic);
|
2011-11-30 09:07:02 +08:00
|
|
|
if ((g.size % kGlobalAndStackRedzone) != 0) {
|
|
|
|
// partial right redzone
|
2012-05-31 23:02:07 +08:00
|
|
|
u64 g_aligned_down_size = kGlobalAndStackRedzone *
|
2011-12-01 02:50:23 +08:00
|
|
|
(g.size / kGlobalAndStackRedzone);
|
|
|
|
CHECK(g_aligned_down_size == g_aligned_size - kGlobalAndStackRedzone);
|
|
|
|
PoisonShadowPartialRightRedzone(g.beg + g_aligned_down_size,
|
2011-11-30 09:07:02 +08:00
|
|
|
g.size % kGlobalAndStackRedzone,
|
|
|
|
kGlobalAndStackRedzone,
|
|
|
|
kAsanGlobalRedzoneMagic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
static uptr GetAlignedSize(uptr size) {
|
2011-11-30 09:07:02 +08:00
|
|
|
return ((size + kGlobalAndStackRedzone - 1) / kGlobalAndStackRedzone)
|
|
|
|
* kGlobalAndStackRedzone;
|
|
|
|
}
|
|
|
|
|
2012-08-09 17:06:52 +08:00
|
|
|
bool DescribeAddressIfGlobal(uptr addr) {
|
2012-07-09 22:36:04 +08:00
|
|
|
if (!flags()->report_globals) return false;
|
2011-11-30 09:07:02 +08:00
|
|
|
ScopedLock lock(&mu_for_globals);
|
|
|
|
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;
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->report_globals >= 2)
|
2012-08-09 17:27:24 +08:00
|
|
|
Report("Search Global: beg=%p size=%zu name=%s\n",
|
|
|
|
(void*)g.beg, g.size, (char*)g.name);
|
|
|
|
res |= DescribeAddressRelativeToGlobal(addr, g);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
Report("Added Global: beg=%p size=%zu/%zu name=%s dyn.init=%zu\n",
|
|
|
|
(void*)g->beg, g->size, g->size_with_redzone, g->name,
|
|
|
|
g->has_dynamic_init);
|
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));
|
2011-12-03 05:02:20 +08:00
|
|
|
PoisonRedZones(*g);
|
|
|
|
ListOfGlobals *l =
|
|
|
|
(ListOfGlobals*)allocator_for_globals.Allocate(sizeof(ListOfGlobals));
|
|
|
|
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) {
|
|
|
|
l = (ListOfGlobals*)allocator_for_globals.Allocate(sizeof(ListOfGlobals));
|
|
|
|
l->g = g;
|
|
|
|
l->next = list_of_dynamic_init_globals;
|
|
|
|
list_of_dynamic_init_globals = l;
|
|
|
|
}
|
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));
|
|
|
|
PoisonShadow(g->beg, g->size_with_redzone, 0);
|
|
|
|
// 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.
|
|
|
|
}
|
|
|
|
|
2012-08-21 22:10:25 +08:00
|
|
|
// Poison all shadow memory for a single global.
|
|
|
|
static void PoisonGlobalAndRedzones(const Global *g) {
|
|
|
|
CHECK(asan_inited);
|
|
|
|
CHECK(flags()->check_initialization_order);
|
|
|
|
CHECK(AddrIsInMem(g->beg));
|
|
|
|
CHECK(AddrIsAlignedByGranularity(g->beg));
|
|
|
|
CHECK(AddrIsAlignedByGranularity(g->size_with_redzone));
|
|
|
|
if (flags()->report_globals >= 3)
|
|
|
|
Printf("DynInitPoison : %s\n", g->name);
|
|
|
|
PoisonShadow(g->beg, g->size_with_redzone, kAsanInitializationOrderMagic);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void UnpoisonGlobal(const Global *g) {
|
|
|
|
CHECK(asan_inited);
|
|
|
|
CHECK(flags()->check_initialization_order);
|
|
|
|
CHECK(AddrIsInMem(g->beg));
|
|
|
|
CHECK(AddrIsAlignedByGranularity(g->beg));
|
|
|
|
CHECK(AddrIsAlignedByGranularity(g->size_with_redzone));
|
|
|
|
if (flags()->report_globals >= 3)
|
|
|
|
Printf("DynInitUnpoison: %s\n", g->name);
|
|
|
|
PoisonShadow(g->beg, g->size_with_redzone, 0);
|
|
|
|
PoisonRedZones(*g);
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
} // namespace __asan
|
|
|
|
|
|
|
|
// ---------------------- Interface ---------------- {{{1
|
|
|
|
using namespace __asan; // NOLINT
|
|
|
|
|
|
|
|
// Register one global with a default redzone.
|
2012-05-31 22:11:07 +08:00
|
|
|
void __asan_register_global(uptr addr, uptr size,
|
2011-11-30 09:07:02 +08:00
|
|
|
const char *name) {
|
2012-07-09 22:36:04 +08:00
|
|
|
if (!flags()->report_globals) return;
|
2011-12-16 05:55:34 +08:00
|
|
|
ScopedLock lock(&mu_for_globals);
|
|
|
|
Global *g = (Global *)allocator_for_globals.Allocate(sizeof(Global));
|
|
|
|
g->beg = addr;
|
|
|
|
g->size = size;
|
|
|
|
g->size_with_redzone = GetAlignedSize(size) + kGlobalAndStackRedzone;
|
|
|
|
g->name = name;
|
|
|
|
RegisterGlobal(g);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2011-12-16 05:55:34 +08:00
|
|
|
ScopedLock lock(&mu_for_globals);
|
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;
|
2011-12-16 05:55:34 +08:00
|
|
|
ScopedLock 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.
|
|
|
|
void __asan_before_dynamic_init(uptr first_addr, uptr last_addr) {
|
|
|
|
if (!flags()->check_initialization_order) return;
|
|
|
|
CHECK(list_of_dynamic_init_globals);
|
|
|
|
ScopedLock lock(&mu_for_globals);
|
|
|
|
bool from_current_tu = false;
|
|
|
|
// The list looks like:
|
|
|
|
// a => ... => b => last_addr => ... => first_addr => c => ...
|
|
|
|
// The globals of the current TU reside between last_addr and first_addr.
|
|
|
|
for (ListOfGlobals *l = list_of_dynamic_init_globals; l; l = l->next) {
|
|
|
|
if (l->g->beg == last_addr)
|
|
|
|
from_current_tu = true;
|
|
|
|
if (!from_current_tu)
|
|
|
|
PoisonGlobalAndRedzones(l->g);
|
|
|
|
if (l->g->beg == first_addr)
|
|
|
|
from_current_tu = false;
|
|
|
|
}
|
|
|
|
CHECK(!from_current_tu);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
if (!flags()->check_initialization_order) return;
|
|
|
|
ScopedLock lock(&mu_for_globals);
|
|
|
|
for (ListOfGlobals *l = list_of_dynamic_init_globals; l; l = l->next)
|
|
|
|
UnpoisonGlobal(l->g);
|
|
|
|
}
|