Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NFC][asan] Track current dynamic init module #101597

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading