xref: /llvm-project/compiler-rt/test/dfsan/origin_with_sigactions.c (revision 975327a609e55ad9c53bfeee63443128ce20006c)
1 // Check that stores in signal handlers are not recorded in origin history.
2 //
3 // Origin tracking uses ChainedOriginDepot that is not async signal safe, so we
4 // do not track origins inside signal handlers.
5 //
6 // RUN: %clang_dfsan -gmlt -DUSE_SIGNAL_ACTION -mllvm -dfsan-track-origins=1 %s -o %t && \
7 // RUN:      %run %t >%t.out 2>&1
8 // RUN: FileCheck %s < %t.out
9 //
10 // RUN: %clang_dfsan -gmlt -DUSE_SIGNAL_ACTION -mllvm -dfsan-instrument-with-call-threshold=0 -mllvm -dfsan-track-origins=1 %s -o %t && \
11 // RUN:     %run %t >%t.out 2>&1
12 // RUN: FileCheck %s < %t.out
13 //
14 // RUN: %clang_dfsan -gmlt -mllvm -dfsan-track-origins=1 %s -o %t && \
15 // RUN:     %run %t >%t.out 2>&1
16 // RUN: FileCheck %s < %t.out
17 //
18 // RUN: %clang_dfsan -gmlt -mllvm -dfsan-instrument-with-call-threshold=0 -mllvm -dfsan-track-origins=1 %s -o %t && \
19 // RUN:     %run %t >%t.out 2>&1
20 // RUN: FileCheck %s < %t.out
21 
22 #include <sanitizer/dfsan_interface.h>
23 
24 #include <assert.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 
30 int x, y, u;
31 
CopyXtoYtoU()32 void CopyXtoYtoU() {
33   y = x;
34   memcpy(&u, &y, sizeof(int));
35 }
36 
SignalHandler(int signo)37 void SignalHandler(int signo) {
38   CopyXtoYtoU();
39 }
40 
SignalAction(int signo,siginfo_t * si,void * uc)41 void SignalAction(int signo, siginfo_t *si, void *uc) {
42   CopyXtoYtoU();
43 }
44 
main(int argc,char * argv[])45 int main(int argc, char *argv[]) {
46   int z = 1;
47   dfsan_set_label(8, &z, sizeof(z));
48   x = z;
49 
50   struct sigaction psa = {};
51 #ifdef USE_SIGNAL_ACTION
52   psa.sa_flags = SA_SIGINFO;
53   psa.sa_sigaction = SignalAction;
54 #else
55   psa.sa_flags = 0;
56   psa.sa_handler = SignalHandler;
57 #endif
58   sigaction(SIGHUP, &psa, NULL);
59   kill(getpid(), SIGHUP);
60   signal(SIGHUP, SIG_DFL);
61 
62   assert(x == 1);
63   assert(y == 1);
64   assert(u == 1);
65 
66   dfsan_print_origin_trace(&u, NULL);
67   return 0;
68 }
69 
70 // CHECK: Taint value 0x8 {{.*}} origin tracking ()
71 // CHECK: Origin value: {{.*}}, Taint value was stored to memory at
72 // CHECK-NOT: {{.*}} in CopyXtoYtoU.dfsan {{.*}}origin_with_sigactions.c{{.*}}
73 
74 // CHECK: #0 {{.*}} in main {{.*}}origin_with_sigactions.c:[[@LINE-26]]
75 
76 // CHECK: Origin value: {{.*}}, Taint value was created at
77 // CHECK: #0 {{.*}} in main {{.*}}origin_with_sigactions.c:[[@LINE-30]]
78