xref: /freebsd/contrib/llvm-project/compiler-rt/lib/rtsan/tests/rtsan_test_assertions.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
1*700637cbSDimitry Andric //===--- rtsan_test_assertions.cpp - Realtime Sanitizer ---------*- C++ -*-===//
2*700637cbSDimitry Andric //
3*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*700637cbSDimitry Andric //
7*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
8*700637cbSDimitry Andric //
9*700637cbSDimitry Andric // Part of the RealtimeSanitizer runtime library test suite
10*700637cbSDimitry Andric //
11*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
12*700637cbSDimitry Andric 
13*700637cbSDimitry Andric #include "rtsan_test_utilities.h"
14*700637cbSDimitry Andric 
15*700637cbSDimitry Andric #include "rtsan/rtsan_assertions.h"
16*700637cbSDimitry Andric 
17*700637cbSDimitry Andric #include "sanitizer_common/sanitizer_stacktrace.h"
18*700637cbSDimitry Andric 
19*700637cbSDimitry Andric #include <gmock/gmock.h>
20*700637cbSDimitry Andric 
21*700637cbSDimitry Andric using namespace __sanitizer;
22*700637cbSDimitry Andric using namespace __rtsan;
23*700637cbSDimitry Andric 
24*700637cbSDimitry Andric class TestRtsanAssertions : public ::testing::Test {
25*700637cbSDimitry Andric protected:
SetUp()26*700637cbSDimitry Andric   void SetUp() override { __rtsan_ensure_initialized(); }
27*700637cbSDimitry Andric };
28*700637cbSDimitry Andric 
ExpectViolationAction(Context & context,bool expect_violation_callback)29*700637cbSDimitry Andric static void ExpectViolationAction(Context &context,
30*700637cbSDimitry Andric                                   bool expect_violation_callback) {
31*700637cbSDimitry Andric   ::testing::MockFunction<void(const BufferedStackTrace &stack,
32*700637cbSDimitry Andric                                const DiagnosticsInfo &info)>
33*700637cbSDimitry Andric       mock_on_violation;
34*700637cbSDimitry Andric   EXPECT_CALL(mock_on_violation, Call).Times(expect_violation_callback ? 1 : 0);
35*700637cbSDimitry Andric   DiagnosticsInfo info{};
36*700637cbSDimitry Andric   ExpectNotRealtime(context, info, mock_on_violation.AsStdFunction());
37*700637cbSDimitry Andric }
38*700637cbSDimitry Andric 
TEST_F(TestRtsanAssertions,ExpectNotRealtimeDoesNotCallViolationActionIfNotInRealtimeContext)39*700637cbSDimitry Andric TEST_F(TestRtsanAssertions,
40*700637cbSDimitry Andric        ExpectNotRealtimeDoesNotCallViolationActionIfNotInRealtimeContext) {
41*700637cbSDimitry Andric   Context context{};
42*700637cbSDimitry Andric   ASSERT_FALSE(context.InRealtimeContext());
43*700637cbSDimitry Andric   ExpectViolationAction(context, false);
44*700637cbSDimitry Andric }
45*700637cbSDimitry Andric 
TEST_F(TestRtsanAssertions,ExpectNotRealtimeCallsViolationActionIfInRealtimeContext)46*700637cbSDimitry Andric TEST_F(TestRtsanAssertions,
47*700637cbSDimitry Andric        ExpectNotRealtimeCallsViolationActionIfInRealtimeContext) {
48*700637cbSDimitry Andric   Context context{};
49*700637cbSDimitry Andric   context.RealtimePush();
50*700637cbSDimitry Andric   ASSERT_TRUE(context.InRealtimeContext());
51*700637cbSDimitry Andric   ExpectViolationAction(context, true);
52*700637cbSDimitry Andric }
53*700637cbSDimitry Andric 
TEST_F(TestRtsanAssertions,ExpectNotRealtimeDoesNotCallViolationActionIfRealtimeButBypassed)54*700637cbSDimitry Andric TEST_F(TestRtsanAssertions,
55*700637cbSDimitry Andric        ExpectNotRealtimeDoesNotCallViolationActionIfRealtimeButBypassed) {
56*700637cbSDimitry Andric   Context context{};
57*700637cbSDimitry Andric   context.RealtimePush();
58*700637cbSDimitry Andric   context.BypassPush();
59*700637cbSDimitry Andric   ASSERT_TRUE(context.IsBypassed());
60*700637cbSDimitry Andric   ExpectViolationAction(context, false);
61*700637cbSDimitry Andric }
62