[llvm-branch-commits] [asan] Catch `initialization-order-fiasco` in mudules without globals (PR #104621)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Aug 16 10:28:55 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-compiler-rt-sanitizer
Author: Vitaly Buka (vitalybuka)
<details>
<summary>Changes</summary>
Thouse modules still can have global constructors and access
globals in other modules which are not initialized yet.
---
Full diff: https://github.com/llvm/llvm-project/pull/104621.diff
4 Files Affected:
- (modified) compiler-rt/test/asan/TestCases/initialization-bug-no-global.cpp (-3)
- (modified) llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp (+3-11)
- (modified) llvm/lib/Transforms/Instrumentation/CMakeLists.txt (+2)
- (modified) llvm/test/Instrumentation/AddressSanitizer/instrument_initializer_without_global.ll (+2)
``````````diff
diff --git a/compiler-rt/test/asan/TestCases/initialization-bug-no-global.cpp b/compiler-rt/test/asan/TestCases/initialization-bug-no-global.cpp
index 8249abf804324a..a8243016bdcf66 100644
--- a/compiler-rt/test/asan/TestCases/initialization-bug-no-global.cpp
+++ b/compiler-rt/test/asan/TestCases/initialization-bug-no-global.cpp
@@ -1,9 +1,6 @@
// RUN: %clangxx_asan %min_macos_deployment_target=10.11 -O0 %s %p/Helpers/initialization-bug-extra.cpp -o %t
// RUN: %env_asan_opts=check_initialization_order=true:strict_init_order=true not %run %t 2>&1 | FileCheck %s
-// Not implemented.
-// XFAIL: *
-
// Do not test with optimization -- the error may be optimized away.
// FIXME: https://code.google.com/p/address-sanitizer/issues/detail?id=186
diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
index d1bb1334aae6a3..34366b98aed7ae 100644
--- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -2531,15 +2531,10 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB, Module &M,
SmallVector<GlobalVariable *, 16> NewGlobals(n);
SmallVector<Constant *, 16> Initializers(n);
- bool HasDynamicallyInitializedGlobals = false;
-
// We shouldn't merge same module names, as this string serves as unique
// module ID in runtime.
- GlobalVariable *ModuleName =
- n != 0
- ? createPrivateGlobalForString(M, M.getModuleIdentifier(),
- /*AllowMerging*/ false, kAsanGenPrefix)
- : nullptr;
+ GlobalVariable *ModuleName = createPrivateGlobalForString(
+ M, M.getModuleIdentifier(), /*AllowMerging*/ false, kAsanGenPrefix);
for (size_t i = 0; i < n; i++) {
GlobalVariable *G = GlobalsToChange[i];
@@ -2646,9 +2641,6 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB, Module &M,
Constant::getNullValue(IntptrTy),
ConstantExpr::getPointerCast(ODRIndicator, IntptrTy));
- if (ClInitializers && MD.IsDynInit)
- HasDynamicallyInitializedGlobals = true;
-
LLVM_DEBUG(dbgs() << "NEW GLOBAL: " << *NewGlobal << "\n");
Initializers[i] = Initializer;
@@ -2688,7 +2680,7 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB, Module &M,
}
// Create calls for poisoning before initializers run and unpoisoning after.
- if (HasDynamicallyInitializedGlobals)
+ if (ClInitializers)
createInitializerPoisonCalls(M, ModuleName);
LLVM_DEBUG(dbgs() << M);
diff --git a/llvm/lib/Transforms/Instrumentation/CMakeLists.txt b/llvm/lib/Transforms/Instrumentation/CMakeLists.txt
index deab37801ff1df..99efa37d11572f 100644
--- a/llvm/lib/Transforms/Instrumentation/CMakeLists.txt
+++ b/llvm/lib/Transforms/Instrumentation/CMakeLists.txt
@@ -43,3 +43,5 @@ add_llvm_component_library(LLVMInstrumentation
TransformUtils
ProfileData
)
+
+ set_property(TARGET LLVMInstrumentation APPEND_STRING PROPERTY COMPILE_FLAGS " -g -O0")
\ No newline at end of file
diff --git a/llvm/test/Instrumentation/AddressSanitizer/instrument_initializer_without_global.ll b/llvm/test/Instrumentation/AddressSanitizer/instrument_initializer_without_global.ll
index c8a6541bacfdfa..b6ab4aca547a4f 100644
--- a/llvm/test/Instrumentation/AddressSanitizer/instrument_initializer_without_global.ll
+++ b/llvm/test/Instrumentation/AddressSanitizer/instrument_initializer_without_global.ll
@@ -18,7 +18,9 @@ define internal void @__late_ctor() sanitize_address section ".text.startup" {
; CHECK-LABEL: define internal void @__late_ctor(
; CHECK-SAME: ) #[[ATTR1:[0-9]+]] section ".text.startup" {
; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: call void @__asan_before_dynamic_init(i64 ptrtoint (ptr @___asan_gen_ to i64))
; CHECK-NEXT: call void @initializer()
+; CHECK-NEXT: call void @__asan_after_dynamic_init()
; CHECK-NEXT: ret void
;
; NOINIT-LABEL: define internal void @__late_ctor(
``````````
</details>
https://github.com/llvm/llvm-project/pull/104621
More information about the llvm-branch-commits
mailing list