27#include "dbus-memory.h"
28#include "dbus-internals.h"
29#include "dbus-sysdeps.h"
31#include "dbus-threads.h"
32#include <dbus/dbus-test-tap.h>
103#ifdef DBUS_ENABLE_EMBEDDED_TESTS
105static dbus_bool_t debug_initialized =
FALSE;
106static int fail_nth = -1;
107static size_t fail_size = 0;
108static int fail_alloc_counter = -1;
109static int n_failures_per_failure = 1;
110static int n_failures_this_failure = 0;
111static dbus_bool_t guards =
FALSE;
112static dbus_bool_t disable_mem_pools =
FALSE;
113static dbus_bool_t backtrace_on_fail_alloc =
FALSE;
114static dbus_bool_t malloc_cannot_fail =
FALSE;
118#define GUARD_VALUE 0xdeadbeef
120#define GUARD_INFO_SIZE 8
122#define GUARD_START_PAD 16
124#define GUARD_END_PAD 16
126#define GUARD_START_OFFSET (GUARD_START_PAD + GUARD_INFO_SIZE)
128#define GUARD_EXTRA_SIZE (GUARD_START_OFFSET + GUARD_END_PAD)
131_dbus_initialize_malloc_debug (
void)
133 if (!debug_initialized)
135 debug_initialized =
TRUE;
139 fail_nth = atoi (
_dbus_getenv (
"DBUS_MALLOC_FAIL_NTH"));
140 fail_alloc_counter = fail_nth;
141 _dbus_verbose (
"Will fail dbus_malloc every %d times\n", fail_nth);
146 fail_size = atoi (
_dbus_getenv (
"DBUS_MALLOC_FAIL_GREATER_THAN"));
147 _dbus_verbose (
"Will fail mallocs over %ld bytes\n",
154 _dbus_verbose (
"Will use dbus_malloc guards\n");
159 disable_mem_pools =
TRUE;
160 _dbus_verbose (
"Will disable memory pools\n");
165 backtrace_on_fail_alloc =
TRUE;
166 _dbus_verbose (
"Will backtrace on failing a dbus_malloc\n");
171 malloc_cannot_fail =
TRUE;
172 _dbus_verbose (
"Will abort if system malloc() and friends fail\n");
183_dbus_disable_mem_pools (
void)
185 _dbus_initialize_malloc_debug ();
186 return disable_mem_pools;
202_dbus_set_fail_alloc_counter (
int until_next_fail)
204 _dbus_initialize_malloc_debug ();
206 fail_alloc_counter = until_next_fail;
209 _dbus_verbose (
"Set fail alloc counter = %d\n", fail_alloc_counter);
224_dbus_get_fail_alloc_counter (
void)
226 _dbus_initialize_malloc_debug ();
228 return fail_alloc_counter;
238_dbus_set_fail_alloc_failures (
int failures_per_failure)
240 n_failures_per_failure = failures_per_failure;
250_dbus_get_fail_alloc_failures (
void)
252 return n_failures_per_failure;
264_dbus_decrement_fail_alloc_counter (
void)
266 _dbus_initialize_malloc_debug ();
268 if (fail_alloc_counter < 0)
275 fail_alloc_counter -= 1;
279 else if (fail_alloc_counter == 0)
282 if (backtrace_on_fail_alloc)
283 _dbus_print_backtrace ();
285 _dbus_verbose (
"failure %d\n", n_failures_this_failure);
287 n_failures_this_failure += 1;
288 if (n_failures_this_failure >= n_failures_per_failure)
290 fail_alloc_counter = fail_nth;
291 n_failures_this_failure = 0;
293 _dbus_verbose (
"reset fail alloc counter to %d\n", fail_alloc_counter);
302 fail_alloc_counter -= 1;
313_dbus_get_malloc_blocks_outstanding (
void)
331source_string (BlockSource source)
341 case SOURCE_MALLOC_ZERO:
343 case SOURCE_REALLOC_NULL:
344 return "realloc(NULL)";
352check_guards (
void *free_block,
353 dbus_bool_t overwrite)
355 if (free_block !=
NULL)
357 unsigned char *block = ((
unsigned char*)free_block) - GUARD_START_OFFSET;
358 size_t requested_bytes = *(dbus_uint32_t *) (
void *) block;
359 BlockSource source = *(dbus_uint32_t *) (
void *) (block + 4);
366 _dbus_verbose (
"Checking %d bytes request from source %s\n",
367 requested_bytes, source_string (source));
371 while (i < GUARD_START_OFFSET)
373 dbus_uint32_t value = *(dbus_uint32_t *) (
void *) &block[i];
374 if (value != GUARD_VALUE)
376 _dbus_warn (
"Block of %lu bytes from %s had start guard value 0x%ux at %d expected 0x%x",
377 (
long) requested_bytes, source_string (source),
378 value, i, GUARD_VALUE);
385 i = GUARD_START_OFFSET + requested_bytes;
386 while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
388 dbus_uint32_t value = *(dbus_uint32_t *) (
void *) &block[i];
389 if (value != GUARD_VALUE)
391 _dbus_warn (
"Block of %lu bytes from %s had end guard value 0x%ux at %d expected 0x%x",
392 (
long) requested_bytes, source_string (source),
393 value, i, GUARD_VALUE);
402 memset (free_block,
'g', requested_bytes);
410set_guards (
void *real_block,
411 size_t requested_bytes,
414 unsigned char *block = real_block;
420 _dbus_assert (GUARD_START_OFFSET + GUARD_END_PAD == GUARD_EXTRA_SIZE);
422 *((dbus_uint32_t *) (
void *) block) = requested_bytes;
423 *((dbus_uint32_t *) (
void *) (block + 4)) = source;
426 while (i < GUARD_START_OFFSET)
428 (*(dbus_uint32_t *) (
void *) &block[i]) = GUARD_VALUE;
433 i = GUARD_START_OFFSET + requested_bytes;
434 while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
436 (*(dbus_uint32_t *) (
void *) &block[i]) = GUARD_VALUE;
441 check_guards (block + GUARD_START_OFFSET,
FALSE);
443 return block + GUARD_START_OFFSET;
472#ifdef DBUS_ENABLE_EMBEDDED_TESTS
473 _dbus_initialize_malloc_debug ();
475 if (_dbus_decrement_fail_alloc_counter ())
477 _dbus_verbose (
" FAILING malloc of %ld bytes\n", (
long) bytes);
484#ifdef DBUS_ENABLE_EMBEDDED_TESTS
485 else if (fail_size != 0 && bytes > fail_size)
491 block = malloc (bytes + GUARD_EXTRA_SIZE);
496 else if (malloc_cannot_fail)
498 _dbus_warn (
"out of memory: malloc (%ld + %ld)",
499 (
long) bytes, (
long) GUARD_EXTRA_SIZE);
503 return set_guards (block, bytes, SOURCE_MALLOC);
509 mem = malloc (bytes);
511#ifdef DBUS_ENABLE_EMBEDDED_TESTS
516 else if (malloc_cannot_fail)
518 _dbus_warn (
"out of memory: malloc (%ld)", (
long) bytes);
542#ifdef DBUS_ENABLE_EMBEDDED_TESTS
543 _dbus_initialize_malloc_debug ();
545 if (_dbus_decrement_fail_alloc_counter ())
547 _dbus_verbose (
" FAILING malloc0 of %ld bytes\n", (
long) bytes);
555#ifdef DBUS_ENABLE_EMBEDDED_TESTS
556 else if (fail_size != 0 && bytes > fail_size)
562 block = calloc (bytes + GUARD_EXTRA_SIZE, 1);
568 else if (malloc_cannot_fail)
570 _dbus_warn (
"out of memory: calloc (%ld + %ld, 1)",
571 (
long) bytes, (
long) GUARD_EXTRA_SIZE);
575 return set_guards (block, bytes, SOURCE_MALLOC_ZERO);
581 mem = calloc (bytes, 1);
583#ifdef DBUS_ENABLE_EMBEDDED_TESTS
588 else if (malloc_cannot_fail)
590 _dbus_warn (
"out of memory: calloc (%ld)", (
long) bytes);
613#ifdef DBUS_ENABLE_EMBEDDED_TESTS
614 _dbus_initialize_malloc_debug ();
616 if (_dbus_decrement_fail_alloc_counter ())
618 _dbus_verbose (
" FAILING realloc of %ld bytes\n", (
long) bytes);
629#ifdef DBUS_ENABLE_EMBEDDED_TESTS
630 else if (fail_size != 0 && bytes > fail_size)
639 check_guards (memory,
FALSE);
641 block = realloc (((
unsigned char*)memory) - GUARD_START_OFFSET,
642 bytes + GUARD_EXTRA_SIZE);
646 if (malloc_cannot_fail)
648 _dbus_warn (
"out of memory: realloc (%p, %ld + %ld)",
649 memory, (
long) bytes, (
long) GUARD_EXTRA_SIZE);
656 old_bytes = *(dbus_uint32_t*)block;
657 if (bytes >= old_bytes)
659 check_guards (((
unsigned char*)block) + GUARD_START_OFFSET,
FALSE);
661 return set_guards (block, bytes, SOURCE_REALLOC);
667 block = malloc (bytes + GUARD_EXTRA_SIZE);
673 else if (malloc_cannot_fail)
675 _dbus_warn (
"out of memory: malloc (%ld + %ld)",
676 (
long) bytes, (
long) GUARD_EXTRA_SIZE);
680 return set_guards (block, bytes, SOURCE_REALLOC_NULL);
687 mem = realloc (memory, bytes);
689#ifdef DBUS_ENABLE_EMBEDDED_TESTS
690 if (mem ==
NULL && malloc_cannot_fail)
692 _dbus_warn (
"out of memory: malloc (%ld)", (
long) bytes);
712#ifdef DBUS_ENABLE_EMBEDDED_TESTS
715 check_guards (memory,
TRUE);
718#ifdef DBUS_DISABLE_ASSERT
721 dbus_int32_t old_value;
727 free (((
unsigned char*)memory) - GUARD_START_OFFSET);
736#ifdef DBUS_ENABLE_EMBEDDED_TESTS
737#ifdef DBUS_DISABLE_ASSERT
740 dbus_int32_t old_value;
827 ok = _dbus_register_shutdown_func_unlocked (func, data);
833_dbus_register_shutdown_func_unlocked (DBusShutdownFunction func,
846 c->
next = registered_globals;
847 registered_globals = c;
908 while (registered_globals !=
NULL)
912 c = registered_globals;
913 registered_globals = c->
next;
931#ifdef DBUS_ENABLE_EMBEDDED_TESTS
932#include "dbus-test.h"
940_dbus_memory_test (
const char *test_data_dir _DBUS_GNUC_UNUSED)
942 dbus_bool_t old_guards;
950 _dbus_test_fatal (
"no memory");
951 for (size = 4; size < 256; size += 4)
955 _dbus_test_fatal (
"no memory");
957 for (size = 256; size != 0; size -= 4)
961 _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_MIN
Minimum 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).
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.