Prepare for 1.1.1-pre9 release
[openssl.git] / include / internal / tsan_assist.h
1 /*
2  * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9  
10 /*
11  * Goal here is to facilitate writing "thread-opportunistic" code that
12  * withstands Thread Sanitizer's scrutiny. "Thread-opportunistic" is when
13  * exact result is not required, e.g. some statistics, or execution flow
14  * doesn't have to be unambiguous. Simplest example is lazy "constant"
15  * initialization when one can synchronize on variable itself, e.g.
16  *
17  * if (var == NOT_YET_INITIALIZED)
18  *     var = function_returning_same_value();
19  *
20  * This does work provided that loads and stores are single-instuction
21  * operations (and integer ones are on *all* supported platforms), but
22  * it upsets Thread Sanitizer. Suggested solution is
23  *
24  * if (tsan_load(&var) == NOT_YET_INITIALIZED)
25  *     tsan_store(&var, function_returning_same_value());
26  *
27  * Production machine code would be the same, so one can wonder why
28  * bother. Having Thread Sanitizer accept "thread-opportunistic" code
29  * allows to move on trouble-shooting real bugs.
30  *
31  * We utilize the fact that compilers that implement Thread Sanitizer
32  * implement even atomic operations. Then it's assumed that
33  * ATOMIC_{LONG|INT}_LOCK_FREE are assigned same value as
34  * ATOMIC_POINTER_LOCK_FREE. And check for >= 2 ensures that correspodning
35  * code is inlined. It should be noted that statistics counters become
36  * accurate in such case.
37  */
38
39 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
40     && !defined(__STDC_NO_ATOMICS__)
41 # include <stdatomic.h>
42
43 # if defined(ATOMIC_POINTER_LOCK_FREE) \
44           && ATOMIC_POINTER_LOCK_FREE >= 2
45 #  define TSAN_QUALIFIER _Atomic
46 #  define tsan_load(ptr) atomic_load_explicit((ptr), memory_order_relaxed)
47 #  define tsan_store(ptr, val) atomic_store_explicit((ptr), (val), memory_order_relaxed)
48 #  define tsan_counter(ptr) atomic_fetch_add_explicit((ptr), 1, memory_order_relaxed)
49 # endif
50
51 #elif defined(__GNUC__) && defined(__ATOMIC_RELAXED)
52
53 # if defined(__GCC_ATOMIC_POINTER_LOCK_FREE) \
54           && __GCC_ATOMIC_POINTER_LOCK_FREE >= 2
55 #  define TSAN_QUALIFIER volatile
56 #  define tsan_load(ptr) __atomic_load_n((ptr), __ATOMIC_RELAXED)
57 #  define tsan_store(ptr, val) __atomic_store_n((ptr), (val), __ATOMIC_RELAXED)
58 #  define tsan_counter(ptr) __atomic_fetch_add((ptr), 1, __ATOMIC_RELAXED)
59 # endif
60
61 #elif defined(_MSC_VER) && _MSC_VER>=1200
62
63 # define TSAN_QUALIFIER volatile
64 # define tsan_load(ptr) (*(ptr))
65 # define tsan_store(ptr, val) (*(ptr) = (val))
66 # pragma intrinsic(_InterlockedExchangeAdd)
67 # ifdef _WIN64
68 #  pragma intrinsic(_InterlockedExchangeAdd64)
69 #  define tsan_counter(ptr) (sizeof(*ptr) == 8 ? _InterlockedExchangeAdd64((ptr), 1) \
70                                                : _InterlockedExchangeAdd((ptr), 1))
71 # else
72 #  define tsan_counter(ptr) _InterlockedExchangeAdd((ptr), 1)
73 # endif
74
75 #endif
76
77 #ifndef TSAN_QUALIFIER
78
79 # define TSAN_QUALIFIER volatile
80 # define tsan_load(ptr) (*(ptr))
81 # define tsan_store(ptr, val) (*(ptr) = (val))
82 # define tsan_counter(ptr) ((*(ptr))++)
83
84 #endif