blob: 0b487082326283bdde007c783ffe7d1069e2b6ad [file] [log] [blame]
Rich Salzaa6bb132016-05-17 15:38:09 -04001/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
Richard Levitte9ac42ed1999-12-17 12:56:24 +00003 *
Rich Salzaa6bb132016-05-17 15:38:09 -04004 * 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öller48fc5822006-06-23 15:21:36 +00008 */
Richard Levitte9ac42ed1999-12-17 12:56:24 +00009
10#include <stdio.h>
11#include <stdlib.h>
Matt Caswell0f113f32015-01-22 03:40:55 +000012#include <time.h>
Richard Levitteb39fc562015-05-14 16:56:48 +020013#include "internal/cryptlib.h"
Richard Levitte9ac42ed1999-12-17 12:56:24 +000014#include <openssl/crypto.h>
15#include <openssl/buffer.h>
Matt Caswell1ee7b8b2016-04-14 21:28:54 +010016#include "internal/bio.h"
Richard Levitte9ac42ed1999-12-17 12:56:24 +000017#include <openssl/lhash.h>
Richard Levitteef8ca6b2016-02-14 12:16:52 +010018
19#ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
Richard Levitte012c5402015-12-02 13:19:45 +010020# include <execinfo.h>
21#endif
Richard Levitte9ac42ed1999-12-17 12:56:24 +000022
Matt Caswell0f113f32015-01-22 03:40:55 +000023/*
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.
Richard Levitte9ac42ed1999-12-17 12:56:24 +000033 */
Viktor Dukhovnic2e27312016-01-10 14:42:10 -050034#ifndef OPENSSL_NO_CRYPTO_MDEBUG
Rich Salzbbd86bf2016-01-07 15:06:38 -050035static int mh_mode = CRYPTO_MEM_CHECK_OFF;
Viktor Dukhovnic2e27312016-01-10 14:42:10 -050036#endif
Richard Levitte9ac42ed1999-12-17 12:56:24 +000037
Rich Salz6ac11bd2016-01-07 21:40:52 -050038#ifndef OPENSSL_NO_CRYPTO_MDEBUG
Bodo Möller0cd08cc1999-12-18 05:22:50 +000039static unsigned long order = 0; /* number of memory requests */
Ben Laurie3c1d6bb2008-05-26 11:24:29 +000040
Matt Caswellc80fd6b2015-01-16 09:21:50 +000041/*-
42 * For application-defined information (static C-string `info')
Bodo Möller0cd08cc1999-12-18 05:22:50 +000043 * to be displayed in memory leak list.
44 * Each thread has its own stack. For applications, there is
Rich Salz4fae3862015-12-16 23:02:47 -050045 * OPENSSL_mem_debug_push("...") to push an entry,
46 * OPENSSL_mem_debug_pop() to pop an entry,
Bodo Möller0cd08cc1999-12-18 05:22:50 +000047 */
Dr. Stephen Hensone6b5c342016-01-11 14:11:13 +000048struct app_mem_info_st {
Matt Caswell9471f772016-03-08 15:44:05 +000049 CRYPTO_THREAD_ID threadid;
Matt Caswell0f113f32015-01-22 03:40:55 +000050 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;
Dr. Stephen Hensone6b5c342016-01-11 14:11:13 +000055};
Richard Levitte9ac42ed1999-12-17 12:56:24 +000056
Matt Caswell9471f772016-03-08 15:44:05 +000057static CRYPTO_ONCE memdbg_init = CRYPTO_ONCE_STATIC_INIT;
58static CRYPTO_RWLOCK *malloc_lock = NULL;
59static CRYPTO_RWLOCK *long_malloc_lock = NULL;
60static CRYPTO_THREAD_LOCAL appinfokey;
Richard Levitte9ac42ed1999-12-17 12:56:24 +000061
Bodo Möller0cd08cc1999-12-18 05:22:50 +000062/* memory-block description */
Dr. Stephen Hensone6b5c342016-01-11 14:11:13 +000063struct mem_st {
Matt Caswell0f113f32015-01-22 03:40:55 +000064 void *addr;
65 int num;
66 const char *file;
67 int line;
Matt Caswell9471f772016-03-08 15:44:05 +000068 CRYPTO_THREAD_ID threadid;
Matt Caswell0f113f32015-01-22 03:40:55 +000069 unsigned long order;
70 time_t time;
71 APP_INFO *app_info;
Richard Levitteef8ca6b2016-02-14 12:16:52 +010072#ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
Richard Levitte012c5402015-12-02 13:19:45 +010073 void *array[30];
74 size_t array_siz;
75#endif
Dr. Stephen Hensone6b5c342016-01-11 14:11:13 +000076};
77
78static LHASH_OF(MEM) *mh = NULL; /* hash-table of memory requests (address as
79 * key); access requires MALLOC2 lock */
Richard Levitte9ac42ed1999-12-17 12:56:24 +000080
Rich Salzbbd86bf2016-01-07 15:06:38 -050081/* num_disable > 0 iff mh_mode == CRYPTO_MEM_CHECK_ON (w/o ..._ENABLE) */
82static unsigned int num_disable = 0;
Richard Levitte9ac42ed1999-12-17 12:56:24 +000083
Matt Caswell0f113f32015-01-22 03:40:55 +000084/*
Matt Caswell9471f772016-03-08 15:44:05 +000085 * Valid iff num_disable > 0. long_malloc_lock is locked exactly in this
Geoff Thorpe4c329692008-08-06 15:54:15 +000086 * case (by the thread named in disabling_thread).
Geoff Thorpe5f834ab2008-07-03 19:59:25 +000087 */
Matt Caswell9471f772016-03-08 15:44:05 +000088static CRYPTO_THREAD_ID disabling_threadid;
89
90static 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}
Richard Levitte9ac42ed1999-12-17 12:56:24 +000096
Bodo Möller9dc61042002-11-18 14:00:42 +000097static void app_info_free(APP_INFO *inf)
Matt Caswell0f113f32015-01-22 03:40:55 +000098{
Rich Salz25aaa982015-05-01 14:37:16 -040099 if (!inf)
100 return;
Matt Caswell0f113f32015-01-22 03:40:55 +0000101 if (--(inf->references) <= 0) {
Rich Salz25aaa982015-05-01 14:37:16 -0400102 app_info_free(inf->next);
Matt Caswell0f113f32015-01-22 03:40:55 +0000103 OPENSSL_free(inf);
104 }
105}
Rich Salzbbd86bf2016-01-07 15:06:38 -0500106#endif
Bodo Möller9dc61042002-11-18 14:00:42 +0000107
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000108int CRYPTO_mem_ctrl(int mode)
Matt Caswell0f113f32015-01-22 03:40:55 +0000109{
Viktor Dukhovnic2e27312016-01-10 14:42:10 -0500110#ifdef OPENSSL_NO_CRYPTO_MDEBUG
Rich Salzbbd86bf2016-01-07 15:06:38 -0500111 return mode - mode;
112#else
Matt Caswell0f113f32015-01-22 03:40:55 +0000113 int ret = mh_mode;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000114
Matt Caswell9471f772016-03-08 15:44:05 +0000115 CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init);
116
117 CRYPTO_THREAD_write_lock(malloc_lock);
Matt Caswell0f113f32015-01-22 03:40:55 +0000118 switch (mode) {
Rich Salzbbd86bf2016-01-07 15:06:38 -0500119 default:
120 break;
121
122 case CRYPTO_MEM_CHECK_ON:
Matt Caswell0f113f32015-01-22 03:40:55 +0000123 mh_mode = CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE;
124 num_disable = 0;
125 break;
Rich Salzbbd86bf2016-01-07 15:06:38 -0500126
127 case CRYPTO_MEM_CHECK_OFF:
Matt Caswell0f113f32015-01-22 03:40:55 +0000128 mh_mode = 0;
Rich Salzbbd86bf2016-01-07 15:06:38 -0500129 num_disable = 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000130 break;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000131
Rich Salzbbd86bf2016-01-07 15:06:38 -0500132 /* switch off temporarily (for library-internal use): */
133 case CRYPTO_MEM_CHECK_DISABLE:
Matt Caswell0f113f32015-01-22 03:40:55 +0000134 if (mh_mode & CRYPTO_MEM_CHECK_ON) {
Matt Caswell9471f772016-03-08 15:44:05 +0000135 CRYPTO_THREAD_ID cur = CRYPTO_THREAD_get_current_id();
136 /* see if we don't have long_malloc_lock already */
Matt Caswell0f113f32015-01-22 03:40:55 +0000137 if (!num_disable
Matt Caswell9471f772016-03-08 15:44:05 +0000138 || !CRYPTO_THREAD_compare_id(disabling_threadid, cur)) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000139 /*
Matt Caswell9471f772016-03-08 15:44:05 +0000140 * 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
Matt Caswell0f113f32015-01-22 03:40:55 +0000143 * 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 */
Matt Caswell9471f772016-03-08 15:44:05 +0000147 CRYPTO_THREAD_unlock(malloc_lock);
Matt Caswell0f113f32015-01-22 03:40:55 +0000148 /*
Matt Caswell9471f772016-03-08 15:44:05 +0000149 * Note that after we have waited for long_malloc_lock and
150 * malloc_lock, we'll still be in the right "case" and
Matt Caswell0f113f32015-01-22 03:40:55 +0000151 * "if" branch because MemCheck_start and MemCheck_stop may
152 * never be used while there are multiple OpenSSL threads.
153 */
Matt Caswell9471f772016-03-08 15:44:05 +0000154 CRYPTO_THREAD_write_lock(long_malloc_lock);
155 CRYPTO_THREAD_write_lock(malloc_lock);
Matt Caswell0f113f32015-01-22 03:40:55 +0000156 mh_mode &= ~CRYPTO_MEM_CHECK_ENABLE;
Matt Caswell9471f772016-03-08 15:44:05 +0000157 disabling_threadid = cur;
Matt Caswell0f113f32015-01-22 03:40:55 +0000158 }
159 num_disable++;
160 }
161 break;
Rich Salzbbd86bf2016-01-07 15:06:38 -0500162
163 case CRYPTO_MEM_CHECK_ENABLE:
Matt Caswell0f113f32015-01-22 03:40:55 +0000164 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;
Matt Caswell9471f772016-03-08 15:44:05 +0000169 CRYPTO_THREAD_unlock(long_malloc_lock);
Matt Caswell0f113f32015-01-22 03:40:55 +0000170 }
171 }
172 }
173 break;
Matt Caswell0f113f32015-01-22 03:40:55 +0000174 }
Matt Caswell9471f772016-03-08 15:44:05 +0000175 CRYPTO_THREAD_unlock(malloc_lock);
Matt Caswell0f113f32015-01-22 03:40:55 +0000176 return (ret);
Rich Salzbbd86bf2016-01-07 15:06:38 -0500177#endif
Matt Caswell0f113f32015-01-22 03:40:55 +0000178}
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000179
Rich Salz6ac11bd2016-01-07 21:40:52 -0500180#ifndef OPENSSL_NO_CRYPTO_MDEBUG
Rich Salzbbd86bf2016-01-07 15:06:38 -0500181
182static int mem_check_on(void)
Matt Caswell0f113f32015-01-22 03:40:55 +0000183{
184 int ret = 0;
Alessandro Ghedinia0605742016-03-08 21:58:17 +0000185 CRYPTO_THREAD_ID cur;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000186
Matt Caswell0f113f32015-01-22 03:40:55 +0000187 if (mh_mode & CRYPTO_MEM_CHECK_ON) {
Matt Caswell9471f772016-03-08 15:44:05 +0000188 CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init);
189
Alessandro Ghedinia0605742016-03-08 21:58:17 +0000190 cur = CRYPTO_THREAD_get_current_id();
Matt Caswell9471f772016-03-08 15:44:05 +0000191 CRYPTO_THREAD_read_lock(malloc_lock);
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000192
Matt Caswell0f113f32015-01-22 03:40:55 +0000193 ret = (mh_mode & CRYPTO_MEM_CHECK_ENABLE)
Matt Caswell9471f772016-03-08 15:44:05 +0000194 || !CRYPTO_THREAD_compare_id(disabling_threadid, cur);
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000195
Matt Caswell9471f772016-03-08 15:44:05 +0000196 CRYPTO_THREAD_unlock(malloc_lock);
Matt Caswell0f113f32015-01-22 03:40:55 +0000197 }
198 return (ret);
199}
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000200
Ben Laurie3c1d6bb2008-05-26 11:24:29 +0000201static int mem_cmp(const MEM *a, const MEM *b)
Matt Caswell0f113f32015-01-22 03:40:55 +0000202{
Andy Polyakov1875e6d2005-07-05 11:44:45 +0000203#ifdef _WIN64
Matt Caswell0f113f32015-01-22 03:40:55 +0000204 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;
Andy Polyakov1875e6d2005-07-05 11:44:45 +0000211#else
Matt Caswell0f113f32015-01-22 03:40:55 +0000212 return (const char *)a->addr - (const char *)b->addr;
Andy Polyakov1875e6d2005-07-05 11:44:45 +0000213#endif
Matt Caswell0f113f32015-01-22 03:40:55 +0000214}
215
Ben Laurie3c1d6bb2008-05-26 11:24:29 +0000216static unsigned long mem_hash(const MEM *a)
Matt Caswell0f113f32015-01-22 03:40:55 +0000217{
Andy Polyakov5f0580c2015-09-30 10:36:21 +0200218 size_t ret;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000219
Andy Polyakov5f0580c2015-09-30 10:36:21 +0200220 ret = (size_t)a->addr;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000221
Matt Caswell0f113f32015-01-22 03:40:55 +0000222 ret = ret * 17851 + (ret >> 14) * 7 + (ret >> 4) * 251;
223 return (ret);
224}
225
Pascal Cuoq96e25c42016-01-10 13:43:37 +0100226/* returns 1 if there was an info to pop, 0 if the stack was empty. */
227static int pop_info(void)
Matt Caswell0f113f32015-01-22 03:40:55 +0000228{
Pascal Cuoq96e25c42016-01-10 13:43:37 +0100229 APP_INFO *current = NULL;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000230
Matt Caswell9471f772016-03-08 15:44:05 +0000231 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;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000235
Matt Caswell9471f772016-03-08 15:44:05 +0000236 if (next != NULL) {
237 next->references++;
238 CRYPTO_THREAD_set_local(&appinfokey, next);
239 } else {
240 CRYPTO_THREAD_set_local(&appinfokey, NULL);
Matt Caswell0f113f32015-01-22 03:40:55 +0000241 }
Matt Caswell9471f772016-03-08 15:44:05 +0000242 if (--(current->references) <= 0) {
243 current->next = NULL;
244 if (next != NULL)
245 next->references--;
246 OPENSSL_free(current);
247 }
248 return 1;
Matt Caswell0f113f32015-01-22 03:40:55 +0000249 }
Pascal Cuoq96e25c42016-01-10 13:43:37 +0100250 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000251}
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000252
Rich Salz4fae3862015-12-16 23:02:47 -0500253int CRYPTO_mem_debug_push(const char *info, const char *file, int line)
Matt Caswell0f113f32015-01-22 03:40:55 +0000254{
255 APP_INFO *ami, *amim;
256 int ret = 0;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000257
Rich Salzbbd86bf2016-01-07 15:06:38 -0500258 if (mem_check_on()) {
259 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000260
Matt Caswell9471f772016-03-08 15:44:05 +0000261 CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init);
262
Rich Salzbbd86bf2016-01-07 15:06:38 -0500263 if ((ami = OPENSSL_malloc(sizeof(*ami))) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000264 goto err;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000265
Matt Caswell9471f772016-03-08 15:44:05 +0000266 ami->threadid = CRYPTO_THREAD_get_current_id();
Matt Caswell0f113f32015-01-22 03:40:55 +0000267 ami->file = file;
268 ami->line = line;
269 ami->info = info;
270 ami->references = 1;
271 ami->next = NULL;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000272
Matt Caswell9471f772016-03-08 15:44:05 +0000273 amim = (APP_INFO *)CRYPTO_THREAD_get_local(&appinfokey);
274 CRYPTO_THREAD_set_local(&appinfokey, ami);
275
276 if (amim != NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000277 ami->next = amim;
Rich Salzbbd86bf2016-01-07 15:06:38 -0500278 ret = 1;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000279 err:
Rich Salzbbd86bf2016-01-07 15:06:38 -0500280 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
Matt Caswell0f113f32015-01-22 03:40:55 +0000281 }
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000282
Matt Caswell0f113f32015-01-22 03:40:55 +0000283 return (ret);
284}
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000285
Rich Salz4fae3862015-12-16 23:02:47 -0500286int CRYPTO_mem_debug_pop(void)
Matt Caswell0f113f32015-01-22 03:40:55 +0000287{
288 int ret = 0;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000289
Rich Salzbbd86bf2016-01-07 15:06:38 -0500290 if (mem_check_on()) {
291 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
Pascal Cuoq96e25c42016-01-10 13:43:37 +0100292 ret = pop_info();
Rich Salzbbd86bf2016-01-07 15:06:38 -0500293 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
Matt Caswell0f113f32015-01-22 03:40:55 +0000294 }
295 return (ret);
296}
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000297
Matt Caswell0f113f32015-01-22 03:40:55 +0000298static unsigned long break_order_num = 0;
Rich Salzbbd86bf2016-01-07 15:06:38 -0500299
300void CRYPTO_mem_debug_malloc(void *addr, size_t num, int before_p,
301 const char *file, int line)
Matt Caswell0f113f32015-01-22 03:40:55 +0000302{
303 MEM *m, *mm;
Matt Caswell9471f772016-03-08 15:44:05 +0000304 APP_INFO *amim;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000305
Matt Caswell0f113f32015-01-22 03:40:55 +0000306 switch (before_p & 127) {
307 case 0:
308 break;
309 case 1:
310 if (addr == NULL)
311 break;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000312
Rich Salzbbd86bf2016-01-07 15:06:38 -0500313 if (mem_check_on()) {
314 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
Matt Caswell9471f772016-03-08 15:44:05 +0000315
316 CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init);
317
Rich Salzb4faea52015-05-01 23:10:31 -0400318 if ((m = OPENSSL_malloc(sizeof(*m))) == NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000319 OPENSSL_free(addr);
Rich Salzbbd86bf2016-01-07 15:06:38 -0500320 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
Matt Caswell0f113f32015-01-22 03:40:55 +0000321 return;
322 }
323 if (mh == NULL) {
Dr. Stephen Henson62d05772015-12-24 15:51:23 +0000324 if ((mh = lh_MEM_new(mem_hash, mem_cmp)) == NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000325 OPENSSL_free(addr);
326 OPENSSL_free(m);
327 addr = NULL;
328 goto err;
329 }
330 }
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000331
Matt Caswell0f113f32015-01-22 03:40:55 +0000332 m->addr = addr;
333 m->file = file;
334 m->line = line;
335 m->num = num;
Matt Caswell9471f772016-03-08 15:44:05 +0000336 m->threadid = CRYPTO_THREAD_get_current_id();
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000337
Matt Caswell0f113f32015-01-22 03:40:55 +0000338 if (order == break_order_num) {
339 /* BREAK HERE */
340 m->order = order;
341 }
342 m->order = order++;
Richard Levitteef8ca6b2016-02-14 12:16:52 +0100343# ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
Richard Levitte012c5402015-12-02 13:19:45 +0100344 m->array_siz = backtrace(m->array, OSSL_NELEM(m->array));
Rich Salzbbd86bf2016-01-07 15:06:38 -0500345# endif
346 m->time = time(NULL);
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000347
Matt Caswell9471f772016-03-08 15:44:05 +0000348 amim = (APP_INFO *)CRYPTO_THREAD_get_local(&appinfokey);
349 m->app_info = amim;
350 if (amim != NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000351 amim->references++;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000352
Matt Caswell0f113f32015-01-22 03:40:55 +0000353 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:
Rich Salzbbd86bf2016-01-07 15:06:38 -0500361 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
Matt Caswell0f113f32015-01-22 03:40:55 +0000362 }
363 break;
364 }
365 return;
366}
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000367
Richard Levitte05c7b162016-02-17 02:24:25 +0100368void CRYPTO_mem_debug_free(void *addr, int before_p,
369 const char *file, int line)
Matt Caswell0f113f32015-01-22 03:40:55 +0000370{
371 MEM m, *mp;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000372
Matt Caswell0f113f32015-01-22 03:40:55 +0000373 switch (before_p) {
374 case 0:
375 if (addr == NULL)
376 break;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000377
Rich Salzbbd86bf2016-01-07 15:06:38 -0500378 if (mem_check_on() && (mh != NULL)) {
379 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000380
Matt Caswell0f113f32015-01-22 03:40:55 +0000381 m.addr = addr;
382 mp = lh_MEM_delete(mh, &m);
383 if (mp != NULL) {
Rich Salz25aaa982015-05-01 14:37:16 -0400384 app_info_free(mp->app_info);
Matt Caswell0f113f32015-01-22 03:40:55 +0000385 OPENSSL_free(mp);
386 }
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000387
Rich Salzbbd86bf2016-01-07 15:06:38 -0500388 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
Matt Caswell0f113f32015-01-22 03:40:55 +0000389 }
390 break;
391 case 1:
392 break;
393 }
394}
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000395
Rich Salzbbd86bf2016-01-07 15:06:38 -0500396void CRYPTO_mem_debug_realloc(void *addr1, void *addr2, size_t num,
397 int before_p, const char *file, int line)
Matt Caswell0f113f32015-01-22 03:40:55 +0000398{
399 MEM m, *mp;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000400
Matt Caswell0f113f32015-01-22 03:40:55 +0000401 switch (before_p) {
402 case 0:
403 break;
404 case 1:
405 if (addr2 == NULL)
406 break;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000407
Matt Caswell0f113f32015-01-22 03:40:55 +0000408 if (addr1 == NULL) {
Rich Salzbbd86bf2016-01-07 15:06:38 -0500409 CRYPTO_mem_debug_malloc(addr2, num, 128 | before_p, file, line);
Matt Caswell0f113f32015-01-22 03:40:55 +0000410 break;
411 }
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000412
Rich Salzbbd86bf2016-01-07 15:06:38 -0500413 if (mem_check_on()) {
414 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000415
Matt Caswell0f113f32015-01-22 03:40:55 +0000416 m.addr = addr1;
417 mp = lh_MEM_delete(mh, &m);
418 if (mp != NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000419 mp->addr = addr2;
420 mp->num = num;
Richard Levitteef8ca6b2016-02-14 12:16:52 +0100421#ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
Richard Levitte012c5402015-12-02 13:19:45 +0100422 mp->array_siz = backtrace(mp->array, OSSL_NELEM(mp->array));
423#endif
Matt Caswell0f113f32015-01-22 03:40:55 +0000424 (void)lh_MEM_insert(mh, mp);
425 }
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000426
Rich Salzbbd86bf2016-01-07 15:06:38 -0500427 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
Matt Caswell0f113f32015-01-22 03:40:55 +0000428 }
429 break;
430 }
431 return;
432}
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000433
Matt Caswell0f113f32015-01-22 03:40:55 +0000434typedef struct mem_leak_st {
435 BIO *bio;
436 int chunks;
Matt Caswell0f113f32015-01-22 03:40:55 +0000437 long bytes;
438} MEM_LEAK;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000439
Dr. Stephen Henson2a056de2015-12-24 16:20:54 +0000440static void print_leak(const MEM *m, MEM_LEAK *l)
Matt Caswell0f113f32015-01-22 03:40:55 +0000441{
442 char buf[1024];
443 char *bufp = buf;
444 APP_INFO *amip;
445 int ami_cnt;
446 struct tm *lcl = NULL;
Matt Caswell9471f772016-03-08 15:44:05 +0000447 /*
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;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000457
Richard Levitted420ac22003-12-27 14:40:17 +0000458#define BUF_REMAIN (sizeof buf - (size_t)(bufp - buf))
459
Rich Salzbbd86bf2016-01-07 15:06:38 -0500460 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);
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000464
Matt Caswell0f113f32015-01-22 03:40:55 +0000465 BIO_snprintf(bufp, BUF_REMAIN, "%5lu file=%s, line=%d, ",
466 m->order, m->file, m->line);
467 bufp += strlen(bufp);
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000468
Matt Caswell9471f772016-03-08 15:44:05 +0000469 tid.ltid = 0;
470 tid.tid = m->threadid;
471 BIO_snprintf(bufp, BUF_REMAIN, "thread=%lu, ", tid.ltid);
Rich Salzbbd86bf2016-01-07 15:06:38 -0500472 bufp += strlen(bufp);
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000473
Andy Polyakov5f0580c2015-09-30 10:36:21 +0200474 BIO_snprintf(bufp, BUF_REMAIN, "number=%d, address=%p\n",
475 m->num, m->addr);
Matt Caswell0f113f32015-01-22 03:40:55 +0000476 bufp += strlen(bufp);
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000477
Matt Caswell0f113f32015-01-22 03:40:55 +0000478 BIO_puts(l->bio, buf);
Geoff Thorpe4c329692008-08-06 15:54:15 +0000479
Matt Caswell0f113f32015-01-22 03:40:55 +0000480 l->chunks++;
481 l->bytes += m->num;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000482
Matt Caswell0f113f32015-01-22 03:40:55 +0000483 amip = m->app_info;
484 ami_cnt = 0;
Rich Salzbbd86bf2016-01-07 15:06:38 -0500485
Richard Levitte012c5402015-12-02 13:19:45 +0100486 if (amip) {
Matt Caswell9471f772016-03-08 15:44:05 +0000487 ti = amip->threadid;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000488
Richard Levitte012c5402015-12-02 13:19:45 +0100489 do {
490 int buf_len;
491 int info_len;
Matt Caswell0f113f32015-01-22 03:40:55 +0000492
Richard Levitte012c5402015-12-02 13:19:45 +0100493 ami_cnt++;
494 memset(buf, '>', ami_cnt);
Matt Caswell9471f772016-03-08 15:44:05 +0000495 tid.ltid = 0;
496 tid.tid = amip->threadid;
Richard Levitte012c5402015-12-02 13:19:45 +0100497 BIO_snprintf(buf + ami_cnt, sizeof buf - ami_cnt,
498 " thread=%lu, file=%s, line=%d, info=\"",
Matt Caswell9471f772016-03-08 15:44:05 +0000499 tid.ltid, amip->file,
Richard Levitte012c5402015-12-02 13:19:45 +0100500 amip->line);
Matt Caswell0f113f32015-01-22 03:40:55 +0000501 buf_len = strlen(buf);
Richard Levitte012c5402015-12-02 13:19:45 +0100502 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 {
Rich Salz7644a9a2015-12-16 16:12:24 -0500507 OPENSSL_strlcpy(buf + buf_len, amip->info, sizeof buf - buf_len);
Richard Levitte012c5402015-12-02 13:19:45 +0100508 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;
Matt Caswell0f113f32015-01-22 03:40:55 +0000515 }
Matt Caswell9471f772016-03-08 15:44:05 +0000516 while (amip && CRYPTO_THREAD_compare_id(amip->threadid, ti));
Matt Caswell0f113f32015-01-22 03:40:55 +0000517 }
Geoff Thorpef7ccba32008-03-28 02:49:43 +0000518
Richard Levitteef8ca6b2016-02-14 12:16:52 +0100519#ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
Richard Levitte012c5402015-12-02 13:19:45 +0100520 {
521 size_t i;
522 char **strings = backtrace_symbols(m->array, m->array_siz);
Rich Salzbbd86bf2016-01-07 15:06:38 -0500523
Richard Levitte012c5402015-12-02 13:19:45 +0100524 for (i = 0; i < m->array_siz; i++)
525 fprintf(stderr, "##> %s\n", strings[i]);
Richard Levitte012c5402015-12-02 13:19:45 +0100526 free(strings);
527 }
528#endif
Matt Caswell0f113f32015-01-22 03:40:55 +0000529}
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000530
Dr. Stephen Henson2a056de2015-12-24 16:20:54 +0000531IMPLEMENT_LHASH_DOALL_ARG_CONST(MEM, MEM_LEAK);
Geoff Thorpe98d517c2001-01-09 00:13:25 +0000532
Dr. Stephen Henson4e482ae2016-01-10 23:25:07 +0000533int CRYPTO_mem_leaks(BIO *b)
Matt Caswell0f113f32015-01-22 03:40:55 +0000534{
535 MEM_LEAK ml;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000536
Matt Caswell1ee7b8b2016-04-14 21:28:54 +0100537 /*
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
Matt Caswell38a6d7f2016-02-09 22:09:56 +0000543 /* Ensure all resources are released */
Rich Salzf672aee2016-02-09 11:52:40 -0500544 OPENSSL_cleanup();
Matt Caswell38a6d7f2016-02-09 22:09:56 +0000545
Matt Caswell9471f772016-03-08 15:44:05 +0000546 CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init);
547
Rich Salzbbd86bf2016-01-07 15:06:38 -0500548 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
Bodo Möller934397e2001-02-19 10:32:53 +0000549
Matt Caswell0f113f32015-01-22 03:40:55 +0000550 ml.bio = b;
551 ml.bytes = 0;
552 ml.chunks = 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000553 if (mh != NULL)
Dr. Stephen Henson2a056de2015-12-24 16:20:54 +0000554 lh_MEM_doall_MEM_LEAK(mh, print_leak, &ml);
Matt Caswellb3895f42016-03-30 18:12:59 +0100555
Matt Caswell0f113f32015-01-22 03:40:55 +0000556 if (ml.chunks != 0) {
557 BIO_printf(b, "%ld bytes leaked in %d chunks\n", ml.bytes, ml.chunks);
Matt Caswell0f113f32015-01-22 03:40:55 +0000558 } 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
Rich Salzbbd86bf2016-01-07 15:06:38 -0500563 * call this function, we declare it to be their fault.)
Matt Caswell0f113f32015-01-22 03:40:55 +0000564 */
565 int old_mh_mode;
Bodo Möller3ac82fa2000-12-15 16:40:35 +0000566
Matt Caswell9471f772016-03-08 15:44:05 +0000567 CRYPTO_THREAD_write_lock(malloc_lock);
Bodo Möller3ac82fa2000-12-15 16:40:35 +0000568
Matt Caswell0f113f32015-01-22 03:40:55 +0000569 /*
Rich Salzbbd86bf2016-01-07 15:06:38 -0500570 * avoid deadlock when lh_free() uses CRYPTO_mem_debug_free(), which uses
571 * mem_check_on
Matt Caswell0f113f32015-01-22 03:40:55 +0000572 */
573 old_mh_mode = mh_mode;
574 mh_mode = CRYPTO_MEM_CHECK_OFF;
Bodo Möller3ac82fa2000-12-15 16:40:35 +0000575
Rich Salz25aaa982015-05-01 14:37:16 -0400576 lh_MEM_free(mh);
577 mh = NULL;
Bodo Möller3ac82fa2000-12-15 16:40:35 +0000578
Matt Caswell0f113f32015-01-22 03:40:55 +0000579 mh_mode = old_mh_mode;
Matt Caswell9471f772016-03-08 15:44:05 +0000580 CRYPTO_THREAD_unlock(malloc_lock);
Matt Caswell0f113f32015-01-22 03:40:55 +0000581 }
Matt Caswell9471f772016-03-08 15:44:05 +0000582 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
Dr. Stephen Henson4e482ae2016-01-10 23:25:07 +0000591 return ml.chunks == 0 ? 1 : 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000592}
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000593
Rich Salzbbd86bf2016-01-07 15:06:38 -0500594# ifndef OPENSSL_NO_STDIO
Dr. Stephen Henson4e482ae2016-01-10 23:25:07 +0000595int CRYPTO_mem_leaks_fp(FILE *fp)
Matt Caswell0f113f32015-01-22 03:40:55 +0000596{
597 BIO *b;
Dr. Stephen Henson4e482ae2016-01-10 23:25:07 +0000598 int ret;
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000599
Matt Caswell0f113f32015-01-22 03:40:55 +0000600 /*
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 */
Rich Salzbbd86bf2016-01-07 15:06:38 -0500605 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
Matt Caswell0f113f32015-01-22 03:40:55 +0000606 b = BIO_new(BIO_s_file());
Rich Salzbbd86bf2016-01-07 15:06:38 -0500607 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
Matt Caswell90945fa2015-10-30 11:12:26 +0000608 if (b == NULL)
Dr. Stephen Henson4e482ae2016-01-10 23:25:07 +0000609 return -1;
Matt Caswell0f113f32015-01-22 03:40:55 +0000610 BIO_set_fp(b, fp, BIO_NOCLOSE);
Dr. Stephen Henson4e482ae2016-01-10 23:25:07 +0000611 ret = CRYPTO_mem_leaks(b);
Matt Caswell0f113f32015-01-22 03:40:55 +0000612 BIO_free(b);
Dr. Stephen Henson4e482ae2016-01-10 23:25:07 +0000613 return ret;
Matt Caswell0f113f32015-01-22 03:40:55 +0000614}
Rich Salzbbd86bf2016-01-07 15:06:38 -0500615# endif
616
Richard Levitte9ac42ed1999-12-17 12:56:24 +0000617#endif