GH540: add casts to safestack.h
[openssl.git] / crypto / mem_dbg.c
1 /* crypto/mem_dbg.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 /* ====================================================================
59  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
60  *
61  * Redistribution and use in source and binary forms, with or without
62  * modification, are permitted provided that the following conditions
63  * are met:
64  *
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer.
67  *
68  * 2. Redistributions in binary form must reproduce the above copyright
69  *    notice, this list of conditions and the following disclaimer in
70  *    the documentation and/or other materials provided with the
71  *    distribution.
72  *
73  * 3. All advertising materials mentioning features or use of this
74  *    software must display the following acknowledgment:
75  *    "This product includes software developed by the OpenSSL Project
76  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77  *
78  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79  *    endorse or promote products derived from this software without
80  *    prior written permission. For written permission, please contact
81  *    openssl-core@openssl.org.
82  *
83  * 5. Products derived from this software may not be called "OpenSSL"
84  *    nor may "OpenSSL" appear in their names without prior written
85  *    permission of the OpenSSL Project.
86  *
87  * 6. Redistributions of any form whatsoever must retain the following
88  *    acknowledgment:
89  *    "This product includes software developed by the OpenSSL Project
90  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91  *
92  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103  * OF THE POSSIBILITY OF SUCH DAMAGE.
104  * ====================================================================
105  *
106  * This product includes cryptographic software written by Eric Young
107  * (eay@cryptsoft.com).  This product includes software written by Tim
108  * Hudson (tjh@cryptsoft.com).
109  *
110  */
111
112 #include <stdio.h>
113 #include <stdlib.h>
114 #include <time.h>
115 #include "internal/cryptlib.h"
116 #include <openssl/crypto.h>
117 #include <openssl/buffer.h>
118 #include <openssl/bio.h>
119 #include <openssl/lhash.h>
120 #if defined(CRYPTO_MDEBUG_BACKTRACE) && defined(__GNUC__)
121 # include <execinfo.h>
122 #endif
123
124 /*
125  * The state changes to CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE when
126  * the application asks for it (usually after library initialisation for
127  * which no book-keeping is desired). State CRYPTO_MEM_CHECK_ON exists only
128  * temporarily when the library thinks that certain allocations should not be
129  * checked (e.g. the data structures used for memory checking).  It is not
130  * suitable as an initial state: the library will unexpectedly enable memory
131  * checking when it executes one of those sections that want to disable
132  * checking temporarily. State CRYPTO_MEM_CHECK_ENABLE without ..._ON makes
133  * no sense whatsoever.
134  */
135 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
136 static int mh_mode = CRYPTO_MEM_CHECK_OFF;
137 #endif
138
139 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
140 static unsigned long order = 0; /* number of memory requests */
141
142 DECLARE_LHASH_OF(MEM);
143 static LHASH_OF(MEM) *mh = NULL; /* hash-table of memory requests (address as
144                                   * key); access requires MALLOC2 lock */
145
146 /*-
147  * For application-defined information (static C-string `info')
148  * to be displayed in memory leak list.
149  * Each thread has its own stack.  For applications, there is
150  *   OPENSSL_mem_debug_push("...")     to push an entry,
151  *   OPENSSL_mem_debug_pop()     to pop an entry,
152  */
153 typedef struct app_mem_info_st {
154     CRYPTO_THREADID threadid;
155     const char *file;
156     int line;
157     const char *info;
158     struct app_mem_info_st *next; /* tail of thread's stack */
159     int references;
160 } APP_INFO;
161
162 /*
163  * hash-table with those app_mem_info_st's that are at the
164  * top of their thread's stack (with `thread' as key); access requires
165  * MALLOC2 lock
166  */
167 DECLARE_LHASH_OF(APP_INFO);
168 static LHASH_OF(APP_INFO) *amih = NULL;
169
170 /* memory-block description */
171 typedef struct mem_st {
172     void *addr;
173     int num;
174     const char *file;
175     int line;
176     CRYPTO_THREADID threadid;
177     unsigned long order;
178     time_t time;
179     APP_INFO *app_info;
180 #if defined(CRYPTO_MDEBUG_BACKTRACE) && defined(__GNUC__)
181     void *array[30];
182     size_t array_siz;
183 #endif
184 } MEM;
185
186 /* num_disable > 0 iff mh_mode == CRYPTO_MEM_CHECK_ON (w/o ..._ENABLE) */
187 static unsigned int num_disable = 0;
188
189 /*
190  * Valid iff num_disable > 0.  CRYPTO_LOCK_MALLOC2 is locked exactly in this
191  * case (by the thread named in disabling_thread).
192  */
193 static CRYPTO_THREADID disabling_threadid;
194
195 static void app_info_free(APP_INFO *inf)
196 {
197     if (!inf)
198         return;
199     if (--(inf->references) <= 0) {
200         app_info_free(inf->next);
201         OPENSSL_free(inf);
202     }
203 }
204 #endif
205
206 int CRYPTO_mem_ctrl(int mode)
207 {
208 #ifdef OPENSSL_NO_CRYPTO_MDEBUG
209     return mode - mode;
210 #else
211     int ret = mh_mode;
212
213     CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
214     switch (mode) {
215     default:
216         break;
217
218     case CRYPTO_MEM_CHECK_ON:
219         mh_mode = CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE;
220         num_disable = 0;
221         break;
222
223     case CRYPTO_MEM_CHECK_OFF:
224         mh_mode = 0;
225         num_disable = 0;
226         break;
227
228     /* switch off temporarily (for library-internal use): */
229     case CRYPTO_MEM_CHECK_DISABLE:
230         if (mh_mode & CRYPTO_MEM_CHECK_ON) {
231             CRYPTO_THREADID cur;
232             CRYPTO_THREADID_current(&cur);
233             /* see if we don't have the MALLOC2 lock already */
234             if (!num_disable
235                 || CRYPTO_THREADID_cmp(&disabling_threadid, &cur)) {
236                 /*
237                  * Long-time lock CRYPTO_LOCK_MALLOC2 must not be claimed
238                  * while we're holding CRYPTO_LOCK_MALLOC, or we'll deadlock
239                  * if somebody else holds CRYPTO_LOCK_MALLOC2 (and cannot
240                  * release it because we block entry to this function). Give
241                  * them a chance, first, and then claim the locks in
242                  * appropriate order (long-time lock first).
243                  */
244                 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
245                 /*
246                  * Note that after we have waited for CRYPTO_LOCK_MALLOC2 and
247                  * CRYPTO_LOCK_MALLOC, we'll still be in the right "case" and
248                  * "if" branch because MemCheck_start and MemCheck_stop may
249                  * never be used while there are multiple OpenSSL threads.
250                  */
251                 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC2);
252                 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
253                 mh_mode &= ~CRYPTO_MEM_CHECK_ENABLE;
254                 CRYPTO_THREADID_cpy(&disabling_threadid, &cur);
255             }
256             num_disable++;
257         }
258         break;
259
260     case CRYPTO_MEM_CHECK_ENABLE:
261         if (mh_mode & CRYPTO_MEM_CHECK_ON) {
262             if (num_disable) {  /* always true, or something is going wrong */
263                 num_disable--;
264                 if (num_disable == 0) {
265                     mh_mode |= CRYPTO_MEM_CHECK_ENABLE;
266                     CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2);
267                 }
268             }
269         }
270         break;
271     }
272     CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
273     return (ret);
274 #endif
275 }
276
277 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
278
279 static int mem_check_on(void)
280 {
281     int ret = 0;
282
283     if (mh_mode & CRYPTO_MEM_CHECK_ON) {
284         CRYPTO_THREADID cur;
285         CRYPTO_THREADID_current(&cur);
286         CRYPTO_r_lock(CRYPTO_LOCK_MALLOC);
287
288         ret = (mh_mode & CRYPTO_MEM_CHECK_ENABLE)
289             || CRYPTO_THREADID_cmp(&disabling_threadid, &cur);
290
291         CRYPTO_r_unlock(CRYPTO_LOCK_MALLOC);
292     }
293     return (ret);
294 }
295
296 static int mem_cmp(const MEM *a, const MEM *b)
297 {
298 #ifdef _WIN64
299     const char *ap = (const char *)a->addr, *bp = (const char *)b->addr;
300     if (ap == bp)
301         return 0;
302     else if (ap > bp)
303         return 1;
304     else
305         return -1;
306 #else
307     return (const char *)a->addr - (const char *)b->addr;
308 #endif
309 }
310
311 static IMPLEMENT_LHASH_COMP_FN(mem, MEM)
312
313 static unsigned long mem_hash(const MEM *a)
314 {
315     size_t ret;
316
317     ret = (size_t)a->addr;
318
319     ret = ret * 17851 + (ret >> 14) * 7 + (ret >> 4) * 251;
320     return (ret);
321 }
322
323 static IMPLEMENT_LHASH_HASH_FN(mem, MEM)
324
325 /* static int app_info_cmp(APP_INFO *a, APP_INFO *b) */
326 static int app_info_cmp(const void *a_void, const void *b_void)
327 {
328     return CRYPTO_THREADID_cmp(&((const APP_INFO *)a_void)->threadid,
329                                &((const APP_INFO *)b_void)->threadid);
330 }
331
332 static IMPLEMENT_LHASH_COMP_FN(app_info, APP_INFO)
333
334 static unsigned long app_info_hash(const APP_INFO *a)
335 {
336     unsigned long ret;
337
338     ret = CRYPTO_THREADID_hash(&a->threadid);
339     /* This is left in as a "who am I to question legacy?" measure */
340     ret = ret * 17851 + (ret >> 14) * 7 + (ret >> 4) * 251;
341     return (ret);
342 }
343
344 static IMPLEMENT_LHASH_HASH_FN(app_info, APP_INFO)
345
346 static APP_INFO *pop_info(void)
347 {
348     APP_INFO tmp;
349     APP_INFO *ret = NULL;
350
351     if (amih != NULL) {
352         CRYPTO_THREADID_current(&tmp.threadid);
353         if ((ret = lh_APP_INFO_delete(amih, &tmp)) != NULL) {
354             APP_INFO *next = ret->next;
355
356             if (next != NULL) {
357                 next->references++;
358                 (void)lh_APP_INFO_insert(amih, next);
359             }
360             if (--(ret->references) <= 0) {
361                 ret->next = NULL;
362                 if (next != NULL)
363                     next->references--;
364                 OPENSSL_free(ret);
365             }
366         }
367     }
368     return (ret);
369 }
370
371 int CRYPTO_mem_debug_push(const char *info, const char *file, int line)
372 {
373     APP_INFO *ami, *amim;
374     int ret = 0;
375
376     if (mem_check_on()) {
377         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
378
379         if ((ami = OPENSSL_malloc(sizeof(*ami))) == NULL)
380             goto err;
381         if (amih == NULL) {
382             if ((amih = lh_APP_INFO_new()) == NULL) {
383                 OPENSSL_free(ami);
384                 goto err;
385             }
386         }
387
388         CRYPTO_THREADID_current(&ami->threadid);
389         ami->file = file;
390         ami->line = line;
391         ami->info = info;
392         ami->references = 1;
393         ami->next = NULL;
394
395         if ((amim = lh_APP_INFO_insert(amih, ami)) != NULL)
396             ami->next = amim;
397         ret = 1;
398  err:
399         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
400     }
401
402     return (ret);
403 }
404
405 int CRYPTO_mem_debug_pop(void)
406 {
407     int ret = 0;
408
409     if (mem_check_on()) {
410         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
411         ret = (pop_info() != NULL);
412         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
413     }
414     return (ret);
415 }
416
417 static unsigned long break_order_num = 0;
418
419 void CRYPTO_mem_debug_malloc(void *addr, size_t num, int before_p,
420                              const char *file, int line)
421 {
422     MEM *m, *mm;
423     APP_INFO tmp, *amim;
424
425     switch (before_p & 127) {
426     case 0:
427         break;
428     case 1:
429         if (addr == NULL)
430             break;
431
432         if (mem_check_on()) {
433             CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
434             if ((m = OPENSSL_malloc(sizeof(*m))) == NULL) {
435                 OPENSSL_free(addr);
436                 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
437                 return;
438             }
439             if (mh == NULL) {
440                 if ((mh = lh_MEM_new()) == NULL) {
441                     OPENSSL_free(addr);
442                     OPENSSL_free(m);
443                     addr = NULL;
444                     goto err;
445                 }
446             }
447
448             m->addr = addr;
449             m->file = file;
450             m->line = line;
451             m->num = num;
452             CRYPTO_THREADID_current(&m->threadid);
453
454             if (order == break_order_num) {
455                 /* BREAK HERE */
456                 m->order = order;
457             }
458             m->order = order++;
459 # if defined(CRYPTO_MDEBUG_BACKTRACE) && defined(__GNUC__)
460             m->array_siz = backtrace(m->array, OSSL_NELEM(m->array));
461 # endif
462             m->time = time(NULL);
463
464             CRYPTO_THREADID_current(&tmp.threadid);
465             m->app_info = NULL;
466             if (amih != NULL
467                 && (amim = lh_APP_INFO_retrieve(amih, &tmp)) != NULL) {
468                 m->app_info = amim;
469                 amim->references++;
470             }
471
472             if ((mm = lh_MEM_insert(mh, m)) != NULL) {
473                 /* Not good, but don't sweat it */
474                 if (mm->app_info != NULL) {
475                     mm->app_info->references--;
476                 }
477                 OPENSSL_free(mm);
478             }
479  err:
480             CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
481         }
482         break;
483     }
484     return;
485 }
486
487 void CRYPTO_mem_debug_free(void *addr, int before_p)
488 {
489     MEM m, *mp;
490
491     switch (before_p) {
492     case 0:
493         if (addr == NULL)
494             break;
495
496         if (mem_check_on() && (mh != NULL)) {
497             CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
498
499             m.addr = addr;
500             mp = lh_MEM_delete(mh, &m);
501             if (mp != NULL) {
502                 app_info_free(mp->app_info);
503                 OPENSSL_free(mp);
504             }
505
506             CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
507         }
508         break;
509     case 1:
510         break;
511     }
512 }
513
514 void CRYPTO_mem_debug_realloc(void *addr1, void *addr2, size_t num,
515                               int before_p, const char *file, int line)
516 {
517     MEM m, *mp;
518
519     switch (before_p) {
520     case 0:
521         break;
522     case 1:
523         if (addr2 == NULL)
524             break;
525
526         if (addr1 == NULL) {
527             CRYPTO_mem_debug_malloc(addr2, num, 128 | before_p, file, line);
528             break;
529         }
530
531         if (mem_check_on()) {
532             CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
533
534             m.addr = addr1;
535             mp = lh_MEM_delete(mh, &m);
536             if (mp != NULL) {
537                 mp->addr = addr2;
538                 mp->num = num;
539 #if defined(CRYPTO_MDEBUG_BACKTRACE) && defined(__GNUC__)
540                 mp->array_siz = backtrace(mp->array, OSSL_NELEM(mp->array));
541 #endif
542                 (void)lh_MEM_insert(mh, mp);
543             }
544
545             CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
546         }
547         break;
548     }
549     return;
550 }
551
552 typedef struct mem_leak_st {
553     BIO *bio;
554     int chunks;
555     int seen;
556     long bytes;
557 } MEM_LEAK;
558
559 static void print_leak_doall_arg(const MEM *m, MEM_LEAK *l)
560 {
561     char buf[1024];
562     char *bufp = buf;
563     APP_INFO *amip;
564     int ami_cnt;
565     struct tm *lcl = NULL;
566     CRYPTO_THREADID ti;
567
568 #define BUF_REMAIN (sizeof buf - (size_t)(bufp - buf))
569
570     /* Is one "leak" the BIO we were given? */
571     if (m->addr == (char *)l->bio) {
572         l->seen = 1;
573         return;
574     }
575
576     lcl = localtime(&m->time);
577     BIO_snprintf(bufp, BUF_REMAIN, "[%02d:%02d:%02d] ",
578                  lcl->tm_hour, lcl->tm_min, lcl->tm_sec);
579     bufp += strlen(bufp);
580
581     BIO_snprintf(bufp, BUF_REMAIN, "%5lu file=%s, line=%d, ",
582                  m->order, m->file, m->line);
583     bufp += strlen(bufp);
584
585     BIO_snprintf(bufp, BUF_REMAIN, "thread=%lu, ",
586                  CRYPTO_THREADID_hash(&m->threadid));
587     bufp += strlen(bufp);
588
589     BIO_snprintf(bufp, BUF_REMAIN, "number=%d, address=%p\n",
590                  m->num, m->addr);
591     bufp += strlen(bufp);
592
593     BIO_puts(l->bio, buf);
594
595     l->chunks++;
596     l->bytes += m->num;
597
598     amip = m->app_info;
599     ami_cnt = 0;
600
601     if (amip) {
602         CRYPTO_THREADID_cpy(&ti, &amip->threadid);
603
604         do {
605             int buf_len;
606             int info_len;
607
608             ami_cnt++;
609             memset(buf, '>', ami_cnt);
610             BIO_snprintf(buf + ami_cnt, sizeof buf - ami_cnt,
611                          " thread=%lu, file=%s, line=%d, info=\"",
612                          CRYPTO_THREADID_hash(&amip->threadid), amip->file,
613                          amip->line);
614             buf_len = strlen(buf);
615             info_len = strlen(amip->info);
616             if (128 - buf_len - 3 < info_len) {
617                 memcpy(buf + buf_len, amip->info, 128 - buf_len - 3);
618                 buf_len = 128 - 3;
619             } else {
620                 OPENSSL_strlcpy(buf + buf_len, amip->info, sizeof buf - buf_len);
621                 buf_len = strlen(buf);
622             }
623             BIO_snprintf(buf + buf_len, sizeof buf - buf_len, "\"\n");
624
625             BIO_puts(l->bio, buf);
626
627             amip = amip->next;
628         }
629         while (amip && !CRYPTO_THREADID_cmp(&amip->threadid, &ti));
630     }
631
632 #if defined(CRYPTO_MDEBUG_BACKTRACE) && defined(__GNUC__)
633     {
634         size_t i;
635         char **strings = backtrace_symbols(m->array, m->array_siz);
636
637         for (i = 0; i < m->array_siz; i++)
638             fprintf(stderr, "##> %s\n", strings[i]);
639         free(strings);
640     }
641 #endif
642 }
643
644 static IMPLEMENT_LHASH_DOALL_ARG_FN(print_leak, const MEM, MEM_LEAK)
645
646 int CRYPTO_mem_leaks(BIO *b)
647 {
648     MEM_LEAK ml;
649
650     if (mh == NULL && amih == NULL)
651         return 1;
652
653     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
654
655     ml.bio = b;
656     ml.bytes = 0;
657     ml.chunks = 0;
658     ml.seen = 0;
659     if (mh != NULL)
660         lh_MEM_doall_arg(mh, LHASH_DOALL_ARG_FN(print_leak), MEM_LEAK, &ml);
661     /* Don't count the BIO that was passed in as a "leak" */
662     if (ml.seen && ml.chunks >= 1 && ml.bytes >= (int)sizeof (*b)) {
663         ml.chunks--;
664         ml.bytes -= (int)sizeof (*b);
665     }
666     if (ml.chunks != 0) {
667         BIO_printf(b, "%ld bytes leaked in %d chunks\n", ml.bytes, ml.chunks);
668     } else {
669         /*
670          * Make sure that, if we found no leaks, memory-leak debugging itself
671          * does not introduce memory leaks (which might irritate external
672          * debugging tools). (When someone enables leak checking, but does not
673          * call this function, we declare it to be their fault.)
674          */
675         int old_mh_mode;
676
677         CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
678
679         /*
680          * avoid deadlock when lh_free() uses CRYPTO_mem_debug_free(), which uses
681          * mem_check_on
682          */
683         old_mh_mode = mh_mode;
684         mh_mode = CRYPTO_MEM_CHECK_OFF;
685
686         lh_MEM_free(mh);
687         mh = NULL;
688         if (amih != NULL) {
689             if (lh_APP_INFO_num_items(amih) == 0) {
690                 lh_APP_INFO_free(amih);
691                 amih = NULL;
692             }
693         }
694
695         mh_mode = old_mh_mode;
696         CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
697     }
698     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
699     return ml.chunks == 0 ? 1 : 0;
700 }
701
702 # ifndef OPENSSL_NO_STDIO
703 int CRYPTO_mem_leaks_fp(FILE *fp)
704 {
705     BIO *b;
706     int ret;
707
708     if (mh == NULL)
709         return 0;
710     /*
711      * Need to turn off memory checking when allocated BIOs ... especially as
712      * we're creating them at a time when we're trying to check we've not
713      * left anything un-free()'d!!
714      */
715     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
716     b = BIO_new(BIO_s_file());
717     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
718     if (b == NULL)
719         return -1;
720     BIO_set_fp(b, fp, BIO_NOCLOSE);
721     ret = CRYPTO_mem_leaks(b);
722     BIO_free(b);
723     return ret;
724 }
725 # endif
726
727 #endif