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