Skip to content

Commit 30d56be

Browse files
authored
[compiler-rt][rtsan] NFC: Refactor context helper functions (#106869)
1 parent 4a505e1 commit 30d56be

File tree

4 files changed

+31
-27
lines changed

4 files changed

+31
-27
lines changed

compiler-rt/lib/rtsan/rtsan.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_enable() {
6969
SANITIZER_INTERFACE_ATTRIBUTE void
7070
__rtsan_expect_not_realtime(const char *intercepted_function_name) {
7171
__rtsan_ensure_initialized();
72-
__rtsan::GetContextForThisThread().ExpectNotRealtime(
73-
intercepted_function_name);
72+
ExpectNotRealtime(GetContextForThisThread(), intercepted_function_name);
7473
}
7574

7675
} // extern "C"

compiler-rt/lib/rtsan/rtsan_context.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,29 +63,29 @@ static void InvokeViolationDetectedAction() { exit(EXIT_FAILURE); }
6363

6464
__rtsan::Context::Context() = default;
6565

66-
void __rtsan::Context::RealtimePush() { realtime_depth++; }
66+
void __rtsan::Context::RealtimePush() { realtime_depth_++; }
6767

68-
void __rtsan::Context::RealtimePop() { realtime_depth--; }
68+
void __rtsan::Context::RealtimePop() { realtime_depth_--; }
6969

70-
void __rtsan::Context::BypassPush() { bypass_depth++; }
70+
void __rtsan::Context::BypassPush() { bypass_depth_++; }
7171

72-
void __rtsan::Context::BypassPop() { bypass_depth--; }
72+
void __rtsan::Context::BypassPop() { bypass_depth_--; }
7373

74-
void __rtsan::Context::ExpectNotRealtime(
75-
const char *intercepted_function_name) {
76-
if (InRealtimeContext() && !IsBypassed()) {
77-
BypassPush();
74+
void __rtsan::ExpectNotRealtime(Context &context,
75+
const char *intercepted_function_name) {
76+
if (context.InRealtimeContext() && !context.IsBypassed()) {
77+
context.BypassPush();
7878
PrintDiagnostics(intercepted_function_name);
7979
InvokeViolationDetectedAction();
80-
BypassPop();
80+
context.BypassPop();
8181
}
8282
}
8383

84-
bool __rtsan::Context::InRealtimeContext() const { return realtime_depth > 0; }
84+
bool __rtsan::Context::InRealtimeContext() const { return realtime_depth_ > 0; }
8585

86-
bool __rtsan::Context::IsBypassed() const { return bypass_depth > 0; }
86+
bool __rtsan::Context::IsBypassed() const { return bypass_depth_ > 0; }
8787

88-
void __rtsan::Context::PrintDiagnostics(const char *intercepted_function_name) {
88+
void __rtsan::PrintDiagnostics(const char *intercepted_function_name) {
8989
fprintf(stderr,
9090
"Real-time violation: intercepted call to real-time unsafe function "
9191
"`%s` in real-time context! Stack trace:\n",

compiler-rt/lib/rtsan/rtsan_context.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,22 @@ class Context {
2222
void BypassPush();
2323
void BypassPop();
2424

25-
void ExpectNotRealtime(const char *intercepted_function_name);
26-
27-
private:
2825
bool InRealtimeContext() const;
2926
bool IsBypassed() const;
30-
void PrintDiagnostics(const char *intercepted_function_name);
3127

32-
int realtime_depth{0};
33-
int bypass_depth{0};
28+
Context(const Context &) = delete;
29+
Context(Context &&) = delete;
30+
Context &operator=(const Context &) = delete;
31+
Context &operator=(Context &&) = delete;
32+
33+
private:
34+
int realtime_depth_{0};
35+
int bypass_depth_{0};
3436
};
3537

3638
Context &GetContextForThisThread();
3739

40+
void ExpectNotRealtime(Context &context, const char *intercepted_function_name);
41+
void PrintDiagnostics(const char *intercepted_function_name);
42+
3843
} // namespace __rtsan

compiler-rt/lib/rtsan/tests/rtsan_test_context.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ TEST(TestRtsanContext, CanCreateContext) { __rtsan::Context context{}; }
1616

1717
TEST(TestRtsanContext, ExpectNotRealtimeDoesNotDieBeforeRealtimePush) {
1818
__rtsan::Context context{};
19-
context.ExpectNotRealtime("do_some_stuff");
19+
ExpectNotRealtime(context, "do_some_stuff");
2020
}
2121

2222
TEST(TestRtsanContext, ExpectNotRealtimeDoesNotDieAfterPushAndPop) {
2323
__rtsan::Context context{};
2424
context.RealtimePush();
2525
context.RealtimePop();
26-
context.ExpectNotRealtime("do_some_stuff");
26+
ExpectNotRealtime(context, "do_some_stuff");
2727
}
2828

2929
TEST(TestRtsanContext, ExpectNotRealtimeDiesAfterRealtimePush) {
3030
__rtsan::Context context{};
3131

3232
context.RealtimePush();
33-
EXPECT_DEATH(context.ExpectNotRealtime("do_some_stuff"), "");
33+
EXPECT_DEATH(ExpectNotRealtime(context, "do_some_stuff"), "");
3434
}
3535

3636
TEST(TestRtsanContext,
@@ -42,15 +42,15 @@ TEST(TestRtsanContext,
4242
context.RealtimePush();
4343
context.RealtimePop();
4444
context.RealtimePop();
45-
EXPECT_DEATH(context.ExpectNotRealtime("do_some_stuff"), "");
45+
EXPECT_DEATH(ExpectNotRealtime(context, "do_some_stuff"), "");
4646
}
4747

4848
TEST(TestRtsanContext, ExpectNotRealtimeDoesNotDieAfterBypassPush) {
4949
__rtsan::Context context{};
5050

5151
context.RealtimePush();
5252
context.BypassPush();
53-
context.ExpectNotRealtime("do_some_stuff");
53+
ExpectNotRealtime(context, "do_some_stuff");
5454
}
5555

5656
TEST(TestRtsanContext,
@@ -63,7 +63,7 @@ TEST(TestRtsanContext,
6363
context.BypassPush();
6464
context.BypassPop();
6565
context.BypassPop();
66-
context.ExpectNotRealtime("do_some_stuff");
66+
ExpectNotRealtime(context, "do_some_stuff");
6767
context.BypassPop();
68-
EXPECT_DEATH(context.ExpectNotRealtime("do_some_stuff"), "");
68+
EXPECT_DEATH(ExpectNotRealtime(context, "do_some_stuff"), "");
6969
}

0 commit comments

Comments
 (0)