[compiler-rt] [rtsan][NFC] Move assertions and diagnostics into own impl files (PR #109500)

via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 20 17:38:27 PDT 2024


https://github.com/davidtrevelyan created https://github.com/llvm/llvm-project/pull/109500

In preparation for providing more information to rtsan's diagnostics output (via `__rtsan_expect_not_realtime`), this PR separates out all logic for i) making rtsan's assertions about real-time context state and ii) displaying diagnostics to the user - disentangling them both from the rtsan `Context`. 

We'll follow up this PR with a simplification to the unit tests that reflect this new separation. 

>From 2e5590030f18abb3cb272b5c542b128e4d006cfc Mon Sep 17 00:00:00 2001
From: David Trevelyan <david.trevelyan at gmail.com>
Date: Fri, 20 Sep 2024 17:19:18 -0600
Subject: [PATCH] [rtsan][NFC] Move assertions and diagnostics into own impl
 files

---
 compiler-rt/lib/rtsan/CMakeLists.txt          |  9 +--
 compiler-rt/lib/rtsan/rtsan.cpp               |  4 +-
 compiler-rt/lib/rtsan/rtsan_assertions.cpp    | 32 ++++++++++
 .../{rtsan_stack.h => rtsan_assertions.h}     |  8 ++-
 compiler-rt/lib/rtsan/rtsan_context.cpp       | 60 -------------------
 compiler-rt/lib/rtsan/rtsan_context.h         |  5 --
 ...{rtsan_stack.cpp => rtsan_diagnostics.cpp} | 36 +++++++++--
 compiler-rt/lib/rtsan/rtsan_diagnostics.h     | 18 ++++++
 .../lib/rtsan/tests/rtsan_test_context.cpp    |  1 +
 9 files changed, 94 insertions(+), 79 deletions(-)
 create mode 100644 compiler-rt/lib/rtsan/rtsan_assertions.cpp
 rename compiler-rt/lib/rtsan/{rtsan_stack.h => rtsan_assertions.h} (66%)
 rename compiler-rt/lib/rtsan/{rtsan_stack.cpp => rtsan_diagnostics.cpp} (50%)
 create mode 100644 compiler-rt/lib/rtsan/rtsan_diagnostics.h

diff --git a/compiler-rt/lib/rtsan/CMakeLists.txt b/compiler-rt/lib/rtsan/CMakeLists.txt
index 07a21b49eb45aa..68f890050c6ea8 100644
--- a/compiler-rt/lib/rtsan/CMakeLists.txt
+++ b/compiler-rt/lib/rtsan/CMakeLists.txt
@@ -2,9 +2,10 @@ include_directories(..)
 
 set(RTSAN_CXX_SOURCES
   rtsan.cpp
+  rtsan_assertions.cpp
   rtsan_context.cpp
+  rtsan_diagnostics.cpp
   rtsan_flags.cpp
-  rtsan_stack.cpp
   rtsan_interceptors.cpp)
 
 set(RTSAN_PREINIT_SOURCES
@@ -12,11 +13,11 @@ set(RTSAN_PREINIT_SOURCES
 
 set(RTSAN_HEADERS
   rtsan.h
+  rtsan_assertions.h
   rtsan_context.h
+  rtsan_diagnostics.h
   rtsan_flags.h
-  rtsan_flags.inc
-  rtsan_stack.h
-  )
+  rtsan_flags.inc)
 
 set(RTSAN_DEPS)
 
diff --git a/compiler-rt/lib/rtsan/rtsan.cpp b/compiler-rt/lib/rtsan/rtsan.cpp
index f929c9ae81c11b..1119e69333f34d 100644
--- a/compiler-rt/lib/rtsan/rtsan.cpp
+++ b/compiler-rt/lib/rtsan/rtsan.cpp
@@ -9,7 +9,8 @@
 //===----------------------------------------------------------------------===//
 
 #include <rtsan/rtsan.h>
-#include <rtsan/rtsan_context.h>
+#include <rtsan/rtsan_assertions.h>
+#include <rtsan/rtsan_diagnostics.h>
 #include <rtsan/rtsan_flags.h>
 #include <rtsan/rtsan_interceptors.h>
 
@@ -77,5 +78,4 @@ __rtsan_expect_not_realtime(const char *intercepted_function_name) {
   __rtsan_ensure_initialized();
   ExpectNotRealtime(GetContextForThisThread(), intercepted_function_name);
 }
-
 } // extern "C"
