From f7ccba3edf9f1f02d7bd3b019d7bc96f25a95718 Mon Sep 17 00:00:00 2001 From: Geoff Thorpe Date: Fri, 28 Mar 2008 02:49:43 +0000 Subject: There was a need to support thread ID types that couldn't be reliably cast to 'unsigned long' (ie. odd platforms/compilers), so a pointer-typed version was added but it required portable code to check *both* modes to determine equality. This commit maintains the availability of both thread ID types, but deprecates the type-specific accessor APIs that invoke the callbacks - instead a single type-independent API is used. This simplifies software that calls into this interface, and should also make it less error-prone - as forgetting to call and compare *both* thread ID accessors could have led to hard-to-debug/infrequent bugs (that might only affect certain platforms or thread implementations). As the CHANGES note says, there were corresponding deprecations and replacements in the thread-related functions for BN_BLINDING and ERR too. --- crypto/mem_dbg.c | 96 ++++++++++++++++++++++---------------------------------- 1 file changed, 38 insertions(+), 58 deletions(-) (limited to 'crypto/mem_dbg.c') diff --git a/crypto/mem_dbg.c b/crypto/mem_dbg.c index 5e55d22277..1d94d3c5a2 100644 --- a/crypto/mem_dbg.c +++ b/crypto/mem_dbg.c @@ -146,9 +146,8 @@ typedef struct app_mem_info_st * CRYPTO_pop_info() to pop an entry, * CRYPTO_remove_all_info() to pop all entries. */ - { - unsigned long thread_id; - void *thread_idptr; + { + CRYPTO_THREADID threadid; const char *file; int line; const char *info; @@ -170,8 +169,7 @@ typedef struct mem_st int num; const char *file; int line; - unsigned long thread_id; - void *thread_idptr; + CRYPTO_THREADID threadid; unsigned long order; time_t time; APP_INFO *app_info; @@ -192,12 +190,9 @@ static unsigned int num_disable = 0; /* num_disable > 0 * mh_mode == CRYPTO_MEM_CHECK_ON (w/o ..._ENABLE) */ -/* The following two variables, disabling_thread_id and disabling_thread_idptr, - * are valid iff num_disable > 0. CRYPTO_LOCK_MALLOC2 is locked exactly in - * this case (by the thread named in disabling_thread_id / disabling_thread_idptr). - */ -static unsigned long disabling_thread_id = 0; -static void *disabling_thread_idptr = NULL; +/* Valid iff num_disable > 0. CRYPTO_LOCK_MALLOC2 is locked exactly in this + * case (by the thread named in disabling_threadid). */ +static CRYPTO_THREADID disabling_threadid; static void app_info_free(APP_INFO *inf) { @@ -234,9 +229,10 @@ int CRYPTO_mem_ctrl(int mode) case CRYPTO_MEM_CHECK_DISABLE: /* aka MemCheck_off() */ if (mh_mode & CRYPTO_MEM_CHECK_ON) { - if (!num_disable - || (disabling_thread_id != CRYPTO_thread_id()) - || (disabling_thread_idptr != CRYPTO_thread_idptr())) /* otherwise we already have the MALLOC2 lock */ + CRYPTO_THREADID tid; + CRYPTO_THREADID_set(&tid); + if (!num_disable || CRYPTO_THREADID_cmp(&tid, + &disabling_threadid)) { /* Long-time lock CRYPTO_LOCK_MALLOC2 must not be claimed while * we're holding CRYPTO_LOCK_MALLOC, or we'll deadlock if @@ -254,8 +250,7 @@ int CRYPTO_mem_ctrl(int mode) CRYPTO_w_lock(CRYPTO_LOCK_MALLOC2); CRYPTO_w_lock(CRYPTO_LOCK_MALLOC); mh_mode &= ~CRYPTO_MEM_CHECK_ENABLE; - disabling_thread_id=CRYPTO_thread_id(); - disabling_thread_idptr=CRYPTO_thread_idptr(); + CRYPTO_THREADID_set(&disabling_threadid); } num_disable++; } @@ -288,11 +283,12 @@ int CRYPTO_is_mem_check_on(void) if (mh_mode & CRYPTO_MEM_CHECK_ON) { + CRYPTO_THREADID tid; + CRYPTO_THREADID_set(&tid); CRYPTO_r_lock(CRYPTO_LOCK_MALLOC); - ret = (mh_mode & CRYPTO_MEM_CHECK_ENABLE) - || (disabling_thread_id != CRYPTO_thread_id()) - || (disabling_thread_idptr != CRYPTO_thread_idptr()); + ret = (mh_mode & CRYPTO_MEM_CHECK_ENABLE) || + CRYPTO_THREADID_cmp(&tid, &disabling_threadid); CRYPTO_r_unlock(CRYPTO_LOCK_MALLOC); } @@ -339,20 +335,15 @@ static unsigned long mem_hash(const void *a_void) /* 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); + return CRYPTO_THREADID_cmp(&((const APP_INFO *)a_void)->threadid, + &((const APP_INFO *)b_void)->threadid); } /* 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 = CRYPTO_THREADID_hash(&((const APP_INFO *)a_void)->threadid); ret=ret*17851+(ret>>14)*7+(ret>>4)*251; return(ret); } @@ -364,8 +355,7 @@ static APP_INFO *pop_info(void) if (amih != NULL) { - tmp.thread_id=CRYPTO_thread_id(); - tmp.thread_idptr=CRYPTO_thread_idptr(); + CRYPTO_THREADID_set(&tmp.threadid); if ((ret=(APP_INFO *)lh_delete(amih,&tmp)) != NULL) { APP_INFO *next=ret->next; @@ -376,10 +366,11 @@ static APP_INFO *pop_info(void) lh_insert(amih,(char *)next); } #ifdef LEVITTE_DEBUG_MEM - if (ret->thread_id != tmp.thread_id || ret->thread_idptr != tmp.thread_idptr) + if (CRYPTO_THREADID_cmp(&ret->threadid, &tmp.threadid)) { - 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); + fprintf(stderr, "pop_info(): deleted info has other thread ID (%lu) than the current thread (%lu)!!!!\n", + CRYPTO_THREADID_hash(&ret->threadid), + CRYPTO_THREADID_hash(&tmp.threadid)); abort(); } #endif @@ -419,8 +410,7 @@ int CRYPTO_push_info_(const char *info, const char *file, int line) } } - ami->thread_id=CRYPTO_thread_id(); - ami->thread_idptr=CRYPTO_thread_idptr(); + CRYPTO_THREADID_set(&ami->threadid); ami->file=file; ami->line=line; ami->info=info; @@ -430,10 +420,11 @@ 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_MEM - if (ami->thread_id != amim->thread_id || ami->thread_idptr != amim->thread_idptr) + if (CRYPTO_THREADID_cmp(&ami->threadid, &amim->threadid)) { - 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); + fprintf(stderr, "CRYPTO_push_info(): previous info has other thread ID (%lu) than the current thread (%lu)!!!!\n", + CRYPTO_THREADID_hash(&amim->threadid), + CRYPTO_THREADID_hash(&ami->threadid)); abort(); } #endif @@ -518,16 +509,7 @@ void CRYPTO_dbg_malloc(void *addr, int num, const char *file, int line, 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; - } + CRYPTO_THREADID_set(&m->threadid); if (order == break_order_num) { @@ -546,8 +528,7 @@ void CRYPTO_dbg_malloc(void *addr, int num, const char *file, int line, else m->time=0; - tmp.thread_id=CRYPTO_thread_id(); - tmp.thread_idptr=CRYPTO_thread_idptr(); + CRYPTO_THREADID_set(&m->threadid); m->app_info=NULL; if (amih != NULL && (amim=(APP_INFO *)lh_retrieve(amih,(char *)&tmp)) != NULL) @@ -676,8 +657,7 @@ static void print_leak(const MEM *m, MEM_LEAK *l) APP_INFO *amip; int ami_cnt; struct tm *lcl = NULL; - unsigned long ti; - void *tip; + CRYPTO_THREADID tid; #define BUF_REMAIN (sizeof buf - (size_t)(bufp - buf)) @@ -699,7 +679,8 @@ static void print_leak(const MEM *m, MEM_LEAK *l) if (options & V_CRYPTO_MDEBUG_THREAD) { - BIO_snprintf(bufp, BUF_REMAIN, "thread=%lu/%p, ", m->thread_id, m->thread_idptr); + BIO_snprintf(bufp, BUF_REMAIN, "thread=%lu, ", + CRYPTO_THREADID_hash(&m->threadid)); bufp += strlen(bufp); } @@ -716,8 +697,7 @@ static void print_leak(const MEM *m, MEM_LEAK *l) ami_cnt=0; if (!amip) return; - ti=amip->thread_id; - tip=amip->thread_idptr; + CRYPTO_THREADID_set(&tid); do { @@ -727,8 +707,8 @@ static void print_leak(const MEM *m, MEM_LEAK *l) ami_cnt++; memset(buf,'>',ami_cnt); BIO_snprintf(buf + ami_cnt, sizeof buf - ami_cnt, - " thread=%lu/%p, file=%s, line=%d, info=\"", - amip->thread_id, amip->thread_idptr, amip->file, amip->line); + " thread=%lu, file=%s, line=%d, info=\"", + CRYPTO_THREADID_hash(&amip->threadid), amip->file, amip->line); buf_len=strlen(buf); info_len=strlen(amip->info); if (128 - buf_len - 3 < info_len) @@ -748,8 +728,8 @@ static void print_leak(const MEM *m, MEM_LEAK *l) amip = amip->next; } - while(amip && amip->thread_id == ti && amip->thread_idptr == tip); - + while(amip && !CRYPTO_THREADID_cmp(&amip->threadid, &tid)); + #ifdef LEVITTE_DEBUG_MEM if (amip) { -- cgit v1.2.3