Nvwa  1.1
Classes | Namespaces | Macros | Functions | Variables
debug_new.cpp File Reference

Implementation of debug versions of new and delete to check leakage. More...

#include <new>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "_nvwa.h"
#include "c++11.h"
#include "fast_mutex.h"
#include "static_assert.h"
#include "debug_new.h"
Include dependency graph for debug_new.cpp:

Classes

struct  nvwa::new_ptr_list_t
 Structure to store the position information where new occurs. More...
 

Namespaces

 nvwa
 Namespace of the nvwa project.
 

Macros

#define _DEBUG_NEW_REDEFINE_NEW   0
 Macro to indicate whether redefinition of new is wanted. More...
 
#define _DEBUG_NEW_ALIGNMENT   16
 The alignment requirement of allocated memory blocks. More...
 
#define _DEBUG_NEW_CALLER_ADDRESS   __builtin_return_address(0)
 The expression to return the caller address. More...
 
#define _DEBUG_NEW_ERROR_ACTION   abort()
 The action to take when an error occurs. More...
 
#define _DEBUG_NEW_FILENAME_LEN   44
 The length of file name stored if greater than zero. More...
 
#define _DEBUG_NEW_PROGNAME   _NULLPTR
 The program (executable) name to be set at compile time. More...
 
#define _DEBUG_NEW_STD_OPER_NEW   1
 Macro to indicate whether the standard-conformant behaviour of operator new is wanted. More...
 
#define _DEBUG_NEW_TAILCHECK   0
 Macro to indicate whether a writing-past-end check will be performed. More...
 
#define _DEBUG_NEW_TAILCHECK_CHAR   0xCC
 Value of the padding bytes at the end of a memory block.
 
#define _DEBUG_NEW_USE_ADDR2LINE   1
 Whether to use addr2line to convert a caller address to file/line information. More...
 
#define ALIGN(s)   (((s) + _DEBUG_NEW_ALIGNMENT - 1) & ~(_DEBUG_NEW_ALIGNMENT - 1))
 Gets the aligned value of memory block size.
 
#define _DEBUG_NEW_REMEMBER_STACK_TRACE   0
 Macro to indicate whether stack traces of allocations should be included in NVWA allocation information and be printed while leaks are reported. More...
 

Functions

static bool nvwa::print_position_from_addr (const void *addr)
 Tries printing the position information from an instruction address. More...
 
static void nvwa::print_position (const void *ptr, int line)
 Prints the position information of a memory operation point. More...
 
static void nvwa::print_stacktrace (void **stacktrace)
 Prints the stack backtrace. More...
 
static bool nvwa::is_leak_whitelisted (new_ptr_list_t *ptr)
 Checks whether a leak should be ignored. More...
 
static void * nvwa::alloc_mem (size_t size, const char *file, int line, bool is_array)
 Allocates memory and initializes control data. More...
 
static void nvwa::free_pointer (void *usr_ptr, void *addr, bool is_array)
 Frees memory and adjusts pointers. More...
 
int nvwa::check_leaks ()
 Checks for memory leaks. More...
 
int nvwa::check_mem_corruption ()
 Checks for heap corruption. More...
 
void * operator new (size_t size, const char *file, int line)
 Allocates memory with file/line information. More...
 
void * operator new[] (size_t size, const char *file, int line)
 Allocates array memory with file/line information. More...
 
void * operator new (size_t size) throw (std::bad_alloc)
 Allocates memory without file/line information. More...
 
void * operator new[] (size_t size) throw (std::bad_alloc)
 Allocates array memory without file/line information. More...
 
void * operator new (size_t size, const std::nothrow_t &) noexcept
 Allocates memory with no-throw guarantee. More...
 
void * operator new[] (size_t size, const std::nothrow_t &) noexcept
 Allocates array memory with no-throw guarantee. More...
 
void operator delete (void *ptr) noexcept
 Deallocates memory. More...
 
void operator delete[] (void *ptr) noexcept
 Deallocates array memory. More...
 
void operator delete (void *ptr, const char *file, int line) noexcept
 Placement deallocation function. More...
 
void operator delete[] (void *ptr, const char *file, int line) noexcept
 Placement deallocation function. More...
 
void operator delete (void *ptr, const std::nothrow_t &) noexcept
 Placement deallocation function. More...
 
void operator delete[] (void *ptr, const std::nothrow_t &) noexcept
 Placement deallocation function. More...
 

Variables

const size_t nvwa::PLATFORM_MEM_ALIGNMENT = sizeof(size_t) * 2
 The platform memory alignment. More...
 
