2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
13 #include "internal/cryptlib.h"
14 #include <openssl/crypto.h>
15 #include <openssl/buffer.h>
16 #include "internal/bio.h"
17 #include <openssl/lhash.h>
19 #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
20 # include <execinfo.h>
24 * The state changes to CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE when
25 * the application asks for it (usually after library initialisation for
26 * which no book-keeping is desired). State CRYPTO_MEM_CHECK_ON exists only
27 * temporarily when the library thinks that certain allocations should not be
28 * checked (e.g. the data structures used for memory checking). It is not
29 * suitable as an initial state: the library will unexpectedly enable memory
30 * checking when it executes one of those sections that want to disable
31 * checking temporarily. State CRYPTO_MEM_CHECK_ENABLE without ..._ON makes
32 * no sense whatsoever.
34 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
35 static int mh_mode
= CRYPTO_MEM_CHECK_OFF
;
38 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
39 static unsigned long order
= 0; /* number of memory requests */
42 * For application-defined information (static C-string `info')
43 * to be displayed in memory leak list.
44 * Each thread has its own stack. For applications, there is
45 * OPENSSL_mem_debug_push("...") to push an entry,
46 * OPENSSL_mem_debug_pop() to pop an entry,
48 struct app_mem_info_st
{
49 CRYPTO_THREAD_ID threadid
;
53 struct app_mem_info_st
*next
; /* tail of thread's stack */
57 static CRYPTO_ONCE memdbg_init
= CRYPTO_ONCE_STATIC_INIT
;
58 static CRYPTO_RWLOCK
*malloc_lock
= NULL
;
59 static CRYPTO_RWLOCK
*long_malloc_lock
= NULL
;
60 static CRYPTO_THREAD_LOCAL appinfokey
;
62 /* memory-block description */
68 CRYPTO_THREAD_ID threadid
;
72 #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
78 static LHASH_OF(MEM
) *mh
= NULL
; /* hash-table of memory requests (address as
79 * key); access requires MALLOC2 lock */
81 /* num_disable > 0 iff mh_mode == CRYPTO_MEM_CHECK_ON (w/o ..._ENABLE) */
82 static unsigned int num_disable
= 0;
85 * Valid iff num_disable > 0. long_malloc_lock is locked exactly in this
86 * case (by the thread named in disabling_thread).
88 static CRYPTO_THREAD_ID disabling_threadid
;
90 static void do_memdbg_init(void)
92 malloc_lock
= CRYPTO_THREAD_lock_new();
93 long_malloc_lock
= CRYPTO_THREAD_lock_new();
94 CRYPTO_THREAD_init_local(&appinfokey
, NULL
);
97 static void app_info_free(APP_INFO
*inf
)
101 if (--(inf
->references
) <= 0) {
102 app_info_free(inf
->next
);
108 int CRYPTO_mem_ctrl(int mode
)
110 #ifdef OPENSSL_NO_CRYPTO_MDEBUG
115 CRYPTO_THREAD_run_once(&memdbg_init
, do_memdbg_init
);
117 CRYPTO_THREAD_write_lock(malloc_lock
);
122 case CRYPTO_MEM_CHECK_ON
:
123 mh_mode
= CRYPTO_MEM_CHECK_ON
| CRYPTO_MEM_CHECK_ENABLE
;
127 case CRYPTO_MEM_CHECK_OFF
:
132 /* switch off temporarily (for library-internal use): */
133 case CRYPTO_MEM_CHECK_DISABLE
:
134 if (mh_mode
& CRYPTO_MEM_CHECK_ON
) {
135 CRYPTO_THREAD_ID cur
= CRYPTO_THREAD_get_current_id();
136 /* see if we don't have long_malloc_lock already */
138 || !CRYPTO_THREAD_compare_id(disabling_threadid
, cur
)) {
140 * Long-time lock long_malloc_lock must not be claimed
141 * while we're holding malloc_lock, or we'll deadlock
142 * if somebody else holds long_malloc_lock (and cannot
143 * release it because we block entry to this function). Give
144 * them a chance, first, and then claim the locks in
145 * appropriate order (long-time lock first).
147 CRYPTO_THREAD_unlock(malloc_lock
);
149 * Note that after we have waited for long_malloc_lock and
150 * malloc_lock, we'll still be in the right "case" and
151 * "if" branch because MemCheck_start and MemCheck_stop may
152 * never be used while there are multiple OpenSSL threads.
154 CRYPTO_THREAD_write_lock(long_malloc_lock
);
155 CRYPTO_THREAD_write_lock(malloc_lock
);
156 mh_mode
&= ~CRYPTO_MEM_CHECK_ENABLE
;
157 disabling_threadid
= cur
;
163 case CRYPTO_MEM_CHECK_ENABLE
:
164 if (mh_mode
& CRYPTO_MEM_CHECK_ON
) {
165 if (num_disable
) { /* always true, or something is going wrong */
167 if (num_disable
== 0) {
168 mh_mode
|= CRYPTO_MEM_CHECK_ENABLE
;
169 CRYPTO_THREAD_unlock(long_malloc_lock
);
175 CRYPTO_THREAD_unlock(malloc_lock
);
180 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
182 static int mem_check_on(void)
185 CRYPTO_THREAD_ID cur
;
187 if (mh_mode
& CRYPTO_MEM_CHECK_ON
) {
188 CRYPTO_THREAD_run_once(&memdbg_init
, do_memdbg_init
);
190 cur
= CRYPTO_THREAD_get_current_id();
191 CRYPTO_THREAD_read_lock(malloc_lock
);
193 ret
= (mh_mode
& CRYPTO_MEM_CHECK_ENABLE
)
194 || !CRYPTO_THREAD_compare_id(disabling_threadid
, cur
);
196 CRYPTO_THREAD_unlock(malloc_lock
);
201 static int mem_cmp(const MEM
*a
, const MEM
*b
)
204 const char *ap
= (const char *)a
->addr
, *bp
= (const char *)b
->addr
;
212 return (const char *)a
->addr
- (const char *)b
->addr
;
216 static unsigned long mem_hash(const MEM
*a
)
220 ret
= (size_t)a
->addr
;
222 ret
= ret
* 17851 + (ret
>> 14) * 7 + (ret
>> 4) * 251;
226 /* returns 1 if there was an info to pop, 0 if the stack was empty. */
227 static int pop_info(void)
229 APP_INFO
*current
= NULL
;
231 CRYPTO_THREAD_run_once(&memdbg_init
, do_memdbg_init
);
232 current
= (APP_INFO
*)CRYPTO_THREAD_get_local(&appinfokey
);
233 if (current
!= NULL
) {
234 APP_INFO
*next
= current
->next
;
238 CRYPTO_THREAD_set_local(&appinfokey
, next
);
240 CRYPTO_THREAD_set_local(&appinfokey
, NULL
);
242 if (--(current
->references
) <= 0) {
243 current
->next
= NULL
;
246 OPENSSL_free(current
);
253 int CRYPTO_mem_debug_push(const char *info
, const char *file
, int line
)
255 APP_INFO
*ami
, *amim
;
258 if (mem_check_on()) {
259 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE
);
261 CRYPTO_THREAD_run_once(&memdbg_init
, do_memdbg_init
);
263 if ((ami
= OPENSSL_malloc(sizeof(*ami
))) == NULL
)
266 ami
->threadid
= CRYPTO_THREAD_get_current_id();
273 amim
= (APP_INFO
*)CRYPTO_THREAD_get_local(&appinfokey
);
274 CRYPTO_THREAD_set_local(&appinfokey
, ami
);
280 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE
);
286 int CRYPTO_mem_debug_pop(void)
290 if (mem_check_on()) {
291 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE
);
293 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE
);
298 static unsigned long break_order_num
= 0;
300 void CRYPTO_mem_debug_malloc(void *addr
, size_t num
, int before_p
,
301 const char *file
, int line
)
306 switch (before_p
& 127) {
313 if (mem_check_on()) {
314 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE
);
316 CRYPTO_THREAD_run_once(&memdbg_init
, do_memdbg_init
);
318 if ((m
= OPENSSL_malloc(sizeof(*m
))) == NULL
) {
320 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE
);
324 if ((mh
= lh_MEM_new(mem_hash
, mem_cmp
)) == NULL
) {
336 m
->threadid
= CRYPTO_THREAD_get_current_id();
338 if (order
== break_order_num
) {
343 # ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
344 m
->array_siz
= backtrace(m
->array
, OSSL_NELEM(m
->array
));
346 m
->time
= time(NULL
);
348 amim
= (APP_INFO
*)CRYPTO_THREAD_get_local(&appinfokey
);
353 if ((mm
= lh_MEM_insert(mh
, m
)) != NULL
) {
354 /* Not good, but don't sweat it */
355 if (mm
->app_info
!= NULL
) {
356 mm
->app_info
->references
--;
361 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE
);
368 void CRYPTO_mem_debug_free(void *addr
, int before_p
,
369 const char *file
, int line
)
378 if (mem_check_on() && (mh
!= NULL
)) {
379 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE
);
382 mp
= lh_MEM_delete(mh
, &m
);
384 app_info_free(mp
->app_info
);
388 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE
);
396 void CRYPTO_mem_debug_realloc(void *addr1
, void *addr2
, size_t num
,
397 int before_p
, const char *file
, int line
)
409 CRYPTO_mem_debug_malloc(addr2
, num
, 128 | before_p
, file
, line
);
413 if (mem_check_on()) {
414 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE
);
417 mp
= lh_MEM_delete(mh
, &m
);
421 #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
422 mp
->array_siz
= backtrace(mp
->array
, OSSL_NELEM(mp
->array
));
424 (void)lh_MEM_insert(mh
, mp
);
427 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE
);
434 typedef struct mem_leak_st
{
440 static void print_leak(const MEM
*m
, MEM_LEAK
*l
)
446 struct tm
*lcl
= NULL
;
448 * Convert between CRYPTO_THREAD_ID (which could be anything at all) and
449 * a long. This may not be meaningful depending on what CRYPTO_THREAD_ID is
450 * but hopefully should give something sensible on most platforms
453 CRYPTO_THREAD_ID tid
;
458 #define BUF_REMAIN (sizeof buf - (size_t)(bufp - buf))
460 lcl
= localtime(&m
->time
);
461 BIO_snprintf(bufp
, BUF_REMAIN
, "[%02d:%02d:%02d] ",
462 lcl
->tm_hour
, lcl
->tm_min
, lcl
->tm_sec
);
463 bufp
+= strlen(bufp
);
465 BIO_snprintf(bufp
, BUF_REMAIN
, "%5lu file=%s, line=%d, ",
466 m
->order
, m
->file
, m
->line
);
467 bufp
+= strlen(bufp
);
470 tid
.tid
= m
->threadid
;
471 BIO_snprintf(bufp
, BUF_REMAIN
, "thread=%lu, ", tid
.ltid
);
472 bufp
+= strlen(bufp
);
474 BIO_snprintf(bufp
, BUF_REMAIN
, "number=%d, address=%p\n",
476 bufp
+= strlen(bufp
);
478 BIO_puts(l
->bio
, buf
);
494 memset(buf
, '>', ami_cnt
);
496 tid
.tid
= amip
->threadid
;
497 BIO_snprintf(buf
+ ami_cnt
, sizeof buf
- ami_cnt
,
498 " thread=%lu, file=%s, line=%d, info=\"",
499 tid
.ltid
, amip
->file
,
501 buf_len
= strlen(buf
);
502 info_len
= strlen(amip
->info
);
503 if (128 - buf_len
- 3 < info_len
) {
504 memcpy(buf
+ buf_len
, amip
->info
, 128 - buf_len
- 3);
507 OPENSSL_strlcpy(buf
+ buf_len
, amip
->info
, sizeof buf
- buf_len
);
508 buf_len
= strlen(buf
);
510 BIO_snprintf(buf
+ buf_len
, sizeof buf
- buf_len
, "\"\n");
512 BIO_puts(l
->bio
, buf
);
516 while (amip
&& CRYPTO_THREAD_compare_id(amip
->threadid
, ti
));
519 #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
522 char **strings
= backtrace_symbols(m
->array
, m
->array_siz
);
524 for (i
= 0; i
< m
->array_siz
; i
++)
525 fprintf(stderr
, "##> %s\n", strings
[i
]);
531 IMPLEMENT_LHASH_DOALL_ARG_CONST(MEM
, MEM_LEAK
);
533 int CRYPTO_mem_leaks(BIO
*b
)
538 * OPENSSL_cleanup() will free the ex_data locks so we can't have any
539 * ex_data hanging around
543 /* Ensure all resources are released */
546 CRYPTO_THREAD_run_once(&memdbg_init
, do_memdbg_init
);
548 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE
);
554 lh_MEM_doall_MEM_LEAK(mh
, print_leak
, &ml
);
556 if (ml
.chunks
!= 0) {
557 BIO_printf(b
, "%ld bytes leaked in %d chunks\n", ml
.bytes
, ml
.chunks
);
560 * Make sure that, if we found no leaks, memory-leak debugging itself
561 * does not introduce memory leaks (which might irritate external
562 * debugging tools). (When someone enables leak checking, but does not
563 * call this function, we declare it to be their fault.)
567 CRYPTO_THREAD_write_lock(malloc_lock
);
570 * avoid deadlock when lh_free() uses CRYPTO_mem_debug_free(), which uses
573 old_mh_mode
= mh_mode
;
574 mh_mode
= CRYPTO_MEM_CHECK_OFF
;
579 mh_mode
= old_mh_mode
;
580 CRYPTO_THREAD_unlock(malloc_lock
);
582 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_OFF
);
584 /* Clean up locks etc */
585 CRYPTO_THREAD_cleanup_local(&appinfokey
);
586 CRYPTO_THREAD_lock_free(malloc_lock
);
587 CRYPTO_THREAD_lock_free(long_malloc_lock
);
589 long_malloc_lock
= NULL
;
591 return ml
.chunks
== 0 ? 1 : 0;
594 # ifndef OPENSSL_NO_STDIO
595 int CRYPTO_mem_leaks_fp(FILE *fp
)
601 * Need to turn off memory checking when allocated BIOs ... especially as
602 * we're creating them at a time when we're trying to check we've not
603 * left anything un-free()'d!!
605 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE
);
606 b
= BIO_new(BIO_s_file());
607 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE
);
610 BIO_set_fp(b
, fp
, BIO_NOCLOSE
);
611 ret
= CRYPTO_mem_leaks(b
);