Reputation: 41
I'm developing some modules to verify signing with c++ in linux. And, I'm testing and debugging it in visual studio 2015. But, for memory leak testing, I do not know how to detect it.
First, I tried to use vld.h and crtdbg.h, both of them cannot detect openssl elements memory leak such as Bio*. or X509*. Are there other tools to support detect memory leak of openssl elements??
Of course I tried to use "int CRYPTO_mem_leaks(Bio *)" but it is defined as comments like below and in visual studio I do not know to make them active. like:
# ifndef OPENSSL_NO_CRYPTO_MDEBUG
# define OPENSSL_mem_debug_push(info) \
CRYPTO_mem_debug_push(info, OPENSSL_FILE, OPENSSL_LINE)
# define OPENSSL_mem_debug_pop() \
CRYPTO_mem_debug_pop()
int CRYPTO_mem_debug_push(const char *info, const char *file, int
line);
int CRYPTO_mem_debug_pop(void);
void CRYPTO_get_alloc_counts(int *mcount, int *rcount, int *fcount);
/*-
* Debugging functions (enabled by CRYPTO_set_mem_debug(1))
* The flag argument has the following significance:
* 0: called before the actual memory allocation has taken place
* 1: called after the actual memory allocation has taken place
*/
void CRYPTO_mem_debug_malloc(void *addr, size_t num, int flag,
const char *file, int line);
void CRYPTO_mem_debug_realloc(void *addr1, void *addr2, size_t num,
int flag,
const char *file, int line);
void CRYPTO_mem_debug_free(void *addr, int flag,
const char *file, int line);
int CRYPTO_mem_leaks_cb(int (*cb) (const char *str, size_t len, void *u),
void *u);
# ifndef OPENSSL_NO_STDIO
int CRYPTO_mem_leaks_fp(FILE *);
# endif
int CRYPTO_mem_leaks(BIO *bio);
# endif
Upvotes: 2
Views: 1770
Reputation: 17373
From the documentation for CRYPTO_set_mem_functions():
If no allocations have been done, it is possible to "swap out" the default implementations for OPENSSL_malloc(), OPENSSL_realloc and OPENSSL_free() and replace them with alternate versions (hooks). CRYPTO_get_mem_functions() function fills in the given arguments with the function pointers for the current implementations. With CRYPTO_set_mem_functions(), you can specify a different set of functions. If any of m, r, or f are NULL, then the function is not changed.
You can use this mechanism to insert your own memory allocation tracking functions or to be creative with breakpoints if you suspect certain problems under certain circumstances. When doing Windows, you could invoke the _malloc_dbg(), _realloc_dbg() and _free_dbg() functions from inside those hooks.
More generically, the Visual Studio debugger and C Run-time Library (CRT) can help you detect and identify memory leaks. This is the best starting point, in my opinion. Reading the page from the start to the end will give you a lot of tools to attack your memory leak problem.
To get full insight, step through OpenSSL code and get full debug capabilities, it is probably best to rebuild your OpenSSL libraries locally with debugging enabled (if you have not already done so).
Upvotes: 2