[clang] 2a9372f - halt_on_error flag for TySan and docs (#182479)

via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 9 08:42:33 PDT 2026


Author: Matthew Nagy
Date: 2026-03-09T15:42:25Z
New Revision: 2a9372ff021546a9b3310c2fb435a0cb4c6c744d

URL: https://github.com/llvm/llvm-project/commit/2a9372ff021546a9b3310c2fb435a0cb4c6c744d
DIFF: https://github.com/llvm/llvm-project/commit/2a9372ff021546a9b3310c2fb435a0cb4c6c744d.diff

LOG: halt_on_error flag for TySan and docs (#182479)

Added: 
    compiler-rt/test/tysan/halt_on_error.c

Modified: 
    clang/docs/TypeSanitizer.rst
    compiler-rt/lib/tysan/tysan.cpp
    compiler-rt/lib/tysan/tysan_flags.inc

Removed: 
    


################################################################################
diff  --git a/clang/docs/TypeSanitizer.rst b/clang/docs/TypeSanitizer.rst
index 5a98a2547aa87..08cb247aefe3a 100644
--- a/clang/docs/TypeSanitizer.rst
+++ b/clang/docs/TypeSanitizer.rst
@@ -180,6 +180,18 @@ violation reports in the specified source files or functions. Like
 with other methods of ignoring instrumentation, this can result in false 
 positives/ false-negatives.
 
+Runtime Options
+---------------
+
+Similar to other sanitizers, you can modify TypeSanitizers runtime behaviour by 
+using an environment variable. These flags should be provided as a colon separated 
+list. For example, ``TYSAN_OPTIONS=print_stacktrace=1:halt_on_error=1``
+
+* ``print_stacktrace`` when true will tell the sanitizer to emit more lengthy 
+  and detailed stack traces on error.
+* ``halt_on_error`` when true will make the instrumented program abort after 
+  the first type violation detected.
+
 Limitations
 -----------
 

diff  --git a/compiler-rt/lib/tysan/tysan.cpp b/compiler-rt/lib/tysan/tysan.cpp
index 1c67adeba0fc5..52f941180b8eb 100644
--- a/compiler-rt/lib/tysan/tysan.cpp
+++ b/compiler-rt/lib/tysan/tysan.cpp
@@ -253,6 +253,11 @@ static void reportError(void *Addr, int Size, tysan_type_descriptor *TD,
   } else {
     Printf("\n");
   }
+
+  if (flags().halt_on_error) {
+    Report("ABORTING\n");
+    Die();
+  }
 }
 
 ALWAYS_INLINE

diff  --git a/compiler-rt/lib/tysan/tysan_flags.inc b/compiler-rt/lib/tysan/tysan_flags.inc
index be65c8e828794..f8bd934fc3d82 100644
--- a/compiler-rt/lib/tysan/tysan_flags.inc
+++ b/compiler-rt/lib/tysan/tysan_flags.inc
@@ -18,3 +18,5 @@
 
 TYSAN_FLAG(bool, print_stacktrace, false,
            "Include full stacktrace into an error report")
+TYSAN_FLAG(bool, halt_on_error, false,
+           "Crash the program after printing the first error report.")

diff  --git a/compiler-rt/test/tysan/halt_on_error.c b/compiler-rt/test/tysan/halt_on_error.c
new file mode 100644
index 0000000000000..6d64eb2bb8959
--- /dev/null
+++ b/compiler-rt/test/tysan/halt_on_error.c
@@ -0,0 +1,23 @@
+// RUN: %clang_tysan %s -o %t
+// RUN: %run %t 2>&1 | FileCheck --check-prefixes=CHECK,CHECK-CONTINUE %s
+// RUN: %env_tysan_opts=halt_on_error=1 not %run %t 2>&1 | FileCheck --check-prefixes=CHECK,CHECK-HALT %s
+
+int main() {
+
+  int i = 5;
+
+  float *f = (float *)&i;
+
+  // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
+  // CHECK: WRITE of size 4
+  // CHECK-HALT: ABORTING
+  *f = 5.0f;
+
+  // CHECK-CONTINUE: ERROR: TypeSanitizer: type-aliasing-violation
+  // CHECK-CONTINUE: READ of size 4
+  // CHECK-HALT-NOT: ERROR: TypeSanitizer: type-aliasing-violation
+  // CHECK-HALT-NOT: READ of size 4
+  i = *f;
+
+  return 0;
+}


        


More information about the cfe-commits mailing list