|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
Packit Service |
084de1 |
* this file except in compliance with the License. You can obtain a copy
|
|
Packit Service |
084de1 |
* in the file LICENSE in the source distribution or at
|
|
Packit Service |
084de1 |
* https://www.openssl.org/source/license.html
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#include <stdio.h>
|
|
Packit Service |
084de1 |
#include <stdlib.h>
|
|
Packit Service |
084de1 |
#include <time.h>
|
|
Packit Service |
084de1 |
#include "internal/cryptlib.h"
|
|
Packit Service |
084de1 |
#include "internal/thread_once.h"
|
|
Packit Service |
084de1 |
#include <openssl/crypto.h>
|
|
Packit Service |
084de1 |
#include <openssl/buffer.h>
|
|
Packit Service |
084de1 |
#include "internal/bio.h"
|
|
Packit Service |
084de1 |
#include <openssl/lhash.h>
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
|
|
Packit Service |
084de1 |
# include <execinfo.h>
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* The state changes to CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE when
|
|
Packit Service |
084de1 |
* the application asks for it (usually after library initialisation for
|
|
Packit Service |
084de1 |
* which no book-keeping is desired). State CRYPTO_MEM_CHECK_ON exists only
|
|
Packit Service |
084de1 |
* temporarily when the library thinks that certain allocations should not be
|
|
Packit Service |
084de1 |
* checked (e.g. the data structures used for memory checking). It is not
|
|
Packit Service |
084de1 |
* suitable as an initial state: the library will unexpectedly enable memory
|
|
Packit Service |
084de1 |
* checking when it executes one of those sections that want to disable
|
|
Packit Service |
084de1 |
* checking temporarily. State CRYPTO_MEM_CHECK_ENABLE without ..._ON makes
|
|
Packit Service |
084de1 |
* no sense whatsoever.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
|
|
Packit Service |
084de1 |
static int mh_mode = CRYPTO_MEM_CHECK_OFF;
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
|
|
Packit Service |
084de1 |
static unsigned long order = 0; /* number of memory requests */
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*-
|
|
Packit Service |
084de1 |
* For application-defined information (static C-string `info')
|
|
Packit Service |
084de1 |
* to be displayed in memory leak list.
|
|
Packit Service |
084de1 |
* Each thread has its own stack. For applications, there is
|
|
Packit Service |
084de1 |
* OPENSSL_mem_debug_push("...") to push an entry,
|
|
Packit Service |
084de1 |
* OPENSSL_mem_debug_pop() to pop an entry,
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
struct app_mem_info_st {
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_ID threadid;
|
|
Packit Service |
084de1 |
const char *file;
|
|
Packit Service |
084de1 |
int line;
|
|
Packit Service |
084de1 |
const char *info;
|
|
Packit Service |
084de1 |
struct app_mem_info_st *next; /* tail of thread's stack */
|
|
Packit Service |
084de1 |
int references;
|
|
Packit Service |
084de1 |
};
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static CRYPTO_ONCE memdbg_init = CRYPTO_ONCE_STATIC_INIT;
|
|
Packit Service |
084de1 |
CRYPTO_RWLOCK *memdbg_lock;
|
|
Packit Service |
084de1 |
static CRYPTO_RWLOCK *long_memdbg_lock;
|
|
Packit Service |
084de1 |
static CRYPTO_THREAD_LOCAL appinfokey;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* memory-block description */
|
|
Packit Service |
084de1 |
struct mem_st {
|
|
Packit Service |
084de1 |
void *addr;
|
|
Packit Service |
084de1 |
int num;
|
|
Packit Service |
084de1 |
const char *file;
|
|
Packit Service |
084de1 |
int line;
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_ID threadid;
|
|
Packit Service |
084de1 |
unsigned long order;
|
|
Packit Service |
084de1 |
time_t time;
|
|
Packit Service |
084de1 |
APP_INFO *app_info;
|
|
Packit Service |
084de1 |
#ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
|
|
Packit Service |
084de1 |
void *array[30];
|
|
Packit Service |
084de1 |
size_t array_siz;
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
};
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* hash-table of memory requests (address as * key); access requires
|
|
Packit Service |
084de1 |
* long_memdbg_lock lock
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
static LHASH_OF(MEM) *mh = NULL;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* num_disable > 0 iff mh_mode == CRYPTO_MEM_CHECK_ON (w/o ..._ENABLE) */
|
|
Packit Service |
084de1 |
static unsigned int num_disable = 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Valid iff num_disable > 0. long_memdbg_lock is locked exactly in this
|
|
Packit Service |
084de1 |
* case (by the thread named in disabling_thread).
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
static CRYPTO_THREAD_ID disabling_threadid;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
DEFINE_RUN_ONCE_STATIC(do_memdbg_init)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
memdbg_lock = CRYPTO_THREAD_lock_new();
|
|
Packit Service |
084de1 |
long_memdbg_lock = CRYPTO_THREAD_lock_new();
|
|
Packit Service |
084de1 |
if (memdbg_lock == NULL || long_memdbg_lock == NULL
|
|
Packit Service |
084de1 |
|| !CRYPTO_THREAD_init_local(&appinfokey, NULL)) {
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_lock_free(memdbg_lock);
|
|
Packit Service |
084de1 |
memdbg_lock = NULL;
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_lock_free(long_memdbg_lock);
|
|
Packit Service |
084de1 |
long_memdbg_lock = NULL;
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static void app_info_free(APP_INFO *inf)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
if (inf == NULL)
|
|
Packit Service |
084de1 |
return;
|
|
Packit Service |
084de1 |
if (--(inf->references) <= 0) {
|
|
Packit Service |
084de1 |
app_info_free(inf->next);
|
|
Packit Service |
084de1 |
OPENSSL_free(inf);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int CRYPTO_mem_ctrl(int mode)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
#ifdef OPENSSL_NO_CRYPTO_MDEBUG
|
|
Packit Service |
084de1 |
return mode - mode;
|
|
Packit Service |
084de1 |
#else
|
|
Packit Service |
084de1 |
int ret = mh_mode;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!RUN_ONCE(&memdbg_init, do_memdbg_init))
|
|
Packit Service |
084de1 |
return -1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_write_lock(memdbg_lock);
|
|
Packit Service |
084de1 |
switch (mode) {
|
|
Packit Service |
084de1 |
default:
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
case CRYPTO_MEM_CHECK_ON:
|
|
Packit Service |
084de1 |
mh_mode = CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE;
|
|
Packit Service |
084de1 |
num_disable = 0;
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
case CRYPTO_MEM_CHECK_OFF:
|
|
Packit Service |
084de1 |
mh_mode = 0;
|
|
Packit Service |
084de1 |
num_disable = 0;
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* switch off temporarily (for library-internal use): */
|
|
Packit Service |
084de1 |
case CRYPTO_MEM_CHECK_DISABLE:
|
|
Packit Service |
084de1 |
if (mh_mode & CRYPTO_MEM_CHECK_ON) {
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_ID cur = CRYPTO_THREAD_get_current_id();
|
|
Packit Service |
084de1 |
/* see if we don't have long_memdbg_lock already */
|
|
Packit Service |
084de1 |
if (!num_disable
|
|
Packit Service |
084de1 |
|| !CRYPTO_THREAD_compare_id(disabling_threadid, cur)) {
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Long-time lock long_memdbg_lock must not be claimed
|
|
Packit Service |
084de1 |
* while we're holding memdbg_lock, or we'll deadlock
|
|
Packit Service |
084de1 |
* if somebody else holds long_memdbg_lock (and cannot
|
|
Packit Service |
084de1 |
* release it because we block entry to this function). Give
|
|
Packit Service |
084de1 |
* them a chance, first, and then claim the locks in
|
|
Packit Service |
084de1 |
* appropriate order (long-time lock first).
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_unlock(memdbg_lock);
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Note that after we have waited for long_memdbg_lock and
|
|
Packit Service |
084de1 |
* memdbg_lock, we'll still be in the right "case" and
|
|
Packit Service |
084de1 |
* "if" branch because MemCheck_start and MemCheck_stop may
|
|
Packit Service |
084de1 |
* never be used while there are multiple OpenSSL threads.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_write_lock(long_memdbg_lock);
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_write_lock(memdbg_lock);
|
|
Packit Service |
084de1 |
mh_mode &= ~CRYPTO_MEM_CHECK_ENABLE;
|
|
Packit Service |
084de1 |
disabling_threadid = cur;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
num_disable++;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
case CRYPTO_MEM_CHECK_ENABLE:
|
|
Packit Service |
084de1 |
if (mh_mode & CRYPTO_MEM_CHECK_ON) {
|
|
Packit Service |
084de1 |
if (num_disable) { /* always true, or something is going wrong */
|
|
Packit Service |
084de1 |
num_disable--;
|
|
Packit Service |
084de1 |
if (num_disable == 0) {
|
|
Packit Service |
084de1 |
mh_mode |= CRYPTO_MEM_CHECK_ENABLE;
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_unlock(long_memdbg_lock);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_unlock(memdbg_lock);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int mem_check_on(void)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
int ret = 0;
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_ID cur;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (mh_mode & CRYPTO_MEM_CHECK_ON) {
|
|
Packit Service |
084de1 |
if (!RUN_ONCE(&memdbg_init, do_memdbg_init))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
cur = CRYPTO_THREAD_get_current_id();
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_read_lock(memdbg_lock);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ret = (mh_mode & CRYPTO_MEM_CHECK_ENABLE)
|
|
Packit Service |
084de1 |
|| !CRYPTO_THREAD_compare_id(disabling_threadid, cur);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_unlock(memdbg_lock);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int mem_cmp(const MEM *a, const MEM *b)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
#ifdef _WIN64
|
|
Packit Service |
084de1 |
const char *ap = (const char *)a->addr, *bp = (const char *)b->addr;
|
|
Packit Service |
084de1 |
if (ap == bp)
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
else if (ap > bp)
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
else
|
|
Packit Service |
084de1 |
return -1;
|
|
Packit Service |
084de1 |
#else
|
|
Packit Service |
084de1 |
return (const char *)a->addr - (const char *)b->addr;
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static unsigned long mem_hash(const MEM *a)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
size_t ret;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ret = (size_t)a->addr;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ret = ret * 17851 + (ret >> 14) * 7 + (ret >> 4) * 251;
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* returns 1 if there was an info to pop, 0 if the stack was empty. */
|
|
Packit Service |
084de1 |
static int pop_info(void)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
APP_INFO *current = NULL;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!RUN_ONCE(&memdbg_init, do_memdbg_init))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
current = (APP_INFO *)CRYPTO_THREAD_get_local(&appinfokey);
|
|
Packit Service |
084de1 |
if (current != NULL) {
|
|
Packit Service |
084de1 |
APP_INFO *next = current->next;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (next != NULL) {
|
|
Packit Service |
084de1 |
next->references++;
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_set_local(&appinfokey, next);
|
|
Packit Service |
084de1 |
} else {
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_set_local(&appinfokey, NULL);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (--(current->references) <= 0) {
|
|
Packit Service |
084de1 |
current->next = NULL;
|
|
Packit Service |
084de1 |
if (next != NULL)
|
|
Packit Service |
084de1 |
next->references--;
|
|
Packit Service |
084de1 |
OPENSSL_free(current);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int CRYPTO_mem_debug_push(const char *info, const char *file, int line)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
APP_INFO *ami, *amim;
|
|
Packit Service |
084de1 |
int ret = 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (mem_check_on()) {
|
|
Packit Service |
084de1 |
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!RUN_ONCE(&memdbg_init, do_memdbg_init)
|
|
Packit Service |
084de1 |
|| (ami = OPENSSL_malloc(sizeof(*ami))) == NULL)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ami->threadid = CRYPTO_THREAD_get_current_id();
|
|
Packit Service |
084de1 |
ami->file = file;
|
|
Packit Service |
084de1 |
ami->line = line;
|
|
Packit Service |
084de1 |
ami->info = info;
|
|
Packit Service |
084de1 |
ami->references = 1;
|
|
Packit Service |
084de1 |
ami->next = NULL;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
amim = (APP_INFO *)CRYPTO_THREAD_get_local(&appinfokey);
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_set_local(&appinfokey, ami);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (amim != NULL)
|
|
Packit Service |
084de1 |
ami->next = amim;
|
|
Packit Service |
084de1 |
ret = 1;
|
|
Packit Service |
084de1 |
err:
|
|
Packit Service |
084de1 |
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int CRYPTO_mem_debug_pop(void)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
int ret = 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (mem_check_on()) {
|
|
Packit Service |
084de1 |
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
|
|
Packit Service |
084de1 |
ret = pop_info();
|
|
Packit Service |
084de1 |
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static unsigned long break_order_num = 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
void CRYPTO_mem_debug_malloc(void *addr, size_t num, int before_p,
|
|
Packit Service |
084de1 |
const char *file, int line)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
MEM *m, *mm;
|
|
Packit Service |
084de1 |
APP_INFO *amim;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
switch (before_p & 127) {
|
|
Packit Service |
084de1 |
case 0:
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
case 1:
|
|
Packit Service |
084de1 |
if (addr == NULL)
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (mem_check_on()) {
|
|
Packit Service |
084de1 |
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!RUN_ONCE(&memdbg_init, do_memdbg_init)
|
|
Packit Service |
084de1 |
|| (m = OPENSSL_malloc(sizeof(*m))) == NULL) {
|
|
Packit Service |
084de1 |
OPENSSL_free(addr);
|
|
Packit Service |
084de1 |
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
|
|
Packit Service |
084de1 |
return;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (mh == NULL) {
|
|
Packit Service |
084de1 |
if ((mh = lh_MEM_new(mem_hash, mem_cmp)) == NULL) {
|
|
Packit Service |
084de1 |
OPENSSL_free(addr);
|
|
Packit Service |
084de1 |
OPENSSL_free(m);
|
|
Packit Service |
084de1 |
addr = NULL;
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
m->addr = addr;
|
|
Packit Service |
084de1 |
m->file = file;
|
|
Packit Service |
084de1 |
m->line = line;
|
|
Packit Service |
084de1 |
m->num = num;
|
|
Packit Service |
084de1 |
m->threadid = CRYPTO_THREAD_get_current_id();
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (order == break_order_num) {
|
|
Packit Service |
084de1 |
/* BREAK HERE */
|
|
Packit Service |
084de1 |
m->order = order;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
m->order = order++;
|
|
Packit Service |
084de1 |
# ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
|
|
Packit Service |
084de1 |
m->array_siz = backtrace(m->array, OSSL_NELEM(m->array));
|
|
Packit Service |
084de1 |
# endif
|
|
Packit Service |
084de1 |
m->time = time(NULL);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
amim = (APP_INFO *)CRYPTO_THREAD_get_local(&appinfokey);
|
|
Packit Service |
084de1 |
m->app_info = amim;
|
|
Packit Service |
084de1 |
if (amim != NULL)
|
|
Packit Service |
084de1 |
amim->references++;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if ((mm = lh_MEM_insert(mh, m)) != NULL) {
|
|
Packit Service |
084de1 |
/* Not good, but don't sweat it */
|
|
Packit Service |
084de1 |
if (mm->app_info != NULL) {
|
|
Packit Service |
084de1 |
mm->app_info->references--;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
OPENSSL_free(mm);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
err:
|
|
Packit Service |
084de1 |
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
return;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
void CRYPTO_mem_debug_free(void *addr, int before_p,
|
|
Packit Service |
084de1 |
const char *file, int line)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
MEM m, *mp;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
switch (before_p) {
|
|
Packit Service |
084de1 |
case 0:
|
|
Packit Service |
084de1 |
if (addr == NULL)
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (mem_check_on() && (mh != NULL)) {
|
|
Packit Service |
084de1 |
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
m.addr = addr;
|
|
Packit Service |
084de1 |
mp = lh_MEM_delete(mh, &m);
|
|
Packit Service |
084de1 |
if (mp != NULL) {
|
|
Packit Service |
084de1 |
app_info_free(mp->app_info);
|
|
Packit Service |
084de1 |
OPENSSL_free(mp);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
case 1:
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
void CRYPTO_mem_debug_realloc(void *addr1, void *addr2, size_t num,
|
|
Packit Service |
084de1 |
int before_p, const char *file, int line)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
MEM m, *mp;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
switch (before_p) {
|
|
Packit Service |
084de1 |
case 0:
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
case 1:
|
|
Packit Service |
084de1 |
if (addr2 == NULL)
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (addr1 == NULL) {
|
|
Packit Service |
084de1 |
CRYPTO_mem_debug_malloc(addr2, num, 128 | before_p, file, line);
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (mem_check_on()) {
|
|
Packit Service |
084de1 |
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
m.addr = addr1;
|
|
Packit Service |
084de1 |
mp = lh_MEM_delete(mh, &m);
|
|
Packit Service |
084de1 |
if (mp != NULL) {
|
|
Packit Service |
084de1 |
mp->addr = addr2;
|
|
Packit Service |
084de1 |
mp->num = num;
|
|
Packit Service |
084de1 |
#ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
|
|
Packit Service |
084de1 |
mp->array_siz = backtrace(mp->array, OSSL_NELEM(mp->array));
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
(void)lh_MEM_insert(mh, mp);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
return;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
typedef struct mem_leak_st {
|
|
Packit Service |
084de1 |
int (*print_cb) (const char *str, size_t len, void *u);
|
|
Packit Service |
084de1 |
void *print_cb_arg;
|
|
Packit Service |
084de1 |
int chunks;
|
|
Packit Service |
084de1 |
long bytes;
|
|
Packit Service |
084de1 |
} MEM_LEAK;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static void print_leak(const MEM *m, MEM_LEAK *l)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
char buf[1024];
|
|
Packit Service |
084de1 |
char *bufp = buf;
|
|
Packit Service |
084de1 |
size_t len = sizeof(buf), ami_cnt;
|
|
Packit Service |
084de1 |
APP_INFO *amip;
|
|
Packit Service |
084de1 |
int n;
|
|
Packit Service |
084de1 |
struct tm *lcl = NULL;
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Convert between CRYPTO_THREAD_ID (which could be anything at all) and
|
|
Packit Service |
084de1 |
* a long. This may not be meaningful depending on what CRYPTO_THREAD_ID is
|
|
Packit Service |
084de1 |
* but hopefully should give something sensible on most platforms
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
union {
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_ID tid;
|
|
Packit Service |
084de1 |
unsigned long ltid;
|
|
Packit Service |
084de1 |
} tid;
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_ID ti;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
lcl = localtime(&m->time);
|
|
Packit Service |
084de1 |
n = BIO_snprintf(bufp, len, "[%02d:%02d:%02d] ",
|
|
Packit Service |
084de1 |
lcl->tm_hour, lcl->tm_min, lcl->tm_sec);
|
|
Packit Service |
084de1 |
if (n <= 0) {
|
|
Packit Service |
084de1 |
bufp[0] = '\0';
|
|
Packit Service |
084de1 |
return;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
bufp += n;
|
|
Packit Service |
084de1 |
len -= n;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
n = BIO_snprintf(bufp, len, "%5lu file=%s, line=%d, ",
|
|
Packit Service |
084de1 |
m->order, m->file, m->line);
|
|
Packit Service |
084de1 |
if (n <= 0)
|
|
Packit Service |
084de1 |
return;
|
|
Packit Service |
084de1 |
bufp += n;
|
|
Packit Service |
084de1 |
len -= n;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
tid.ltid = 0;
|
|
Packit Service |
084de1 |
tid.tid = m->threadid;
|
|
Packit Service |
084de1 |
n = BIO_snprintf(bufp, len, "thread=%lu, ", tid.ltid);
|
|
Packit Service |
084de1 |
if (n <= 0)
|
|
Packit Service |
084de1 |
return;
|
|
Packit Service |
084de1 |
bufp += n;
|
|
Packit Service |
084de1 |
len -= n;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
n = BIO_snprintf(bufp, len, "number=%d, address=%p\n", m->num, m->addr);
|
|
Packit Service |
084de1 |
if (n <= 0)
|
|
Packit Service |
084de1 |
return;
|
|
Packit Service |
084de1 |
bufp += n;
|
|
Packit Service |
084de1 |
len -= n;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
l->print_cb(buf, (size_t)(bufp - buf), l->print_cb_arg);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
l->chunks++;
|
|
Packit Service |
084de1 |
l->bytes += m->num;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
amip = m->app_info;
|
|
Packit Service |
084de1 |
ami_cnt = 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (amip) {
|
|
Packit Service |
084de1 |
ti = amip->threadid;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
do {
|
|
Packit Service |
084de1 |
int buf_len;
|
|
Packit Service |
084de1 |
int info_len;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ami_cnt++;
|
|
Packit Service |
084de1 |
if (ami_cnt >= sizeof(buf) - 1)
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
memset(buf, '>', ami_cnt);
|
|
Packit Service |
084de1 |
buf[ami_cnt] = '\0';
|
|
Packit Service |
084de1 |
tid.ltid = 0;
|
|
Packit Service |
084de1 |
tid.tid = amip->threadid;
|
|
Packit Service |
084de1 |
n = BIO_snprintf(buf + ami_cnt, sizeof(buf) - ami_cnt,
|
|
Packit Service |
084de1 |
" thread=%lu, file=%s, line=%d, info=\"",
|
|
Packit Service |
084de1 |
tid.ltid, amip->file, amip->line);
|
|
Packit Service |
084de1 |
if (n <= 0)
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
buf_len = ami_cnt + n;
|
|
Packit Service |
084de1 |
info_len = strlen(amip->info);
|
|
Packit Service |
084de1 |
if (128 - buf_len - 3 < info_len) {
|
|
Packit Service |
084de1 |
memcpy(buf + buf_len, amip->info, 128 - buf_len - 3);
|
|
Packit Service |
084de1 |
buf_len = 128 - 3;
|
|
Packit Service |
084de1 |
} else {
|
|
Packit Service |
084de1 |
n = BIO_snprintf(buf + buf_len, sizeof(buf) - buf_len, "%s",
|
|
Packit Service |
084de1 |
amip->info);
|
|
Packit Service |
084de1 |
if (n < 0)
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
buf_len += n;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
n = BIO_snprintf(buf + buf_len, sizeof(buf) - buf_len, "\"\n");
|
|
Packit Service |
084de1 |
if (n <= 0)
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
l->print_cb(buf, buf_len + n, l->print_cb_arg);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
amip = amip->next;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
while (amip && CRYPTO_THREAD_compare_id(amip->threadid, ti));
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
size_t i;
|
|
Packit Service |
084de1 |
char **strings = backtrace_symbols(m->array, m->array_siz);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
for (i = 0; i < m->array_siz; i++)
|
|
Packit Service |
084de1 |
fprintf(stderr, "##> %s\n", strings[i]);
|
|
Packit Service |
084de1 |
free(strings);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
IMPLEMENT_LHASH_DOALL_ARG_CONST(MEM, MEM_LEAK);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int CRYPTO_mem_leaks_cb(int (*cb) (const char *str, size_t len, void *u),
|
|
Packit Service |
084de1 |
void *u)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
MEM_LEAK ml;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Ensure all resources are released */
|
|
Packit Service |
084de1 |
OPENSSL_cleanup();
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!RUN_ONCE(&memdbg_init, do_memdbg_init))
|
|
Packit Service |
084de1 |
return -1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ml.print_cb = cb;
|
|
Packit Service |
084de1 |
ml.print_cb_arg = u;
|
|
Packit Service |
084de1 |
ml.bytes = 0;
|
|
Packit Service |
084de1 |
ml.chunks = 0;
|
|
Packit Service |
084de1 |
if (mh != NULL)
|
|
Packit Service |
084de1 |
lh_MEM_doall_MEM_LEAK(mh, print_leak, &ml);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (ml.chunks != 0) {
|
|
Packit Service |
084de1 |
char buf[256];
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
BIO_snprintf(buf, sizeof(buf), "%ld bytes leaked in %d chunks\n",
|
|
Packit Service |
084de1 |
ml.bytes, ml.chunks);
|
|
Packit Service |
084de1 |
cb(buf, strlen(buf), u);
|
|
Packit Service |
084de1 |
} else {
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Make sure that, if we found no leaks, memory-leak debugging itself
|
|
Packit Service |
084de1 |
* does not introduce memory leaks (which might irritate external
|
|
Packit Service |
084de1 |
* debugging tools). (When someone enables leak checking, but does not
|
|
Packit Service |
084de1 |
* call this function, we declare it to be their fault.)
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
int old_mh_mode;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_write_lock(memdbg_lock);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* avoid deadlock when lh_free() uses CRYPTO_mem_debug_free(), which uses
|
|
Packit Service |
084de1 |
* mem_check_on
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
old_mh_mode = mh_mode;
|
|
Packit Service |
084de1 |
mh_mode = CRYPTO_MEM_CHECK_OFF;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
lh_MEM_free(mh);
|
|
Packit Service |
084de1 |
mh = NULL;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
mh_mode = old_mh_mode;
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_unlock(memdbg_lock);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_OFF);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Clean up locks etc */
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_cleanup_local(&appinfokey);
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_lock_free(memdbg_lock);
|
|
Packit Service |
084de1 |
CRYPTO_THREAD_lock_free(long_memdbg_lock);
|
|
Packit Service |
084de1 |
memdbg_lock = NULL;
|
|
Packit Service |
084de1 |
long_memdbg_lock = NULL;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
return ml.chunks == 0 ? 1 : 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int print_bio(const char *str, size_t len, void *b)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
return BIO_write((BIO *)b, str, len);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int CRYPTO_mem_leaks(BIO *b)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* OPENSSL_cleanup() will free the ex_data locks so we can't have any
|
|
Packit Service |
084de1 |
* ex_data hanging around
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
bio_free_ex_data(b);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
return CRYPTO_mem_leaks_cb(print_bio, b);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# ifndef OPENSSL_NO_STDIO
|
|
Packit Service |
084de1 |
int CRYPTO_mem_leaks_fp(FILE *fp)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
BIO *b;
|
|
Packit Service |
084de1 |
int ret;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Need to turn off memory checking when allocated BIOs ... especially as
|
|
Packit Service |
084de1 |
* we're creating them at a time when we're trying to check we've not
|
|
Packit Service |
084de1 |
* left anything un-free()'d!!
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
|
|
Packit Service |
084de1 |
b = BIO_new(BIO_s_file());
|
|
Packit Service |
084de1 |
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
|
|
Packit Service |
084de1 |
if (b == NULL)
|
|
Packit Service |
084de1 |
return -1;
|
|
Packit Service |
084de1 |
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
|
Packit Service |
084de1 |
ret = CRYPTO_mem_leaks_cb(print_bio, b);
|
|
Packit Service |
084de1 |
BIO_free(b);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
# endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#endif
|