2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
18 #include <ac/stdlib.h>
19 #include <ac/string.h>
23 #ifdef LDAP_MEMORY_TRACE
24 # ifndef LDAP_MEMORY_DEBUG
25 # define LDAP_MEMORY_DEBUG 1
32 * LDAP_MEMORY_DEBUG should only be enabled for the purposes of
33 * debugging memory management within OpenLDAP libraries and slapd.
34 * It should only be enabled by an experienced developer as it
35 * causes the inclusion of numerous assert()'s, many of which may
36 * be triggered by a prefectly valid program.
38 * The code behind this macro is subject to change as needed to
39 * support this testing.
43 ber_int_t bm_top; /* Pattern to detect buf overrun from prev buffer */
44 ber_int_t bm_length; /* Length of user allocated area */
45 #ifdef LDAP_MEMORY_TRACE
46 ber_int_t bm_sequence; /* Allocation sequence number */
48 union bmu_align_u { /* Force alignment, pattern to detect back clobber */
57 long (*bmu_funcp)( double );
58 unsigned char bmu_char[4];
60 #define bm_junk ber_align.bmu_len_t
61 #define bm_data ber_align.bmu_char[1]
62 #define bm_char ber_align.bmu_char
65 /* Pattern at top of allocated space */
66 #define LBER_MEM_JUNK 0xdeaddadaU
68 static const struct ber_mem_hdr ber_int_mem_hdr = { LBER_MEM_JUNK, 0, 0 };
70 /* Note sequence and ber_int_options.lbu_meminuse are counters, but are not
71 * thread safe. If you want to use these values for multithreaded applications,
72 * you must put mutexes around them, otherwise they will have incorrect values.
73 * When debugging, if you sort the debug output, the sequence number will
74 * put allocations/frees together. It is then a simple matter to write a script
75 * to find any allocations that don't have a buffer free function.
77 #ifdef LDAP_MEMORY_TRACE
78 static ber_int_t sequence = 0;
81 /* Pattern placed just before user data */
82 static unsigned char toppattern[4] = { 0xde, 0xad, 0xba, 0xde };
83 /* Pattern placed just after user data */
84 static unsigned char endpattern[4] = { 0xd1, 0xed, 0xde, 0xca };
86 #define mbu_len sizeof(ber_int_mem_hdr.ber_align)
88 /* Test if pattern placed just before user data is good */
89 #define testdatatop(val) ( \
90 *(val->bm_char+mbu_len-4)==toppattern[0] && \
91 *(val->bm_char+mbu_len-3)==toppattern[1] && \
92 *(val->bm_char+mbu_len-2)==toppattern[2] && \
93 *(val->bm_char+mbu_len-1)==toppattern[3] )
95 /* Place pattern just before user data */
96 #define setdatatop(val) *(val->bm_char+mbu_len-4)=toppattern[0]; \
97 *(val->bm_char+mbu_len-3)=toppattern[1]; \
98 *(val->bm_char+mbu_len-2)=toppattern[2]; \
99 *(val->bm_char+mbu_len-1)=toppattern[3];
101 /* Test if pattern placed just after user data is good */
102 #define testend(val) ( *((unsigned char *)val+0)==endpattern[0] && \
103 *((unsigned char *)val+1)==endpattern[1] && \
104 *((unsigned char *)val+2)==endpattern[2] && \
105 *((unsigned char *)val+3)==endpattern[3] )
107 /* Place pattern just after user data */
108 #define setend(val) *((unsigned char *)val+0)=endpattern[0]; \
109 *((unsigned char *)val+1)=endpattern[1]; \
110 *((unsigned char *)val+2)=endpattern[2]; \
111 *((unsigned char *)val+3)=endpattern[3];
113 #define BER_MEM_BADADDR ((void *) &ber_int_mem_hdr.bm_data)
114 #define BER_MEM_VALID(p) do { \
115 assert( (p) != BER_MEM_BADADDR ); \
116 assert( (p) != (void *) &ber_int_mem_hdr ); \
120 #define BER_MEM_VALID(p) /* no-op */
123 BerMemoryFunctions *ber_int_memory_fns = NULL;
126 ber_memfree_x( void *p, void *ctx )
128 ber_int_options.lbo_valid = LBER_INITIALIZED;
136 if( ber_int_memory_fns == NULL || ctx == NULL ) {
137 #ifdef LDAP_MEMORY_DEBUG
138 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
139 ((char *)p - sizeof(struct ber_mem_hdr));
140 assert( mh->bm_top == LBER_MEM_JUNK);
141 assert( testdatatop( mh));
142 assert( testend( (char *)&mh[1] + mh->bm_length) );
143 ber_int_options.lbo_meminuse -= mh->bm_length;
145 #ifdef LDAP_MEMORY_TRACE
146 fprintf(stderr, "0x%08x 0x%08x -f- %d ber_memfree %d\n",
147 mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
149 /* Fill the free space with poison */
150 memset( mh, 0xff, mh->bm_length + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t));
158 assert( ber_int_memory_fns->bmf_free );
160 (*ber_int_memory_fns->bmf_free)( p, ctx );
164 ber_memfree( void *p )
166 ber_memfree_x(p, NULL);
170 ber_memvfree_x( void **vec, void *ctx )
174 ber_int_options.lbo_valid = LBER_INITIALIZED;
180 BER_MEM_VALID( vec );
182 for ( i = 0; vec[i] != NULL; i++ ) {
183 ber_memfree_x( vec[i], ctx );
186 ber_memfree_x( vec, ctx );
190 ber_memvfree( void **vec )
192 ber_memvfree_x( vec, NULL );
196 ber_memalloc_x( ber_len_t s, void *ctx )
199 ber_int_options.lbo_valid = LBER_INITIALIZED;
201 #ifdef LDAP_MEMORY_DEBUG
209 if( ber_int_memory_fns == NULL || ctx == NULL ) {
210 #ifdef LDAP_MEMORY_DEBUG
211 struct ber_mem_hdr *mh = malloc(s + sizeof(struct ber_mem_hdr) + sizeof( ber_int_t));
212 if( mh == NULL ) return NULL;
214 mh->bm_top = LBER_MEM_JUNK;
217 setend( (char *)&mh[1] + mh->bm_length );
219 ber_int_options.lbo_meminuse += mh->bm_length; /* Count mem inuse */
221 #ifdef LDAP_MEMORY_TRACE
222 mh->bm_sequence = sequence++;
223 fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memalloc %d\n",
224 mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
226 /* poison new memory */
227 memset( (char *)&mh[1], 0xff, s);
229 BER_MEM_VALID( &mh[1] );
235 new = (*ber_int_memory_fns->bmf_malloc)( s, ctx );
239 ber_errno = LBER_ERROR_MEMORY;
246 ber_memalloc( ber_len_t s )
248 return ber_memalloc_x( s, NULL );
252 ber_memcalloc_x( ber_len_t n, ber_len_t s, void *ctx )
255 ber_int_options.lbo_valid = LBER_INITIALIZED;
257 #ifdef LDAP_MEMORY_DEBUG
258 assert( n != 0 && s != 0);
261 if( n == 0 || s == 0 ) {
265 if( ber_int_memory_fns == NULL || ctx == NULL ) {
266 #ifdef LDAP_MEMORY_DEBUG
267 struct ber_mem_hdr *mh = calloc(1,
268 (n * s) + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t) );
269 if( mh == NULL ) return NULL;
271 mh->bm_top = LBER_MEM_JUNK;
274 setend( (char *)&mh[1] + mh->bm_length );
276 ber_int_options.lbo_meminuse += mh->bm_length;
278 #ifdef LDAP_MEMORY_TRACE
279 mh->bm_sequence = sequence++;
280 fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memcalloc %d\n",
281 mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
283 BER_MEM_VALID( &mh[1] );
286 new = calloc( n, s );
290 new = (*ber_int_memory_fns->bmf_calloc)( n, s, ctx );
294 ber_errno = LBER_ERROR_MEMORY;
301 ber_memcalloc( ber_len_t n, ber_len_t s )
303 return ber_memcalloc_x( n, s, NULL );
307 ber_memrealloc_x( void* p, ber_len_t s, void *ctx )
310 ber_int_options.lbo_valid = LBER_INITIALIZED;
312 /* realloc(NULL,s) -> malloc(s) */
314 return ber_memalloc_x( s, ctx );
317 /* realloc(p,0) -> free(p) */
319 ber_memfree_x( p, ctx );
325 if( ber_int_memory_fns == NULL || ctx == NULL ) {
326 #ifdef LDAP_MEMORY_DEBUG
328 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
329 ((char *)p - sizeof(struct ber_mem_hdr));
330 assert( mh->bm_top == LBER_MEM_JUNK);
331 assert( testdatatop( mh));
332 assert( testend( (char *)&mh[1] + mh->bm_length) );
333 oldlen = mh->bm_length;
335 p = realloc( mh, s + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t) );
337 ber_errno = LBER_ERROR_MEMORY;
343 setend( (char *)&mh[1] + mh->bm_length );
345 /* poison any new memory */
346 memset( (char *)&mh[1] + oldlen, 0xff, s - oldlen);
349 assert( mh->bm_top == LBER_MEM_JUNK);
350 assert( testdatatop( mh));
352 ber_int_options.lbo_meminuse += s - oldlen;
353 #ifdef LDAP_MEMORY_TRACE
354 fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memrealloc %d\n",
355 mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
357 BER_MEM_VALID( &mh[1] );
360 new = realloc( p, s );
363 new = (*ber_int_memory_fns->bmf_realloc)( p, s, ctx );
367 ber_errno = LBER_ERROR_MEMORY;
374 ber_memrealloc( void* p, ber_len_t s )
376 return ber_memrealloc_x( p, s, NULL );
380 ber_bvfree_x( struct berval *bv, void *ctx )
382 ber_int_options.lbo_valid = LBER_INITIALIZED;
390 if ( bv->bv_val != NULL ) {
391 ber_memfree_x( bv->bv_val, ctx );
394 ber_memfree_x( (char *) bv, ctx );
398 ber_bvfree( struct berval *bv )
400 ber_bvfree_x( bv, NULL );
404 ber_bvecfree_x( struct berval **bv, void *ctx )
408 ber_int_options.lbo_valid = LBER_INITIALIZED;
417 for ( i = 0; bv[i] != NULL; i++ ) ;
419 /* free in reverse order */
420 for ( i--; i >= 0; i-- ) {
421 ber_bvfree_x( bv[i], ctx );
424 ber_memfree_x( (char *) bv, ctx );
428 ber_bvecfree( struct berval **bv )
430 ber_bvecfree_x( bv, NULL );
434 ber_bvecadd_x( struct berval ***bvec, struct berval *bv, void *ctx )
439 ber_int_options.lbo_valid = LBER_INITIALIZED;
441 if( *bvec == NULL ) {
447 *bvec = ber_memalloc_x( 2 * sizeof(struct berval *), ctx );
449 if( *bvec == NULL ) {
459 BER_MEM_VALID( bvec );
462 for ( i = 0; (*bvec)[i] != NULL; i++ ) {
470 new = ber_memrealloc_x( *bvec, (i+2) * sizeof(struct berval *), ctx);
485 ber_bvecadd( struct berval ***bvec, struct berval *bv )
487 return ber_bvecadd_x( bvec, bv, NULL );
492 struct berval *dst, struct berval *src, void *ctx )
496 ber_int_options.lbo_valid = LBER_INITIALIZED;
499 ber_errno = LBER_ERROR_PARAM;
506 if(( new = ber_memalloc_x( sizeof(struct berval), ctx )) == NULL ) {
507 ber_errno = LBER_ERROR_MEMORY;
512 if ( src->bv_val == NULL ) {
518 if(( new->bv_val = ber_memalloc_x( src->bv_len + 1, ctx )) == NULL ) {
519 ber_errno = LBER_ERROR_MEMORY;
521 ber_memfree_x( new, ctx );
525 AC_MEMCPY( new->bv_val, src->bv_val, src->bv_len );
526 new->bv_val[src->bv_len] = '\0';
527 new->bv_len = src->bv_len;
534 struct berval *dst, struct berval *src )
536 return ber_dupbv_x( dst, src, NULL );
543 return ber_dupbv_x( NULL, src, NULL );
548 LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv,
553 ber_int_options.lbo_valid = LBER_INITIALIZED;
556 ber_errno = LBER_ERROR_PARAM;
563 if(( new = ber_memalloc_x( sizeof(struct berval), ctx )) == NULL ) {
564 ber_errno = LBER_ERROR_MEMORY;
569 new->bv_len = len ? len : strlen( s );
571 if ( (new->bv_val = ber_memalloc_x( new->bv_len+1, ctx )) == NULL ) {
572 ber_errno = LBER_ERROR_MEMORY;
574 ber_memfree_x( new, ctx );
578 AC_MEMCPY( new->bv_val, s, new->bv_len );
579 new->bv_val[new->bv_len] = '\0';
581 new->bv_val = (char *) s;
589 LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv)
591 return ber_str2bv_x( s, len, dup, bv, NULL );
596 LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv,
601 ber_int_options.lbo_valid = LBER_INITIALIZED;
604 ber_errno = LBER_ERROR_PARAM;
611 if(( new = ber_memalloc_x( sizeof(struct berval), ctx )) == NULL ) {
612 ber_errno = LBER_ERROR_MEMORY;
619 if ( (new->bv_val = ber_memalloc_x( new->bv_len+1, ctx )) == NULL ) {
620 ber_errno = LBER_ERROR_MEMORY;
622 ber_memfree_x( new, ctx );
627 AC_MEMCPY( new->bv_val, s, new->bv_len );
628 new->bv_val[new->bv_len] = '\0';
630 new->bv_val = (char *) s;
638 LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv)
640 return ber_mem2bv_x( s, len, dup, bv, NULL );
644 ber_strdup_x( LDAP_CONST char *s, void *ctx )
649 ber_int_options.lbo_valid = LBER_INITIALIZED;
651 #ifdef LDAP_MEMORY_DEBUG
652 assert(s != NULL); /* bv damn better point to something */
656 ber_errno = LBER_ERROR_PARAM;
660 len = strlen( s ) + 1;
662 if ( (p = ber_memalloc_x( len, ctx )) == NULL ) {
663 ber_errno = LBER_ERROR_MEMORY;
667 AC_MEMCPY( p, s, len );
672 ber_strdup( LDAP_CONST char *s )
674 return ber_strdup_x( s, NULL );
678 ber_strndup_x( LDAP_CONST char *s, ber_len_t l, void *ctx )
683 ber_int_options.lbo_valid = LBER_INITIALIZED;
685 #ifdef LDAP_MEMORY_DEBUG
686 assert(s != NULL); /* bv damn better point to something */
690 ber_errno = LBER_ERROR_PARAM;
700 if ( (p = ber_memalloc_x( len + 1, ctx )) == NULL ) {
701 ber_errno = LBER_ERROR_MEMORY;
705 AC_MEMCPY( p, s, len );
711 ber_strndup( LDAP_CONST char *s, ber_len_t l )
713 return ber_strndup_x( s, l, NULL );
717 ber_bvarray_free_x( BerVarray a, void *ctx )
721 ber_int_options.lbo_valid = LBER_INITIALIZED;
727 for (i=0; a[i].bv_val; i++) ;
729 /* free in reverse order */
730 for (i--; i>=0; i--) {
731 ber_memfree_x(a[i].bv_val, ctx);
734 ber_memfree_x(a, ctx);
739 ber_bvarray_free( BerVarray a )
741 ber_bvarray_free_x(a, NULL);
745 ber_bvarray_add_x( BerVarray *a, BerValue *bv, void *ctx )
749 ber_int_options.lbo_valid = LBER_INITIALIZED;
757 *a = (BerValue *) ber_memalloc_x( 2 * sizeof(BerValue), ctx );
766 for ( n = 0; *a != NULL && (*a)[n].bv_val != NULL; n++ ) {
767 ; /* just count them */
774 atmp = (BerValue *) ber_memrealloc_x( (char *) *a,
775 (n + 2) * sizeof(BerValue), ctx );
785 (*a)[n].bv_val = NULL;
791 ber_bvarray_add( BerVarray *a, BerValue *bv )
793 return ber_bvarray_add_x( a, bv, NULL );