[llvm] [hwasan] Add hwasan-all-globals option (PR #149621)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 22 21:45:58 PDT 2025
================
@@ -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")
----------------
shuffle2 wrote:
Reading the code a bit more, hwasan transform already checks
```c++
if (GV.hasSanitizerMetadata() && GV.getSanitizerMetadata().NoHWAddress)
continue;
```
which seems like it should have covered the same case I was trying to detect by checking the section name:
```c++
struct SanitizerMetadata {
SanitizerMetadata()
: NoAddress(false), NoHWAddress(false),
Memtag(false), IsDynInit(false) {}
// For ASan and HWASan, this instrumentation is implicitly applied to all
// global variables when built with -fsanitize=*. What we need is a way to
// persist the information that a certain global variable should *not* have
// sanitizers applied, which occurs if:
// 1. The global variable is in the sanitizer ignore list, or
// 2. The global variable is created by the sanitizers itself for internal
// usage, or
// 3. The global variable has __attribute__((no_sanitize("..."))) or
// __attribute__((disable_sanitizer_instrumentation)).
//
// This is important, a some IR passes like GlobalMerge can delete global
// variables and replace them with new ones. If the old variables were
// marked to be unsanitized, then the new ones should also be.
unsigned NoAddress : 1;
unsigned NoHWAddress : 1;
```
(case `2`)
it seems like by definition, any `new GlobalVariable` within the hwasan transform should have `NoHWAddress=1` set on it.
furthermore, the hwasan transform pass also already skips the global if `GV.getName().starts_with("llvm.")`.
```c++
if (GV.isDeclarationForLinker() || GV.getName().starts_with("llvm.") ||
GV.isThreadLocal())
continue;
```
which catches `llvm.used`, etc.
is that pre-existing check enough? Or should `GV.getSection() == "llvm.metadata"` still be checked as well?
https://llvm.org/docs/LangRef.html#intrinsic-global-variables
> This section and all globals that start with “llvm.” are reserved for use by LLVM.
A little unclear if this means intrinsic globals can be prefixed with "llvm." and be in a section not named "llvm.metadata", IMO.
https://github.com/llvm/llvm-project/pull/149621
More information about the llvm-commits
mailing list