Skip to content

[hwasan] Don't check code model if there are no globals #131152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 13, 2025

Conversation

thurstond
Copy link
Contributor

@thurstond thurstond commented Mar 13, 2025

Currently, the code model check is always performed even if there are no globals, because:

  1. the HWASan compiler pass always leaves a note
  2. the HWASan runtime always performs the check if there is a HWASan globals note.
    This unnecessarily adds a 2**32 byte size limit.

This patch elides the check if the globals note doesn't actually contain globals, thus allowing larger libraries to be successfully instrumented without globals.

Sent from my iPhone

Currently, the code model check is always performed even if there are no real globals, because the HWASan compiler pass always leaves a note. This unnecessarily adds a 2**32 byte size limit.

This patch elides the check if the globals note doesn't actually contain globals, thus allowing larger libraries to be successfully instrumented without globals.

Sent from my iPhone
@llvmbot
Copy link
Member

llvmbot commented Mar 13, 2025

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Thurston Dang (thurstond)

Changes

Currently, the code model check is always performed even if there are no real globals, because the HWASan compiler pass always leaves a note. This unnecessarily adds a 2**32 byte size limit.

This patch elides the check if the globals note doesn't actually contain globals, thus allowing larger libraries to be successfully instrumented without globals.

Sent from my iPhone


Full diff: https://github.com/llvm/llvm-project/pull/131152.diff

1 Files Affected:

  • (modified) compiler-rt/lib/hwasan/hwasan_globals.cpp (+9-4)
diff --git a/compiler-rt/lib/hwasan/hwasan_globals.cpp b/compiler-rt/lib/hwasan/hwasan_globals.cpp
index 7e0f3df20dd05..e2723450e26ec 100644
--- a/compiler-rt/lib/hwasan/hwasan_globals.cpp
+++ b/compiler-rt/lib/hwasan/hwasan_globals.cpp
@@ -73,16 +73,21 @@ ArrayRef<const hwasan_global> HwasanGlobalsFor(ElfW(Addr) base,
         continue;
       }
 
-      // Only libraries with instrumented globals need to be checked against the
-      // code model since they use relocations that aren't checked at link time.
-      CheckCodeModel(base, phdr, phnum);
-
       auto *global_note = reinterpret_cast<const hwasan_global_note *>(desc);
       auto *globals_begin = reinterpret_cast<const hwasan_global *>(
           note + global_note->begin_relptr);
       auto *globals_end = reinterpret_cast<const hwasan_global *>(
           note + global_note->end_relptr);
 
+      // Only libraries with instrumented globals need to be checked against the
+      // code model since they use relocations that aren't checked at link time.
+      //
+      // There is always a HWASan globals note ("Create the note even if we
+      // aren't instrumenting globals." - HWAddressSanitizer.cpp), but we can
+      // elide the code model check if there are no real globals.
+      if (globals_begin != globals_end)
+        CheckCodeModel(base, phdr, phnum);
+
       return {globals_begin, globals_end};
     }
   }

@fmayer
Copy link
Contributor

fmayer commented Mar 13, 2025

What makes a global real vs unreal?

@thurstond
Copy link
Contributor Author

What makes a global real vs unreal?

  // Create a zero-length global in hwasan_globals so that the linker will
  // always create start and stop symbols.

Zero-length globals aren't real, they can't hurt you

except they can hurt because the runtime will still perform the code model check.

Copy link
Contributor

@fmayer fmayer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A real good change.

nit: you still have "real" in the commit message

@thurstond thurstond changed the title [hwasan] Don't check code model if there are no real globals [hwasan] Don't check code model if there are no globals Mar 13, 2025
@thurstond thurstond merged commit 143bf95 into llvm:main Mar 13, 2025
8 of 9 checks passed
frederik-h pushed a commit to frederik-h/llvm-project that referenced this pull request Mar 18, 2025
Currently, the code model check is always performed even if there are no
globals, because:
1) the HWASan compiler pass always leaves a note
2) the HWASan runtime always performs the check if there is a HWASan
globals note.
This unnecessarily adds a 2**32 byte size limit.

This patch elides the check if the globals note doesn't actually contain
globals, thus allowing larger libraries to be successfully instrumented
without globals.

Sent from my iPhone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants