Change all our uses of CRYPTO_THREAD_run_once to use RUN_ONCE instead
authorRichard Levitte <levitte@openssl.org>
Tue, 19 Jul 2016 17:42:11 +0000 (19:42 +0200)
committerRichard Levitte <levitte@openssl.org>
Tue, 19 Jul 2016 21:49:54 +0000 (23:49 +0200)
That way, we have a way to check if the init function was successful
or not.

Reviewed-by: Kurt Roeckx <kurt@openssl.org>
15 files changed:
crypto/bio/b_addr.c
crypto/engine/eng_init.c
crypto/engine/eng_int.h
crypto/engine/eng_lib.c
crypto/engine/eng_list.c
crypto/engine/tb_asnmth.c
crypto/err/err.c
crypto/ex_data.c
crypto/init.c
crypto/mem_dbg.c
crypto/rand/md_rand.c
include/openssl/err.h
ssl/ssl_cert.c
ssl/ssl_ciph.c
ssl/ssl_init.c

index b77fd4b1a0c7e9b5b8ead554b2216754031345fd..4e8785fdcd41e28ca9f393e82621096f49175b8f 100644 (file)
@@ -15,6 +15,7 @@
 #ifndef OPENSSL_NO_SOCK
 #include <openssl/err.h>
 #include <openssl/buffer.h>
+#include <internal/thread_once.h>
 #include <ctype.h>
 
 CRYPTO_RWLOCK *bio_lookup_lock;
@@ -601,9 +602,10 @@ static int addrinfo_wrap(int family, int socktype,
     return 1;
 }
 
-static void do_bio_lookup_init(void)
+DEFINE_RUN_ONCE_STATIC(do_bio_lookup_init)
 {
     bio_lookup_lock = CRYPTO_THREAD_lock_new();
+    return (bio_lookup_lock != NULL);
 }
 
 /*-
@@ -727,7 +729,11 @@ int BIO_lookup(const char *host, const char *service,
         struct servent se_fallback = { NULL, NULL, 0, NULL };
 #endif
 
-        CRYPTO_THREAD_run_once(&bio_lookup_init, do_bio_lookup_init);
+        if (!RUN_ONCE(&bio_lookup_init, do_bio_lookup_init)) {
+            BIOerr(BIO_F_BIO_LOOKUP, ERR_R_MALLOC_FAILURE);
+            ret = 0;
+            goto err;
+        }
 
         CRYPTO_THREAD_write_lock(bio_lookup_lock);
         he_fallback_address = INADDR_ANY;
index c51a38dac0cfdefa33e3f885a4fb2d2246828fcd..8be7c6fc8658b69d0997db69545bce35bf3e1dfe 100644 (file)
@@ -80,7 +80,10 @@ int ENGINE_init(ENGINE *e)
         ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_PASSED_NULL_PARAMETER);
         return 0;
     }
-    CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
+    if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
+        ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_MALLOC_FAILURE);
+        return 0;
+    }
     CRYPTO_THREAD_write_lock(global_engine_lock);
     ret = engine_unlocked_init(e);
     CRYPTO_THREAD_unlock(global_engine_lock);
index 6b6ff9fa4cbe8df77d25930a690dcf02d3194458..c604faddd7a0cad122b89b1af0bf0cf6ed335998 100644 (file)
@@ -18,6 +18,7 @@
 
 # include "internal/cryptlib.h"
 # include <internal/engine.h>
+# include <internal/thread_once.h>
 
 #ifdef  __cplusplus
 extern "C" {
@@ -123,7 +124,7 @@ void engine_pkey_asn1_meths_free(ENGINE *e);
 
 /* Once initialisation function */
 extern CRYPTO_ONCE engine_lock_init;
-void do_engine_lock_init(void);
+DECLARE_RUN_ONCE(do_engine_lock_init)
 
 /*
  * This is a structure for storing implementations of various crypto
index f7d0216efb01f5e7235991c7cc5fbe62047aef8d..67d755a2aa988d6471236012987cdefe54d74182 100644 (file)
@@ -16,19 +16,18 @@ CRYPTO_ONCE engine_lock_init = CRYPTO_ONCE_STATIC_INIT;
 
 /* The "new"/"free" stuff first */
 
