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;
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;
198_dbus_set_fail_alloc_counter (
int until_next_fail)
200 _dbus_initialize_malloc_debug ();
202 fail_alloc_counter = until_next_fail;
205 _dbus_verbose (
"Set fail alloc counter = %d\n", fail_alloc_counter);
216_dbus_get_fail_alloc_counter (
void)
218 _dbus_initialize_malloc_debug ();
220 return fail_alloc_counter;
230_dbus_set_fail_alloc_failures (
int failures_per_failure)
232 n_failures_per_failure = failures_per_failure;
242_dbus_get_fail_alloc_failures (
void)
244 return n_failures_per_failure;
247#ifdef DBUS_ENABLE_EMBEDDED_TESTS
257_dbus_decrement_fail_alloc_counter (
void)
259 _dbus_initialize_malloc_debug ();
261 if (fail_alloc_counter <= 0)
263 if (backtrace_on_fail_alloc)
264 _dbus_print_backtrace ();
266 _dbus_verbose (
"failure %d\n", n_failures_this_failure);
268 n_failures_this_failure += 1;
269 if (n_failures_this_failure >= n_failures_per_failure)
272 fail_alloc_counter = fail_nth;
276 n_failures_this_failure = 0;
278 _dbus_verbose (
"reset fail alloc counter to %d\n", fail_alloc_counter);
285 fail_alloc_counter -= 1;
297_dbus_get_malloc_blocks_outstanding (
void)
315source_string (BlockSource source)
325 case SOURCE_MALLOC_ZERO:
327 case SOURCE_REALLOC_NULL:
328 return "realloc(NULL)";
336check_guards (
void *free_block,
337 dbus_bool_t overwrite)
339 if (free_block !=
NULL)
341 unsigned char *block = ((
unsigned char*)free_block) - GUARD_START_OFFSET;
342 size_t requested_bytes = *(dbus_uint32_t *) (
void *) block;
343 BlockSource source = *(dbus_uint32_t *) (
void *) (block + 4);
350 _dbus_verbose (
"Checking %d bytes request from source %s\n",
351 requested_bytes, source_string (source));
355 while (i < GUARD_START_OFFSET)
357 dbus_uint32_t value = *(dbus_uint32_t *) (
void *) &block[i];
358 if (value != GUARD_VALUE)
360 _dbus_warn (
"Block of %lu bytes from %s had start guard value 0x%ux at %d expected 0x%x",
361 (
long) requested_bytes, source_string (source),
362 value, i, GUARD_VALUE);
369 i = GUARD_START_OFFSET + requested_bytes;
370 while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
372 dbus_uint32_t value = *(dbus_uint32_t *) (
void *) &block[i];
373 if (value != GUARD_VALUE)
375 _dbus_warn (
"Block of %lu bytes from %s had end guard value 0x%ux at %d expected 0x%x",
376 (
long) requested_bytes, source_string (source),
377 value, i, GUARD_VALUE);
386 memset (free_block,
'g', requested_bytes);
394set_guards (
void *real_block,
395 size_t requested_bytes,
398 unsigned char *block = real_block;
404 _dbus_assert (GUARD_START_OFFSET + GUARD_END_PAD == GUARD_EXTRA_SIZE);
406 *((dbus_uint32_t *) (
void *) block) = requested_bytes;
407 *((dbus_uint32_t *) (
void *) (block + 4)) = source;
410 while (i < GUARD_START_OFFSET)
412 (*(dbus_uint32_t *) (
void *) &block[i]) = GUARD_VALUE;
417 i = GUARD_START_OFFSET + requested_bytes;
418 while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
420 (*(dbus_uint32_t *) (
void *) &block[i]) = GUARD_VALUE;
425 check_guards (block + GUARD_START_OFFSET,
FALSE);
427 return block + GUARD_START_OFFSET;
456#ifdef DBUS_ENABLE_EMBEDDED_TESTS
457 _dbus_initialize_malloc_debug ();
459 if (_dbus_decrement_fail_alloc_counter ())
461 _dbus_verbose (
" FAILING malloc of %ld bytes\n", (
long) bytes);
468#ifdef DBUS_ENABLE_EMBEDDED_TESTS
469 else if (fail_size != 0 && bytes > fail_size)
475 block = malloc (bytes + GUARD_EXTRA_SIZE);
480 else if (malloc_cannot_fail)
482 _dbus_warn (
"out of memory: malloc (%ld + %ld)",
483 (
long) bytes, (
long) GUARD_EXTRA_SIZE);
487 return set_guards (block, bytes, SOURCE_MALLOC);
493 mem = malloc (bytes);
495#ifdef DBUS_ENABLE_EMBEDDED_TESTS
500 else if (malloc_cannot_fail)
502 _dbus_warn (
"out of memory: malloc (%ld)", (
long) bytes);
526#ifdef DBUS_ENABLE_EMBEDDED_TESTS
527 _dbus_initialize_malloc_debug ();
529 if (_dbus_decrement_fail_alloc_counter ())
531 _dbus_verbose (
" FAILING malloc0 of %ld bytes\n", (
long) bytes);
539#ifdef DBUS_ENABLE_EMBEDDED_TESTS
540 else if (fail_size != 0 && bytes > fail_size)
546 block = calloc (bytes + GUARD_EXTRA_SIZE, 1);
552 else if (malloc_cannot_fail)
554 _dbus_warn (
"out of memory: calloc (%ld + %ld, 1)",
555 (
long) bytes, (
long) GUARD_EXTRA_SIZE);
559 return set_guards (block, bytes, SOURCE_MALLOC_ZERO);
565 mem = calloc (bytes, 1);
567#ifdef DBUS_ENABLE_EMBEDDED_TESTS
572 else if (malloc_cannot_fail)
574 _dbus_warn (
"out of memory: calloc (%ld)", (
long) bytes);
597#ifdef DBUS_ENABLE_EMBEDDED_TESTS
598 _dbus_initialize_malloc_debug ();
600 if (_dbus_decrement_fail_alloc_counter ())
602 _dbus_verbose (
" FAILING realloc of %ld bytes\n", (
long) bytes);
613#ifdef DBUS_ENABLE_EMBEDDED_TESTS
614 else if (fail_size != 0 && bytes > fail_size)
623 check_guards (memory,
FALSE);
625 block = realloc (((
unsigned char*)memory) - GUARD_START_OFFSET,
626 bytes + GUARD_EXTRA_SIZE);
630 if (malloc_cannot_fail)
632 _dbus_warn (
"out of memory: realloc (%p, %ld + %ld)",
633 memory, (
long) bytes, (
long) GUARD_EXTRA_SIZE);
640 old_bytes = *(dbus_uint32_t*)block;
641 if (bytes >= old_bytes)
643 check_guards (((
unsigned char*)block) + GUARD_START_OFFSET,
FALSE);
645 return set_guards (block, bytes, SOURCE_REALLOC);
651 block = malloc (bytes + GUARD_EXTRA_SIZE);
657 else if (malloc_cannot_fail)
659 _dbus_warn (
"out of memory: malloc (%ld + %ld)",
660 (
long) bytes, (
long) GUARD_EXTRA_SIZE);
664 return set_guards (block, bytes, SOURCE_REALLOC_NULL);
671 mem = realloc (memory, bytes);
673#ifdef DBUS_ENABLE_EMBEDDED_TESTS
674 if (mem ==
NULL && malloc_cannot_fail)
676 _dbus_warn (
"out of memory: malloc (%ld)", (
long) bytes);
696#ifdef DBUS_ENABLE_EMBEDDED_TESTS
699 check_guards (memory,
TRUE);
702#ifdef DBUS_DISABLE_ASSERT
705 dbus_int32_t old_value;
711 free (((
unsigned char*)memory) - GUARD_START_OFFSET);
720#ifdef DBUS_ENABLE_EMBEDDED_TESTS
721#ifdef DBUS_DISABLE_ASSERT
724 dbus_int32_t old_value;
811 ok = _dbus_register_shutdown_func_unlocked (func, data);
817_dbus_register_shutdown_func_unlocked (DBusShutdownFunction func,
830 c->
next = registered_globals;
831 registered_globals = c;
892 while (registered_globals !=
NULL)
896 c = registered_globals;
897 registered_globals = c->
next;
915#ifdef DBUS_ENABLE_EMBEDDED_TESTS
916#include "dbus-test.h"
924_dbus_memory_test (
const char *test_data_dir _DBUS_GNUC_UNUSED)
926 dbus_bool_t old_guards;
934 _dbus_test_fatal (
"no memory");
935 for (size = 4; size < 256; size += 4)
939 _dbus_test_fatal (
"no memory");
941 for (size = 256; size != 0; size -= 4)
945 _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).
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.