[compiler-rt] [TySan] Added a 'print_stacktrace' flag for more detailed errors (PR #121756)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 7 03:17:35 PST 2025


https://github.com/gbMattN updated https://github.com/llvm/llvm-project/pull/121756

>From 6580b3f182ef1d943e89e006e4f47ee77529867b Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Mon, 6 Jan 2025 11:45:48 +0000
Subject: [PATCH 1/4] [TySan] Added a 'print_stacktrace' flag for more detailed
 errors

---
 compiler-rt/lib/tysan/tysan.cpp       | 3 ++-
 compiler-rt/lib/tysan/tysan_flags.inc | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/compiler-rt/lib/tysan/tysan.cpp b/compiler-rt/lib/tysan/tysan.cpp
index 39d78e7c95e0cd..504d8a383b99c5 100644
--- a/compiler-rt/lib/tysan/tysan.cpp
+++ b/compiler-rt/lib/tysan/tysan.cpp
@@ -198,7 +198,8 @@ static void reportError(void *Addr, int Size, tysan_type_descriptor *TD,
 
   if (pc) {
 
-    bool request_fast = StackTrace::WillUseFastUnwind(true);
+    bool request_fast =
+        StackTrace::WillUseFastUnwind(true) && !flags().print_stacktrace;
     BufferedStackTrace ST;
     ST.Unwind(kStackTraceMax, pc, bp, 0, 0, 0, request_fast);
     ST.Print();
diff --git a/compiler-rt/lib/tysan/tysan_flags.inc b/compiler-rt/lib/tysan/tysan_flags.inc
index 98b6591f844ef0..be65c8e828794a 100644
--- a/compiler-rt/lib/tysan/tysan_flags.inc
+++ b/compiler-rt/lib/tysan/tysan_flags.inc
@@ -15,3 +15,6 @@
 
 // TYSAN_FLAG(Type, Name, DefaultValue, Description)
 // See COMMON_FLAG in sanitizer_flags.inc for more details.
+
+TYSAN_FLAG(bool, print_stacktrace, false,
+           "Include full stacktrace into an error report")

>From 5a4876954e55934ba602089ebfbf18d39e6dc0d0 Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Mon, 6 Jan 2025 13:44:33 +0000
Subject: [PATCH 2/4] Use GetThreadStackTopAndBottom rather than playing with
 fast unwind

---
 compiler-rt/lib/tysan/tysan.cpp       | 9 ++++++---
 compiler-rt/lib/tysan/tysan_flags.inc | 3 ---
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/compiler-rt/lib/tysan/tysan.cpp b/compiler-rt/lib/tysan/tysan.cpp
index 504d8a383b99c5..263b690ca230d6 100644
--- a/compiler-rt/lib/tysan/tysan.cpp
+++ b/compiler-rt/lib/tysan/tysan.cpp
@@ -198,10 +198,13 @@ static void reportError(void *Addr, int Size, tysan_type_descriptor *TD,
 
   if (pc) {
 
-    bool request_fast =
-        StackTrace::WillUseFastUnwind(true) && !flags().print_stacktrace;
+    uptr top = 0;
+    uptr bottom = 0;
+    GetThreadStackTopAndBottom(false, &top, &bottom);
+
+    bool request_fast = StackTrace::WillUseFastUnwind(true);
     BufferedStackTrace ST;
-    ST.Unwind(kStackTraceMax, pc, bp, 0, 0, 0, request_fast);
+    ST.Unwind(kStackTraceMax, pc, bp, 0, top, bottom, request_fast);
     ST.Print();
   } else {
     Printf("\n");
diff --git a/compiler-rt/lib/tysan/tysan_flags.inc b/compiler-rt/lib/tysan/tysan_flags.inc
index be65c8e828794a..98b6591f844ef0 100644
--- a/compiler-rt/lib/tysan/tysan_flags.inc
+++ b/compiler-rt/lib/tysan/tysan_flags.inc
@@ -15,6 +15,3 @@
 
 // TYSAN_FLAG(Type, Name, DefaultValue, Description)
 // See COMMON_FLAG in sanitizer_flags.inc for more details.
-
-TYSAN_FLAG(bool, print_stacktrace, false,
-           "Include full stacktrace into an error report")

>From c1a1f9ad48edff3dccd79b24bf21aadeb66f0092 Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Mon, 6 Jan 2025 14:49:43 +0000
Subject: [PATCH 3/4] fixup

---
 compiler-rt/lib/tysan/tysan.cpp       | 3 ++-
 compiler-rt/lib/tysan/tysan_flags.inc | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/compiler-rt/lib/tysan/tysan.cpp b/compiler-rt/lib/tysan/tysan.cpp
index 263b690ca230d6..529ac22f658ae9 100644
--- a/compiler-rt/lib/tysan/tysan.cpp
+++ b/compiler-rt/lib/tysan/tysan.cpp
@@ -200,7 +200,8 @@ static void reportError(void *Addr, int Size, tysan_type_descriptor *TD,
 
     uptr top = 0;
     uptr bottom = 0;
-    GetThreadStackTopAndBottom(false, &top, &bottom);
+    if(flags().print_stacktrace)
+      GetThreadStackTopAndBottom(false, &top, &bottom);
 
     bool request_fast = StackTrace::WillUseFastUnwind(true);
     BufferedStackTrace ST;
diff --git a/compiler-rt/lib/tysan/tysan_flags.inc b/compiler-rt/lib/tysan/tysan_flags.inc
index 98b6591f844ef0..be65c8e828794a 100644
--- a/compiler-rt/lib/tysan/tysan_flags.inc
+++ b/compiler-rt/lib/tysan/tysan_flags.inc
@@ -15,3 +15,6 @@
 
 // TYSAN_FLAG(Type, Name, DefaultValue, Description)
 // See COMMON_FLAG in sanitizer_flags.inc for more details.
+
+TYSAN_FLAG(bool, print_stacktrace, false,
+           "Include full stacktrace into an error report")

>From b3d03daf9c99b41795ca46152a47140319304f36 Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Tue, 7 Jan 2025 11:17:24 +0000
Subject: [PATCH 4/4] Added a test and fixed formating

---
 compiler-rt/lib/tysan/tysan.cpp           |  2 +-
 compiler-rt/test/tysan/print_stacktrace.c | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
 create mode 100644 compiler-rt/test/tysan/print_stacktrace.c

diff --git a/compiler-rt/lib/tysan/tysan.cpp b/compiler-rt/lib/tysan/tysan.cpp
index 529ac22f658ae9..df9856ceedad47 100644
--- a/compiler-rt/lib/tysan/tysan.cpp
+++ b/compiler-rt/lib/tysan/tysan.cpp
@@ -200,7 +200,7 @@ static void reportError(void *Addr, int Size, tysan_type_descriptor *TD,
 
     uptr top = 0;
     uptr bottom = 0;
-    if(flags().print_stacktrace)
+    if (flags().print_stacktrace)
       GetThreadStackTopAndBottom(false, &top, &bottom);
 
     bool request_fast = StackTrace::WillUseFastUnwind(true);
diff --git a/compiler-rt/test/tysan/print_stacktrace.c b/compiler-rt/test/tysan/print_stacktrace.c
new file mode 100644
index 00000000000000..d32a372fa6c1bd
--- /dev/null
+++ b/compiler-rt/test/tysan/print_stacktrace.c
@@ -0,0 +1,22 @@
+// RUN: %clang_tysan -O0 %s -o %t && %run %t >%t.out 2>&1
+// RUN: FileCheck --check-prefixes=CHECK,CHECK-SHORT %s < %t.out
+
+// RUN: env TYSAN_OPTIONS=print_stacktrace=1 %run %t >%t.out 2>&1
+// RUN: FileCheck --check-prefixes=CHECK,CHECK-LONG %s < %t.out
+
+float *P;
+void zero_array() {
+  int i;
+  for (i = 0; i < 1; ++i)
+    P[i] = 0.0f;
+  // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
+  // CHECK: WRITE of size 4 at {{.*}} with type float accesses an existing object of type p1 float
+  // CHECK: {{#0 0x.* in zero_array .*print_stacktrace.c:}}[[@LINE-3]]
+  // CHECK-SHORT-NOT: {{#1 0x.* in main .*print_stacktrace.c}}
+  // CHECK-LONG-NEXT: {{#1 0x.* in main .*print_stacktrace.c}}
+}
+
+int main() {
+  P = (float *)&P;
+  zero_array();
+}



More information about the llvm-commits mailing list