Skip to content

Commit

Permalink
[NFC][asan] Switch to IntrusiveList in asan_globals (#101577)
Browse files Browse the repository at this point in the history
It's preparation for switching to hash table in #101596.
  • Loading branch information
vitalybuka authored Aug 2, 2024
1 parent 46bc11d commit 54a940b
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions compiler-rt/lib/asan/asan_globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,13 @@ typedef IntrusiveList<GlobalListNode> ListOfGlobals;
static Mutex mu_for_globals;
static ListOfGlobals list_of_all_globals SANITIZER_GUARDED_BY(mu_for_globals);

static const int kDynamicInitGlobalsInitialCapacity = 512;
struct DynInitGlobal {
Global g;
bool initialized;
Global g = {};
bool initialized = false;
DynInitGlobal *next = nullptr;
};
typedef InternalMmapVector<DynInitGlobal> VectorOfGlobals;
// Lazy-initialized and never deleted.
static VectorOfGlobals *dynamic_init_globals
SANITIZER_GUARDED_BY(mu_for_globals);
typedef IntrusiveList<DynInitGlobal> DynInitGlobals;
static DynInitGlobals dynamic_init_globals SANITIZER_GUARDED_BY(mu_for_globals);

// We want to remember where a certain range of globals was registered.
struct GlobalRegistrationSite {
Expand Down Expand Up @@ -259,12 +257,8 @@ static void RegisterGlobal(const Global *g) SANITIZER_REQUIRES(mu_for_globals) {
AddGlobalToList(list_of_all_globals, g);

if (g->has_dynamic_init) {
if (!dynamic_init_globals) {
dynamic_init_globals = new (GetGlobalLowLevelAllocator()) VectorOfGlobals;
dynamic_init_globals->reserve(kDynamicInitGlobalsInitialCapacity);
}
DynInitGlobal dyn_global = { *g, false };
dynamic_init_globals->push_back(dyn_global);
dynamic_init_globals.push_back(new (GetGlobalLowLevelAllocator())
DynInitGlobal{*g, false});
}
}

Expand Down Expand Up @@ -294,11 +288,8 @@ void StopInitOrderChecking() {
if (!flags()->check_initialization_order)
return;
Lock lock(&mu_for_globals);
if (!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];
for (const DynInitGlobal &dyn_g : dynamic_init_globals) {
const Global *g = &dyn_g.g;
// Unpoison the whole global.
PoisonShadowForGlobal(g, 0);
Expand Down Expand Up @@ -465,12 +456,9 @@ void __asan_before_dynamic_init(const char *module_name) {
CHECK(module_name);
CHECK(AsanInited());
Lock lock(&mu_for_globals);
if (!dynamic_init_globals)
return;
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];
for (DynInitGlobal &dyn_g : dynamic_init_globals) {
const Global *g = &dyn_g.g;
if (dyn_g.initialized)
continue;
Expand All @@ -489,11 +477,8 @@ void __asan_after_dynamic_init() {
return;
CHECK(AsanInited());
Lock lock(&mu_for_globals);
if (!dynamic_init_globals)
return;
// 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];
for (const DynInitGlobal &dyn_g : dynamic_init_globals) {
const Global *g = &dyn_g.g;
if (!dyn_g.initialized) {
// Unpoison the whole global.
Expand Down

0 comments on commit 54a940b

Please sign in to comment.