-void do_engine_lock_init(void)
+DEFINE_RUN_ONCE(do_engine_lock_init)
 {
     global_engine_lock = CRYPTO_THREAD_lock_new();
+    return global_engine_lock != NULL;
 }
 
 ENGINE *ENGINE_new(void)
 {
     ENGINE *ret;
 
-    CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
-
-    ret = OPENSSL_zalloc(sizeof(*ret));
-    if (ret == NULL) {
+    if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)
+        || (ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
         ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE);
         return NULL;
     }
index a7252e930b5d163ab47c2a04ddbf95bdb8143327..934389f74e497d03bd61681c1f847536af80907b 100644 (file)
@@ -136,7 +136,11 @@ ENGINE *ENGINE_get_first(void)
 {
     ENGINE *ret;
 
-    CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
+    if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
+        ENGINEerr(ENGINE_F_ENGINE_GET_FIRST, ERR_R_MALLOC_FAILURE);
+        return NULL;
+    }
+
     CRYPTO_THREAD_write_lock(global_engine_lock);
     ret = engine_list_head;
     if (ret) {
@@ -151,7 +155,11 @@ ENGINE *ENGINE_get_last(void)
 {
     ENGINE *ret;
 
-    CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
+    if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
+        ENGINEerr(ENGINE_F_ENGINE_GET_LAST, ERR_R_MALLOC_FAILURE);
+        return NULL;
+    }
+
     CRYPTO_THREAD_write_lock(global_engine_lock);
     ret = engine_list_tail;
     if (ret) {
@@ -279,7 +287,11 @@ ENGINE *ENGINE_by_id(const char *id)
         ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_PASSED_NULL_PARAMETER);
         return NULL;
     }
-    CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
+    if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
+        ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_MALLOC_FAILURE);
+        return NULL;
+    }
+
     CRYPTO_THREAD_write_lock(global_engine_lock);
     iterator = engine_list_head;
     while (iterator && (strcmp(id, iterator->id) != 0))
index 2a6a4ae95ed4acfa71e5601c4811e92e302bd103..480267daabad91ed1ab65dfc0916d87e1da485c0 100644 (file)
@@ -189,7 +189,11 @@ const EVP_PKEY_ASN1_METHOD *ENGINE_pkey_asn1_find_str(ENGINE **pe,
     fstr.str = str;
     fstr.len = len;
 
-    CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
+    if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
+        ENGINEerr(ENGINE_F_ENGINE_PKEY_ASN1_FIND_STR, ERR_R_MALLOC_FAILURE);
+        return NULL;
+    }
+
     CRYPTO_THREAD_write_lock(global_engine_lock);
     engine_table_doall(pkey_asn1_meth_table, look_str_cb, &fstr);
     /* If found obtain a structural reference to engine */
index 836fac4260cb230e31d215d1149ad8c25ff3b119..ad1ccd10d076bb8d7f6a90645633d74ce30fcb53 100644 (file)
@@ -18,6 +18,7 @@
 #include <openssl/buffer.h>
 #include <openssl/bio.h>
 #include <openssl/opensslconf.h>
+#include <internal/thread_once.h>
 
 static void err_load_strings(int lib, ERR_STRING_DATA *str);
 
@@ -270,9 +271,10 @@ static void ERR_STATE_free(ERR_STATE *s)
     OPENSSL_free(s);
 }
 
-static void do_err_strings_init(void)
+DEFINE_RUN_ONCE_STATIC(do_err_strings_init)
 {
     err_string_lock = CRYPTO_THREAD_lock_new();
+    return err_string_lock != NULL;
 }
 
 void err_cleanup(void)
@@ -284,7 +286,7 @@ void err_cleanup(void)
 void ERR_load_ERR_strings(void)
 {
 #ifndef OPENSSL_NO_ERR
-    CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init);
+    RUN_ONCE(&err_string_init, do_err_strings_init);
 
     err_load_strings(0, ERR_str_libraries);
     err_load_strings(0, ERR_str_reasons);
@@ -316,11 +318,12 @@ void ERR_load_strings(int lib, ERR_STRING_DATA *str)
     err_load_strings(lib, str);
 }
 
-void ERR_unload_strings(int lib, ERR_STRING_DATA *str)
+int ERR_unload_strings(int lib, ERR_STRING_DATA *str)
 {
     LHASH_OF(ERR_STRING_DATA) *hash;
 
-    CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init);
+    if (!RUN_ONCE(&err_string_init, do_err_strings_init))
+        return 0;
 
     CRYPTO_THREAD_write_lock(err_string_lock);
     hash = get_hash(0, 0);
@@ -332,11 +335,14 @@ void ERR_unload_strings(int lib, ERR_STRING_DATA *str)
         }
     }
     CRYPTO_THREAD_unlock(err_string_lock);
+
+    return 1;
 }
 
 void err_free_strings_int(void)
 {
-    CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init);
+    if (!RUN_ONCE(&err_string_init, do_err_strings_init))
+        return;
 
     CRYPTO_THREAD_write_lock(err_string_lock);
     lh_ERR_STRING_DATA_free(int_error_hash);
@@ -582,7 +588,9 @@ const char *ERR_lib_error_string(unsigned long e)
     ERR_STRING_DATA d, *p;
     unsigned long l;
 
-    CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init);
+    if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
+        return NULL;
+    }
 
     l = ERR_GET_LIB(e);
     d.error = ERR_PACK(l, 0, 0);
@@ -595,7 +603,9 @@ const char *ERR_func_error_string(unsigned long e)
     ERR_STRING_DATA d, *p;
     unsigned long l, f;
 
-    CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init);
+    if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
+        return NULL;
+    }
 
     l = ERR_GET_LIB(e);
     f = ERR_GET_FUNC(e);
@@ -609,7 +619,9 @@ const char *ERR_reason_error_string(unsigned long e)
     ERR_STRING_DATA d, *p = NULL;
     unsigned long l, r;
 
-    CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init);
+    if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
+        return NULL;
+    }
 
     l = ERR_GET_LIB(e);
     r = ERR_GET_REASON(e);
@@ -644,16 +656,17 @@ void ERR_remove_state(unsigned long pid)
 }
 #endif
 
-static void err_do_init(void)
+DEFINE_RUN_ONCE_STATIC(err_do_init)
 {
-    CRYPTO_THREAD_init_local(&err_thread_local, NULL);
+    return CRYPTO_THREAD_init_local(&err_thread_local, NULL);
 }
 
 ERR_STATE *ERR_get_state(void)
 {
     ERR_STATE *state = NULL;
 
-    CRYPTO_THREAD_run_once(&err_init, err_do_init);
+    if (!RUN_ONCE(&err_init, err_do_init))
+        return NULL;
 
     state = CRYPTO_THREAD_get_local(&err_thread_local);
 
@@ -679,7 +692,9 @@ int ERR_get_next_error_library(void)
 {
     int ret;
 
-    CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init);
+    if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
+        return 0;
+    }
 
     CRYPTO_THREAD_write_lock(err_string_lock);
     ret = int_err_library_number++;
index 0666393900b4852012a76ac26722c594fd0ec5e9..d9807d585916572892856bdc7c26e67774cb8999 100644 (file)
@@ -8,6 +8,7 @@
  */
 
 #include "internal/cryptlib_int.h"
+#include "internal/thread_once.h"
 #include <openssl/lhash.h>
 
 /*
@@ -35,9 +36,10 @@ static EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT];
 static CRYPTO_RWLOCK *ex_data_lock = NULL;
 static CRYPTO_ONCE ex_data_init = CRYPTO_ONCE_STATIC_INIT;
 
-static void do_ex_data_init(void)
+DEFINE_RUN_ONCE_STATIC(do_ex_data_init)
 {
     ex_data_lock = CRYPTO_THREAD_lock_new();
+    return ex_data_lock != NULL;
 }
 
 /*
@@ -53,7 +55,10 @@ static EX_CALLBACKS *get_and_lock(int class_index)
         return NULL;
     }
 
-    CRYPTO_THREAD_run_once(&ex_data_init, do_ex_data_init);
+    if (!RUN_ONCE(&ex_data_init, do_ex_data_init)) {
+        CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_MALLOC_FAILURE);
+        return NULL;
+    }
 
     if (ex_data_lock == NULL) {
         /*
index f38129fa20e8c62de15c4a255956ab1fba3f7d8b..ace11da610e570b543a2c94a44b47676e77a380f 100644 (file)
@@ -22,6 +22,7 @@
 #include <internal/objects.h>
 #include <stdlib.h>
 #include <assert.h>
+#include <internal/thread_once.h>
 
 static int stopped = 0;
 
@@ -61,7 +62,7 @@ static CRYPTO_RWLOCK *init_lock = NULL;
 
 static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
 static int base_inited = 0;
-static void ossl_init_base(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_base)
 {
 #ifdef OPENSSL_INIT_DEBUG
     fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
@@ -74,20 +75,22 @@ static void ossl_init_base(void)
 #ifndef OPENSSL_SYS_UEFI
     atexit(OPENSSL_cleanup);
 #endif
-    init_lock = CRYPTO_THREAD_lock_new();
+    if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL)
+        return 0;
     OPENSSL_cpuid_setup();
     base_inited = 1;
+    return 1;
 }
 
 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
 static int load_crypto_strings_inited = 0;
-static void ossl_init_no_load_crypto_strings(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_crypto_strings)
 {
     /* Do nothing in this case */
