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