int OBJ_NAME_new_index(unsigned long (*hash_func)(const char *), int (*cmp_func)(const char *, const char *), void (*free_func)(const char *, int, const char *)) { int ret; int i; NAME_FUNCS *name_funcs; if (name_funcs_stack == NULL) { MemCheck_off(); name_funcs_stack=sk_NAME_FUNCS_new_null(); MemCheck_on(); } if ((name_funcs_stack == NULL)) { /* ERROR */ return(0); } ret=names_type_num; names_type_num++; for (i=sk_NAME_FUNCS_num(name_funcs_stack); i<names_type_num; i++) { MemCheck_off(); name_funcs = OPENSSL_malloc(sizeof(NAME_FUNCS)); MemCheck_on(); if (!name_funcs) { OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX,ERR_R_MALLOC_FAILURE); return(0); } name_funcs->hash_func = lh_strhash; name_funcs->cmp_func = OPENSSL_strcmp; name_funcs->free_func = 0; /* NULL is often declared to * ((void *)0), which according * to Compaq C is not really * compatible with a function * pointer. -- Richard Levitte*/ MemCheck_off(); sk_NAME_FUNCS_push(name_funcs_stack,name_funcs); MemCheck_on(); } name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret); if (hash_func != NULL) name_funcs->hash_func = hash_func; if (cmp_func != NULL) name_funcs->cmp_func = cmp_func; if (free_func != NULL) name_funcs->free_func = free_func; return(ret); }
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: [%5d] - 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; } }
static void load_builtin_compressions(void) { int got_write_lock = 0; CRYPTO_r_lock(CRYPTO_LOCK_SSL); if (ssl_comp_methods == NULL) { CRYPTO_r_unlock(CRYPTO_LOCK_SSL); CRYPTO_w_lock(CRYPTO_LOCK_SSL); got_write_lock = 1; if (ssl_comp_methods == NULL) { SSL_COMP *comp = NULL; MemCheck_off(); ssl_comp_methods=sk_SSL_COMP_new(sk_comp_cmp); MemCheck_on(); } } if (got_write_lock) CRYPTO_w_unlock(CRYPTO_LOCK_SSL); else CRYPTO_r_unlock(CRYPTO_LOCK_SSL); }
int OBJ_NAME_init(void) { if (names_lh != NULL) return(1); MemCheck_off(); names_lh=lh_new((func_hash_t)obj_name_hash, (func_comp_t)obj_name_cmp); MemCheck_on(); return(names_lh != NULL); }
int OBJ_NAME_init(void) { if (names_lh != NULL) return(1); MemCheck_off(); names_lh=lh_OBJ_NAME_new(); MemCheck_on(); return(names_lh != NULL); }
EXPORT_C int OBJ_NAME_init(void) { if (names_lh != NULL) return(1); MemCheck_off(); names_lh=lh_new(obj_name_hash, obj_name_cmp); MemCheck_on(); return(names_lh != NULL); }
ERR_STATE *ERR_get_state(void) { ERR_STATE *ret=NULL,tmp,*tmpp; int i; unsigned long pid; pid=(unsigned long)CRYPTO_thread_id(); CRYPTO_r_lock(CRYPTO_LOCK_ERR); if (thread_hash == NULL) { CRYPTO_r_unlock(CRYPTO_LOCK_ERR); CRYPTO_w_lock(CRYPTO_LOCK_ERR); if (thread_hash == NULL) { MemCheck_off(); thread_hash=lh_new(pid_hash,pid_cmp); MemCheck_on(); CRYPTO_w_unlock(CRYPTO_LOCK_ERR); if (thread_hash == NULL) return(getFallback()); } else CRYPTO_w_unlock(CRYPTO_LOCK_ERR); } else { tmp.pid=pid; ret=(ERR_STATE *)lh_retrieve(thread_hash,&tmp); CRYPTO_r_unlock(CRYPTO_LOCK_ERR); } /* ret == the error state, if NULL, make a new one */ if (ret == NULL) { ret=(ERR_STATE *)Malloc(sizeof(ERR_STATE)); if (ret == NULL) return(getFallback()); ret->pid=pid; ret->top=0; ret->bottom=0; for (i=0; i<ERR_NUM_ERRORS; i++) { ret->err_data[i]=NULL; ret->err_data_flags[i]=0; } CRYPTO_w_lock(CRYPTO_LOCK_ERR); tmpp=(ERR_STATE *)lh_insert(thread_hash,ret); CRYPTO_w_unlock(CRYPTO_LOCK_ERR); if (tmpp != NULL) /* old entry - should not happen */ { ERR_STATE_free(tmpp); } } return(ret); }
static struct hostent *ghbn_dup(struct hostent *a) { struct hostent *ret; int i,j; MemCheck_off(); ret=(struct hostent *)OPENSSL_malloc(sizeof(struct hostent)); if (ret == NULL) return(NULL); memset(ret,0,sizeof(struct hostent)); for (i=0; a->h_aliases[i] != NULL; i++) ; i++; ret->h_aliases = (char **)OPENSSL_malloc(i*sizeof(char *)); if (ret->h_aliases == NULL) goto err; memset(ret->h_aliases, 0, i*sizeof(char *)); for (i=0; a->h_addr_list[i] != NULL; i++) ; i++; ret->h_addr_list=(char **)OPENSSL_malloc(i*sizeof(char *)); if (ret->h_addr_list == NULL) goto err; memset(ret->h_addr_list, 0, i*sizeof(char *)); j=strlen(a->h_name)+1; if ((ret->h_name=OPENSSL_malloc(j)) == NULL) goto err; memcpy((char *)ret->h_name,a->h_name,j); for (i=0; a->h_aliases[i] != NULL; i++) { j=strlen(a->h_aliases[i])+1; if ((ret->h_aliases[i]=OPENSSL_malloc(j)) == NULL) goto err; memcpy(ret->h_aliases[i],a->h_aliases[i],j); } ret->h_length=a->h_length; ret->h_addrtype=a->h_addrtype; for (i=0; a->h_addr_list[i] != NULL; i++) { if ((ret->h_addr_list[i]=OPENSSL_malloc(a->h_length)) == NULL) goto err; memcpy(ret->h_addr_list[i],a->h_addr_list[i],a->h_length); } if (0) { err: if (ret != NULL) ghbn_free(ret); ret=NULL; } MemCheck_on(); return(ret); }
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); #endif switch(before_p) { case 0: break; case 1: if (addr2 == NULL) break; if (addr1 == NULL) { CRYPTO_dbg_malloc(addr2, num, file, line, 128 | before_p); break; } if (is_MemCheck_on()) { MemCheck_off(); /* make sure we hold MALLOC2 lock */ m.addr=addr1; mp=(MEM *)lh_delete(mh,(char *)&m); if (mp != NULL) { #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); #endif mp->addr=addr2; mp->num=num; lh_insert(mh,(char *)mp); } MemCheck_on(); /* release MALLOC2 lock * if num_disabled drops to 0 */ } break; } return; }
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_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=CRYPTO_thread_id(); 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 != amim->thread) { fprintf(stderr, "CRYPTO_push_info(): previous info has other thread ID (%lu) than the current thread (%lu)!!!!\n", amim->thread, ami->thread); abort(); } #endif ami->next=amim; } err: 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); }
void CRYPTO_mem_leaks_fp(FILE *fp) { BIO *b; if (mh == NULL) return; /* Need to turn off memory checking when allocated BIOs ... especially * as we're creating them at a time when we're trying to check we've not * left anything un-free()'d!! */ MemCheck_off(); b = BIO_new(BIO_s_file()); MemCheck_on(); if(!b) return; BIO_set_fp(b,fp,BIO_NOCLOSE); CRYPTO_mem_leaks(b); BIO_free(b); }
static void load_builtin_compressions(void) { int got_write_lock = 0; CRYPTO_r_lock(CRYPTO_LOCK_SSL); if (ssl_comp_methods == NULL) { CRYPTO_r_unlock(CRYPTO_LOCK_SSL); CRYPTO_w_lock(CRYPTO_LOCK_SSL); got_write_lock = 1; if (ssl_comp_methods == NULL) { SSL_COMP *comp = NULL; MemCheck_off(); ssl_comp_methods=sk_SSL_COMP_new(sk_comp_cmp); if (ssl_comp_methods != NULL) { comp=(SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP)); if (comp != NULL) { comp->method=COMP_zlib(); if (comp->method && comp->method->type == NID_undef) OPENSSL_free(comp); else { comp->id=SSL_COMP_ZLIB_IDX; comp->name=comp->method->name; sk_SSL_COMP_push(ssl_comp_methods,comp); } } sk_SSL_COMP_sort(ssl_comp_methods); } MemCheck_on(); } } if (got_write_lock) CRYPTO_w_unlock(CRYPTO_LOCK_SSL); else CRYPTO_r_unlock(CRYPTO_LOCK_SSL); }
void CRYPTO_mem_leaks(BIO *b) { MEM_LEAK ml; if (mh == NULL && amih == NULL) return; MemCheck_off(); /* obtain MALLOC2 lock */ ml.bio=b; ml.bytes=0; ml.chunks=0; if (mh != NULL) lh_doall_arg(mh, LHASH_DOALL_ARG_FN(print_leak), (char *)&ml); if (ml.chunks != 0) { BIO_printf(b,"%ld bytes leaked in %d chunks\n", ml.bytes,ml.chunks); } 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(); /* release MALLOC2 lock */ }
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=CRYPTO_thread_id(); else m->thread=0; if (order == break_order_num) { /* BREAK HERE */ m->order=order; } m->order=order++; #ifdef LEVITTE_DEBUG_MEM fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5d] %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=CRYPTO_thread_id(); 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; }