Skip to content

Conversation

@Ritanya-B-Bharadwaj
Copy link
Contributor

Sanitizers achieve better accuracy with lower optimization levels, and it is generally recommended to use -O0 (the default optimization level) when using sanitizers for the most accurate results. However, many users might not be aware of this recommendation.

To ensure transparency, we should issue a warning when optimization levels other than -O0 are enabled alongside sanitizers. This warning will inform users that higher optimization levels can reduce the effectiveness of sanitizers, thereby making them aware of the potential impact on the accuracy of sanitizer.

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 18, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 18, 2024

@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Author: None (Ritanya-B-Bharadwaj)

Changes

Sanitizers achieve better accuracy with lower optimization levels, and it is generally recommended to use -O0 (the default optimization level) when using sanitizers for the most accurate results. However, many users might not be aware of this recommendation.

To ensure transparency, we should issue a warning when optimization levels other than -O0 are enabled alongside sanitizers. This warning will inform users that higher optimization levels can reduce the effectiveness of sanitizers, thereby making them aware of the potential impact on the accuracy of sanitizer.


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

3 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticDriverKinds.td (+2)
  • (modified) clang/lib/Driver/ToolChains/Clang.cpp (+6)
  • (modified) clang/test/Driver/fsanitize.c (+7)
diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 1ca2cb85565a1..bd254bcaf5d97 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -477,6 +477,8 @@ def warn_drv_disabling_vptr_no_rtti_default : Warning<
 def warn_drv_object_size_disabled_O0 : Warning<
   "the object size sanitizer has no effect at -O0, but is explicitly enabled: %0">,
   InGroup<InvalidCommandLineArgument>, DefaultWarnNoWerror;
+def warn_sanitizer_with_optimization : Warning<
+  "enabling optimizations with sanitizers may potentially reduce effectiveness">;
 def warn_ignoring_verify_debuginfo_preserve_export : Warning<
   "ignoring -fverify-debuginfo-preserve-export=%0 because "
   "-fverify-debuginfo-preserve wasn't enabled">,
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 331cf6e713d89..2d72b3eb62308 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -6208,6 +6208,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
     PScpu::addProfileRTArgs(TC, Args, CmdArgs);
     PScpu::addSanitizerArgs(TC, Args, CmdArgs);
   }
+  
+  // Emit a warning if optimizations are enabled with sanitizers
+  if (Args.hasArg(options::OPT_fsanitize_EQ) &&
+      (Args.hasArg(options::OPT_Ofast) || Args.hasArg(options::OPT_O))) {
+    D.Diag(diag::warn_sanitizer_with_optimization);
+  }
 
   // Pass options for controlling the default header search paths.
   if (Args.hasArg(options::OPT_nostdinc)) {
diff --git a/clang/test/Driver/fsanitize.c b/clang/test/Driver/fsanitize.c
index 08e9c78f9d1d2..5657bb9a6da26 100644
--- a/clang/test/Driver/fsanitize.c
+++ b/clang/test/Driver/fsanitize.c
@@ -1038,3 +1038,10 @@
 // RUN: not %clang --target=aarch64-none-elf -fsanitize=dataflow %s -### 2>&1 | FileCheck %s -check-prefix=UNSUPPORTED-BAREMETAL
 // RUN: not %clang --target=arm-arm-none-eabi -fsanitize=shadow-call-stack %s -### 2>&1 | FileCheck %s -check-prefix=UNSUPPORTED-BAREMETAL
 // UNSUPPORTED-BAREMETAL: unsupported option '-fsanitize={{.*}}' for target
+
+// RUN: %clang -O0 -O1 -fsanitize=address %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-SAN-OPT-WARN
+// RUN: %clang -Ofast -fsanitize=address %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-SAN-OPT-WARN
+// RUN: %clang -O3 -fsanitize=address %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-SAN-OPT-WARN
+// RUN: %clang -O2 -fsanitize=thread %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-SAN-OPT-WARN
+// RUN: %clang -O1 -fsanitize=thread %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-SAN-OPT-WARN
+// CHECK-SAN-OPT-WARN: warning: enabling optimizations with sanitizers may potentially reduce effectiveness

"the object size sanitizer has no effect at -O0, but is explicitly enabled: %0">,
InGroup<InvalidCommandLineArgument>, DefaultWarnNoWerror;
def warn_sanitizer_with_optimization : Warning<
"enabling optimizations with sanitizers may potentially reduce effectiveness">;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"enabling optimizations with sanitizers may potentially reduce effectiveness">;
"enabling optimizations may reduce the effectiveness of sanitizers">;

// RUN: %clang -O3 -fsanitize=address %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-SAN-OPT-WARN
// RUN: %clang -O2 -fsanitize=thread %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-SAN-OPT-WARN
// RUN: %clang -O1 -fsanitize=thread %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-SAN-OPT-WARN
// CHECK-SAN-OPT-WARN: warning: enabling optimizations with sanitizers may potentially reduce effectiveness
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// CHECK-SAN-OPT-WARN: warning: enabling optimizations with sanitizers may potentially reduce effectiveness
// CHECK-SAN-OPT-WARN: warning: enabling optimizations may reduce the effectiveness of sanitizers

@Ritanya-B-Bharadwaj
Copy link
Contributor Author

ping

@AaronBallman AaronBallman requested a review from MaskRay June 27, 2024 15:44
Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

I'm not certain we should issue a diagnostic when optimizations are enabled with sanitizers. For example, there is explicit documentation that users should use -O1 with address sanitizer and similarly, users should use -O2 with the thread sanitizer.

So perhaps this is a matter that's better handled with documentation so we can explain the tradeoffs to users?

@MaskRay
Copy link
Member

MaskRay commented Jun 29, 2024

I agree that we should not add a driver diagnostic.
Using sanitizers with -O2/-O3 is pretty common, especially when user programs are so large that -O0 is too slow or does not build at all.
I think msan is the most affected sanitizer which will detect fewer bugs.

Copy link
Member

@MaskRay MaskRay left a comment

Choose a reason for hiding this comment

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

.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants