2012-11-12 16:53:47 +01:00
|
|
|
//===-- asan_globals.cc ---------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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-11-04 22:33:31 +01:00
|
|
|
#include "asan_poisoning.h"
|
2012-11-12 16:53:47 +01:00
|
|
|
#include "asan_report.h"
|
|
|
|
#include "asan_stack.h"
|
|
|
|
#include "asan_stats.h"
|
|
|
|
#include "asan_thread.h"
|
2013-11-04 22:33:31 +01:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
2013-01-23 12:41:33 +01:00
|
|
|
#include "sanitizer_common/sanitizer_mutex.h"
|
2013-11-04 22:33:31 +01:00
|
|
|
#include "sanitizer_common/sanitizer_placement_new.h"
|
2012-11-12 16:53:47 +01:00
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
|
|
|
|
typedef __asan_global Global;
|
|
|
|
|
|
|
|
struct ListOfGlobals {
|
|
|
|
const Global *g;
|
|
|
|
ListOfGlobals *next;
|
|
|
|
};
|
|
|
|
|
2013-01-23 12:41:33 +01:00
|
|
|
static BlockingMutex mu_for_globals(LINKER_INITIALIZED);
|
2012-11-12 16:53:47 +01:00
|
|
|
static LowLevelAllocator allocator_for_globals;
|
|
|
|
static ListOfGlobals *list_of_all_globals;
|
|
|
|
|
2013-11-04 22:33:31 +01:00
|
|
|
static const int kDynamicInitGlobalsInitialCapacity = 512;
|
|
|
|
struct DynInitGlobal {
|
|
|
|
Global g;
|
|
|
|
bool initialized;
|
|
|
|
};
|
|
|
|
typedef InternalMmapVector<DynInitGlobal> VectorOfGlobals;
|
|
|
|
// Lazy-initialized and never deleted.
|
|
|
|
static VectorOfGlobals *dynamic_init_globals;
|
|
|
|
|
|
|
|
ALWAYS_INLINE void PoisonShadowForGlobal(const Global *g, u8 value) {
|
|
|
|
FastPoisonShadow(g->beg, g->size_with_redzone, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE void PoisonRedZones(const Global &g) {
|
2013-02-13 11:46:01 +01:00
|
|
|
uptr aligned_size = RoundUpTo(g.size, SHADOW_GRANULARITY);
|
2013-11-04 22:33:31 +01:00
|
|
|
FastPoisonShadow(g.beg + aligned_size, g.size_with_redzone - aligned_size,
|
|
|
|
kAsanGlobalRedzoneMagic);
|
2013-02-13 11:46:01 +01:00
|
|
|
if (g.size != aligned_size) {
|
2013-11-04 22:33:31 +01:00
|
|
|
FastPoisonShadowPartialRightRedzone(
|
2013-02-13 11:46:01 +01:00
|
|
|
g.beg + RoundDownTo(g.size, SHADOW_GRANULARITY),
|
|
|
|
g.size % SHADOW_GRANULARITY,
|
|
|
|
SHADOW_GRANULARITY,
|
|
|
|
kAsanGlobalRedzoneMagic);
|
2012-11-12 16:53:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-04 22:33:31 +01:00
|
|
|
static void ReportGlobal(const Global &g, const char *prefix) {
|
|
|
|
Report("%s Global: beg=%p size=%zu/%zu name=%s module=%s dyn_init=%zu\n",
|
|
|
|
prefix, (void*)g.beg, g.size, g.size_with_redzone, g.name,
|
|
|
|
g.module_name, g.has_dynamic_init);
|
|
|
|
}
|
|
|
|
|
2013-02-13 11:46:01 +01:00
|
|
|
bool DescribeAddressIfGlobal(uptr addr, uptr size) {
|
2012-11-12 16:53:47 +01:00
|
|
|
if (!flags()->report_globals) return false;
|
2013-01-23 12:41:33 +01:00
|
|
|
BlockingMutexLock lock(&mu_for_globals);
|
2012-11-12 16:53:47 +01:00
|
|
|
bool res = false;
|
|
|
|
for (ListOfGlobals *l = list_of_all_globals; l; l = l->next) {
|
|
|
|
const Global &g = *l->g;
|
|
|
|
if (flags()->report_globals >= 2)
|
2013-11-04 22:33:31 +01:00
|
|
|
ReportGlobal(g, "Search");
|
2013-02-13 11:46:01 +01:00
|
|
|
res |= DescribeAddressRelativeToGlobal(addr, size, g);
|
2012-11-12 16:53:47 +01: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);
|
|
|
|
if (flags()->report_globals >= 2)
|
2013-11-04 22:33:31 +01:00
|
|
|
ReportGlobal(*g, "Added");
|
2012-11-12 16:53:47 +01:00
|
|
|
CHECK(flags()->report_globals);
|
|
|
|
CHECK(AddrIsInMem(g->beg));
|
|
|
|
CHECK(AddrIsAlignedByGranularity(g->beg));
|
|
|
|
CHECK(AddrIsAlignedByGranularity(g->size_with_redzone));
|
2013-11-04 22:33:31 +01:00
|
|
|
if (flags()->poison_heap)
|
|
|
|
PoisonRedZones(*g);
|
2013-12-05 10:18:38 +01:00
|
|
|
ListOfGlobals *l = new(allocator_for_globals) ListOfGlobals;
|
2012-11-12 16:53:47 +01:00
|
|
|
l->g = g;
|
|
|
|
l->next = list_of_all_globals;
|
|
|
|
list_of_all_globals = l;
|
|
|
|
if (g->has_dynamic_init) {
|
2013-11-04 22:33:31 +01:00
|
|
|
if (dynamic_init_globals == 0) {
|
2013-12-05 10:18:38 +01:00
|
|
|
dynamic_init_globals = new(allocator_for_globals)
|
2013-11-04 22:33:31 +01:00
|
|
|
VectorOfGlobals(kDynamicInitGlobalsInitialCapacity);
|
|
|
|
}
|
|
|
|
DynInitGlobal dyn_global = { *g, false };
|
|
|
|
dynamic_init_globals->push_back(dyn_global);
|
2012-11-12 16:53:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void UnregisterGlobal(const Global *g) {
|
|
|
|
CHECK(asan_inited);
|
|
|
|
CHECK(flags()->report_globals);
|
|
|
|
CHECK(AddrIsInMem(g->beg));
|
|
|
|
CHECK(AddrIsAlignedByGranularity(g->beg));
|
|
|
|
CHECK(AddrIsAlignedByGranularity(g->size_with_redzone));
|
2013-11-04 22:33:31 +01:00
|
|
|
if (flags()->poison_heap)
|
|
|
|
PoisonShadowForGlobal(g, 0);
|
2012-11-12 16:53:47 +01: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-11-04 22:33:31 +01: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);
|
|
|
|
}
|
2012-11-12 16:53:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace __asan
|
|
|
|
|
|
|
|
// ---------------------- Interface ---------------- {{{1
|
|
|
|
using namespace __asan; // NOLINT
|
|
|
|
|
|
|
|
// Register an array of globals.
|
|
|
|
void __asan_register_globals(__asan_global *globals, uptr n) {
|
|
|
|
if (!flags()->report_globals) return;
|
2013-01-23 12:41:33 +01:00
|
|
|
BlockingMutexLock lock(&mu_for_globals);
|
2012-11-12 16:53:47 +01:00
|
|
|
for (uptr i = 0; i < n; i++) {
|
|
|
|
RegisterGlobal(&globals[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unregister an array of globals.
|
|
|
|
// We must do this when a shared objects gets dlclosed.
|
|
|
|
void __asan_unregister_globals(__asan_global *globals, uptr n) {
|
|
|
|
if (!flags()->report_globals) return;
|
2013-01-23 12:41:33 +01:00
|
|
|
BlockingMutexLock lock(&mu_for_globals);
|
2012-11-12 16:53:47 +01:00
|
|
|
for (uptr i = 0; i < n; i++) {
|
|
|
|
UnregisterGlobal(&globals[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-11-04 22:33:31 +01:00
|
|
|
void __asan_before_dynamic_init(const char *module_name) {
|
|
|
|
if (!flags()->check_initialization_order ||
|
|
|
|
!flags()->poison_heap)
|
|
|
|
return;
|
|
|
|
bool strict_init_order = flags()->strict_init_order;
|
|
|
|
CHECK(dynamic_init_globals);
|
|
|
|
CHECK(module_name);
|
|
|
|
CHECK(asan_inited);
|
2013-01-23 12:41:33 +01:00
|
|
|
BlockingMutexLock lock(&mu_for_globals);
|
2013-11-04 22:33:31 +01: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) {
|
|
|
|
DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];
|
|
|
|
const Global *g = &dyn_g.g;
|
|
|
|
if (dyn_g.initialized)
|
|
|
|
continue;
|
|
|
|
if (g->module_name != module_name)
|
|
|
|
PoisonShadowForGlobal(g, kAsanInitializationOrderMagic);
|
|
|
|
else if (!strict_init_order)
|
|
|
|
dyn_g.initialized = true;
|
2012-11-12 16:53:47 +01: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-11-04 22:33:31 +01:00
|
|
|
if (!flags()->check_initialization_order ||
|
|
|
|
!flags()->poison_heap)
|
|
|
|
return;
|
|
|
|
CHECK(asan_inited);
|
2013-01-23 12:41:33 +01:00
|
|
|
BlockingMutexLock lock(&mu_for_globals);
|
2013-11-04 22:33:31 +01:00
|
|
|
// FIXME: Optionally report that we're unpoisoning globals from a module.
|
|
|
|
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;
|
|
|
|
if (!dyn_g.initialized) {
|
|
|
|
// Unpoison the whole global.
|
|
|
|
PoisonShadowForGlobal(g, 0);
|
|
|
|
// Poison redzones back.
|
|
|
|
PoisonRedZones(*g);
|
|
|
|
}
|
|
|
|
}
|
2012-11-12 16:53:47 +01:00
|
|
|
}
|