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 // Tests that callbacks are inserted for store events when
6 // -dfsan-event-callbacks is specified.
9 #include <sanitizer/dfsan_interface.h>
14 // Compile this code without DFSan to avoid recursive instrumentation.
16 extern dfsan_label LabelI
;
17 extern dfsan_label LabelJ
;
18 extern dfsan_label LabelIJ
;
19 extern dfsan_label LabelArgv
;
20 extern size_t LenArgv
;
22 void __dfsan_store_callback(dfsan_label Label
) {
29 assert(Label
== LabelI
);
32 assert(Label
== LabelJ
);
35 assert(Label
== LabelIJ
);
41 fprintf(stderr
, "Label %u stored to memory\n", Label
);
44 void __dfsan_load_callback(dfsan_label Label
) {
48 fprintf(stderr
, "Label %u loaded from memory\n", Label
);
51 void __dfsan_mem_transfer_callback(dfsan_label
*Start
, size_t Len
) {
52 assert(Len
== LenArgv
);
53 for (int I
= 0; I
< Len
; ++I
) {
54 assert(Start
[I
] == LabelArgv
);
57 fprintf(stderr
, "Label %u copied to memory\n", Start
[0]);
60 void __dfsan_cmp_callback(dfsan_label CombinedLabel
) {
64 fprintf(stderr
, "Label %u used for branching\n", CombinedLabel
);
68 // Compile this code with DFSan and -dfsan-event-callbacks to insert the
74 dfsan_label LabelArgv
;
78 int main(int Argc
, char *Argv
[]) {
83 dfsan_set_label(LabelI
, &I
, sizeof(I
));
85 dfsan_set_label(LabelJ
, &J
, sizeof(J
));
86 LabelIJ
= dfsan_union(LabelI
, LabelJ
);
88 // CHECK: Label 1 stored to memory
89 volatile int Sink
= I
;
91 // CHECK: Label 1 loaded from memory
92 // CHECK: Label 1 used for branching
95 // CHECK: Label 2 stored to memory
98 // CHECK: Label 2 loaded from memory
99 // CHECK: Label 2 used for branching
102 // CHECK: Label 2 loaded from memory
103 // CHECK: Label 3 stored to memory
106 // CHECK: Label 3 loaded from memory
107 // CHECK: Label 3 used for branching
110 // CHECK: Label 3 used for branching
113 LenArgv
= strlen(Argv
[1]);
115 dfsan_set_label(LabelArgv
, Argv
[1], LenArgv
);
118 assert(LenArgv
< sizeof(Buf
) - 1);
120 // CHECK: Label 4 copied to memory
121 void *volatile SinkPtr
= Buf
;
122 memcpy(SinkPtr
, Argv
[1], LenArgv
);
124 // CHECK: Label 4 copied to memory
126 memmove(SinkPtr
, Buf
, LenArgv
);
131 #endif // #ifdef CALLBACKS