D-Bus 1.13.18
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#ifdef DBUS_WIN
259 {
260 static dbus_bool_t called = 0;
261
262 if (!called)
263 {
264 _dbus_verbose_raw ("TODO: memory allocation testing errors disabled for now\n");
265 called = 1;
266 }
267 return FALSE;
268 }
269#endif
270
271 if (fail_alloc_counter <= 0)
272 {
273 if (backtrace_on_fail_alloc)
275
276 _dbus_verbose ("failure %d\n", n_failures_this_failure);
277
278 n_failures_this_failure += 1;
279 if (n_failures_this_failure >= n_failures_per_failure)
280 {
281 if (fail_nth >= 0)
282 fail_alloc_counter = fail_nth;
283 else
284 fail_alloc_counter = _DBUS_INT_MAX;
285
286 n_failures_this_failure = 0;
287
288 _dbus_verbose ("reset fail alloc counter to %d\n", fail_alloc_counter);
289 }
290
291 return TRUE;
292 }
293 else
294 {
295 fail_alloc_counter -= 1;
296 return FALSE;
297 }
298}
299#endif /* DBUS_ENABLE_EMBEDDED_TESTS */
300
306int
307_dbus_get_malloc_blocks_outstanding (void)
308{
309 return _dbus_atomic_get (&n_blocks_outstanding);
310}
311
315typedef enum
316{
317 SOURCE_UNKNOWN,
318 SOURCE_MALLOC,
319 SOURCE_REALLOC,
320 SOURCE_MALLOC_ZERO,
321 SOURCE_REALLOC_NULL
322} BlockSource;
323
324static const char*
325source_string (BlockSource source)
326{
327 switch (source)
328 {
329 case SOURCE_UNKNOWN:
330 return "unknown";
331 case SOURCE_MALLOC:
332 return "malloc";
333 case SOURCE_REALLOC:
334 return "realloc";
335 case SOURCE_MALLOC_ZERO:
336 return "malloc0";
337 case SOURCE_REALLOC_NULL:
338 return "realloc(NULL)";
339 default:
340 _dbus_assert_not_reached ("Invalid malloc block source ID");
341 return "invalid!";
342 }
343}
344
345static void
346check_guards (void *free_block,
347 dbus_bool_t overwrite)
348{
349 if (free_block != NULL)
350 {
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);
354 unsigned int i;
355 dbus_bool_t failed;
356
357 failed = FALSE;
358
359#if 0
360 _dbus_verbose ("Checking %d bytes request from source %s\n",
361 requested_bytes, source_string (source));
362#endif
363
364 i = GUARD_INFO_SIZE;
365 while (i < GUARD_START_OFFSET)
366 {
367 dbus_uint32_t value = *(dbus_uint32_t*) &block[i];
368 if (value != GUARD_VALUE)
369 {
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);
373 failed = TRUE;
374 }
375
376 i += 4;
377 }
378
379 i = GUARD_START_OFFSET + requested_bytes;
380 while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
381 {
382 dbus_uint32_t value = *(dbus_uint32_t*) &block[i];
383 if (value != GUARD_VALUE)
384 {
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);
388 failed = TRUE;
389 }
390
391 i += 4;
392 }
393
394 /* set memory to anything but nul bytes */
395 if (overwrite)
396 memset (free_block, 'g', requested_bytes);
397
398 if (failed)
399 _dbus_assert_not_reached ("guard value corruption");
400 }
401}
402
403static void*
404set_guards (void *real_block,
405 size_t requested_bytes,
406 BlockSource source)
407{
408 unsigned char *block = real_block;
409 unsigned int i;
410
411 if (block == NULL)
412 return NULL;
413
414 _dbus_assert (GUARD_START_OFFSET + GUARD_END_PAD == GUARD_EXTRA_SIZE);
415
416 *((dbus_uint32_t*)block) = requested_bytes;
417 *((dbus_uint32_t*)(block + 4)) = source;
418
419 i = GUARD_INFO_SIZE;
420 while (i < GUARD_START_OFFSET)
421 {
422 (*(dbus_uint32_t*) &block[i]) = GUARD_VALUE;
423
424 i += 4;
425 }
426
427 i = GUARD_START_OFFSET + requested_bytes;
428 while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
429 {
430 (*(dbus_uint32_t*) &block[i]) = GUARD_VALUE;
431
432 i += 4;
433 }
434
435 check_guards (block + GUARD_START_OFFSET, FALSE);
436
437 return block + GUARD_START_OFFSET;
438}
439
440#endif
441 /* End of internals docs */
443
444
463void*
464dbus_malloc (size_t bytes)
465{
466#ifdef DBUS_ENABLE_EMBEDDED_TESTS
467 _dbus_initialize_malloc_debug ();
468
469 if (_dbus_decrement_fail_alloc_counter ())
470 {
471 _dbus_verbose (" FAILING malloc of %ld bytes\n", (long) bytes);
472 return NULL;
473 }
474#endif
475
476 if (bytes == 0) /* some system mallocs handle this, some don't */
477 return NULL;
478#ifdef DBUS_ENABLE_EMBEDDED_TESTS
479 else if (fail_size != 0 && bytes > fail_size)
480 return NULL;
481 else if (guards)
482 {
483 void *block;
484
485 block = malloc (bytes + GUARD_EXTRA_SIZE);
486 if (block)
487 {
488 _dbus_atomic_inc (&n_blocks_outstanding);
489 }
490 else if (malloc_cannot_fail)
491 {
492 _dbus_warn ("out of memory: malloc (%ld + %ld)",
493 (long) bytes, (long) GUARD_EXTRA_SIZE);
494 _dbus_abort ();
495 }
496
497 return set_guards (block, bytes, SOURCE_MALLOC);
498 }
499#endif
500 else
501 {
502 void *mem;
503 mem = malloc (bytes);
504
505#ifdef DBUS_ENABLE_EMBEDDED_TESTS
506 if (mem)
507 {
508 _dbus_atomic_inc (&n_blocks_outstanding);
509 }
510 else if (malloc_cannot_fail)
511 {
512 _dbus_warn ("out of memory: malloc (%ld)", (long) bytes);
513 _dbus_abort ();
514 }
515#endif
516
517 return mem;
518 }
519}
520
533void*
534dbus_malloc0 (size_t bytes)
535{
536#ifdef DBUS_ENABLE_EMBEDDED_TESTS
537 _dbus_initialize_malloc_debug ();
538
539 if (_dbus_decrement_fail_alloc_counter ())
540 {
541 _dbus_verbose (" FAILING malloc0 of %ld bytes\n", (long) bytes);
542
543 return NULL;
544 }
545#endif
546
547 if (bytes == 0)
548 return NULL;
549#ifdef DBUS_ENABLE_EMBEDDED_TESTS
550 else if (fail_size != 0 && bytes > fail_size)
551 return NULL;
552 else if (guards)
553 {
554 void *block;
555
556 block = calloc (bytes + GUARD_EXTRA_SIZE, 1);
557
558 if (block)
559 {
560 _dbus_atomic_inc (&n_blocks_outstanding);
561 }
562 else if (malloc_cannot_fail)
563 {
564 _dbus_warn ("out of memory: calloc (%ld + %ld, 1)",
565 (long) bytes, (long) GUARD_EXTRA_SIZE);
566 _dbus_abort ();
567 }
568
569 return set_guards (block, bytes, SOURCE_MALLOC_ZERO);
570 }
571#endif
572 else
573 {
574 void *mem;
575 mem = calloc (bytes, 1);
576
577#ifdef DBUS_ENABLE_EMBEDDED_TESTS
578 if (mem)
579 {
580 _dbus_atomic_inc (&n_blocks_outstanding);
581 }
582 else if (malloc_cannot_fail)
583 {
584 _dbus_warn ("out of memory: calloc (%ld)", (long) bytes);
585 _dbus_abort ();
586 }
587#endif
588
589 return mem;
590 }
591}
592
603void*
604dbus_realloc (void *memory,
605 size_t bytes)
606{
607#ifdef DBUS_ENABLE_EMBEDDED_TESTS
608 _dbus_initialize_malloc_debug ();
609
610 if (_dbus_decrement_fail_alloc_counter ())
611 {
612 _dbus_verbose (" FAILING realloc of %ld bytes\n", (long) bytes);
613
614 return NULL;
615 }
616#endif
617
618 if (bytes == 0) /* guarantee this is safe */
619 {
620 dbus_free (memory);
621 return NULL;
622 }
623#ifdef DBUS_ENABLE_EMBEDDED_TESTS
624 else if (fail_size != 0 && bytes > fail_size)
625 return NULL;
626 else if (guards)
627 {
628 if (memory)
629 {
630 size_t old_bytes;
631 void *block;
632
633 check_guards (memory, FALSE);
634
635 block = realloc (((unsigned char*)memory) - GUARD_START_OFFSET,
636 bytes + GUARD_EXTRA_SIZE);
637
638 if (block == NULL)
639 {
640 if (malloc_cannot_fail)
641 {
642 _dbus_warn ("out of memory: realloc (%p, %ld + %ld)",
643 memory, (long) bytes, (long) GUARD_EXTRA_SIZE);
644 _dbus_abort ();
645 }
646
647 return NULL;
648 }
649
650 old_bytes = *(dbus_uint32_t*)block;
651 if (bytes >= old_bytes)
652 /* old guards shouldn't have moved */
653 check_guards (((unsigned char*)block) + GUARD_START_OFFSET, FALSE);
654
655 return set_guards (block, bytes, SOURCE_REALLOC);
656 }
657 else
658 {
659 void *block;
660
661 block = malloc (bytes + GUARD_EXTRA_SIZE);
662
663 if (block)
664 {
665 _dbus_atomic_inc (&n_blocks_outstanding);
666 }
667 else if (malloc_cannot_fail)
668 {
669 _dbus_warn ("out of memory: malloc (%ld + %ld)",
670 (long) bytes, (long) GUARD_EXTRA_SIZE);
671 _dbus_abort ();
672 }
673
674 return set_guards (block, bytes, SOURCE_REALLOC_NULL);
675 }
676 }
677#endif
678 else
679 {
680 void *mem;
681 mem = realloc (memory, bytes);
682
683#ifdef DBUS_ENABLE_EMBEDDED_TESTS
684 if (mem == NULL && malloc_cannot_fail)
685 {
686 _dbus_warn ("out of memory: malloc (%ld)", (long) bytes);
687 _dbus_abort ();
688 }
689
690 if (memory == NULL && mem != NULL)
691 _dbus_atomic_inc (&n_blocks_outstanding);
692#endif
693 return mem;
694 }
695}
696
703void
704dbus_free (void *memory)
705{
706#ifdef DBUS_ENABLE_EMBEDDED_TESTS
707 if (guards)
708 {
709 check_guards (memory, TRUE);
710 if (memory)
711 {
712#ifdef DBUS_DISABLE_ASSERT
713 _dbus_atomic_dec (&n_blocks_outstanding);
714#else
715 dbus_int32_t old_value;
716
717 old_value = _dbus_atomic_dec (&n_blocks_outstanding);
718 _dbus_assert (old_value >= 1);
719#endif
720
721 free (((unsigned char*)memory) - GUARD_START_OFFSET);
722 }
723
724 return;
725 }
726#endif
727
728 if (memory) /* we guarantee it's safe to free (NULL) */
729 {
730#ifdef DBUS_ENABLE_EMBEDDED_TESTS
731#ifdef DBUS_DISABLE_ASSERT
732 _dbus_atomic_dec (&n_blocks_outstanding);
733#else
734 dbus_int32_t old_value;
735
736 old_value = _dbus_atomic_dec (&n_blocks_outstanding);
737 _dbus_assert (old_value >= 1);
738#endif
739#endif
740
741 free (memory);
742 }
743}
744
751void
752dbus_free_string_array (char **str_array)
753{
754 if (str_array)
755 {
756 int i;
757
758 i = 0;
759 while (str_array[i])
760 {
761 dbus_free (str_array[i]);
762 i++;
763 }
764
765 dbus_free (str_array);
766 }
767}
768 /* End of public API docs block */
770
771
785
790
795{
797 DBusShutdownFunction func;
798 void *data;
799};
800
801/* Protected by _DBUS_LOCK (shutdown_funcs) */
802static ShutdownClosure *registered_globals = NULL;
803
813_dbus_register_shutdown_func (DBusShutdownFunction func,
814 void *data)
815{
816 dbus_bool_t ok;
817
818 if (!_DBUS_LOCK (shutdown_funcs))
819 return FALSE;
820
821 ok = _dbus_register_shutdown_func_unlocked (func, data);
822 _DBUS_UNLOCK (shutdown_funcs);
823 return ok;
824}
825
827_dbus_register_shutdown_func_unlocked (DBusShutdownFunction func,
828 void *data)
829{
831
832 c = dbus_new (ShutdownClosure, 1);
833
834 if (c == NULL)
835 return FALSE;
836
837 c->func = func;
838 c->data = data;
839
840 c->next = registered_globals;
841 registered_globals = c;
842
843 return TRUE;
844}
845 /* End of private API docs block */
847
848
899void
901{
902 while (registered_globals != NULL)
903 {
905
906 c = registered_globals;
907 registered_globals = c->next;
908
909 (* c->func) (c->data);
910
911 dbus_free (c);
912 }
913
914 /* We wrap this in the thread-initialization lock because
915 * dbus_threads_init() uses the current generation to tell whether
916 * we're initialized, so we need to make sure that un-initializing
917 * propagates into all threads. */
921}
922
925#ifdef DBUS_ENABLE_EMBEDDED_TESTS
926#include "dbus-test.h"
927
934_dbus_memory_test (const char *test_data_dir _DBUS_GNUC_UNUSED)
935{
936 dbus_bool_t old_guards;
937 void *p;
938 size_t size;
939
940 old_guards = guards;
941 guards = TRUE;
942 p = dbus_malloc (4);
943 if (p == NULL)
944 _dbus_test_fatal ("no memory");
945 for (size = 4; size < 256; size += 4)
946 {
947 p = dbus_realloc (p, size);
948 if (p == NULL)
949 _dbus_test_fatal ("no memory");
950 }
951 for (size = 256; size != 0; size -= 4)
952 {
953 p = dbus_realloc (p, size);
954 if (p == NULL)
955 _dbus_test_fatal ("no memory");
956 }
957 dbus_free (p);
958 guards = old_guards;
959 return TRUE;
960}
961
962#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:784
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:813
void dbus_shutdown(void)
Frees all memory allocated internally by libdbus and reverses the effects of dbus_threads_init().
Definition: dbus-memory.c:900
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:704
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:604
#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:534
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
Definition: dbus-memory.c:752
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
Definition: dbus-memory.c:464
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
An atomic integer safe to increment or decrement from multiple threads.
Definition: dbus-sysdeps.h:327
This struct represents a function to be called on shutdown.
Definition: dbus-memory.c:795
ShutdownClosure * next
Next ShutdownClosure.
Definition: dbus-memory.c:796
DBusShutdownFunction func
Function to call.
Definition: dbus-memory.c:797
void * data
Data for function.
Definition: dbus-memory.c:798