static const unsigned nvwa::DEBUG_NEW_MAGIC = 0x4442474E
 Definition of the constant magic number used for error detection.
 
static const int nvwa::ALIGNED_LIST_ITEM_SIZE = ALIGN(sizeof(new_ptr_list_t))
 The extra memory allocated by operator new.
 
static new_ptr_list_t nvwa::new_ptr_list
 List of all new'd pointers.
 
static fast_mutex nvwa::new_ptr_lock
 The mutex guard to protect simultaneous access to the pointer list.
 
static fast_mutex nvwa::new_output_lock
 The mutex guard to protect simultaneous output to new_output_fp.
 
static size_t nvwa::total_mem_alloc = 0
 Total memory allocated in bytes.
 

Detailed Description

Implementation of debug versions of new and delete to check leakage.

Date
2016-10-14

Macro Definition Documentation

◆ _DEBUG_NEW_ALIGNMENT

#define _DEBUG_NEW_ALIGNMENT   16

The alignment requirement of allocated memory blocks.

It must be a power of two.

◆ _DEBUG_NEW_CALLER_ADDRESS

#define _DEBUG_NEW_CALLER_ADDRESS   __builtin_return_address(0)

The expression to return the caller address.

nvwa::print_position will later on use this address to print the position information of memory operation points.

◆ _DEBUG_NEW_ERROR_ACTION

#define _DEBUG_NEW_ERROR_ACTION   abort()

The action to take when an error occurs.

The default behaviour is to call abort, unless _DEBUG_NEW_ERROR_CRASH is defined, in which case a segmentation fault will be triggered instead (which can be useful on platforms like Windows that do not generate a core dump when abort is called).

◆ _DEBUG_NEW_FILENAME_LEN

#define _DEBUG_NEW_FILENAME_LEN   44

The length of file name stored if greater than zero.

If it is zero, only a const char pointer will be stored. Currently the default value is non-zero (thus to copy the file name) on non-Windows platforms, because I once found that the exit leakage check could not access the address of the file name on Linux (in my case, a core dump occurred when check_leaks tried to access the file name in a shared library after a SIGINT). This value makes the size of new_ptr_list_t 64 on non-Windows 32-bit platforms (w/o stack backtrace).

◆ _DEBUG_NEW_PROGNAME

#define _DEBUG_NEW_PROGNAME   _NULLPTR

The program (executable) name to be set at compile time.

It is better to assign the full program path to nvwa::new_progname in main (at run time) than to use this (compile-time) macro, but this macro serves well as a quick hack. Note also that double quotation marks need to be used around the program name, i.e., one should specify a command-line option like -D_DEBUG_NEW_PROGNAME=\"a.out\" in bash, or -D_DEBUG_NEW_PROGNAME=\"a.exe\" in the Windows command prompt.

◆ _DEBUG_NEW_REDEFINE_NEW

#define _DEBUG_NEW_REDEFINE_NEW   0

Macro to indicate whether redefinition of new is wanted.

If one wants to define one's own operator new, or to call operator new directly, it should be defined to 0 to alter the default behaviour. Unless, of course, one is willing to take the trouble to write something like:

# ifdef new
# define _NEW_REDEFINED
# undef new
# endif
// Code that uses new is here
# ifdef _NEW_REDEFINED
# ifdef DEBUG_NEW
# define new DEBUG_NEW
# endif
# undef _NEW_REDEFINED
# endif

Here it is defined to 0 to disable the redefinition of new.

◆ _DEBUG_NEW_REMEMBER_STACK_TRACE

#define _DEBUG_NEW_REMEMBER_STACK_TRACE   0

Macro to indicate whether stack traces of allocations should be included in NVWA allocation information and be printed while leaks are reported.

Useful to pinpoint leaks caused by strdup and other custom allocation functions. It is also very helpful in filtering out false positives caused by internal STL/C runtime operations. It is off by default because it is quite memory heavy. Set it to 1 to make all allocations have stack trace attached, or set to 2 to make only allocations that lack calling source code information (file and line) have the stack trace attached.

◆ _DEBUG_NEW_STD_OPER_NEW

#define _DEBUG_NEW_STD_OPER_NEW   1

Macro to indicate whether the standard-conformant behaviour of operator new is wanted.

It is on by default now, but the user may set it to 0 to revert to the old behaviour.

◆ _DEBUG_NEW_TAILCHECK

#define _DEBUG_NEW_TAILCHECK   0

