[compiler-rt] [rtsan] Introduce halt_on_error flag (PR #109832)

Chris Apple via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 24 13:40:16 PDT 2024


https://github.com/cjappl updated https://github.com/llvm/llvm-project/pull/109832

>From fd099077450903177aead4911235fcae49b14b11 Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Fri, 16 Aug 2024 10:44:45 -0700
Subject: [PATCH 1/2] [rtsan] Introduce halt_on_error flag

---
 compiler-rt/lib/rtsan/rtsan.cpp          | 19 +++++++++---------
 compiler-rt/lib/rtsan/rtsan_flags.inc    |  3 +--
 compiler-rt/test/rtsan/halt_on_error.cpp | 25 ++++++++++++++++++++++++
 3 files changed, 35 insertions(+), 12 deletions(-)
 create mode 100644 compiler-rt/test/rtsan/halt_on_error.cpp

diff --git a/compiler-rt/lib/rtsan/rtsan.cpp b/compiler-rt/lib/rtsan/rtsan.cpp
index 84e4b8fae1e2fa..7f7be3d5a06407 100644
--- a/compiler-rt/lib/rtsan/rtsan.cpp
+++ b/compiler-rt/lib/rtsan/rtsan.cpp
@@ -44,10 +44,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();
   };
 }
 
@@ -105,20 +106,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`.*}}
+}

>From 087c47f775ec3a29609c0b12f6fc3811fab9360c Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Tue, 24 Sep 2024 13:40:01 -0700
Subject: [PATCH 2/2] [PR] fmayer - Test for true, fix name of function

---
 compiler-rt/lib/rtsan/rtsan.cpp          | 10 +++++-----
 compiler-rt/test/rtsan/halt_on_error.cpp |  7 ++++---
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/compiler-rt/lib/rtsan/rtsan.cpp b/compiler-rt/lib/rtsan/rtsan.cpp
index 7f7be3d5a06407..1e10069f51dd3b 100644
--- a/compiler-rt/lib/rtsan/rtsan.cpp
+++ b/compiler-rt/lib/rtsan/rtsan.cpp
@@ -44,7 +44,7 @@ static InitializationState GetInitializationState() {
       atomic_load(&rtsan_initialized, memory_order_acquire));
 }
 
-static auto DefaultErrorAction(DiagnosticsInfo info) {
+static auto OnViolationAction(DiagnosticsInfo info) {
   return [info]() {
     __rtsan::PrintDiagnostics(info);
     if (flags().halt_on_error)
@@ -107,8 +107,8 @@ __rtsan_notify_intercepted_call(const char *func_name) {
   __rtsan_ensure_initialized();
   GET_CALLER_PC_BP;
   ExpectNotRealtime(GetContextForThisThread(),
-                    DefaultErrorAction({DiagnosticsInfoType::InterceptedCall,
-                                        func_name, pc, bp}));
+                    OnViolationAction({DiagnosticsInfoType::InterceptedCall,
+                                       func_name, pc, bp}));
 }
 
 SANITIZER_INTERFACE_ATTRIBUTE void
@@ -116,8 +116,8 @@ __rtsan_notify_blocking_call(const char *func_name) {
   __rtsan_ensure_initialized();
   GET_CALLER_PC_BP;
   ExpectNotRealtime(GetContextForThisThread(),
-                    DefaultErrorAction({DiagnosticsInfoType::BlockingCall,
-                                        func_name, pc, bp}));
+                    OnViolationAction({DiagnosticsInfoType::BlockingCall,
+                                       func_name, pc, bp}));
 }
 
 } // extern "C"
diff --git a/compiler-rt/test/rtsan/halt_on_error.cpp b/compiler-rt/test/rtsan/halt_on_error.cpp
index 1d1d90e93f3f44..c2ebdf349f371a 100644
--- a/compiler-rt/test/rtsan/halt_on_error.cpp
+++ b/compiler-rt/test/rtsan/halt_on_error.cpp
@@ -1,5 +1,6 @@
 // RUN: %clangxx -fsanitize=realtime %s -o %t
-// RUN: env RTSAN_OPTIONS="halt_on_error=false" %run %t 2>&1 | FileCheck %s
+// RUN: %env_rtsan_opts="halt_on_error=true" not %run %t 2>&1 | FileCheck %s
+// RUN: %env_rtsan_opts="halt_on_error=false" %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK-NO-HALT,CHECK
 // UNSUPPORTED: ios
 
 // Intent: Ensure that halt_on_error does not exit on the first violation.
@@ -20,6 +21,6 @@ int main() {
   return 0;
   // CHECK: ==ERROR: RealtimeSanitizer
   // CHECK-NEXT: {{.*`malloc`.*}}
-  // CHECK: ==ERROR: RealtimeSanitizer
-  // CHECK-NEXT: {{.*`free`.*}}
+  // CHECK-NO-HALT: ==ERROR: RealtimeSanitizer
+  // CHECK-NO-HALT-NEXT: {{.*`free`.*}}
 }



More information about the llvm-commits mailing list