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 -mllvm -dfsan-fast-16-labels=true %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 -mllvm -dfsan-fast-16-labels=true %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 -mllvm -dfsan-fast-16-labels=true %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 -mllvm -dfsan-fast-16-labels=true %s -o %t && \
19// RUN: %run %t >%t.out 2>&1
20// RUN: FileCheck %s < %t.out
21//
22// REQUIRES: x86_64-target-arch
23
24#include <sanitizer/dfsan_interface.h>
25
26#include <assert.h>
27#include <signal.h>
28#include <string.h>
29#include <sys/types.h>
30#include <unistd.h>
31
32int x, y, u;
33
34void CopyXtoYtoU() {
35 y = x;
36 memcpy(&u, &y, sizeof(int));
37}
38
39void SignalHandler(int signo) {
40 CopyXtoYtoU();
41}
42
43void SignalAction(int signo, siginfo_t *si, void *uc) {
44 CopyXtoYtoU();
45}
46
47int main(int argc, char *argv[]) {
48 int z = 1;
49 dfsan_set_label(8, &z, sizeof(z));
50 x = z;
51
52 struct sigaction psa = {};
53#ifdef USE_SIGNAL_ACTION
54 psa.sa_flags = SA_SIGINFO;
55 psa.sa_sigaction = SignalAction;
56#else
57 psa.sa_flags = 0;
58 psa.sa_handler = SignalHandler;
59#endif
60 sigaction(SIGHUP, &psa, NULL);
61 kill(getpid(), SIGHUP);
62 signal(SIGHUP, SIG_DFL);
63
64 assert(x == 1);
65 assert(y == 1);
66 assert(u == 1);
67
68 dfsan_print_origin_trace(&u, NULL);
69 return 0;
70}
71
72// CHECK: Taint value 0x8 {{.*}} origin tracking ()
73// CHECK: Origin value: {{.*}}, Taint value was stored to memory at
74// CHECK-NOT: {{.*}} in dfs$CopyXtoYtoU {{.*}}origin_with_sigactions.c{{.*}}
75
76// CHECK: #0 {{.*}} in main {{.*}}origin_with_sigactions.c:[[@LINE-26]]
77
78// CHECK: Origin value: {{.*}}, Taint value was created at
79// CHECK: #0 {{.*}} in main {{.*}}origin_with_sigactions.c:[[@LINE-30]]
80