diff options
author | Walter Doekes <walter+asterisk@wjd.nu> | 2013-08-16 07:18:51 +0000 |
---|---|---|
committer | Walter Doekes <walter+asterisk@wjd.nu> | 2013-08-16 07:18:51 +0000 |
commit | c43e19e8e5fc5bc133f2ce3eeafbf9ce07048bd1 (patch) | |
tree | 3109874227e570a721bdd7f670a3f43a8ee39b4f /include/asterisk/utils.h | |
parent | 812355db8282eac83ddcd1969aea5bdd334e63a8 (diff) |
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
Diffstat (limited to 'include/asterisk/utils.h')
-rw-r--r-- | include/asterisk/utils.h | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h index 184850905..599beba54 100644 --- a/include/asterisk/utils.h +++ b/include/asterisk/utils.h @@ -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() * |