[compiler-rt] [llvm] Revert "[asan] Catch `initialization-order-fiasco` in modules without globals" (PR #104665)

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 16 22:43:53 PDT 2024


https://github.com/vitalybuka created https://github.com/llvm/llvm-project/pull/104665

Reverts llvm/llvm-project#104621

To many bots are broken, see #104621.

>From 30e7b89bc9c26267ca28f937c79b07540baa8ce3 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at gmail.com>
Date: Fri, 16 Aug 2024 22:43:22 -0700
Subject: [PATCH] =?UTF-8?q?Revert=20"[asan]=20Catch=20`initialization-orde?=
 =?UTF-8?q?r-fiasco`=20in=20modules=20without=20globals=E2=80=A6"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This reverts commit f44f02629274ea67aad23553ebc11042537758ad.
---
 .../TestCases/initialization-bug-no-global.cpp     |  3 +++
 .../Instrumentation/AddressSanitizer.cpp           | 14 +++++++++++---
 .../instrument_initializer_without_global.ll       |  2 --
 3 files changed, 14 insertions(+), 5 deletions(-)

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 369449303f3e08..50698b2a7d06fc 100644
--- a/compiler-rt/test/asan/TestCases/initialization-bug-no-global.cpp
+++ b/compiler-rt/test/asan/TestCases/initialization-bug-no-global.cpp
@@ -1,6 +1,9 @@
 // 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 34366b98aed7ae..d1bb1334aae6a3 100644
--- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -2531,10 +2531,15 @@ 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 = createPrivateGlobalForString(
-      M, M.getModuleIdentifier(), /*AllowMerging*/ false, kAsanGenPrefix);
+  GlobalVariable *ModuleName =
+      n != 0
+          ? createPrivateGlobalForString(M, M.getModuleIdentifier(),
+                                         /*AllowMerging*/ false, kAsanGenPrefix)
+          : nullptr;
 
   for (size_t i = 0; i < n; i++) {
     GlobalVariable *G = GlobalsToChange[i];
@@ -2641,6 +2646,9 @@ 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;
@@ -2680,7 +2688,7 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB, Module &M,
   }
 
   // Create calls for poisoning before initializers run and unpoisoning after.
-  if (ClInitializers)
+  if (HasDynamicallyInitializedGlobals)
     createInitializerPoisonCalls(M, ModuleName);
 
   LLVM_DEBUG(dbgs() << M);
diff --git a/llvm/test/Instrumentation/AddressSanitizer/instrument_initializer_without_global.ll b/llvm/test/Instrumentation/AddressSanitizer/instrument_initializer_without_global.ll
index b6ab4aca547a4f..c8a6541bacfdfa 100644
--- a/llvm/test/Instrumentation/AddressSanitizer/instrument_initializer_without_global.ll
+++ b/llvm/test/Instrumentation/AddressSanitizer/instrument_initializer_without_global.ll
@@ -18,9 +18,7 @@ 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(



More information about the llvm-commits mailing list