diff options
author | Rich Salz <rsalz@openssl.org> | 2016-01-07 15:06:38 -0500 |
---|---|---|
committer | Rich Salz <rsalz@openssl.org> | 2016-01-07 15:14:18 -0500 |
commit | bbd86bf5424a611cb6b77a3a17fc522931c4dcb8 (patch) | |
tree | cc45d4fb29946c41879e563547a877a70b0c957a /crypto | |
parent | 3cb8c3265f639f8eebf32053457ae6a6d61e2413 (diff) |
mem functions cleanup
Only two macros CRYPTO_MDEBUG and CRYPTO_MDEBUG_ABORT to control this.
If CRYPTO_MDEBUG is not set, #ifdef out the whole debug machinery.
(Thanks to Jakob Bohm for the suggestion!)
Make the "change wrapper functions" be the only paradigm.
Wrote documentation!
Format the 'set func' functions so their paramlists are legible.
Format some multi-line comments.
Remove ability to get/set the "memory debug" functions at runtme.
Remove MemCheck_* and CRYPTO_malloc_debug_init macros.
Add CRYPTO_mem_debug(int flag) function.
Add test/memleaktest.
Rename CRYPTO_malloc_init to OPENSSL_malloc_init; remove needless calls.
Reviewed-by: Richard Levitte <levitte@openssl.org>
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/cms/Makefile | 42 | ||||
-rw-r--r-- | crypto/ct/Makefile | 3 | ||||
-rw-r--r-- | crypto/dh/Makefile | 35 | ||||
-rw-r--r-- | crypto/dsa/Makefile | 15 | ||||
-rw-r--r-- | crypto/ec/Makefile | 16 | ||||
-rw-r--r-- | crypto/engine/eng_dyn.c | 2 | ||||
-rw-r--r-- | crypto/mem.c | 336 | ||||
-rw-r--r-- | crypto/mem_dbg.c | 237 | ||||
-rw-r--r-- | crypto/mem_sec.c | 9 | ||||
-rw-r--r-- | crypto/objects/Makefile | 2 | ||||
-rw-r--r-- | crypto/objects/o_names.c | 16 | ||||
-rw-r--r-- | crypto/rsa/Makefile | 14 | ||||
-rw-r--r-- | crypto/x509/Makefile | 19 |
13 files changed, 272 insertions, 474 deletions
diff --git a/crypto/cms/Makefile b/crypto/cms/Makefile index 832c731114..e18e9ad07a 100644 --- a/crypto/cms/Makefile +++ b/crypto/cms/Makefile @@ -160,15 +160,16 @@ cms_env.o: ../include/internal/asn1_int.h ../include/internal/cryptlib.h cms_env.o: cms_env.c cms_lcl.h cms_err.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h cms_err.o: ../../include/openssl/buffer.h ../../include/openssl/cms.h -cms_err.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h -cms_err.o: ../../include/openssl/ec.h ../../include/openssl/err.h -cms_err.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h -cms_err.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h -cms_err.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h -cms_err.o: ../../include/openssl/ossl_typ.h ../../include/openssl/pkcs7.h -cms_err.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h -cms_err.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h -cms_err.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h +cms_err.o: ../../include/openssl/conf.h ../../include/openssl/crypto.h +cms_err.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h +cms_err.o: ../../include/openssl/err.h ../../include/openssl/evp.h +cms_err.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h +cms_err.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h +cms_err.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h +cms_err.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h +cms_err.o: ../../include/openssl/sha.h ../../include/openssl/stack.h +cms_err.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h +cms_err.o: ../../include/openssl/x509_vfy.h ../../include/openssl/x509v3.h cms_err.o: cms_err.c cms_ess.o: ../../e_os.h ../../include/openssl/asn1.h cms_ess.o: ../../include/openssl/asn1t.h ../../include/openssl/bio.h @@ -188,17 +189,18 @@ cms_ess.o: ../../include/openssl/x509v3.h ../include/internal/cryptlib.h cms_ess.o: cms_ess.c cms_lcl.h cms_io.o: ../../include/openssl/asn1.h ../../include/openssl/asn1t.h cms_io.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h -cms_io.o: ../../include/openssl/cms.h ../../include/openssl/crypto.h -cms_io.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h -cms_io.o: ../../include/openssl/err.h ../../include/openssl/evp.h -cms_io.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h -cms_io.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h -cms_io.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h -cms_io.o: ../../include/openssl/pem.h ../../include/openssl/pem2.h -cms_io.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h -cms_io.o: ../../include/openssl/sha.h ../../include/openssl/stack.h -cms_io.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h -cms_io.o: ../../include/openssl/x509_vfy.h cms_io.c cms_lcl.h +cms_io.o: ../../include/openssl/cms.h ../../include/openssl/conf.h +cms_io.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h +cms_io.o: ../../include/openssl/ec.h ../../include/openssl/err.h +cms_io.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h +cms_io.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h +cms_io.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h +cms_io.o: ../../include/openssl/ossl_typ.h ../../include/openssl/pem.h +cms_io.o: ../../include/openssl/pem2.h ../../include/openssl/pkcs7.h +cms_io.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h +cms_io.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h +cms_io.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h +cms_io.o: ../../include/openssl/x509v3.h cms_io.c cms_lcl.h cms_kari.o: ../../e_os.h ../../include/openssl/aes.h cms_kari.o: ../../include/openssl/asn1.h ../../include/openssl/asn1t.h cms_kari.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h diff --git a/crypto/ct/Makefile b/crypto/ct/Makefile index 49771d9681..e4fb65b77c 100644 --- a/crypto/ct/Makefile +++ b/crypto/ct/Makefile @@ -70,7 +70,8 @@ ct_err.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h ct_err.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h ct_err.o: ../../include/openssl/symhacks.h ../include/internal/ct_int.h ct_err.o: ct_err.c -ct_lib.o: ../../e_os.h ../../include/openssl/asn1.h +ct_lib.o: ../../e_os.h ../../include/internal/dane.h +ct_lib.o: ../../include/internal/numbers.h ../../include/openssl/asn1.h ct_lib.o: ../../include/openssl/async.h ../../include/openssl/bio.h ct_lib.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h ct_lib.o: ../../include/openssl/comp.h ../../include/openssl/crypto.h diff --git a/crypto/dh/Makefile b/crypto/dh/Makefile index 757ccf01fe..86e3c2c6b1 100644 --- a/crypto/dh/Makefile +++ b/crypto/dh/Makefile @@ -65,17 +65,19 @@ clean: dh_ameth.o: ../../e_os.h ../../include/openssl/asn1.h dh_ameth.o: ../../include/openssl/bio.h ../../include/openssl/bn.h dh_ameth.o: ../../include/openssl/buffer.h ../../include/openssl/cms.h -dh_ameth.o: ../../include/openssl/crypto.h ../../include/openssl/dh.h -dh_ameth.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h -dh_ameth.o: ../../include/openssl/err.h ../../include/openssl/evp.h -dh_ameth.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h -dh_ameth.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h +dh_ameth.o: ../../include/openssl/conf.h ../../include/openssl/crypto.h +dh_ameth.o: ../../include/openssl/dh.h ../../include/openssl/e_os2.h +dh_ameth.o: ../../include/openssl/ec.h ../../include/openssl/err.h +dh_ameth.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h +dh_ameth.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h +dh_ameth.o: ../../include/openssl/opensslconf.h dh_ameth.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h dh_ameth.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h dh_ameth.o: ../../include/openssl/sha.h ../../include/openssl/stack.h dh_ameth.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h -dh_ameth.o: ../../include/openssl/x509_vfy.h ../include/internal/asn1_int.h -dh_ameth.o: ../include/internal/cryptlib.h dh_ameth.c +dh_ameth.o: ../../include/openssl/x509_vfy.h ../../include/openssl/x509v3.h +dh_ameth.o: ../include/internal/asn1_int.h ../include/internal/cryptlib.h +dh_ameth.o: dh_ameth.c dh_asn1.o: ../../e_os.h ../../include/openssl/asn1.h dh_asn1.o: ../../include/openssl/asn1t.h ../../include/openssl/bio.h dh_asn1.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h @@ -121,15 +123,16 @@ dh_gen.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h dh_gen.o: ../include/internal/cryptlib.h dh_gen.c dh_kdf.o: ../../e_os.h ../../include/openssl/asn1.h ../../include/openssl/bio.h dh_kdf.o: ../../include/openssl/buffer.h ../../include/openssl/cms.h -dh_kdf.o: ../../include/openssl/crypto.h ../../include/openssl/dh.h -dh_kdf.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h -dh_kdf.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h -dh_kdf.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h -dh_kdf.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h -dh_kdf.o: ../../include/openssl/ossl_typ.h ../../include/openssl/pkcs7.h -dh_kdf.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h -dh_kdf.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h -dh_kdf.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h +dh_kdf.o: ../../include/openssl/conf.h ../../include/openssl/crypto.h +dh_kdf.o: ../../include/openssl/dh.h ../../include/openssl/e_os2.h +dh_kdf.o: ../../include/openssl/ec.h ../../include/openssl/evp.h +dh_kdf.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h +dh_kdf.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h +dh_kdf.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h +dh_kdf.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h +dh_kdf.o: ../../include/openssl/sha.h ../../include/openssl/stack.h +dh_kdf.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h +dh_kdf.o: ../../include/openssl/x509_vfy.h ../../include/openssl/x509v3.h dh_kdf.o: dh_kdf.c dh_key.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h dh_key.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h diff --git a/crypto/dsa/Makefile b/crypto/dsa/Makefile index 5f4fb520e1..796d8a3fdc 100644 --- a/crypto/dsa/Makefile +++ b/crypto/dsa/Makefile @@ -65,18 +65,19 @@ clean: dsa_ameth.o: ../../e_os.h ../../include/openssl/asn1.h dsa_ameth.o: ../../include/openssl/bio.h ../../include/openssl/bn.h dsa_ameth.o: ../../include/openssl/buffer.h ../../include/openssl/cms.h -dsa_ameth.o: ../../include/openssl/crypto.h ../../include/openssl/dsa.h -dsa_ameth.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h -dsa_ameth.o: ../../include/openssl/err.h ../../include/openssl/evp.h -dsa_ameth.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h -dsa_ameth.o: ../../include/openssl/objects.h +dsa_ameth.o: ../../include/openssl/conf.h ../../include/openssl/crypto.h +dsa_ameth.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h +dsa_ameth.o: ../../include/openssl/ec.h ../../include/openssl/err.h +dsa_ameth.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h +dsa_ameth.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h dsa_ameth.o: ../../include/openssl/opensslconf.h dsa_ameth.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h dsa_ameth.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h dsa_ameth.o: ../../include/openssl/sha.h ../../include/openssl/stack.h dsa_ameth.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h -dsa_ameth.o: ../../include/openssl/x509_vfy.h ../include/internal/asn1_int.h -dsa_ameth.o: ../include/internal/cryptlib.h dsa_ameth.c +dsa_ameth.o: ../../include/openssl/x509_vfy.h ../../include/openssl/x509v3.h +dsa_ameth.o: ../include/internal/asn1_int.h ../include/internal/cryptlib.h +dsa_ameth.o: dsa_ameth.c dsa_asn1.o: ../../e_os.h ../../include/openssl/asn1.h dsa_asn1.o: ../../include/openssl/asn1t.h ../../include/openssl/bio.h dsa_asn1.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h diff --git a/crypto/ec/Makefile b/crypto/ec/Makefile index 7933898562..5abe958197 100644 --- a/crypto/ec/Makefile +++ b/crypto/ec/Makefile @@ -118,17 +118,19 @@ ec2_smpl.o: ec2_smpl.c ec_lcl.h ec_ameth.o: ../../e_os.h ../../include/openssl/asn1.h ec_ameth.o: ../../include/openssl/asn1t.h ../../include/openssl/bio.h ec_ameth.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h -ec_ameth.o: ../../include/openssl/cms.h ../../include/openssl/crypto.h -ec_ameth.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h -ec_ameth.o: ../../include/openssl/err.h ../../include/openssl/evp.h -ec_ameth.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h -ec_ameth.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h +ec_ameth.o: ../../include/openssl/cms.h ../../include/openssl/conf.h +ec_ameth.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h +ec_ameth.o: ../../include/openssl/ec.h ../../include/openssl/err.h +ec_ameth.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h +ec_ameth.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h +ec_ameth.o: ../../include/openssl/opensslconf.h ec_ameth.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h ec_ameth.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h ec_ameth.o: ../../include/openssl/sha.h ../../include/openssl/stack.h ec_ameth.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h -ec_ameth.o: ../../include/openssl/x509_vfy.h ../include/internal/asn1_int.h -ec_ameth.o: ../include/internal/cryptlib.h ec_ameth.c +ec_ameth.o: ../../include/openssl/x509_vfy.h ../../include/openssl/x509v3.h +ec_ameth.o: ../include/internal/asn1_int.h ../include/internal/cryptlib.h +ec_ameth.o: ec_ameth.c ec_asn1.o: ../../include/openssl/asn1.h ../../include/openssl/asn1t.h ec_asn1.o: ../../include/openssl/bio.h ../../include/openssl/bn.h ec_asn1.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h diff --git a/crypto/engine/eng_dyn.c b/crypto/engine/eng_dyn.c index 23f01cfc35..d19cab2ecf 100644 --- a/crypto/engine/eng_dyn.c +++ b/crypto/engine/eng_dyn.c @@ -506,8 +506,6 @@ static int dynamic_load(ENGINE *e, dynamic_data_ctx *ctx) * would also increase opaqueness. */ fns.static_state = ENGINE_get_static_state(); - CRYPTO_get_mem_functions(&fns.mem_fns.malloc_cb, - &fns.mem_fns.realloc_cb, &fns.mem_fns.free_cb); fns.lock_fns.lock_locking_cb = CRYPTO_get_locking_callback(); fns.lock_fns.lock_add_lock_cb = CRYPTO_get_add_lock_callback(); fns.lock_fns.dynlock_create_cb = CRYPTO_get_dynlock_create_callback(); diff --git a/crypto/mem.c b/crypto/mem.c index 939ad69775..ec9efea716 100644 --- a/crypto/mem.c +++ b/crypto/mem.c @@ -58,222 +58,63 @@ #include <stdio.h> #include <stdlib.h> +#include <limits.h> #include <openssl/crypto.h> #include "internal/cryptlib.h" -static int allow_customize = 1; /* we provide flexible functions for */ -static int allow_customize_debug = 1; /* exchanging memory-related functions - * at run-time, but this must be done - * before any blocks are actually - * allocated; or we'll run into huge - * problems when malloc/free pairs - * don't match etc. */ - /* * the following pointers may be changed as long as 'allow_customize' is set */ +static int allow_customize = 1; -static void *(*malloc_func) (size_t) = malloc; -static void *default_malloc_ex(size_t num, const char *file, int line) -{ - return malloc_func(num); -} - -static void *(*malloc_ex_func) (size_t, const char *file, int line) - = default_malloc_ex; - -static void *(*realloc_func) (void *, size_t) = realloc; -static void *default_realloc_ex(void *str, size_t num, - const char *file, int line) -{ - return realloc_func(str, num); -} - -static void *(*realloc_ex_func) (void *, size_t, const char *file, int line) - = default_realloc_ex; - -static void (*free_func) (void *) = free; - -static void *(*malloc_secure_func)(size_t) = malloc; -static void *default_malloc_secure_ex(size_t num, const char *file, int line) -{ - return malloc_secure_func(num); -} -static void *(*malloc_secure_ex_func)(size_t, const char *file, int line) - = default_malloc_secure_ex; -static void (*free_secure_func)(void *) = free; +static void *(*malloc_wrapper)(size_t, const char *, int) + = CRYPTO_malloc; +static void *(*realloc_wrapper)(void *, size_t, const char *, int) + = CRYPTO_realloc; +static void (*free_wrapper)(void *) + = CRYPTO_free; -/* may be changed as long as 'allow_customize_debug' is set */ -/* XXX use correct function pointer types */ #ifdef CRYPTO_MDEBUG -/* use default functions from mem_dbg.c */ -static void (*malloc_debug_func) (void *, size_t, const char *, int, int) - = CRYPTO_dbg_malloc; -static void (*realloc_debug_func) (void *, void *, size_t, const char *, int, - int) - = CRYPTO_dbg_realloc; -static void (*free_debug_func) (void *, int) = CRYPTO_dbg_free; -static void (*set_debug_options_func) (long) = CRYPTO_dbg_set_options; -static long (*get_debug_options_func) (void) = CRYPTO_dbg_get_options; +static int call_malloc_debug = 1; #else -/* - * applications can use CRYPTO_malloc_debug_init() to select above case at - * run-time - */ -static void (*malloc_debug_func) (void *, size_t, const char *, int, int) = NULL; -static void (*realloc_debug_func) (void *, void *, size_t, const char *, int, - int) - = NULL; -static void (*free_debug_func) (void *, int) = NULL; -static void (*set_debug_options_func) (long) = NULL; -static long (*get_debug_options_func) (void) = NULL; +static int call_malloc_debug = 0; #endif -int CRYPTO_set_mem_functions(void *(*m) (size_t), void *(*r) (void *, size_t), - void (*f) (void *)) -{ - /* Dummy call just to ensure OPENSSL_init() gets linked in */ - OPENSSL_init(); - if (!allow_customize) - return 0; - if ((m == 0) || (r == 0) || (f == 0)) - return 0; - malloc_func = m; - malloc_ex_func = default_malloc_ex; - realloc_func = r; - realloc_ex_func = default_realloc_ex; - free_func = f; - /* If user wants to intercept the secure or locked functions, do it - * after the basic functions. */ - malloc_secure_func = m; - malloc_secure_ex_func = default_malloc_secure_ex; - free_secure_func = f; - return 1; -} - -int CRYPTO_set_mem_ex_functions(void *(*m) (size_t, const char *, int), - void *(*r) (void *, size_t, const char *, - int), void (*f) (void *)) -{ - if (!allow_customize) - return 0; - if ((m == 0) || (r == 0) || (f == 0)) - return 0; - malloc_func = 0; - malloc_ex_func = m; - realloc_func = 0; - realloc_ex_func = r; - free_func = f; - malloc_secure_func = 0; - malloc_secure_ex_func = m; - free_secure_func = f; - return 1; -} - -int CRYPTO_set_secure_mem_functions(void *(*m)(size_t), void (*f)(void *)) +int CRYPTO_set_mem_functions( + void *(*m)(size_t, const char *, int), + void *(*r)(void *, size_t, const char *, int), + void (*f)(void *)) { - /* Dummy call just to ensure OPENSSL_init() gets linked in */ - OPENSSL_init(); if (!allow_customize) return 0; - if ((m == 0) || (f == 0)) - return 0; - malloc_secure_func = m; - malloc_secure_ex_func = default_malloc_secure_ex; - free_secure_func = f; + if (m) + malloc_wrapper = m; + if (r) + realloc_wrapper = r; + if (f) + free_wrapper = f; return 1; } -int CRYPTO_set_secure_mem_ex_functions(void *(*m)(size_t, const char *, int), - void (*f)(void *)) +int CRYPTO_set_mem_debug(int flag) { if (!allow_customize) return 0; - if ((m == NULL) || (f == NULL)) - return 0; - malloc_secure_func = 0; - malloc_secure_ex_func = m; - free_secure_func = f; - return 1; -} - -int CRYPTO_set_mem_debug_functions(void (*m) (void *, size_t, - const char *, int, int), - void (*r) (void *, void *, size_t, - const char *, int, int), - void (*f) (void *, int), void (*so) (long), - long (*go) (void)) -{ - if (!allow_customize_debug) - return 0; - malloc_debug_func = m; - realloc_debug_func = r; - free_debug_func = f; - set_debug_options_func = so; - get_debug_options_func = go; + call_malloc_debug = flag; return 1; } -void CRYPTO_get_mem_functions(void *(**m) (size_t), - void *(**r) (void *, size_t), - void (**f) (void *)) +void CRYPTO_get_mem_functions( + void *(**m)(size_t, const char *, int), + void *(**r)(void *, size_t, const char *, int), + void (**f)(void *)) { if (m != NULL) - *m = (malloc_ex_func == default_malloc_ex) ? malloc_func : 0; + *m = malloc_wrapper; if (r != NULL) - *r = (realloc_ex_func == default_realloc_ex) ? realloc_func : 0; - if (f != NULL) - *f = free_func; -} - -void CRYPTO_get_mem_ex_functions(void *(**m) (size_t, const char *, int), - void *(**r) (void *, size_t, const char *, - int), void (**f) (void *)) -{ - if (m != NULL) - *m = (malloc_ex_func != default_malloc_ex) ? malloc_ex_func : 0; - if (r != NULL) - *r = (realloc_ex_func != default_realloc_ex) ? realloc_ex_func : 0; - if (f != NULL) - *f = free_func; -} - -void CRYPTO_get_secure_mem_functions(void *(**m)(size_t), void (**f)(void *)) -{ - if (m != NULL) - *m = (malloc_secure_ex_func == default_malloc_secure_ex) ? - malloc_secure_func : 0; + *r = realloc_wrapper; if (f != NULL) - *f=free_secure_func; -} - -void CRYPTO_get_secure_mem_ex_functions(void *(**m)(size_t,const char *,int), - void (**f)(void *)) -{ - if (m != NULL) - *m = (malloc_secure_ex_func != default_malloc_secure_ex) ? - malloc_secure_ex_func : 0; - if (f != NULL) - *f=free_secure_func; -} - -void CRYPTO_get_mem_debug_functions(void (**m) (void *, size_t, - const char *, int, int), - void (**r) (void *, void *, size_t, - const char *, int, int), - void (**f) (void *, int), - void (**so) (long), long (**go) (void)) -{ - if (m != NULL) - *m = malloc_debug_func; - if (r != NULL) - *r = realloc_debug_func; - if (f != NULL) - *f = free_debug_func; - if (so != NULL) - *so = set_debug_options_func; - if (go != NULL) - *go = get_debug_options_func; + *f = free_wrapper; } void *CRYPTO_malloc(size_t num, const char *file, int line) @@ -283,16 +124,20 @@ void *CRYPTO_malloc(size_t num, const char *file, int line) if (num <= 0) return NULL; - if (allow_customize) - allow_customize = 0; - if (malloc_debug_func != NULL) { - if (allow_customize_debug) - allow_customize_debug = 0; - malloc_debug_func(NULL, num, file, line, 0); + allow_customize = 0; +#ifdef CRYPTO_MDEBUG + if (call_malloc_debug) { + CRYPTO_mem_debug_malloc(NULL, num, 0, file, line); + ret = malloc(num); + CRYPTO_mem_debug_malloc(ret, num, 1, file, line); + } else { + ret = malloc(num); } - ret = malloc_ex_func(num, file, line); - if (malloc_debug_func != NULL) - malloc_debug_func(ret, num, file, line, 1); +#else + (void)file; + (void)line; + ret = malloc(num); +#endif #ifndef OPENSSL_CPUID_OBJ /* @@ -320,21 +165,29 @@ void *CRYPTO_zalloc(size_t num, const char *file, int line) void *CRYPTO_realloc(void *str, size_t num, const char *file, int line) { - void *ret = NULL; - if (str == NULL) return CRYPTO_malloc(num, file, line); - if (num <= 0) + if (num == 0) { + CRYPTO_free(str); return NULL; + } - if (realloc_debug_func != NULL) - realloc_debug_func(str, NULL, num, file, line, 0); - ret = realloc_ex_func(str, num, file, line); - if (realloc_debug_func != NULL) - realloc_debug_func(str, ret, num, file, line, 1); + allow_customize = 0; +#ifdef CRYPTO_MDEBUG + if (call_malloc_debug) { + void *ret; + CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line); + ret = realloc(str, num); + CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line); + return ret; + } +#else + (void)file; + (void)line; +#endif + return realloc(str, num); - return ret; } void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num, @@ -345,56 +198,59 @@ void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num, if (str == NULL) return CRYPTO_malloc(num, file, line); - if (num <= 0) + if (num == 0) { + CRYPTO_clear_free(str, old_len); return NULL; + } - /* - * We don't support shrinking the buffer. Note the memcpy that copies - * |old_len| bytes to the new buffer, below. - */ - if (num < old_len) - return NULL; + /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */ + if (num < old_len) { + memset((char*)str + num, 0, old_len - num); + return str; + } - if (realloc_debug_func != NULL) - realloc_debug_func(str, NULL, num, file, line, 0); - ret = malloc_ex_func(num, file, line); - if (ret) { - memcpy(ret, str, old_len); - OPENSSL_clear_free(str, old_len); + /* Allocate new memory. Call malloc and do a copy, so that we can + * cleanse the old buffer. */ +#ifdef CRYPTO_MDEBUG + if (call_malloc_debug) { + CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line); + ret = malloc(num); + CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line); + } else { + ret = malloc(num); } - if (realloc_debug_func != NULL) - realloc_debug_func(str, ret, num, file, line, 1); +#else + (void)file; + (void)line; + ret = malloc(num); +#endif + if (ret) + memcpy(ret, str, old_len); + CRYPTO_clear_free(str, old_len); return ret; } void CRYPTO_free(void *str) { - if (free_debug_func != NULL) - free_debug_func(str, 0); - free_func(str); - if (free_debug_func != NULL) - free_debug_func(NULL, 1); +#ifdef CRYPTO_MDEBUG + if (call_malloc_debug) { + CRYPTO_mem_debug_free(str, 0); + free(str); + CRYPTO_mem_debug_free(str, 1); + } else { + free(str); + } +#else + free(str); +#endif } void CRYPTO_clear_free(void *str, size_t num) { - if (!str) + if (str == NULL) return; if (num) OPENSSL_cleanse(str, num); CRYPTO_free(str); } - -void CRYPTO_set_mem_debug_options(long bits) -{ - if (set_debug_options_func != NULL) - set_debug_options_func(bits); -} - -long CRYPTO_get_mem_debug_options(void) -{ - if (get_debug_options_func != NULL) - return get_debug_options_func(); - return 0; -} diff --git a/crypto/mem_dbg.c b/crypto/mem_dbg.c index 924652a6cb..2132b7da5c 100644 --- a/crypto/mem_dbg.c +++ b/crypto/mem_dbg.c @@ -121,7 +121,6 @@ # include <execinfo.h> #endif -static int mh_mode = CRYPTO_MEM_CHECK_OFF; /* * The state changes to CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE when * the application asks for it (usually after library initialisation for @@ -133,14 +132,15 @@ static int mh_mode = CRYPTO_MEM_CHECK_OFF; * checking temporarily. State CRYPTO_MEM_CHECK_ENABLE without ..._ON makes * no sense whatsoever. */ +static int mh_mode = CRYPTO_MEM_CHECK_OFF; +#ifdef CRYPTO_MDEBUG static unsigned long order = 0; /* number of memory requests */ DECLARE_LHASH_OF(MEM); static LHASH_OF(MEM) *mh = NULL; /* hash-table of memory requests (address as * key); access requires MALLOC2 lock */ -typedef struct app_mem_info_st /*- * For application-defined information (static C-string `info') * to be displayed in memory leak list. @@ -148,7 +148,7 @@ typedef struct app_mem_info_st * OPENSSL_mem_debug_push("...") to push an entry, * OPENSSL_mem_debug_pop() to pop an entry, */ -{ +typedef struct app_mem_info_st { CRYPTO_THREADID threadid; const char *file; int line; @@ -157,18 +157,16 @@ typedef struct app_mem_info_st int references; } APP_INFO; -static void app_info_free(APP_INFO *); - +/* + * hash-table with those app_mem_info_st's that are at the + * top of their thread's stack (with `thread' as key); access requires + * MALLOC2 lock + */ DECLARE_LHASH_OF(APP_INFO); -static LHASH_OF(APP_INFO) *amih = NULL; /* hash-table with those - * app_mem_info_st's that are at the - * top of their thread's stack (with - * `thread' as key); access requires - * MALLOC2 lock */ +static LHASH_OF(APP_INFO) *amih = NULL; -typedef struct mem_st /* memory-block description */ -{ +typedef struct mem_st { void *addr; int num; const char *file; @@ -183,17 +181,8 @@ typedef struct mem_st #endif } MEM; -static long options = /* extra information to be recorded */ -#if defined(CRYPTO_MDEBUG_TIME) || defined(CRYPTO_MDEBUG_ALL) - V_CRYPTO_MDEBUG_TIME | -#endif -#if defined(CRYPTO_MDEBUG_THREAD) || defined(CRYPTO_MDEBUG_ALL) - V_CRYPTO_MDEBUG_THREAD | -#endif - 0; - -static unsigned int num_disable = 0; /* num_disable > 0 iff mh_mode == - * CRYPTO_MEM_CHECK_ON (w/o ..._ENABLE) */ +/* num_disable > 0 iff mh_mode == CRYPTO_MEM_CHECK_ON (w/o ..._ENABLE) */ +static unsigned int num_disable = 0; /* * Valid iff num_disable > 0. CRYPTO_LOCK_MALLOC2 is locked exactly in this @@ -210,29 +199,32 @@ static void app_info_free(APP_INFO *inf) OPENSSL_free(inf); } } +#endif int CRYPTO_mem_ctrl(int mode) { +#ifndef CRYPTO_MDEBUG + return mode - mode; +#else int ret = mh_mode; CRYPTO_w_lock(CRYPTO_LOCK_MALLOC); switch (mode) { - /* - * for applications (not to be called while multiple threads use the - * library): - */ - case CRYPTO_MEM_CHECK_ON: /* aka MemCheck_start() */ + default: + break; + + case CRYPTO_MEM_CHECK_ON: mh_mode = CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE; num_disable = 0; break; - case CRYPTO_MEM_CHECK_OFF: /* aka MemCheck_stop() */ + + case CRYPTO_MEM_CHECK_OFF: mh_mode = 0; - num_disable = 0; /* should be true *before* MemCheck_stop is - * used, or there'll be a lot of confusion */ + num_disable = 0; break; - /* switch off temporarily (for library-internal use): */ - case CRYPTO_MEM_CHECK_DISABLE: /* aka MemCheck_off() */ + /* switch off temporarily (for library-internal use): */ + case CRYPTO_MEM_CHECK_DISABLE: if (mh_mode & CRYPTO_MEM_CHECK_ON) { CRYPTO_THREADID cur; CRYPTO_THREADID_current(&cur); @@ -262,7 +254,8 @@ int CRYPTO_mem_ctrl(int mode) num_disable++; } break; - case CRYPTO_MEM_CHECK_ENABLE: /* aka MemCheck_on() */ + + case CRYPTO_MEM_CHECK_ENABLE: if (mh_mode & CRYPTO_MEM_CHECK_ON) { if (num_disable) { /* always true, or something is going wrong */ num_disable--; @@ -273,15 +266,15 @@ int CRYPTO_mem_ctrl(int mode) } } break; - - default: - break; } CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC); return (ret); +#endif } -int CRYPTO_is_mem_check_on(void) +#ifdef CRYPTO_MDEBUG + +static int mem_check_on(void) { int ret = 0; @@ -298,16 +291,6 @@ int CRYPTO_is_mem_check_on(void) return (ret); } -void CRYPTO_dbg_set_options(long bits) -{ - options = bits; -} - -long CRYPTO_dbg_get_options(void) -{ - return options; -} - static int mem_cmp(const MEM *a, const MEM *b) { #ifdef _WIN64 @@ -388,17 +371,14 @@ int CRYPTO_mem_debug_push(const char *info, const char *file, int line) APP_INFO *ami, *amim; int ret = 0; - if (is_MemCheck_on()) { - MemCheck_off(); /* obtain MALLOC2 lock */ + if (mem_check_on()) { + CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE); - if ((ami = OPENSSL_malloc(sizeof(*ami))) == NULL) { - ret = 0; + if ((ami = OPENSSL_malloc(sizeof(*ami))) == NULL) goto err; - } if (amih == NULL) { if ((amih = lh_APP_INFO_new()) == NULL) { OPENSSL_free(ami); - ret = 0; goto err; } } |