[compiler-rt] [rtsan][NFC] Prune rtsan context and assertions tests (PR #109503)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 20 19:01:48 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-compiler-rt-sanitizer
Author: None (davidtrevelyan)
<details>
<summary>Changes</summary>
Disentangles (and simplifies) integration-like tests for `__rtsan::ExpectNotRealtime` and `__rtsan::Context` into simpler unit tests. None of the tests are new, but their assertions have changed to reflect the more direct testing strategy.
---
Full diff: https://github.com/llvm/llvm-project/pull/109503.diff
3 Files Affected:
- (modified) compiler-rt/lib/rtsan/tests/CMakeLists.txt (+1)
- (added) compiler-rt/lib/rtsan/tests/rtsan_test_assertions.cpp (+42)
- (modified) compiler-rt/lib/rtsan/tests/rtsan_test_context.cpp (+55-34)
``````````diff
diff --git a/compiler-rt/lib/rtsan/tests/CMakeLists.txt b/compiler-rt/lib/rtsan/tests/CMakeLists.txt
index 0529917bc895ce..139eea785fcdca 100644
--- a/compiler-rt/lib/rtsan/tests/CMakeLists.txt
+++ b/compiler-rt/lib/rtsan/tests/CMakeLists.txt
@@ -21,6 +21,7 @@ set(RTSAN_INST_TEST_SOURCES
set(RTSAN_NOINST_TEST_SOURCES
../rtsan_preinit.cpp
+ rtsan_test_assertions.cpp
rtsan_test_context.cpp
rtsan_test_main.cpp)
diff --git a/compiler-rt/lib/rtsan/tests/rtsan_test_assertions.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_assertions.cpp
new file mode 100644
index 00000000000000..cdf2ac32170043
--- /dev/null
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_assertions.cpp
@@ -0,0 +1,42 @@
+//===--- rtsan_test_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 test suite
+//
+//===----------------------------------------------------------------------===//
+
+#include "rtsan_test_utilities.h"
+
+#include "rtsan/rtsan_assertions.h"
+
+#include <gtest/gtest.h>
+
+class TestRtsanAssertions : public ::testing::Test {
+protected:
+ void SetUp() override { __rtsan_ensure_initialized(); }
+};
+
+TEST_F(TestRtsanAssertions, ExpectNotRealtimeDoesNotDieIfNotInRealtimeContext) {
+ __rtsan::Context context{};
+ ASSERT_FALSE(context.InRealtimeContext());
+ ExpectNotRealtime(context, "fake_function_name");
+}
+
+TEST_F(TestRtsanAssertions, ExpectNotRealtimeDiesIfInRealtimeContext) {
+ __rtsan::Context context{};
+ context.RealtimePush();
+ ASSERT_TRUE(context.InRealtimeContext());
+ EXPECT_DEATH(ExpectNotRealtime(context, "fake_function_name"), "");
+}
+
+TEST_F(TestRtsanAssertions, ExpectNotRealtimeDoesNotDieIfRealtimeButBypassed) {
+ __rtsan::Context context{};
+ context.RealtimePush();
+ context.BypassPush();
+ ExpectNotRealtime(context, "fake_function_name");
+}
diff --git a/compiler-rt/lib/rtsan/tests/rtsan_test_context.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_context.cpp
index a7a96161cf2b54..9ba33185bde28e 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_context.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_context.cpp
@@ -11,66 +11,87 @@
#include "rtsan_test_utilities.h"
#include "rtsan/rtsan.h"
-#include "rtsan/rtsan_assertions.h"
#include "rtsan/rtsan_context.h"
#include <gtest/gtest.h>
-class TestRtsanContext : public ::testing::Test {
+using namespace ::testing;
+
+class TestRtsanContext : public Test {
protected:
void SetUp() override { __rtsan_ensure_initialized(); }
};
-TEST_F(TestRtsanContext, ExpectNotRealtimeDoesNotDieBeforeRealtimePush) {
+TEST_F(TestRtsanContext, IsNotRealtimeAfterDefaultConstruction) {
__rtsan::Context context{};
- ExpectNotRealtime(context, "do_some_stuff");
+ EXPECT_THAT(context.InRealtimeContext(), Eq(false));
}
-TEST_F(TestRtsanContext, ExpectNotRealtimeDoesNotDieAfterPushAndPop) {
+TEST_F(TestRtsanContext, IsRealtimeAfterRealtimePush) {
__rtsan::Context context{};
context.RealtimePush();
- context.RealtimePop();
- ExpectNotRealtime(context, "do_some_stuff");
+ EXPECT_THAT(context.InRealtimeContext(), Eq(true));
}
-TEST_F(TestRtsanContext, ExpectNotRealtimeDiesAfterRealtimePush) {
+TEST_F(TestRtsanContext, IsNotRealtimeAfterRealtimePushAndPop) {
__rtsan::Context context{};
-
context.RealtimePush();
- EXPECT_DEATH(ExpectNotRealtime(context, "do_some_stuff"), "");
+ ASSERT_THAT(context.InRealtimeContext(), Eq(true));
+ context.RealtimePop();
+ EXPECT_THAT(context.InRealtimeContext(), Eq(false));
}
-TEST_F(TestRtsanContext,
- ExpectNotRealtimeDiesAfterRealtimeAfterMorePushesThanPops) {
+TEST_F(TestRtsanContext, RealtimeContextStateIsStatefullyTracked) {
__rtsan::Context context{};
-
- context.RealtimePush();
- context.RealtimePush();
- context.RealtimePush();
- context.RealtimePop();
- context.RealtimePop();
- EXPECT_DEATH(ExpectNotRealtime(context, "do_some_stuff"), "");
+ auto const expect_rt = [&context](bool is_rt) {
+ EXPECT_THAT(context.InRealtimeContext(), Eq(is_rt));
+ };
+ expect_rt(false);
+ context.RealtimePush(); // depth 1
+ expect_rt(true);
+ context.RealtimePush(); // depth 2
+ expect_rt(true);
+ context.RealtimePop(); // depth 1
+ expect_rt(true);
+ context.RealtimePush(); // depth 2
+ expect_rt(true);
+ context.RealtimePop(); // depth 1
+ expect_rt(true);
+ context.RealtimePop(); // depth 0
+ expect_rt(false);
+ context.RealtimePush(); // depth 1
+ expect_rt(true);
}
-TEST_F(TestRtsanContext, ExpectNotRealtimeDoesNotDieAfterBypassPush) {
+TEST_F(TestRtsanContext, IsNotBypassedAfterDefaultConstruction) {
__rtsan::Context context{};
+ EXPECT_THAT(context.IsBypassed(), Eq(false));
+}
- context.RealtimePush();
+TEST_F(TestRtsanContext, IsBypassedAfterBypassPush) {
+ __rtsan::Context context{};
context.BypassPush();
- ExpectNotRealtime(context, "do_some_stuff");
+ EXPECT_THAT(context.IsBypassed(), Eq(true));
}
-TEST_F(TestRtsanContext,
- ExpectNotRealtimeDoesNotDieIfBypassDepthIsGreaterThanZero) {
+TEST_F(TestRtsanContext, BypassedStateIsStatefullyTracked) {
__rtsan::Context context{};
-
- context.RealtimePush();
- context.BypassPush();
- context.BypassPush();
- context.BypassPush();
- context.BypassPop();
- context.BypassPop();
- ExpectNotRealtime(context, "do_some_stuff");
- context.BypassPop();
- EXPECT_DEATH(ExpectNotRealtime(context, "do_some_stuff"), "");
+ auto const expect_bypassed = [&context](bool is_bypassed) {
+ EXPECT_THAT(context.IsBypassed(), Eq(is_bypassed));
+ };
+ expect_bypassed(false);
+ context.BypassPush(); // depth 1
+ expect_bypassed(true);
+ context.BypassPush(); // depth 2
+ expect_bypassed(true);
+ context.BypassPop(); // depth 1
+ expect_bypassed(true);
+ context.BypassPush(); // depth 2
+ expect_bypassed(true);
+ context.BypassPop(); // depth 1
+ expect_bypassed(true);
+ context.BypassPop(); // depth 0
+ expect_bypassed(false);
+ context.BypassPush(); // depth 1
+ expect_bypassed(true);
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/109503
More information about the llvm-commits
mailing list