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 ();
259 if (fail_alloc_counter <= 0)
261 if (backtrace_on_fail_alloc)
264 _dbus_verbose (
"failure %d\n", n_failures_this_failure);
266 n_failures_this_failure += 1;
267 if (n_failures_this_failure >= n_failures_per_failure)
270 fail_alloc_counter = fail_nth;
274 n_failures_this_failure = 0;
276 _dbus_verbose (
"reset fail alloc counter to %d\n", fail_alloc_counter);
283 fail_alloc_counter -= 1;
295_dbus_get_malloc_blocks_outstanding (
void)
313source_string (BlockSource source)
323 case SOURCE_MALLOC_ZERO:
325 case SOURCE_REALLOC_NULL:
326 return "realloc(NULL)";
334check_guards (
void *free_block,
337 if (free_block !=
NULL)
339 unsigned char *block = ((
unsigned char*)free_block) - GUARD_START_OFFSET;
340 size_t requested_bytes = *(dbus_uint32_t*)block;
341 BlockSource source = *(dbus_uint32_t*)(block + 4);
348 _dbus_verbose (
"Checking %d bytes request from source %s\n",
349 requested_bytes, source_string (source));
353 while (i < GUARD_START_OFFSET)
355 dbus_uint32_t value = *(dbus_uint32_t*) &block[i];
356 if (value != GUARD_VALUE)
358 _dbus_warn (
"Block of %lu bytes from %s had start guard value 0x%ux at %d expected 0x%x",
359 (
long) requested_bytes, source_string (source),
360 value, i, GUARD_VALUE);
367 i = GUARD_START_OFFSET + requested_bytes;
368 while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
370 dbus_uint32_t value = *(dbus_uint32_t*) &block[i];
371 if (value != GUARD_VALUE)
373 _dbus_warn (
"Block of %lu bytes from %s had end guard value 0x%ux at %d expected 0x%x",
374 (
long) requested_bytes, source_string (source),
375 value, i, GUARD_VALUE);
384 memset (free_block,
'g', requested_bytes);
392set_guards (
void *real_block,
393 size_t requested_bytes,
396 unsigned char *block = real_block;
402 _dbus_assert (GUARD_START_OFFSET + GUARD_END_PAD == GUARD_EXTRA_SIZE);
404 *((dbus_uint32_t*)block) = requested_bytes;
405 *((dbus_uint32_t*)(block + 4)) = source;
408 while (i < GUARD_START_OFFSET)
410 (*(dbus_uint32_t*) &block[i]) = GUARD_VALUE;
415 i = GUARD_START_OFFSET + requested_bytes;
416 while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
418 (*(dbus_uint32_t*) &block[i]) = GUARD_VALUE;
423 check_guards (block + GUARD_START_OFFSET,
FALSE);
425 return block + GUARD_START_OFFSET;
454#ifdef DBUS_ENABLE_EMBEDDED_TESTS
455 _dbus_initialize_malloc_debug ();
457 if (_dbus_decrement_fail_alloc_counter ())
459 _dbus_verbose (
" FAILING malloc of %ld bytes\n", (
long) bytes);
466#ifdef DBUS_ENABLE_EMBEDDED_TESTS
467 else if (fail_size != 0 && bytes > fail_size)
473 block = malloc (bytes + GUARD_EXTRA_SIZE);
478 else if (malloc_cannot_fail)
480 _dbus_warn (
"out of memory: malloc (%ld + %ld)",
481 (
long) bytes, (
long) GUARD_EXTRA_SIZE);
485 return set_guards (block, bytes, SOURCE_MALLOC);
491 mem = malloc (bytes);
493#ifdef DBUS_ENABLE_EMBEDDED_TESTS
498 else if (malloc_cannot_fail)
500 _dbus_warn (
"out of memory: malloc (%ld)", (
long) bytes);
524#ifdef DBUS_ENABLE_EMBEDDED_TESTS
525 _dbus_initialize_malloc_debug ();
527 if (_dbus_decrement_fail_alloc_counter ())
529 _dbus_verbose (
" FAILING malloc0 of %ld bytes\n", (
long) bytes);
537#ifdef DBUS_ENABLE_EMBEDDED_TESTS
538 else if (fail_size != 0 && bytes > fail_size)
544 block = calloc (bytes + GUARD_EXTRA_SIZE, 1);
550 else if (malloc_cannot_fail)
552 _dbus_warn (
"out of memory: calloc (%ld + %ld, 1)",
553 (
long) bytes, (
long) GUARD_EXTRA_SIZE);
557 return set_guards (block, bytes, SOURCE_MALLOC_ZERO);
563 mem = calloc (bytes, 1);
565#ifdef DBUS_ENABLE_EMBEDDED_TESTS
570 else if (malloc_cannot_fail)
572 _dbus_warn (
"out of memory: calloc (%ld)", (
long) bytes);
595#ifdef DBUS_ENABLE_EMBEDDED_TESTS
596 _dbus_initialize_malloc_debug ();
598 if (_dbus_decrement_fail_alloc_counter ())
600 _dbus_verbose (
" FAILING realloc of %ld bytes\n", (
long) bytes);
611#ifdef DBUS_ENABLE_EMBEDDED_TESTS
612 else if (fail_size != 0 && bytes > fail_size)
621 check_guards (memory,
FALSE);
623 block = realloc (((
unsigned char*)memory) - GUARD_START_OFFSET,
624 bytes + GUARD_EXTRA_SIZE);
628 if (malloc_cannot_fail)
630 _dbus_warn (
"out of memory: realloc (%p, %ld + %ld)",
631 memory, (
long) bytes, (
long) GUARD_EXTRA_SIZE);
638 old_bytes = *(dbus_uint32_t*)block;
639 if (bytes >= old_bytes)
641 check_guards (((
unsigned char*)block) + GUARD_START_OFFSET,
FALSE);
643 return set_guards (block, bytes, SOURCE_REALLOC);
649 block = malloc (bytes + GUARD_EXTRA_SIZE);
655 else if (malloc_cannot_fail)
657 _dbus_warn (
"out of memory: malloc (%ld + %ld)",
658 (
long) bytes, (
long) GUARD_EXTRA_SIZE);
662 return set_guards (block, bytes, SOURCE_REALLOC_NULL);
669 mem = realloc (memory, bytes);
671#ifdef DBUS_ENABLE_EMBEDDED_TESTS
672 if (mem ==
NULL && malloc_cannot_fail)
674 _dbus_warn (
"out of memory: malloc (%ld)", (
long) bytes);
694#ifdef DBUS_ENABLE_EMBEDDED_TESTS
697 check_guards (memory,
TRUE);
700#ifdef DBUS_DISABLE_ASSERT
703 dbus_int32_t old_value;
709 free (((
unsigned char*)memory) - GUARD_START_OFFSET);
718#ifdef DBUS_ENABLE_EMBEDDED_TESTS
719#ifdef DBUS_DISABLE_ASSERT
722 dbus_int32_t old_value;
809 ok = _dbus_register_shutdown_func_unlocked (func, data);
815_dbus_register_shutdown_func_unlocked (DBusShutdownFunction func,
828 c->
next = registered_globals;
829 registered_globals = c;
890 while (registered_globals !=
NULL)
894 c = registered_globals;
895 registered_globals = c->
next;
913#ifdef DBUS_ENABLE_EMBEDDED_TESTS
914#include "dbus-test.h"
922_dbus_memory_test (
const char *test_data_dir _DBUS_GNUC_UNUSED)
932 _dbus_test_fatal (
"no memory");
933 for (size = 4; size < 256; size += 4)
937 _dbus_test_fatal (
"no memory");
939 for (size = 256; size != 0; size -= 4)
943 _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.