Skip to content

Commit

Permalink
[NFC][asan] Track current dynamic init module (llvm#101597)
Browse files Browse the repository at this point in the history
This patch allows sequences like:
`__asan_before_dynamic_init`
`__asan_before_dynamic_init`
...
`__asan_before_dynamic_init`
to do minimal incrementa poisoning.

It's NFC as now callbacks invokes in pairs:
`__asan_before_dynamic_init`
`__asan_after_dynamic_init`
`__asan_before_dynamic_init`
`__asan_after_dynamic_init`
and `__asan_after_dynamic_init` unpoisons
everything anyway.

For llvm#101837
  • Loading branch information
vitalybuka authored and kstoimenov committed Aug 15, 2024
1 parent 38406b9 commit 931fd9e
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions compiler-rt/lib/asan/asan_globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ static ListOfGlobals &GlobalsByIndicator(uptr odr_indicator)
return (*globals_by_indicator)[odr_indicator];
}

static const char *current_dynamic_init_module_name
SANITIZER_GUARDED_BY(mu_for_globals) = nullptr;

using DynInitGlobalsByModule =
DenseMap<const char *, IntrusiveList<DynInitGlobal>>;

Expand Down Expand Up @@ -492,18 +495,29 @@ void __asan_before_dynamic_init(const char *module_name) {
CHECK(module_name);
CHECK(AsanInited());
Lock lock(&mu_for_globals);
if (current_dynamic_init_module_name == module_name)
return;
if (flags()->report_globals >= 3)
Printf("DynInitPoison module: %s\n", module_name);

DynInitGlobals().forEach([&](auto &kv) {
if (kv.first != module_name) {
PoisonDynamicGlobals(kv.second);
} else {
UnpoisonDynamicGlobals(kv.second,
/*mark_initialized=*/!strict_init_order);
}
return true;
});
if (current_dynamic_init_module_name == nullptr) {
// First call, poison all globals from other modules.
DynInitGlobals().forEach([&](auto &kv) {
if (kv.first != module_name) {
PoisonDynamicGlobals(kv.second);
} else {
UnpoisonDynamicGlobals(kv.second,
/*mark_initialized=*/!strict_init_order);
}
return true;
});
} else {
// Module changed.
PoisonDynamicGlobals(DynInitGlobals()[current_dynamic_init_module_name]);
UnpoisonDynamicGlobals(DynInitGlobals()[module_name],
/*mark_initialized=*/!strict_init_order);
}
current_dynamic_init_module_name = module_name;
}

// This method runs immediately after dynamic initialization in each TU, when
Expand All @@ -514,11 +528,16 @@ void __asan_after_dynamic_init() {
return;
CHECK(AsanInited());
Lock lock(&mu_for_globals);
if (!current_dynamic_init_module_name)
return;

if (flags()->report_globals >= 3)
Printf("DynInitUnpoison\n");

DynInitGlobals().forEach([&](auto &kv) {
UnpoisonDynamicGlobals(kv.second, /*mark_initialized=*/false);
return true;
});

current_dynamic_init_module_name = nullptr;
}

0 comments on commit 931fd9e

Please sign in to comment.