-    return;
+    return 1;
 }
 
-static void ossl_init_load_crypto_strings(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
 {
     /*
      * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
@@ -101,10 +104,11 @@ static void ossl_init_load_crypto_strings(void)
     err_load_crypto_strings_int();
 #endif
     load_crypto_strings_inited = 1;
+    return 1;
 }
 
 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
-static void ossl_init_add_all_ciphers(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
 {
     /*
      * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
@@ -117,10 +121,11 @@ static void ossl_init_add_all_ciphers(void)
 # endif
     openssl_add_all_ciphers_int();
 #endif
+    return 1;
 }
 
 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
-static void ossl_init_add_all_digests(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
 {
     /*
      * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
@@ -133,18 +138,19 @@ static void ossl_init_add_all_digests(void)
 # endif
     openssl_add_all_digests_int();
 #endif
+    return 1;
 }
 
-static void ossl_init_no_add_algs(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_no_add_algs)
 {
     /* Do nothing */
-    return;
+    return 1;
 }
 
 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
 static int config_inited = 0;
 static const char *appname;
-static void ossl_init_config(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_config)
 {
 #ifdef OPENSSL_INIT_DEBUG
     fprintf(stderr,
@@ -153,8 +159,9 @@ static void ossl_init_config(void)
 #endif
     openssl_config_int(appname);
     config_inited = 1;
+    return 1;
 }
-static void ossl_init_no_config(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_no_config)
 {
 #ifdef OPENSSL_INIT_DEBUG
     fprintf(stderr,
@@ -162,103 +169,114 @@ static void ossl_init_no_config(void)
 #endif
     openssl_no_config_int();
     config_inited = 1;
+    return 1;
 }
 
 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
 static int async_inited = 0;
-static void ossl_init_async(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_async)
 {
 #ifdef OPENSSL_INIT_DEBUG
     fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
 #endif
-    async_init();
+    if (!async_init())
+        return 0;
     async_inited = 1;
+    return 1;
 }
 
 #ifndef OPENSSL_NO_ENGINE
 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
-static void ossl_init_engine_openssl(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
 {
 # ifdef OPENSSL_INIT_DEBUG
     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
                     "engine_load_openssl_int()\n");
 # endif
     engine_load_openssl_int();
+    return 1;
 }
 # if !defined(OPENSSL_NO_HW) && \
     (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
 static CRYPTO_ONCE engine_cryptodev = CRYPTO_ONCE_STATIC_INIT;
-static void ossl_init_engine_cryptodev(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_engine_cryptodev)
 {
 #  ifdef OPENSSL_INIT_DEBUG
     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
                     "engine_load_cryptodev_int()\n");
 #  endif
     engine_load_cryptodev_int();
+    return 1;
 }
 # endif
 
 # ifndef OPENSSL_NO_RDRAND
 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
-static void ossl_init_engine_rdrand(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
 {
 #  ifdef OPENSSL_INIT_DEBUG
     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
                     "engine_load_rdrand_int()\n");
 #  endif
     engine_load_rdrand_int();
+    return 1;
 }
 # endif
 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
-static void ossl_init_engine_dynamic(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
 {
 # ifdef OPENSSL_INIT_DEBUG
     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
                     "engine_load_dynamic_int()\n");
 # endif
     engine_load_dynamic_int();
+    return 1;
 }
 # ifndef OPENSSL_NO_STATIC_ENGINE
 #  if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
-static void ossl_init_engine_padlock(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
 {
 #   ifdef OPENSSL_INIT_DEBUG
     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
                     "engine_load_padlock_int()\n");
 #   endif
     engine_load_padlock_int();
+    return 1;
 }
 #  endif
 #  if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
-static void ossl_init_engine_capi(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
 {
 #   ifdef OPENSSL_INIT_DEBUG
     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
                     "engine_load_capi_int()\n");
 #   endif
     engine_load_capi_int();
+    return 1;
 }
 #  endif
 static CRYPTO_ONCE engine_dasync = CRYPTO_ONCE_STATIC_INIT;
-static void ossl_init_engine_dasync(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dasync)
 {
 # ifdef OPENSSL_INIT_DEBUG
     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dasync: "
                     "engine_load_dasync_int()\n");
 # endif
     engine_load_dasync_int();
+    return 1;
 }
 #  if !defined(OPENSSL_NO_AFALGENG)
 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
-static void ossl_init_engine_afalg(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
 {
 #   ifdef OPENSSL_INIT_DEBUG
     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
                     "engine_load_afalg_int()\n");
 #   endif
     engine_load_afalg_int();
+    return 1;
 }
 #  endif
 # endif
@@ -268,10 +286,11 @@ static void ossl_init_engine_afalg(void)
 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
 
 static int zlib_inited = 0;
-static void ossl_init_zlib(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
 {
     /* Do nothing - we need to know about this for the later cleanup */
     zlib_inited = 1;
+    return 1;
 }
 #endif
 
@@ -464,94 +483,87 @@ int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
         return 0;
     }
 
-    if (!CRYPTO_THREAD_run_once(&base, ossl_init_base))
+    if (!RUN_ONCE(&base, ossl_init_base))
         return 0;
 
     if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
-            && !CRYPTO_THREAD_run_once(&load_crypto_strings,
-                                       ossl_init_no_load_crypto_strings))
+            && !RUN_ONCE(&load_crypto_strings,
+                         ossl_init_no_load_crypto_strings))
         return 0;
 
     if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
