Skip to content

Commit 7c41b5c

Browse files
authored
[rtsan] Add support for ReportErrorSummary (#116424)
Adding support for the extra SUMMARY line that is output by most compilers. This also adds the ability for end-users to specify their own handlers for reporting these errors (see the test).
1 parent 012dd8b commit 7c41b5c

File tree

4 files changed

+59
-13
lines changed

4 files changed

+59
-13
lines changed

compiler-rt/lib/rtsan/rtsan.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,12 @@ static void OnViolation(const BufferedStackTrace &stack,
6262
if (UNLIKELY(is_stack_novel)) {
6363
IncrementUniqueErrorCount();
6464

65-
PrintDiagnostics(info);
66-
stack.Print();
65+
{
66+
ScopedErrorReportLock l;
67+
PrintDiagnostics(info);
68+
stack.Print();
69+
PrintErrorSummary(info, stack);
70+
}
6771

6872
handle.inc_use_count_unsafe();
6973
}

compiler-rt/lib/rtsan/rtsan_diagnostics.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,22 @@ class Decorator : public SanitizerCommonDecorator {
3939
};
4040
} // namespace
4141

42+
static const char *GetErrorTypeStr(const DiagnosticsInfo &info) {
43+
switch (info.type) {
44+
case DiagnosticsInfoType::InterceptedCall:
45+
return "unsafe-library-call";
46+
case DiagnosticsInfoType::BlockingCall:
47+
return "blocking-call";
48+
}
49+
CHECK(false);
50+
return "(unknown error)";
51+
}
52+
4253
static void PrintError(const Decorator &decorator,
4354
const DiagnosticsInfo &info) {
44-
const auto ErrorTypeStr = [&info]() -> const char * {
45-
switch (info.type) {
46-
case DiagnosticsInfoType::InterceptedCall:
47-
return "unsafe-library-call";
48-
case DiagnosticsInfoType::BlockingCall:
49-
return "blocking-call";
50-
}
51-
return "(unknown error)";
52-
};
5355

5456
Printf("%s", decorator.Error());
55-
Report("ERROR: RealtimeSanitizer: %s\n", ErrorTypeStr());
57+
Report("ERROR: RealtimeSanitizer: %s\n", GetErrorTypeStr(info));
5658
}
5759

5860
static void PrintReason(const Decorator &decorator,
@@ -78,10 +80,16 @@ static void PrintReason(const Decorator &decorator,
7880
}
7981

8082
void __rtsan::PrintDiagnostics(const DiagnosticsInfo &info) {
81-
ScopedErrorReportLock l;
83+
ScopedErrorReportLock::CheckLocked();
8284

8385
Decorator d;
8486
PrintError(d, info);
8587
PrintReason(d, info);
8688
Printf("%s", d.Default());
8789
}
90+
91+
void __rtsan::PrintErrorSummary(const DiagnosticsInfo &info,
92+
const BufferedStackTrace &stack) {
93+
ScopedErrorReportLock::CheckLocked();
94+
ReportErrorSummary(GetErrorTypeStr(info), &stack);
95+
}

compiler-rt/lib/rtsan/rtsan_diagnostics.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ struct DiagnosticsInfo {
3030
};
3131

3232
void PrintDiagnostics(const DiagnosticsInfo &info);
33+
void PrintErrorSummary(const DiagnosticsInfo &info,
34+
const __sanitizer::BufferedStackTrace &stack);
3335
} // namespace __rtsan
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: %clangxx -fsanitize=realtime %s -o %t
2+
// RUN: %env_rtsan_opts="halt_on_error=false" %run %t 2>&1 | FileCheck %s
3+
4+
// RUN: %clangxx -DTEST_CUSTOM_HANDLER=1 -fsanitize=realtime %s -o %t
5+
// RUN: not %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK-CUSTOM-HANDLER
6+
7+
// UNSUPPORTED: ios
8+
9+
// Intent: Make sure we support ReporErrorSummary, including custom handlers
10+
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
14+
#ifdef TEST_CUSTOM_HANDLER
15+
extern "C" void __sanitizer_report_error_summary(const char *error_summary) {
16+
fprintf(stderr, "%s %s\n", "In custom handler! ", error_summary);
17+
}
18+
#endif
19+
20+
int blocking_call() [[clang::blocking]] { return 0; }
21+
22+
int main() [[clang::nonblocking]] {
23+
void *ptr = malloc(2);
24+
blocking_call();
25+
26+
printf("ptr: %p\n", ptr); // ensure we don't optimize out the malloc
27+
}
28+
29+
// CHECK: SUMMARY: RealtimeSanitizer: unsafe-library-call
30+
// CHECK: SUMMARY: RealtimeSanitizer: blocking-call
31+
32+
// CHECK-CUSTOM-HANDLER: In custom handler! SUMMARY: RealtimeSanitizer: unsafe-library-call

0 commit comments

Comments
 (0)