Skip to content

[rtsan] Prune rtsan context and assertions tests #109503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler-rt/lib/rtsan/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
42 changes: 42 additions & 0 deletions compiler-rt/lib/rtsan/tests/rtsan_test_assertions.cpp
Original file line number Diff line number Diff line change
@@ -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");
}
89 changes: 55 additions & 34 deletions compiler-rt/lib/rtsan/tests/rtsan_test_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Loading