1 // RUN: %clang_dfsan -fno-sanitize=dataflow -O2 -fPIE -DCALLBACKS -c %s -o %t-callbacks.o
2 // RUN: %clang_dfsan -O2 -mllvm -dfsan-event-callbacks %s %t-callbacks.o -o %t
3 // RUN: %run %t FooBarBaz 2>&1 | FileCheck %s
5 // REQUIRES: x86_64-target-arch
7 // Tests that callbacks are inserted for store events when
8 // -dfsan-event-callbacks is specified.
11 #include <sanitizer/dfsan_interface.h>
16 // Compile this code without DFSan to avoid recursive instrumentation.
18 extern dfsan_label LabelI
;
19 extern dfsan_label LabelJ
;
20 extern dfsan_label LabelIJ
;
21 extern dfsan_label LabelArgv
;
22 extern size_t LenArgv
;
24 void __dfsan_store_callback(dfsan_label Label
) {
31 assert(Label
== LabelI
);
34 assert(Label
== LabelJ
);
37 assert(Label
== LabelIJ
);
43 fprintf(stderr
, "Label %u stored to memory\n", Label
);
46 void __dfsan_load_callback(dfsan_label Label
) {
50 fprintf(stderr
, "Label %u loaded from memory\n", Label
);
53 void __dfsan_mem_transfer_callback(dfsan_label
*Start
, size_t Len
) {
54 assert(Len
== LenArgv
);
55 for (int I
= 0; I
< Len
; ++I
) {
56 assert(Start
[I
] == LabelArgv
);
59 fprintf(stderr
, "Label %u copied to memory\n", Start
[0]);
62 void __dfsan_cmp_callback(dfsan_label CombinedLabel
) {
66 fprintf(stderr
, "Label %u used for branching\n", CombinedLabel
);
70 // Compile this code with DFSan and -dfsan-event-callbacks to insert the
76 dfsan_label LabelArgv
;
80 int main(int Argc
, char *Argv
[]) {
85 dfsan_set_label(LabelI
, &I
, sizeof(I
));
87 dfsan_set_label(LabelJ
, &J
, sizeof(J
));
88 LabelIJ
= dfsan_union(LabelI
, LabelJ
);
90 // CHECK: Label 1 stored to memory
91 volatile int Sink
= I
;
93 // CHECK: Label 1 loaded from memory
94 // CHECK: Label 1 used for branching
97 // CHECK: Label 2 stored to memory
100 // CHECK: Label 2 loaded from memory
101 // CHECK: Label 2 used for branching
104 // CHECK: Label 2 loaded from memory
105 // CHECK: Label 3 stored to memory
108 // CHECK: Label 3 loaded from memory
109 // CHECK: Label 3 used for branching
112 // CHECK: Label 3 used for branching
115 LenArgv
= strlen(Argv
[1]);
117 dfsan_set_label(LabelArgv
, Argv
[1], LenArgv
);
120 assert(LenArgv
< sizeof(Buf
) - 1);
122 // CHECK: Label 4 copied to memory
123 void *volatile SinkPtr
= Buf
;
124 memcpy(SinkPtr
, Argv
[1], LenArgv
);
126 // CHECK: Label 4 copied to memory
128 memmove(SinkPtr
, Buf
, LenArgv
);
133 #endif // #ifdef CALLBACKS