-            && !CRYPTO_THREAD_run_once(&load_crypto_strings,
-                                       ossl_init_load_crypto_strings))
+            && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
         return 0;
 
     if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
-            && !CRYPTO_THREAD_run_once(&add_all_ciphers, ossl_init_no_add_algs))
+            && !RUN_ONCE(&add_all_ciphers, ossl_init_no_add_algs))
         return 0;
 
     if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
-            && !CRYPTO_THREAD_run_once(&add_all_ciphers,
-                                       ossl_init_add_all_ciphers))
+            && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
         return 0;
 
     if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
-            && !CRYPTO_THREAD_run_once(&add_all_digests, ossl_init_no_add_algs))
+            && !RUN_ONCE(&add_all_digests, ossl_init_no_add_algs))
         return 0;
 
     if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
-            && !CRYPTO_THREAD_run_once(&add_all_digests,
-                                       ossl_init_add_all_digests))
+            && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
         return 0;
 
     if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
-            && !CRYPTO_THREAD_run_once(&config, ossl_init_no_config))
+            && !RUN_ONCE(&config, ossl_init_no_config))
         return 0;
 
     if (opts & OPENSSL_INIT_LOAD_CONFIG) {
         int ret;
         CRYPTO_THREAD_write_lock(init_lock);
         appname = (settings == NULL) ? NULL : settings->appname;
-        ret = CRYPTO_THREAD_run_once(&config, ossl_init_config);
+        ret = RUN_ONCE(&config, ossl_init_config);
         CRYPTO_THREAD_unlock(init_lock);
         if (!ret)
             return 0;
     }
 
     if ((opts & OPENSSL_INIT_ASYNC)
-            && !CRYPTO_THREAD_run_once(&async, ossl_init_async))
+            && !RUN_ONCE(&async, ossl_init_async))
         return 0;
 
 #ifndef OPENSSL_NO_ENGINE
     if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
-            && !CRYPTO_THREAD_run_once(&engine_openssl,
-                                       ossl_init_engine_openssl))
+            && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
         return 0;
 # if !defined(OPENSSL_NO_HW) && \
     (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
     if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
-            && !CRYPTO_THREAD_run_once(&engine_cryptodev,
-                                       ossl_init_engine_cryptodev))
+            && !RUN_ONCE(&engine_cryptodev, ossl_init_engine_cryptodev))
         return 0;
 # endif
 # ifndef OPENSSL_NO_RDRAND
     if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
