[docs] Fix build-docs.sh
[llvm-project.git] / compiler-rt / test / dfsan / libatomic.c
blob9a3b926bcc0da40e211045a47d18f6a7430d387a
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
5 //
6 // REQUIRES: x86_64-target-arch
8 #include <assert.h>
9 #include <sanitizer/dfsan_interface.h>
10 #include <stdatomic.h>
12 typedef struct __attribute((packed)) {
13 uint8_t val[DATA_BYTES];
14 } idata;
16 void test_idata_load() {
17 idata dest = {-1};
18 idata init = {0};
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);
31 #endif
34 void test_idata_store() {
35 idata dest = {-1};
36 idata init = {0};
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);
49 #endif
52 void test_idata_exchange() {
53 idata target = {-1};
54 idata init = {0};
55 idata dest = {3};
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);
68 #endif
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);
78 #endif
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);
86 #endif
89 void test_idata_cmp_exchange_1() {
90 idata target = {0};
91 idata expected = {0}; // Target matches expected
92 idata desired = {3};
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));
101 int r =
102 __atomic_compare_exchange(&target, &expected, &desired, /*weak=false*/ 0,
103 __ATOMIC_RELAXED, __ATOMIC_RELAXED);
104 // Target matches expected => true
105 assert(r);
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);
114 #endif
117 void test_idata_cmp_exchange_2() {
118 idata target = {0};
119 idata expected = {-1}; // Target does not match expected
120 idata desired = {3};
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));
129 int r =
130 __atomic_compare_exchange(&target, &expected, &desired, /*weak=false*/ 0,
131 __ATOMIC_RELAXED, __ATOMIC_RELAXED);
132 // Target does not match expected => false
133 assert(!r);
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);
142 #endif
145 int main() {
146 test_idata_load();
147 test_idata_store();
148 test_idata_exchange();
149 test_idata_cmp_exchange_1();
150 test_idata_cmp_exchange_2();
152 return 0;