"Andrew W. Gray" <agray@iconsinc.com> says /GD is no longer a valid
[openssl.git] / crypto / mem_dbg.c
index 6068dcc806296531eeba073de55a717d3c939823..a6c70e431a9f67ed5781b384a5b9e281da48e456 100644 (file)
@@ -108,7 +108,7 @@ static LHASH *amih=NULL; /* hash-table with those app_mem_info_st's
 typedef struct mem_st
 /* memory-block description */
        {
-       char *addr;
+       void *addr;
        int num;
        const char *file;
        int line;
@@ -214,42 +214,48 @@ void CRYPTO_dbg_set_options(long bits)
        options = bits;
        }
 
-long CRYPTO_dbg_get_options()
+long CRYPTO_dbg_get_options(void)
        {
        return options;
        }
 
-static int mem_cmp(MEM *a, MEM *b)
+/* static int mem_cmp(MEM *a, MEM *b) */
+static int mem_cmp(const void *a_void, const void *b_void)
        {
-       return(a->addr - b->addr);
+       return((const char *)((MEM *)a_void)->addr
+               - (const char *)((MEM *)b_void)->addr);
        }
 
-static unsigned long mem_hash(MEM *a)
+/* static unsigned long mem_hash(MEM *a) */
+static unsigned long mem_hash(const void *a_void)
        {
        unsigned long ret;
 
-       ret=(unsigned long)a->addr;
+       ret=(unsigned long)((const MEM *)a_void)->addr;
 
        ret=ret*17851+(ret>>14)*7+(ret>>4)*251;
        return(ret);
        }
 
-static int app_info_cmp(APP_INFO *a, APP_INFO *b)
+/* static int app_info_cmp(APP_INFO *a, APP_INFO *b) */
+static int app_info_cmp(const void *a_void, const void *b_void)
        {
-       return(a->thread != b->thread);
+       return(((const APP_INFO *)a_void)->thread
+               != ((const APP_INFO *)b_void)->thread);
        }
 
-static unsigned long app_info_hash(APP_INFO *a)
+/* static unsigned long app_info_hash(APP_INFO *a) */
+static unsigned long app_info_hash(const void *a_void)
        {
        unsigned long ret;
 
-       ret=(unsigned long)a->thread;
+       ret=(unsigned long)((const APP_INFO *)a_void)->thread;
 
        ret=ret*17851+(ret>>14)*7+(ret>>4)*251;
        return(ret);
        }
 
