1 // RUN: %clang_dfsan -DUSE_SIGNAL_ACTION %s -o %t && %run %t
2 // RUN: %clang_dfsan %s -o %t && %run %t
3 //
4 // REQUIRES: x86_64-target-arch
5 
6 #include <sanitizer/dfsan_interface.h>
7 
8 #include <assert.h>
9 #include <signal.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <unistd.h>
13 
14 volatile int x;
15 volatile int z = 1;
16 
SignalHandler(int signo)17 void SignalHandler(int signo) {
18   assert(dfsan_get_label(signo) == 0);
19   x = z;
20 }
21 
SignalAction(int signo,siginfo_t * si,void * uc)22 void SignalAction(int signo, siginfo_t *si, void *uc) {
23   assert(dfsan_get_label(signo) == 0);
24   assert(dfsan_get_label(si) == 0);
25   assert(dfsan_get_label(uc) == 0);
26   assert(0 == dfsan_read_label(si, sizeof(*si)));
27   assert(0 == dfsan_read_label(uc, sizeof(ucontext_t)));
28   x = z;
29 }
30 
main(int argc,char * argv[])31 int main(int argc, char *argv[]) {
32   dfsan_set_label(8, (void *)&z, sizeof(z));
33 
34   struct sigaction sa = {};
35 #ifdef USE_SIGNAL_ACTION
36   sa.sa_flags = SA_SIGINFO;
37   sa.sa_sigaction = SignalAction;
38 #else
39   sa.sa_handler = SignalHandler;
40 #endif
41   int r = sigaction(SIGHUP, &sa, NULL);
42   assert(dfsan_get_label(r) == 0);
43 
44   kill(getpid(), SIGHUP);
45   signal(SIGHUP, SIG_DFL);
46 
47   assert(x == 1);
48 
49   return 0;
50 }
51