xref: /llvm-project/compiler-rt/test/msan/poison_in_signal.cpp (revision eb4d22917eb69ba0f41e6e677d64f67f8b426496)
1 // Stress test of poisoning from signal handler.
2 
3 // RUN: %clangxx_msan -std=c++11 -O2 %s -o %t && %run %t
4 // RUN: %clangxx_msan -fsanitize-memory-track-origins=2 -std=c++11 -O2 %s -o %t && %run %t
5 // RUN: %clangxx_msan -fsanitize-memory-track-origins=2 -fsanitize-memory-use-after-dtor -std=c++11 -O2 %s -o %t && %run %t
6 
7 #include <assert.h>
8 #include <atomic>
9 #include <pthread.h>
10 #include <signal.h>
11 #include <sys/time.h>
12 
13 #include <sanitizer/msan_interface.h>
14 
15 std::atomic<int> n = {1000};
16 
17 struct Tmp {
18   char buff[1];
~TmpTmp19   ~Tmp() {}
20 };
21 
Poison()22 __attribute__((noinline, optnone)) void Poison() {
23   // use-after-dtor.
24   volatile Tmp t;
25   // Regular poisoning.
26   __msan_poison(&t, sizeof(t));
27 }
28 
thr(void * p)29 void *thr(void *p) {
30   for (; n >= 0;) {
31     for (int i = 0; i < 1000; i++) {
32       Poison();
33     }
34   }
35   return 0;
36 }
37 
handler(int)38 void handler(int) {
39   Poison();
40   --n;
41 }
42 
main(int argc,char ** argv)43 int main(int argc, char **argv) {
44   const int kThreads = 10;
45   pthread_t th[kThreads];
46   for (int i = 0; i < kThreads; i++)
47     pthread_create(&th[i], 0, thr, 0);
48 
49   struct sigaction sa = {};
50   sa.sa_handler = handler;
51   assert(!sigaction(SIGPROF, &sa, 0));
52 
53   itimerval t;
54   t.it_value.tv_sec = 0;
55   t.it_value.tv_usec = 10;
56   t.it_interval = t.it_value;
57   assert(!setitimer(ITIMER_PROF, &t, 0));
58 
59   for (int i = 0; i < kThreads; i++)
60     pthread_join(th[i], 0);
61 
62   return 0;
63 }
64