]>
Commit | Line | Data |
---|---|---|
9ac42ed8 | 1 | /* crypto/mem_dbg.c */ |
08807172 RL |
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | * All rights reserved. | |
9ac42ed8 | 4 | * |
08807172 RL |
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. | |
0f113f3e | 8 | * |
08807172 RL |
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). | |
0f113f3e | 15 | * |
08807172 RL |
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. | |
0f113f3e | 22 | * |
9ac42ed8 RL |
23 | * Redistribution and use in source and binary forms, with or without |
24 | * modification, are permitted provided that the following conditions | |
25 | * are met: | |
08807172 RL |
26 | * 1. Redistributions of source code must retain the copyright |
27 | * notice, this list of conditions and the following disclaimer. | |
9ac42ed8 | 28 | * 2. Redistributions in binary form must reproduce the above copyright |
08807172 RL |
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 :-). | |
0f113f3e | 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
08807172 RL |
38 | * the apps directory (application code) you must include an acknowledgement: |
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 40 | * |
08807172 RL |
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. | |
0f113f3e | 52 | * |
08807172 RL |
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.] | |
9ac42ed8 | 57 | */ |
48fc582f BM |
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 | |
0f113f3e | 66 | * notice, this list of conditions and the following disclaimer. |
48fc582f BM |
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 | */ | |
9ac42ed8 RL |
111 | |
112 | #include <stdio.h> | |
113 | #include <stdlib.h> | |
0f113f3e | 114 | #include <time.h> |
b39fc560 | 115 | #include "internal/cryptlib.h" |
9ac42ed8 RL |
116 | #include <openssl/crypto.h> |
117 | #include <openssl/buffer.h> | |
118 | #include <openssl/bio.h> | |
119 | #include <openssl/lhash.h> | |
012c5408 RL |
120 | #if defined(CRYPTO_MDEBUG_BACKTRACE) && defined(__GNUC__) |
121 | # include <execinfo.h> | |
122 | #endif | |
9ac42ed8 | 123 | |
0f113f3e MC |
124 | static int mh_mode = CRYPTO_MEM_CHECK_OFF; |
125 | /* | |
126 | * The state changes to CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE when | |
127 | * the application asks for it (usually after library initialisation for | |
128 | * which no book-keeping is desired). State CRYPTO_MEM_CHECK_ON exists only | |
129 | * temporarily when the library thinks that certain allocations should not be | |
130 | * checked (e.g. the data structures used for memory checking). It is not | |
131 | * suitable as an initial state: the library will unexpectedly enable memory | |
132 | * checking when it executes one of those sections that want to disable | |
133 | * checking temporarily. State CRYPTO_MEM_CHECK_ENABLE without ..._ON makes | |
134 | * no sense whatsoever. | |
9ac42ed8 | 135 | */ |
9ac42ed8 | 136 | |
0cd08cce | 137 | static unsigned long order = 0; /* number of memory requests */ |
3c1d6bbc BL |
138 | |
139 | DECLARE_LHASH_OF(MEM); | |
0f113f3e MC |
140 | static LHASH_OF(MEM) *mh = NULL; /* hash-table of memory requests (address as |
141 | * key); access requires MALLOC2 lock */ | |
9ac42ed8 RL |
142 | |
143 | typedef struct app_mem_info_st | |
c80fd6b2 MC |
144 | /*- |
145 | * For application-defined information (static C-string `info') | |
0cd08cce BM |
146 | * to be displayed in memory leak list. |
147 | * Each thread has its own stack. For applications, there is | |
2b6313d0 BM |
148 | * CRYPTO_push_info("...") to push an entry, |
149 | * CRYPTO_pop_info() to pop an entry, | |
150 | * CRYPTO_remove_all_info() to pop all entries. | |
0cd08cce | 151 | */ |
0f113f3e MC |
152 | { |
153 | CRYPTO_THREADID threadid; | |
154 | const char *file; | |
155 | int line; | |
156 | const char *info; | |
157 | struct app_mem_info_st *next; /* tail of thread's stack */ | |
158 | int references; | |
159 | } APP_INFO; | |
9ac42ed8 | 160 | |
9dc61049 BM |
161 | static void app_info_free(APP_INFO *); |
162 | ||
3c1d6bbc | 163 | DECLARE_LHASH_OF(APP_INFO); |
0f113f3e MC |
164 | static LHASH_OF(APP_INFO) *amih = NULL; /* hash-table with those |
165 | * app_mem_info_st's that are at the | |
166 | * top of their thread's stack (with | |
167 | * `thread' as key); access requires | |
168 | * MALLOC2 lock */ | |
9ac42ed8 RL |
169 | |
170 | typedef struct mem_st | |
0cd08cce | 171 | /* memory-block description */ |
0f113f3e MC |
172 | { |
173 | void *addr; | |
174 | int num; | |
175 | const char *file; | |
176 | int line; | |
177 | CRYPTO_THREADID threadid; | |
178 | unsigned long order; | |
179 | time_t time; | |
180 | APP_INFO *app_info; | |
012c5408 RL |
181 | #if defined(CRYPTO_MDEBUG_BACKTRACE) && defined(__GNUC__) |
182 | void *array[30]; | |
183 | size_t array_siz; | |
184 | #endif | |
0f113f3e MC |
185 | } MEM; |
186 | ||
187 | static long options = /* extra information to be recorded */ | |
0cd08cce | 188 | #if defined(CRYPTO_MDEBUG_TIME) || defined(CRYPTO_MDEBUG_ALL) |
0f113f3e | 189 | V_CRYPTO_MDEBUG_TIME | |
d8df48a9 | 190 | #endif |
0cd08cce | 191 | #if defined(CRYPTO_MDEBUG_THREAD) || defined(CRYPTO_MDEBUG_ALL) |
0f113f3e | 192 | V_CRYPTO_MDEBUG_THREAD | |
d8df48a9 | 193 | #endif |
0f113f3e | 194 | 0; |
9ac42ed8 | 195 | |
0f113f3e MC |
196 | static unsigned int num_disable = 0; /* num_disable > 0 iff mh_mode == |
197 | * CRYPTO_MEM_CHECK_ON (w/o ..._ENABLE) */ | |
48fc582f | 198 | |
0f113f3e MC |
199 | /* |
200 | * Valid iff num_disable > 0. CRYPTO_LOCK_MALLOC2 is locked exactly in this | |
4c329696 | 201 | * case (by the thread named in disabling_thread). |
5f834ab1 | 202 | */ |
4c329696 | 203 | static CRYPTO_THREADID disabling_threadid; |
9ac42ed8 | 204 | |
9dc61049 | 205 | static void app_info_free(APP_INFO *inf) |
0f113f3e | 206 | { |
25aaa98a RS |
207 | if (!inf) |
208 | return; | |
0f113f3e | 209 | if (--(inf->references) <= 0) { |
25aaa98a | 210 | app_info_free(inf->next); |
0f113f3e MC |
211 | OPENSSL_free(inf); |
212 | } | |
213 | } | |
9dc61049 | 214 | |
9ac42ed8 | 215 | int CRYPTO_mem_ctrl(int mode) |
0f113f3e MC |
216 | { |
217 | int ret = mh_mode; | |
218 | ||
219 | CRYPTO_w_lock(CRYPTO_LOCK_MALLOC); | |
220 | switch (mode) { | |
221 | /* | |
222 | * for applications (not to be called while multiple threads use the | |
223 | * library): | |
224 | */ | |
225 | case CRYPTO_MEM_CHECK_ON: /* aka MemCheck_start() */ | |
226 | mh_mode = CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE; | |
227 | num_disable = 0; | |
228 | break; | |
229 | case CRYPTO_MEM_CHECK_OFF: /* aka MemCheck_stop() */ | |
230 | mh_mode = 0; | |
231 | num_disable = 0; /* should be true *before* MemCheck_stop is | |
232 | * used, or there'll be a lot of confusion */ | |
233 | break; | |
234 | ||
235 | /* switch off temporarily (for library-internal use): */ | |
236 | case CRYPTO_MEM_CHECK_DISABLE: /* aka MemCheck_off() */ | |
237 | if (mh_mode & CRYPTO_MEM_CHECK_ON) { | |
238 | CRYPTO_THREADID cur; | |
239 | CRYPTO_THREADID_current(&cur); | |
240 | /* see if we don't have the MALLOC2 lock already */ | |
241 | if (!num_disable | |
242 | || CRYPTO_THREADID_cmp(&disabling_threadid, &cur)) { | |
243 | /* | |
244 | * Long-time lock CRYPTO_LOCK_MALLOC2 must not be claimed | |
245 | * while we're holding CRYPTO_LOCK_MALLOC, or we'll deadlock | |
246 | * if somebody else holds CRYPTO_LOCK_MALLOC2 (and cannot | |
247 | * release it because we block entry to this function). Give | |
248 | * them a chance, first, and then claim the locks in | |
249 | * appropriate order (long-time lock first). | |
250 | */ | |
251 | CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC); | |
252 | /* | |
253 | * Note that after we have waited for CRYPTO_LOCK_MALLOC2 and | |
254 | * CRYPTO_LOCK_MALLOC, we'll still be in the right "case" and | |
255 | * "if" branch because MemCheck_start and MemCheck_stop may | |
256 | * never be used while there are multiple OpenSSL threads. | |
257 | */ | |
258 | CRYPTO_w_lock(CRYPTO_LOCK_MALLOC2); | |
259 | CRYPTO_w_lock(CRYPTO_LOCK_MALLOC); | |
260 | mh_mode &= ~CRYPTO_MEM_CHECK_ENABLE; | |
261 | CRYPTO_THREADID_cpy(&disabling_threadid, &cur); | |
262 | } | |
263 | num_disable++; | |
264 | } | |
265 | break; | |
266 | case CRYPTO_MEM_CHECK_ENABLE: /* aka MemCheck_on() */ | |
267 | if (mh_mode & CRYPTO_MEM_CHECK_ON) { | |
268 | if (num_disable) { /* always true, or something is going wrong */ | |
269 | num_disable--; | |
270 | if (num_disable == 0) { | |
271 | mh_mode |= CRYPTO_MEM_CHECK_ENABLE; | |
272 | CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2); | |
273 | } | |
274 | } | |
275 | } | |
276 | break; | |
277 | ||
278 | default: | |
279 | break; | |
280 | } | |
281 | CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC); | |
282 | return (ret); | |
283 | } | |
9ac42ed8 | 284 | |
0cd08cce | 285 | int CRYPTO_is_mem_check_on(void) |
0f113f3e MC |
286 | { |
287 | int ret = 0; | |
9ac42ed8 | 288 | |
0f113f3e MC |
289 | if (mh_mode & CRYPTO_MEM_CHECK_ON) { |
290 | CRYPTO_THREADID cur; | |
291 | CRYPTO_THREADID_current(&cur); | |
292 | CRYPTO_r_lock(CRYPTO_LOCK_MALLOC); | |
9ac42ed8 | 293 | |
0f113f3e MC |
294 | ret = (mh_mode & CRYPTO_MEM_CHECK_ENABLE) |
295 | || CRYPTO_THREADID_cmp(&disabling_threadid, &cur); | |
9ac42ed8 | 296 | |
0f113f3e MC |
297 | CRYPTO_r_unlock(CRYPTO_LOCK_MALLOC); |
298 | } | |
299 | return (ret); | |
300 | } | |
9ac42ed8 | 301 | |
0cd08cce | 302 | void CRYPTO_dbg_set_options(long bits) |
0f113f3e MC |
303 | { |
304 | options = bits; | |
305 | } | |
9ac42ed8 | 306 | |
667ac4ec | 307 | long CRYPTO_dbg_get_options(void) |
0f113f3e MC |
308 | { |
309 | return options; | |
310 | } | |
9ac42ed8 | 311 | |
3c1d6bbc | 312 | static int mem_cmp(const MEM *a, const MEM *b) |
0f113f3e | 313 | { |
1875e6db | 314 | #ifdef _WIN64 |
0f113f3e MC |
315 | const char *ap = (const char *)a->addr, *bp = (const char *)b->addr; |
316 | if (ap == bp) | |
317 | return 0; | |
318 | else if (ap > bp) | |
319 | return 1; | |
320 | else | |
321 | return -1; | |
1875e6db | 322 | #else |
0f113f3e | 323 | return (const char *)a->addr - (const char *)b->addr; |
1875e6db | 324 | #endif |
0f113f3e MC |
325 | } |
326 | ||
3c1d6bbc | 327 | static IMPLEMENT_LHASH_COMP_FN(mem, MEM) |
9ac42ed8 | 328 | |
3c1d6bbc | 329 | static unsigned long mem_hash(const MEM *a) |
0f113f3e | 330 | { |
5f0580cc | 331 | size_t ret; |
9ac42ed8 | 332 | |
5f0580cc | 333 | ret = (size_t)a->addr; |
0f113f3e MC |
334 | |
335 | ret = ret * 17851 + (ret >> 14) * 7 + (ret >> 4) * 251; | |
336 | return (ret); | |
337 | } | |
9ac42ed8 | 338 | |
3c1d6bbc | 339 | static IMPLEMENT_LHASH_HASH_FN(mem, MEM) |
9ac42ed8 | 340 | |
d0fa136c | 341 | /* static int app_info_cmp(APP_INFO *a, APP_INFO *b) */ |
8d28d5f8 | 342 | static int app_info_cmp(const void *a_void, const void *b_void) |
0f113f3e MC |
343 | { |
344 | return CRYPTO_THREADID_cmp(&((const APP_INFO *)a_void)->threadid, | |
345 | &((const APP_INFO *)b_void)->threadid); | |
346 | } | |
347 | ||
3c1d6bbc | 348 | static IMPLEMENT_LHASH_COMP_FN(app_info, APP_INFO) |
9ac42ed8 | 349 | |
3c1d6bbc | 350 | static unsigned long app_info_hash(const APP_INFO *a) |
0f113f3e MC |
351 | { |
352 | unsigned long ret; | |
353 | ||
354 | ret = CRYPTO_THREADID_hash(&a->threadid); | |
355 | /* This is left in as a "who am I to question legacy?" measure */ | |
356 | ret = ret * 17851 + (ret >> 14) * 7 + (ret >> 4) * 251; | |
357 | return (ret); | |
358 | } | |
359 | ||
3c1d6bbc | 360 | static IMPLEMENT_LHASH_HASH_FN(app_info, APP_INFO) |
9ac42ed8 | 361 | |
fe8686ba | 362 | static APP_INFO *pop_info(void) |
0f113f3e MC |
363 | { |
364 | APP_INFO tmp; | |
365 | APP_INFO *ret = NULL; | |
366 | ||
367 | if (amih != NULL) { | |
368 | CRYPTO_THREADID_current(&tmp.threadid); | |
369 | if ((ret = lh_APP_INFO_delete(amih, &tmp)) != NULL) { | |
370 | APP_INFO *next = ret->next; | |
371 | ||
372 | if (next != NULL) { | |
373 | next->references++; | |
374 | (void)lh_APP_INFO_insert(amih, next); | |
375 | } | |
3d6001f7 | 376 | #ifdef LEVITTE_DEBUG_MEM |
0f113f3e MC |
377 | if (CRYPTO_THREADID_cmp(&ret->threadid, &tmp.threadid)) { |
378 | fprintf(stderr, | |
379 | "pop_info(): deleted info has other thread ID (%lu) than the current thread (%lu)!!!!\n", | |
380 | CRYPTO_THREADID_hash(&ret->threadid), | |
381 | CRYPTO_THREADID_hash(&tmp.threadid)); | |
382 | abort(); | |
383 | } | |
9ac42ed8 | 384 | #endif |
0f113f3e MC |
385 | if (--(ret->references) <= 0) { |
386 | ret->next = NULL; | |
387 | if (next != NULL) | |
388 | next->references--; | |
389 | OPENSSL_free(ret); | |
390 | } | |
391 | } | |
392 | } | |
393 | return (ret); | |
394 | } | |
9ac42ed8 | 395 | |
2b6313d0 | 396 | int CRYPTO_push_info_(const char *info, const char *file, int line) |
0f113f3e MC |
397 | { |
398 | APP_INFO *ami, *amim; | |
399 | int ret = 0; | |
400 | ||
401 | if (is_MemCheck_on()) { | |
402 | MemCheck_off(); /* obtain MALLOC2 lock */ | |
403 | ||
b4faea50 | 404 | if ((ami = OPENSSL_malloc(sizeof(*ami))) == NULL) { |
0f113f3e MC |
405 | ret = 0; |
406 | goto err; | |
407 | } | |
408 | if (amih == NULL) { | |
409 | if ((amih = lh_APP_INFO_new()) == NULL) { | |
410 | OPENSSL_free(ami); | |
411 | ret = 0; | |
412 | goto err; | |
413 | } | |
414 | } | |
415 | ||
416 | CRYPTO_THREADID_current(&ami->threadid); | |
417 | ami->file = file; | |
418 | ami->line = line; | |
419 | ami->info = info; | |
420 | ami->references = 1; | |
421 | ami->next = NULL; | |
422 | ||
423 | if ((amim = lh_APP_INFO_insert(amih, ami)) != NULL) { | |
3d6001f7 | 424 | #ifdef LEVITTE_DEBUG_MEM |
0f113f3e MC |
425 | if (CRYPTO_THREADID_cmp(&ami->threadid, &amim->threadid)) { |
426 | fprintf(stderr, | |
427 | "CRYPTO_push_info(): previous info has other thread ID (%lu) than the current thread (%lu)!!!!\n", | |
428 | CRYPTO_THREADID_hash(&amim->threadid), | |
429 | CRYPTO_THREADID_hash(&ami->threadid)); | |
430 | abort(); | |
431 | } | |
9ac42ed8 | 432 | #endif |
0f113f3e MC |
433 | ami->next = amim; |
434 | } | |
9ac42ed8 | 435 | err: |
0f113f3e MC |
436 | MemCheck_on(); /* release MALLOC2 lock */ |
437 | } | |
9ac42ed8 | 438 | |
0f113f3e MC |
439 | return (ret); |
440 | } | |
9ac42ed8 | 441 | |
2b6313d0 | 442 | int CRYPTO_pop_info(void) |
0f113f3e MC |
443 | { |
444 | int ret = 0; | |
9ac42ed8 | 445 | |
0f113f3e MC |
446 | if (is_MemCheck_on()) { /* _must_ be true, or something went severely |
447 | * wrong */ | |
448 | MemCheck_off(); /* obtain MALLOC2 lock */ | |
9ac42ed8 | 449 | |
0f113f3e | 450 | ret = (pop_info() != NULL); |
9ac42ed8 | 451 | |
0f113f3e MC |
452 | MemCheck_on(); /* release MALLOC2 lock */ |
453 | } | |
454 | return (ret); | |
455 | } | |
9ac42ed8 RL |
456 | |
457 | int CRYPTO_remove_all_info(void) | |
0f113f3e MC |
458 | { |
459 | int ret = 0; | |
9ac42ed8 | 460 | |
0f113f3e MC |
461 | if (is_MemCheck_on()) { /* _must_ be true */ |
462 | MemCheck_off(); /* obtain MALLOC2 lock */ | |
9ac42ed8 | 463 | |
0f113f3e MC |
464 | while (pop_info() != NULL) |
465 | ret++; | |
9ac42ed8 | 466 | |
0f113f3e MC |
467 | MemCheck_on(); /* release MALLOC2 lock */ |
468 | } | |
469 | return (ret); | |
470 | } | |
9ac42ed8 | 471 | |
0f113f3e | 472 | static unsigned long break_order_num = 0; |
6343829a | 473 | void CRYPTO_dbg_malloc(void *addr, int num, const char *file, int line, |
0f113f3e MC |
474 | int before_p) |
475 | { | |
476 | MEM *m, *mm; | |
477 | APP_INFO tmp, *amim; | |
478 | ||
479 | switch (before_p & 127) { | |
480 | case 0: | |
481 | break; | |
482 | case 1: | |
483 | if (addr == NULL) | |
484 | break; | |
485 | ||
486 | if (is_MemCheck_on()) { | |
487 | MemCheck_off(); /* make sure we hold MALLOC2 lock */ | |
b4faea50 | 488 | if ((m = OPENSSL_malloc(sizeof(*m))) == NULL) { |
0f113f3e MC |
489 | OPENSSL_free(addr); |
490 | MemCheck_on(); /* release MALLOC2 lock if num_disabled drops | |
491 | * to 0 */ | |
492 | return; | |
493 | } | |
494 | if (mh == NULL) { | |
495 | if ((mh = lh_MEM_new()) == NULL) { | |
496 | OPENSSL_free(addr); | |
497 | OPENSSL_free(m); | |
498 | addr = NULL; | |
499 | goto err; | |
500 | } | |
501 | } | |
502 | ||
503 | m->addr = addr; | |
504 | m->file = file; | |
505 | m->line = line; | |
506 | m->num = num; | |
507 | if (options & V_CRYPTO_MDEBUG_THREAD) | |
508 | CRYPTO_THREADID_current(&m->threadid); | |
509 | else | |
510 | memset(&m->threadid, 0, sizeof(m->threadid)); | |
511 | ||
512 | if (order == break_order_num) { | |
513 | /* BREAK HERE */ | |
514 | m->order = order; | |
515 | } | |
516 | m->order = order++; | |
3d6001f7 | 517 | #ifdef LEVITTE_DEBUG_MEM |
0f113f3e MC |
518 | fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5ld] %c 0x%p (%d)\n", |
519 | m->order, (before_p & 128) ? '*' : '+', m->addr, m->num); | |
9ac42ed8 | 520 | #endif |
0f113f3e MC |
521 | if (options & V_CRYPTO_MDEBUG_TIME) |
522 | m->time = time(NULL); | |
523 | else | |
524 | m->time = 0; | |
012c5408 RL |
525 | #if defined(CRYPTO_MDEBUG_BACKTRACE) && defined(__GNUC__) |
526 | m->array_siz = backtrace(m->array, OSSL_NELEM(m->array)); | |
527 | #endif | |
0f113f3e MC |
528 | |
529 | CRYPTO_THREADID_current(&tmp.threadid); | |
530 | m->app_info = NULL; | |
531 | if (amih != NULL | |
532 | && (amim = lh_APP_INFO_retrieve(amih, &tmp)) != NULL) { | |
533 | m->app_info = amim; | |
534 | amim->references++; | |
535 | } | |
536 | ||
537 | if ((mm = lh_MEM_insert(mh, m)) != NULL) { | |
538 | /* Not good, but don't sweat it */ | |
539 | if (mm->app_info != NULL) { | |
540 | mm->app_info->references--; | |
541 | } | |
542 | OPENSSL_free(mm); | |
543 | } | |
544 | err: | |
545 | MemCheck_on(); /* release MALLOC2 lock if num_disabled drops | |
546 | * to 0 */ | |
547 | } | |
548 | break; | |
549 | } | |
550 | return; | |
551 | } | |
9ac42ed8 RL |
552 | |
553 | void CRYPTO_dbg_free(void *addr, int before_p) | |
0f113f3e MC |
554 | { |
555 | MEM m, *mp; | |
556 | ||
557 | switch (before_p) { | |
558 | case 0: | |
559 | if (addr == NULL) | |
560 | break; | |
561 | ||
562 | if (is_MemCheck_on() && (mh != NULL)) { | |
563 | MemCheck_off(); /* make sure we hold MALLOC2 lock */ | |
564 | ||
565 | m.addr = addr; | |
566 | mp = lh_MEM_delete(mh, &m); | |
567 | if (mp != NULL) { | |
3d6001f7 | 568 | #ifdef LEVITTE_DEBUG_MEM |
0f113f3e MC |
569 | fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5ld] - 0x%p (%d)\n", |
570 | mp->order, mp->addr, mp->num); | |
9ac42ed8 | 571 | #endif |
25aaa98a | 572 | app_info_free(mp->app_info); |
0f113f3e MC |
573 | OPENSSL_free(mp); |
574 | } | |
575 | ||
576 | MemCheck_on(); /* release MALLOC2 lock if num_disabled drops | |
577 | * to 0 */ | |
578 | } | |
579 | break; | |
580 | case 1: | |
581 | break; | |
582 | } | |
583 | } | |
9ac42ed8 | 584 | |
6343829a | 585 | void CRYPTO_dbg_realloc(void *addr1, void *addr2, int num, |
0f113f3e MC |
586 | const char *file, int line, int before_p) |
587 | { | |
588 | MEM m, *mp; | |
9ac42ed8 | 589 | |
3d6001f7 | 590 | #ifdef LEVITTE_DEBUG_MEM |
0f113f3e MC |
591 | fprintf(stderr, |
592 | "LEVITTE_DEBUG_MEM: --> CRYPTO_dbg_malloc(addr1 = %p, addr2 = %p, num = %d, file = \"%s\", line = %d, before_p = %d)\n", | |
593 | addr1, addr2, num, file, line, before_p); | |
9ac42ed8 RL |
594 | #endif |
595 | ||
0f113f3e MC |
596 | switch (before_p) { |
597 | case 0: | |
598 | break; | |
599 | case 1: | |
600 | if (addr2 == NULL) | |
601 | break; | |
602 | ||
603 | if (addr1 == NULL) { | |
604 | CRYPTO_dbg_malloc(addr2, num, file, line, 128 | before_p); | |
605 | break; | |
606 | } | |
607 | ||
608 | if (is_MemCheck_on()) { | |
609 | MemCheck_off(); /* make sure we hold MALLOC2 lock */ | |
610 | ||
611 | m.addr = addr1; | |
612 | mp = lh_MEM_delete(mh, &m); | |
613 | if (mp != NULL) { | |
3d6001f7 | 614 | #ifdef LEVITTE_DEBUG_MEM |
0f113f3e MC |
615 | fprintf(stderr, |
616 | "LEVITTE_DEBUG_MEM: [%5ld] * 0x%p (%d) -> 0x%p (%d)\n", | |
617 | mp->order, mp->addr, mp->num, addr2, num); | |
9ac42ed8 | 618 | #endif |
0f113f3e MC |
619 | mp->addr = addr2; |
620 | mp->num = num; | |
012c5408 RL |
621 | #if defined(CRYPTO_MDEBUG_BACKTRACE) && defined(__GNUC__) |
622 | mp->array_siz = backtrace(mp->array, OSSL_NELEM(mp->array)); | |
623 | #endif | |
0f113f3e MC |
624 | (void)lh_MEM_insert(mh, mp); |
625 | } | |
626 | ||
627 | MemCheck_on(); /* release MALLOC2 lock if num_disabled drops | |
628 | * to 0 */ | |
629 | } | |
630 | break; | |
631 | } | |
632 | return; | |
633 | } | |
634 | ||
635 | typedef struct mem_leak_st { | |
636 | BIO *bio; | |
637 | int chunks; | |
31b222da | 638 | int seen; |
0f113f3e MC |
639 | long bytes; |
640 | } MEM_LEAK; | |
9ac42ed8 | 641 | |
3c1d6bbc | 642 | static void print_leak_doall_arg(const MEM *m, MEM_LEAK *l) |
0f113f3e MC |
643 | { |
644 | char buf[1024]; | |
645 | char *bufp = buf; | |
646 | APP_INFO *amip; | |
647 | int ami_cnt; | |
648 | struct tm *lcl = NULL; | |
649 | CRYPTO_THREADID ti; | |
9ac42ed8 | 650 | |
d420ac2c RL |
651 | #define BUF_REMAIN (sizeof buf - (size_t)(bufp - buf)) |
652 | ||
31b222da RS |
653 | /* Is one "leak" the BIO we were given? */ |
654 | if (m->addr == (char *)l->bio) { | |
655 | l->seen = 1; | |
0f113f3e | 656 | return; |
31b222da | 657 | } |
0f113f3e MC |
658 | |
659 | if (options & V_CRYPTO_MDEBUG_TIME) { | |
660 | lcl = localtime(&m->time); | |
661 | ||
662 | BIO_snprintf(bufp, BUF_REMAIN, "[%02d:%02d:%02d] ", | |
663 | lcl->tm_hour, lcl->tm_min, lcl->tm_sec); | |
664 | bufp += strlen(bufp); | |
665 | } | |
666 | ||
667 | BIO_snprintf(bufp, BUF_REMAIN, "%5lu file=%s, line=%d, ", | |
668 | m->order, m->file, m->line); | |
669 | bufp += strlen(bufp); | |
670 | ||
671 | if (options & V_CRYPTO_MDEBUG_THREAD) { | |
672 | BIO_snprintf(bufp, BUF_REMAIN, "thread=%lu, ", | |
673 | CRYPTO_THREADID_hash(&m->threadid)); | |
674 | bufp += strlen(bufp); | |
675 | } | |
676 | ||
5f0580cc AP |
677 | BIO_snprintf(bufp, BUF_REMAIN, "number=%d, address=%p\n", |
678 | m->num, m->addr); | |
0f113f3e MC |
679 | bufp += strlen(bufp); |
680 | ||
681 | BIO_puts(l->bio, buf); | |
682 | ||
683 | l->chunks++; | |
684 | l->bytes += m->num; | |
685 | ||
686 | amip = m->app_info; | |
687 | ami_cnt = 0; | |
012c5408 RL |
688 | if (amip) { |
689 | CRYPTO_THREADID_cpy(&ti, &amip->threadid); | |
690 | ||
691 | do { | |
692 | int buf_len; | |
693 | int info_len; | |
694 | ||
695 | ami_cnt++; | |
696 | memset(buf, '>', ami_cnt); | |
697 | BIO_snprintf(buf + ami_cnt, sizeof buf - ami_cnt, | |
698 | " thread=%lu, file=%s, line=%d, info=\"", | |
699 | CRYPTO_THREADID_hash(&amip->threadid), amip->file, | |
700 | amip->line); | |
0f113f3e | 701 | buf_len = strlen(buf); |
012c5408 RL |
702 | info_len = strlen(amip->info); |
703 | if (128 - buf_len - 3 < info_len) { | |
704 | memcpy(buf + buf_len, amip->info, 128 - buf_len - 3); | |
705 | buf_len = 128 - 3; | |
706 | } else { | |
7644a9ae | 707 | OPENSSL_strlcpy(buf + buf_len, amip->info, sizeof buf - buf_len); |
012c5408 RL |
708 | buf_len = strlen(buf); |
709 | } | |
710 | BIO_snprintf(buf + buf_len, sizeof buf - buf_len, "\"\n"); | |
0f113f3e | 711 | |
012c5408 | 712 | BIO_puts(l->bio, buf); |
0f113f3e | 713 | |
012c5408 RL |
714 | amip = amip->next; |
715 | } | |
716 | while (amip && !CRYPTO_THREADID_cmp(&amip->threadid, &ti)); | |
0f113f3e | 717 | } |
f7ccba3e | 718 | |
3d6001f7 | 719 | #ifdef LEVITTE_DEBUG_MEM |
0f113f3e MC |
720 | if (amip) { |
721 | fprintf(stderr, "Thread switch detected in backtrace!!!!\n"); | |
722 | abort(); | |
723 | } | |
9ac42ed8 | 724 | #endif |
012c5408 RL |
725 | #if defined(CRYPTO_MDEBUG_BACKTRACE) && defined(__GNUC__) |
726 | { | |
727 | size_t i; | |
728 | char **strings = backtrace_symbols(m->array, m->array_siz); | |
729 | for (i = 0; i < m->array_siz; i++) | |
730 | fprintf(stderr, "##> %s\n", strings[i]); | |
731 | ||
732 | free(strings); | |
733 | } | |
734 | #endif | |
0f113f3e | 735 | } |
9ac42ed8 | 736 | |
3c1d6bbc | 737 | static IMPLEMENT_LHASH_DOALL_ARG_FN(print_leak, const MEM, MEM_LEAK) |
98d517c5 | 738 | |
9ac42ed8 | 739 | void CRYPTO_mem_leaks(BIO *b) |
0f113f3e MC |
740 | { |
741 | MEM_LEAK ml; | |
742 | ||
743 | if (mh == NULL && amih == NULL) | |
744 | return; | |
745 | ||
746 | MemCheck_off(); /* obtain MALLOC2 lock */ | |
747 | ||
748 | ml.bio = b; | |
749 | ml.bytes = 0; | |
750 | ml.chunks = 0; | |
31b222da | 751 | ml.seen = 0; |
0f113f3e MC |
752 | if (mh != NULL) |
753 | lh_MEM_doall_arg(mh, LHASH_DOALL_ARG_FN(print_leak), MEM_LEAK, &ml); | |
31b222da RS |
754 | /* Don't count the BIO that was passed in as a "leak" */ |
755 | if (ml.seen && ml.chunks >= 1 && ml.bytes >= (int)sizeof (*b)) { | |
756 | ml.chunks--; | |
757 | ml.bytes -= (int)sizeof (*b); | |
758 | } | |
0f113f3e MC |
759 | if (ml.chunks != 0) { |
760 | BIO_printf(b, "%ld bytes leaked in %d chunks\n", ml.bytes, ml.chunks); | |
8e6925b0 | 761 | #ifdef CRYPTO_MDEBUG_ABORT |
0f113f3e | 762 | abort(); |
8e6925b0 | 763 | #endif |
0f113f3e MC |
764 | } else { |
765 | /* | |
766 | * Make sure that, if we found no leaks, memory-leak debugging itself | |
767 | * does not introduce memory leaks (which might irritate external | |
768 | * debugging tools). (When someone enables leak checking, but does not | |
769 | * call this function, we declare it to be their fault.) XXX This | |
770 | * should be in CRYPTO_mem_leaks_cb, and CRYPTO_mem_leaks should be | |
771 | * implemented by using CRYPTO_mem_leaks_cb. (Also there should be a | |
772 | * variant of lh_doall_arg that takes a function pointer instead of a | |
773 | * void *; this would obviate the ugly and illegal void_fn_to_char | |
774 | * kludge in CRYPTO_mem_leaks_cb. Otherwise the code police will come | |
775 | * and get us.) | |
776 | */ | |
777 | int old_mh_mode; | |
778 | ||
779 | CRYPTO_w_lock(CRYPTO_LOCK_MALLOC); | |
780 | ||
781 | /* | |
782 | * avoid deadlock when lh_free() uses CRYPTO_dbg_free(), which uses | |
783 | * CRYPTO_is_mem_check_on | |
784 | */ | |
785 | old_mh_mode = mh_mode; | |
786 | mh_mode = CRYPTO_MEM_CHECK_OFF; | |
787 | ||
25aaa98a RS |
788 | lh_MEM_free(mh); |
789 | mh = NULL; | |
0f113f3e MC |
790 | if (amih != NULL) { |
791 | if (lh_APP_INFO_num_items(amih) == 0) { | |
792 | lh_APP_INFO_free(amih); | |
793 | amih = NULL; | |
794 | } | |
795 | } | |
796 | ||
797 | mh_mode = old_mh_mode; | |
798 | CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC); | |
799 | } | |
800 | MemCheck_on(); /* release MALLOC2 lock */ | |
801 | } | |
9ac42ed8 | 802 | |
4b618848 | 803 | #ifndef OPENSSL_NO_STDIO |
9ac42ed8 | 804 | void CRYPTO_mem_leaks_fp(FILE *fp) |
0f113f3e MC |
805 | { |
806 | BIO *b; | |
807 | ||
808 | if (mh == NULL) | |
809 | return; | |
810 | /* | |
811 | * Need to turn off memory checking when allocated BIOs ... especially as | |
812 | * we're creating them at a time when we're trying to check we've not | |
813 | * left anything un-free()'d!! | |
814 | */ | |
815 | MemCheck_off(); | |
816 | b = BIO_new(BIO_s_file()); | |
817 | MemCheck_on(); | |
90945fa3 | 818 | if (b == NULL) |
0f113f3e MC |
819 | return; |
820 | BIO_set_fp(b, fp, BIO_NOCLOSE); | |
821 | CRYPTO_mem_leaks(b); | |
822 | BIO_free(b); | |
823 | } | |
9ac42ed8 RL |
824 | #endif |
825 | ||
0f113f3e MC |
826 | /* |
827 | * FIXME: We really don't allow much to the callback. For example, it has no | |
828 | * chance of reaching the info stack for the item it processes. Should it | |
829 | * really be this way? -- Richard Levitte | |
830 | */ | |
831 | /* | |
832 | * NB: The prototypes have been typedef'd to CRYPTO_MEM_LEAK_CB inside | |
833 | * crypto.h If this code is restructured, remove the callback type if it is | |
834 | * no longer needed. -- Geoff Thorpe | |
835 | */ | |
65962686 | 836 | |
0f113f3e MC |
837 | /* |
838 | * Can't pass CRYPTO_MEM_LEAK_CB directly to lh_MEM_doall_arg because it is a | |
839 | * function pointer and conversion to void * is prohibited. Instead pass its | |
840 | * address | |
d4cdbab9 DSH |
841 | */ |
842 | ||
843 | typedef CRYPTO_MEM_LEAK_CB *PCRYPTO_MEM_LEAK_CB; | |
844 | ||
845 | static void cb_leak_doall_arg(const MEM *m, PCRYPTO_MEM_LEAK_CB *cb) | |
0f113f3e MC |
846 | { |
847 | (*cb) (m->order, m->file, m->line, m->num, m->addr); | |
848 | } | |
65962686 | 849 | |
d4cdbab9 | 850 | static IMPLEMENT_LHASH_DOALL_ARG_FN(cb_leak, const MEM, PCRYPTO_MEM_LEAK_CB) |
98d517c5 | 851 | |
32654e79 | 852 | void CRYPTO_mem_leaks_cb(CRYPTO_MEM_LEAK_CB *cb) |
0f113f3e MC |
853 | { |
854 | if (mh == NULL) | |
855 | return; | |
856 | CRYPTO_w_lock(CRYPTO_LOCK_MALLOC2); | |
857 | lh_MEM_doall_arg(mh, LHASH_DOALL_ARG_FN(cb_leak), PCRYPTO_MEM_LEAK_CB, | |
858 | &cb); | |
859 | CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2); | |
860 | } |