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

[asan] Optimize initialization order checking #101837

40 changes: 40 additions & 0 deletions compiler-rt/lib/asan/asan_globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,44 @@ void __asan_before_dynamic_init(const char *module_name) {
current_dynamic_init_module_name = module_name;
}

// Maybe SANITIZER_CAN_USE_PREINIT_ARRAY is to conservative for `.init_array`,
// however we should not make mistake here. If `AfterDynamicInit` was not
// executed at all we will have false reports on globals.
#if SANITIZER_CAN_USE_PREINIT_ARRAY
// This is optimization. We will ignore all `__asan_after_dynamic_init`, but the
vitalybuka marked this conversation as resolved.
Show resolved Hide resolved
// last `__asan_after_dynamic_init`. We don't need number of
// `__asan_{before,after}_dynamic_init` matches, but we need that the last call
// was to `__asan_after_dynamic_init`, as it will unpoison all global preparing
// program for `main` execution. To run `__asan_after_dynamic_init` later, we
// will register in `.init_array`.
static bool allow_after_dynamic_init SANITIZER_GUARDED_BY(mu_for_globals) =
false;

static void __attribute__((used)) AfterDynamicInit(void) {
vitalybuka marked this conversation as resolved.
Show resolved Hide resolved
{
Lock lock(&mu_for_globals);
if (allow_after_dynamic_init)
return;
allow_after_dynamic_init = true;
}
if (flags()->report_globals >= 3)
Printf("AfterDynamicInit\n");
__asan_after_dynamic_init();
}

// 65537 will make it run after constructors with default priority, but it
vitalybuka marked this conversation as resolved.
Show resolved Hide resolved
// requires ld.lld. With ld.bfd it can be called to early, and fail the
// optimization. However, correctness should not be affected, as after the first
// call all subsequent `__asan_after_dynamic_init` will be allowed.
__attribute__((section(".init_array.65537"), used)) static void (
*asan_after_init_array)(void) = AfterDynamicInit;
#else
// Allow all `__asan_after_dynamic_init` if `AfterDynamicInit` is not set.
vitalybuka marked this conversation as resolved.
Show resolved Hide resolved
// Compiler still generates `__asan_{before,after}_dynamic_init`in pairs, and
// it's guaranteed that `__asan_after_dynamic_init` will be the last.
static constexpr bool allow_after_dynamic_init = true;
vitalybuka marked this conversation as resolved.
Show resolved Hide resolved
#endif // SANITIZER_CAN_USE_PREINIT_ARRAY

// 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.
Expand All @@ -528,6 +566,8 @@ void __asan_after_dynamic_init() {
return;
CHECK(AsanInited());
Lock lock(&mu_for_globals);
if (!allow_after_dynamic_init)
return;
if (!current_dynamic_init_module_name)
return;

Expand Down
14 changes: 14 additions & 0 deletions compiler-rt/test/asan/TestCases/initialization-nobug-lld.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %clangxx_asan -O3 %p/initialization-nobug.cpp %p/Helpers/initialization-nobug-extra.cpp -fuse-ld=lld -o %t && %env_asan_opts=check_initialization_order=true:report_globals=3 %run %t 2>&1 | FileCheck %s --implicit-check-not "DynInit"
vitalybuka marked this conversation as resolved.
Show resolved Hide resolved

// Same as initialization-nobug.cpp, but with lld we expect just one
// `DynInitUnpoison` executed after `AfterDynamicInit` at the end.
// REQUIRES: lld-available

// With dynamic runtimes `AfterDynamicInit` will called before `executable`
// contructors, with constructors of dynamic runtime.
// XFAIL: asan-dynamic-runtime

// CHECK: DynInitPoison module: {{.*}}initialization-nobug.cpp
// CHECK: DynInitPoison module: {{.*}}initialization-nobug-extra.cpp
// CHECK: AfterDynamicInit
// CHECK: DynInitUnpoison
14 changes: 9 additions & 5 deletions compiler-rt/test/asan/TestCases/initialization-nobug.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// A collection of various initializers which shouldn't trip up initialization
// order checking. If successful, this will just return 0.

// RUN: %clangxx_asan -O0 %s %p/Helpers/initialization-nobug-extra.cpp -o %t && %env_asan_opts=check_initialization_order=true:report_globals=3 %run %t 2>&1 | FileCheck %s --implicit-check-not "DynInit"
// RUN: %clangxx_asan -O1 %s %p/Helpers/initialization-nobug-extra.cpp -o %t && %env_asan_opts=check_initialization_order=true:report_globals=3 %run %t 2>&1 | FileCheck %s --implicit-check-not "DynInit"
// RUN: %clangxx_asan -O2 %s %p/Helpers/initialization-nobug-extra.cpp -o %t && %env_asan_opts=check_initialization_order=true:report_globals=3 %run %t 2>&1 | FileCheck %s --implicit-check-not "DynInit"
// RUN: %clangxx_asan -O3 %s %p/Helpers/initialization-nobug-extra.cpp -o %t && %env_asan_opts=check_initialization_order=true:report_globals=3 %run %t 2>&1 | FileCheck %s --implicit-check-not "DynInit"
// RUN: %clangxx_asan -O0 %s %p/Helpers/initialization-nobug-extra.cpp -o %t && %env_asan_opts=check_initialization_order=true:report_globals=3 %run %t 2>&1 | FileCheck %s --implicit-check-not "DynInitPoison"
// RUN: %clangxx_asan -O1 %s %p/Helpers/initialization-nobug-extra.cpp -o %t && %env_asan_opts=check_initialization_order=true:report_globals=3 %run %t 2>&1 | FileCheck %s --implicit-check-not "DynInitPoison"
// RUN: %clangxx_asan -O2 %s %p/Helpers/initialization-nobug-extra.cpp -o %t && %env_asan_opts=check_initialization_order=true:report_globals=3 %run %t 2>&1 | FileCheck %s --implicit-check-not "DynInitPoison"
// RUN: %clangxx_asan -O3 %s %p/Helpers/initialization-nobug-extra.cpp -o %t && %env_asan_opts=check_initialization_order=true:report_globals=3 %run %t 2>&1 | FileCheck %s --implicit-check-not "DynInitPoison"

// Simple access:
// Make sure that accessing a global in the same TU is safe
Expand Down Expand Up @@ -44,6 +44,10 @@ int getStructWithDtorValue() { return struct_with_dtor.value; }
int main() { return 0; }

// CHECK: DynInitPoison
// CHECK: DynInitUnpoison
// CHECK: DynInitPoison

// In general case entire set of DynInitPoison must be followed by at lest one
// DynInitUnpoison. In some cases we can limit the number of DynInitUnpoison,
// see initialization-nobug-lld.cpp.

// CHECK: DynInitUnpoison
Loading