64 lines · cpp
1// Stress test of poisoning from signal handler.2 3// RUN: %clangxx_msan -std=c++11 -O2 %s -o %t && %run %t4// RUN: %clangxx_msan -fsanitize-memory-track-origins=2 -std=c++11 -O2 %s -o %t && %run %t5// RUN: %clangxx_msan -fsanitize-memory-track-origins=2 -fsanitize-memory-use-after-dtor -std=c++11 -O2 %s -o %t && %run %t6 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 15std::atomic<int> n = {1000};16 17struct Tmp {18 char buff[1];19 ~Tmp() {}20};21 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 29void *thr(void *p) {30 for (; n >= 0;) {31 for (int i = 0; i < 1000; i++) {32 Poison();33 }34 }35 return 0;36}37 38void handler(int) {39 Poison();40 --n;41}42 43int 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