diff options
Diffstat (limited to 'src/lib/libssl/src/crypto/mem_dbg.c')
-rw-r--r-- | src/lib/libssl/src/crypto/mem_dbg.c | 80 |
1 files changed, 49 insertions, 31 deletions
diff --git a/src/lib/libssl/src/crypto/mem_dbg.c b/src/lib/libssl/src/crypto/mem_dbg.c index ef19d8f844..1c4e04f51f 100644 --- a/src/lib/libssl/src/crypto/mem_dbg.c +++ b/src/lib/libssl/src/crypto/mem_dbg.c | |||
@@ -235,37 +235,43 @@ long CRYPTO_dbg_get_options(void) | |||
235 | return options; | 235 | return options; |
236 | } | 236 | } |
237 | 237 | ||
238 | static int mem_cmp(MEM *a, MEM *b) | 238 | /* static int mem_cmp(MEM *a, MEM *b) */ |
239 | static int mem_cmp(const void *a_void, const void *b_void) | ||
239 | { | 240 | { |
240 | return((char *)a->addr - (char *)b->addr); | 241 | return((const char *)((const MEM *)a_void)->addr |
242 | - (const char *)((const MEM *)b_void)->addr); | ||
241 | } | 243 | } |
242 | 244 | ||
243 | static unsigned long mem_hash(MEM *a) | 245 | /* static unsigned long mem_hash(MEM *a) */ |
246 | static unsigned long mem_hash(const void *a_void) | ||
244 | { | 247 | { |
245 | unsigned long ret; | 248 | unsigned long ret; |
246 | 249 | ||
247 | ret=(unsigned long)a->addr; | 250 | ret=(unsigned long)((const MEM *)a_void)->addr; |
248 | 251 | ||
249 | ret=ret*17851+(ret>>14)*7+(ret>>4)*251; | 252 | ret=ret*17851+(ret>>14)*7+(ret>>4)*251; |
250 | return(ret); | 253 | return(ret); |
251 | } | 254 | } |
252 | 255 | ||
253 | static int app_info_cmp(APP_INFO *a, APP_INFO *b) | 256 | /* static int app_info_cmp(APP_INFO *a, APP_INFO *b) */ |
257 | static int app_info_cmp(const void *a_void, const void *b_void) | ||
254 | { | 258 | { |
255 | return(a->thread != b->thread); | 259 | return(((const APP_INFO *)a_void)->thread |
260 | != ((const APP_INFO *)b_void)->thread); | ||
256 | } | 261 | } |
257 | 262 | ||
258 | static unsigned long app_info_hash(APP_INFO *a) | 263 | /* static unsigned long app_info_hash(APP_INFO *a) */ |
264 | static unsigned long app_info_hash(const void *a_void) | ||
259 | { | 265 | { |
260 | unsigned long ret; | 266 | unsigned long ret; |
261 | 267 | ||
262 | ret=(unsigned long)a->thread; | 268 | ret=(unsigned long)((const APP_INFO *)a_void)->thread; |
263 | 269 | ||
264 | ret=ret*17851+(ret>>14)*7+(ret>>4)*251; | 270 | ret=ret*17851+(ret>>14)*7+(ret>>4)*251; |
265 | return(ret); | 271 | return(ret); |
266 | } | 272 | } |
267 | 273 | ||
268 | static APP_INFO *pop_info() | 274 | static APP_INFO *pop_info(void) |
269 | { | 275 | { |
270 | APP_INFO tmp; | 276 | APP_INFO tmp; |
271 | APP_INFO *ret = NULL; | 277 | APP_INFO *ret = NULL; |
@@ -282,7 +288,7 @@ static APP_INFO *pop_info() | |||
282 | next->references++; | 288 | next->references++; |
283 | lh_insert(amih,(char *)next); | 289 | lh_insert(amih,(char *)next); |
284 | } | 290 | } |
285 | #ifdef LEVITTE_DEBUG | 291 | #ifdef LEVITTE_DEBUG_MEM |
286 | if (ret->thread != tmp.thread) | 292 | if (ret->thread != tmp.thread) |
287 | { | 293 | { |
288 | fprintf(stderr, "pop_info(): deleted info has other thread ID (%lu) than the current thread (%lu)!!!!\n", | 294 | fprintf(stderr, "pop_info(): deleted info has other thread ID (%lu) than the current thread (%lu)!!!!\n", |
@@ -318,7 +324,7 @@ int CRYPTO_push_info_(const char *info, const char *file, int line) | |||
318 | } | 324 | } |
319 | if (amih == NULL) | 325 | if (amih == NULL) |
320 | { | 326 | { |
321 | if ((amih=lh_new(app_info_hash,app_info_cmp)) == NULL) | 327 | if ((amih=lh_new(app_info_hash, app_info_cmp)) == NULL) |
322 | { | 328 | { |
323 | OPENSSL_free(ami); | 329 | OPENSSL_free(ami); |
324 | ret=0; | 330 | ret=0; |
@@ -335,7 +341,7 @@ int CRYPTO_push_info_(const char *info, const char *file, int line) | |||
335 | 341 | ||
336 | if ((amim=(APP_INFO *)lh_insert(amih,(char *)ami)) != NULL) | 342 | if ((amim=(APP_INFO *)lh_insert(amih,(char *)ami)) != NULL) |
337 | { | 343 | { |
338 | #ifdef LEVITTE_DEBUG | 344 | #ifdef LEVITTE_DEBUG_MEM |
339 | if (ami->thread != amim->thread) | 345 | if (ami->thread != amim->thread) |
340 | { | 346 | { |
341 | fprintf(stderr, "CRYPTO_push_info(): previous info has other thread ID (%lu) than the current thread (%lu)!!!!\n", | 347 | fprintf(stderr, "CRYPTO_push_info(): previous info has other thread ID (%lu) than the current thread (%lu)!!!!\n", |
@@ -411,7 +417,7 @@ void CRYPTO_dbg_malloc(void *addr, int num, const char *file, int line, | |||
411 | } | 417 | } |
412 | if (mh == NULL) | 418 | if (mh == NULL) |
413 | { | 419 | { |
414 | if ((mh=lh_new(mem_hash,mem_cmp)) == NULL) | 420 | if ((mh=lh_new(mem_hash, mem_cmp)) == NULL) |
415 | { | 421 | { |
416 | OPENSSL_free(addr); | 422 | OPENSSL_free(addr); |
417 | OPENSSL_free(m); | 423 | OPENSSL_free(m); |
@@ -435,8 +441,8 @@ void CRYPTO_dbg_malloc(void *addr, int num, const char *file, int line, | |||
435 | m->order=order; | 441 | m->order=order; |
436 | } | 442 | } |
437 | m->order=order++; | 443 | m->order=order++; |
438 | #ifdef LEVITTE_DEBUG | 444 | #ifdef LEVITTE_DEBUG_MEM |
439 | fprintf(stderr, "LEVITTE_DEBUG: [%5d] %c 0x%p (%d)\n", | 445 | fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5d] %c 0x%p (%d)\n", |
440 | m->order, | 446 | m->order, |
441 | (before_p & 128) ? '*' : '+', | 447 | (before_p & 128) ? '*' : '+', |
442 | m->addr, m->num); | 448 | m->addr, m->num); |
@@ -491,8 +497,8 @@ void CRYPTO_dbg_free(void *addr, int before_p) | |||
491 | mp=(MEM *)lh_delete(mh,(char *)&m); | 497 | mp=(MEM *)lh_delete(mh,(char *)&m); |
492 | if (mp != NULL) | 498 | if (mp != NULL) |
493 | { | 499 | { |
494 | #ifdef LEVITTE_DEBUG | 500 | #ifdef LEVITTE_DEBUG_MEM |
495 | fprintf(stderr, "LEVITTE_DEBUG: [%5d] - 0x%p (%d)\n", | 501 | fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5d] - 0x%p (%d)\n", |
496 | mp->order, mp->addr, mp->num); | 502 | mp->order, mp->addr, mp->num); |
497 | #endif | 503 | #endif |
498 | if (mp->app_info != NULL) | 504 | if (mp->app_info != NULL) |
@@ -516,8 +522,8 @@ void CRYPTO_dbg_realloc(void *addr1, void *addr2, int num, | |||
516 | { | 522 | { |
517 | MEM m,*mp; | 523 | MEM m,*mp; |
518 | 524 | ||
519 | #ifdef LEVITTE_DEBUG | 525 | #ifdef LEVITTE_DEBUG_MEM |
520 | fprintf(stderr, "LEVITTE_DEBUG: --> CRYPTO_dbg_malloc(addr1 = %p, addr2 = %p, num = %d, file = \"%s\", line = %d, before_p = %d)\n", | 526 | fprintf(stderr, "LEVITTE_DEBUG_MEM: --> CRYPTO_dbg_malloc(addr1 = %p, addr2 = %p, num = %d, file = \"%s\", line = %d, before_p = %d)\n", |
521 | addr1, addr2, num, file, line, before_p); | 527 | addr1, addr2, num, file, line, before_p); |
522 | #endif | 528 | #endif |
523 | 529 | ||
@@ -543,8 +549,8 @@ void CRYPTO_dbg_realloc(void *addr1, void *addr2, int num, | |||
543 | mp=(MEM *)lh_delete(mh,(char *)&m); | 549 | mp=(MEM *)lh_delete(mh,(char *)&m); |
544 | if (mp != NULL) | 550 | if (mp != NULL) |
545 | { | 551 | { |
546 | #ifdef LEVITTE_DEBUG | 552 | #ifdef LEVITTE_DEBUG_MEM |
547 | fprintf(stderr, "LEVITTE_DEBUG: [%5d] * 0x%p (%d) -> 0x%p (%d)\n", | 553 | fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5d] * 0x%p (%d) -> 0x%p (%d)\n", |
548 | mp->order, | 554 | mp->order, |
549 | mp->addr, mp->num, | 555 | mp->addr, mp->num, |
550 | addr2, num); | 556 | addr2, num); |
@@ -570,7 +576,7 @@ typedef struct mem_leak_st | |||
570 | long bytes; | 576 | long bytes; |
571 | } MEM_LEAK; | 577 | } MEM_LEAK; |
572 | 578 | ||
573 | static void print_leak(MEM *m, MEM_LEAK *l) | 579 | static void print_leak(const MEM *m, MEM_LEAK *l) |
574 | { | 580 | { |
575 | char buf[1024]; | 581 | char buf[1024]; |
576 | char *bufp = buf; | 582 | char *bufp = buf; |
@@ -646,7 +652,7 @@ static void print_leak(MEM *m, MEM_LEAK *l) | |||
646 | } | 652 | } |
647 | while(amip && amip->thread == ti); | 653 | while(amip && amip->thread == ti); |
648 | 654 | ||
649 | #ifdef LEVITTE_DEBUG | 655 | #ifdef LEVITTE_DEBUG_MEM |
650 | if (amip) | 656 | if (amip) |
651 | { | 657 | { |
652 | fprintf(stderr, "Thread switch detected in backtrace!!!!\n"); | 658 | fprintf(stderr, "Thread switch detected in backtrace!!!!\n"); |
@@ -655,6 +661,8 @@ static void print_leak(MEM *m, MEM_LEAK *l) | |||
655 | #endif | 661 | #endif |
656 | } | 662 | } |
657 | 663 | ||
664 | static IMPLEMENT_LHASH_DOALL_ARG_FN(print_leak, const MEM *, MEM_LEAK *) | ||
665 | |||
658 | void CRYPTO_mem_leaks(BIO *b) | 666 | void CRYPTO_mem_leaks(BIO *b) |
659 | { | 667 | { |
660 | MEM_LEAK ml; | 668 | MEM_LEAK ml; |
@@ -669,7 +677,8 @@ void CRYPTO_mem_leaks(BIO *b) | |||
669 | ml.bytes=0; | 677 | ml.bytes=0; |
670 | ml.chunks=0; | 678 | ml.chunks=0; |
671 | if (mh != NULL) | 679 | if (mh != NULL) |
672 | lh_doall_arg(mh,(void (*)())print_leak,(char *)&ml); | 680 | lh_doall_arg(mh, LHASH_DOALL_ARG_FN(print_leak), |
681 | (char *)&ml); | ||
673 | if (ml.chunks != 0) | 682 | if (ml.chunks != 0) |
674 | { | 683 | { |
675 | sprintf(buf,"%ld bytes leaked in %d chunks\n", | 684 | sprintf(buf,"%ld bytes leaked in %d chunks\n", |
@@ -722,14 +731,19 @@ void CRYPTO_mem_leaks(BIO *b) | |||
722 | MemCheck_on(); /* release MALLOC2 lock */ | 731 | MemCheck_on(); /* release MALLOC2 lock */ |
723 | } | 732 | } |
724 | 733 | ||
725 | #ifndef NO_FP_API | 734 | #ifndef OPENSSL_NO_FP_API |
726 | void CRYPTO_mem_leaks_fp(FILE *fp) | 735 | void CRYPTO_mem_leaks_fp(FILE *fp) |
727 | { | 736 | { |
728 | BIO *b; | 737 | BIO *b; |
729 | 738 | ||
730 | if (mh == NULL) return; | 739 | if (mh == NULL) return; |
731 | if ((b=BIO_new(BIO_s_file())) == NULL) | 740 | /* Need to turn off memory checking when allocated BIOs ... especially |
732 | return; | 741 | * as we're creating them at a time when we're trying to check we've not |
742 | * left anything un-free()'d!! */ | ||
743 | MemCheck_off(); | ||
744 | b = BIO_new(BIO_s_file()); | ||
745 | MemCheck_on(); | ||
746 | if(!b) return; | ||
733 | BIO_set_fp(b,fp,BIO_NOCLOSE); | 747 | BIO_set_fp(b,fp,BIO_NOCLOSE); |
734 | CRYPTO_mem_leaks(b); | 748 | CRYPTO_mem_leaks(b); |
735 | BIO_free(b); | 749 | BIO_free(b); |
@@ -741,16 +755,20 @@ void CRYPTO_mem_leaks_fp(FILE *fp) | |||
741 | /* FIXME: We really don't allow much to the callback. For example, it has | 755 | /* FIXME: We really don't allow much to the callback. For example, it has |
742 | no chance of reaching the info stack for the item it processes. Should | 756 | no chance of reaching the info stack for the item it processes. Should |
743 | it really be this way? -- Richard Levitte */ | 757 | it really be this way? -- Richard Levitte */ |
744 | static void cb_leak(MEM *m, | 758 | /* NB: The prototypes have been typedef'd to CRYPTO_MEM_LEAK_CB inside crypto.h |
745 | void (**cb)(unsigned long, const char *, int, int, void *)) | 759 | * If this code is restructured, remove the callback type if it is no longer |
760 | * needed. -- Geoff Thorpe */ | ||
761 | static void cb_leak(const MEM *m, CRYPTO_MEM_LEAK_CB **cb) | ||
746 | { | 762 | { |
747 | (**cb)(m->order,m->file,m->line,m->num,m->addr); | 763 | (**cb)(m->order,m->file,m->line,m->num,m->addr); |
748 | } | 764 | } |
749 | 765 | ||
750 | void CRYPTO_mem_leaks_cb(void (*cb)(unsigned long, const char *, int, int, void *)) | 766 | static IMPLEMENT_LHASH_DOALL_ARG_FN(cb_leak, const MEM *, CRYPTO_MEM_LEAK_CB **) |
767 | |||
768 | void CRYPTO_mem_leaks_cb(CRYPTO_MEM_LEAK_CB *cb) | ||
751 | { | 769 | { |
752 | if (mh == NULL) return; | 770 | if (mh == NULL) return; |
753 | CRYPTO_w_lock(CRYPTO_LOCK_MALLOC2); | 771 | CRYPTO_w_lock(CRYPTO_LOCK_MALLOC2); |
754 | lh_doall_arg(mh,(void (*)())cb_leak,(void *)&cb); | 772 | lh_doall_arg(mh, LHASH_DOALL_ARG_FN(cb_leak), &cb); |
755 | CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2); | 773 | CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2); |
756 | } | 774 | } |