[compiler-rt] 30d56be - [compiler-rt][rtsan] NFC: Refactor context helper functions (#106869)

via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 2 08:45:17 PDT 2024


Author: Chris Apple
Date: 2024-09-02T08:45:13-07:00
New Revision: 30d56bedd0a77c4c075e4cdc6191611bb84c8a49

URL: https://github.com/llvm/llvm-project/commit/30d56bedd0a77c4c075e4cdc6191611bb84c8a49
DIFF: https://github.com/llvm/llvm-project/commit/30d56bedd0a77c4c075e4cdc6191611bb84c8a49.diff

LOG: [compiler-rt][rtsan] NFC: Refactor context helper functions (#106869)

Added: 
    

Modified: 
    compiler-rt/lib/rtsan/rtsan.cpp
    compiler-rt/lib/rtsan/rtsan_context.cpp
    compiler-rt/lib/rtsan/rtsan_context.h
    compiler-rt/lib/rtsan/tests/rtsan_test_context.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/rtsan/rtsan.cpp b/compiler-rt/lib/rtsan/rtsan.cpp
index b2c4616b5fd0dc..1388ce66cbde22 100644
--- a/compiler-rt/lib/rtsan/rtsan.cpp
+++ b/compiler-rt/lib/rtsan/rtsan.cpp
@@ -69,8 +69,7 @@ SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_enable() {
 SANITIZER_INTERFACE_ATTRIBUTE void
 __rtsan_expect_not_realtime(const char *intercepted_function_name) {
   __rtsan_ensure_initialized();
-  __rtsan::GetContextForThisThread().ExpectNotRealtime(
-      intercepted_function_name);
+  ExpectNotRealtime(GetContextForThisThread(), intercepted_function_name);
 }
 
 } // extern "C"

diff  --git a/compiler-rt/lib/rtsan/rtsan_context.cpp b/compiler-rt/lib/rtsan/rtsan_context.cpp
index f761ddedce6727..81ee44670ff3e8 100644
--- a/compiler-rt/lib/rtsan/rtsan_context.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_context.cpp
@@ -63,29 +63,29 @@ static void InvokeViolationDetectedAction() { exit(EXIT_FAILURE); }
 
 __rtsan::Context::Context() = default;
 
-void __rtsan::Context::RealtimePush() { realtime_depth++; }
+void __rtsan::Context::RealtimePush() { realtime_depth_++; }
 
-void __rtsan::Context::RealtimePop() { realtime_depth--; }
+void __rtsan::Context::RealtimePop() { realtime_depth_--; }
 
-void __rtsan::Context::BypassPush() { bypass_depth++; }
+void __rtsan::Context::BypassPush() { bypass_depth_++; }
 
-void __rtsan::Context::BypassPop() { bypass_depth--; }
+void __rtsan::Context::BypassPop() { bypass_depth_--; }
 
-void __rtsan::Context::ExpectNotRealtime(
-    const char *intercepted_function_name) {
-  if (InRealtimeContext() && !IsBypassed()) {
-    BypassPush();
+void __rtsan::ExpectNotRealtime(Context &context,
+                                const char *intercepted_function_name) {
+  if (context.InRealtimeContext() && !context.IsBypassed()) {
+    context.BypassPush();
     PrintDiagnostics(intercepted_function_name);
     InvokeViolationDetectedAction();
-    BypassPop();
+    context.BypassPop();
   }
 }
 
-bool __rtsan::Context::InRealtimeContext() const { return realtime_depth > 0; }
+bool __rtsan::Context::InRealtimeContext() const { return realtime_depth_ > 0; }
 
-bool __rtsan::Context::IsBypassed() const { return bypass_depth > 0; }
+bool __rtsan::Context::IsBypassed() const { return bypass_depth_ > 0; }
 
-void __rtsan::Context::PrintDiagnostics(const char *intercepted_function_name) {
+void __rtsan::PrintDiagnostics(const char *intercepted_function_name) {
   fprintf(stderr,
           "Real-time violation: intercepted call to real-time unsafe function "
           "`%s` in real-time context! Stack trace:\n",

diff  --git a/compiler-rt/lib/rtsan/rtsan_context.h b/compiler-rt/lib/rtsan/rtsan_context.h
index 515bb8ade1eb97..c8f0bf20e75a78 100644
--- a/compiler-rt/lib/rtsan/rtsan_context.h
+++ b/compiler-rt/lib/rtsan/rtsan_context.h
@@ -22,17 +22,22 @@ class Context {
   void BypassPush();
   void BypassPop();
 
-  void ExpectNotRealtime(const char *intercepted_function_name);
-
-private:
   bool InRealtimeContext() const;
   bool IsBypassed() const;
-  void PrintDiagnostics(const char *intercepted_function_name);
 
-  int realtime_depth{0};
-  int bypass_depth{0};
+  Context(const Context &) = delete;
+  Context(Context &&) = delete;
+  Context &operator=(const Context &) = delete;
+  Context &operator=(Context &&) = delete;
+
+private:
+  int realtime_depth_{0};
+  int bypass_depth_{0};
 };
 
 Context &GetContextForThisThread();
 
+void ExpectNotRealtime(Context &context, const char *intercepted_function_name);
+void PrintDiagnostics(const char *intercepted_function_name);
+
 } // namespace __rtsan

diff  --git a/compiler-rt/lib/rtsan/tests/rtsan_test_context.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_context.cpp
index b7e73236a14cc6..cad8cf2d539960 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_context.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_context.cpp
@@ -16,21 +16,21 @@ TEST(TestRtsanContext, CanCreateContext) { __rtsan::Context context{}; }
 
 TEST(TestRtsanContext, ExpectNotRealtimeDoesNotDieBeforeRealtimePush) {
   __rtsan::Context context{};
-  context.ExpectNotRealtime("do_some_stuff");
+  ExpectNotRealtime(context, "do_some_stuff");
 }
 
 TEST(TestRtsanContext, ExpectNotRealtimeDoesNotDieAfterPushAndPop) {
   __rtsan::Context context{};
   context.RealtimePush();
   context.RealtimePop();
-  context.ExpectNotRealtime("do_some_stuff");
+  ExpectNotRealtime(context, "do_some_stuff");
 }
 
 TEST(TestRtsanContext, ExpectNotRealtimeDiesAfterRealtimePush) {
   __rtsan::Context context{};
 
   context.RealtimePush();
-  EXPECT_DEATH(context.ExpectNotRealtime("do_some_stuff"), "");
+  EXPECT_DEATH(ExpectNotRealtime(context, "do_some_stuff"), "");
 }
 
 TEST(TestRtsanContext,
@@ -42,7 +42,7 @@ TEST(TestRtsanContext,
   context.RealtimePush();
   context.RealtimePop();
   context.RealtimePop();
-  EXPECT_DEATH(context.ExpectNotRealtime("do_some_stuff"), "");
+  EXPECT_DEATH(ExpectNotRealtime(context, "do_some_stuff"), "");
 }
 
 TEST(TestRtsanContext, ExpectNotRealtimeDoesNotDieAfterBypassPush) {
@@ -50,7 +50,7 @@ TEST(TestRtsanContext, ExpectNotRealtimeDoesNotDieAfterBypassPush) {
 
   context.RealtimePush();
   context.BypassPush();
-  context.ExpectNotRealtime("do_some_stuff");
+  ExpectNotRealtime(context, "do_some_stuff");
 }
 
 TEST(TestRtsanContext,
@@ -63,7 +63,7 @@ TEST(TestRtsanContext,
   context.BypassPush();
   context.BypassPop();
   context.BypassPop();
-  context.ExpectNotRealtime("do_some_stuff");
+  ExpectNotRealtime(context, "do_some_stuff");
   context.BypassPop();
-  EXPECT_DEATH(context.ExpectNotRealtime("do_some_stuff"), "");
+  EXPECT_DEATH(ExpectNotRealtime(context, "do_some_stuff"), "");
 }


        


More information about the llvm-commits mailing list