-            && !CRYPTO_THREAD_run_once(&engine_rdrand, ossl_init_engine_rdrand))
+            && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
         return 0;
 # endif
     if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
-            && !CRYPTO_THREAD_run_once(&engine_dynamic,
-                                       ossl_init_engine_dynamic))
+            && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
         return 0;
 # ifndef OPENSSL_NO_STATIC_ENGINE
 #  if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
     if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
-            && !CRYPTO_THREAD_run_once(&engine_padlock,
-                                       ossl_init_engine_padlock))
+            && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
         return 0;
 #  endif
 #  if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
     if ((opts & OPENSSL_INIT_ENGINE_CAPI)
-            && !CRYPTO_THREAD_run_once(&engine_capi, ossl_init_engine_capi))
+            && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
         return 0;
 #  endif
     if ((opts & OPENSSL_INIT_ENGINE_DASYNC)
-            && !CRYPTO_THREAD_run_once(&engine_dasync, ossl_init_engine_dasync))
+            && !RUN_ONCE(&engine_dasync, ossl_init_engine_dasync))
         return 0;
 #  if !defined(OPENSSL_NO_AFALGENG)
     if ((opts & OPENSSL_INIT_ENGINE_AFALG)
-            && !CRYPTO_THREAD_run_once(&engine_afalg, ossl_init_engine_afalg))
+            && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
         return 0;
 #  endif
 # endif
@@ -564,7 +576,7 @@ int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
 
 #ifndef OPENSSL_NO_COMP
     if ((opts & OPENSSL_INIT_ZLIB)
-            && !CRYPTO_THREAD_run_once(&zlib, ossl_init_zlib))
+            && !RUN_ONCE(&zlib, ossl_init_zlib))
         return 0;
 #endif
 
index 0b487082326283bdde007c783ffe7d1069e2b6ad..0307c0251723749d1d63aa28ddb614cc4efcfcf2 100644 (file)
@@ -112,7 +112,8 @@ int CRYPTO_mem_ctrl(int mode)
 #else
     int ret = mh_mode;
 
