Skip to content

[TySan] Added a 'print_stacktrace' flag for more detailed errors #121756

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

Conversation

gbMattN
Copy link
Contributor

@gbMattN gbMattN commented Jan 6, 2025

Raised in issue #121697

@llvmbot
Copy link
Member

llvmbot commented Jan 6, 2025

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

Author: None (gbMattN)

Changes

Raised in issue #121697


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

2 Files Affected:

  • (modified) compiler-rt/lib/tysan/tysan.cpp (+1-1)
  • (modified) compiler-rt/lib/tysan/tysan_flags.inc (+3)
diff --git a/compiler-rt/lib/tysan/tysan.cpp b/compiler-rt/lib/tysan/tysan.cpp
index 39d78e7c95e0cd..2786d826afb5f4 100644
--- a/compiler-rt/lib/tysan/tysan.cpp
+++ b/compiler-rt/lib/tysan/tysan.cpp
@@ -198,7 +198,7 @@ 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")

Copy link

github-actions bot commented Jan 6, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@gbMattN gbMattN force-pushed the users/nagym/tysan-add-full-stack-trace-option branch from f55dd6c to 6580b3f Compare January 6, 2025 11:50
@gbMattN
Copy link
Contributor Author

gbMattN commented Jan 6, 2025

@firewave @goussepi manually pinging for review

Copy link
Collaborator

@goussepi goussepi left a comment

Choose a reason for hiding this comment

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

Is the issue maybe that stack trace are not being printed unless fast unwind is disabled ?

@@ -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 =
Copy link
Collaborator

Choose a reason for hiding this comment

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

I thought "fastunwind" was unrelated to whether we print a stack trace or not ? I thought it controls the method of obtaining the stack trace.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For some reason, setting it to false also gave a full stack printout
Thanks for pointing this out though, I've found the proper way of doing it!

Copy link
Collaborator

@goussepi goussepi left a comment

Choose a reason for hiding this comment

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

Great find! I guess now stack traces are being printed by default but before only one line was being printed ? The question is which behavior is best by default, should we still have the "print_stacktrace" option in case ?

@firewave
Copy link

firewave commented Jan 6, 2025

Great find! I guess now stack traces are being printed by default but before only one line was being printed ? The question is which behavior is best by default, should we still have the "print_stacktrace" option in case ?

I think it should match the existing behavior in other places i.e. full stacktrace off by default.

@gbMattN
Copy link
Contributor Author

gbMattN commented Jan 6, 2025

That sounds goo to me! We now have the existing behaviour if you don't add TYSAN_OPTIONS=print_stacktrace, and if you do, you get the full stack trace printed out to you

@firewave
Copy link

firewave commented Jan 6, 2025

That sounds goo to me! We now have the existing behaviour if you don't add TYSAN_OPTIONS=print_stacktrace, and if you do, you get the full stack trace printed out to you

For correctness that should be TYSAN_OPTIONS=print_stacktrace=1.

Copy link
Collaborator

@goussepi goussepi left a comment

Choose a reason for hiding this comment

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

Does Tysan support passing options through environment variables, such as TYSAN_OPTIONS? If so, it might be helpful to add a test to check if print_stacktrace works as intended. This could be done using a lit substitution variable like %env_tysan_opts (if not already present?).

@gbMattN gbMattN requested a review from goussepi January 7, 2025 11:17
@gbMattN
Copy link
Contributor Author

gbMattN commented Jan 7, 2025

My previous comment was a mistake, TYSAN_OPTIONS=print_stacktrace=1 is indeed what you need to use. TySan does indeed support using TYSAN_OPTIONS

Copy link
Collaborator

@goussepi goussepi left a comment

Choose a reason for hiding this comment

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

LGTM!

// 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
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think eventually we will need to introduce a %env_tysan_opts lit substitutions to protect from environment variables affecting test results, but can be done in a separate patch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah! We actually have that already it seems. I'll change that

Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@@ -198,9 +198,14 @@ static void reportError(void *Addr, int Size, tysan_type_descriptor *TD,

if (pc) {

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: While you are here, maybe drop the newline at the top here?

@gbMattN gbMattN force-pushed the users/nagym/tysan-add-full-stack-trace-option branch from b3d03da to af53f83 Compare January 7, 2025 13:11
@gbMattN gbMattN force-pushed the users/nagym/tysan-add-full-stack-trace-option branch from cacfa49 to d8c7734 Compare January 7, 2025 14:30
@goussepi goussepi merged commit 20d7fa1 into llvm:main Jan 8, 2025
7 checks passed
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.

5 participants