[compiler-rt] a04db2c - [rtsan] Decouple assertions from error actions (#109535)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Sep 21 11:14:14 PDT 2024
Author: davidtrevelyan
Date: 2024-09-21T12:14:11-06:00
New Revision: a04db2c7a6f46ea147e7635dbfe349c68341c897
URL: https://github.com/llvm/llvm-project/commit/a04db2c7a6f46ea147e7635dbfe349c68341c897
DIFF: https://github.com/llvm/llvm-project/commit/a04db2c7a6f46ea147e7635dbfe349c68341c897.diff
LOG: [rtsan] Decouple assertions from error actions (#109535)
Decouples sanitizer assertion `ExpectNotRealtime` from the action that
should happen if a real-time context is detected.
Added:
Modified:
compiler-rt/lib/rtsan/CMakeLists.txt
compiler-rt/lib/rtsan/rtsan.cpp
compiler-rt/lib/rtsan/rtsan_assertions.h
compiler-rt/lib/rtsan/tests/rtsan_test_assertions.cpp
Removed:
compiler-rt/lib/rtsan/rtsan_assertions.cpp
################################################################################
diff --git a/compiler-rt/lib/rtsan/CMakeLists.txt b/compiler-rt/lib/rtsan/CMakeLists.txt
index 68f890050c6ea8..0fc3a3f8f48960 100644
--- a/compiler-rt/lib/rtsan/CMakeLists.txt
+++ b/compiler-rt/lib/rtsan/CMakeLists.txt
@@ -2,7 +2,6 @@ include_directories(..)
set(RTSAN_CXX_SOURCES
rtsan.cpp
- rtsan_assertions.cpp
rtsan_context.cpp
rtsan_diagnostics.cpp
rtsan_flags.cpp
diff --git a/compiler-rt/lib/rtsan/rtsan.cpp b/compiler-rt/lib/rtsan/rtsan.cpp
index 936f0b5b8cee39..2afdf3c76696e7 100644
--- a/compiler-rt/lib/rtsan/rtsan.cpp
+++ b/compiler-rt/lib/rtsan/rtsan.cpp
@@ -10,6 +10,7 @@
#include <rtsan/rtsan.h>
#include <rtsan/rtsan_assertions.h>
+#include <rtsan/rtsan_diagnostics.h>
#include <rtsan/rtsan_flags.h>
#include <rtsan/rtsan_interceptors.h>
@@ -28,6 +29,13 @@ static void SetInitialized() {
atomic_store(&rtsan_initialized, 1, memory_order_release);
}
+static auto PrintDiagnosticsAndDieAction(DiagnosticsInfo info) {
+ return [info]() {
+ __rtsan::PrintDiagnostics(info);
+ Die();
+ };
+}
+
extern "C" {
SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_init() {
@@ -74,22 +82,21 @@ SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_enable() {
}
SANITIZER_INTERFACE_ATTRIBUTE void
-__rtsan_notify_intercepted_call(const char *intercepted_function_name) {
+__rtsan_notify_intercepted_call(const char *func_name) {
__rtsan_ensure_initialized();
-
GET_CALLER_PC_BP;
- DiagnosticsInfo info = {InterceptedCallInfo{intercepted_function_name}, pc,
- bp};
- ExpectNotRealtime(GetContextForThisThread(), info);
+ ExpectNotRealtime(
+ GetContextForThisThread(),
+ PrintDiagnosticsAndDieAction({InterceptedCallInfo{func_name}, pc, bp}));
}
SANITIZER_INTERFACE_ATTRIBUTE void
-__rtsan_notify_blocking_call(const char *blocking_function_name) {
+__rtsan_notify_blocking_call(const char *func_name) {
__rtsan_ensure_initialized();
-
GET_CALLER_PC_BP;
- DiagnosticsInfo info = {BlockingCallInfo{blocking_function_name}, pc, bp};
- ExpectNotRealtime(GetContextForThisThread(), info);
+ ExpectNotRealtime(
+ GetContextForThisThread(),
+ PrintDiagnosticsAndDieAction({BlockingCallInfo{func_name}, pc, bp}));
}
} // extern "C"
diff --git a/compiler-rt/lib/rtsan/rtsan_assertions.cpp b/compiler-rt/lib/rtsan/rtsan_assertions.cpp
deleted file mode 100644
index 4aae85de5c52f1..00000000000000
--- a/compiler-rt/lib/rtsan/rtsan_assertions.cpp
+++ /dev/null
@@ -1,28 +0,0 @@
-//===--- 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"
-
-using namespace __sanitizer;
-
-void __rtsan::ExpectNotRealtime(Context &context, const DiagnosticsInfo &info) {
- CHECK(__rtsan_is_initialized());
- if (context.InRealtimeContext() && !context.IsBypassed()) {
- context.BypassPush();
-
- PrintDiagnostics(info);
- Die();
- context.BypassPop();
- }
-}
diff --git a/compiler-rt/lib/rtsan/rtsan_assertions.h b/compiler-rt/lib/rtsan/rtsan_assertions.h
index bc1235363669df..1a653d198ab88b 100644
--- a/compiler-rt/lib/rtsan/rtsan_assertions.h
+++ b/compiler-rt/lib/rtsan/rtsan_assertions.h
@@ -12,10 +12,19 @@
#pragma once
+#include "rtsan/rtsan.h"
#include "rtsan/rtsan_context.h"
-#include "rtsan/rtsan_diagnostics.h"
namespace __rtsan {
-void ExpectNotRealtime(Context &context, const DiagnosticsInfo &info);
+template <typename OnViolationAction>
+void ExpectNotRealtime(Context &context, OnViolationAction &&OnViolation) {
+ CHECK(__rtsan_is_initialized());
+ if (context.InRealtimeContext() && !context.IsBypassed()) {
+ context.BypassPush();
+ OnViolation();
+ context.BypassPop();
+ }
+}
+
} // namespace __rtsan
diff --git a/compiler-rt/lib/rtsan/tests/rtsan_test_assertions.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_assertions.cpp
index b6999eeb7746a2..58f7dbae96e9f5 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_assertions.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_assertions.cpp
@@ -13,9 +13,8 @@
#include "rtsan_test_utilities.h"
#include "rtsan/rtsan_assertions.h"
-#include "rtsan/rtsan_diagnostics.h"
-#include <gtest/gtest.h>
+#include <gmock/gmock.h>
using namespace __rtsan;
@@ -24,30 +23,33 @@ class TestRtsanAssertions : public ::testing::Test {
void SetUp() override { __rtsan_ensure_initialized(); }
};
-DiagnosticsInfo FakeDiagnosticsInfo() {
- DiagnosticsInfo info;
- info.pc = 0;
- info.bp = 0;
- info.call_info = InterceptedCallInfo{"fake_function_name"};
- return info;
+static void ExpectViolationAction(__rtsan::Context &context,
+ bool expect_violation_callback) {
+ ::testing::MockFunction<void()> mock_on_violation;
+ EXPECT_CALL(mock_on_violation, Call).Times(expect_violation_callback ? 1 : 0);
+ ExpectNotRealtime(context, mock_on_violation.AsStdFunction());
}
-TEST_F(TestRtsanAssertions, ExpectNotRealtimeDoesNotDieIfNotInRealtimeContext) {
+TEST_F(TestRtsanAssertions,
+ ExpectNotRealtimeDoesNotCallViolationActionIfNotInRealtimeContext) {
__rtsan::Context context{};
ASSERT_FALSE(context.InRealtimeContext());
- ExpectNotRealtime(context, FakeDiagnosticsInfo());
+ ExpectViolationAction(context, false);
}
-TEST_F(TestRtsanAssertions, ExpectNotRealtimeDiesIfInRealtimeContext) {
+TEST_F(TestRtsanAssertions,
+ ExpectNotRealtimeCallsViolationActionIfInRealtimeContext) {
__rtsan::Context context{};
context.RealtimePush();
ASSERT_TRUE(context.InRealtimeContext());
- EXPECT_DEATH(ExpectNotRealtime(context, FakeDiagnosticsInfo()), "");
+ ExpectViolationAction(context, true);
}
-TEST_F(TestRtsanAssertions, ExpectNotRealtimeDoesNotDieIfRealtimeButBypassed) {
+TEST_F(TestRtsanAssertions,
+ ExpectNotRealtimeDoesNotCallViolationActionIfRealtimeButBypassed) {
__rtsan::Context context{};
context.RealtimePush();
context.BypassPush();
- ExpectNotRealtime(context, FakeDiagnosticsInfo());
+ ASSERT_TRUE(context.IsBypassed());
+ ExpectViolationAction(context, false);
}
More information about the llvm-commits
mailing list