[llvm] [ASan][X86] Mark asan_globals section large (PR #74514)

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 7 13:48:04 PST 2023


https://github.com/aeubanks updated https://github.com/llvm/llvm-project/pull/74514

>From c651790ec812cc227d4cf2815060ec8a8e098b97 Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks at google.com>
Date: Tue, 5 Dec 2023 10:03:36 -0800
Subject: [PATCH 1/2] [ASan][X86] Mark asan_globals section large

We'd like to make the asan_globals section large to make it not
contribute to relocation pressure since there are no direct PC32
references to it.

Following #74498, we can do that by marking the code model for the
global explicitly large.

Without this change, asan_globals gets placed between .data and .bss.
With this change, it gets placed after .bss.
---
 .../Transforms/Instrumentation/AddressSanitizer.cpp    |  5 +++++
 .../AddressSanitizer/global_metadata_code_model.ll     | 10 ++++++++++
 .../AddressSanitizer/global_with_comdat.ll             |  4 ++--
 3 files changed, 17 insertions(+), 2 deletions(-)
 create mode 100644 llvm/test/Instrumentation/AddressSanitizer/global_metadata_code_model.ll

diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
index da157c966bfcb..a86198591808a 100644
--- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -2144,6 +2144,11 @@ ModuleAddressSanitizer::CreateMetadataGlobal(Module &M, Constant *Initializer,
       M, Initializer->getType(), false, Linkage, Initializer,
       Twine("__asan_global_") + GlobalValue::dropLLVMManglingEscape(OriginalName));
   Metadata->setSection(getGlobalMetadataSection());
+  // Move metadata section out of the way for x86-64 ELF binaries to mitigate
+  // relocation pressure.
+  if (TargetTriple.getArch() == Triple::x86_64 &&
+      TargetTriple.getObjectFormat() == Triple::ELF)
+    Metadata->setCodeModel(CodeModel::Large);
   return Metadata;
 }
 
diff --git a/llvm/test/Instrumentation/AddressSanitizer/global_metadata_code_model.ll b/llvm/test/Instrumentation/AddressSanitizer/global_metadata_code_model.ll
new file mode 100644
index 0000000000000..c1e7694d4cd53
--- /dev/null
+++ b/llvm/test/Instrumentation/AddressSanitizer/global_metadata_code_model.ll
@@ -0,0 +1,10 @@
+; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes=asan -S | FileCheck %s --check-prefix=LARGE
+; RUN: opt < %s -mtriple=aarch64-unknown-linux-gnu -passes=asan -S | FileCheck %s --check-prefix=NORMAL
+; RUN: opt < %s -mtriple=x86_64-pc-windows -passes=asan -S | FileCheck %s --check-prefix=NORMAL
+
+; check that asan globals metadata are emitted to a large section for x86-64 ELF
+
+; LARGE: @__asan_global_global = {{.*}}global {{.*}}, code_model "large"
+; NORMAL-NOT: code_model "large"
+
+ at global = global i32 0, align 4
diff --git a/llvm/test/Instrumentation/AddressSanitizer/global_with_comdat.ll b/llvm/test/Instrumentation/AddressSanitizer/global_with_comdat.ll
index 699b8287d358a..74f8fc9997d40 100644
--- a/llvm/test/Instrumentation/AddressSanitizer/global_with_comdat.ll
+++ b/llvm/test/Instrumentation/AddressSanitizer/global_with_comdat.ll
@@ -102,8 +102,8 @@ target triple = "x86_64-unknown-linux-gnu"
 ;; Don't place the instrumented globals in a comdat when the unique module ID is empty.
 ; NOMODULEID:      @.str = internal constant { [4 x i8], [28 x i8] } { [4 x i8] c"str\00", [28 x i8] zeroinitializer }, align 32
 ; NOMODULEID:      @_ZL3buf = internal global { [4 x i8], [28 x i8] } zeroinitializer, align 32
-; NOMODULEID:      @__asan_global_.str = private global {{.*}}, section "asan_globals", !associated !0
-; NOMODULEID:      @__asan_global__ZL3buf = private global {{.*}}, section "asan_globals", !associated !1
+; NOMODULEID:      @__asan_global_.str = private global {{.*}}, section "asan_globals"{{.*}}, !associated !0
+; NOMODULEID:      @__asan_global__ZL3buf = private global {{.*}}, section "asan_globals"{{.*}}, !associated !1
 ; NOMODULEID:      @llvm.compiler.used = appending global [4 x ptr] [ptr @.str, ptr @_ZL3buf, ptr @__asan_global_.str, ptr @__asan_global__ZL3buf]
 
 ; NOMODULEID:      define internal void @asan.module_ctor() #[[#]] comdat {

>From 88ca657d13e1c3d3280f18deffd0ca8864722c21 Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks at google.com>
Date: Thu, 7 Dec 2023 13:47:51 -0800
Subject: [PATCH 2/2] update comment

---
 llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
index a86198591808a..3a3b41f5dc87d 100644
--- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -2144,7 +2144,7 @@ ModuleAddressSanitizer::CreateMetadataGlobal(Module &M, Constant *Initializer,
       M, Initializer->getType(), false, Linkage, Initializer,
       Twine("__asan_global_") + GlobalValue::dropLLVMManglingEscape(OriginalName));
   Metadata->setSection(getGlobalMetadataSection());
-  // Move metadata section out of the way for x86-64 ELF binaries to mitigate
+  // Place metadata in a large section for x86-64 ELF binaries to mitigate
   // relocation pressure.
   if (TargetTriple.getArch() == Triple::x86_64 &&
       TargetTriple.getObjectFormat() == Triple::ELF)



More information about the llvm-commits mailing list