As response to a user request to be able to use external memory

handling routines that need file name and line number information,
I've added a call level to our memory handling routines to allow that
kind of hooking.
This commit is contained in:
Richard Levitte 2001-01-10 13:14:58 +00:00
parent cbf0f45f90
commit 65a22e8e4d
3 changed files with 119 additions and 5 deletions

25
CHANGES
View File

@ -3,6 +3,31 @@
Changes between 0.9.6 and 0.9.7 [xx XXX 2000]
*) Add another call level for memory allocation routines, thereby
allowing memory allocation callbacks that can be given file
name and line number information. The basic functionality
remains, as well as the original possibility to just replace
malloc(), realloc() and free(). The new functions that can be
registered help users provide variants of malloc(), realloc()
and free() that take two extra arguments, a const char* and an
int. To register and find out the current settings for those
hooks, the following functions are provided:
CRYPTO_set_mem_ex_functions
CRYPTO_set_locked_mem_ex_functions
CRYPTO_get_mem_ex_functions
CRYPTO_get_locked_mem_ex_functions
They work the same way as the corresponding CRYPTO_set_mem_functions
and friends with one exception: giving NULL as arguments will restore
the internal hooks to internal routines and will still make the above
functions return 1 and not 0.
This functionality was created as a direct request to add the
possibility to interface with the Windows debugging routines
_malloc_dbg, _realloc_dbg and _free_dbg.
[Richard Levitte]
*) Fix to uni2asc() to cope with zero length Unicode strings.
These are present in some PKCS#12 files.
[Steve Henson]

View File

