Skip to content

[rtsan] Introduce halt_on_error flag #109832

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 2 commits into from
Sep 25, 2024
Merged

[rtsan] Introduce halt_on_error flag #109832

merged 2 commits into from
Sep 25, 2024

Conversation

cjappl
Copy link
Contributor

@cjappl cjappl commented Sep 24, 2024

This is what we were previously calling "continue mode"

Allow a runtime option to either

  1. Die (halt_on_error=false) -- default behavior
  2. Continue

Follow up reviews will allow for de-duplication so users do not drown in info.

@cjappl
Copy link
Contributor Author

cjappl commented Sep 24, 2024

CC Review @davidtrevelyan

@llvmbot
Copy link
Member

llvmbot commented Sep 24, 2024

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

Author: Chris Apple (cjappl)

Changes

This is what we were previously calling "continue mode"

Allow a runtime option to either

  1. Die (halt_on_error=false) -- default behavior
  2. Continue

Follow up reviews will allow for de-duplication so users do not drown in info.


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

3 Files Affected:

  • (modified) compiler-rt/lib/rtsan/rtsan.cpp (+9-10)
  • (modified) compiler-rt/lib/rtsan/rtsan_flags.inc (+1-2)
  • (added) compiler-rt/test/rtsan/halt_on_error.cpp (+25)
diff --git a/compiler-rt/lib/rtsan/rtsan.cpp b/compiler-rt/lib/rtsan/rtsan.cpp
index e6d2481b2c2a3d..5a2318633a9bc7 100644
--- a/compiler-rt/lib/rtsan/rtsan.cpp
+++ b/compiler-rt/lib/rtsan/rtsan.cpp
@@ -43,10 +43,11 @@ static InitializationState GetInitializationState() {
       atomic_load(&rtsan_initialized, memory_order_acquire));
 }
 
-static auto PrintDiagnosticsAndDieAction(DiagnosticsInfo info) {
+static auto DefaultErrorAction(DiagnosticsInfo info) {
   return [info]() {
     __rtsan::PrintDiagnostics(info);
-    Die();
+    if (flags().halt_on_error)
+      Die();
   };
 }
 
@@ -104,20 +105,18 @@ __rtsan_notify_intercepted_call(const char *func_name) {
 
   __rtsan_ensure_initialized();
   GET_CALLER_PC_BP;
-  ExpectNotRealtime(
-      GetContextForThisThread(),
-      PrintDiagnosticsAndDieAction(
-          {DiagnosticsInfoType::InterceptedCall, func_name, pc, bp}));
+  ExpectNotRealtime(GetContextForThisThread(),
+                    DefaultErrorAction({DiagnosticsInfoType::InterceptedCall,
+                                        func_name, pc, bp}));
 }
 
 SANITIZER_INTERFACE_ATTRIBUTE void
 __rtsan_notify_blocking_call(const char *func_name) {
   __rtsan_ensure_initialized();
   GET_CALLER_PC_BP;
-  ExpectNotRealtime(
-      GetContextForThisThread(),
-      PrintDiagnosticsAndDieAction(
-          {DiagnosticsInfoType::BlockingCall, func_name, pc, bp}));
+  ExpectNotRealtime(GetContextForThisThread(),
+                    DefaultErrorAction({DiagnosticsInfoType::BlockingCall,
+                                        func_name, pc, bp}));
 }
 
 } // extern "C"
diff --git a/compiler-rt/lib/rtsan/rtsan_flags.inc b/compiler-rt/lib/rtsan/rtsan_flags.inc
index 93b0294313672f..25d62cf0a60fb0 100644
--- a/compiler-rt/lib/rtsan/rtsan_flags.inc
+++ b/compiler-rt/lib/rtsan/rtsan_flags.inc
@@ -16,5 +16,4 @@
 // RTSAN_FLAG(Type, Name, DefaultValue, Description)
 // See COMMON_FLAG in sanitizer_flags.inc for more details.
 
-// Example flag, until we get a real one
-// RTSAN_FLAG(bool, halt_on_error, true, "If true, halt the program on error")
+RTSAN_FLAG(bool, halt_on_error, true, "Exit after first reported error.")
diff --git a/compiler-rt/test/rtsan/halt_on_error.cpp b/compiler-rt/test/rtsan/halt_on_error.cpp
new file mode 100644
index 00000000000000..1d1d90e93f3f44
--- /dev/null
+++ b/compiler-rt/test/rtsan/halt_on_error.cpp
@@ -0,0 +1,25 @@
+// RUN: %clangxx -fsanitize=realtime %s -o %t
+// RUN: env RTSAN_OPTIONS="halt_on_error=false" %run %t 2>&1 | FileCheck %s
+// UNSUPPORTED: ios
+
+// Intent: Ensure that halt_on_error does not exit on the first violation.
+
+#include <stdlib.h>
+
+void *MallocViolation() { return malloc(10); }
+
+void FreeViolation(void *Ptr) { free(Ptr); }
+
+void process() [[clang::nonblocking]] {
+  void *Ptr = MallocViolation();
+  FreeViolation(Ptr);
+}
+
+int main() {
+  process();
+  return 0;
+  // CHECK: ==ERROR: RealtimeSanitizer
+  // CHECK-NEXT: {{.*`malloc`.*}}
+  // CHECK: ==ERROR: RealtimeSanitizer
+  // CHECK-NEXT: {{.*`free`.*}}
+}

@@ -16,5 +16,4 @@
// RTSAN_FLAG(Type, Name, DefaultValue, Description)
// See COMMON_FLAG in sanitizer_flags.inc for more details.

// Example flag, until we get a real one
// RTSAN_FLAG(bool, halt_on_error, true, "If true, halt the program on error")
RTSAN_FLAG(bool, halt_on_error, true, "Exit after first reported error.")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

My preference for this default is to remain true, but I'm open for discussion on the matter.

You can grep for this flag name and see the defaults for the other sanitizers, each takes their own approach

@cjappl cjappl requested a review from fmayer September 24, 2024 20:18
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.

thanks

@cjappl
Copy link
Contributor Author

cjappl commented Sep 24, 2024

Thanks for the reviews @fmayer , we really appreciate having more eyes on this thing

@cjappl cjappl merged commit 9ef9acb into llvm:main Sep 25, 2024
7 checks passed
@cjappl cjappl deleted the continue_basic branch September 25, 2024 00:08
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.

4 participants