Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / dfsan / struct.c
blob7ba0016f7beee4cc2a2eda634550f321da8280cf
1 // RUN: %clang_dfsan %s -O1 -o %t && %run %t
2 // RUN: %clang_dfsan %s -O0 -DO0 -o %t && %run %t
4 #include <assert.h>
5 #include <sanitizer/dfsan_interface.h>
7 typedef struct Pair {
8 int i;
9 char *ptr;
10 } Pair;
12 __attribute__((noinline))
13 Pair make_pair(int i, char *ptr) {
14 Pair pair;
15 pair.i = i;
16 pair.ptr = ptr;
17 return pair;
20 __attribute__((noinline))
21 Pair copy_pair1(const Pair *pair0) {
22 Pair pair;
23 pair.i = pair0->i;
24 pair.ptr = pair0->ptr;
25 return pair;
28 __attribute__((noinline))
29 Pair copy_pair2(const Pair pair0) {
30 Pair pair;
31 pair.i = pair0.i;
32 pair.ptr = pair0.ptr;
33 return pair;
36 int main(void) {
37 int i = 1;
38 char *ptr = NULL;
39 dfsan_label i_label = 1;
40 dfsan_label ptr_label = 2;
41 dfsan_set_label(i_label, &i, sizeof(i));
42 dfsan_set_label(ptr_label, &ptr, sizeof(ptr));
44 Pair pair1 = make_pair(i, ptr);
45 int i1 = pair1.i;
46 char *ptr1 = pair1.ptr;
48 dfsan_label i1_label = dfsan_read_label(&i1, sizeof(i1));
49 dfsan_label ptr1_label = dfsan_read_label(&ptr1, sizeof(ptr1));
50 #if defined(O0)
51 assert(i1_label == (i_label | ptr_label));
52 assert(ptr1_label == (i_label | ptr_label));
53 #else
54 assert(i1_label == i_label);
55 assert(ptr1_label == ptr_label);
56 #endif
58 Pair pair2 = copy_pair1(&pair1);
59 int i2 = pair2.i;
60 char *ptr2 = pair2.ptr;
62 dfsan_label i2_label = dfsan_read_label(&i2, sizeof(i2));
63 dfsan_label ptr2_label = dfsan_read_label(&ptr2, sizeof(ptr2));
64 #if defined(O0)
65 assert(i2_label == (i_label | ptr_label));
66 assert(ptr2_label == (i_label | ptr_label));
67 #else
68 assert(i2_label == i_label);
69 assert(ptr2_label == ptr_label);
70 #endif
72 Pair pair3 = copy_pair2(pair1);
73 int i3 = pair3.i;
74 char *ptr3 = pair3.ptr;
76 dfsan_label i3_label = dfsan_read_label(&i3, sizeof(i3));
77 dfsan_label ptr3_label = dfsan_read_label(&ptr3, sizeof(ptr3));
78 #if defined(O0)
79 assert(i3_label == (i_label | ptr_label));
80 assert(ptr3_label == (i_label | ptr_label));
81 #else
82 assert(i3_label == i_label);
83 assert(ptr3_label == ptr_label);
84 #endif
87 return 0;