D-Bus  1.13.7
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 static dbus_bool_t debug_initialized = FALSE;
103 static int fail_nth = -1;
104 static size_t fail_size = 0;
105 static int fail_alloc_counter = _DBUS_INT_MAX;
106 static int n_failures_per_failure = 1;
107 static int n_failures_this_failure = 0;
108 static dbus_bool_t guards = FALSE;
109 static dbus_bool_t disable_mem_pools = FALSE;
110 static dbus_bool_t backtrace_on_fail_alloc = FALSE;
111 static dbus_bool_t malloc_cannot_fail = FALSE;
112 static DBusAtomic n_blocks_outstanding = {0};
113 
115 #define GUARD_VALUE 0xdeadbeef
116 
117 #define GUARD_INFO_SIZE 8
118 
119 #define GUARD_START_PAD 16
120 
121 #define GUARD_END_PAD 16
122 
123 #define GUARD_START_OFFSET (GUARD_START_PAD + GUARD_INFO_SIZE)
124 
125 #define GUARD_EXTRA_SIZE (GUARD_START_OFFSET + GUARD_END_PAD)
126 
127 static void
128 _dbus_initialize_malloc_debug (void)
129 {
130  if (!debug_initialized)
131  {
132  debug_initialized = TRUE;
133 
134  if (_dbus_getenv ("DBUS_MALLOC_FAIL_NTH") != NULL)
135  {
136  fail_nth = atoi (_dbus_getenv ("DBUS_MALLOC_FAIL_NTH"));
137  fail_alloc_counter = fail_nth;
138  _dbus_verbose ("Will fail dbus_malloc every %d times\n", fail_nth);
139  }
140 
141  if (_dbus_getenv ("DBUS_MALLOC_FAIL_GREATER_THAN") != NULL)
142  {
143  fail_size = atoi (_dbus_getenv ("DBUS_MALLOC_FAIL_GREATER_THAN"));
144  _dbus_verbose ("Will fail mallocs over %ld bytes\n",
145  (long) fail_size);
146  }
147 
148  if (_dbus_getenv ("DBUS_MALLOC_GUARDS") != NULL)
149  {
150  guards = TRUE;
151  _dbus_verbose ("Will use dbus_malloc guards\n");
152  }
153 
154  if (_dbus_getenv ("DBUS_DISABLE_MEM_POOLS") != NULL)
155  {
156  disable_mem_pools = TRUE;
157  _dbus_verbose ("Will disable memory pools\n");
158  }
159 
160  if (_dbus_getenv ("DBUS_MALLOC_BACKTRACES") != NULL)
161  {
162  backtrace_on_fail_alloc = TRUE;
163  _dbus_verbose ("Will backtrace on failing a dbus_malloc\n");
164  }
165 
166  if (_dbus_getenv ("DBUS_MALLOC_CANNOT_FAIL") != NULL)
167  {
168  malloc_cannot_fail = TRUE;
169  _dbus_verbose ("Will abort if system malloc() and friends fail\n");
170  }
171  }
172 }
173 
180 _dbus_disable_mem_pools (void)
181 {
182  _dbus_initialize_malloc_debug ();
183  return disable_mem_pools;
184 }
185 
194 void
195 _dbus_set_fail_alloc_counter (int until_next_fail)
196 {
197  _dbus_initialize_malloc_debug ();
198 
199  fail_alloc_counter = until_next_fail;
200 
201 #if 0
202  _dbus_verbose ("Set fail alloc counter = %d\n", fail_alloc_counter);
203 #endif
204 }
205 
212 int
213 _dbus_get_fail_alloc_counter (void)
214 {
215  _dbus_initialize_malloc_debug ();
216 
217  return fail_alloc_counter;
218 }
219 
226 void
227 _dbus_set_fail_alloc_failures (int failures_per_failure)
228 {
229  n_failures_per_failure = failures_per_failure;
230 }
231 
238 int
239 _dbus_get_fail_alloc_failures (void)
240 {
241  return n_failures_per_failure;
242 }
243 
244 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
245 
254 _dbus_decrement_fail_alloc_counter (void)
255 {
256  _dbus_initialize_malloc_debug ();
257 #ifdef DBUS_WIN_FIXME
258  {
259  static dbus_bool_t called = 0;
260 
261  if (!called)
262  {
263  _dbus_verbose("TODO: memory allocation testing errors disabled for now\n");
264  called = 1;
265  }
266  return FALSE;
267  }
268 #endif
269 
270  if (fail_alloc_counter <= 0)
271  {
272  if (backtrace_on_fail_alloc)
274 
275  _dbus_verbose ("failure %d\n", n_failures_this_failure);
276 
277  n_failures_this_failure += 1;
278  if (n_failures_this_failure >= n_failures_per_failure)
279  {
280  if (fail_nth >= 0)
281  fail_alloc_counter = fail_nth;
282  else
283  fail_alloc_counter = _DBUS_INT_MAX;
284 
285  n_failures_this_failure = 0;
286 
287  _dbus_verbose ("reset fail alloc counter to %d\n", fail_alloc_counter);
288  }
289 
290  return TRUE;
291  }
292  else
293  {
294  fail_alloc_counter -= 1;
295  return FALSE;
296  }
297 }
298 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
299 
305 int
306 _dbus_get_malloc_blocks_outstanding (void)
307 {
308  return _dbus_atomic_get (&n_blocks_outstanding);
309 }
310 
314 typedef enum
315 {
316  SOURCE_UNKNOWN,
317  SOURCE_MALLOC,
318  SOURCE_REALLOC,
319  SOURCE_MALLOC_ZERO,
320  SOURCE_REALLOC_NULL
321 } BlockSource;
322 
323 static const char*
324 source_string (BlockSource source)
325 {
326  switch (source)
327  {
328  case SOURCE_UNKNOWN:
329  return "unknown";
330  case SOURCE_MALLOC:
331  return "malloc";
332  case SOURCE_REALLOC:
333  return "realloc";
334  case SOURCE_MALLOC_ZERO:
335  return "malloc0";
336  case SOURCE_REALLOC_NULL:
337  return "realloc(NULL)";
338  default:
339  _dbus_assert_not_reached ("Invalid malloc block source ID");
340  return "invalid!";
341  }
342 }
343 
344 static void
345 check_guards (void *free_block,
346  dbus_bool_t overwrite)
347 {
348  if (free_block != NULL)
349  {
350  unsigned char *block = ((unsigned char*)free_block) - GUARD_START_OFFSET;
351  size_t requested_bytes = *(dbus_uint32_t*)block;
352  BlockSource source = *(dbus_uint32_t*)(block + 4);
353  unsigned int i;
354  dbus_bool_t failed;
355 
356  failed = FALSE;
357 
358 #if 0
359  _dbus_verbose ("Checking %d bytes request from source %s\n",
360  requested_bytes, source_string (source));
361 #endif
362 
363  i = GUARD_INFO_SIZE;
364  while (i < GUARD_START_OFFSET)
365  {
366  dbus_uint32_t value = *(dbus_uint32_t*) &block[i];
367  if (value != GUARD_VALUE)
368  {
369  _dbus_warn ("Block of %lu bytes from %s had start guard value 0x%ux at %d expected 0x%x",
370  (long) requested_bytes, source_string (source),
371  value, i, GUARD_VALUE);
372  failed = TRUE;
373  }
374 
375  i += 4;
376  }
377 
378  i = GUARD_START_OFFSET + requested_bytes;
379  while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
380  {
381  dbus_uint32_t value = *(dbus_uint32_t*) &block[i];
382  if (value != GUARD_VALUE)
383  {
384  _dbus_warn ("Block of %lu bytes from %s had end guard value 0x%ux at %d expected 0x%x",
385  (long) requested_bytes, source_string (source),
386  value, i, GUARD_VALUE);
387  failed = TRUE;
388  }
389 
390  i += 4;
391  }
392 
393  /* set memory to anything but nul bytes */
394  if (overwrite)
395  memset (free_block, 'g', requested_bytes);
396 
397  if (failed)
398  _dbus_assert_not_reached ("guard value corruption");
399  }
400 }
401 
402 static void*
403 set_guards (void *real_block,
404  size_t requested_bytes,
405  BlockSource source)
406 {
407  unsigned char *block = real_block;
408  unsigned int i;
409 
410  if (block == NULL)
411  return NULL;
412 
413  _dbus_assert (GUARD_START_OFFSET + GUARD_END_PAD == GUARD_EXTRA_SIZE);
414 
415  *((dbus_uint32_t*)block) = requested_bytes;
416  *((dbus_uint32_t*)(block + 4)) = source;
417 
418  i = GUARD_INFO_SIZE;
419  while (i < GUARD_START_OFFSET)
420  {
421  (*(dbus_uint32_t*) &block[i]) = GUARD_VALUE;
422 
423  i += 4;
424  }
425 
426  i = GUARD_START_OFFSET + requested_bytes;
427  while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
428  {
429  (*(dbus_uint32_t*) &block[i]) = GUARD_VALUE;
430 
431  i += 4;
432  }
433 
434  check_guards (block + GUARD_START_OFFSET, FALSE);
435 
436  return block + GUARD_START_OFFSET;
437 }
438 
439 #endif
440  /* End of internals docs */
442 
443 
462 void*
463 dbus_malloc (size_t bytes)
464 {
465 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
466  _dbus_initialize_malloc_debug ();
467 
468  if (_dbus_decrement_fail_alloc_counter ())
469  {
470  _dbus_verbose (" FAILING malloc of %ld bytes\n", (long) bytes);
471  return NULL;
472  }
473 #endif
474 
475  if (bytes == 0) /* some system mallocs handle this, some don't */
476  return NULL;
477 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
478  else if (fail_size != 0 && bytes > fail_size)
479  return NULL;
480  else if (guards)
481  {
482  void *block;
483 
484  block = malloc (bytes + GUARD_EXTRA_SIZE);
485  if (block)
486  {
487  _dbus_atomic_inc (&n_blocks_outstanding);
488  }
489  else if (malloc_cannot_fail)
490  {
491  _dbus_warn ("out of memory: malloc (%ld + %ld)",
492  (long) bytes, (long) GUARD_EXTRA_SIZE);
493  _dbus_abort ();
494  }
495 
496  return set_guards (block, bytes, SOURCE_MALLOC);
497  }
498 #endif
499  else
500  {
501  void *mem;
502  mem = malloc (bytes);
503 
504 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
505  if (mem)
506  {
507  _dbus_atomic_inc (&n_blocks_outstanding);
508  }
509  else if (malloc_cannot_fail)
510  {
511  _dbus_warn ("out of memory: malloc (%ld)", (long) bytes);
512  _dbus_abort ();
513  }
514 #endif
515 
516  return mem;
517  }
518 }
519 
532 void*
533 dbus_malloc0 (size_t bytes)
534 {
535 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
536  _dbus_initialize_malloc_debug ();
537 
538  if (_dbus_decrement_fail_alloc_counter ())
539  {
540  _dbus_verbose (" FAILING malloc0 of %ld bytes\n", (long) bytes);
541 
542  return NULL;
543  }
544 #endif
545 
546  if (bytes == 0)
547  return NULL;
548 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
549  else if (fail_size != 0 && bytes > fail_size)
550  return NULL;
551  else if (guards)
552  {
553  void *block;
554 
555  block = calloc (bytes + GUARD_EXTRA_SIZE, 1);
556 
557  if (block)
558  {
559  _dbus_atomic_inc (&n_blocks_outstanding);
560  }
561  else if (malloc_cannot_fail)
562  {
563  _dbus_warn ("out of memory: calloc (%ld + %ld, 1)",
564  (long) bytes, (long) GUARD_EXTRA_SIZE);
565  _dbus_abort ();
566  }
567 
568  return set_guards (block, bytes, SOURCE_MALLOC_ZERO);
569  }
570 #endif
571  else
572  {
573  void *mem;
574  mem = calloc (bytes, 1);
575 
576 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
577  if (mem)
578  {
579  _dbus_atomic_inc (&n_blocks_outstanding);
580  }
581  else if (malloc_cannot_fail)
582  {
583  _dbus_warn ("out of memory: calloc (%ld)", (long) bytes);
584  _dbus_abort ();
585  }
586 #endif
587 
588  return mem;
589  }
590 }
591 
602 void*
603 dbus_realloc (void *memory,
604  size_t bytes)
605 {
606 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
607  _dbus_initialize_malloc_debug ();
608 
609  if (_dbus_decrement_fail_alloc_counter ())
610  {
611  _dbus_verbose (" FAILING realloc of %ld bytes\n", (long) bytes);
612 
613  return NULL;
614  }
615 #endif
616 
617  if (bytes == 0) /* guarantee this is safe */
618  {
619  dbus_free (memory);
620  return NULL;
621  }
622 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
623  else if (fail_size != 0 && bytes > fail_size)
624  return NULL;
625  else if (guards)
626  {
627  if (memory)
628  {
629  size_t old_bytes;
630  void *block;
631 
632  check_guards (memory, FALSE);
633 
634  block = realloc (((unsigned char*)memory) - GUARD_START_OFFSET,
635  bytes + GUARD_EXTRA_SIZE);
636 
637  if (block == NULL)
638  {
639  if (malloc_cannot_fail)
640  {
641  _dbus_warn ("out of memory: realloc (%p, %ld + %ld)",
642  memory, (long) bytes, (long) GUARD_EXTRA_SIZE);
643  _dbus_abort ();
644  }
645 
646  return NULL;
647  }
648 
649  old_bytes = *(dbus_uint32_t*)block;
650  if (bytes >= old_bytes)
651  /* old guards shouldn't have moved */
652  check_guards (((unsigned char*)block) + GUARD_START_OFFSET, FALSE);
653 
654  return set_guards (block, bytes, SOURCE_REALLOC);
655  }
656  else
657  {
658  void *block;
659 
660  block = malloc (bytes + GUARD_EXTRA_SIZE);
661 
662  if (block)
663  {
664  _dbus_atomic_inc (&n_blocks_outstanding);
665  }
666  else if (malloc_cannot_fail)
667  {
668  _dbus_warn ("out of memory: malloc (%ld + %ld)",
669  (long) bytes, (long) GUARD_EXTRA_SIZE);
670  _dbus_abort ();
671  }
672 
673  return set_guards (block, bytes, SOURCE_REALLOC_NULL);
674  }
675  }
676 #endif
677  else
678  {
679  void *mem;
680  mem = realloc (memory, bytes);
681 
682 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
683  if (mem == NULL && malloc_cannot_fail)
684  {
685  _dbus_warn ("out of memory: malloc (%ld)", (long) bytes);
686  _dbus_abort ();
687  }
688 
689  if (memory == NULL && mem != NULL)
690  _dbus_atomic_inc (&n_blocks_outstanding);
691 #endif
692  return mem;
693  }
694 }
695 
702 void
703 dbus_free (void *memory)
704 {
705 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
706  if (guards)
707  {
708  check_guards (memory, TRUE);
709  if (memory)
710  {
711 #ifdef DBUS_DISABLE_ASSERT
712  _dbus_atomic_dec (&n_blocks_outstanding);
713 #else
714  dbus_int32_t old_value;
715 
716  old_value = _dbus_atomic_dec (&n_blocks_outstanding);
717  _dbus_assert (old_value >= 1);
718 #endif
719 
720  free (((unsigned char*)memory) - GUARD_START_OFFSET);
721  }
722 
723  return;
724  }
725 #endif
726 
727  if (memory) /* we guarantee it's safe to free (NULL) */
728  {
729 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
730 #ifdef DBUS_DISABLE_ASSERT
731  _dbus_atomic_dec (&n_blocks_outstanding);
732 #else
733  dbus_int32_t old_value;
734 
735  old_value = _dbus_atomic_dec (&n_blocks_outstanding);
736  _dbus_assert (old_value >= 1);
737 #endif
738 #endif
739 
740  free (memory);
741  }
742 }
743 
750 void
751 dbus_free_string_array (char **str_array)
752 {
753  if (str_array)
754  {
755  int i;
756 
757  i = 0;
758  while (str_array[i])
759  {
760  dbus_free (str_array[i]);
761  i++;
762  }
763 
764  dbus_free (str_array);
765  }
766 }
767  /* End of public API docs block */
769 
770 
784 
789 
794 {
796  DBusShutdownFunction func;
797  void *data;
798 };
799 
800 /* Protected by _DBUS_LOCK (shutdown_funcs) */
801 static ShutdownClosure *registered_globals = NULL;
802 
812 _dbus_register_shutdown_func (DBusShutdownFunction func,
813  void *data)
814 {
815  dbus_bool_t ok;
816 
817  if (!_DBUS_LOCK (shutdown_funcs))
818  return FALSE;
819 
820  ok = _dbus_register_shutdown_func_unlocked (func, data);
821  _DBUS_UNLOCK (shutdown_funcs);
822  return ok;
823 }
824 
826 _dbus_register_shutdown_func_unlocked (DBusShutdownFunction func,
827  void *data)
828 {
829  ShutdownClosure *c;
830 
831  c = dbus_new (ShutdownClosure, 1);
832 
833  if (c == NULL)
834  return FALSE;
835 
836  c->func = func;
837  c->data = data;
838 
839  c->next = registered_globals;
840  registered_globals = c;
841 
842  return TRUE;
843 }
844  /* End of private API docs block */
846 
847 
898 void
900 {
901  while (registered_globals != NULL)
902  {
903  ShutdownClosure *c;
904 
905  c = registered_globals;
906  registered_globals = c->next;
907 
908  (* c->func) (c->data);
909 
910  dbus_free (c);
911  }
912 
913  /* We wrap this in the thread-initialization lock because
914  * dbus_threads_init() uses the current generation to tell whether
915  * we're initialized, so we need to make sure that un-initializing
916  * propagates into all threads. */
918  _dbus_current_generation += 1;
920 }
921 
924 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
925 #include "dbus-test.h"
926 
933 _dbus_memory_test (void)
934 {
935  dbus_bool_t old_guards;
936  void *p;
937  size_t size;
938 
939  old_guards = guards;
940  guards = TRUE;
941  p = dbus_malloc (4);
942  if (p == NULL)
943  _dbus_test_fatal ("no memory");
944  for (size = 4; size < 256; size += 4)
945  {
946  p = dbus_realloc (p, size);
947  if (p == NULL)
948  _dbus_test_fatal ("no memory");
949  }
950  for (size = 256; size != 0; size -= 4)
951  {
952  p = dbus_realloc (p, size);
953  if (p == NULL)
954  _dbus_test_fatal ("no memory");
955  }
956  dbus_free (p);
957  guards = old_guards;
958  return TRUE;
959 }
960 
961 #endif
This struct represents a function to be called on shutdown.
Definition: dbus-memory.c:793
An atomic integer safe to increment or decrement from multiple threads.
Definition: dbus-sysdeps.h:323
#define NULL
A null pointer, defined appropriately for C or C++.
void _dbus_abort(void)
Aborts the program with SIGABRT (dumping core).
Definition: dbus-sysdeps.c:87
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:703
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
Definition: dbus-memory.h:57
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
void * data
Data for function.
Definition: dbus-memory.c:797
#define _DBUS_INT_MAX
Maximum value of type "int".
void _dbus_threads_unlock_platform_specific(void)
Undo _dbus_threads_lock_platform_specific().
ShutdownClosure * next
Next ShutdownClosure.
Definition: dbus-memory.c:795
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
Definition: dbus-memory.c:463
void _dbus_threads_lock_platform_specific(void)
Lock a static mutex used to protect _dbus_threads_init_platform_specific().
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
dbus_int32_t _dbus_atomic_inc(DBusAtomic *atomic)
Atomically increments an integer.
void _dbus_print_backtrace(void)
On GNU libc systems, print a crude backtrace to stderr.
int _dbus_current_generation
_dbus_current_generation is used to track each time that dbus_shutdown() is called, so we can reinit things after it&#39;s been called.
Definition: dbus-memory.c:783
DBusShutdownFunction func
Function to call.
Definition: dbus-memory.c:796
#define _DBUS_UNLOCK(name)
Unlocks a global lock.
#define TRUE
Expands to "1".
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
dbus_int32_t _dbus_atomic_dec(DBusAtomic *atomic)
Atomically decrement an integer.
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
Definition: dbus-memory.c:751
void dbus_shutdown(void)
Frees all memory allocated internally by libdbus and reverses the effects of dbus_threads_init().
Definition: dbus-memory.c:899
#define FALSE
Expands to "0".
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:812
#define _DBUS_LOCK(name)
Locks a global lock, initializing it first if necessary.
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:533
dbus_int32_t _dbus_atomic_get(DBusAtomic *atomic)
Atomically get the value of an integer.
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:603
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
Definition: dbus-sysdeps.c:195