[llvm] [hwasan] Add hwasan-all-globals option (PR #149621)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 18 22:00:01 PDT 2025


https://github.com/shuffle2 updated https://github.com/llvm/llvm-project/pull/149621

>From e7522ae941d2f4d80fa1d7f3d6641a3daf7891b1 Mon Sep 17 00:00:00 2001
From: Shawn Hoffman <godisgovernment at gmail.com>
Date: Fri, 18 Jul 2025 15:56:28 -0700
Subject: [PATCH] [hwasan] Add hwasan-all-globals option hwasan-globals does
 not instrument globals with custom sections, because existing code may use
 __start_/__stop_ symbols to iterate over globals in such a way which will
 cause hwasan assertions.

Introduce new hwasan-all-globals option, which instruments all
user-defined globals (but not those globals which are generated by
the hwasan instrumentation itself), including those with custom sections.
---
 .../Instrumentation/HWAddressSanitizer.cpp    | 30 +++++++++++++++----
 .../HWAddressSanitizer/globals.ll             |  4 ++-
 2 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
index 2c34bf2157cdd..439c1f28571ff 100644
--- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -160,6 +160,16 @@ static cl::opt<bool> ClGenerateTagsWithCalls(
 static cl::opt<bool> ClGlobals("hwasan-globals", cl::desc("Instrument globals"),
                                cl::Hidden, cl::init(false));
 
+static cl::opt<bool> ClAllGlobals(
+    "hwasan-all-globals",
+    cl::desc(
+        "Instrument globals, even those within user-defined sections. Warning: "
+        "This may break existing code which walks globals via linker-generated "
+        "symbols, expects certain globals to be contiguous with each other, or "
+        "makes other assumptions which are invalidated by HWASan "
+        "instrumentation."),
+    cl::Hidden, cl::init(false));
+
 static cl::opt<int> ClMatchAllTag(
     "hwasan-match-all-tag",
     cl::desc("don't report bad accesses via pointers with this tag"),
@@ -1780,11 +1790,21 @@ void HWAddressSanitizer::instrumentGlobals() {
     if (GV.hasCommonLinkage())
       continue;
 
-    // Globals with custom sections may be used in __start_/__stop_ enumeration,
-    // which would be broken both by adding tags and potentially by the extra
-    // padding/alignment that we insert.
-    if (GV.hasSection())
-      continue;
+    if (ClAllGlobals) {
+      // Avoid adding metadata emitted for the hwasan instrumentation itself.
+      // Code which makes assumptions about memory layout of globals between
+      // __start_<section>/__end_<section> linker-generated symbols may need
+      // manual adaptation.
+      auto section = GV.getSection();
+      if (section == "hwasan_globals" || section == ".note.hwasan.globals")
+        continue;
+    } else {
+      // Globals with custom sections may be used in __start_/__stop_
+      // enumeration, which would be broken both by adding tags and potentially
+      // by the extra padding/alignment that we insert.
+      if (GV.hasSection())
+        continue;
+    }
 
     Globals.push_back(&GV);
   }
diff --git a/llvm/test/Instrumentation/HWAddressSanitizer/globals.ll b/llvm/test/Instrumentation/HWAddressSanitizer/globals.ll
index f5ae1c0f80497..5739b49985482 100644
--- a/llvm/test/Instrumentation/HWAddressSanitizer/globals.ll
+++ b/llvm/test/Instrumentation/HWAddressSanitizer/globals.ll
@@ -1,10 +1,12 @@
 ; RUN: opt < %s -S -passes=hwasan -mtriple=aarch64--linux-android29 | FileCheck --check-prefixes=CHECK,CHECK29 %s
 ; RUN: opt < %s -S -passes=hwasan -mtriple=aarch64--linux-android30 | FileCheck --check-prefixes=CHECK,CHECK30 %s
+; RUN: opt < %s -S -passes=hwasan -mtriple=riscv64-unknown-elf -hwasan-globals=1 -hwasan-all-globals=1 | FileCheck --check-prefixes=CHECKALLGLOBALS %s
 
 ; CHECK29: @four = global
 
-; CHECK: @specialcaselisted = global i16 2, no_sanitize_hwaddress
+; CHECK,CHECKALLGLOBALS: @specialcaselisted = global i16 2, no_sanitize_hwaddress
 ; CHECK: @insection = global i16 2, section "custom"
+; CHECKALLGLOBALS: @insection.hwasan = private global { i16, [14 x i8] } { i16 2, [14 x i8] c"\00\00\00\00\00\00\00\00\00\00\00\00\00\AF" }, section "custom", align 16
 ; CHECK: @__start_hwasan_globals = external hidden constant [0 x i8]
 ; CHECK: @__stop_hwasan_globals = external hidden constant [0 x i8]
 



More information about the llvm-commits mailing list