1// RUN: %clang_dfsan %s -O1 -o %t && %run %t
2// RUN: %clang_dfsan %s -O0 -DO0 -o %t && %run %t
3
4#include <assert.h>
5#include <sanitizer/dfsan_interface.h>
6
7typedef struct Pair {
8 int i;
9 char *ptr;
10} Pair;
11
12__attribute__((noinline))
13Pair make_pair(int i, char *ptr) {
14 Pair pair;
15 pair.i = i;
16 pair.ptr = ptr;
17 return pair;
18}
19
20__attribute__((noinline))
21Pair copy_pair1(const Pair *pair0) {
22 Pair pair;
23 pair.i = pair0->i;
24 pair.ptr = pair0->ptr;
25 return pair;
26}
27
28__attribute__((noinline))
29Pair copy_pair2(const Pair pair0) {
30 Pair pair;
31 pair.i = pair0.i;
32 pair.ptr = pair0.ptr;
33 return pair;
34}
35
36int 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(label: i_label, addr: &i, size: sizeof(i));
42 dfsan_set_label(label: ptr_label, addr: &ptr, size: sizeof(ptr));
43
44 Pair pair1 = make_pair(i, ptr);
45 int i1 = pair1.i;
46 char *ptr1 = pair1.ptr;
47
48 dfsan_label i1_label = dfsan_read_label(addr: &i1, size: sizeof(i1));
49 dfsan_label ptr1_label = dfsan_read_label(addr: &ptr1, size: 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
57
58 Pair pair2 = copy_pair1(pair0: &pair1);
59 int i2 = pair2.i;
60 char *ptr2 = pair2.ptr;
61
62 dfsan_label i2_label = dfsan_read_label(addr: &i2, size: sizeof(i2));
63 dfsan_label ptr2_label = dfsan_read_label(addr: &ptr2, size: 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
71
72 Pair pair3 = copy_pair2(pair0: pair1);
73 int i3 = pair3.i;
74 char *ptr3 = pair3.ptr;
75
76 dfsan_label i3_label = dfsan_read_label(addr: &i3, size: sizeof(i3));
77 dfsan_label ptr3_label = dfsan_read_label(addr: &ptr3, size: 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
85
86
87 return 0;
88}
89

source code of compiler-rt/test/dfsan/struct.c