xref: /llvm-project/compiler-rt/test/rtsan/stack_suppressions.cpp (revision 8f8d5f005a937bf8d5244c5bf22906095ff08c70)
1 // RUN: %clangxx -fsanitize=realtime %s -o %t
2 // RUN: %env_rtsan_opts=halt_on_error=false %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-NOSUPPRESSIONS
3 // RUN: %env_rtsan_opts=suppressions='%s.supp':print_stats_on_exit=true not %run %t 2>&1 | FileCheck %s
4 // UNSUPPORTED: ios
5 
6 // Intent: Ensure that suppressions work as intended
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 
12 #include <atomic>
13 #include <vector>
14 
15 std::atomic<int> cas_atomic{0};
16 
17 void *MallocViolation() { return malloc(10); }
18 
19 void VectorViolations() {
20   // All of these should be suppressed by *vector*
21   std::vector<int> v(10);
22   v.resize(20);
23   v.clear();
24   v.resize(0);
25   v.push_back(1);
26   v.reserve(10);
27 }
28 
29 void BlockFunc() [[clang::blocking]] {
30   int expected = 0;
31   while (!cas_atomic.compare_exchange_weak(expected, 1)) {
32     expected = cas_atomic.load();
33   }
34 }
35 
36 void *process() [[clang::nonblocking]] {
37   void *ptr = MallocViolation(); // Suppressed call-stack-contains
38   VectorViolations();            // Suppressed call-stack-contains with regex
39   BlockFunc();                   // Suppressed function-name-matches
40   free(ptr);                     // Suppressed function-name-matches
41 
42   // This is the one that should abort the program
43   // Everything else is suppressed
44   usleep(1);
45 
46   return ptr;
47 }
48 
49 int main() {
50   process();
51   return 0;
52 }
53 
54 // CHECK-NOT: failed to open suppressions file
55 // CHECK: Intercepted call to real-time unsafe function
56 // CHECK-SAME: usleep
57 
58 // CHECK-NOT: Intercepted call to real-time unsafe function
59 // CHECK-NOT: malloc
60 // CHECK-NOT: vector
61 // CHECK-NOT: free
62 // CHECK-NOT: BlockFunc
63 
64 // CHECK: RealtimeSanitizer exit stats:
65 // CHECK: Suppression count: 7
66 
67 // CHECK-NOSUPPRESSIONS: malloc
68 // CHECK-NOSUPPRESSIONS: vector
69 // CHECK-NOSUPPRESSIONS: free
70 // CHECK-NOSUPPRESSIONS: BlockFunc
71 // CHECK-NOSUPPRESSIONS: usleep
72