mem.c revision 68651
155714Skris/* crypto/mem.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 855714Skris * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1555714Skris * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 2255714Skris * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4055714Skris * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 5255714Skris * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#include <stdio.h> 6055714Skris#include <stdlib.h> 6155714Skris#include <openssl/crypto.h> 6255714Skris#include "cryptlib.h" 6355714Skris 6455714Skris 6559191Skrisstatic int allow_customize = 1; /* we provide flexible functions for */ 6659191Skrisstatic int allow_customize_debug = 1;/* exchanging memory-related functions at 6759191Skris * run-time, but this must be done 6859191Skris * before any blocks are actually 6959191Skris * allocated; or we'll run into huge 7059191Skris * problems when malloc/free pairs 7159191Skris * don't match etc. */ 7255714Skris 7359191Skris/* may be changed as long as `allow_customize' is set */ 7459191Skrisstatic void *(*malloc_locked_func)(size_t) = malloc; 7559191Skrisstatic void (*free_locked_func)(void *) = free; 7659191Skrisstatic void *(*malloc_func)(size_t) = malloc; 7759191Skrisstatic void *(*realloc_func)(void *, size_t)= realloc; 7859191Skrisstatic void (*free_func)(void *) = free; 7955714Skris 8059191Skris/* may be changed as long as `allow_customize_debug' is set */ 8159191Skris/* XXX use correct function pointer types */ 8259191Skris#ifdef CRYPTO_MDEBUG 8368651Skris/* use default functions from mem_dbg.c */ 8468651Skrisstatic void (*malloc_debug_func)(void *,int,const char *,int,int) 8568651Skris = CRYPTO_dbg_malloc; 8668651Skrisstatic void (*realloc_debug_func)(void *,void *,int,const char *,int,int) 8768651Skris = CRYPTO_dbg_realloc; 8868651Skrisstatic void (*free_debug_func)(void *,int) = CRYPTO_dbg_free; 8968651Skrisstatic void (*set_debug_options_func)(long) = CRYPTO_dbg_set_options; 9068651Skrisstatic long (*get_debug_options_func)(void) = CRYPTO_dbg_get_options; 9159191Skris#else 9268651Skris/* applications can use CRYPTO_malloc_debug_init() to select above case 9368651Skris * at run-time */ 9468651Skrisstatic void (*malloc_debug_func)(void *,int,const char *,int,int) = NULL; 9568651Skrisstatic void (*realloc_debug_func)(void *,void *,int,const char *,int,int) 9668651Skris = NULL; 9768651Skrisstatic void (*free_debug_func)(void *,int) = NULL; 9868651Skrisstatic void (*set_debug_options_func)(long) = NULL; 9968651Skrisstatic long (*get_debug_options_func)(void) = NULL; 10055714Skris#endif 10155714Skris 10255714Skris 10359191Skrisint CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t), 10459191Skris void (*f)(void *)) 10555714Skris { 10659191Skris if (!allow_customize) 10759191Skris return 0; 10859191Skris if ((m == NULL) || (r == NULL) || (f == NULL)) 10959191Skris return 0; 11055714Skris malloc_func=m; 11155714Skris realloc_func=r; 11255714Skris free_func=f; 11355714Skris malloc_locked_func=m; 11455714Skris free_locked_func=f; 11559191Skris return 1; 11655714Skris } 11755714Skris 11859191Skrisint CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *)) 11955714Skris { 12059191Skris if (!allow_customize) 12159191Skris return 0; 12259191Skris if ((m == NULL) || (f == NULL)) 12359191Skris return 0; 12455714Skris malloc_locked_func=m; 12555714Skris free_locked_func=f; 12659191Skris return 1; 12755714Skris } 12855714Skris 12968651Skrisint CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int), 13068651Skris void (*r)(void *,void *,int,const char *,int,int), 13168651Skris void (*f)(void *,int), 13268651Skris void (*so)(long), 13368651Skris long (*go)(void)) 13455714Skris { 13559191Skris if (!allow_customize_debug) 13659191Skris return 0; 13759191Skris malloc_debug_func=m; 13859191Skris realloc_debug_func=r; 13959191Skris free_debug_func=f; 14059191Skris set_debug_options_func=so; 14159191Skris get_debug_options_func=go; 14259191Skris return 1; 14359191Skris } 14459191Skris 14559191Skrisvoid CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t), 14659191Skris void (**f)(void *)) 14759191Skris { 14855714Skris if (m != NULL) *m=malloc_func; 14955714Skris if (r != NULL) *r=realloc_func; 15055714Skris if (f != NULL) *f=free_func; 15155714Skris } 15255714Skris 15359191Skrisvoid CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *)) 15455714Skris { 15555714Skris if (m != NULL) *m=malloc_locked_func; 15655714Skris if (f != NULL) *f=free_locked_func; 15755714Skris } 15855714Skris 15968651Skrisvoid CRYPTO_get_mem_debug_functions(void (**m)(void *,int,const char *,int,int), 16068651Skris void (**r)(void *,void *,int,const char *,int,int), 16168651Skris void (**f)(void *,int), 16268651Skris void (**so)(long), 16368651Skris long (**go)(void)) 16455714Skris { 16559191Skris if (m != NULL) *m=malloc_debug_func; 16659191Skris if (r != NULL) *r=realloc_debug_func; 16759191Skris if (f != NULL) *f=free_debug_func; 16859191Skris if (so != NULL) *so=set_debug_options_func; 16959191Skris if (go != NULL) *go=get_debug_options_func; 17055714Skris } 17155714Skris 17255714Skris 17359191Skrisvoid *CRYPTO_malloc_locked(int num, const char *file, int line) 17455714Skris { 17568651Skris void *ret = NULL; 17655714Skris 17759191Skris allow_customize = 0; 17859191Skris if (malloc_debug_func != NULL) 17959191Skris { 18059191Skris allow_customize_debug = 0; 18159191Skris malloc_debug_func(NULL, num, file, line, 0); 18259191Skris } 18359191Skris ret = malloc_locked_func(num); 18459191Skris#ifdef LEVITTE_DEBUG 18559191Skris fprintf(stderr, "LEVITTE_DEBUG: > 0x%p (%d)\n", ret, num); 18659191Skris#endif 18759191Skris if (malloc_debug_func != NULL) 18859191Skris malloc_debug_func(ret, num, file, line, 1); 18955714Skris 19059191Skris return ret; 19155714Skris } 19255714Skris 19359191Skrisvoid CRYPTO_free_locked(void *str) 19455714Skris { 19559191Skris if (free_debug_func != NULL) 19659191Skris free_debug_func(str, 0); 19759191Skris#ifdef LEVITTE_DEBUG 19859191Skris fprintf(stderr, "LEVITTE_DEBUG: < 0x%p\n", str); 19955714Skris#endif 20059191Skris free_locked_func(str); 20159191Skris if (free_debug_func != NULL) 20259191Skris free_debug_func(NULL, 1); 20355714Skris } 20455714Skris 20559191Skrisvoid *CRYPTO_malloc(int num, const char *file, int line) 20655714Skris { 20768651Skris void *ret = NULL; 20855714Skris 20959191Skris allow_customize = 0; 21059191Skris if (malloc_debug_func != NULL) 21155714Skris { 21259191Skris allow_customize_debug = 0; 21359191Skris malloc_debug_func(NULL, num, file, line, 0); 21455714Skris } 21559191Skris ret = malloc_func(num); 21659191Skris#ifdef LEVITTE_DEBUG 21759191Skris fprintf(stderr, "LEVITTE_DEBUG: > 0x%p (%d)\n", ret, num); 21859191Skris#endif 21959191Skris if (malloc_debug_func != NULL) 22059191Skris malloc_debug_func(ret, num, file, line, 1); 22155714Skris 22259191Skris return ret; 22355714Skris } 22455714Skris 22559191Skrisvoid *CRYPTO_realloc(void *str, int num, const char *file, int line) 22655714Skris { 22768651Skris void *ret = NULL; 22855714Skris 22959191Skris if (realloc_debug_func != NULL) 23059191Skris realloc_debug_func(str, NULL, num, file, line, 0); 23159191Skris ret = realloc_func(str,num); 23259191Skris#ifdef LEVITTE_DEBUG 23359191Skris fprintf(stderr, "LEVITTE_DEBUG: | 0x%p -> 0x%p (%d)\n", str, ret, num); 23455714Skris#endif 23559191Skris if (realloc_debug_func != NULL) 23659191Skris realloc_debug_func(str, ret, num, file, line, 1); 23755714Skris 23859191Skris return ret; 23955714Skris } 24055714Skris 24159191Skrisvoid CRYPTO_free(void *str) 24255714Skris { 24359191Skris if (free_debug_func != NULL) 24459191Skris free_debug_func(str, 0); 24559191Skris#ifdef LEVITTE_DEBUG 24659191Skris fprintf(stderr, "LEVITTE_DEBUG: < 0x%p\n", str); 24755714Skris#endif 24859191Skris free_func(str); 24959191Skris if (free_debug_func != NULL) 25059191Skris free_debug_func(NULL, 1); 25155714Skris } 25255714Skris 25359191Skrisvoid *CRYPTO_remalloc(void *a, int num, const char *file, int line) 25455714Skris { 25568651Skris if (a != NULL) OPENSSL_free(a); 25668651Skris a=(char *)OPENSSL_malloc(num); 25759191Skris return(a); 25855714Skris } 25955714Skris 26059191Skris 26159191Skrisvoid CRYPTO_set_mem_debug_options(long bits) 26255714Skris { 26359191Skris if (set_debug_options_func != NULL) 26459191Skris set_debug_options_func(bits); 26555714Skris } 26655714Skris 26759191Skrislong CRYPTO_get_mem_debug_options(void) 26855714Skris { 26959191Skris if (get_debug_options_func != NULL) 27059191Skris return get_debug_options_func(); 27159191Skris return 0; 27255714Skris } 273