xref: /freebsd/contrib/llvm-project/compiler-rt/lib/rtsan/tests/rtsan_test_utilities.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===--- rtsan_test_utilities.h - 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 #pragma once
12 
13 #include "rtsan.h"
14 #include "gmock/gmock.h"
15 #include <string>
16 
17 namespace rtsan_testing {
18 
RealtimeInvoke(Function && Func)19 template <typename Function> void RealtimeInvoke(Function &&Func) {
20   __rtsan_realtime_enter();
21   std::forward<Function>(Func)();
22   __rtsan_realtime_exit();
23 }
24 
25 template <typename Function>
26 void ExpectRealtimeDeath(Function &&Func,
27                          const char *intercepted_method_name = nullptr) {
28 
29   using namespace testing;
30 
31   auto GetExpectedErrorSubstring = [&]() -> std::string {
32     return intercepted_method_name != nullptr
33                ? "Real-time violation: intercepted call to real-time unsafe "
34                  "function `" +
35                      std::string(intercepted_method_name) + "`"
36                : "";
37   };
38 
39   EXPECT_EXIT(RealtimeInvoke(std::forward<Function>(Func)),
40               ExitedWithCode(EXIT_FAILURE), GetExpectedErrorSubstring());
41 }
42 
ExpectNonRealtimeSurvival(Function && Func)43 template <typename Function> void ExpectNonRealtimeSurvival(Function &&Func) {
44   std::forward<Function>(Func)();
45 }
46 
47 } // namespace rtsan_testing
48