Macro to indicate whether a writing-past-end check will be performed.

Define it to a positive integer as the number of padding bytes at the end of a memory block for checking.

◆ _DEBUG_NEW_USE_ADDR2LINE

#define _DEBUG_NEW_USE_ADDR2LINE   1

Whether to use addr2line to convert a caller address to file/line information.

Defining it to a non-zero value will enable the conversion (automatically done if GCC is detected). Defining it to zero will disable the conversion.

Function Documentation

◆ operator delete() [1/3]

void operator delete ( void *  ptr)
noexcept

Deallocates memory.

Parameters
ptrpointer to the previously allocated memory

◆ operator delete() [2/3]

void operator delete ( void *  ptr,
const char *  file,
int  line 
)
noexcept

Placement deallocation function.

For details, please check Section 5.3.4 of the C++ 1998 or 2011 Standard.

Parameters
ptrpointer to the previously allocated memory
filenull-terminated string of the file name
lineline number
See also
http://www.csci.csusb.edu/dick/c++std/cd2/expr.html#expr.new
http://wyw.dcweb.cn/leakage.htm

◆ operator delete() [3/3]

void operator delete ( void *  ptr,
const std::nothrow_t &   
)
noexcept

Placement deallocation function.

For details, please check Section 5.3.4 of the C++ 1998 or 2011 Standard.

Parameters
ptrpointer to the previously allocated memory

◆ operator delete[]() [1/3]

void operator delete[] ( void *  ptr)
noexcept

Deallocates array memory.

Parameters
ptrpointer to the previously allocated memory

◆ operator delete[]() [2/3]

void operator delete[] ( void *  ptr,
const char *  file,
int  line 
)
noexcept

Placement deallocation function.

For details, please check Section 5.3.4 of the C++ 1998 or 2011 Standard.

Parameters
ptrpointer to the previously allocated memory
filenull-terminated string of the file name
lineline number

◆ operator delete[]() [3/3]

void operator delete[] ( void *  ptr,
const std::nothrow_t &   
)
noexcept

Placement deallocation function.

For details, please check Section 5.3.4 of the C++ 1998 or 2011 Standard.

Parameters
ptrpointer to the previously allocated memory

◆ operator new() [1/3]

void* operator new ( size_t  size,
const char *  file,
int  line 
)

Allocates memory with file/line information.

Parameters
sizesize of the required memory block
filenull-terminated string of the file name
lineline number
Returns
pointer to the memory allocated; or null if memory is insufficient (_DEBUG_NEW_STD_OPER_NEW is 0)
Exceptions
bad_allocmemory is insufficient (_DEBUG_NEW_STD_OPER_NEW is 1)

◆ operator new() [2/3]

void* operator new ( size_t  size)
throw (std::bad_alloc
)

Allocates memory without file/line information.

Parameters
sizesize of the required memory block
Returns
pointer to the memory allocated; or null if memory is insufficient (_DEBUG_NEW_STD_OPER_NEW is 0)
Exceptions
bad_allocmemory is insufficient (_DEBUG_NEW_STD_OPER_NEW is 1)

◆ operator new() [3/3]

void* operator new ( size_t  size,
const std::nothrow_t &   
)
noexcept

Allocates memory with no-throw guarantee.

Parameters
sizesize of the required memory block
Returns
pointer to the memory allocated; or null if memory is insufficient

◆ operator new[]() [1/3]

void* operator new[] ( size_t  size,
const char *  file,
int  line 
)

Allocates array memory with file/line information.

Parameters
sizesize of the required memory block
filenull-terminated string of the file name
lineline number
Returns
pointer to the memory allocated; or null if memory is insufficient (_DEBUG_NEW_STD_OPER_NEW is 0)
Exceptions
bad_allocmemory is insufficient (_DEBUG_NEW_STD_OPER_NEW is 1)

◆ operator new[]() [2/3]

void* operator new[] ( size_t  size)
throw (std::bad_alloc
)

Allocates array memory without file/line information.

Parameters
sizesize of the required memory block
Returns
pointer to the memory allocated; or null if memory is insufficient (_DEBUG_NEW_STD_OPER_NEW is 0)
Exceptions
bad_allocmemory is insufficient (_DEBUG_NEW_STD_OPER_NEW is 1)

◆ operator new[]() [3/3]

void* operator new[] ( size_t  size,
const std::nothrow_t &   
)
noexcept

Allocates array memory with no-throw guarantee.

Parameters
sizesize of the required memory block
Returns
pointer to the memory allocated; or null if memory is insufficient