D-Bus 1.15.2
dbus-memory.c
1/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2/* dbus-memory.c D-Bus memory handling
3 *
4 * Copyright (C) 2002, 2003 Red Hat Inc.
5 *
6 * Licensed under the Academic Free License version 2.1
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23
24#include <config.h>
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"
30#include <dbus/dbus-test-tap.h>
31#include <stdlib.h>
32 /* end of public API docs */
94
101#ifdef DBUS_ENABLE_EMBEDDED_TESTS
102/* Test-only, does not need to be thread-safe */
103static dbus_bool_t debug_initialized = FALSE;
104static int fail_nth = -1;
105static size_t fail_size = 0;
106static int fail_alloc_counter = _DBUS_INT_MAX;
107static int n_failures_per_failure = 1;
108static int n_failures_this_failure = 0;
109static dbus_bool_t guards = FALSE;
110static dbus_bool_t disable_mem_pools = FALSE;
111static dbus_bool_t backtrace_on_fail_alloc = FALSE;
112static dbus_bool_t malloc_cannot_fail = FALSE;
113static DBusAtomic n_blocks_outstanding = {0};
114
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)
127
128static void
129_dbus_initialize_malloc_debug (void)
130{
131 if (!debug_initialized)
132 {
133 debug_initialized = TRUE;
134
135 if (_dbus_getenv ("DBUS_MALLOC_FAIL_NTH") != NULL)
136 {
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);
140 }
141
142 if (_dbus_getenv ("DBUS_MALLOC_FAIL_GREATER_THAN") != NULL)
143 {
144 fail_size = atoi (_dbus_getenv ("DBUS_MALLOC_FAIL_GREATER_THAN"));
145 _dbus_verbose ("Will fail mallocs over %ld bytes\n",
146 (long) fail_size);
147 }
148
149 if (_dbus_getenv ("DBUS_MALLOC_GUARDS") != NULL)
150 {
151 guards = TRUE;
152 _dbus_verbose ("Will use dbus_malloc guards\n");
153 }
154
155 if (_dbus_getenv ("DBUS_DISABLE_MEM_POOLS") != NULL)
156 {
157 disable_mem_pools = TRUE;
158 _dbus_verbose ("Will disable memory pools\n");
159 }
160
161 if (_dbus_getenv ("DBUS_MALLOC_BACKTRACES") != NULL)
162 {
163 backtrace_on_fail_alloc = TRUE;
164 _dbus_verbose ("Will backtrace on failing a dbus_malloc\n");
165 }
166
167 if (_dbus_getenv ("DBUS_MALLOC_CANNOT_FAIL") != NULL)
168 {
169 malloc_cannot_fail = TRUE;
170 _dbus_verbose ("Will abort if system malloc() and friends fail\n");
171 }
172 }
173}
174
181_dbus_disable_mem_pools (void)
182{
183 _dbus_initialize_malloc_debug ();
184 return disable_mem_pools;
185}
186
195void
196_dbus_set_fail_alloc_counter (int until_next_fail)
197{
198 _dbus_initialize_malloc_debug ();
199
200 fail_alloc_counter = until_next_fail;
201
202#if 0
203 _dbus_verbose ("Set fail alloc counter = %d\n", fail_alloc_counter);
204#endif
205}
206
213int
214_dbus_get_fail_alloc_counter (void)
215{
216 _dbus_initialize_malloc_debug ();
217
218 return fail_alloc_counter;
219}
220
227void
228_dbus_set_fail_alloc_failures (int failures_per_failure)
229{
230 n_failures_per_failure = failures_per_failure;
231}
232
239int
240_dbus_get_fail_alloc_failures (void)
241{
242 return n_failures_per_failure;
243}
244
245#ifdef DBUS_ENABLE_EMBEDDED_TESTS
255_dbus_decrement_fail_alloc_counter (void)
256{
257 _dbus_initialize_malloc_debug ();
258
259 if (fail_alloc_counter <= 0)
260 {
261 if (backtrace_on_fail_alloc)
263
264 _dbus_verbose ("failure %d\n", n_failures_this_failure);
265
266 n_failures_this_failure += 1;
267 if (n_failures_this_failure >= n_failures_per_failure)
268 {
269 if (fail_nth >= 0)
270 fail_alloc_counter = fail_nth;
271 else
272 fail_alloc_counter = _DBUS_INT_MAX;
273
274 n_failures_this_failure = 0;
275
276 _dbus_verbose ("reset fail alloc counter to %d\n", fail_alloc_counter);
277 }
278
279 return TRUE;
280 }
281 else
282 {
283 fail_alloc_counter -= 1;
284 return FALSE;
285 }
286}
287#endif /* DBUS_ENABLE_EMBEDDED_TESTS */
288
294int
295_dbus_get_malloc_blocks_outstanding (void)
296{
297 return _dbus_atomic_get (&n_blocks_outstanding);
298}
299
303typedef enum
304{
305 SOURCE_UNKNOWN,
306 SOURCE_MALLOC,
307 SOURCE_REALLOC,
308 SOURCE_MALLOC_ZERO,
309 SOURCE_REALLOC_NULL
310} BlockSource;
311
312static const char*
313source_string (BlockSource source)
314{
315 switch (source)
316 {
317 case SOURCE_UNKNOWN:
318 return "unknown";
319 case SOURCE_MALLOC:
320 return "malloc";
321 case SOURCE_REALLOC:
322 return "realloc";
323 case SOURCE_MALLOC_ZERO:
324 return "malloc0";
325 case SOURCE_REALLOC_NULL:
326 return "realloc(NULL)";
327 default:
328 _dbus_assert_not_reached ("Invalid malloc block source ID");
329 return "invalid!";
330 }
331}
332
333static void
334check_guards (void *free_block,
335 dbus_bool_t overwrite)
336{
337 if (free_block != NULL)
338 {
339 unsigned char *block = ((unsigned char*)free_block) - GUARD_START_OFFSET;
340 size_t requested_bytes = *(dbus_uint32_t *) (void *) block;
341 BlockSource source = *(dbus_uint32_t *) (void *) (block + 4);
342 unsigned int i;
343 dbus_bool_t failed;
344
345 failed = FALSE;
346
347#if 0
348 _dbus_verbose ("Checking %d bytes request from source %s\n",
349 requested_bytes, source_string (source));
350#endif
351
352 i = GUARD_INFO_SIZE;
353 while (i < GUARD_START_OFFSET)
354 {
355 dbus_uint32_t value = *(dbus_uint32_t *) (void *) &block[i];
356 if (value != GUARD_VALUE)
357 {
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);
361 failed = TRUE;
362 }
363
364 i += 4;
365 }
366
367 i = GUARD_START_OFFSET + requested_bytes;
368 while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
369 {
370 dbus_uint32_t value = *(dbus_uint32_t *) (void *) &block[i];
371 if (value != GUARD_VALUE)
372 {
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);
376 failed = TRUE;
377 }
378
379 i += 4;
380 }
381
382 /* set memory to anything but nul bytes */
383 if (overwrite)
384 memset (free_block, 'g', requested_bytes);
385
386 if (failed)
387 _dbus_assert_not_reached ("guard value corruption");
388 }
389}
390
391static void*
392set_guards (void *real_block,
393 size_t requested_bytes,
394 BlockSource source)
395{
396 unsigned char *block = real_block;
397 unsigned int i;
398
399 if (block == NULL)
400 return NULL;
401
402 _dbus_assert (GUARD_START_OFFSET + GUARD_END_PAD == GUARD_EXTRA_SIZE);
403
404 *((dbus_uint32_t *) (void *) block) = requested_bytes;
405 *((dbus_uint32_t *) (void *) (block + 4)) = source;
406
407 i = GUARD_INFO_SIZE;
408 while (i < GUARD_START_OFFSET)
409 {
410 (*(dbus_uint32_t *) (void *) &block[i]) = GUARD_VALUE;
411
412 i += 4;
413 }
414
415 i = GUARD_START_OFFSET + requested_bytes;
416 while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
417 {
418 (*(dbus_uint32_t *) (void *) &block[i]) = GUARD_VALUE;
419
420 i += 4;
421 }
422
423 check_guards (block + GUARD_START_OFFSET, FALSE);
424
425 return block + GUARD_START_OFFSET;
426}
427
428#endif
429 /* End of internals docs */
431
432
451void*
452dbus_malloc (size_t bytes)
453{
454#ifdef DBUS_ENABLE_EMBEDDED_TESTS
455 _dbus_initialize_malloc_debug ();
456
457 if (_dbus_decrement_fail_alloc_counter ())
458 {
459 _dbus_verbose (" FAILING malloc of %ld bytes\n", (long) bytes);
460 return NULL;
461 }
462#endif
463
464 if (bytes == 0) /* some system mallocs handle this, some don't */
465 return NULL;
466#ifdef DBUS_ENABLE_EMBEDDED_TESTS
467 else if (fail_size != 0 && bytes > fail_size)
468 return NULL;
469 else if (guards)
470 {
471 void *block;
472
473 block = malloc (bytes + GUARD_EXTRA_SIZE);
474 if (block)
475 {
476 _dbus_atomic_inc (&n_blocks_outstanding);
477 }
478 else if (malloc_cannot_fail)
479 {
480 _dbus_warn ("out of memory: malloc (%ld + %ld)",
481 (long) bytes, (long) GUARD_EXTRA_SIZE);
482 _dbus_abort ();
483 }
484
485 return set_guards (block, bytes, SOURCE_MALLOC);
486 }
487#endif
488 else
489 {
490 void *mem;
491 mem = malloc (bytes);
492
493#ifdef DBUS_ENABLE_EMBEDDED_TESTS
494 if (mem)
495 {
496 _dbus_atomic_inc (&n_blocks_outstanding);
497 }
498 else if (malloc_cannot_fail)
499 {
500 _dbus_warn ("out of memory: malloc (%ld)", (long) bytes);
501 _dbus_abort ();
502 }
503#endif
504
505 return mem;
506 }
507}
508
521void*
522dbus_malloc0 (size_t bytes)
523{
524#ifdef DBUS_ENABLE_EMBEDDED_TESTS
525 _dbus_initialize_malloc_debug ();
526
527 if (_dbus_decrement_fail_alloc_counter ())
528 {
529 _dbus_verbose (" FAILING malloc0 of %ld bytes\n", (long) bytes);
530
531 return NULL;
532 }
533#endif
534
535 if (bytes == 0)
536 return NULL;
537#ifdef DBUS_ENABLE_EMBEDDED_TESTS
538 else if (fail_size != 0 && bytes > fail_size)
539 return NULL;
540 else if (guards)
541 {
542 void *block;
543
544 block = calloc (bytes + GUARD_EXTRA_SIZE, 1);
545
546 if (block)
547 {
548 _dbus_atomic_inc (&n_blocks_outstanding);
549 }
550 else if (malloc_cannot_fail)
551 {
552 _dbus_warn ("out of memory: calloc (%ld + %ld, 1)",
553 (long) bytes, (long) GUARD_EXTRA_SIZE);
554 _dbus_abort ();
555 }
556
557 return set_guards (block, bytes, SOURCE_MALLOC_ZERO);
558 }
559#endif
560 else
561 {
562 void *mem;
563 mem = calloc (bytes, 1);
564
565#ifdef DBUS_ENABLE_EMBEDDED_TESTS
566 if (mem)
567 {
568 _dbus_atomic_inc (&n_blocks_outstanding);
569 }
570 else if (malloc_cannot_fail)
571 {
572 _dbus_warn ("out of memory: calloc (%ld)", (long) bytes);
573 _dbus_abort ();
574 }
575#endif
576
577 return mem;
578 }
579}
580
591void*
592dbus_realloc (void *memory,
593 size_t bytes)
594{
595#ifdef DBUS_ENABLE_EMBEDDED_TESTS
596 _dbus_initialize_malloc_debug ();
597
598 if (_dbus_decrement_fail_alloc_counter ())
599 {
600 _dbus_verbose (" FAILING realloc of %ld bytes\n", (long) bytes);
601
602 return NULL;
603 }
604#endif
605
606 if (bytes == 0) /* guarantee this is safe */
607 {
608 dbus_free (memory);
609 return NULL;
610 }
611#ifdef DBUS_ENABLE_EMBEDDED_TESTS
612 else if (fail_size != 0 && bytes > fail_size)
613 return NULL;
614 else if (guards)
615 {
616 if (memory)
617 {
618 size_t old_bytes;
619 void *block;
620
621 check_guards (memory, FALSE);
622
623 block = realloc (((unsigned char*)memory) - GUARD_START_OFFSET,
624 bytes + GUARD_EXTRA_SIZE);
625
626 if (block == NULL)
627 {
628 if (malloc_cannot_fail)
629 {
630 _dbus_warn ("out of memory: realloc (%p, %ld + %ld)",
631 memory, (long) bytes, (long) GUARD_EXTRA_SIZE);
632 _dbus_abort ();
633 }
634
635 return NULL;
636 }
637
638 old_bytes = *(dbus_uint32_t*)block;
639 if (bytes >= old_bytes)
640 /* old guards shouldn't have moved */
641 check_guards (((unsigned char*)block) + GUARD_START_OFFSET, FALSE);
642
643 return set_guards (block, bytes, SOURCE_REALLOC);
644 }
645 else
646 {
647 void *block;
648
649 block = malloc (bytes + GUARD_EXTRA_SIZE);
650
651 if (block)
652 {
653 _dbus_atomic_inc (&n_blocks_outstanding);
654 }
655 else if (malloc_cannot_fail)
656 {
657 _dbus_warn ("out of memory: malloc (%ld + %ld)",
658 (long) bytes, (long) GUARD_EXTRA_SIZE);
659 _dbus_abort ();
660 }
661
662 return set_guards (block, bytes, SOURCE_REALLOC_NULL);
663 }
664 }
665#endif
666 else
667 {
668 void *mem;
669 mem = realloc (memory, bytes);
670
671#ifdef DBUS_ENABLE_EMBEDDED_TESTS
672 if (mem == NULL && malloc_cannot_fail)
673 {
674 _dbus_warn ("out of memory: malloc (%ld)", (long) bytes);
675 _dbus_abort ();
676 }
677
678 if (memory == NULL && mem != NULL)
679 _dbus_atomic_inc (&n_blocks_outstanding);
680#endif
681 return mem;
682 }
683}
684
691void
692dbus_free (void *memory)
693{
694#ifdef DBUS_ENABLE_EMBEDDED_TESTS
695 if (guards)
696 {
697 check_guards (memory, TRUE);
698 if (memory)
699 {
700#ifdef DBUS_DISABLE_ASSERT
701 _dbus_atomic_dec (&n_blocks_outstanding);
702#else
703 dbus_int32_t old_value;
704
705 old_value = _dbus_atomic_dec (&n_blocks_outstanding);
706 _dbus_assert (old_value >= 1);
707#endif
708
709 free (((unsigned char*)memory) - GUARD_START_OFFSET);
710 }
711
712 return;
713 }
714#endif
715
716 if (memory) /* we guarantee it's safe to free (NULL) */
717 {
718#ifdef DBUS_ENABLE_EMBEDDED_TESTS
719#ifdef DBUS_DISABLE_ASSERT
720 _dbus_atomic_dec (&n_blocks_outstanding);
721#else
722 dbus_int32_t old_value;
723
724 old_value = _dbus_atomic_dec (&n_blocks_outstanding);
725 _dbus_assert (old_value >= 1);
726#endif
727#endif
728
729 free (memory);
730 }
731}
732
739void
740dbus_free_string_array (char **str_array)
741{
742 if (str_array)
743 {
744 int i;
745
746 i = 0;
747 while (str_array[i])
748 {
749 dbus_free (str_array[i]);
750 i++;
751 }
752
753 dbus_free (str_array);
754 }
755}
756 /* End of public API docs block */
758
759
773
778
783{
785 DBusShutdownFunction func;
786 void *data;
787};
788
789/* Protected by _DBUS_LOCK (shutdown_funcs) */
790static ShutdownClosure *registered_globals = NULL;
791
801_dbus_register_shutdown_func (DBusShutdownFunction func,
802 void *data)
803{
804 dbus_bool_t ok;
805
806 if (!_DBUS_LOCK (shutdown_funcs))
807 return FALSE;
808
809 ok = _dbus_register_shutdown_func_unlocked (func, data);
810 _DBUS_UNLOCK (shutdown_funcs);
811 return ok;
812}
813
815_dbus_register_shutdown_func_unlocked (DBusShutdownFunction func,
816 void *data)
817{
819
820 c = dbus_new (ShutdownClosure, 1);
821
822 if (c == NULL)
823 return FALSE;
824
825 c->func = func;
826 c->data = data;
827
828 c->next = registered_globals;
829 registered_globals = c;
830
831 return TRUE;
832}
833 /* End of private API docs block */
835
836
887void
889{
890 while (registered_globals != NULL)
891 {
893
894 c = registered_globals;
895 registered_globals = c->next;
896
897 (* c->func) (c->data);
898
899 dbus_free (c);
900 }
901
902 /* We wrap this in the thread-initialization lock because
903 * dbus_threads_init() uses the current generation to tell whether
904 * we're initialized, so we need to make sure that un-initializing
905 * propagates into all threads. */
909}
910
913#ifdef DBUS_ENABLE_EMBEDDED_TESTS
914#include "dbus-test.h"
915
922_dbus_memory_test (const char *test_data_dir _DBUS_GNUC_UNUSED)
923{
924 dbus_bool_t old_guards;
925 void *p;
926 size_t size;
927
928 old_guards = guards;
929 guards = TRUE;
930 p = dbus_malloc (4);
931 if (p == NULL)
932 _dbus_test_fatal ("no memory");
933 for (size = 4; size < 256; size += 4)
934 {
935 p = dbus_realloc (p, size);
936 if (p == NULL)
937 _dbus_test_fatal ("no memory");
938 }
939 for (size = 256; size != 0; size -= 4)
940 {
941 p = dbus_realloc (p, size);
942 if (p == NULL)
943 _dbus_test_fatal ("no memory");
944 }
945 dbus_free (p);
946 guards = old_guards;
947 return TRUE;
948}
949
950#endif
#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,...
Definition: dbus-memory.c:772
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.
Definition: dbus-memory.c:801
void dbus_shutdown(void)
Frees all memory allocated internally by libdbus and reverses the effects of dbus_threads_init().
Definition: dbus-memory.c:888
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:692
void * dbus_realloc(void *memory, size_t bytes)
Resizes a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:592
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
Definition: dbus-memory.h:57
void * dbus_malloc0(size_t bytes)
Allocates the given number of bytes, as with standard malloc(), but all bytes are initialized to zero...
Definition: dbus-memory.c:522
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
Definition: dbus-memory.c:740
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
Definition: dbus-memory.c:452
dbus_int32_t _dbus_atomic_dec(DBusAtomic *atomic)
Atomically decrement an integer.
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
Definition: dbus-sysdeps.c:195
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).
Definition: dbus-sysdeps.c:87
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.
Definition: dbus-types.h:35
int dbus_int32_t
A 32-bit signed integer on all platforms.
unsigned int dbus_uint32_t
A 32-bit unsigned integer on all platforms.
An atomic integer safe to increment or decrement from multiple threads.
Definition: dbus-sysdeps.h:321
This struct represents a function to be called on shutdown.
Definition: dbus-memory.c:783
ShutdownClosure * next
Next ShutdownClosure.
Definition: dbus-memory.c:784
DBusShutdownFunction func
Function to call.
Definition: dbus-memory.c:785
void * data
Data for function.
Definition: dbus-memory.c:786