Skip to content

[ASan] Move early exit checks outside "instrumentFunction()" to avoid… #133285

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

Conversation

HankChang736
Copy link
Contributor

@HankChang736 HankChang736 commented Mar 27, 2025

… unnecessary FunctionSanitizer construction (NFC)

This patch moves several early-exit checks (e.g., empty function, etc.) out of AddressSanitizer::instrumentFunction and into the caller. This change avoids unnecessary construction of FunctionSanitizer when instrumentation is not needed.

… unnecessary FunctionSanitizer construction (NFC)

This patch moves several early-exit checks (e.g., empty function, etc.) out of "AddressSanitizer::instrumentFunction" and into the caller.
This change avoids unnecessary construction of FunctionSanitizer when instrumentation is not needed.
@llvmbot
Copy link
Member

llvmbot commented Mar 27, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Hank Chang (HankChang736)

Changes

… unnecessary FunctionSanitizer construction (NFC)

This patch moves several early-exit checks (e.g., empty function, etc.) out of "AddressSanitizer::instrumentFunction" and into the caller. This change avoids unnecessary construction of FunctionSanitizer when instrumentation is not needed.


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

1 Files Affected:

  • (modified) llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp (+7-8)
diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
index 80da830567d2e..0284e78577413 100644
--- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -1312,6 +1312,13 @@ PreservedAnalyses AddressSanitizerPass::run(Module &M,
   const StackSafetyGlobalInfo *const SSGI =
       ClUseStackSafety ? &MAM.getResult<StackSafetyGlobalAnalysis>(M) : nullptr;
   for (Function &F : M) {
+    if (F.empty())
+      continue;
+    if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage) continue;
+    if (!ClDebugFunc.empty() && ClDebugFunc == F.getName()) continue;
+    if (F.getName().starts_with("__asan_")) continue;
+    if (F.isPresplitCoroutine())
+      continue;
     AddressSanitizer FunctionSanitizer(
         M, SSGI, Options.InstrumentationWithCallsThreshold,
         Options.MaxInlinePoisoningSize, Options.CompileKernel, Options.Recover,
@@ -2989,14 +2996,6 @@ bool AddressSanitizer::suppressInstrumentationSiteForDebug(int &Instrumented) {
 
 bool AddressSanitizer::instrumentFunction(Function &F,
                                           const TargetLibraryInfo *TLI) {
-  if (F.empty())
-    return false;
-  if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage) return false;
-  if (!ClDebugFunc.empty() && ClDebugFunc == F.getName()) return false;
-  if (F.getName().starts_with("__asan_")) return false;
-  if (F.isPresplitCoroutine())
-    return false;
-
   bool FunctionModified = false;
 
   // Do not apply any instrumentation for naked functions.

@llvmbot
Copy link
Member

llvmbot commented Mar 27, 2025

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

Author: Hank Chang (HankChang736)

Changes

… unnecessary FunctionSanitizer construction (NFC)

This patch moves several early-exit checks (e.g., empty function, etc.) out of "AddressSanitizer::instrumentFunction" and into the caller. This change avoids unnecessary construction of FunctionSanitizer when instrumentation is not needed.


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

1 Files Affected:

  • (modified) llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp (+7-8)
diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
index 80da830567d2e..0284e78577413 100644
--- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -1312,6 +1312,13 @@ PreservedAnalyses AddressSanitizerPass::run(Module &M,
   const StackSafetyGlobalInfo *const SSGI =
       ClUseStackSafety ? &MAM.getResult<StackSafetyGlobalAnalysis>(M) : nullptr;
   for (Function &F : M) {
+    if (F.empty())
+      continue;
+    if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage) continue;
+    if (!ClDebugFunc.empty() && ClDebugFunc == F.getName()) continue;
+    if (F.getName().starts_with("__asan_")) continue;
+    if (F.isPresplitCoroutine())
+      continue;
     AddressSanitizer FunctionSanitizer(
         M, SSGI, Options.InstrumentationWithCallsThreshold,
         Options.MaxInlinePoisoningSize, Options.CompileKernel, Options.Recover,
@@ -2989,14 +2996,6 @@ bool AddressSanitizer::suppressInstrumentationSiteForDebug(int &Instrumented) {
 
 bool AddressSanitizer::instrumentFunction(Function &F,
                                           const TargetLibraryInfo *TLI) {
-  if (F.empty())
-    return false;
-  if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage) return false;
-  if (!ClDebugFunc.empty() && ClDebugFunc == F.getName()) return false;
-  if (F.getName().starts_with("__asan_")) return false;
-  if (F.isPresplitCoroutine())
-    return false;
-
   bool FunctionModified = false;
 
   // Do not apply any instrumentation for naked functions.

Copy link

github-actions bot commented Mar 27, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@HankChang736 HankChang736 merged commit d443cd6 into llvm:main Mar 28, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants