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