Skip to content

[rtsan] Add support for ReportErrorSummary #116424

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
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions compiler-rt/lib/rtsan/rtsan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ static void OnViolation(const BufferedStackTrace &stack,
if (UNLIKELY(is_stack_novel)) {
IncrementUniqueErrorCount();

PrintDiagnostics(info);
stack.Print();
{
ScopedErrorReportLock l;
Copy link
Contributor Author

@cjappl cjappl Nov 15, 2024

Choose a reason for hiding this comment

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

This lock is now bubbled up here to make sure we don't ever separate any of this reporting across threads, we then just assert the lock has been taken in debug mode later in these methods

PrintDiagnostics(info);
stack.Print();
PrintErrorSummary(info, stack);
}

handle.inc_use_count_unsafe();
}
Expand Down
30 changes: 19 additions & 11 deletions compiler-rt/lib/rtsan/rtsan_diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,22 @@ class Decorator : public SanitizerCommonDecorator {
};
} // namespace

static const char *GetErrorTypeStr(const DiagnosticsInfo &info) {
switch (info.type) {
case DiagnosticsInfoType::InterceptedCall:
return "unsafe-library-call";
case DiagnosticsInfoType::BlockingCall:
return "blocking-call";
}
CHECK(false);
return "(unknown error)";
}

static void PrintError(const Decorator &decorator,
const DiagnosticsInfo &info) {
const auto ErrorTypeStr = [&info]() -> const char * {
switch (info.type) {
case DiagnosticsInfoType::InterceptedCall:
return "unsafe-library-call";
case DiagnosticsInfoType::BlockingCall:
return "blocking-call";
}
return "(unknown error)";
};

Printf("%s", decorator.Error());
Report("ERROR: RealtimeSanitizer: %s\n", ErrorTypeStr());
Report("ERROR: RealtimeSanitizer: %s\n", GetErrorTypeStr(info));
}

static void PrintReason(const Decorator &decorator,
Expand All @@ -78,10 +80,16 @@ static void PrintReason(const Decorator &decorator,
}

void __rtsan::PrintDiagnostics(const DiagnosticsInfo &info) {
ScopedErrorReportLock l;
ScopedErrorReportLock::CheckLocked();

Decorator d;
PrintError(d, info);
PrintReason(d, info);
Printf("%s", d.Default());
}

void __rtsan::PrintErrorSummary(const DiagnosticsInfo &info,
const BufferedStackTrace &stack) {
ScopedErrorReportLock::CheckLocked();
ReportErrorSummary(GetErrorTypeStr(info), &stack);
}
2 changes: 2 additions & 0 deletions compiler-rt/lib/rtsan/rtsan_diagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ struct DiagnosticsInfo {
};

void PrintDiagnostics(const DiagnosticsInfo &info);
void PrintErrorSummary(const DiagnosticsInfo &info,
const __sanitizer::BufferedStackTrace &stack);
} // namespace __rtsan
32 changes: 32 additions & 0 deletions compiler-rt/test/rtsan/report_error_summary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// RUN: %clangxx -fsanitize=realtime %s -o %t
// RUN: %env_rtsan_opts="halt_on_error=false" %run %t 2>&1 | FileCheck %s

// RUN: %clangxx -DTEST_CUSTOM_HANDLER=1 -fsanitize=realtime %s -o %t
// RUN: not %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK-CUSTOM-HANDLER

// UNSUPPORTED: ios

// Intent: Make sure we support ReporErrorSummary, including custom handlers

#include <stdio.h>
#include <stdlib.h>

#ifdef TEST_CUSTOM_HANDLER
extern "C" void __sanitizer_report_error_summary(const char *error_summary) {
fprintf(stderr, "%s %s\n", "In custom handler! ", error_summary);
}
#endif

int blocking_call() [[clang::blocking]] { return 0; }

int main() [[clang::nonblocking]] {
void *ptr = malloc(2);
blocking_call();

printf("ptr: %p\n", ptr); // ensure we don't optimize out the malloc
}

// CHECK: SUMMARY: RealtimeSanitizer: unsafe-library-call
// CHECK: SUMMARY: RealtimeSanitizer: blocking-call

// CHECK-CUSTOM-HANDLER: In custom handler! SUMMARY: RealtimeSanitizer: unsafe-library-call
Loading