Prevent heap alloc functions from running out of stack space.

When asterisk has run out of memory (for whatever reason), the alloc
function logs a message. Logging requires memory. A recipe for
infinite recursion.

Stop the recursion by comparing the function call depth for sane values
before attempting another OOM log message.

Review: https://reviewboard.asterisk.org/r/2743/


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@396822 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Walter Doekes
2013-08-16 07:18:51 +00:00
parent 812355db82
commit c43e19e8e5
3 changed files with 98 additions and 1 deletions

View File

@@ -25,6 +25,7 @@
#include "asterisk/network.h"
#include <execinfo.h>
#include <time.h> /* we want to override localtime_r */
#include <unistd.h>
#include <string.h>
@@ -483,8 +484,24 @@ static void ast_free_ptr(void *ptr)
#ifndef __AST_DEBUG_MALLOC
/* This buffer is in static memory. We never intend to read it,
* nor do we care about multiple threads writing to it at the
* same time. We only want to know if we're recursing too deep
* already. 60 entries should be more than enough. Function call
* depth rarely exceeds 20 or so. */
#define _AST_MEM_BACKTRACE_BUFLEN 60
extern void *_ast_mem_backtrace_buffer[_AST_MEM_BACKTRACE_BUFLEN];
#define MALLOC_FAILURE_MSG \
ast_log(LOG_ERROR, "Memory Allocation Failure in function %s at line %d of %s\n", func, lineno, file);
{ \
/* Ok, this sucks. But if we're already out of mem, we don't \
* want the logger to create infinite recursion (and a crash). */ \
if (backtrace(_ast_mem_backtrace_buffer, _AST_MEM_BACKTRACE_BUFLEN) < \
_AST_MEM_BACKTRACE_BUFLEN) { \
ast_log(LOG_ERROR, "Memory Allocation Failure in function %s at line %d of %s\n", func, lineno, file); \
} \
}
/*!
* \brief A wrapper for malloc()
*