xref: /llvm-project/compiler-rt/lib/rtsan/tests/rtsan_test_utilities.h (revision aa43f3abe0e9a7199a8df3f71364d7084f968825)
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 
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                ? ".*==ERROR: RealtimeSanitizer: unsafe-library-call.*"
34                  "Intercepted call to real-time unsafe function `" +
35                      std::string(intercepted_method_name) +
36                      "` in real-time context!"
37                : "";
38   };
39 
40   EXPECT_EXIT(RealtimeInvoke(std::forward<Function>(Func)), ExitedWithCode(43),
41               GetExpectedErrorSubstring());
42 }
43 
44 template <typename Function> void ExpectNonRealtimeSurvival(Function &&Func) {
45   std::forward<Function>(Func)();
46 }
47 
48 } // namespace rtsan_testing
49