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