Debian Codegraph @

crt/aws-c-common/tests/atomics_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/common/atomics.h>
7 #include <aws/common/common.h>
8 #include <aws/common/condition_variable.h>
9 #include <aws/common/mutex.h>
10 #include <aws/common/thread.h>
11
12 #include <aws/testing/aws_test_harness.h>
13
14 #ifdef _WIN32
15 # include <malloc.h>
16 # ifndef alloca
17 # define alloca _alloca
18 # endif
19 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
20 # include <stdlib.h>
21 #else
22 # include <alloca.h>
23 #endif
24
25 AWS_TEST_CASE(atomics_semantics, t_semantics)
26 static int t_semantics(struct aws_allocator *allocator, void *ctx) {
27 /*
28 * This test verifies that the atomics work properly on a single thread.
29 * Because we're only accessing from a single thread, program order fully constrains
30 * the order in which all loads and stores can happen, and so our memory order selection
31 * doesn't matter. We do however use a variety of memory orders to ensure that they
32 * are accepted.
33 */
34 (void)ctx;
35 (void)allocator;
36
37 /* These provide us with some unique test pointers */
38 int dummy_1, dummy_2, dummy_3;
39
40 void *expected_ptr;
41 size_t expected_int;
42
43 struct aws_atomic_var var;
44
45 /* First, pointer tests */
46 aws_atomic_init_ptr(&var, &dummy_1);
47 ASSERT_PTR_EQUALS(&dummy_1, aws_atomic_load_ptr_explicit(&var, aws_memory_order_relaxed));
48 ASSERT_PTR_EQUALS(&dummy_1, aws_atomic_exchange_ptr_explicit(&var, &dummy_2, aws_memory_order_acq_rel));
49 ASSERT_PTR_EQUALS(&dummy_2, aws_atomic_load_ptr_explicit(&var, aws_memory_order_acquire));
50 aws_atomic_store_ptr_explicit(&var, &dummy_1, aws_memory_order_release);
51 ASSERT_PTR_EQUALS(&dummy_1, aws_atomic_load_ptr_explicit(&var, aws_memory_order_acquire));
52
53 expected_ptr = &dummy_3;
54 ASSERT_FALSE(aws_atomic_compare_exchange_ptr_explicit(
55 &var, &expected_ptr, &dummy_3, aws_memory_order_release, aws_memory_order_relaxed));
56 ASSERT_PTR_EQUALS(&dummy_1, aws_atomic_load_ptr_explicit(&var, aws_memory_order_acquire));
57 ASSERT_PTR_EQUALS(&dummy_1, expected_ptr);
58
59 ASSERT_TRUE(aws_atomic_compare_exchange_ptr_explicit(
60 &var, &expected_ptr, &dummy_3, aws_memory_order_release, aws_memory_order_relaxed));
61 ASSERT_PTR_EQUALS(&dummy_3, aws_atomic_load_ptr_explicit(&var, aws_memory_order_acquire));
62
63 /* Integer tests */
64 aws_atomic_init_int(&var, 12345);
65 ASSERT_INT_EQUALS(12345, aws_atomic_load_int_explicit(&var, aws_memory_order_relaxed));
66 aws_atomic_store_int_explicit(&var, 54321, aws_memory_order_release);
67 ASSERT_INT_EQUALS(54321, aws_atomic_load_int_explicit(&var, aws_memory_order_acquire));
68 ASSERT_INT_EQUALS(54321, aws_atomic_exchange_int_explicit(&var, 9999, aws_memory_order_acq_rel));
69 ASSERT_INT_EQUALS(9999, aws_atomic_load_int_explicit(&var, aws_memory_order_acquire));
70
71 expected_int = 1111;
72 ASSERT_FALSE(aws_atomic_compare_exchange_int_explicit(
73 &var, &expected_int, 0, aws_memory_order_acq_rel, aws_memory_order_relaxed));
74 ASSERT_INT_EQUALS(9999, aws_atomic_load_int_explicit(&var, aws_memory_order_acquire));
75 ASSERT_INT_EQUALS(9999, expected_int);
76 ASSERT_TRUE(aws_atomic_compare_exchange_int_explicit(
77 &var, &expected_int, 0x7000, aws_memory_order_acq_rel, aws_memory_order_relaxed));
78 ASSERT_INT_EQUALS(0x7000, aws_atomic_load_int_explicit(&var, aws_memory_order_acquire));
79
80 ASSERT_INT_EQUALS(0x7000, aws_atomic_fetch_add_explicit(&var, 6, aws_memory_order_relaxed));
81 ASSERT_INT_EQUALS(0x7006, aws_atomic_fetch_sub_explicit(&var, 0x16, aws_memory_order_relaxed));
82 ASSERT_INT_EQUALS(0x6ff0, aws_atomic_fetch_or_explicit(&var, 0x14, aws_memory_order_relaxed));
83 ASSERT_INT_EQUALS(0x6ff4, aws_atomic_fetch_and_explicit(&var, 0x2115, aws_memory_order_relaxed));
84 ASSERT_INT_EQUALS(0x2114, aws_atomic_fetch_xor_explicit(&var, 0x3356, aws_memory_order_relaxed));
85 ASSERT_INT_EQUALS(0x1242, aws_atomic_load_int_explicit(&var, aws_memory_order_acquire));
86
87 /* Proving that atomic_thread_fence works is hard, for now just demonstrate that it doesn't crash */
88 aws_atomic_thread_fence(aws_memory_order_relaxed);
89 aws_atomic_thread_fence(aws_memory_order_release);
90 aws_atomic_thread_fence(aws_memory_order_acquire);
91 aws_atomic_thread_fence(aws_memory_order_acq_rel);
92
93 return 0;
94 }
95
96 AWS_TEST_CASE(atomics_semantics_implicit, t_semantics_implicit)
97 static int t_semantics_implicit(struct aws_allocator *allocator, void *ctx) {
98 /*
99 * This test verifies that the non-_explicit atomics work properly on a single thread.
100 */
101 (void)ctx;
102 (void)allocator;
103
104 /* These provide us with some unique test pointers */
105 int dummy_1, dummy_2, dummy_3;
106
107 void *expected_ptr;
108 size_t expected_int;
109
110 struct aws_atomic_var var;
111
112 /* First, pointer tests */
113 aws_atomic_init_ptr(&var, &dummy_1);
114 ASSERT_PTR_EQUALS(&dummy_1, aws_atomic_load_ptr(&var));
115 ASSERT_PTR_EQUALS(&dummy_1, aws_atomic_exchange_ptr(&var, &dummy_2));
116 ASSERT_PTR_EQUALS(&dummy_2, aws_atomic_load_ptr(&var));
117 aws_atomic_store_ptr(&var, &dummy_1);
118 ASSERT_PTR_EQUALS(&dummy_1, aws_atomic_load_ptr(&var));
119
120 expected_ptr = &dummy_3;
121 ASSERT_FALSE(aws_atomic_compare_exchange_ptr(&var, &expected_ptr, &dummy_3));
122 ASSERT_PTR_EQUALS(&dummy_1, aws_atomic_load_ptr(&var));
123 ASSERT_PTR_EQUALS(&dummy_1, expected_ptr);
124
125 ASSERT_TRUE(aws_atomic_compare_exchange_ptr(&var, &expected_ptr, &dummy_3));
126 ASSERT_PTR_EQUALS(&dummy_3, aws_atomic_load_ptr(&var));
127
128 /* Integer tests */
129 aws_atomic_init_int(&var, 12345);
130 ASSERT_INT_EQUALS(12345, aws_atomic_load_int(&var));
131 aws_atomic_store_int(&var, 54321);
132 ASSERT_INT_EQUALS(54321, aws_atomic_load_int(&var));
133 ASSERT_INT_EQUALS(54321, aws_atomic_exchange_int(&var, 9999));
134 ASSERT_INT_EQUALS(9999, aws_atomic_load_int(&var));
135
136 expected_int = 1111;
137 ASSERT_FALSE(aws_atomic_compare_exchange_int(&var, &expected_int, 0));
138 ASSERT_INT_EQUALS(9999, aws_atomic_load_int(&var));
139 ASSERT_INT_EQUALS(9999, expected_int);
140 ASSERT_TRUE(aws_atomic_compare_exchange_int(&var, &expected_int, 0x7000));
141 ASSERT_INT_EQUALS(0x7000, aws_atomic_load_int(&var));
142
143 ASSERT_INT_EQUALS(0x7000, aws_atomic_fetch_add(&var, 6));
144 ASSERT_INT_EQUALS(0x7006, aws_atomic_fetch_sub(&var, 0x16));
145 ASSERT_INT_EQUALS(0x6ff0, aws_atomic_fetch_or(&var, 0x14));
146 ASSERT_INT_EQUALS(0x6ff4, aws_atomic_fetch_and(&var, 0x2115));
147 ASSERT_INT_EQUALS(0x2114, aws_atomic_fetch_xor(&var, 0x3356));
148 ASSERT_INT_EQUALS(0x1242, aws_atomic_load_int(&var));
149
150 /* Proving that atomic_thread_fence works is hard, for now just demonstrate that it doesn't crash */
151 aws_atomic_thread_fence(aws_memory_order_relaxed);
152 aws_atomic_thread_fence(aws_memory_order_release);
153 aws_atomic_thread_fence(aws_memory_order_acquire);
154 aws_atomic_thread_fence(aws_memory_order_acq_rel);
155
156 return 0;
157 }
158
159 AWS_TEST_CASE(atomics_static_init, t_static_init)
160 static int t_static_init(struct aws_allocator *allocator, void *ctx) {
161 (void)allocator;
162 (void)ctx;
163
164 /* Verify that we have the right sign extension behavior - we should see zero extension here */
165 struct aws_atomic_var int_init = AWS_ATOMIC_INIT_INT((uint8_t)0x80);
166 struct aws_atomic_var ptr_init = AWS_ATOMIC_INIT_PTR(&int_init);
167
168 ASSERT_INT_EQUALS(0x80, aws_atomic_load_int(&int_init));
169 ASSERT_PTR_EQUALS(&int_init, aws_atomic_load_int(&ptr_init));
170
171 return 0;
172 }
173
174 union padded_var {
175 struct aws_atomic_var var;
176 char pad[32];
177 };
178
179 /*
180 * We define the race loop in a macro to encourage inlining; performance matters when trying to tickle low-level data
181 * races
182 */
183 struct one_race {
184 struct aws_atomic_var *wait;
185 struct aws_atomic_var **vars;
186 struct aws_atomic_var **observations;
187 };
188
189 static struct one_race *races;
190 static size_t n_races, n_vars, n_observations;
191 static int n_participants;
192
193 static int done_racing;
194 static struct aws_mutex done_mutex = AWS_MUTEX_INIT;
195 static struct aws_condition_variable done_cvar = AWS_CONDITION_VARIABLE_INIT;
196
197 static struct aws_atomic_var last_race_index;
198
199 static struct aws_atomic_var *alloc_var(struct aws_allocator *alloc, const struct aws_atomic_var *template) {
200 struct aws_atomic_var *var = aws_mem_acquire(alloc, sizeof(union padded_var));
201 if (!var) {
202 abort();
203 }
204
205 memcpy(var, template, sizeof(*var));
206 return var;
207 }
208
209 static void setup_races(
210 struct aws_allocator *alloc,
211 size_t n_races_v,
212 size_t n_vars_v,
213 size_t n_observations_v,
214 const struct aws_atomic_var *init_vars,
215 const struct aws_atomic_var *init_observations) {
216 struct aws_atomic_var init_wait;
217 aws_atomic_init_int(&init_wait, 0);
218
219 n_races = n_races_v;
220 n_vars = n_vars_v;
221 n_observations = n_observations_v;
222
223 races = aws_mem_acquire(alloc, n_races * sizeof(*races));
224 if (!races) {
225 abort();
226 }
227
228 for (size_t i = 0; i < n_races; i++) {
229 races[i].wait = alloc_var(alloc, &init_wait);
230
231 races[i].vars = aws_mem_acquire(alloc, n_vars * sizeof(*races[i].vars));
232 races[i].observations = aws_mem_acquire(alloc, n_observations * sizeof(*races[i].observations));
233 if (!races[i].vars || !races[i].observations) {
234 abort();
235 }
236
237 for (size_t j = 0; j < n_vars; j++) {
238 races[i].vars[j] = alloc_var(alloc, &init_vars[j]);
239 }
240 for (size_t j = 0; j < n_observations; j++) {
241 races[i].observations[j] = alloc_var(alloc, &init_observations[j]);
242 }
243 }
244 }
245
246 static void free_races(struct aws_allocator *alloc) {
247 for (size_t i = 0; i < n_races; i++) {
248 for (size_t j = 0; j < n_vars; j++) {
249 aws_mem_release(alloc, races[i].vars[j]);
250 }
251 for (size_t j = 0; j < n_observations; j++) {
252 aws_mem_release(alloc, races[i].observations[j]);
253 }
254 aws_mem_release(alloc, races[i].wait);
255 aws_mem_release(alloc, races[i].vars);
256 aws_mem_release(alloc, races[i].observations);
257 }
258 aws_mem_release(alloc, races);
259 }
260
261 static bool are_races_done(void *ignored) {
262 (void)ignored;
263
264 return done_racing >= n_participants;
265 }
266
267 static int run_races(
268 size_t *last_race,
269 struct aws_allocator *alloc,
270 int n_participants_local,
271 void (*race_fn)(void *vp_participant)) {
272 int *participant_indexes = alloca(n_participants_local * sizeof(*participant_indexes));
273 struct aws_thread *threads = alloca(n_participants_local * sizeof(struct aws_thread));
274
275 *last_race = SIZE_MAX;
276 n_participants = n_participants_local;
277 done_racing = false;
278 aws_atomic_init_int(&last_race_index, 0);
279
280 for (int i = 0; i < n_participants; i++) {
281 participant_indexes[i] = i;
282 ASSERT_SUCCESS(aws_thread_init(&threads[i], alloc));
283 ASSERT_SUCCESS(aws_thread_launch(&threads[i], race_fn, &participant_indexes[i], NULL));
284 }
285
286 ASSERT_SUCCESS(aws_mutex_lock(&done_mutex));
287 if (aws_condition_variable_wait_for_pred(&done_cvar, &done_mutex, 1000000000ULL /* 1s */, are_races_done, NULL) ==
288 AWS_OP_ERR) {
289 ASSERT_TRUE(aws_last_error() == AWS_ERROR_COND_VARIABLE_TIMED_OUT);
290 }
291
292 if (done_racing >= n_participants) {
293 *last_race = n_races;
294 } else {
295 *last_race = (size_t)aws_atomic_load_int_explicit(&last_race_index, aws_memory_order_relaxed);
296 if (*last_race == SIZE_MAX) {
297 /* We didn't even see the first race complete */
298 *last_race = 0;
299 }
300 }
301 ASSERT_SUCCESS(aws_mutex_unlock(&done_mutex));
302
303 /* Poison all remaining races to make sure the threads exit quickly */
304 for (size_t i = 0; i < n_races; i++) {
305 aws_atomic_store_int_explicit(races[i].wait, n_participants, aws_memory_order_relaxed);
306 }
307
308 for (int i = 0; i < n_participants; i++) {
309 ASSERT_SUCCESS(aws_thread_join(&threads[i]));
310 aws_thread_clean_up(&threads[i]);
311 }
312
313 aws_atomic_thread_fence(aws_memory_order_acq_rel);
314
315 return 0;
316 }
317
318 static void notify_race_completed(void) {
319 if (aws_mutex_lock(&done_mutex)) {
320 abort();
321 }
322
323 done_racing++;
324 if (done_racing >= n_participants) {
325 if (aws_condition_variable_notify_all(&done_cvar)) {
326 abort();
327 }
328 }
329
330 if (aws_mutex_unlock(&done_mutex)) {
331 abort();
332 }
333 }
334
335 #define DEFINE_RACE(race_name, vn_participant, vn_race) \
336 static void race_name##_iter(int participant, struct one_race *race); \
337 static void race_name(void *vp_participant) { \
338 int participant = *(int *)vp_participant; \
339 size_t n_races_local = n_races; \
340 size_t n_participants_local = n_participants; \
341 for (size_t i = 0; i < n_races_local; i++) { \
342 while (i > 0 && \
343 aws_atomic_load_int_explicit(races[i - 1].wait, aws_memory_order_relaxed) < n_participants_local) { \
344 /* spin */ \
345 } \
346 if (participant == 0) { \
347 aws_atomic_store_int_explicit(&last_race_index, i - 1, aws_memory_order_relaxed); \
348 } \
349 race_name##_iter(participant, &races[i]); \
350 aws_atomic_fetch_add_explicit(races[i].wait, 1, aws_memory_order_relaxed); \
351 } \
352 notify_race_completed(); \
353 aws_atomic_thread_fence(aws_memory_order_release); \
354 } \
355 static void race_name##_iter(int vn_participant, struct one_race *vn_race) /* NOLINT */
356
357 /*
358 * The following race races these two threads:
359 *
360 * Thread 1:
361 * DATA <- 1 [relaxed]
362 * FLAG <- 2 [release]
363 * Thread 2:
364 * Read FLAG [acquire]
365 * Read DATA [relaxed]
366 *
367 * We expect that, if FLAG is observed to be 2, then DATA must be 1, due to
368 * acquire-release ordering.
369 *
370 * Note however, that this race never fails on x86; on x86 all loads have acquire semantics,
371 * and all stores have release semantics.
372 */
373 DEFINE_RACE(acquire_to_release_one_direction, participant, race) {
374 struct aws_atomic_var *flag = race->vars[0];
375 struct aws_atomic_var *protected_data = race->vars[1];
376 struct aws_atomic_var *observation = race->observations[0];
377
378 if (participant == 0) {
379 aws_atomic_store_int_explicit(protected_data, 1, aws_memory_order_relaxed);
380 aws_atomic_store_int_explicit(flag, 2, aws_memory_order_release);
381 } else {
382 size_t flagval = aws_atomic_load_int_explicit(flag, aws_memory_order_acquire);
383 size_t dataval = aws_atomic_load_int_explicit(protected_data, aws_memory_order_relaxed);
384 aws_atomic_store_int_explicit(observation, flagval ^ dataval, aws_memory_order_relaxed);
385 }
386 }
387
388 AWS_TEST_CASE(atomics_acquire_to_release_one_direction, t_acquire_to_release_one_direction)
389 static int t_acquire_to_release_one_direction(struct aws_allocator *allocator, void *ctx) {
390 (void)ctx;
391
392 struct aws_atomic_var template[2];
393 size_t last_race;
394 aws_atomic_init_int(&template[0], 0);
395 aws_atomic_init_int(&template[1], 0);
396
397 setup_races(allocator, 100000, 2, 1, template, template);
398 run_races(&last_race, allocator, 2, acquire_to_release_one_direction);
399
400 for (size_t i = 0; i < last_race; i++) {
401 size_t a = aws_atomic_load_int_explicit(races[i].observations[0], aws_memory_order_relaxed);
402
403 /*
404 * If we see that flag == 2, then the data observation must be 1.
405 * If flag == 0, then the data value may be anything.
406 */
407 ASSERT_FALSE(a == 2, "Acquire-release ordering failed at iteration %zu", i);
408 }
409
410 free_races(allocator);
411
412 return 0;
413 }
414
415 /*
416 * The following race races these two threads:
417 *
418 * Thread 1:
419 * Read DATA [relaxed] (observation 0)
420 * FLAG <- 1 [release]
421 * Thread 2:
422 * Read FLAG [acquire] (observation 1)
423 * DATA <- 1 [relaxed]
424 *
425 * We expect that, if FLAG is observed to be 1, then DATA must be 0, due to
426 * acquire-release ordering.
427 *
428 * Note however, that this race never fails on x86; on x86 all loads have acquire semantics,
429 * and all stores have release semantics.
430 */
431 DEFINE_RACE(acquire_to_release_mixed, participant, race) {
432 struct aws_atomic_var *flag = race->vars[0];
433 struct aws_atomic_var *protected_data = race->vars[1];
434
435 if (participant == 0) {
436 aws_atomic_store_int_explicit(
437 race->observations[0],
438 aws_atomic_load_int_explicit(protected_data, aws_memory_order_relaxed),
439 aws_memory_order_relaxed);
440 aws_atomic_store_int_explicit(flag, 2, aws_memory_order_release);
441 } else {
442 aws_atomic_store_int_explicit(
443 race->observations[1],
444 aws_atomic_load_int_explicit(flag, aws_memory_order_acquire),
445 aws_memory_order_relaxed);
446 aws_atomic_store_int_explicit(protected_data, 1, aws_memory_order_relaxed);
447 }
448 }
449
450 AWS_TEST_CASE(atomics_acquire_to_release_mixed, t_acquire_to_release_mixed)
451 static int t_acquire_to_release_mixed(struct aws_allocator *allocator, void *ctx) {
452 (void)ctx;
453
454 struct aws_atomic_var template[2];
455 size_t last_race;
456 aws_atomic_init_int(&template[0], 0);
457 aws_atomic_init_int(&template[1], 0);
458
459 setup_races(allocator, 100000, 2, 2, template, template);
460 run_races(&last_race, allocator, 2, acquire_to_release_mixed);
461
462 for (size_t i = 0; i < last_race; i++) {
463 size_t data_observation = aws_atomic_load_int_explicit(races[i].observations[0], aws_memory_order_relaxed);
464 size_t flag_observation = aws_atomic_load_int_explicit(races[i].observations[0], aws_memory_order_relaxed);
465
466 /*
467 * If we see that flag == 2, then the data observation must be 1.
468 * If flag == 0, then the data value may be anything.
469 */
470 ASSERT_FALSE(flag_observation && !data_observation, "Acquire-release ordering failed at iteration %zu", i);
471 }
472
473 free_races(allocator);
474
475 return 0;
476 }