Malloc detect

Malloc Detect provides an optional error reporting and detection extension to the malloc subsystem. Information on errors that occurred in the malloc environment will be reported and actions can be performed, if specified.

Malloc Detect can be divided into three distinct capabilities:
  • Detecting an error
  • Reporting the error using stderr
  • Reporting the error using an application-defined function

Detecting an error

Some errors in the malloc subsystem are easy to detect. Errors such as freeing a pointer which is not a valid heap address are detected synchronously in the codepath of free. However, errors caused by asynchronous events, such as heap corruption, are much more difficult to pinpoint. The check_arena option of Malloc Detect is designed to check for this type of corruption on a synchronous basis. On every call to a malloc subsystem API, a check of the internal data structures is performed. If corruption is found, it is then reported to the application. This provides another point in time with which to debug these difficult problems. The check_arena option can be enabled by setting the MALLOCDEBUG environment variable as follows:
MALLOCDEBUG=checkarena

Reporting an error using stderr

The normal way to report errors in the malloc subsystem is through the return value and the errno environment variable. The verbose option of Malloc Detect allows these errors to be printed to the standard error stream of the application program. This provides a more visible, more detailed way of reporting errors in malloc. The verbose option can be enabled by setting the MALLOCDEBUG environment variable as follows:
MALLOCDEBUG=verbose

|Reporting an error using an application-defined function

Malloc Detect also allows the user to provide a function that the malloc subsystem will call whenever it encounters an error. Malloc Report will call the application-provided function, wait for a return, and then return as usual. This facility allows the application to perform whatever necessary debug tasks are required before allowing the program to continue. To enable this facility, the application must set the value of a global function pointer malloc_err_function equal to the value of the application's error routine. For example:
extern void (*malloc_err_function)(int, ...)
malloc_err_function = &application_malloc_err_hdl

Limitations

The Malloc Detect debugging feature is compatible with the following malloc policies and options: