25 #include "dbus-memory.h" 
   26 #include "dbus-internals.h" 
   27 #include "dbus-sysdeps.h" 
   28 #include "dbus-list.h" 
   29 #include "dbus-threads.h" 
  100 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 
  102 static int fail_nth = -1;
 
  103 static size_t fail_size = 0;
 
  105 static int n_failures_per_failure = 1;
 
  106 static int n_failures_this_failure = 0;
 
  114 #define GUARD_VALUE 0xdeadbeef 
  116 #define GUARD_INFO_SIZE 8 
  118 #define GUARD_START_PAD 16 
  120 #define GUARD_END_PAD 16 
  122 #define GUARD_START_OFFSET (GUARD_START_PAD + GUARD_INFO_SIZE) 
  124 #define GUARD_EXTRA_SIZE (GUARD_START_OFFSET + GUARD_END_PAD) 
  127 _dbus_initialize_malloc_debug (
void)
 
  129   if (!debug_initialized)
 
  131       debug_initialized = 
TRUE;
 
  135           fail_nth = atoi (
_dbus_getenv (
"DBUS_MALLOC_FAIL_NTH"));
 
  136           fail_alloc_counter = fail_nth;
 
  137           _dbus_verbose (
"Will fail dbus_malloc every %d times\n", fail_nth);
 
  142           fail_size = atoi (
_dbus_getenv (
"DBUS_MALLOC_FAIL_GREATER_THAN"));
 
  143           _dbus_verbose (
"Will fail mallocs over %ld bytes\n",
 
  150           _dbus_verbose (
"Will use dbus_malloc guards\n");
 
  155           disable_mem_pools = 
TRUE;
 
  156           _dbus_verbose (
"Will disable memory pools\n");
 
  161           backtrace_on_fail_alloc = 
TRUE;
 
  162           _dbus_verbose (
"Will backtrace on failing a dbus_malloc\n");
 
  167           malloc_cannot_fail = 
TRUE;
 
  168           _dbus_verbose (
"Will abort if system malloc() and friends fail\n");
 
  179 _dbus_disable_mem_pools (
void)
 
  181   _dbus_initialize_malloc_debug ();
 
  182   return disable_mem_pools;
 
  194 _dbus_set_fail_alloc_counter (
int until_next_fail)
 
  196   _dbus_initialize_malloc_debug ();
 
  198   fail_alloc_counter = until_next_fail;
 
  201   _dbus_verbose (
"Set fail alloc counter = %d\n", fail_alloc_counter);
 
  212 _dbus_get_fail_alloc_counter (
void)
 
  214   _dbus_initialize_malloc_debug ();
 
  216   return fail_alloc_counter;
 
  226 _dbus_set_fail_alloc_failures (
int failures_per_failure)
 
  228   n_failures_per_failure = failures_per_failure;
 
  238 _dbus_get_fail_alloc_failures (
void)
 
  240   return n_failures_per_failure;
 
  243 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 
  253 _dbus_decrement_fail_alloc_counter (
void)
 
  255   _dbus_initialize_malloc_debug ();
 
  256 #ifdef DBUS_WIN_FIXME 
  262         _dbus_verbose(
"TODO: memory allocation testing errors disabled for now\n");
 
  269   if (fail_alloc_counter <= 0)
 
  271       if (backtrace_on_fail_alloc)
 
  274       _dbus_verbose (
"failure %d\n", n_failures_this_failure);
 
  276       n_failures_this_failure += 1;
 
  277       if (n_failures_this_failure >= n_failures_per_failure)
 
  280             fail_alloc_counter = fail_nth;
 
  284           n_failures_this_failure = 0;
 
  286           _dbus_verbose (
"reset fail alloc counter to %d\n", fail_alloc_counter);
 
  293       fail_alloc_counter -= 1;
 
  305 _dbus_get_malloc_blocks_outstanding (
void)
 
  323 source_string (BlockSource source)
 
  333     case SOURCE_MALLOC_ZERO:
 
  335     case SOURCE_REALLOC_NULL:
 
  336       return "realloc(NULL)";
 
  344 check_guards (
void       *free_block,
 
  347   if (free_block != 
NULL)
 
  349       unsigned char *block = ((
unsigned char*)free_block) - GUARD_START_OFFSET;
 
  358       _dbus_verbose (
"Checking %d bytes request from source %s\n",
 
  359                      requested_bytes, source_string (source));
 
  363       while (i < GUARD_START_OFFSET)
 
  366           if (value != GUARD_VALUE)
 
  368               _dbus_warn (
"Block of %lu bytes from %s had start guard value 0x%ux at %d expected 0x%x",
 
  369                           (
long) requested_bytes, source_string (source),
 
  370                           value, i, GUARD_VALUE);
 
  377       i = GUARD_START_OFFSET + requested_bytes;
 
  378       while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
 
  381           if (value != GUARD_VALUE)
 
  383               _dbus_warn (
"Block of %lu bytes from %s had end guard value 0x%ux at %d expected 0x%x",
 
  384                           (
long) requested_bytes, source_string (source),
 
  385                           value, i, GUARD_VALUE);
 
  394         memset (free_block, 
'g', requested_bytes);
 
  402 set_guards (
void       *real_block,
 
  403             size_t      requested_bytes,
 
  406   unsigned char *block = real_block;
 
  412   _dbus_assert (GUARD_START_OFFSET + GUARD_END_PAD == GUARD_EXTRA_SIZE);
 
  418   while (i < GUARD_START_OFFSET)
 
  425   i = GUARD_START_OFFSET + requested_bytes;
 
  426   while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
 
  433   check_guards (block + GUARD_START_OFFSET, 
FALSE);
 
  435   return block + GUARD_START_OFFSET;
 
  464 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 
  465   _dbus_initialize_malloc_debug ();
 
  467   if (_dbus_decrement_fail_alloc_counter ())
 
  469       _dbus_verbose (
" FAILING malloc of %ld bytes\n", (
long) bytes);
 
  476 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 
  477   else if (fail_size != 0 && bytes > fail_size)
 
  483       block = malloc (bytes + GUARD_EXTRA_SIZE);
 
  488       else if (malloc_cannot_fail)
 
  490           _dbus_warn (
"out of memory: malloc (%ld + %ld)",
 
  491               (
long) bytes, (
long) GUARD_EXTRA_SIZE);
 
  495       return set_guards (block, bytes, SOURCE_MALLOC);
 
  501       mem = malloc (bytes);
 
  503 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 
  508       else if (malloc_cannot_fail)
 
  510           _dbus_warn (
"out of memory: malloc (%ld)", (
long) bytes);
 
  534 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 
  535   _dbus_initialize_malloc_debug ();
 
  537   if (_dbus_decrement_fail_alloc_counter ())
 
  539       _dbus_verbose (
" FAILING malloc0 of %ld bytes\n", (
long) bytes);
 
  547 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 
  548   else if (fail_size != 0 && bytes > fail_size)
 
  554       block = calloc (bytes + GUARD_EXTRA_SIZE, 1);
 
  560       else if (malloc_cannot_fail)
 
  562           _dbus_warn (
"out of memory: calloc (%ld + %ld, 1)",
 
  563               (
long) bytes, (
long) GUARD_EXTRA_SIZE);
 
  567       return set_guards (block, bytes, SOURCE_MALLOC_ZERO);
 
  573       mem = calloc (bytes, 1);
 
  575 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 
  580       else if (malloc_cannot_fail)
 
  582           _dbus_warn (
"out of memory: calloc (%ld)", (
long) bytes);
 
  605 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 
  606   _dbus_initialize_malloc_debug ();
 
  608   if (_dbus_decrement_fail_alloc_counter ())
 
  610       _dbus_verbose (
" FAILING realloc of %ld bytes\n", (
long) bytes);
 
  621 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 
  622   else if (fail_size != 0 && bytes > fail_size)
 
  631           check_guards (memory, 
FALSE);
 
  633           block = realloc (((
unsigned char*)memory) - GUARD_START_OFFSET,
 
  634                            bytes + GUARD_EXTRA_SIZE);
 
  638               if (malloc_cannot_fail)
 
  640                   _dbus_warn (
"out of memory: realloc (%p, %ld + %ld)",
 
  641                       memory, (
long) bytes, (
long) GUARD_EXTRA_SIZE);
 
  649           if (bytes >= old_bytes)
 
  651             check_guards (((
unsigned char*)block) + GUARD_START_OFFSET, 
FALSE);
 
  653           return set_guards (block, bytes, SOURCE_REALLOC);
 
  659           block = malloc (bytes + GUARD_EXTRA_SIZE);
 
  665           else if (malloc_cannot_fail)
 
  667               _dbus_warn (
"out of memory: malloc (%ld + %ld)",
 
  668                   (
long) bytes, (
long) GUARD_EXTRA_SIZE);
 
  672           return set_guards (block, bytes, SOURCE_REALLOC_NULL);   
 
  679       mem = realloc (memory, bytes);
 
  681 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 
  682       if (mem == 
NULL && malloc_cannot_fail)
 
  684           _dbus_warn (
"out of memory: malloc (%ld)", (
long) bytes);
 
  704 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 
  707       check_guards (memory, 
TRUE);
 
  710 #ifdef DBUS_DISABLE_ASSERT 
  719           free (((
unsigned char*)memory) - GUARD_START_OFFSET);
 
  728 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 
  729 #ifdef DBUS_DISABLE_ASSERT 
  819   ok = _dbus_register_shutdown_func_unlocked (func, data);
 
  825 _dbus_register_shutdown_func_unlocked (DBusShutdownFunction  func,
 
  838   c->
next = registered_globals;
 
  839   registered_globals = c;
 
  900   while (registered_globals != 
NULL)
 
  904       c = registered_globals;
 
  905       registered_globals = c->
next;
 
  923 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 
  924 #include "dbus-test.h" 
  932 _dbus_memory_test (
void)
 
  943   for (size = 4; size < 256; size += 4)
 
  949   for (size = 256; size != 0; size -= 4)