xref: /llvm-project/compiler-rt/lib/rtsan/tests/rtsan_test_assertions.cpp (revision 3423a5e3b779db8f2e8018fad477abff67b33404)
1 //===--- rtsan_test_assertions.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 // Part of the RealtimeSanitizer runtime library test suite
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "rtsan_test_utilities.h"
14 
15 #include "rtsan/rtsan_assertions.h"
16 
17 #include "sanitizer_common/sanitizer_stacktrace.h"
18 
19 #include <gmock/gmock.h>
20 
21 using namespace __sanitizer;
22 using namespace __rtsan;
23 
24 class TestRtsanAssertions : public ::testing::Test {
25 protected:
26   void SetUp() override { __rtsan_ensure_initialized(); }
27 };
28 
29 static void ExpectViolationAction(Context &context,
30                                   bool expect_violation_callback) {
31   ::testing::MockFunction<void(const BufferedStackTrace &stack,
32                                const DiagnosticsInfo &info)>
33       mock_on_violation;
34   EXPECT_CALL(mock_on_violation, Call).Times(expect_violation_callback ? 1 : 0);
35   DiagnosticsInfo info{};
36   ExpectNotRealtime(context, info, mock_on_violation.AsStdFunction());
37 }
38 
39 TEST_F(TestRtsanAssertions,
40        ExpectNotRealtimeDoesNotCallViolationActionIfNotInRealtimeContext) {
41   Context context{};
42   ASSERT_FALSE(context.InRealtimeContext());
43   ExpectViolationAction(context, false);
44 }
45 
46 TEST_F(TestRtsanAssertions,
47        ExpectNotRealtimeCallsViolationActionIfInRealtimeContext) {
48   Context context{};
49   context.RealtimePush();
50   ASSERT_TRUE(context.InRealtimeContext());
51   ExpectViolationAction(context, true);
52 }
53 
54 TEST_F(TestRtsanAssertions,
55        ExpectNotRealtimeDoesNotCallViolationActionIfRealtimeButBypassed) {
56   Context context{};
57   context.RealtimePush();
58   context.BypassPush();
59   ASSERT_TRUE(context.IsBypassed());
60   ExpectViolationAction(context, false);
61 }
62