diff --git a/compiler-rt/lib/rtsan/rtsan_assertions.h b/compiler-rt/lib/rtsan/rtsan_assertions.h index 1a653d198ab88..4646e750b6796 100644 --- a/compiler-rt/lib/rtsan/rtsan_assertions.h +++ b/compiler-rt/lib/rtsan/rtsan_assertions.h @@ -21,9 +21,8 @@ template 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 cb0c2eb0a5e0d..97fd9b48062ec 100644 --- a/compiler-rt/lib/rtsan/rtsan_context.h +++ b/compiler-rt/lib/rtsan/rtsan_context.h @@ -35,5 +35,22 @@ class Context { int bypass_depth_{0}; }; +class ScopedBypass { +public: + [[nodiscard]] explicit ScopedBypass(Context &context) : context_(context) { + context_.BypassPush(); + } + + ~ScopedBypass() { context_.BypassPop(); } + + ScopedBypass(const ScopedBypass &) = delete; + ScopedBypass &operator=(const ScopedBypass &) = delete; + ScopedBypass(ScopedBypass &&) = delete; + ScopedBypass &operator=(ScopedBypass &&) = delete; + +private: + Context &context_; +}; + Context &GetContextForThisThread(); } // namespace __rtsan