1 // RUN: %clang_dfsan -g3 -DDATA_BYTES=3 %s -fno-exceptions %libatomic -o %t && %run %t
2 // RUN: %clang_dfsan -g3 -DDATA_BYTES=3 -DORIGIN_TRACKING -mllvm -dfsan-track-origins=1 %s -fno-exceptions %libatomic -o %t && %run %t
3 // RUN: %clang_dfsan -g3 -DDATA_BYTES=32 %s -fno-exceptions %libatomic -o %t && %run %t
4 // RUN: %clang_dfsan -g3 -DDATA_BYTES=32 -DORIGIN_TRACKING -mllvm -dfsan-track-origins=1 %s -fno-exceptions %libatomic -o %t && %run %t
7 #include <sanitizer/dfsan_interface.h>
10 typedef struct __attribute((packed
)) {
11 uint8_t val
[DATA_BYTES
];
14 void test_idata_load() {
18 dfsan_label i_label
= 2;
19 dfsan_set_label(i_label
, &init
, sizeof(init
));
21 __atomic_load(&init
, &dest
, __ATOMIC_RELAXED
);
23 dfsan_label read_label
= dfsan_read_label(&dest
, sizeof(dest
));
24 assert(read_label
== i_label
);
25 #ifdef ORIGIN_TRACKING
26 dfsan_origin read_origin
=
27 dfsan_read_origin_of_first_taint(&dest
, sizeof(dest
));
28 assert(read_origin
!= 0);
32 void test_idata_store() {
36 dfsan_label i_label
= 2;
37 dfsan_set_label(i_label
, &init
, sizeof(init
));
39 __atomic_store(&init
, &dest
, __ATOMIC_RELAXED
);
41 dfsan_label read_label
= dfsan_read_label(&dest
, sizeof(dest
));
42 assert(read_label
== i_label
);
43 #ifdef ORIGIN_TRACKING
44 dfsan_origin read_origin
=
45 dfsan_read_origin_of_first_taint(&dest
, sizeof(dest
));
46 assert(read_origin
!= 0);
50 void test_idata_exchange() {
55 dfsan_label i_label
= 1;
56 dfsan_set_label(i_label
, &init
, sizeof(init
));
57 dfsan_label j_label
= 2;
58 dfsan_set_label(j_label
, &target
, sizeof(target
));
60 dfsan_label dest0_label
= dfsan_read_label(&dest
, sizeof(dest
));
61 assert(dest0_label
== 0);
62 #ifdef ORIGIN_TRACKING
63 dfsan_origin dest0_origin
=
64 dfsan_read_origin_of_first_taint(&dest
, sizeof(dest
));
65 assert(dest0_origin
== 0);
68 __atomic_exchange(&target
, &init
, &dest
, __ATOMIC_RELAXED
);
70 dfsan_label dest_label
= dfsan_read_label(&dest
, sizeof(dest
));
71 assert(dest_label
== j_label
);
72 #ifdef ORIGIN_TRACKING
73 dfsan_origin dest_origin
=
74 dfsan_read_origin_of_first_taint(&dest
, sizeof(dest
));
75 assert(dest_origin
!= 0);
78 dfsan_label target_label
= dfsan_read_label(&target
, sizeof(target
));
79 assert(target_label
== i_label
);
80 #ifdef ORIGIN_TRACKING
81 dfsan_origin target_origin
=
82 dfsan_read_origin_of_first_taint(&target
, sizeof(target
));
83 assert(target_origin
!= 0);
87 void test_idata_cmp_exchange_1() {
89 idata expected
= {0}; // Target matches expected
92 dfsan_label i_label
= 1;
93 dfsan_set_label(i_label
, &expected
, sizeof(expected
));
94 dfsan_label j_label
= 2;
95 dfsan_set_label(j_label
, &target
, sizeof(target
));
96 dfsan_label k_label
= 4;
97 dfsan_set_label(k_label
, &desired
, sizeof(desired
));
100 __atomic_compare_exchange(&target
, &expected
, &desired
, /*weak=false*/ 0,
101 __ATOMIC_RELAXED
, __ATOMIC_RELAXED
);
102 // Target matches expected => true
105 // Copy desired to target.
106 dfsan_label target_label
= dfsan_read_label(&target
, sizeof(target
));
107 assert(target_label
== k_label
);
108 #ifdef ORIGIN_TRACKING
109 dfsan_origin target_origin
=
110 dfsan_read_origin_of_first_taint(&target
, sizeof(target
));
111 assert(target_origin
!= 0);
115 void test_idata_cmp_exchange_2() {
117 idata expected
= {-1}; // Target does not match expected
120 dfsan_label i_label
= 1;
121 dfsan_set_label(i_label
, &expected
, sizeof(expected
));
122 dfsan_label j_label
= 2;
123 dfsan_set_label(j_label
, &target
, sizeof(target
));
124 dfsan_label k_label
= 4;
125 dfsan_set_label(k_label
, &desired
, sizeof(desired
));
128 __atomic_compare_exchange(&target
, &expected
, &desired
, /*weak=false*/ 0,
129 __ATOMIC_RELAXED
, __ATOMIC_RELAXED
);
130 // Target does not match expected => false
133 // Copy target to expected
134 dfsan_label expected_label
= dfsan_read_label(&expected
, sizeof(expected
));
135 assert(expected_label
== j_label
);
136 #ifdef ORIGIN_TRACKING
137 dfsan_origin expected_origin
=
138 dfsan_read_origin_of_first_taint(&expected
, sizeof(expected
));
139 assert(expected_origin
!= 0);
146 test_idata_exchange();
147 test_idata_cmp_exchange_1();
148 test_idata_cmp_exchange_2();