-static APP_INFO *pop_info()
+static APP_INFO *pop_info(void)
        {
        APP_INFO tmp;
        APP_INFO *ret = NULL;
@@ -257,7 +263,7 @@ static APP_INFO *pop_info()
        if (amih != NULL)
                {
                tmp.thread=CRYPTO_thread_id();
-               if ((ret=(APP_INFO *)lh_delete(amih,(char *)&tmp)) != NULL)
+               if ((ret=(APP_INFO *)lh_delete(amih,&tmp)) != NULL)
                        {
                        APP_INFO *next=ret->next;
 
@@ -266,7 +272,7 @@ static APP_INFO *pop_info()
                                next->references++;
                                lh_insert(amih,(char *)next);
                                }
-#ifdef LEVITTE_DEBUG
+#ifdef LEVITTE_DEBUG_MEM
                        if (ret->thread != tmp.thread)
                                {
                                fprintf(stderr, "pop_info(): deleted info has other thread ID (%lu) than the current thread (%lu)!!!!\n",
@@ -279,7 +285,7 @@ static APP_INFO *pop_info()
                                ret->next = NULL;
                                if (next != NULL)
                                        next->references--;
-                               Free(ret);
+                               OPENSSL_free(ret);
                                }
                        }
                }
@@ -295,16 +301,16 @@ int CRYPTO_push_info_(const char *info, const char *file, int line)
                {
                MemCheck_off(); /* obtains CRYPTO_LOCK_MALLOC2 */
 
-               if ((ami = (APP_INFO *)Malloc(sizeof(APP_INFO))) == NULL)
+               if ((ami = (APP_INFO *)OPENSSL_malloc(sizeof(APP_INFO))) == NULL)
                        {
                        ret=0;
                        goto err;
                        }
                if (amih == NULL)
                        {
-                       if ((amih=lh_new(app_info_hash,app_info_cmp)) == NULL)
+                       if ((amih=lh_new(app_info_hash, app_info_cmp)) == NULL)
                                {
-                               Free(ami);
+                               OPENSSL_free(ami);
                                ret=0;
                                goto err;
                                }
@@ -319,7 +325,7 @@ int CRYPTO_push_info_(const char *info, const char *file, int line)
 
                if ((amim=(APP_INFO *)lh_insert(amih,(char *)ami)) != NULL)
                        {
-#ifdef LEVITTE_DEBUG
+#ifdef LEVITTE_DEBUG_MEM
                        if (ami->thread != amim->thread)
                                {
                                fprintf(stderr, "CRYPTO_push_info(): previous info has other thread ID (%lu) than the current thread (%lu)!!!!\n",
@@ -386,18 +392,18 @@ void CRYPTO_dbg_malloc(void *addr, int num, const char *file, int line,
                if (is_MemCheck_on())
                        {
                        MemCheck_off(); /* obtains CRYPTO_LOCK_MALLOC2 */
-                       if ((m=(MEM *)Malloc(sizeof(MEM))) == NULL)
+                       if ((m=(MEM *)OPENSSL_malloc(sizeof(MEM))) == NULL)
                                {
-                               Free(addr);
+                               OPENSSL_free(addr);
                                MemCheck_on(); /* releases CRYPTO_LOCK_MALLOC2 */
                                return;
                                }
                        if (mh == NULL)
                                {
-                               if ((mh=lh_new(mem_hash,mem_cmp)) == NULL)
+                               if ((mh=lh_new(mem_hash, mem_cmp)) == NULL)
                                        {
-                                       Free(addr);
-                                       Free(m);
+                                       OPENSSL_free(addr);
+                                       OPENSSL_free(m);
                                        addr=NULL;
                                        goto err;
                                        }
@@ -418,8 +424,8 @@ void CRYPTO_dbg_malloc(void *addr, int num, const char *file, int line,
                                m->order=order;
                                }
                        m->order=order++;
-#ifdef LEVITTE_DEBUG
-                       fprintf(stderr, "LEVITTE_DEBUG: [%5d] %c 0x%p (%d)\n",
+#ifdef LEVITTE_DEBUG_MEM
+                       fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5d] %c 0x%p (%d)\n",
                                m->order,
                                (before_p & 128) ? '*' : '+',
                                m->addr, m->num);
@@ -445,7 +451,7 @@ void CRYPTO_dbg_malloc(void *addr, int num, const char *file, int line,
                                        {
                                        mm->app_info->references--;
                                        }
-                               Free(mm);
+                               OPENSSL_free(mm);
                                }
                err:
                        MemCheck_on(); /* releases CRYPTO_LOCK_MALLOC2 */
@@ -473,15 +479,15 @@ void CRYPTO_dbg_free(void *addr, int before_p)
                        mp=(MEM *)lh_delete(mh,(char *)&m);
                        if (mp != NULL)
                                {
-#ifdef LEVITTE_DEBUG
-                       fprintf(stderr, "LEVITTE_DEBUG: [%5d] - 0x%p (%d)\n",
+#ifdef LEVITTE_DEBUG_MEM
+                       fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5d] - 0x%p (%d)\n",
                                mp->order, mp->addr, mp->num);
 #endif
                                if (mp->app_info != NULL)
                                        {
                                        mp->app_info->references--;
                                        }
-                               Free(mp);
+                               OPENSSL_free(mp);
                                }
 
                        MemCheck_on(); /* releases CRYPTO_LOCK_MALLOC2 */
@@ -497,8 +503,8 @@ void CRYPTO_dbg_realloc(void *addr1, void *addr2, int num,
        {
        MEM m,*mp;
 
-#ifdef LEVITTE_DEBUG
-       fprintf(stderr, "LEVITTE_DEBUG: --> CRYPTO_dbg_malloc(addr1 = %p, addr2 = %p, num = %d, file = \"%s\", line = %d, before_p = %d)\n",
+#ifdef LEVITTE_DEBUG_MEM
+       fprintf(stderr, "LEVITTE_DEBUG_MEM: --> CRYPTO_dbg_malloc(addr1 = %p, addr2 = %p, num = %d, file = \"%s\", line = %d, before_p = %d)\n",
                addr1, addr2, num, file, line, before_p);
 #endif
 
@@ -524,8 +530,8 @@ void CRYPTO_dbg_realloc(void *addr1, void *addr2, int num,
                        mp=(MEM *)lh_delete(mh,(char *)&m);
                        if (mp != NULL)
                                {
-#ifdef LEVITTE_DEBUG
-                               fprintf(stderr, "LEVITTE_DEBUG: [%5d] * 0x%p (%d) -> 0x%p (%d)\n",
+#ifdef LEVITTE_DEBUG_MEM
+                               fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5d] * 0x%p (%d) -> 0x%p (%d)\n",
                                        mp->order,
                                        mp->addr, mp->num,
                                        addr2, num);
@@ -626,7 +632,7 @@ static void print_leak(MEM *m, MEM_LEAK *l)
                }
        while(amip && amip->thread == ti);
                
-#ifdef LEVITTE_DEBUG
+#ifdef LEVITTE_DEBUG_MEM
        if (amip)
                {
                fprintf(stderr, "Thread switch detected in backtrace!!!!\n");
@@ -640,19 +646,65 @@ void CRYPTO_mem_leaks(BIO *b)
        MEM_LEAK ml;
        char buf[80];
 
-       if (mh == NULL) return;
+       if (mh == NULL && amih == NULL)
+               return;
        ml.bio=b;
        ml.bytes=0;
        ml.chunks=0;
-       CRYPTO_w_lock(CRYPTO_LOCK_MALLOC2);
-       lh_doall_arg(mh,(void (*)())print_leak,(char *)&ml);
-       CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2);
+       MemCheck_off(); /* obtains CRYPTO_LOCK_MALLOC2 */
+       if (mh != NULL)
+               lh_doall_arg(mh, (LHASH_DOALL_ARG_FN_TYPE)print_leak,
+                               (char *)&ml);
        if (ml.chunks != 0)
                {
                sprintf(buf,"%ld bytes leaked in %d chunks\n",
                        ml.bytes,ml.chunks);
                BIO_puts(b,buf);
                }
+       else
+               {
+               /* Make sure that, if we found no leaks, memory-leak debugging itself
+                * does not introduce memory leaks (which might irritate
+                * external debugging tools).
+                * (When someone enables leak checking, but does not call
+                * this function, we declare it to be their fault.)
+                *
+                * XXX    This should be in CRYPTO_mem_leaks_cb,
+                * and CRYPTO_mem_leaks should be implemented by
+                * using CRYPTO_mem_leaks_cb.
+                * (Also their should be a variant of lh_doall_arg
+                * that takes a function pointer instead of a void *;
+                * this would obviate the ugly and illegal
+                * void_fn_to_char kludge in CRYPTO_mem_leaks_cb.
+                * Otherwise the code police will come and get us.)
+                */
+               int old_mh_mode;
+
+               CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
+
+               /* avoid deadlock when lh_free() uses CRYPTO_dbg_free(),
+                * which uses CRYPTO_is_mem_check_on */
+               old_mh_mode = mh_mode;
+               mh_mode = CRYPTO_MEM_CHECK_OFF;
+
+               if (mh != NULL)
+                       {
+                       lh_free(mh);
+                       mh = NULL;
+                       }
+               if (amih != NULL)
+                       {
+                       if (lh_num_items(amih) == 0) 
+                               {
+                               lh_free(amih);
+                               amih = NULL;
+                               }
+                       }
+
+               mh_mode = old_mh_mode;
+               CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
+               }
+       MemCheck_on(); /* releases CRYPTO_LOCK_MALLOC2 */
 
 #if 0
        lh_stats_bio(mh,b);
@@ -661,24 +713,6 @@ void CRYPTO_mem_leaks(BIO *b)
 #endif
        }
 
-static void (*mem_cb)()=NULL;
-
-static void cb_leak(MEM *m, char *cb)
-       {
-       void (*mem_callback)()=(void (*)())cb;
-       mem_callback(m->order,m->file,m->line,m->num,m->addr);
-       }
-
-void CRYPTO_mem_leaks_cb(void (*cb)())
-       {
-       if (mh == NULL) return;
-       CRYPTO_w_lock(CRYPTO_LOCK_MALLOC2);
-       mem_cb=cb;
-       lh_doall_arg(mh,(void (*)())cb_leak,(char *)mem_cb);
-       mem_cb=NULL;
-       CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2);
-       }
-
 #ifndef NO_FP_API
 void CRYPTO_mem_leaks_fp(FILE *fp)
        {
@@ -693,3 +727,21 @@ void CRYPTO_mem_leaks_fp(FILE *fp)
        }
 #endif
 
+
+
+/* FIXME: We really don't allow much to the callback.  For example, it has
+   no chance of reaching the info stack for the item it processes.  Should
+   it really be this way?  -- Richard Levitte */
+static void cb_leak(MEM *m,
+                   void (**cb)(unsigned long, const char *, int, int, void *))
+       {
+       (**cb)(m->order,m->file,m->line,m->num,m->addr);
+       }
+
+void CRYPTO_mem_leaks_cb(void (*cb)(unsigned long, const char *, int, int, void *))
+       {
+       if (mh == NULL) return;
+       CRYPTO_w_lock(CRYPTO_LOCK_MALLOC2);
+       lh_doall_arg(mh, (LHASH_DOALL_ARG_FN_TYPE)cb_leak,(void *)&cb);
+       CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2);
+       }