- return((const char *)((const MEM *)a_void)->addr
- - (const char *)((const MEM *)b_void)->addr);
-#endif
- }
-
-/* static unsigned long mem_hash(MEM *a) */
-static unsigned long mem_hash(const void *a_void)
- {
- unsigned long ret;
-
- 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(const void *a_void, const void *b_void)
- {
- return (((const APP_INFO *)a_void)->thread_id != ((const APP_INFO *)b_void)->thread_id)
- || (((const APP_INFO *)a_void)->thread_idptr != ((const APP_INFO *)b_void)->thread_idptr);
- }
-
-/* static unsigned long app_info_hash(APP_INFO *a) */
-static unsigned long app_info_hash(const void *a_void)
- {
- unsigned long id1, id2;
- unsigned long ret;
-
- id1=(unsigned long)((const APP_INFO *)a_void)->thread_id;
- id2=(unsigned long)((const APP_INFO *)a_void)->thread_idptr;
- ret = id1 + id2;
-
- ret=ret*17851+(ret>>14)*7+(ret>>4)*251;
- return(ret);
- }
-
-static APP_INFO *pop_info(void)
- {
- APP_INFO tmp;
- APP_INFO *ret = NULL;
-
- if (amih != NULL)
- {
- tmp.thread_id=CRYPTO_thread_id();
- tmp.thread_idptr=CRYPTO_thread_idptr();
- if ((ret=(APP_INFO *)lh_delete(amih,&tmp)) != NULL)
- {
- APP_INFO *next=ret->next;
-
- if (next != NULL)
- {
- next->references++;
- lh_insert(amih,(char *)next);
- }
-#ifdef LEVITTE_DEBUG_MEM
- if (ret->thread_id != tmp.thread_id || ret->thread_idptr != tmp.thread_idptr)
- {
- fprintf(stderr, "pop_info(): deleted info has other thread ID (%lu/%p) than the current thread (%lu/%p)!!!!\n",
- ret->thread_id, ret->thread_idptr, tmp.thread_id, tmp.thread_idptr);
- abort();
- }
-#endif
- if (--(ret->references) <= 0)
- {
- ret->next = NULL;
- if (next != NULL)
- next->references--;
- OPENSSL_free(ret);
- }
- }
- }
- return(ret);
- }
-
-int CRYPTO_push_info_(const char *info, const char *file, int line)
- {
- APP_INFO *ami, *amim;
- int ret=0;
-
- if (is_MemCheck_on())
- {
- MemCheck_off(); /* obtain MALLOC2 lock */
-
- 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)
- {
- OPENSSL_free(ami);
- ret=0;
- goto err;
- }
- }
-
- ami->thread_id=CRYPTO_thread_id();
- ami->thread_idptr=CRYPTO_thread_idptr();
- ami->file=file;
- ami->line=line;
- ami->info=info;
- ami->references=1;
- ami->next=NULL;
-
- if ((amim=(APP_INFO *)lh_insert(amih,(char *)ami)) != NULL)
- {
-#ifdef LEVITTE_DEBUG_MEM
- if (ami->thread_id != amim->thread_id || ami->thread_idptr != amim->thread_idptr)
- {
- fprintf(stderr, "CRYPTO_push_info(): previous info has other thread ID (%lu/%p) than the current thread (%lu/%p)!!!!\n",
- amim->thread_id, amim->thread_idptr, ami->thread_id, ami->thread_idptr);
- abort();
- }
-#endif
- ami->next=amim;
- }
- err:
- MemCheck_on(); /* release MALLOC2 lock */
- }
-
- return(ret);
- }
-
-int CRYPTO_pop_info(void)
- {
- int ret=0;
-
- if (is_MemCheck_on()) /* _must_ be true, or something went severely wrong */
- {
- MemCheck_off(); /* obtain MALLOC2 lock */
-
- ret=(pop_info() != NULL);
-
- MemCheck_on(); /* release MALLOC2 lock */
- }
- return(ret);
- }
-
-int CRYPTO_remove_all_info(void)
- {
- int ret=0;
-
- if (is_MemCheck_on()) /* _must_ be true */
- {
- MemCheck_off(); /* obtain MALLOC2 lock */
-
- while(pop_info() != NULL)
- ret++;
-
- MemCheck_on(); /* release MALLOC2 lock */
- }
- return(ret);
- }
-
-
-static unsigned long break_order_num=0;
-void CRYPTO_dbg_malloc(void *addr, int num, const char *file, int line,
- int before_p)
- {
- MEM *m,*mm;
- APP_INFO tmp,*amim;
-
- switch(before_p & 127)
- {
- case 0:
- break;
- case 1:
- if (addr == NULL)
- break;
-
- if (is_MemCheck_on())
- {
- MemCheck_off(); /* make sure we hold MALLOC2 lock */
- if ((m=(MEM *)OPENSSL_malloc(sizeof(MEM))) == NULL)
- {
- OPENSSL_free(addr);
- MemCheck_on(); /* release MALLOC2 lock
- * if num_disabled drops to 0 */
- return;
- }
- if (mh == NULL)
- {
- if ((mh=lh_new(mem_hash, mem_cmp)) == NULL)
- {
- OPENSSL_free(addr);
- OPENSSL_free(m);
- addr=NULL;
- goto err;
- }
- }
-
- m->addr=addr;
- m->file=file;
- m->line=line;
- m->num=num;
- if (options & V_CRYPTO_MDEBUG_THREAD)
- {
- m->thread_id=CRYPTO_thread_id();
- m->thread_idptr=CRYPTO_thread_idptr();
- }
- else
- {
- m->thread_id=0;
- m->thread_idptr=NULL;
- }
-
- if (order == break_order_num)
- {
- /* BREAK HERE */
- m->order=order;
- }
- m->order=order++;
-#ifdef LEVITTE_DEBUG_MEM
- fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5ld] %c 0x%p (%d)\n",
- m->order,
- (before_p & 128) ? '*' : '+',
- m->addr, m->num);
-#endif
- if (options & V_CRYPTO_MDEBUG_TIME)
- m->time=time(NULL);
- else
- m->time=0;
-
- tmp.thread_id=CRYPTO_thread_id();
- tmp.thread_idptr=CRYPTO_thread_idptr();
- m->app_info=NULL;
- if (amih != NULL
- && (amim=(APP_INFO *)lh_retrieve(amih,(char *)&tmp)) != NULL)
- {
- m->app_info = amim;
- amim->references++;
- }
-
- if ((mm=(MEM *)lh_insert(mh,(char *)m)) != NULL)
- {
- /* Not good, but don't sweat it */
- if (mm->app_info != NULL)
- {
- mm->app_info->references--;
- }
- OPENSSL_free(mm);
- }
- err:
- MemCheck_on(); /* release MALLOC2 lock
- * if num_disabled drops to 0 */
- }
- break;
- }
- return;
- }
-
-void CRYPTO_dbg_free(void *addr, int before_p)
- {
- MEM m,*mp;
-
- switch(before_p)
- {
- case 0:
- if (addr == NULL)
- break;
-
- if (is_MemCheck_on() && (mh != NULL))
- {
- MemCheck_off(); /* make sure we hold MALLOC2 lock */
-
- m.addr=addr;
- mp=(MEM *)lh_delete(mh,(char *)&m);
- if (mp != NULL)
- {
-#ifdef LEVITTE_DEBUG_MEM
- fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5ld] - 0x%p (%d)\n",
- mp->order, mp->addr, mp->num);
-#endif
- if (mp->app_info != NULL)
- app_info_free(mp->app_info);
- OPENSSL_free(mp);
- }
-
- MemCheck_on(); /* release MALLOC2 lock
- * if num_disabled drops to 0 */
- }
- break;
- case 1:
- break;
- }
- }
-
-void CRYPTO_dbg_realloc(void *addr1, void *addr2, int num,
- const char *file, int line, int before_p)
- {
- MEM m,*mp;
-
-#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);
+ int ret = mh_mode;
+
+ CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init);
+
+ CRYPTO_THREAD_write_lock(malloc_lock);
+ switch (mode) {
+ default:
+ break;
+
+ case CRYPTO_MEM_CHECK_ON:
+ mh_mode = CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE;
+ num_disable = 0;
+ break;
+
+ case CRYPTO_MEM_CHECK_OFF:
+ mh_mode = 0;
+ num_disable = 0;
+ break;
+
+ /* switch off temporarily (for library-internal use): */
+ case CRYPTO_MEM_CHECK_DISABLE:
+ if (mh_mode & CRYPTO_MEM_CHECK_ON) {
+ CRYPTO_THREAD_ID cur = CRYPTO_THREAD_get_current_id();
+ /* see if we don't have long_malloc_lock already */
+ if (!num_disable
+ || !CRYPTO_THREAD_compare_id(disabling_threadid, cur)) {
+ /*
+ * Long-time lock long_malloc_lock must not be claimed
+ * while we're holding malloc_lock, or we'll deadlock
+ * if somebody else holds long_malloc_lock (and cannot
+ * release it because we block entry to this function). Give
+ * them a chance, first, and then claim the locks in
+ * appropriate order (long-time lock first).
+ */
+ CRYPTO_THREAD_unlock(malloc_lock);
+ /*
+ * Note that after we have waited for long_malloc_lock and
+ * malloc_lock, we'll still be in the right "case" and
+ * "if" branch because MemCheck_start and MemCheck_stop may
+ * never be used while there are multiple OpenSSL threads.
+ */
+ CRYPTO_THREAD_write_lock(long_malloc_lock);
+ CRYPTO_THREAD_write_lock(malloc_lock);
+ mh_mode &= ~CRYPTO_MEM_CHECK_ENABLE;
+ disabling_threadid = cur;
+ }
+ num_disable++;
+ }
+ break;
+
+ case CRYPTO_MEM_CHECK_ENABLE:
+ if (mh_mode & CRYPTO_MEM_CHECK_ON) {
+ if (num_disable) { /* always true, or something is going wrong */
+ num_disable--;
+ if (num_disable == 0) {
+ mh_mode |= CRYPTO_MEM_CHECK_ENABLE;
+ CRYPTO_THREAD_unlock(long_malloc_lock);
+ }
+ }
+ }
+ break;
+ }
+ CRYPTO_THREAD_unlock(malloc_lock);
+ return (ret);