diff options
author | Neil Horman <nhorman@openssl.org> | 2024-03-08 11:58:07 -0500 |
---|---|---|
committer | Pauli <ppzgs1@gmail.com> | 2024-04-24 12:03:03 +1000 |
commit | 7e45ac6891ade57cb0141402745d144c4ce342cb (patch) | |
tree | 674f533e5a639320afa6e15cd847c6818b5ef0e6 /crypto | |
parent | f39a86281883bd7ff0b3791ed203756d055c001b (diff) | |
download | openssl-7e45ac6891ade57cb0141402745d144c4ce342cb.tar.gz |
Add CRYPTO_atomic_store api
Generally we can get away with just using CRYPTO_atomic_load to do
stores by reversing the source and target variables, but doing so
creates a problem for the thread sanitizer as CRYPTO_atomic_load hard
codes an __ATOMIC_ACQUIRE constraint, which confuses tsan into thinking
that loads and stores aren't properly ordered, leading to RAW/WAR
hazzards getting reported. Instead create a CRYPTO_atomic_store api
that is identical to the load variant, save for the fact that the value
is a unit64_t rather than a pointer that gets stored using an
__ATOMIC_RELEASE constraint, satisfying tsan.
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/23671)
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/threads_none.c | 7 | ||||
-rw-r--r-- | crypto/threads_pthread.c | 23 | ||||
-rw-r--r-- | crypto/threads_win.c | 16 |
3 files changed, 46 insertions, 0 deletions
diff --git a/crypto/threads_none.c b/crypto/threads_none.c index 47a7c01f26..e0387650fe 100644 --- a/crypto/threads_none.c +++ b/crypto/threads_none.c @@ -226,6 +226,13 @@ int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock) return 1; } +int CRYPTO_atomic_store(uint64_t *dst, uint64_t val, CRYPTO_RWLOCK *lock) +{ + *dst = val; + + return 1; +} + int CRYPTO_atomic_load_int(int *val, int *ret, CRYPTO_RWLOCK *lock) { *ret = *val; diff --git a/crypto/threads_pthread.c b/crypto/threads_pthread.c index 69b68e5226..8e411671d9 100644 --- a/crypto/threads_pthread.c +++ b/crypto/threads_pthread.c @@ -919,6 +919,29 @@ int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock) return 1; } +int CRYPTO_atomic_store(uint64_t *dst, uint64_t val, CRYPTO_RWLOCK *lock) +{ +# if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE) && !defined(BROKEN_CLANG_ATOMICS) + if (__atomic_is_lock_free(sizeof(*dst), dst)) { + __atomic_store(dst, &val, __ATOMIC_RELEASE); + return 1; + } +# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11)) + /* This will work for all future Solaris versions. */ + if (ret != NULL) { + atomic_swap_64(dst, val); + return 1; + } +# endif + if (lock == NULL || !CRYPTO_THREAD_read_lock(lock)) + return 0; + *dst = val; + if (!CRYPTO_THREAD_unlock(lock)) + return 0; + + return 1; +} + int CRYPTO_atomic_load_int(int *val, int *ret, CRYPTO_RWLOCK *lock) { # if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE) && !defined(BROKEN_CLANG_ATOMICS) diff --git a/crypto/threads_win.c b/crypto/threads_win.c index 6bcbaea10f..ea72670f22 100644 --- a/crypto/threads_win.c +++ b/crypto/threads_win.c @@ -593,6 +593,22 @@ int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock) #endif } +int CRYPTO_atomic_store(uint64_t *dst, uint64_t val, CRYPTO_RWLOCK *lock) +{ +#if (defined(NO_INTERLOCKEDOR64)) + if (lock == NULL || !CRYPTO_THREAD_read_lock(lock)) + return 0; + *dst = val; + if (!CRYPTO_THREAD_unlock(lock)) + return 0; + + return 1; +#else + InterlockedExchange64(dst, val); + return 1; +#endif +} + int CRYPTO_atomic_load_int(int *val, int *ret, CRYPTO_RWLOCK *lock) { #if (defined(NO_INTERLOCKEDOR64)) |