diff --git a/compiler-rt/lib/rtsan/rtsan_assertions.cpp b/compiler-rt/lib/rtsan/rtsan_assertions.cpp
new file mode 100644
index 00000000000000..7bbd0f521f4529
--- /dev/null
+++ b/compiler-rt/lib/rtsan/rtsan_assertions.cpp
@@ -0,0 +1,32 @@
+//===--- rtsan_assertions.cpp - Realtime Sanitizer --------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Part of the RealtimeSanitizer runtime library
+//
+//===----------------------------------------------------------------------===//
+
+#include "rtsan/rtsan_assertions.h"
+#include "rtsan/rtsan.h"
+#include "rtsan/rtsan_diagnostics.h"
+
+#include "sanitizer_common/sanitizer_stacktrace.h"
+
+using namespace __sanitizer;
+
+void __rtsan::ExpectNotRealtime(Context &context,
+                                const char *intercepted_function_name) {
+  CHECK(__rtsan_is_initialized());
+  if (context.InRealtimeContext() && !context.IsBypassed()) {
+    context.BypassPush();
+
+    GET_CALLER_PC_BP;
+    PrintDiagnostics(intercepted_function_name, pc, bp);
+    Die();
+    context.BypassPop();
+  }
+}
diff --git a/compiler-rt/lib/rtsan/rtsan_stack.h b/compiler-rt/lib/rtsan/rtsan_assertions.h
similarity index 66%
rename from compiler-rt/lib/rtsan/rtsan_stack.h
rename to compiler-rt/lib/rtsan/rtsan_assertions.h
index 38f811fa8643df..091de3867d72ec 100644
--- a/compiler-rt/lib/rtsan/rtsan_stack.h
+++ b/compiler-rt/lib/rtsan/rtsan_assertions.h
@@ -1,4 +1,4 @@
-//===--- rtsan_stack.h - Realtime Sanitizer ---------------------*- C++ -*-===//
+//===--- rtsan_assertions.h - Realtime Sanitizer ----------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,12 +6,14 @@
 //
 //===----------------------------------------------------------------------===//
 //
+// Part of the RealtimeSanitizer runtime library
+//
 //===----------------------------------------------------------------------===//
 
 #pragma once
 
-#include <sanitizer_common/sanitizer_internal_defs.h>
+#include "rtsan_context.h"
 
 namespace __rtsan {
-void PrintStackTrace(__sanitizer::uptr pc, __sanitizer::uptr bp);
+void ExpectNotRealtime(Context &context, const char *intercepted_function_name);
 } // namespace __rtsan
diff --git a/compiler-rt/lib/rtsan/rtsan_context.cpp b/compiler-rt/lib/rtsan/rtsan_context.cpp
index 9cf15207e63840..d7afa037f52b5b 100644
--- a/compiler-rt/lib/rtsan/rtsan_context.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_context.cpp
@@ -11,16 +11,10 @@
 #include <rtsan/rtsan.h>
 #include <rtsan/rtsan_context.h>
 
-#include <rtsan/rtsan_stack.h>
-
 #include <sanitizer_common/sanitizer_allocator_internal.h>
-#include <sanitizer_common/sanitizer_report_decorator.h>
-#include <sanitizer_common/sanitizer_stacktrace.h>
 
 #include <new>
 #include <pthread.h>
-#include <stdio.h>
-#include <stdlib.h>
 
 using namespace __sanitizer;
 
@@ -49,22 +43,6 @@ static __rtsan::Context &GetContextForThisThreadImpl() {
   return *current_thread_context;
 }
 
-/*
-    This is a placeholder stub for a future feature that will allow
-    a user to configure RTSan's behaviour when a real-time safety
-    violation is detected. The RTSan developers intend for the
-    following choices to be made available, via a RTSAN_OPTIONS
-    environment variable, in a future PR:
-
-        i) exit,
-       ii) continue, or
-      iii) wait for user input from stdin.
-
-    Until then, and to keep the first PRs small, only the exit mode
-    is available.
-*/
-static void InvokeViolationDetectedAction() { Die(); }
-
 __rtsan::Context::Context() = default;
 
 void __rtsan::Context::RealtimePush() { realtime_depth_++; }
@@ -75,48 +53,10 @@ void __rtsan::Context::BypassPush() { bypass_depth_++; }
 
 void __rtsan::Context::BypassPop() { bypass_depth_--; }
 
-void __rtsan::ExpectNotRealtime(Context &context,
-                                const char *intercepted_function_name) {
-  CHECK(__rtsan_is_initialized());
-  if (context.InRealtimeContext() && !context.IsBypassed()) {
-    context.BypassPush();
-
-    GET_CALLER_PC_BP;
-    PrintDiagnostics(intercepted_function_name, pc, bp);
-    InvokeViolationDetectedAction();
-    context.BypassPop();
-  }
-}
-
 bool __rtsan::Context::InRealtimeContext() const { return realtime_depth_ > 0; }
 
 bool __rtsan::Context::IsBypassed() const { return bypass_depth_ > 0; }
 
