From d6d5170ed205838165dca1cf2f66c7d7a1176802 Mon Sep 17 00:00:00 2001 From: Neil Horman Date: Thu, 12 Jun 2025 13:16:10 -0400 Subject: [PATCH] Update ERR lib to use new thread-local storage api MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Move the err lib to use the new thread-local storage api. This isn't strictly needed, as each thread uses the same key for each context, but it does reduce our Os level key count. Reviewed-by: Saša Nedvědický Reviewed-by: Tomas Mraz Reviewed-by: Matt Caswell (Merged from https://github.com/openssl/openssl/pull/27794) --- crypto/err/err.c | 42 +++++++++++++----------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/crypto/err/err.c b/crypto/err/err.c index b95182d7029..8b06848e88f 100644 --- a/crypto/err/err.c +++ b/crypto/err/err.c @@ -21,6 +21,7 @@ #include #include #include "internal/thread_once.h" +#include "internal/threads_common.h" #include "crypto/ctype.h" #include "internal/constant_time.h" #include "internal/e_os.h" @@ -138,10 +139,6 @@ static ERR_STRING_DATA ERR_str_reasons[] = { }; #endif -static CRYPTO_ONCE err_init = CRYPTO_ONCE_STATIC_INIT; -static int set_err_thread_local; -static CRYPTO_THREAD_LOCAL err_thread_local; - static CRYPTO_ONCE err_string_init = CRYPTO_ONCE_STATIC_INIT; static CRYPTO_RWLOCK *err_string_lock = NULL; @@ -231,8 +228,6 @@ DEFINE_RUN_ONCE_STATIC(do_err_strings_init) void err_cleanup(void) { - if (set_err_thread_local != 0) - CRYPTO_THREAD_cleanup_local(&err_thread_local); CRYPTO_THREAD_lock_free(err_string_lock); err_string_lock = NULL; #ifndef OPENSSL_NO_ERR @@ -643,11 +638,12 @@ const char *ERR_reason_error_string(unsigned long e) static void err_delete_thread_state(void *unused) { - ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local); + ERR_STATE *state = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_ERR_KEY, NULL); + if (state == NULL) return; - CRYPTO_THREAD_set_local(&err_thread_local, NULL); + CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_ERR_KEY, NULL, NULL); OSSL_ERR_STATE_free(state); } @@ -663,12 +659,6 @@ void ERR_remove_state(unsigned long pid) } #endif -DEFINE_RUN_ONCE_STATIC(err_do_init) -{ - set_err_thread_local = 1; - return CRYPTO_THREAD_init_local(&err_thread_local, NULL); -} - ERR_STATE *ossl_err_get_state_int(void) { ERR_STATE *state; @@ -677,27 +667,24 @@ ERR_STATE *ossl_err_get_state_int(void) if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL)) return NULL; - if (!RUN_ONCE(&err_init, err_do_init)) - return NULL; - - state = CRYPTO_THREAD_get_local(&err_thread_local); - if (state == (ERR_STATE*)-1) + state = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_ERR_KEY, NULL); + if (state == (ERR_STATE *)-1) return NULL; if (state == NULL) { - if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1)) + if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_ERR_KEY, NULL, (ERR_STATE *)-1)) return NULL; state = OSSL_ERR_STATE_new(); if (state == NULL) { - CRYPTO_THREAD_set_local(&err_thread_local, NULL); + CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_ERR_KEY, NULL, NULL); return NULL; } if (!ossl_init_thread_start(NULL, NULL, err_delete_thread_state) - || !CRYPTO_THREAD_set_local(&err_thread_local, state)) { + || !CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_ERR_KEY, NULL, state)) { OSSL_ERR_STATE_free(state); - CRYPTO_THREAD_set_local(&err_thread_local, NULL); + CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_ERR_KEY, NULL, NULL); return NULL; } @@ -740,11 +727,8 @@ int err_shelve_state(void **state) if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL)) return 0; - if (!RUN_ONCE(&err_init, err_do_init)) - return 0; - - *state = CRYPTO_THREAD_get_local(&err_thread_local); - if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1)) + *state = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_ERR_KEY, NULL); + if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_ERR_KEY, NULL, (ERR_STATE *)-1)) return 0; set_sys_error(saveerrno); @@ -758,7 +742,7 @@ int err_shelve_state(void **state) void err_unshelve_state(void* state) { if (state != (void*)-1) - CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)state); + CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_ERR_KEY, NULL, (ERR_STATE *)state); } int ERR_get_next_error_library(void) -- 2.47.2