1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2018-2022 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/*
11e1051a39Sopenharmony_ci * Contemporary compilers implement lock-free atomic memory access
12e1051a39Sopenharmony_ci * primitives that facilitate writing "thread-opportunistic" or even real
13e1051a39Sopenharmony_ci * multi-threading low-overhead code. "Thread-opportunistic" is when
14e1051a39Sopenharmony_ci * exact result is not required, e.g. some statistics, or execution flow
15e1051a39Sopenharmony_ci * doesn't have to be unambiguous. Simplest example is lazy "constant"
16e1051a39Sopenharmony_ci * initialization when one can synchronize on variable itself, e.g.
17e1051a39Sopenharmony_ci *
18e1051a39Sopenharmony_ci * if (var == NOT_YET_INITIALIZED)
19e1051a39Sopenharmony_ci *     var = function_returning_same_value();
20e1051a39Sopenharmony_ci *
21e1051a39Sopenharmony_ci * This does work provided that loads and stores are single-instruction
22e1051a39Sopenharmony_ci * operations (and integer ones are on *all* supported platforms), but
23e1051a39Sopenharmony_ci * it upsets Thread Sanitizer. Suggested solution is
24e1051a39Sopenharmony_ci *
25e1051a39Sopenharmony_ci * if (tsan_load(&var) == NOT_YET_INITIALIZED)
26e1051a39Sopenharmony_ci *     tsan_store(&var, function_returning_same_value());
27e1051a39Sopenharmony_ci *
28e1051a39Sopenharmony_ci * Production machine code would be the same, so one can wonder why
29e1051a39Sopenharmony_ci * bother. Having Thread Sanitizer accept "thread-opportunistic" code
30e1051a39Sopenharmony_ci * allows to move on trouble-shooting real bugs.
31e1051a39Sopenharmony_ci *
32e1051a39Sopenharmony_ci * Resolving Thread Sanitizer nits was the initial purpose for this module,
33e1051a39Sopenharmony_ci * but it was later extended with more nuanced primitives that are useful
34e1051a39Sopenharmony_ci * even in "non-opportunistic" scenarios. Most notably verifying if a shared
35e1051a39Sopenharmony_ci * structure is fully initialized and bypassing the initialization lock.
36e1051a39Sopenharmony_ci * It's suggested to view macros defined in this module as "annotations" for
37e1051a39Sopenharmony_ci * thread-safe lock-free code, "Thread-Safe ANnotations"...
38e1051a39Sopenharmony_ci *
39e1051a39Sopenharmony_ci * It's assumed that ATOMIC_{LONG|INT}_LOCK_FREE are assigned same value as
40e1051a39Sopenharmony_ci * ATOMIC_POINTER_LOCK_FREE. And check for >= 2 ensures that corresponding
41e1051a39Sopenharmony_ci * code is inlined. It should be noted that statistics counters become
42e1051a39Sopenharmony_ci * accurate in such case.
43e1051a39Sopenharmony_ci *
44e1051a39Sopenharmony_ci * Special note about TSAN_QUALIFIER. It might be undesired to use it in
45e1051a39Sopenharmony_ci * a shared header. Because whether operation on specific variable or member
46e1051a39Sopenharmony_ci * is atomic or not might be irrelevant in other modules. In such case one
47e1051a39Sopenharmony_ci * can use TSAN_QUALIFIER in cast specifically when it has to count.
48e1051a39Sopenharmony_ci */
49e1051a39Sopenharmony_ci
50e1051a39Sopenharmony_ci#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
51e1051a39Sopenharmony_ci    && !defined(__STDC_NO_ATOMICS__)
52e1051a39Sopenharmony_ci# include <stdatomic.h>
53e1051a39Sopenharmony_ci
54e1051a39Sopenharmony_ci# if defined(ATOMIC_POINTER_LOCK_FREE) \
55e1051a39Sopenharmony_ci          && ATOMIC_POINTER_LOCK_FREE >= 2
56e1051a39Sopenharmony_ci#  define TSAN_QUALIFIER _Atomic
57e1051a39Sopenharmony_ci#  define tsan_load(ptr) atomic_load_explicit((ptr), memory_order_relaxed)
58e1051a39Sopenharmony_ci#  define tsan_store(ptr, val) atomic_store_explicit((ptr), (val), memory_order_relaxed)
59e1051a39Sopenharmony_ci#  define tsan_counter(ptr) atomic_fetch_add_explicit((ptr), 1, memory_order_relaxed)
60e1051a39Sopenharmony_ci#  define tsan_decr(ptr) atomic_fetch_add_explicit((ptr), -1, memory_order_relaxed)
61e1051a39Sopenharmony_ci#  define tsan_ld_acq(ptr) atomic_load_explicit((ptr), memory_order_acquire)
62e1051a39Sopenharmony_ci#  define tsan_st_rel(ptr, val) atomic_store_explicit((ptr), (val), memory_order_release)
63e1051a39Sopenharmony_ci# endif
64e1051a39Sopenharmony_ci
65e1051a39Sopenharmony_ci#elif defined(__GNUC__) && defined(__ATOMIC_RELAXED)
66e1051a39Sopenharmony_ci
67e1051a39Sopenharmony_ci# if defined(__GCC_ATOMIC_POINTER_LOCK_FREE) \
68e1051a39Sopenharmony_ci          && __GCC_ATOMIC_POINTER_LOCK_FREE >= 2
69e1051a39Sopenharmony_ci#  define TSAN_QUALIFIER volatile
70e1051a39Sopenharmony_ci#  define tsan_load(ptr) __atomic_load_n((ptr), __ATOMIC_RELAXED)
71e1051a39Sopenharmony_ci#  define tsan_store(ptr, val) __atomic_store_n((ptr), (val), __ATOMIC_RELAXED)
72e1051a39Sopenharmony_ci#  define tsan_counter(ptr) __atomic_fetch_add((ptr), 1, __ATOMIC_RELAXED)
73e1051a39Sopenharmony_ci#  define tsan_decr(ptr) __atomic_fetch_add((ptr), -1, __ATOMIC_RELAXED)
74e1051a39Sopenharmony_ci#  define tsan_ld_acq(ptr) __atomic_load_n((ptr), __ATOMIC_ACQUIRE)
75e1051a39Sopenharmony_ci#  define tsan_st_rel(ptr, val) __atomic_store_n((ptr), (val), __ATOMIC_RELEASE)
76e1051a39Sopenharmony_ci# endif
77e1051a39Sopenharmony_ci
78e1051a39Sopenharmony_ci#elif defined(_MSC_VER) && _MSC_VER>=1200 \
79e1051a39Sopenharmony_ci      && (defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64) || \
80e1051a39Sopenharmony_ci          defined(_M_ARM64) || (defined(_M_ARM) && _M_ARM >= 7 && !defined(_WIN32_WCE)))
81e1051a39Sopenharmony_ci/*
82e1051a39Sopenharmony_ci * There is subtle dependency on /volatile:<iso|ms> command-line option.
83e1051a39Sopenharmony_ci * "ms" implies same semantic as memory_order_acquire for loads and
84e1051a39Sopenharmony_ci * memory_order_release for stores, while "iso" - memory_order_relaxed for
85e1051a39Sopenharmony_ci * either. Real complication is that defaults are different on x86 and ARM.
86e1051a39Sopenharmony_ci * There is explanation for that, "ms" is backward compatible with earlier
87e1051a39Sopenharmony_ci * compiler versions, while multi-processor ARM can be viewed as brand new
88e1051a39Sopenharmony_ci * platform to MSC and its users, and with non-relaxed semantic taking toll
89e1051a39Sopenharmony_ci * with additional instructions and penalties, it kind of makes sense to
90e1051a39Sopenharmony_ci * default to "iso"...
91e1051a39Sopenharmony_ci */
92e1051a39Sopenharmony_ci# define TSAN_QUALIFIER volatile
93e1051a39Sopenharmony_ci# if defined(_M_ARM) || defined(_M_ARM64)
94e1051a39Sopenharmony_ci#  define _InterlockedExchangeAdd _InterlockedExchangeAdd_nf
95e1051a39Sopenharmony_ci#  pragma intrinsic(_InterlockedExchangeAdd_nf)
96e1051a39Sopenharmony_ci#  pragma intrinsic(__iso_volatile_load32, __iso_volatile_store32)
97e1051a39Sopenharmony_ci#  ifdef _WIN64
98e1051a39Sopenharmony_ci#   define _InterlockedExchangeAdd64 _InterlockedExchangeAdd64_nf
99e1051a39Sopenharmony_ci#   pragma intrinsic(_InterlockedExchangeAdd64_nf)
100e1051a39Sopenharmony_ci#   pragma intrinsic(__iso_volatile_load64, __iso_volatile_store64)
101e1051a39Sopenharmony_ci#   define tsan_load(ptr) (sizeof(*(ptr)) == 8 ? __iso_volatile_load64(ptr) \
102e1051a39Sopenharmony_ci                                               : __iso_volatile_load32(ptr))
103e1051a39Sopenharmony_ci#   define tsan_store(ptr, val) (sizeof(*(ptr)) == 8 ? __iso_volatile_store64((ptr), (val)) \
104e1051a39Sopenharmony_ci                                                     : __iso_volatile_store32((ptr), (val)))
105e1051a39Sopenharmony_ci#  else
106e1051a39Sopenharmony_ci#   define tsan_load(ptr) __iso_volatile_load32(ptr)
107e1051a39Sopenharmony_ci#   define tsan_store(ptr, val) __iso_volatile_store32((ptr), (val))
108e1051a39Sopenharmony_ci#  endif
109e1051a39Sopenharmony_ci# else
110e1051a39Sopenharmony_ci#  define tsan_load(ptr) (*(ptr))
111e1051a39Sopenharmony_ci#  define tsan_store(ptr, val) (*(ptr) = (val))
112e1051a39Sopenharmony_ci# endif
113e1051a39Sopenharmony_ci# pragma intrinsic(_InterlockedExchangeAdd)
114e1051a39Sopenharmony_ci# ifdef _WIN64
115e1051a39Sopenharmony_ci#  pragma intrinsic(_InterlockedExchangeAdd64)
116e1051a39Sopenharmony_ci#  define tsan_counter(ptr) (sizeof(*(ptr)) == 8 ? _InterlockedExchangeAdd64((ptr), 1) \
117e1051a39Sopenharmony_ci                                                 : _InterlockedExchangeAdd((ptr), 1))
118e1051a39Sopenharmony_ci#  define tsan_decr(ptr) (sizeof(*(ptr)) == 8 ? _InterlockedExchangeAdd64((ptr), -1) \
119e1051a39Sopenharmony_ci                                                 : _InterlockedExchangeAdd((ptr), -1))
120e1051a39Sopenharmony_ci# else
121e1051a39Sopenharmony_ci#  define tsan_counter(ptr) _InterlockedExchangeAdd((ptr), 1)
122e1051a39Sopenharmony_ci#  define tsan_decr(ptr) _InterlockedExchangeAdd((ptr), -1)
123e1051a39Sopenharmony_ci# endif
124e1051a39Sopenharmony_ci# if !defined(_ISO_VOLATILE)
125e1051a39Sopenharmony_ci#  define tsan_ld_acq(ptr) (*(ptr))
126e1051a39Sopenharmony_ci#  define tsan_st_rel(ptr, val) (*(ptr) = (val))
127e1051a39Sopenharmony_ci# endif
128e1051a39Sopenharmony_ci
129e1051a39Sopenharmony_ci#endif
130e1051a39Sopenharmony_ci
131e1051a39Sopenharmony_ci#ifndef TSAN_QUALIFIER
132e1051a39Sopenharmony_ci
133e1051a39Sopenharmony_ci# ifdef OPENSSL_THREADS
134e1051a39Sopenharmony_ci#  define TSAN_QUALIFIER volatile
135e1051a39Sopenharmony_ci#  define TSAN_REQUIRES_LOCKING
136e1051a39Sopenharmony_ci# else  /* OPENSSL_THREADS */
137e1051a39Sopenharmony_ci#  define TSAN_QUALIFIER
138e1051a39Sopenharmony_ci# endif /* OPENSSL_THREADS */
139e1051a39Sopenharmony_ci
140e1051a39Sopenharmony_ci# define tsan_load(ptr) (*(ptr))
141e1051a39Sopenharmony_ci# define tsan_store(ptr, val) (*(ptr) = (val))
142e1051a39Sopenharmony_ci# define tsan_counter(ptr) ((*(ptr))++)
143e1051a39Sopenharmony_ci# define tsan_decr(ptr) ((*(ptr))--)
144e1051a39Sopenharmony_ci/*
145e1051a39Sopenharmony_ci * Lack of tsan_ld_acq and tsan_ld_rel means that compiler support is not
146e1051a39Sopenharmony_ci * sophisticated enough to support them. Code that relies on them should be
147e1051a39Sopenharmony_ci * protected with #ifdef tsan_ld_acq with locked fallback.
148e1051a39Sopenharmony_ci */
149e1051a39Sopenharmony_ci
150e1051a39Sopenharmony_ci#endif
151