Author: sgriepentrog
Date: Tue Mar 17 17:03:37 2015
New Revision: 433063

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=433063
Log:
core: Introduce chaos into memory allocations

Locate potential crashes by exercising seldom
used code paths.  This patch introduces a new
define DEBUG_CHAOS, and mechanism to randomly
return an error condition from functions that
will seldom do so.  Functions that handle the
allocation of memory get the first treatment.

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

Merged revisions 433060 from http://svn.asterisk.org/svn/asterisk/branches/13

Modified:
    trunk/   (props changed)
    trunk/build_tools/cflags.xml
    trunk/include/asterisk/utils.h

Propchange: trunk/
------------------------------------------------------------------------------
Binary property 'branch-13-merged' - no diff available.

Modified: trunk/build_tools/cflags.xml
URL: 
http://svnview.digium.com/svn/asterisk/trunk/build_tools/cflags.xml?view=diff&rev=433063&r1=433062&r2=433063
==============================================================================
--- trunk/build_tools/cflags.xml (original)
+++ trunk/build_tools/cflags.xml Tue Mar 17 17:03:37 2015
@@ -79,6 +79,10 @@
                <member name="MALLOC_DEBUG" displayname="Keep Track of Memory 
Allocations">
                        <support_level>core</support_level>
                </member>
+               <member name="DEBUG_CHAOS" displayname="Randomly FAIL memory 
allocations or other operations">
+                       <conflict>MALLOC_DEBUG</conflict>
+                       <support_level>core</support_level>
+               </member>
                <member name="BUSYDETECT_TONEONLY" displayname="Enable 
additional comparision of only the tone duration not the silence part">
                        <conflict>BUSYDETECT_COMPARE_TONE_AND_SILENCE</conflict>
                        <defaultenabled>no</defaultenabled>

Modified: trunk/include/asterisk/utils.h
URL: 
http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/utils.h?view=diff&rev=433063&r1=433062&r2=433063
==============================================================================
--- trunk/include/asterisk/utils.h (original)
+++ trunk/include/asterisk/utils.h Tue Mar 17 17:03:37 2015
@@ -485,6 +485,32 @@
  */
 #define ast_random_double() (((double)ast_random()) / RAND_MAX)
 
+/*!
+ * \brief DEBUG_CHAOS returns failure randomly
+ *
+ * DEBUG_CHAOS_RETURN(failure); can be used to fake
+ * failure of functions such as memory allocation,
+ * for the purposes of testing failure handling.
+ */
+#ifdef DEBUG_CHAOS
+#ifndef DEBUG_CHAOS_ALLOC_CHANCE
+#define DEBUG_CHAOS_ALLOC_CHANCE 100000
+#endif
+/* Could #define DEBUG_CHAOS_ENABLE ast_fully_booted */
+#ifndef DEBUG_CHAOS_ENABLE
+#define DEBUG_CHAOS_ENABLE 1
+#endif
+#define DEBUG_CHAOS_RETURN(CHANCE, FAILURE) \
+       do { \
+               if ((DEBUG_CHAOS_ENABLE) && (ast_random() % CHANCE == 0)) { \
+                       return FAILURE; \
+               } \
+       } while (0)
+#else
+#define DEBUG_CHAOS_RETURN(c,f)
+#endif
+
+
 #ifndef __AST_DEBUG_MALLOC
 #define ast_std_malloc malloc
 #define ast_std_calloc calloc
@@ -537,6 +563,8 @@
 {
        void *p;
 
+       DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, NULL);
+
        if (!(p = malloc(len))) {
                MALLOC_FAILURE_MSG;
        }
@@ -561,6 +589,8 @@
 {
        void *p;
 
+       DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, NULL);
+
        if (!(p = calloc(num, len))) {
                MALLOC_FAILURE_MSG;
        }
@@ -598,6 +628,8 @@
 {
        void *newp;
 
+       DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, NULL);
+
        if (!(newp = realloc(p, len))) {
                MALLOC_FAILURE_MSG;
        }
@@ -625,6 +657,8 @@
 char * attribute_malloc _ast_strdup(const char *str, const char *file, int 
lineno, const char *func),
 {
        char *newstr = NULL;
+
+       DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, NULL);
 
        if (str) {
                if (!(newstr = strdup(str))) {
@@ -656,6 +690,8 @@
 {
        char *newstr = NULL;
 
+       DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, NULL);
+
        if (str) {
                if (!(newstr = strndup(str, len))) {
                        MALLOC_FAILURE_MSG;
@@ -696,6 +732,8 @@
 int _ast_vasprintf(char **ret, const char *file, int lineno, const char *func, 
const char *fmt, va_list ap),
 {
        int res;
+
+       DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, -1);
 
        if ((res = vasprintf(ret, fmt, ap)) == -1) {
                MALLOC_FAILURE_MSG;


-- 
_____________________________________________________________________
-- Bandwidth and Colocation Provided by http://www.api-digital.com --

svn-commits mailing list
To UNSUBSCRIBE or update options visit:
   http://lists.digium.com/mailman/listinfo/svn-commits

Reply via email to