llvm-project
151 строка · 4.4 Кб
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
5
6#include <assert.h>
7#include <sanitizer/dfsan_interface.h>
8#include <stdatomic.h>
9
10typedef struct __attribute((packed)) {
11uint8_t val[DATA_BYTES];
12} idata;
13
14void test_idata_load() {
15idata dest = {-1};
16idata init = {0};
17
18dfsan_label i_label = 2;
19dfsan_set_label(i_label, &init, sizeof(init));
20
21__atomic_load(&init, &dest, __ATOMIC_RELAXED);
22
23dfsan_label read_label = dfsan_read_label(&dest, sizeof(dest));
24assert(read_label == i_label);
25#ifdef ORIGIN_TRACKING
26dfsan_origin read_origin =
27dfsan_read_origin_of_first_taint(&dest, sizeof(dest));
28assert(read_origin != 0);
29#endif
30}
31
32void test_idata_store() {
33idata dest = {-1};
34idata init = {0};
35
36dfsan_label i_label = 2;
37dfsan_set_label(i_label, &init, sizeof(init));
38
39__atomic_store(&init, &dest, __ATOMIC_RELAXED);
40
41dfsan_label read_label = dfsan_read_label(&dest, sizeof(dest));
42assert(read_label == i_label);
43#ifdef ORIGIN_TRACKING
44dfsan_origin read_origin =
45dfsan_read_origin_of_first_taint(&dest, sizeof(dest));
46assert(read_origin != 0);
47#endif
48}
49
50void test_idata_exchange() {
51idata target = {-1};
52idata init = {0};
53idata dest = {3};
54
55dfsan_label i_label = 1;
56dfsan_set_label(i_label, &init, sizeof(init));
57dfsan_label j_label = 2;
58dfsan_set_label(j_label, &target, sizeof(target));
59
60dfsan_label dest0_label = dfsan_read_label(&dest, sizeof(dest));
61assert(dest0_label == 0);
62#ifdef ORIGIN_TRACKING
63dfsan_origin dest0_origin =
64dfsan_read_origin_of_first_taint(&dest, sizeof(dest));
65assert(dest0_origin == 0);
66#endif
67
68__atomic_exchange(&target, &init, &dest, __ATOMIC_RELAXED);
69
70dfsan_label dest_label = dfsan_read_label(&dest, sizeof(dest));
71assert(dest_label == j_label);
72#ifdef ORIGIN_TRACKING
73dfsan_origin dest_origin =
74dfsan_read_origin_of_first_taint(&dest, sizeof(dest));
75assert(dest_origin != 0);
76#endif
77
78dfsan_label target_label = dfsan_read_label(&target, sizeof(target));
79assert(target_label == i_label);
80#ifdef ORIGIN_TRACKING
81dfsan_origin target_origin =
82dfsan_read_origin_of_first_taint(&target, sizeof(target));
83assert(target_origin != 0);
84#endif
85}
86
87void test_idata_cmp_exchange_1() {
88idata target = {0};
89idata expected = {0}; // Target matches expected
90idata desired = {3};
91
92dfsan_label i_label = 1;
93dfsan_set_label(i_label, &expected, sizeof(expected));
94dfsan_label j_label = 2;
95dfsan_set_label(j_label, &target, sizeof(target));
96dfsan_label k_label = 4;
97dfsan_set_label(k_label, &desired, sizeof(desired));
98
99int r =
100__atomic_compare_exchange(&target, &expected, &desired, /*weak=false*/ 0,
101__ATOMIC_RELAXED, __ATOMIC_RELAXED);
102// Target matches expected => true
103assert(r);
104
105// Copy desired to target.
106dfsan_label target_label = dfsan_read_label(&target, sizeof(target));
107assert(target_label == k_label);
108#ifdef ORIGIN_TRACKING
109dfsan_origin target_origin =
110dfsan_read_origin_of_first_taint(&target, sizeof(target));
111assert(target_origin != 0);
112#endif
113}
114
115void test_idata_cmp_exchange_2() {
116idata target = {0};
117idata expected = {-1}; // Target does not match expected
118idata desired = {3};
119
120dfsan_label i_label = 1;
121dfsan_set_label(i_label, &expected, sizeof(expected));
122dfsan_label j_label = 2;
123dfsan_set_label(j_label, &target, sizeof(target));
124dfsan_label k_label = 4;
125dfsan_set_label(k_label, &desired, sizeof(desired));
126
127int r =
128__atomic_compare_exchange(&target, &expected, &desired, /*weak=false*/ 0,
129__ATOMIC_RELAXED, __ATOMIC_RELAXED);
130// Target does not match expected => false
131assert(!r);
132
133// Copy target to expected
134dfsan_label expected_label = dfsan_read_label(&expected, sizeof(expected));
135assert(expected_label == j_label);
136#ifdef ORIGIN_TRACKING
137dfsan_origin expected_origin =
138dfsan_read_origin_of_first_taint(&expected, sizeof(expected));
139assert(expected_origin != 0);
140#endif
141}
142
143int main() {
144test_idata_load();
145test_idata_store();
146test_idata_exchange();
147test_idata_cmp_exchange_1();
148test_idata_cmp_exchange_2();
149
150return 0;
151}
152