[compiler-rt] [rtsan][NFC] Refactor to scoped bypasser for __rtsan::Context (PR #111438)

Chris Apple via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 7 14:05:44 PDT 2024


https://github.com/cjappl created https://github.com/llvm/llvm-project/pull/111438

In an upcoming review, this code block in rtsan_assertions is going to get a lot messier.

Getting this in as a quick early refactor

>From b825c9784fd8079d1580802b1b68511d7f1b1b61 Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Mon, 7 Oct 2024 14:04:21 -0700
Subject: [PATCH] [rtsan][NFC] Refactor to scoped bypasser for Context

---
 compiler-rt/lib/rtsan/rtsan_assertions.h |  3 +--
 compiler-rt/lib/rtsan/rtsan_context.h    | 12 ++++++++++++
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/lib/rtsan/rtsan_assertions.h b/compiler-rt/lib/rtsan/rtsan_assertions.h
index 1a653d198ab88b..4646e750b6796e 100644
--- a/compiler-rt/lib/rtsan/rtsan_assertions.h
+++ b/compiler-rt/lib/rtsan/rtsan_assertions.h
@@ -21,9 +21,8 @@ template <typename OnViolationAction>
 void ExpectNotRealtime(Context &context, OnViolationAction &&OnViolation) {
   CHECK(__rtsan_is_initialized());
   if (context.InRealtimeContext() && !context.IsBypassed()) {
-    context.BypassPush();
+    ScopedBypass sb{context};
     OnViolation();
-    context.BypassPop();
   }
 }
 
diff --git a/compiler-rt/lib/rtsan/rtsan_context.h b/compiler-rt/lib/rtsan/rtsan_context.h
index cb0c2eb0a5e0d7..2d906259e1fe35 100644
--- a/compiler-rt/lib/rtsan/rtsan_context.h
+++ b/compiler-rt/lib/rtsan/rtsan_context.h
@@ -35,5 +35,17 @@ class Context {
   int bypass_depth_{0};
 };
 
+class ScopedBypass {
+public:
+  [[nodiscard]] explicit ScopedBypass(Context &context) : context_(context) {
+    context_.BypassPush();
+  }
+
+  ~ScopedBypass() { context_.BypassPop(); }
+
+private:
+  Context &context_;
+};
+
 Context &GetContextForThisThread();
 } // namespace __rtsan



More information about the llvm-commits mailing list