@ -320,6 +320,11 @@ void (*CRYPTO_get_dynlock_destroy_callback(void))(struct CRYPTO_dynlock_value *l
* call the latter last if you need different functions */
int CRYPTO_set_mem_functions(void *(*m)(size_t),void *(*r)(void *,size_t), void (*f)(void *));
int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*free_func)(void *));
int CRYPTO_set_mem_ex_functions(void *(*m)(size_t,const char *,int),
void *(*r)(void *,size_t,const char *,int),
void (*f)(void *,const char *,int));
int CRYPTO_set_locked_mem_ex_functions(void *(*m)(size_t,const char *,int),
void (*free_func)(void *,const char *,int));
int CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int),
void (*r)(void *,void *,int,const char *,int,int),
void (*f)(void *,int),
@ -327,6 +332,11 @@ int CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int),
long (*go)(void));
void CRYPTO_get_mem_functions(void *(**m)(size_t),void *(**r)(void *, size_t), void (**f)(void *));
void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *));
void CRYPTO_get_mem_ex_functions(void *(**m)(size_t,const char *,int),
void *(**r)(void *, size_t,const char *,int),
void (**f)(void *,const char *,int));
void CRYPTO_get_locked_mem_ex_functions(void *(**m)(size_t,const char *,int),
void (**f)(void *,const char *,int));
void CRYPTO_get_mem_debug_functions(void (**m)(void *,int,const char *,int,int),
void (**r)(void *,void *,int,const char *,int,int),
void (**f)(void *,int),

View File

@ -77,6 +77,20 @@ static void *(*malloc_func)(size_t) = malloc;
static void *(*realloc_func)(void *, size_t)= realloc;
static void (*free_func)(void *) = free;
static void *crypto_i_malloc_ex(size_t, const char *file, int line);
static void *crypto_i_realloc_ex(void *, size_t, const char *file, int line);
static void crypto_i_free_ex(void *);
static void *(*malloc_locked_ex_func)(size_t, const char *file, int line)
= crypto_i_malloc_ex;
static void (*free_locked_ex_func)(void *)
= crypto_i_free_ex;
static void *(*malloc_ex_func)(size_t, const char *file, int line)
= crypto_i_malloc_ex;
static void *(*realloc_ex_func)(void *, size_t, const char *file, int line)
= crypto_i_realloc_ex;
static void (*free_ex_func)(void *)
= crypto_i_free_ex;
/* may be changed as long as `allow_customize_debug' is set */
/* XXX use correct function pointer types */
#ifdef CRYPTO_MDEBUG
@ -115,6 +129,24 @@ int CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t),
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 == NULL) m = crypto_i_malloc_ex;
if (r == NULL) r = crypto_i_realloc_ex;
if (f == NULL) f = crypto_i_free_ex;
malloc_ex_func=m;
realloc_ex_func=r;
free_ex_func=f;
malloc_locked_ex_func=m;
free_locked_ex_func=f;
return 1;
}
int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *))
{
if (!allow_customize)
@ -126,6 +158,19 @@ int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *))
return 1;
}
int CRYPTO_set_locked_mem_ex_functions(
void *(*m)(size_t,const char *,int),
void (*f)(void *))
{
if (!allow_customize)
return 0;
if (m == NULL) m = crypto_i_malloc_ex;
if (f == NULL) f = crypto_i_free_ex;
malloc_locked_ex_func=m;
free_locked_ex_func=f;
return 1;
}
int CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int),
void (*r)(void *,void *,int,const char *,int,int),
void (*f)(void *,int),
@ -150,12 +195,30 @@ void CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t),
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;
if (r != NULL) *r=realloc_ex_func;
if (f != NULL) *f=free_ex_func;
}
void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *))
{
if (m != NULL) *m=malloc_locked_func;
if (f != NULL) *f=free_locked_func;
}
void CRYPTO_get_locked_mem_ex_functions(
void *(**m)(size_t,const char *,int),
void (**f)(void *))
{
if (m != NULL) *m=malloc_locked_ex_func;
if (f != NULL) *f=free_locked_ex_func;
}
void CRYPTO_get_mem_debug_functions(void (**m)(void *,int,const char *,int,int),
void (**r)(void *,void *,int,const char *,int,int),
void (**f)(void *,int),
@ -180,7 +243,7 @@ void *CRYPTO_malloc_locked(int num, const char *file, int line)
allow_customize_debug = 0;
malloc_debug_func(NULL, num, file, line, 0);
}
ret = malloc_locked_func(num);
ret = malloc_locked_ex_func(num,file,line);
#ifdef LEVITTE_DEBUG_MEM
fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num);
#endif
@ -197,7 +260,7 @@ void CRYPTO_free_locked(void *str)
#ifdef LEVITTE_DEBUG_MEM
fprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\n", str);
#endif
free_locked_func(str);
free_locked_ex_func(str);
if (free_debug_func != NULL)
free_debug_func(NULL, 1);
}
@ -212,7 +275,7 @@ void *CRYPTO_malloc(int num, const char *file, int line)
allow_customize_debug = 0;
malloc_debug_func(NULL, num, file, line, 0);
}
ret = malloc_func(num);
ret = malloc_ex_func(num,file,line);
#ifdef LEVITTE_DEBUG_MEM
fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num);
#endif
@ -228,7 +291,7 @@ void *CRYPTO_realloc(void *str, int num, const char *file, int line)
if (realloc_debug_func != NULL)
realloc_debug_func(str, NULL, num, file, line, 0);
ret = realloc_func(str,num);
ret = realloc_ex_func(str,num,file,line);
#ifdef LEVITTE_DEBUG_MEM
fprintf(stderr, "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n", str, ret, num);
#endif
@ -245,7 +308,7 @@ void CRYPTO_free(void *str)
#ifdef LEVITTE_DEBUG_MEM
fprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\n", str);
#endif
free_func(str);
free_ex_func(str);
if (free_debug_func != NULL)
free_debug_func(NULL, 1);
}
@ -270,3 +333,19 @@ long CRYPTO_get_mem_debug_options(void)
return get_debug_options_func();
return 0;
}
static void *crypto_i_malloc_ex(size_t num, const char *file, int line)
{
return malloc_func(num);
}
static void *crypto_i_realloc_ex(void *str, size_t num,
const char *file, int line)
{
return realloc_func(str,num);
}
static void crypto_i_free_ex(void *str)
{
free_func(str);
}