[compiler-rt] [asan] Optimize initialization order checking (PR #101837)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 5 20:55:24 PDT 2024


================
@@ -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
----------------
MaskRay wrote:

Suggested comment:

This optimization aims to reduce the overhead of `__asan_after_dynamic_init` calls by leveraging incremental unpoisoning/poisoning in `__asan_before_dynamic_init`.
We expect most `__asan_after_dynamic_init` calls to be no-ops. However, to ensure all globals are unpoisoned before main, we force AfterDynamicInit to fully execute `__asan_after_dynamic_init`.

With lld, `AfterDynamicInit` runs after standard .init_array, making it the final `__asan_after_dynamic_init` call for the static runtime.
In contrast, GNU ld executes it earlier, causing subsequent calls to perform full unpoisoning.


https://github.com/llvm/llvm-project/pull/101837


More information about the llvm-commits mailing list