1 // RUN: %clang_dfsan -g3 -DDATA_BYTES=3 %s -fno-exceptions -latomic -o %t && %run %t
2 // RUN: %clang_dfsan -g3 -DDATA_BYTES=3 -DORIGIN_TRACKING -mllvm -dfsan-track-origins=1 %s -fno-exceptions -latomic -o %t && %run %t
3 // RUN: %clang_dfsan -g3 -DDATA_BYTES=32 %s -fno-exceptions -latomic -o %t && %run %t
4 // RUN: %clang_dfsan -g3 -DDATA_BYTES=32 -DORIGIN_TRACKING -mllvm -dfsan-track-origins=1 %s -fno-exceptions -latomic -o %t && %run %t
6 // REQUIRES: x86_64-target-arch
9 #include <sanitizer/dfsan_interface.h>
10 #include <stdatomic.h>
12 typedef struct __attribute((packed
)) {
13 uint8_t val
[DATA_BYTES
];
16 void test_idata_load() {
20 dfsan_label i_label
= 2;
21 dfsan_set_label(i_label
, &init
, sizeof(init
));
23 __atomic_load(&init
, &dest
, __ATOMIC_RELAXED
);
25 dfsan_label read_label
= dfsan_read_label(&dest
, sizeof(dest
));
26 assert(read_label
== i_label
);
27 #ifdef ORIGIN_TRACKING
28 dfsan_origin read_origin
=
29 dfsan_read_origin_of_first_taint(&dest
, sizeof(dest
));
30 assert(read_origin
!= 0);
34 void test_idata_store() {
38 dfsan_label i_label
= 2;
39 dfsan_set_label(i_label
, &init
, sizeof(init
));
41 __atomic_store(&init
, &dest
, __ATOMIC_RELAXED
);
43 dfsan_label read_label
= dfsan_read_label(&dest
, sizeof(dest
));
44 assert(read_label
== i_label
);
45 #ifdef ORIGIN_TRACKING
46 dfsan_origin read_origin
=
47 dfsan_read_origin_of_first_taint(&dest
, sizeof(dest
));
48 assert(read_origin
!= 0);
52 void test_idata_exchange() {
57 dfsan_label i_label
= 1;
58 dfsan_set_label(i_label
, &init
, sizeof(init
));
59 dfsan_label j_label
= 2;
60 dfsan_set_label(j_label
, &target
, sizeof(target
));
62 dfsan_label dest0_label
= dfsan_read_label(&dest
, sizeof(dest
));
63 assert(dest0_label
== 0);
64 #ifdef ORIGIN_TRACKING
65 dfsan_origin dest0_origin
=
66 dfsan_read_origin_of_first_taint(&dest
, sizeof(dest
));
67 assert(dest0_origin
== 0);
70 __atomic_exchange(&target
, &init
, &dest
, __ATOMIC_RELAXED
);
72 dfsan_label dest_label
= dfsan_read_label(&dest
, sizeof(dest
));
73 assert(dest_label
== j_label
);
74 #ifdef ORIGIN_TRACKING
75 dfsan_origin dest_origin
=
76 dfsan_read_origin_of_first_taint(&dest
, sizeof(dest
));
77 assert(dest_origin
!= 0);
80 dfsan_label target_label
= dfsan_read_label(&target
, sizeof(target
));
81 assert(target_label
== i_label
);
82 #ifdef ORIGIN_TRACKING
83 dfsan_origin target_origin
=
84 dfsan_read_origin_of_first_taint(&target
, sizeof(target
));
85 assert(target_origin
!= 0);
89 void test_idata_cmp_exchange_1() {
91 idata expected
= {0}; // Target matches expected
94 dfsan_label i_label
= 1;
95 dfsan_set_label(i_label
, &expected
, sizeof(expected
));
96 dfsan_label j_label
= 2;
97 dfsan_set_label(j_label
, &target
, sizeof(target
));
98 dfsan_label k_label
= 4;
99 dfsan_set_label(k_label
, &desired
, sizeof(desired
));
102 __atomic_compare_exchange(&target
, &expected
, &desired
, /*weak=false*/ 0,
103 __ATOMIC_RELAXED
, __ATOMIC_RELAXED
);
104 // Target matches expected => true
107 // Copy desired to target.
108 dfsan_label target_label
= dfsan_read_label(&target
, sizeof(target
));
109 assert(target_label
== k_label
);
110 #ifdef ORIGIN_TRACKING
111 dfsan_origin target_origin
=
112 dfsan_read_origin_of_first_taint(&target
, sizeof(target
));
113 assert(target_origin
!= 0);
117 void test_idata_cmp_exchange_2() {
119 idata expected
= {-1}; // Target does not match expected
122 dfsan_label i_label
= 1;
123 dfsan_set_label(i_label
, &expected
, sizeof(expected
));
124 dfsan_label j_label
= 2;
125 dfsan_set_label(j_label
, &target
, sizeof(target
));
126 dfsan_label k_label
= 4;
127 dfsan_set_label(k_label
, &desired
, sizeof(desired
));
130 __atomic_compare_exchange(&target
, &expected
, &desired
, /*weak=false*/ 0,
131 __ATOMIC_RELAXED
, __ATOMIC_RELAXED
);
132 // Target does not match expected => false
135 // Copy target to expected
136 dfsan_label expected_label
= dfsan_read_label(&expected
, sizeof(expected
));
137 assert(expected_label
== j_label
);
138 #ifdef ORIGIN_TRACKING
139 dfsan_origin expected_origin
=
140 dfsan_read_origin_of_first_taint(&expected
, sizeof(expected
));
141 assert(expected_origin
!= 0);
148 test_idata_exchange();
149 test_idata_cmp_exchange_1();
150 test_idata_cmp_exchange_2();