-namespace {
-class Decorator : public __sanitizer::SanitizerCommonDecorator {
-public:
-  Decorator() : SanitizerCommonDecorator() {}
-  const char *FunctionName() { return Green(); }
-  const char *Reason() { return Blue(); }
-};
-} // namespace
-
-void __rtsan::PrintDiagnostics(const char *intercepted_function_name, uptr pc,
-                               uptr bp) {
-  ScopedErrorReportLock l;
-
-  Decorator d;
-  Printf("%s", d.Error());
-  Report("ERROR: RealtimeSanitizer: unsafe-library-call\n");
-  Printf("%s", d.Reason());
-  Printf("Intercepted call to real-time unsafe function "
-         "`%s%s%s` in real-time context!\n",
-         d.FunctionName(), intercepted_function_name, d.Reason());
-
-  Printf("%s", d.Default());
-  __rtsan::PrintStackTrace(pc, bp);
-}
-
 __rtsan::Context &__rtsan::GetContextForThisThread() {
   return GetContextForThisThreadImpl();
 }
diff --git a/compiler-rt/lib/rtsan/rtsan_context.h b/compiler-rt/lib/rtsan/rtsan_context.h
index 4196137e70b265..8512017793a48c 100644
--- a/compiler-rt/lib/rtsan/rtsan_context.h
+++ b/compiler-rt/lib/rtsan/rtsan_context.h
@@ -38,9 +38,4 @@ class Context {
 };
 
 Context &GetContextForThisThread();
-
-void ExpectNotRealtime(Context &context, const char *intercepted_function_name);
-void PrintDiagnostics(const char *intercepted_function_name,
-                      __sanitizer::uptr pc, __sanitizer::uptr bp);
-
 } // namespace __rtsan
diff --git a/compiler-rt/lib/rtsan/rtsan_stack.cpp b/compiler-rt/lib/rtsan/rtsan_diagnostics.cpp
similarity index 50%
rename from compiler-rt/lib/rtsan/rtsan_stack.cpp
rename to compiler-rt/lib/rtsan/rtsan_diagnostics.cpp
index aeb6a2a3cbd7e2..d4c656606f3653 100644
--- a/compiler-rt/lib/rtsan/rtsan_stack.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_diagnostics.cpp
@@ -1,4 +1,4 @@
-//===--- rtsan_stack.cpp - Realtime Sanitizer -------------------*- C++ -*-===//
+//===--- rtsan_diagnostics.cpp - Realtime Sanitizer -------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -8,10 +8,11 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "rtsan_stack.h"
+#include "rtsan/rtsan_diagnostics.h"
 
-#include <sanitizer_common/sanitizer_flags.h>
-#include <sanitizer_common/sanitizer_stacktrace.h>
+#include "sanitizer_common/sanitizer_flags.h"
+#include "sanitizer_common/sanitizer_report_decorator.h"
+#include "sanitizer_common/sanitizer_stacktrace.h"
 
 using namespace __sanitizer;
 using namespace __rtsan;
@@ -29,9 +30,34 @@ void BufferedStackTrace::UnwindImpl(uptr pc, uptr bp, void *context,
 }
 } // namespace __sanitizer
 
-void __rtsan::PrintStackTrace(uptr pc, uptr bp) {
+namespace {
+class Decorator : public __sanitizer::SanitizerCommonDecorator {
+public:
+  Decorator() : SanitizerCommonDecorator() {}
+  const char *FunctionName() { return Green(); }
+  const char *Reason() { return Blue(); }
+};
+} // namespace
+
+static void PrintStackTrace(uptr pc, uptr bp) {
   BufferedStackTrace stack{};
 
   stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_fatal);
   stack.Print();
 }
+
+void __rtsan::PrintDiagnostics(const char *intercepted_function_name, uptr pc,
+                               uptr bp) {
+  ScopedErrorReportLock l;
+
+  Decorator d;
+  Printf("%s", d.Error());
+  Report("ERROR: RealtimeSanitizer: unsafe-library-call\n");
+  Printf("%s", d.Reason());
+  Printf("Intercepted call to real-time unsafe function "
+         "`%s%s%s` in real-time context!\n",
+         d.FunctionName(), intercepted_function_name, d.Reason());
+
+  Printf("%s", d.Default());
+  PrintStackTrace(pc, bp);
+}
diff --git a/compiler-rt/lib/rtsan/rtsan_diagnostics.h b/compiler-rt/lib/rtsan/rtsan_diagnostics.h
new file mode 100644
index 00000000000000..18214118a50d0a
--- /dev/null
+++ b/compiler-rt/lib/rtsan/rtsan_diagnostics.h
@@ -0,0 +1,18 @@
+//===--- rtsan_diagnostics.h - Realtime Sanitizer ---------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+//===----------------------------------------------------------------------===//
+
+#pragma once
+
+#include "sanitizer_common/sanitizer_internal_defs.h"
+
+namespace __rtsan {
+void PrintDiagnostics(const char *intercepted_function_name,
+                      __sanitizer::uptr pc, __sanitizer::uptr bp);
+} // 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 6b9d7e2df123c1..a7a96161cf2b54 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_context.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_context.cpp
@@ -11,6 +11,7 @@
 #include "rtsan_test_utilities.h"
 
 #include "rtsan/rtsan.h"
+#include "rtsan/rtsan_assertions.h"
 #include "rtsan/rtsan_context.h"
 
 #include <gtest/gtest.h>



More information about the llvm-commits mailing list