Skip to content

Commit 216e1b9

Browse files
[rtsan] Remove std::variant from rtsan diagnostics (#109786)
Following issue #109529 and PR #109715, this PR removes the `std::variant` in rtsan's diagnostics code, in favour of a solution by `enum` without the C++ runtime.
1 parent 622ae7f commit 216e1b9

File tree

3 files changed

+37
-46
lines changed

3 files changed

+37
-46
lines changed

compiler-rt/lib/rtsan/rtsan.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ __rtsan_notify_intercepted_call(const char *func_name) {
8787
GET_CALLER_PC_BP;
8888
ExpectNotRealtime(
8989
GetContextForThisThread(),
90-
PrintDiagnosticsAndDieAction({InterceptedCallInfo{func_name}, pc, bp}));
90+
PrintDiagnosticsAndDieAction(
91+
{DiagnosticsInfoType::InterceptedCall, func_name, pc, bp}));
9192
}
9293

9394
SANITIZER_INTERFACE_ATTRIBUTE void
@@ -96,7 +97,8 @@ __rtsan_notify_blocking_call(const char *func_name) {
9697
GET_CALLER_PC_BP;
9798
ExpectNotRealtime(
9899
GetContextForThisThread(),
99-
PrintDiagnosticsAndDieAction({BlockingCallInfo{func_name}, pc, bp}));
100+
PrintDiagnosticsAndDieAction(
101+
{DiagnosticsInfoType::BlockingCall, func_name, pc, bp}));
100102
}
101103

102104
} // extern "C"

compiler-rt/lib/rtsan/rtsan_diagnostics.cpp

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ class Decorator : public __sanitizer::SanitizerCommonDecorator {
3737
const char *FunctionName() const { return Green(); }
3838
const char *Reason() const { return Blue(); }
3939
};
40-
41-
template <class... Ts> struct Overloaded : Ts... {
42-
using Ts::operator()...;
43-
};
44-
// TODO: Remove below when c++20
45-
template <class... Ts> Overloaded(Ts...) -> Overloaded<Ts...>;
4640
} // namespace
4741

4842
static void PrintStackTrace(uptr pc, uptr bp) {
@@ -53,35 +47,39 @@ static void PrintStackTrace(uptr pc, uptr bp) {
5347
}
5448

5549
static void PrintError(const Decorator &decorator,
56-
const DiagnosticsCallerInfo &info) {
57-
const char *violation_type = std::visit(
58-
Overloaded{
59-
[](const InterceptedCallInfo &) { return "unsafe-library-call"; },
60-
[](const BlockingCallInfo &) { return "blocking-call"; }},
61-
info);
50+
const DiagnosticsInfo &info) {
51+
const auto ErrorTypeStr = [&info]() -> const char * {
52+
switch (info.type) {
53+
case DiagnosticsInfoType::InterceptedCall:
54+
return "unsafe-library-call";
55+
case DiagnosticsInfoType::BlockingCall:
56+
return "blocking-call";
57+
}
58+
return "(unknown error)";
59+
};
6260

6361
Printf("%s", decorator.Error());
64-
Report("ERROR: RealtimeSanitizer: %s\n", violation_type);
62+
Report("ERROR: RealtimeSanitizer: %s\n", ErrorTypeStr());
6563
}
6664

6765
static void PrintReason(const Decorator &decorator,
68-
const DiagnosticsCallerInfo &info) {
66+
const DiagnosticsInfo &info) {
6967
Printf("%s", decorator.Reason());
7068

71-
std::visit(
72-
Overloaded{[decorator](const InterceptedCallInfo &call) {
73-
Printf("Intercepted call to real-time unsafe function "
74-
"`%s%s%s` in real-time context!",
75-
decorator.FunctionName(),
76-
call.intercepted_function_name_, decorator.Reason());
77-
},
78-
[decorator](const BlockingCallInfo &arg) {
79-
Printf("Call to blocking function "
80-
"`%s%s%s` in real-time context!",
81-
decorator.FunctionName(), arg.blocking_function_name_,
82-
decorator.Reason());
83-
}},
84-
info);
69+
switch (info.type) {
70+
case DiagnosticsInfoType::InterceptedCall: {
71+
Printf("Intercepted call to real-time unsafe function "
72+
"`%s%s%s` in real-time context!",
73+
decorator.FunctionName(), info.func_name, decorator.Reason());
74+
break;
75+
}
76+
case DiagnosticsInfoType::BlockingCall: {
77+
Printf("Call to blocking function "
78+
"`%s%s%s` in real-time context!",
79+
decorator.FunctionName(), info.func_name, decorator.Reason());
80+
break;
81+
}
82+
}
8583

8684
Printf("\n");
8785
}
@@ -90,8 +88,8 @@ void __rtsan::PrintDiagnostics(const DiagnosticsInfo &info) {
9088
ScopedErrorReportLock l;
9189

9290
Decorator d;
93-
PrintError(d, info.call_info);
94-
PrintReason(d, info.call_info);
91+
PrintError(d, info);
92+
PrintReason(d, info);
9593
Printf("%s", d.Default());
9694
PrintStackTrace(info.pc, info.bp);
9795
}

compiler-rt/lib/rtsan/rtsan_diagnostics.h

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,16 @@
1515
#include "sanitizer_common/sanitizer_common.h"
1616
#include "sanitizer_common/sanitizer_internal_defs.h"
1717

18-
#include <variant>
19-
2018
namespace __rtsan {
2119

22-
struct InterceptedCallInfo {
23-
const char *intercepted_function_name_;
24-
};
25-
26-
struct BlockingCallInfo {
27-
public:
28-
const char *blocking_function_name_;
20+
enum class DiagnosticsInfoType {
21+
InterceptedCall,
22+
BlockingCall,
2923
};
3024

31-
using DiagnosticsCallerInfo =
32-
std::variant<InterceptedCallInfo, BlockingCallInfo>;
33-
3425
struct DiagnosticsInfo {
35-
DiagnosticsCallerInfo call_info;
36-
26+
DiagnosticsInfoType type;
27+
const char *func_name;
3728
__sanitizer::uptr pc;
3829
__sanitizer::uptr bp;
3930
};

0 commit comments

Comments
 (0)