-    CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init);
+    if (!RUN_ONCE(&memdbg_init, do_memdbg_init))
+        return -1;
 
     CRYPTO_THREAD_write_lock(malloc_lock);
     switch (mode) {
@@ -185,7 +186,8 @@ static int mem_check_on(void)
     CRYPTO_THREAD_ID cur;
 
     if (mh_mode & CRYPTO_MEM_CHECK_ON) {
-        CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init);
+        if (!RUN_ONCE(&memdbg_init, do_memdbg_init))
+            return 0;
 
         cur = CRYPTO_THREAD_get_current_id();
         CRYPTO_THREAD_read_lock(malloc_lock);
@@ -228,7 +230,9 @@ static int pop_info(void)
 {
     APP_INFO *current = NULL;
 
-    CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init);
+    if (!RUN_ONCE(&memdbg_init, do_memdbg_init))
+        return 0;
+
     current = (APP_INFO *)CRYPTO_THREAD_get_local(&appinfokey);
     if (current != NULL) {
         APP_INFO *next = current->next;
@@ -258,9 +262,8 @@ int CRYPTO_mem_debug_push(const char *info, const char *file, int line)
     if (mem_check_on()) {
         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
 
-        CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init);
-
-        if ((ami = OPENSSL_malloc(sizeof(*ami))) == NULL)
+        if (!RUN_ONCE(&memdbg_init, do_memdbg_init)
+            || (ami = OPENSSL_malloc(sizeof(*ami))) == NULL)
             goto err;
 
         ami->threadid = CRYPTO_THREAD_get_current_id();
@@ -313,9 +316,8 @@ void CRYPTO_mem_debug_malloc(void *addr, size_t num, int before_p,
         if (mem_check_on()) {
             CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
 
-            CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init);
-
-            if ((m = OPENSSL_malloc(sizeof(*m))) == NULL) {
+            if (!RUN_ONCE(&memdbg_init, do_memdbg_init)
+                || (m = OPENSSL_malloc(sizeof(*m))) == NULL) {
                 OPENSSL_free(addr);
                 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
                 return;
@@ -543,7 +545,8 @@ int CRYPTO_mem_leaks(BIO *b)
     /* Ensure all resources are released */
     OPENSSL_cleanup();
 
-    CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init);
+    if (!RUN_ONCE(&memdbg_init, do_memdbg_init))
+        return -1;
 
     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
 
index 9d39831cf231c5643bd16ff94bca8ad2c068987e..7924478e9e8973654938941c4fa44775a24adf56 100644 (file)
@@ -27,6 +27,8 @@
 
 #include <openssl/err.h>
 
+#include <internal/thread_once.h>
+
 #ifdef OPENSSL_FIPS
 # include <openssl/fips.h>
 #endif
@@ -85,10 +87,11 @@ static RAND_METHOD rand_meth = {
     rand_status
 };
 
-static void do_rand_lock_init(void)
+DEFINE_RUN_ONCE_STATIC(do_rand_lock_init)
 {
     rand_lock = CRYPTO_THREAD_lock_new();
     rand_tmp_lock = CRYPTO_THREAD_lock_new();
+    return rand_lock != NULL && rand_tmp_lock != NULL;
 }
 
 RAND_METHOD *RAND_OpenSSL(void)
@@ -141,7 +144,8 @@ static int rand_add(const void *buf, int num, double add)
     if (m == NULL)
         goto err;
 
-    CRYPTO_THREAD_run_once(&rand_lock_init, do_rand_lock_init);
+    if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init))
+        goto err;
 
     /* check if we already have the lock */
     if (crypto_lock_rand) {
@@ -339,7 +343,9 @@ static int rand_bytes(unsigned char *buf, int num, int pseudo)
      * global 'md'.
      */
 
-    CRYPTO_THREAD_run_once(&rand_lock_init, do_rand_lock_init);
+    if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init))
+        goto err_mem;
+
     CRYPTO_THREAD_write_lock(rand_lock);
     /*
      * We could end up in an async engine while holding this lock so ensure
@@ -534,7 +540,9 @@ static int rand_status(void)
     int ret;
     int do_not_lock;
 
-    CRYPTO_THREAD_run_once(&rand_lock_init, do_rand_lock_init);
+    if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init))
+        return 0;
+
     cur = CRYPTO_THREAD_get_current_id();
     /*
      * check if we already have the lock (could happen if a RAND_poll()
index 9dcd27e9becd291b694ae0fb1a3193cabb6f4ff8..60ffc49edc623b2983ba4515107af9f00d2dadee 100644 (file)
@@ -232,7 +232,7 @@ void ERR_print_errors(BIO *bp);
 void ERR_add_error_data(int num, ...);
 void ERR_add_error_vdata(int num, va_list args);
 void ERR_load_strings(int lib, ERR_STRING_DATA str[]);
-void ERR_unload_strings(int lib, ERR_STRING_DATA str[]);
+int ERR_unload_strings(int lib, ERR_STRING_DATA str[]);
 void ERR_load_ERR_strings(void);
 
 #if OPENSSL_API_COMPAT < 0x10100000L
index 2a07ee6910a6b9877c48ae3ed8cae94c0c88e138..c155e787d5a151033d1d0e9e6d7b6d45dd0bc45c 100644 (file)
@@ -29,6 +29,7 @@
 #include <openssl/bn.h>
 #include <openssl/crypto.h>
 #include "ssl_locl.h"
+#include "internal/thread_once.h"
 
 static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx, int op,
                                          int bits, int nid, void *other,
@@ -37,17 +38,19 @@ static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx, int o
 static CRYPTO_ONCE ssl_x509_store_ctx_once = CRYPTO_ONCE_STATIC_INIT;
 static volatile int ssl_x509_store_ctx_idx = -1;
 
-static void ssl_x509_store_ctx_init(void)
+DEFINE_RUN_ONCE_STATIC(ssl_x509_store_ctx_init)
 {
     ssl_x509_store_ctx_idx = X509_STORE_CTX_get_ex_new_index(0,
                                                 "SSL for verify callback",
                                                 NULL, NULL, NULL);
+    return ssl_x509_store_ctx_idx >= 0;
 }
 
 int SSL_get_ex_data_X509_STORE_CTX_idx(void)
 {
 
-    CRYPTO_THREAD_run_once(&ssl_x509_store_ctx_once, ssl_x509_store_ctx_init);
+    if (!RUN_ONCE(&ssl_x509_store_ctx_once, ssl_x509_store_ctx_init))
+        return -1;
     return ssl_x509_store_ctx_idx;
 }
 
index 2fc4309a911d4d733f32eba06f3d221ac2dcba5c..2a54f9d44d2a04ceba7fa32ca4085e86383cebf8 100644 (file)
@@ -46,6 +46,7 @@
 #include <openssl/engine.h>
 #include <openssl/crypto.h>
 #include "ssl_locl.h"
+#include "internal/thread_once.h"
 
 #define SSL_ENC_DES_IDX         0
 #define SSL_ENC_3DES_IDX        1
@@ -479,7 +480,7 @@ static int sk_comp_cmp(const SSL_COMP *const *a, const SSL_COMP *const *b)
     return ((*a)->id - (*b)->id);
 }
 
-static void do_load_builtin_compressions(void)
+DEFINE_RUN_ONCE_STATIC(do_load_builtin_compressions)
 {
     SSL_COMP *comp = NULL;
     COMP_METHOD *method = COMP_zlib();
@@ -498,12 +499,12 @@ static void do_load_builtin_compressions(void)
         }
     }
     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
+    return 1;
 }
 
 static void load_builtin_compressions(void)
 {
-    CRYPTO_THREAD_run_once(&ssl_load_builtin_comp_once,
-                           do_load_builtin_compressions);
+    RUN_ONCE(&ssl_load_builtin_comp_once, do_load_builtin_compressions);
 }
 #endif
 
index 66525de6c55abd50df4c4a3fd152816c8a42ac58..543da133399675a702b065e840991ad66e182ddc 100644 (file)
@@ -14,6 +14,7 @@
 #include <openssl/evp.h>
 #include <assert.h>
 #include "ssl_locl.h"
+#include "internal/thread_once.h"
 
 static int stopped;
 
@@ -21,7 +22,7 @@ static void ssl_library_stop(void);
 
 static CRYPTO_ONCE ssl_base = CRYPTO_ONCE_STATIC_INIT;
 static int ssl_base_inited = 0;
-static void ossl_init_ssl_base(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_ssl_base)
 {
 #ifdef OPENSSL_INIT_DEBUG
     fprintf(stderr, "OPENSSL_INIT: ossl_init_ssl_base: "
@@ -108,11 +109,12 @@ static void ossl_init_ssl_base(void)
      */
     OPENSSL_atexit(ssl_library_stop);
     ssl_base_inited = 1;
+    return 1;
 }
 
 static CRYPTO_ONCE ssl_strings = CRYPTO_ONCE_STATIC_INIT;
 static int ssl_strings_inited = 0;
