2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 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
29 * LDAP_MEMORY_DEBUG should only be enabled for the purposes of
30 * debugging memory management within OpenLDAP libraries and slapd.
31 * It should only be enabled by an experienced developer as it
32 * causes the inclusion of numerous assert()'s, many of which may
33 * be triggered by a prefectly valid program.
35 * The code behind this macro is subject to change as needed to
36 * support this testing.
40 ber_int_t bm_top; /* Pattern to detect buf overrun from prev buffer */
41 ber_int_t bm_length; /* Length of user allocated area */
42 #ifdef LDAP_MEMORY_TRACE
43 ber_int_t bm_sequence; /* Allocation sequence number */
45 union bmu_align_u { /* Force alignment, pattern to detect back clobber */
54 long (*bmu_funcp)( double );
55 unsigned char bmu_char[4];
57 #define bm_junk ber_align.bmu_len_t
58 #define bm_data ber_align.bmu_char[1]
59 #define bm_char ber_align.bmu_char
62 /* Pattern at top of allocated space */
63 #define LBER_MEM_JUNK 0xdeaddadaU
65 static const struct ber_mem_hdr ber_int_mem_hdr = { LBER_MEM_JUNK, 0, 0 };
67 /* Note sequence and ber_int_meminuse are counters, but are not
68 * thread safe. If you want to use these values for multithreaded applications,
69 * you must put mutexes around them, otherwise they will have incorrect values.
70 * When debugging, if you sort the debug output, the sequence number will
71 * put allocations/frees together. It is then a simple matter to write a script
72 * to find any allocations that don't have a buffer free function.
74 #ifdef LDAP_MEMORY_TRACE
75 static ber_int_t sequence = 0;
78 /* Pattern placed just before user data */
79 static unsigned char toppattern[4] = { 0xde, 0xad, 0xba, 0xde };
80 /* Pattern placed just after user data */
81 static unsigned char endpattern[4] = { 0xd1, 0xed, 0xde, 0xca };
83 #define mbu_len sizeof(ber_int_mem_hdr.ber_align)
85 /* Test if pattern placed just before user data is good */
86 #define testdatatop(val) ( \
87 *(val->bm_char+mbu_len-4)==toppattern[0] && \
88 *(val->bm_char+mbu_len-3)==toppattern[1] && \
89 *(val->bm_char+mbu_len-2)==toppattern[2] && \
90 *(val->bm_char+mbu_len-1)==toppattern[3] )
92 /* Place pattern just before user data */
93 #define setdatatop(val) *(val->bm_char+mbu_len-4)=toppattern[0]; \
94 *(val->bm_char+mbu_len-3)=toppattern[1]; \
95 *(val->bm_char+mbu_len-2)=toppattern[2]; \
96 *(val->bm_char+mbu_len-1)=toppattern[3];
98 /* Test if pattern placed just after user data is good */
99 #define testend(val) ( *((unsigned char *)val+0)==endpattern[0] && \
100 *((unsigned char *)val+1)==endpattern[1] && \
101 *((unsigned char *)val+2)==endpattern[2] && \
102 *((unsigned char *)val+3)==endpattern[3] )
104 /* Place pattern just after user data */
105 #define setend(val) *((unsigned char *)val+0)=endpattern[0]; \
106 *((unsigned char *)val+1)=endpattern[1]; \
107 *((unsigned char *)val+2)=endpattern[2]; \
108 *((unsigned char *)val+3)=endpattern[3];
110 #define BER_MEM_BADADDR ((void *) &ber_int_mem_hdr.bm_data)
111 #define BER_MEM_VALID(p) do { \
112 assert( (p) != BER_MEM_BADADDR ); \
113 assert( (p) != (void *) &ber_int_mem_hdr ); \
117 #define BER_MEM_VALID(p) /* no-op */
120 BerMemoryFunctions *ber_int_memory_fns = NULL;
123 ber_memfree_x( void *p, void *ctx )
131 if( ber_int_memory_fns == NULL || ctx == NULL ) {
132 #ifdef LDAP_MEMORY_DEBUG
133 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
134 ((char *)p - sizeof(struct ber_mem_hdr));
135 assert( mh->bm_top == LBER_MEM_JUNK);
136 assert( testdatatop( mh));
137 assert( testend( (char *)&mh[1] + mh->bm_length) );
138 ber_int_meminuse -= mh->bm_length;
140 #ifdef LDAP_MEMORY_TRACE
141 fprintf(stderr, "0x%08lx 0x%08lx -f- %ld ber_memfree %ld\n",
142 (long)mh->bm_sequence, (long)mh, (long)mh->bm_length,
145 /* Fill the free space with poison */
146 memset( mh, 0xff, mh->bm_length + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t));
154 assert( ber_int_memory_fns->bmf_free != 0 );
156 (*ber_int_memory_fns->bmf_free)( p, ctx );
160 ber_memfree( void *p )
162 ber_memfree_x(p, NULL);
166 ber_memvfree_x( void **vec, void *ctx )
174 BER_MEM_VALID( vec );
176 for ( i = 0; vec[i] != NULL; i++ ) {
177 ber_memfree_x( vec[i], ctx );
180 ber_memfree_x( vec, ctx );
184 ber_memvfree( void **vec )
186 ber_memvfree_x( vec, NULL );
190 ber_memalloc_x( ber_len_t s, void *ctx )
194 #ifdef LDAP_MEMORY_DEBUG
202 if( ber_int_memory_fns == NULL || ctx == NULL ) {
203 #ifdef LDAP_MEMORY_DEBUG
204 struct ber_mem_hdr *mh = malloc(s + sizeof(struct ber_mem_hdr) + sizeof( ber_int_t));
205 if( mh == NULL ) return NULL;
207 mh->bm_top = LBER_MEM_JUNK;
210 setend( (char *)&mh[1] + mh->bm_length );
212 ber_int_meminuse += mh->bm_length; /* Count mem inuse */
214 #ifdef LDAP_MEMORY_TRACE
215 mh->bm_sequence = sequence++;
216 fprintf(stderr, "0x%08lx 0x%08lx -a- %ld ber_memalloc %ld\n",
217 (long)mh->bm_sequence, (long)mh, (long)mh->bm_length,
220 /* poison new memory */
221 memset( (char *)&mh[1], 0xff, s);
223 BER_MEM_VALID( &mh[1] );
229 new = (*ber_int_memory_fns->bmf_malloc)( s, ctx );
233 ber_errno = LBER_ERROR_MEMORY;
240 ber_memalloc( ber_len_t s )
242 return ber_memalloc_x( s, NULL );
246 ber_memcalloc_x( ber_len_t n, ber_len_t s, void *ctx )
250 #ifdef LDAP_MEMORY_DEBUG
251 assert( n != 0 && s != 0);
254 if( n == 0 || s == 0 ) {
258 if( ber_int_memory_fns == NULL || ctx == NULL ) {
259 #ifdef LDAP_MEMORY_DEBUG
260 struct ber_mem_hdr *mh = calloc(1,
261 (n * s) + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t) );
262 if( mh == NULL ) return NULL;
264 mh->bm_top = LBER_MEM_JUNK;
267 setend( (char *)&mh[1] + mh->bm_length );
269 ber_int_meminuse += mh->bm_length;
271 #ifdef LDAP_MEMORY_TRACE
272 mh->bm_sequence = sequence++;
273 fprintf(stderr, "0x%08lx 0x%08lx -a- %ld ber_memcalloc %ld\n",
274 (long)mh->bm_sequence, (long)mh, (long)mh->bm_length,
277 BER_MEM_VALID( &mh[1] );
280 new = calloc( n, s );
284 new = (*ber_int_memory_fns->bmf_calloc)( n, s, ctx );
288 ber_errno = LBER_ERROR_MEMORY;
295 ber_memcalloc( ber_len_t n, ber_len_t s )
297 return ber_memcalloc_x( n, s, NULL );
301 ber_memrealloc_x( void* p, ber_len_t s, void *ctx )
305 /* realloc(NULL,s) -> malloc(s) */
307 return ber_memalloc_x( s, ctx );
310 /* realloc(p,0) -> free(p) */
312 ber_memfree_x( p, ctx );
318 if( ber_int_memory_fns == NULL || ctx == NULL ) {
319 #ifdef LDAP_MEMORY_DEBUG
321 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
322 ((char *)p - sizeof(struct ber_mem_hdr));
323 assert( mh->bm_top == LBER_MEM_JUNK);
324 assert( testdatatop( mh));
325 assert( testend( (char *)&mh[1] + mh->bm_length) );
326 oldlen = mh->bm_length;
328 p = realloc( mh, s + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t) );
330 ber_errno = LBER_ERROR_MEMORY;
336 setend( (char *)&mh[1] + mh->bm_length );
338 /* poison any new memory */
339 memset( (char *)&mh[1] + oldlen, 0xff, s - oldlen);
342 assert( mh->bm_top == LBER_MEM_JUNK);
343 assert( testdatatop( mh));
345 ber_int_meminuse += s - oldlen;
346 #ifdef LDAP_MEMORY_TRACE
347 fprintf(stderr, "0x%08lx 0x%08lx -a- %ld ber_memrealloc %ld\n",
348 (long)mh->bm_sequence, (long)mh, (long)mh->bm_length,
351 BER_MEM_VALID( &mh[1] );
354 new = realloc( p, s );
357 new = (*ber_int_memory_fns->bmf_realloc)( p, s, ctx );
361 ber_errno = LBER_ERROR_MEMORY;
368 ber_memrealloc( void* p, ber_len_t s )
370 return ber_memrealloc_x( p, s, NULL );
374 ber_bvfree_x( struct berval *bv, void *ctx )
382 if ( bv->bv_val != NULL ) {
383 ber_memfree_x( bv->bv_val, ctx );
386 ber_memfree_x( (char *) bv, ctx );
390 ber_bvfree( struct berval *bv )
392 ber_bvfree_x( bv, NULL );
396 ber_bvecfree_x( struct berval **bv, void *ctx )
407 for ( i = 0; bv[i] != NULL; i++ ) ;
409 /* free in reverse order */
410 for ( i--; i >= 0; i-- ) {
411 ber_bvfree_x( bv[i], ctx );
414 ber_memfree_x( (char *) bv, ctx );
418 ber_bvecfree( struct berval **bv )
420 ber_bvecfree_x( bv, NULL );
424 ber_bvecadd_x( struct berval ***bvec, struct berval *bv, void *ctx )
429 if( *bvec == NULL ) {
435 *bvec = ber_memalloc_x( 2 * sizeof(struct berval *), ctx );
437 if( *bvec == NULL ) {
447 BER_MEM_VALID( bvec );
450 for ( i = 0; (*bvec)[i] != NULL; i++ ) {
458 new = ber_memrealloc_x( *bvec, (i+2) * sizeof(struct berval *), ctx);
473 ber_bvecadd( struct berval ***bvec, struct berval *bv )
475 return ber_bvecadd_x( bvec, bv, NULL );
480 struct berval *dst, struct berval *src, void *ctx )
485 ber_errno = LBER_ERROR_PARAM;
492 if(( new = ber_memalloc_x( sizeof(struct berval), ctx )) == NULL ) {
493 ber_errno = LBER_ERROR_MEMORY;
498 if ( src->bv_val == NULL ) {
504 if(( new->bv_val = ber_memalloc_x( src->bv_len + 1, ctx )) == NULL ) {
505 ber_errno = LBER_ERROR_MEMORY;
507 ber_memfree_x( new, ctx );
511 AC_MEMCPY( new->bv_val, src->bv_val, src->bv_len );
512 new->bv_val[src->bv_len] = '\0';
513 new->bv_len = src->bv_len;
520 struct berval *dst, struct berval *src )
522 return ber_dupbv_x( dst, src, NULL );
529 return ber_dupbv_x( NULL, src, NULL );
534 LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv,
540 ber_errno = LBER_ERROR_PARAM;
547 if(( new = ber_memalloc_x( sizeof(struct berval), ctx )) == NULL ) {
548 ber_errno = LBER_ERROR_MEMORY;
553 new->bv_len = len ? len : strlen( s );
555 if ( (new->bv_val = ber_memalloc_x( new->bv_len+1, ctx )) == NULL ) {
556 ber_errno = LBER_ERROR_MEMORY;
558 ber_memfree_x( new, ctx );
562 AC_MEMCPY( new->bv_val, s, new->bv_len );
563 new->bv_val[new->bv_len] = '\0';
565 new->bv_val = (char *) s;
573 LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv)
575 return ber_str2bv_x( s, len, dup, bv, NULL );
580 LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv,
586 ber_errno = LBER_ERROR_PARAM;
593 if(( new = ber_memalloc_x( sizeof(struct berval), ctx )) == NULL ) {
594 ber_errno = LBER_ERROR_MEMORY;
601 if ( (new->bv_val = ber_memalloc_x( new->bv_len+1, ctx )) == NULL ) {
602 ber_errno = LBER_ERROR_MEMORY;
604 ber_memfree_x( new, ctx );
609 AC_MEMCPY( new->bv_val, s, new->bv_len );
610 new->bv_val[new->bv_len] = '\0';
612 new->bv_val = (char *) s;
620 LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv)
622 return ber_mem2bv_x( s, len, dup, bv, NULL );
626 ber_strdup_x( LDAP_CONST char *s, void *ctx )
631 #ifdef LDAP_MEMORY_DEBUG
632 assert(s != NULL); /* bv damn better point to something */
636 ber_errno = LBER_ERROR_PARAM;
640 len = strlen( s ) + 1;
642 if ( (p = ber_memalloc_x( len, ctx )) == NULL ) {
643 ber_errno = LBER_ERROR_MEMORY;
647 AC_MEMCPY( p, s, len );
652 ber_strdup( LDAP_CONST char *s )
654 return ber_strdup_x( s, NULL );
658 ber_strndup_x( LDAP_CONST char *s, ber_len_t l, void *ctx )
663 #ifdef LDAP_MEMORY_DEBUG
664 assert(s != NULL); /* bv damn better point to something */
668 ber_errno = LBER_ERROR_PARAM;
678 if ( (p = ber_memalloc_x( len + 1, ctx )) == NULL ) {
679 ber_errno = LBER_ERROR_MEMORY;
683 AC_MEMCPY( p, s, len );
689 ber_strndup( LDAP_CONST char *s, ber_len_t l )
691 return ber_strndup_x( s, l, NULL );
695 * dst is resized as required by src and the value of src is copied into dst
696 * dst->bv_val must be NULL (and dst->bv_len must be 0), or it must be
697 * alloc'ed with the context ctx
700 ber_bvreplace_x( struct berval *dst, LDAP_CONST struct berval *src, void *ctx )
702 assert( dst != NULL );
703 assert( !BER_BVISNULL( src ) );
705 if ( BER_BVISNULL( dst ) || dst->bv_len < src->bv_len ) {
706 dst->bv_val = ber_memrealloc_x( dst->bv_val, src->bv_len + 1, ctx );
709 AC_MEMCPY( dst->bv_val, src->bv_val, src->bv_len + 1 );
710 dst->bv_len = src->bv_len;
716 ber_bvreplace( struct berval *dst, LDAP_CONST struct berval *src )
718 return ber_bvreplace_x( dst, src, NULL );
722 ber_bvarray_free_x( BerVarray a, void *ctx )
730 for (i=0; a[i].bv_val; i++) ;
732 /* free in reverse order */
733 for (i--; i>=0; i--) {
734 ber_memfree_x(a[i].bv_val, ctx);
737 ber_memfree_x(a, ctx);
742 ber_bvarray_free( BerVarray a )
744 ber_bvarray_free_x(a, NULL);
748 ber_bvarray_dup_x( BerVarray *dst, BerVarray src, void *ctx )
758 for (i=0; !BER_BVISNULL( &src[i] ); i++) ;
759 new = ber_memalloc_x(( i+1 ) * sizeof(BerValue), ctx );
762 for (j=0; j<i; j++) {
763 ber_dupbv_x( &new[j], &src[j], ctx );
764 if ( BER_BVISNULL( &new[j] )) {
765 ber_bvarray_free_x( new, ctx );
769 BER_BVZERO( &new[j] );
775 ber_bvarray_add_x( BerVarray *a, BerValue *bv, void *ctx )
785 *a = (BerValue *) ber_memalloc_x( 2 * sizeof(BerValue), ctx );
794 for ( n = 0; *a != NULL && (*a)[n].bv_val != NULL; n++ ) {
795 ; /* just count them */
802 atmp = (BerValue *) ber_memrealloc_x( (char *) *a,
803 (n + 2) * sizeof(BerValue), ctx );
813 (*a)[n].bv_val = NULL;
820 ber_bvarray_add( BerVarray *a, BerValue *bv )
822 return ber_bvarray_add_x( a, bv, NULL );