25#include "dbus-memory.h"
26#include "dbus-internals.h"
27#include "dbus-sysdeps.h"
29#include "dbus-threads.h"
30#include <dbus/dbus-test-tap.h>
101#ifdef DBUS_ENABLE_EMBEDDED_TESTS
104static int fail_nth = -1;
105static size_t fail_size = 0;
107static int n_failures_per_failure = 1;
108static int n_failures_this_failure = 0;
116#define GUARD_VALUE 0xdeadbeef
118#define GUARD_INFO_SIZE 8
120#define GUARD_START_PAD 16
122#define GUARD_END_PAD 16
124#define GUARD_START_OFFSET (GUARD_START_PAD + GUARD_INFO_SIZE)
126#define GUARD_EXTRA_SIZE (GUARD_START_OFFSET + GUARD_END_PAD)
129_dbus_initialize_malloc_debug (
void)
131 if (!debug_initialized)
133 debug_initialized =
TRUE;
137 fail_nth = atoi (
_dbus_getenv (
"DBUS_MALLOC_FAIL_NTH"));
138 fail_alloc_counter = fail_nth;
139 _dbus_verbose (
"Will fail dbus_malloc every %d times\n", fail_nth);
144 fail_size = atoi (
_dbus_getenv (
"DBUS_MALLOC_FAIL_GREATER_THAN"));
145 _dbus_verbose (
"Will fail mallocs over %ld bytes\n",
152 _dbus_verbose (
"Will use dbus_malloc guards\n");
157 disable_mem_pools =
TRUE;
158 _dbus_verbose (
"Will disable memory pools\n");
163 backtrace_on_fail_alloc =
TRUE;
164 _dbus_verbose (
"Will backtrace on failing a dbus_malloc\n");
169 malloc_cannot_fail =
TRUE;
170 _dbus_verbose (
"Will abort if system malloc() and friends fail\n");
181_dbus_disable_mem_pools (
void)
183 _dbus_initialize_malloc_debug ();
184 return disable_mem_pools;
196_dbus_set_fail_alloc_counter (
int until_next_fail)
198 _dbus_initialize_malloc_debug ();
200 fail_alloc_counter = until_next_fail;
203 _dbus_verbose (
"Set fail alloc counter = %d\n", fail_alloc_counter);
214_dbus_get_fail_alloc_counter (
void)
216 _dbus_initialize_malloc_debug ();
218 return fail_alloc_counter;
228_dbus_set_fail_alloc_failures (
int failures_per_failure)
230 n_failures_per_failure = failures_per_failure;
240_dbus_get_fail_alloc_failures (
void)
242 return n_failures_per_failure;
245#ifdef DBUS_ENABLE_EMBEDDED_TESTS
255_dbus_decrement_fail_alloc_counter (
void)
257 _dbus_initialize_malloc_debug ();
264 _dbus_verbose_raw (
"TODO: memory allocation testing errors disabled for now\n");
271 if (fail_alloc_counter <= 0)
273 if (backtrace_on_fail_alloc)
276 _dbus_verbose (
"failure %d\n", n_failures_this_failure);
278 n_failures_this_failure += 1;
279 if (n_failures_this_failure >= n_failures_per_failure)
282 fail_alloc_counter = fail_nth;
286 n_failures_this_failure = 0;
288 _dbus_verbose (
"reset fail alloc counter to %d\n", fail_alloc_counter);
295 fail_alloc_counter -= 1;
307_dbus_get_malloc_blocks_outstanding (
void)
325source_string (BlockSource source)
335 case SOURCE_MALLOC_ZERO:
337 case SOURCE_REALLOC_NULL:
338 return "realloc(NULL)";
346check_guards (
void *free_block,
349 if (free_block !=
NULL)
351 unsigned char *block = ((
unsigned char*)free_block) - GUARD_START_OFFSET;
352 size_t requested_bytes = *(dbus_uint32_t*)block;
353 BlockSource source = *(dbus_uint32_t*)(block + 4);
360 _dbus_verbose (
"Checking %d bytes request from source %s\n",
361 requested_bytes, source_string (source));
365 while (i < GUARD_START_OFFSET)
367 dbus_uint32_t value = *(dbus_uint32_t*) &block[i];
368 if (value != GUARD_VALUE)
370 _dbus_warn (
"Block of %lu bytes from %s had start guard value 0x%ux at %d expected 0x%x",
371 (
long) requested_bytes, source_string (source),
372 value, i, GUARD_VALUE);
379 i = GUARD_START_OFFSET + requested_bytes;
380 while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
382 dbus_uint32_t value = *(dbus_uint32_t*) &block[i];
383 if (value != GUARD_VALUE)
385 _dbus_warn (
"Block of %lu bytes from %s had end guard value 0x%ux at %d expected 0x%x",
386 (
long) requested_bytes, source_string (source),
387 value, i, GUARD_VALUE);
396 memset (free_block,
'g', requested_bytes);
404set_guards (
void *real_block,
405 size_t requested_bytes,
408 unsigned char *block = real_block;
414 _dbus_assert (GUARD_START_OFFSET + GUARD_END_PAD == GUARD_EXTRA_SIZE);
416 *((dbus_uint32_t*)block) = requested_bytes;
417 *((dbus_uint32_t*)(block + 4)) = source;
420 while (i < GUARD_START_OFFSET)
422 (*(dbus_uint32_t*) &block[i]) = GUARD_VALUE;
427 i = GUARD_START_OFFSET + requested_bytes;
428 while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
430 (*(dbus_uint32_t*) &block[i]) = GUARD_VALUE;
435 check_guards (block + GUARD_START_OFFSET,
FALSE);
437 return block + GUARD_START_OFFSET;
466#ifdef DBUS_ENABLE_EMBEDDED_TESTS
467 _dbus_initialize_malloc_debug ();
469 if (_dbus_decrement_fail_alloc_counter ())
471 _dbus_verbose (
" FAILING malloc of %ld bytes\n", (
long) bytes);
478#ifdef DBUS_ENABLE_EMBEDDED_TESTS
479 else if (fail_size != 0 && bytes > fail_size)
485 block = malloc (bytes + GUARD_EXTRA_SIZE);
490 else if (malloc_cannot_fail)
492 _dbus_warn (
"out of memory: malloc (%ld + %ld)",
493 (
long) bytes, (
long) GUARD_EXTRA_SIZE);
497 return set_guards (block, bytes, SOURCE_MALLOC);
503 mem = malloc (bytes);
505#ifdef DBUS_ENABLE_EMBEDDED_TESTS
510 else if (malloc_cannot_fail)
512 _dbus_warn (
"out of memory: malloc (%ld)", (
long) bytes);
536#ifdef DBUS_ENABLE_EMBEDDED_TESTS
537 _dbus_initialize_malloc_debug ();
539 if (_dbus_decrement_fail_alloc_counter ())
541 _dbus_verbose (
" FAILING malloc0 of %ld bytes\n", (
long) bytes);
549#ifdef DBUS_ENABLE_EMBEDDED_TESTS
550 else if (fail_size != 0 && bytes > fail_size)
556 block = calloc (bytes + GUARD_EXTRA_SIZE, 1);
562 else if (malloc_cannot_fail)
564 _dbus_warn (
"out of memory: calloc (%ld + %ld, 1)",
565 (
long) bytes, (
long) GUARD_EXTRA_SIZE);
569 return set_guards (block, bytes, SOURCE_MALLOC_ZERO);
575 mem = calloc (bytes, 1);
577#ifdef DBUS_ENABLE_EMBEDDED_TESTS
582 else if (malloc_cannot_fail)
584 _dbus_warn (
"out of memory: calloc (%ld)", (
long) bytes);
607#ifdef DBUS_ENABLE_EMBEDDED_TESTS
608 _dbus_initialize_malloc_debug ();
610 if (_dbus_decrement_fail_alloc_counter ())
612 _dbus_verbose (
" FAILING realloc of %ld bytes\n", (
long) bytes);
623#ifdef DBUS_ENABLE_EMBEDDED_TESTS
624 else if (fail_size != 0 && bytes > fail_size)
633 check_guards (memory,
FALSE);
635 block = realloc (((
unsigned char*)memory) - GUARD_START_OFFSET,
636 bytes + GUARD_EXTRA_SIZE);
640 if (malloc_cannot_fail)
642 _dbus_warn (
"out of memory: realloc (%p, %ld + %ld)",
643 memory, (
long) bytes, (
long) GUARD_EXTRA_SIZE);
650 old_bytes = *(dbus_uint32_t*)block;
651 if (bytes >= old_bytes)
653 check_guards (((
unsigned char*)block) + GUARD_START_OFFSET,
FALSE);
655 return set_guards (block, bytes, SOURCE_REALLOC);
661 block = malloc (bytes + GUARD_EXTRA_SIZE);
667 else if (malloc_cannot_fail)
669 _dbus_warn (
"out of memory: malloc (%ld + %ld)",
670 (
long) bytes, (
long) GUARD_EXTRA_SIZE);
674 return set_guards (block, bytes, SOURCE_REALLOC_NULL);
681 mem = realloc (memory, bytes);
683#ifdef DBUS_ENABLE_EMBEDDED_TESTS
684 if (mem ==
NULL && malloc_cannot_fail)
686 _dbus_warn (
"out of memory: malloc (%ld)", (
long) bytes);
706#ifdef DBUS_ENABLE_EMBEDDED_TESTS
709 check_guards (memory,
TRUE);
712#ifdef DBUS_DISABLE_ASSERT
715 dbus_int32_t old_value;
721 free (((
unsigned char*)memory) - GUARD_START_OFFSET);
730#ifdef DBUS_ENABLE_EMBEDDED_TESTS
731#ifdef DBUS_DISABLE_ASSERT
734 dbus_int32_t old_value;
821 ok = _dbus_register_shutdown_func_unlocked (func, data);
827_dbus_register_shutdown_func_unlocked (DBusShutdownFunction func,
840 c->
next = registered_globals;
841 registered_globals = c;
902 while (registered_globals !=
NULL)
906 c = registered_globals;
907 registered_globals = c->
next;
925#ifdef DBUS_ENABLE_EMBEDDED_TESTS
926#include "dbus-test.h"
934_dbus_memory_test (
const char *test_data_dir _DBUS_GNUC_UNUSED)
944 _dbus_test_fatal (
"no memory");
945 for (size = 4; size < 256; size += 4)
949 _dbus_test_fatal (
"no memory");
951 for (size = 256; size != 0; size -= 4)
955 _dbus_test_fatal (
"no memory");
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define _DBUS_UNLOCK(name)
Unlocks a global lock.
#define _DBUS_LOCK(name)
Locks a global lock, initializing it first if necessary.
#define _DBUS_INT_MAX
Maximum value of type "int".
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
int _dbus_current_generation
_dbus_current_generation is used to track each time that dbus_shutdown() is called,...
dbus_bool_t _dbus_register_shutdown_func(DBusShutdownFunction func, void *data)
Register a cleanup function to be called exactly once the next time dbus_shutdown() is called.
void dbus_shutdown(void)
Frees all memory allocated internally by libdbus and reverses the effects of dbus_threads_init().
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
void * dbus_realloc(void *memory, size_t bytes)
Resizes a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
void * dbus_malloc0(size_t bytes)
Allocates the given number of bytes, as with standard malloc(), but all bytes are initialized to zero...
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
dbus_int32_t _dbus_atomic_dec(DBusAtomic *atomic)
Atomically decrement an integer.
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
dbus_int32_t _dbus_atomic_get(DBusAtomic *atomic)
Atomically get the value of an integer.
void _dbus_threads_lock_platform_specific(void)
Lock a static mutex used to protect _dbus_threads_init_platform_specific().
void _dbus_threads_unlock_platform_specific(void)
Undo _dbus_threads_lock_platform_specific().
dbus_int32_t _dbus_atomic_inc(DBusAtomic *atomic)
Atomically increments an integer.
void _dbus_abort(void)
Aborts the program with SIGABRT (dumping core).
void _dbus_print_backtrace(void)
On GNU libc systems, print a crude backtrace to stderr.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
An atomic integer safe to increment or decrement from multiple threads.
This struct represents a function to be called on shutdown.
ShutdownClosure * next
Next ShutdownClosure.
DBusShutdownFunction func
Function to call.
void * data
Data for function.