-static void ossl_init_load_ssl_strings(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_load_ssl_strings)
 {
     /*
      * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
@@ -126,12 +128,13 @@ static void ossl_init_load_ssl_strings(void)
     ERR_load_SSL_strings();
 #endif
     ssl_strings_inited = 1;
+    return 1;
 }
 
-static void ossl_init_no_load_ssl_strings(void)
+DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_ssl_strings)
 {
     /* Do nothing in this case */
-    return;
+    return 1;
 }
 
 static void ssl_library_stop(void)
@@ -193,17 +196,15 @@ int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
                              | OPENSSL_INIT_ADD_ALL_DIGESTS, settings))
         return 0;
 
-    if (!CRYPTO_THREAD_run_once(&ssl_base, ossl_init_ssl_base))
+    if (!RUN_ONCE(&ssl_base, ossl_init_ssl_base))
         return 0;
 
     if ((opts & OPENSSL_INIT_NO_LOAD_SSL_STRINGS)
-            && !CRYPTO_THREAD_run_once(&ssl_strings,
-                                       ossl_init_no_load_ssl_strings))
+            && !RUN_ONCE(&ssl_strings, ossl_init_no_load_ssl_strings))
         return 0;
 
     if ((opts & OPENSSL_INIT_LOAD_SSL_STRINGS)
-            && !CRYPTO_THREAD_run_once(&ssl_strings,
-                                       ossl_init_load_ssl_strings))
+            && !RUN_ONCE(&ssl_strings, ossl_init_load_ssl_strings))
         return 0;
 
     return 1;