1 //===--- rtsan_test_context.cpp - Realtime Sanitizer ------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //===----------------------------------------------------------------------===//
10
11 #include "rtsan_test_utilities.h"
12
13 #include "rtsan/rtsan.h"
14 #include "rtsan/rtsan_context.h"
15
16 #include <gtest/gtest.h>
17
18 using namespace __rtsan;
19 using namespace ::testing;
20
21 class TestRtsanContext : public Test {
22 protected:
SetUp()23 void SetUp() override { __rtsan_ensure_initialized(); }
24 };
25
TEST_F(TestRtsanContext,IsNotRealtimeAfterDefaultConstruction)26 TEST_F(TestRtsanContext, IsNotRealtimeAfterDefaultConstruction) {
27 Context context{};
28 EXPECT_THAT(context.InRealtimeContext(), Eq(false));
29 }
30
TEST_F(TestRtsanContext,IsRealtimeAfterRealtimePush)31 TEST_F(TestRtsanContext, IsRealtimeAfterRealtimePush) {
32 Context context{};
33 context.RealtimePush();
34 EXPECT_THAT(context.InRealtimeContext(), Eq(true));
35 }
36
TEST_F(TestRtsanContext,IsNotRealtimeAfterRealtimePushAndPop)37 TEST_F(TestRtsanContext, IsNotRealtimeAfterRealtimePushAndPop) {
38 Context context{};
39 context.RealtimePush();
40 ASSERT_THAT(context.InRealtimeContext(), Eq(true));
41 context.RealtimePop();
42 EXPECT_THAT(context.InRealtimeContext(), Eq(false));
43 }
44
TEST_F(TestRtsanContext,RealtimeContextStateIsStatefullyTracked)45 TEST_F(TestRtsanContext, RealtimeContextStateIsStatefullyTracked) {
46 Context context{};
47 auto const ExpectRealtime = [&context](bool is_rt) {
48 EXPECT_THAT(context.InRealtimeContext(), Eq(is_rt));
49 };
50 ExpectRealtime(false);
51 context.RealtimePush(); // depth 1
52 ExpectRealtime(true);
53 context.RealtimePush(); // depth 2
54 ExpectRealtime(true);
55 context.RealtimePop(); // depth 1
56 ExpectRealtime(true);
57 context.RealtimePush(); // depth 2
58 ExpectRealtime(true);
59 context.RealtimePop(); // depth 1
60 ExpectRealtime(true);
61 context.RealtimePop(); // depth 0
62 ExpectRealtime(false);
63 context.RealtimePush(); // depth 1
64 ExpectRealtime(true);
65 }
66
TEST_F(TestRtsanContext,IsNotBypassedAfterDefaultConstruction)67 TEST_F(TestRtsanContext, IsNotBypassedAfterDefaultConstruction) {
68 Context context{};
69 EXPECT_THAT(context.IsBypassed(), Eq(false));
70 }
71
TEST_F(TestRtsanContext,IsBypassedAfterBypassPush)72 TEST_F(TestRtsanContext, IsBypassedAfterBypassPush) {
73 Context context{};
74 context.BypassPush();
75 EXPECT_THAT(context.IsBypassed(), Eq(true));
76 }
77
TEST_F(TestRtsanContext,BypassedStateIsStatefullyTracked)78 TEST_F(TestRtsanContext, BypassedStateIsStatefullyTracked) {
79 Context context{};
80 auto const ExpectBypassed = [&context](bool is_bypassed) {
81 EXPECT_THAT(context.IsBypassed(), Eq(is_bypassed));
82 };
83 ExpectBypassed(false);
84 context.BypassPush(); // depth 1
85 ExpectBypassed(true);
86 context.BypassPush(); // depth 2
87 ExpectBypassed(true);
88 context.BypassPop(); // depth 1
89 ExpectBypassed(true);
90 context.BypassPush(); // depth 2
91 ExpectBypassed(true);
92 context.BypassPop(); // depth 1
93 ExpectBypassed(true);
94 context.BypassPop(); // depth 0
95 ExpectBypassed(false);
96 context.BypassPush(); // depth 1
97 ExpectBypassed(true);
98 }
99