# HG changeset patch # User Taylor R Campbell # Date 1778622433 0 # Tue May 12 21:47:13 2026 +0000 # Branch trunk # Node ID b28152ed56da88505270771699fd246e1eb86908 # Parent 313a5cb6565869d1de13126d3692484b2945233f # EXP-Topic riastradh-pr60244-opensslthreadlocalinit WIP: openssl: Disentangle thread-local initialization logic. https://github.com/openssl/openssl/issues/31166 PR lib/60244: openssl 3.5.6 pullup can emit pthread warnings on init diff -r 313a5cb65658 -r b28152ed56da crypto/external/apache2/openssl/dist/crypto/initthread.c --- a/crypto/external/apache2/openssl/dist/crypto/initthread.c Tue May 12 13:07:52 2026 +0000 +++ b/crypto/external/apache2/openssl/dist/crypto/initthread.c Tue May 12 21:47:13 2026 +0000 @@ -200,41 +200,33 @@ static void init_thread_destructor(void } static CRYPTO_ONCE ossl_init_thread_runonce = CRYPTO_ONCE_STATIC_INIT; -/* MSVC linker can use other segment for uninitialized (zeroed) variables */ -#if defined(OPENSSL_SYS_WINDOWS) -static CRYPTO_THREAD_ID recursion_guard = (CRYPTO_THREAD_ID)-1; -#elif defined(OPENSSL_SYS_TANDEM) && (defined(_PUT_MODEL_) || defined(_KLT_MODEL_)) -static CRYPTO_THREAD_ID recursion_guard = { (void *)-1, (short)-1, (short)-1 }; -#else -static CRYPTO_THREAD_ID recursion_guard = (CRYPTO_THREAD_ID)0; -#endif DEFINE_RUN_ONCE_STATIC(ossl_init_thread_once) { - /* CRYPTO_THREAD_init_local() can call ossl_init_threads() again */ - recursion_guard = CRYPTO_THREAD_get_current_id(); - if (!CRYPTO_THREAD_init_local(&destructor_key.value, + if (!ossl_thread_init_local(&destructor_key.value, init_thread_destructor)) return 0; -#if defined(OPENSSL_SYS_TANDEM) - memset(&recursion_guard, 0, sizeof(recursion_guard)); -#else - recursion_guard = (CRYPTO_THREAD_ID)0; -#endif return 1; } int ossl_init_thread(void) { - if (CRYPTO_THREAD_compare_id(recursion_guard, - CRYPTO_THREAD_get_current_id())) - return 1; if (!RUN_ONCE(&ossl_init_thread_runonce, ossl_init_thread_once)) return 0; return 1; } +int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *)) +{ + +#ifndef FIPS_MODULE + if (!ossl_init_thread()) + return 0; +#endif + return ossl_thread_init_local(key, cleanup); +} + void ossl_cleanup_thread(void) { init_thread_deregister(NULL, 1); diff -r 313a5cb65658 -r b28152ed56da crypto/external/apache2/openssl/dist/crypto/threads_none.c --- a/crypto/external/apache2/openssl/dist/crypto/threads_none.c Tue May 12 13:07:52 2026 +0000 +++ b/crypto/external/apache2/openssl/dist/crypto/threads_none.c Tue May 12 21:47:13 2026 +0000 @@ -162,15 +162,10 @@ struct thread_local_storage_entry { static struct thread_local_storage_entry thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX]; -int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *)) +int ossl_thread_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *)) { int entry_idx = 0; -#ifndef FIPS_MODULE - if (!ossl_init_thread()) - return 0; -#endif - for (entry_idx = 0; entry_idx < OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX; entry_idx++) { if (!thread_local_storage[entry_idx].used) break; diff -r 313a5cb65658 -r b28152ed56da crypto/external/apache2/openssl/dist/crypto/threads_pthread.c --- a/crypto/external/apache2/openssl/dist/crypto/threads_pthread.c Tue May 12 13:07:52 2026 +0000 +++ b/crypto/external/apache2/openssl/dist/crypto/threads_pthread.c Tue May 12 21:47:13 2026 +0000 @@ -728,14 +728,9 @@ int CRYPTO_THREAD_run_once(CRYPTO_ONCE * return 1; } -int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *)) +int ossl_thread_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *)) { -#ifndef FIPS_MODULE - if (!ossl_init_thread()) - return 0; -#endif - if (pthread_key_create(key, cleanup) != 0) return 0; diff -r 313a5cb65658 -r b28152ed56da crypto/external/apache2/openssl/dist/crypto/threads_win.c --- a/crypto/external/apache2/openssl/dist/crypto/threads_win.c Tue May 12 13:07:52 2026 +0000 +++ b/crypto/external/apache2/openssl/dist/crypto/threads_win.c Tue May 12 21:47:13 2026 +0000 @@ -540,14 +540,9 @@ int CRYPTO_THREAD_run_once(CRYPTO_ONCE * return (*lock == ONCE_DONE); } -int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *)) +int ossl_thread_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *)) { -#ifndef FIPS_MODULE - if (!ossl_init_thread()) - return 0; -#endif - *key = TlsAlloc(); if (*key == TLS_OUT_OF_INDEXES) return 0; diff -r 313a5cb65658 -r b28152ed56da crypto/external/apache2/openssl/dist/include/internal/cryptlib.h --- a/crypto/external/apache2/openssl/dist/include/internal/cryptlib.h Tue May 12 13:07:52 2026 +0000 +++ b/crypto/external/apache2/openssl/dist/include/internal/cryptlib.h Tue May 12 21:47:13 2026 +0000 @@ -133,6 +133,8 @@ OSSL_EX_DATA_GLOBAL *ossl_lib_ctx_get_ex const char *ossl_lib_ctx_get_descriptor(OSSL_LIB_CTX *libctx); CRYPTO_THREAD_LOCAL *ossl_lib_ctx_get_rcukey(OSSL_LIB_CTX *libctx); +int ossl_thread_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *)); + OSSL_LIB_CTX *ossl_crypto_ex_data_get_ossl_lib_ctx(const CRYPTO_EX_DATA *ad); int ossl_crypto_new_ex_data_ex(OSSL_LIB_CTX *ctx, int class_index, void *obj, CRYPTO_EX_DATA *ad);