]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/mem_dbg.c
Copyright consolidation 05/10
[thirdparty/openssl.git] / crypto / mem_dbg.c
1 /*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <time.h>
13 #include "internal/cryptlib.h"
14 #include <openssl/crypto.h>
15 #include <openssl/buffer.h>
16 #include "internal/bio.h"
17 #include <openssl/lhash.h>
18
19 #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
20 # include <execinfo.h>
21 #endif
22
23 /*
24 * The state changes to CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE when
25 * the application asks for it (usually after library initialisation for
26 * which no book-keeping is desired). State CRYPTO_MEM_CHECK_ON exists only
27 * temporarily when the library thinks that certain allocations should not be
28 * checked (e.g. the data structures used for memory checking). It is not
29 * suitable as an initial state: the library will unexpectedly enable memory
30 * checking when it executes one of those sections that want to disable
31 * checking temporarily. State CRYPTO_MEM_CHECK_ENABLE without ..._ON makes
32 * no sense whatsoever.
33 */
34 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
35 static int mh_mode = CRYPTO_MEM_CHECK_OFF;
36 #endif
37
38 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
39 static unsigned long order = 0; /* number of memory requests */
40
41 /*-
42 * For application-defined information (static C-string `info')
43 * to be displayed in memory leak list.
44 * Each thread has its own stack. For applications, there is
45 * OPENSSL_mem_debug_push("...") to push an entry,
46 * OPENSSL_mem_debug_pop() to pop an entry,
47 */
48 struct app_mem_info_st {
49 CRYPTO_THREAD_ID threadid;
50 const char *file;
51 int line;
52 const char *info;
53 struct app_mem_info_st *next; /* tail of thread's stack */
54 int references;
55 };
56
57 static CRYPTO_ONCE memdbg_init = CRYPTO_ONCE_STATIC_INIT;
58 static CRYPTO_RWLOCK *malloc_lock = NULL;
59 static CRYPTO_RWLOCK *long_malloc_lock = NULL;
60 static CRYPTO_THREAD_LOCAL appinfokey;
61
62 /* memory-block description */
63 struct mem_st {
64 void *addr;
65 int num;
66 const char *file;
67 int line;
68 CRYPTO_THREAD_ID threadid;
69 unsigned long order;
70 time_t time;
71 APP_INFO *app_info;
72 #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
73 void *array[30];
74 size_t array_siz;
75 #endif
76 };
77
78 static LHASH_OF(MEM) *mh = NULL; /* hash-table of memory requests (address as
79 * key); access requires MALLOC2 lock */
80
81 /* num_disable > 0 iff mh_mode == CRYPTO_MEM_CHECK_ON (w/o ..._ENABLE) */
82 static unsigned int num_disable = 0;
83
84 /*
85 * Valid iff num_disable > 0. long_malloc_lock is locked exactly in this
86 * case (by the thread named in disabling_thread).
87 */
88 static CRYPTO_THREAD_ID disabling_threadid;
89
90 static void do_memdbg_init(void)
91 {
92 malloc_lock = CRYPTO_THREAD_lock_new();
93 long_malloc_lock = CRYPTO_THREAD_lock_new();
94 CRYPTO_THREAD_init_local(&appinfokey, NULL);
95 }
96
97 static void app_info_free(APP_INFO *inf)
98 {
99 if (!inf)
100 return;
101 if (--(inf->references) <= 0) {
102 app_info_free(inf->next);
103 OPENSSL_free(inf);
104 }
105 }
106 #endif
107
108 int CRYPTO_mem_ctrl(int mode)
109 {
110 #ifdef OPENSSL_NO_CRYPTO_MDEBUG
111 return mode - mode;
112 #else
113 int ret = mh_mode;
114
115 CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init);
116
117 CRYPTO_THREAD_write_lock(malloc_lock);
118 switch (mode) {
119 default:
120 break;
121
122 case CRYPTO_MEM_CHECK_ON:
123 mh_mode = CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE;
124 num_disable = 0;
125 break;
126
127 case CRYPTO_MEM_CHECK_OFF:
128 mh_mode = 0;
129 num_disable = 0;
130 break;
131
132 /* switch off temporarily (for library-internal use): */
133 case CRYPTO_MEM_CHECK_DISABLE:
134 if (mh_mode & CRYPTO_MEM_CHECK_ON) {
135 CRYPTO_THREAD_ID cur = CRYPTO_THREAD_get_current_id();
136 /* see if we don't have long_malloc_lock already */
137 if (!num_disable
138 || !CRYPTO_THREAD_compare_id(disabling_threadid, cur)) {
139 /*
140 * Long-time lock long_malloc_lock must not be claimed
141 * while we're holding malloc_lock, or we'll deadlock
142 * if somebody else holds long_malloc_lock (and cannot
143 * release it because we block entry to this function). Give
144 * them a chance, first, and then claim the locks in
145 * appropriate order (long-time lock first).
146 */
147 CRYPTO_THREAD_unlock(malloc_lock);
148 /*
149 * Note that after we have waited for long_malloc_lock and
150 * malloc_lock, we'll still be in the right "case" and
151 * "if" branch because MemCheck_start and MemCheck_stop may
152 * never be used while there are multiple OpenSSL threads.
153 */
154 CRYPTO_THREAD_write_lock(long_malloc_lock);
155 CRYPTO_THREAD_write_lock(malloc_lock);
156 mh_mode &= ~CRYPTO_MEM_CHECK_ENABLE;
157 disabling_threadid = cur;
158 }
159 num_disable++;
160 }
161 break;
162
163 case CRYPTO_MEM_CHECK_ENABLE:
164 if (mh_mode & CRYPTO_MEM_CHECK_ON) {
165 if (num_disable) { /* always true, or something is going wrong */
166 num_disable--;
167 if (num_disable == 0) {
168 mh_mode |= CRYPTO_MEM_CHECK_ENABLE;
169 CRYPTO_THREAD_unlock(long_malloc_lock);
170 }
171 }
172 }
173 break;
174 }
175 CRYPTO_THREAD_unlock(malloc_lock);
176 return (ret);
177 #endif
178 }
179
180 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
181
182 static int mem_check_on(void)
183 {
184 int ret = 0;
185 CRYPTO_THREAD_ID cur;
186
187 if (mh_mode & CRYPTO_MEM_CHECK_ON) {
188 CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init);
189
190 cur = CRYPTO_THREAD_get_current_id();
191 CRYPTO_THREAD_read_lock(malloc_lock);
192
193 ret = (mh_mode & CRYPTO_MEM_CHECK_ENABLE)
194 || !CRYPTO_THREAD_compare_id(disabling_threadid, cur);
195
196 CRYPTO_THREAD_unlock(malloc_lock);
197 }
198 return (ret);
199 }
200
201 static int mem_cmp(const MEM *a, const MEM *b)
202 {
203 #ifdef _WIN64
204 const char *ap = (const char *)a->addr, *bp = (const char *)b->addr;
205 if (ap == bp)
206 return 0;
207 else if (ap > bp)
208 return 1;
209 else
210 return -1;
211 #else
212 return (const char *)a->addr - (const char *)b->addr;
213 #endif
214 }
215
216 static unsigned long mem_hash(const MEM *a)
217 {
218 size_t ret;
219
220 ret = (size_t)a->addr;
221
222 ret = ret * 17851 + (ret >> 14) * 7 + (ret >> 4) * 251;
223 return (ret);
224 }
225
226 /* returns 1 if there was an info to pop, 0 if the stack was empty. */
227 static int pop_info(void)
228 {
229 APP_INFO *current = NULL;
230
231 CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init);
232 current = (APP_INFO *)CRYPTO_THREAD_get_local(&appinfokey);
233 if (current != NULL) {
234 APP_INFO *next = current->next;
235
236 if (next != NULL) {
237 next->references++;
238 CRYPTO_THREAD_set_local(&appinfokey, next);
239 } else {
240 CRYPTO_THREAD_set_local(&appinfokey, NULL);
241 }
242 if (--(current->references) <= 0) {
243 current->next = NULL;
244 if (next != NULL)
245 next->references--;
246 OPENSSL_free(current);
247 }
248 return 1;
249 }
250 return 0;
251 }
252
253 int CRYPTO_mem_debug_push(const char *info, const char *file, int line)
254 {
255 APP_INFO *ami, *amim;
256 int ret = 0;
257
258 if (mem_check_on()) {
259 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
260
261 CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init);
262
263 if ((ami = OPENSSL_malloc(sizeof(*ami))) == NULL)
264 goto err;
265
266 ami->threadid = CRYPTO_THREAD_get_current_id();
267 ami->file = file;
268 ami->line = line;
269 ami->info = info;
270 ami->references = 1;
271 ami->next = NULL;
272
273 amim = (APP_INFO *)CRYPTO_THREAD_get_local(&appinfokey);
274 CRYPTO_THREAD_set_local(&appinfokey, ami);
275
276 if (amim != NULL)
277 ami->next = amim;
278 ret = 1;
279 err:
280 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
281 }
282
283 return (ret);
284 }
285
286 int CRYPTO_mem_debug_pop(void)
287 {
288 int ret = 0;
289
290 if (mem_check_on()) {
291 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
292 ret = pop_info();
293 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
294 }
295 return (ret);
296 }
297
298 static unsigned long break_order_num = 0;
299
300 void CRYPTO_mem_debug_malloc(void *addr, size_t num, int before_p,
301 const char *file, int line)
302 {
303 MEM *m, *mm;
304 APP_INFO *amim;
305
306 switch (before_p & 127) {
307 case 0:
308 break;
309 case 1:
310 if (addr == NULL)
311 break;
312
313 if (mem_check_on()) {
314 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
315
316 CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init);
317
318 if ((m = OPENSSL_malloc(sizeof(*m))) == NULL) {
319 OPENSSL_free(addr);
320 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
321 return;
322 }
323 if (mh == NULL) {
324 if ((mh = lh_MEM_new(mem_hash, mem_cmp)) == NULL) {
325 OPENSSL_free(addr);
326 OPENSSL_free(m);
327 addr = NULL;
328 goto err;
329 }
330 }
331
332 m->addr = addr;
333 m->file = file;
334 m->line = line;
335 m->num = num;
336 m->threadid = CRYPTO_THREAD_get_current_id();
337
338 if (order == break_order_num) {
339 /* BREAK HERE */
340 m->order = order;
341 }
342 m->order = order++;
343 # ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
344 m->array_siz = backtrace(m->array, OSSL_NELEM(m->array));
345 # endif
346 m->time = time(NULL);
347
348 amim = (APP_INFO *)CRYPTO_THREAD_get_local(&appinfokey);
349 m->app_info = amim;
350 if (amim != NULL)
351 amim->references++;
352
353 if ((mm = lh_MEM_insert(mh, m)) != NULL) {
354 /* Not good, but don't sweat it */
355 if (mm->app_info != NULL) {
356 mm->app_info->references--;
357 }
358 OPENSSL_free(mm);
359 }
360 err:
361 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
362 }
363 break;
364 }
365 return;
366 }
367
368 void CRYPTO_mem_debug_free(void *addr, int before_p,
369 const char *file, int line)
370 {
371 MEM m, *mp;
372
373 switch (before_p) {
374 case 0:
375 if (addr == NULL)
376 break;
377
378 if (mem_check_on() && (mh != NULL)) {
379 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
380
381 m.addr = addr;
382 mp = lh_MEM_delete(mh, &m);
383 if (mp != NULL) {
384 app_info_free(mp->app_info);
385 OPENSSL_free(mp);
386 }
387
388 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
389 }
390 break;
391 case 1:
392 break;
393 }
394 }
395
396 void CRYPTO_mem_debug_realloc(void *addr1, void *addr2, size_t num,
397 int before_p, const char *file, int line)
398 {
399 MEM m, *mp;
400
401 switch (before_p) {
402 case 0:
403 break;
404 case 1:
405 if (addr2 == NULL)
406 break;
407
408 if (addr1 == NULL) {
409 CRYPTO_mem_debug_malloc(addr2, num, 128 | before_p, file, line);
410 break;
411 }
412
413 if (mem_check_on()) {
414 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
415
416 m.addr = addr1;
417 mp = lh_MEM_delete(mh, &m);
418 if (mp != NULL) {
419 mp->addr = addr2;
420 mp->num = num;
421 #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
422 mp->array_siz = backtrace(mp->array, OSSL_NELEM(mp->array));
423 #endif
424 (void)lh_MEM_insert(mh, mp);
425 }
426
427 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
428 }
429 break;
430 }
431 return;
432 }
433
434 typedef struct mem_leak_st {
435 BIO *bio;
436 int chunks;
437 long bytes;
438 } MEM_LEAK;
439
440 static void print_leak(const MEM *m, MEM_LEAK *l)
441 {
442 char buf[1024];
443 char *bufp = buf;
444 APP_INFO *amip;
445 int ami_cnt;
446 struct tm *lcl = NULL;
447 /*
448 * Convert between CRYPTO_THREAD_ID (which could be anything at all) and
449 * a long. This may not be meaningful depending on what CRYPTO_THREAD_ID is
450 * but hopefully should give something sensible on most platforms
451 */
452 union {
453 CRYPTO_THREAD_ID tid;
454 unsigned long ltid;
455 } tid;
456 CRYPTO_THREAD_ID ti;
457
458 #define BUF_REMAIN (sizeof buf - (size_t)(bufp - buf))
459
460 lcl = localtime(&m->time);
461 BIO_snprintf(bufp, BUF_REMAIN, "[%02d:%02d:%02d] ",
462 lcl->tm_hour, lcl->tm_min, lcl->tm_sec);
463 bufp += strlen(bufp);
464
465 BIO_snprintf(bufp, BUF_REMAIN, "%5lu file=%s, line=%d, ",
466 m->order, m->file, m->line);
467 bufp += strlen(bufp);
468
469 tid.ltid = 0;
470 tid.tid = m->threadid;
471 BIO_snprintf(bufp, BUF_REMAIN, "thread=%lu, ", tid.ltid);
472 bufp += strlen(bufp);
473
474 BIO_snprintf(bufp, BUF_REMAIN, "number=%d, address=%p\n",
475 m->num, m->addr);
476 bufp += strlen(bufp);
477
478 BIO_puts(l->bio, buf);
479
480 l->chunks++;
481 l->bytes += m->num;
482
483 amip = m->app_info;
484 ami_cnt = 0;
485
486 if (amip) {
487 ti = amip->threadid;
488
489 do {
490 int buf_len;
491 int info_len;
492
493 ami_cnt++;
494 memset(buf, '>', ami_cnt);
495 tid.ltid = 0;
496 tid.tid = amip->threadid;
497 BIO_snprintf(buf + ami_cnt, sizeof buf - ami_cnt,
498 " thread=%lu, file=%s, line=%d, info=\"",
499 tid.ltid, amip->file,
500 amip->line);
501 buf_len = strlen(buf);
502 info_len = strlen(amip->info);
503 if (128 - buf_len - 3 < info_len) {
504 memcpy(buf + buf_len, amip->info, 128 - buf_len - 3);
505 buf_len = 128 - 3;
506 } else {
507 OPENSSL_strlcpy(buf + buf_len, amip->info, sizeof buf - buf_len);
508 buf_len = strlen(buf);
509 }
510 BIO_snprintf(buf + buf_len, sizeof buf - buf_len, "\"\n");
511
512 BIO_puts(l->bio, buf);
513
514 amip = amip->next;
515 }
516 while (amip && CRYPTO_THREAD_compare_id(amip->threadid, ti));
517 }
518
519 #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
520 {
521 size_t i;
522 char **strings = backtrace_symbols(m->array, m->array_siz);
523
524 for (i = 0; i < m->array_siz; i++)
525 fprintf(stderr, "##> %s\n", strings[i]);
526 free(strings);
527 }
528 #endif
529 }
530
531 IMPLEMENT_LHASH_DOALL_ARG_CONST(MEM, MEM_LEAK);
532
533 int CRYPTO_mem_leaks(BIO *b)
534 {
535 MEM_LEAK ml;
536
537 /*
538 * OPENSSL_cleanup() will free the ex_data locks so we can't have any
539 * ex_data hanging around
540 */
541 bio_free_ex_data(b);
542
543 /* Ensure all resources are released */
544 OPENSSL_cleanup();
545
546 CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init);
547
548 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
549
550 ml.bio = b;
551 ml.bytes = 0;
552 ml.chunks = 0;
553 if (mh != NULL)
554 lh_MEM_doall_MEM_LEAK(mh, print_leak, &ml);
555
556 if (ml.chunks != 0) {
557 BIO_printf(b, "%ld bytes leaked in %d chunks\n", ml.bytes, ml.chunks);
558 } else {
559 /*
560 * Make sure that, if we found no leaks, memory-leak debugging itself
561 * does not introduce memory leaks (which might irritate external
562 * debugging tools). (When someone enables leak checking, but does not
563 * call this function, we declare it to be their fault.)
564 */
565 int old_mh_mode;
566
567 CRYPTO_THREAD_write_lock(malloc_lock);
568
569 /*
570 * avoid deadlock when lh_free() uses CRYPTO_mem_debug_free(), which uses
571 * mem_check_on
572 */
573 old_mh_mode = mh_mode;
574 mh_mode = CRYPTO_MEM_CHECK_OFF;
575
576 lh_MEM_free(mh);
577 mh = NULL;
578
579 mh_mode = old_mh_mode;
580 CRYPTO_THREAD_unlock(malloc_lock);
581 }
582 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_OFF);
583
584 /* Clean up locks etc */
585 CRYPTO_THREAD_cleanup_local(&appinfokey);
586 CRYPTO_THREAD_lock_free(malloc_lock);
587 CRYPTO_THREAD_lock_free(long_malloc_lock);
588 malloc_lock = NULL;
589 long_malloc_lock = NULL;
590
591 return ml.chunks == 0 ? 1 : 0;
592 }
593
594 # ifndef OPENSSL_NO_STDIO
595 int CRYPTO_mem_leaks_fp(FILE *fp)
596 {
597 BIO *b;
598 int ret;
599
600 /*
601 * Need to turn off memory checking when allocated BIOs ... especially as
602 * we're creating them at a time when we're trying to check we've not
603 * left anything un-free()'d!!
604 */
605 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
606 b = BIO_new(BIO_s_file());
607 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
608 if (b == NULL)
609 return -1;
610 BIO_set_fp(b, fp, BIO_NOCLOSE);
611 ret = CRYPTO_mem_leaks(b);
612 BIO_free(b);
613 return ret;
614 }
615 # endif
616
617 #endif