From 2a9372ff021546a9b3310c2fb435a0cb4c6c744d Mon Sep 17 00:00:00 2001 From: Matthew Nagy Date: Mon, 9 Mar 2026 15:42:25 +0000 Subject: [PATCH] halt_on_error flag for TySan and docs (#182479) --- clang/docs/TypeSanitizer.rst | 12 ++++++++++++ compiler-rt/lib/tysan/tysan.cpp | 5 +++++ compiler-rt/lib/tysan/tysan_flags.inc | 2 ++ compiler-rt/test/tysan/halt_on_error.c | 23 +++++++++++++++++++++++ 4 files changed, 42 insertions(+) create mode 100644 compiler-rt/test/tysan/halt_on_error.c diff --git a/clang/docs/TypeSanitizer.rst b/clang/docs/TypeSanitizer.rst index 5a98a2547aa8..08cb247aefe3 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 1c67adeba0fc..52f941180b8e 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 be65c8e82879..f8bd934fc3d8 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 000000000000..6d64eb2bb895 --- /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; +}