2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved.
4 * Copyright (c) 1997 by Silicon Graphics. All rights reserved.
5 * Copyright (c) 1999-2004 Hewlett-Packard Development Company, L.P.
6 * Copyright (C) 2007 Free Software Foundation, Inc
8 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
11 * Permission is hereby granted to use or copy this program
12 * for any purpose, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
18 #include "private/dbg_mlc.h"
25 #ifndef SHORT_DBG_HDRS
26 /* Check whether object with base pointer p has debugging info. */
27 /* p is assumed to point to a legitimate object in our part */
29 /* This excludes the check as to whether the back pointer is */
30 /* odd, which is added by the GC_HAS_DEBUG_INFO macro. */
31 /* Note that if DBG_HDRS_ALL is set, uncollectible objects */
32 /* on free lists may not have debug information set. Thus it's */
33 /* not always safe to return TRUE (1), even if the client does */
34 /* its part. Return -1 if the object with debug info has been */
35 /* marked as deallocated. */
36 GC_INNER int GC_has_other_debug_info(ptr_t p)
38 ptr_t body = (ptr_t)((oh *)p + 1);
41 if (HBLKPTR(p) != HBLKPTR((ptr_t)body)
42 || sz < DEBUG_BYTES + EXTRA_BYTES) {
45 if (((oh *)p) -> oh_sf != (START_FLAG ^ (word)body)
46 && ((word *)p)[BYTES_TO_WORDS(sz)-1] != (END_FLAG ^ (word)body)) {
49 if (((oh *)p)->oh_sz == sz) {
50 /* Object may have had debug info, but has been deallocated */
55 #endif /* !SHORT_DBG_HDRS */
60 static unsigned seed = 1; /* not thread-safe */
62 /* Linear congruential pseudo-random numbers generator. */
63 seed = (seed * 1103515245U + 12345) & GC_RAND_MAX; /* overflow is ok */
71 # define RANDOM() GC_random()
74 # define GC_RAND_MAX RAND_MAX
76 # if defined(__GLIBC__) || defined(SOLARIS) \
77 || defined(HPUX) || defined(IRIX5) || defined(OSF1)
78 # define RANDOM() random()
80 # define RANDOM() (long)rand()
84 /* Store back pointer to source in dest, if that appears to be possible. */
85 /* This is not completely safe, since we may mistakenly conclude that */
86 /* dest has a debugging wrapper. But the error probability is very */
87 /* small, and this shouldn't be used in production code. */
88 /* We assume that dest is the real base pointer. Source will usually */
89 /* be a pointer to the interior of an object. */
90 GC_INNER void GC_store_back_pointer(ptr_t source, ptr_t dest)
92 if (GC_HAS_DEBUG_INFO(dest)) {
93 ((oh *)dest) -> oh_back_ptr = HIDE_BACK_PTR(source);
97 GC_INNER void GC_marked_for_finalization(ptr_t dest)
99 GC_store_back_pointer(MARKED_FOR_FINALIZATION, dest);
102 /* Store information about the object referencing dest in *base_p */
104 /* source is root ==> *base_p = address, *offset_p = 0 */
105 /* source is heap object ==> *base_p != 0, *offset_p = offset */
106 /* Returns 1 on success, 0 if source couldn't be determined. */
107 /* Dest can be any address within a heap object. */
108 GC_API GC_ref_kind GC_CALL GC_get_back_ptr_info(void *dest, void **base_p,
111 oh * hdr = (oh *)GC_base(dest);
116 /* Explicitly instruct the code analysis tool that */
117 /* GC_get_back_ptr_info is not expected to be called with an */
118 /* incorrect "dest" value. */
119 if (!hdr) ABORT("Invalid GC_get_back_ptr_info argument");
121 if (!GC_HAS_DEBUG_INFO((ptr_t) hdr)) return GC_NO_SPACE;
122 bp = GC_REVEAL_POINTER(hdr -> oh_back_ptr);
123 if (MARKED_FOR_FINALIZATION == bp) return GC_FINALIZER_REFD;
124 if (MARKED_FROM_REGISTER == bp) return GC_REFD_FROM_REG;
125 if (NOT_MARKED == bp) return GC_UNREFERENCED;
127 /* Heuristically try to fix off by 1 errors we introduced by */
128 /* insisting on even addresses. */
130 ptr_t alternate_ptr = bp + 1;
131 ptr_t target = *(ptr_t *)bp;
132 ptr_t alternate_target = *(ptr_t *)alternate_ptr;
134 if ((word)alternate_target >= (word)GC_least_plausible_heap_addr
135 && (word)alternate_target <= (word)GC_greatest_plausible_heap_addr
136 && ((word)target < (word)GC_least_plausible_heap_addr
137 || (word)target > (word)GC_greatest_plausible_heap_addr)) {
142 bp_base = GC_base(bp);
146 return GC_REFD_FROM_ROOT;
148 if (GC_HAS_DEBUG_INFO(bp_base)) bp_base += sizeof(oh);
150 *offset_p = bp - bp_base;
151 return GC_REFD_FROM_HEAP;
155 /* Generate a random heap address. */
156 /* The resulting address is in the heap, but */
157 /* not necessarily inside a valid object. */
158 GC_API void * GC_CALL GC_generate_random_heap_address(void)
161 word heap_offset = RANDOM();
163 if (GC_heapsize > GC_RAND_MAX) {
164 heap_offset *= GC_RAND_MAX;
165 heap_offset += RANDOM();
167 heap_offset %= GC_heapsize;
168 /* This doesn't yield a uniform distribution, especially if */
169 /* e.g. RAND_MAX = 1.5* GC_heapsize. But for typical cases, */
170 /* it's not too bad. */
174 if (i >= GC_n_heap_sects)
175 ABORT("GC_generate_random_heap_address: size inconsistency");
177 size = GC_heap_sects[i].hs_bytes;
178 if (heap_offset < size) {
184 return GC_heap_sects[i].hs_start + heap_offset;
187 /* Generate a random address inside a valid marked heap object. */
188 GC_API void * GC_CALL GC_generate_random_valid_address(void)
193 result = GC_generate_random_heap_address();
194 base = GC_base(result);
195 } while (base == 0 || !GC_is_marked(base));
199 /* Print back trace for p */
200 GC_API void GC_CALL GC_print_backtrace(void *p)
208 GC_print_heap_obj(GC_base(current));
211 source = GC_get_back_ptr_info(current, &base, &offset);
212 if (GC_UNREFERENCED == source) {
213 GC_err_printf("Reference could not be found\n");
216 if (GC_NO_SPACE == source) {
217 GC_err_printf("No debug info in object: Can't find reference\n");
220 GC_err_printf("Reachable via %d levels of pointers from ", i);
222 case GC_REFD_FROM_ROOT:
223 GC_err_printf("root at %p\n\n", base);
225 case GC_REFD_FROM_REG:
226 GC_err_printf("root in register\n\n");
228 case GC_FINALIZER_REFD:
229 GC_err_printf("list of finalizable objects\n\n");
231 case GC_REFD_FROM_HEAP:
232 GC_err_printf("offset %ld in object:\n", (long)offset);
233 /* Take GC_base(base) to get real base, i.e. header. */
234 GC_print_heap_obj(GC_base(base));
237 GC_err_printf("INTERNAL ERROR: UNEXPECTED SOURCE!!!!\n");
245 /* Force a garbage collection and generate/print a backtrace */
246 /* from a random heap address. */
247 GC_INNER void GC_generate_random_backtrace_no_gc(void)
250 current = GC_generate_random_valid_address();
251 GC_printf("\n****Chosen address %p in object\n", current);
252 GC_print_backtrace(current);
255 GC_API void GC_CALL GC_generate_random_backtrace(void)
257 if (GC_try_to_collect(GC_never_stop_func) == 0) {
258 GC_err_printf("Cannot generate a backtrace: "
259 "garbage collection is disabled!\n");
262 GC_generate_random_backtrace_no_gc();
265 #endif /* KEEP_BACK_PTRS */
267 # define CROSSES_HBLK(p, sz) \
268 (((word)((p) + sizeof(oh) + (sz) - 1) ^ (word)(p)) >= HBLKSIZE)
270 /* Store debugging info into p. Return displaced pointer. */
271 /* This version assumes we do hold the allocation lock. */
272 STATIC ptr_t GC_store_debug_info_inner(ptr_t p, word sz GC_ATTR_UNUSED,
273 const char *string, int linenum)
275 word * result = (word *)((oh *)p + 1);
277 GC_ASSERT(GC_size(p) >= sizeof(oh) + sz);
278 GC_ASSERT(!(SMALL_OBJ(sz) && CROSSES_HBLK(p, sz)));
279 # ifdef KEEP_BACK_PTRS
280 ((oh *)p) -> oh_back_ptr = HIDE_BACK_PTR(NOT_MARKED);
282 # ifdef MAKE_BACK_GRAPH
283 ((oh *)p) -> oh_bg_ptr = HIDE_BACK_PTR((ptr_t)0);
285 ((oh *)p) -> oh_string = string;
286 ((oh *)p) -> oh_int = (word)linenum;
287 # ifndef SHORT_DBG_HDRS
288 ((oh *)p) -> oh_sz = sz;
289 ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
290 ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
291 result[SIMPLE_ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
293 return((ptr_t)result);
296 GC_INNER ptr_t GC_store_debug_info(ptr_t p, word sz, const char *string,
303 result = GC_store_debug_info_inner(p, sz, string, linenum);
308 #ifndef SHORT_DBG_HDRS
309 /* Check the object with debugging info at ohdr. */
310 /* Return NULL if it's OK. Else return clobbered */
312 STATIC ptr_t GC_check_annotated_obj(oh *ohdr)
314 ptr_t body = (ptr_t)(ohdr + 1);
315 word gc_sz = GC_size((ptr_t)ohdr);
316 if (ohdr -> oh_sz + DEBUG_BYTES > gc_sz) {
317 return((ptr_t)(&(ohdr -> oh_sz)));
319 if (ohdr -> oh_sf != (START_FLAG ^ (word)body)) {
320 return((ptr_t)(&(ohdr -> oh_sf)));
322 if (((word *)ohdr)[BYTES_TO_WORDS(gc_sz)-1] != (END_FLAG ^ (word)body)) {
323 return((ptr_t)((word *)ohdr + BYTES_TO_WORDS(gc_sz)-1));
325 if (((word *)body)[SIMPLE_ROUNDED_UP_WORDS(ohdr -> oh_sz)]
326 != (END_FLAG ^ (word)body)) {
327 return((ptr_t)((word *)body + SIMPLE_ROUNDED_UP_WORDS(ohdr->oh_sz)));
331 #endif /* !SHORT_DBG_HDRS */
333 STATIC GC_describe_type_fn GC_describe_type_fns[MAXOBJKINDS] = {0};
335 GC_API void GC_CALL GC_register_describe_type_fn(int kind,
336 GC_describe_type_fn fn)
338 GC_describe_type_fns[kind] = fn;
341 #define GET_OH_LINENUM(ohdr) ((int)(ohdr)->oh_int)
343 #ifndef SHORT_DBG_HDRS
344 # define IF_NOT_SHORTDBG_HDRS(x) x
345 # define COMMA_IFNOT_SHORTDBG_HDRS(x) /* comma */, x
347 # define IF_NOT_SHORTDBG_HDRS(x) /* empty */
348 # define COMMA_IFNOT_SHORTDBG_HDRS(x) /* empty */
351 /* Print a human-readable description of the object to stderr. */
352 /* p points to somewhere inside an object with the debugging info. */
353 STATIC void GC_print_obj(ptr_t p)
355 oh * ohdr = (oh *)GC_base(p);
360 char buffer[GC_TYPE_DESCR_LEN + 1];
362 GC_ASSERT(I_DONT_HOLD_LOCK());
364 if (!ohdr) ABORT("Invalid GC_print_obj argument");
367 q = (ptr_t)(ohdr + 1);
368 /* Print a type description for the object whose client-visible */
370 hhdr = GC_find_header(q);
371 kind = hhdr -> hb_obj_kind;
372 if (0 != GC_describe_type_fns[kind] && GC_is_marked(ohdr)) {
373 /* This should preclude free list objects except with */
374 /* thread-local allocation. */
375 buffer[GC_TYPE_DESCR_LEN] = 0;
376 (GC_describe_type_fns[kind])(q, buffer);
377 GC_ASSERT(buffer[GC_TYPE_DESCR_LEN] == 0);
382 kind_str = "PTRFREE";
388 kind_str = "UNCOLLECTABLE";
390 # ifdef GC_ATOMIC_UNCOLLECTABLE
392 kind_str = "ATOMIC_UNCOLLECTABLE";
396 kind_str = "STUBBORN";
400 /* The alternative is to use snprintf(buffer) but it is */
401 /* not quite portable (see vsnprintf in misc.c). */
405 if (NULL != kind_str) {
406 GC_err_printf("%p (%s:%d," IF_NOT_SHORTDBG_HDRS(" sz=%lu,") " %s)\n",
407 (void *)((ptr_t)ohdr + sizeof(oh)),
408 ohdr->oh_string, GET_OH_LINENUM(ohdr) /*, */
409 COMMA_IFNOT_SHORTDBG_HDRS((unsigned long)ohdr->oh_sz),
412 GC_err_printf("%p (%s:%d," IF_NOT_SHORTDBG_HDRS(" sz=%lu,")
413 " kind=%d descr=0x%lx)\n",
414 (void *)((ptr_t)ohdr + sizeof(oh)),
415 ohdr->oh_string, GET_OH_LINENUM(ohdr) /*, */
416 COMMA_IFNOT_SHORTDBG_HDRS((unsigned long)ohdr->oh_sz),
417 kind, (unsigned long)hhdr->hb_descr);
419 PRINT_CALL_CHAIN(ohdr);
422 STATIC void GC_debug_print_heap_obj_proc(ptr_t p)
424 GC_ASSERT(I_DONT_HOLD_LOCK());
425 if (GC_HAS_DEBUG_INFO(p)) {
428 GC_default_print_heap_obj_proc(p);
432 #ifndef SHORT_DBG_HDRS
433 /* Use GC_err_printf and friends to print a description of the object */
434 /* whose client-visible address is p, and which was smashed at */
435 /* clobbered_addr. */
436 STATIC void GC_print_smashed_obj(const char *msg, ptr_t p,
437 ptr_t clobbered_addr)
439 oh * ohdr = (oh *)GC_base(p);
441 GC_ASSERT(I_DONT_HOLD_LOCK());
443 if (!ohdr) ABORT("Invalid GC_print_smashed_obj argument");
445 if ((word)clobbered_addr <= (word)(&ohdr->oh_sz)
446 || ohdr -> oh_string == 0) {
448 "%s %p in or near object at %p(<smashed>, appr. sz = %lu)\n",
449 msg, (void *)clobbered_addr, (void *)p,
450 (unsigned long)(GC_size((ptr_t)ohdr) - DEBUG_BYTES));
452 GC_err_printf("%s %p in or near object at %p (%s:%d, sz=%lu)\n",
453 msg, (void *)clobbered_addr, (void *)p,
454 (word)(ohdr -> oh_string) < HBLKSIZE ? "(smashed string)" :
455 ohdr -> oh_string[0] == '\0' ? "EMPTY(smashed?)" :
457 GET_OH_LINENUM(ohdr), (unsigned long)(ohdr -> oh_sz));
458 PRINT_CALL_CHAIN(ohdr);
462 STATIC void GC_check_heap_proc (void);
463 STATIC void GC_print_all_smashed_proc (void);
465 STATIC void GC_do_nothing(void) {}
468 STATIC void GC_start_debugging_inner(void)
470 GC_ASSERT(I_HOLD_LOCK());
471 # ifndef SHORT_DBG_HDRS
472 GC_check_heap = GC_check_heap_proc;
473 GC_print_all_smashed = GC_print_all_smashed_proc;
475 GC_check_heap = GC_do_nothing;
476 GC_print_all_smashed = GC_do_nothing;
478 GC_print_heap_obj = GC_debug_print_heap_obj_proc;
479 GC_debugging_started = TRUE;
480 GC_register_displacement_inner((word)sizeof(oh));
483 GC_INNER void GC_start_debugging(void)
488 GC_start_debugging_inner();
492 size_t GC_debug_header_size = sizeof(oh);
494 GC_API void GC_CALL GC_debug_register_displacement(size_t offset)
499 GC_register_displacement_inner(offset);
500 GC_register_displacement_inner((word)sizeof(oh) + offset);
505 # if defined(HAVE_DLADDR) && defined(GC_HAVE_RETURN_ADDR_PARENT)
508 STATIC void GC_caller_func_offset(word ad, const char **symp, int *offp)
512 if (ad && dladdr((void *)ad, &caller) && caller.dli_sname != NULL) {
513 *symp = caller.dli_sname;
514 *offp = (int)((char *)ad - (char *)caller.dli_saddr);
521 # define GC_caller_func_offset(ad, symp, offp) (void)(*(symp) = "unknown")
523 #endif /* GC_ADD_CALLER */
525 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc(size_t lb,
530 /* Note that according to malloc() specification, if size is 0 then */
531 /* malloc() returns either NULL, or a unique pointer value that can */
532 /* later be successfully passed to free(). We always do the latter. */
533 result = GC_malloc(SIZET_SAT_ADD(lb, DEBUG_BYTES));
534 # ifdef GC_ADD_CALLER
536 GC_caller_func_offset(ra, &s, &i);
540 GC_err_printf("GC_debug_malloc(%lu) returning NULL (%s:%d)\n",
541 (unsigned long)lb, s, i);
544 if (!GC_debugging_started) {
545 GC_start_debugging();
547 ADD_CALL_CHAIN(result, ra);
548 return (GC_store_debug_info(result, (word)lb, s, i));
551 GC_API GC_ATTR_MALLOC void * GC_CALL
552 GC_debug_malloc_ignore_off_page(size_t lb, GC_EXTRA_PARAMS)
554 void * result = GC_malloc_ignore_off_page(SIZET_SAT_ADD(lb, DEBUG_BYTES));
557 GC_err_printf("GC_debug_malloc_ignore_off_page(%lu)"
558 " returning NULL (%s:%d)\n", (unsigned long)lb, s, i);
561 if (!GC_debugging_started) {
562 GC_start_debugging();
564 ADD_CALL_CHAIN(result, ra);
565 return (GC_store_debug_info(result, (word)lb, s, i));
568 GC_API GC_ATTR_MALLOC void * GC_CALL
569 GC_debug_malloc_atomic_ignore_off_page(size_t lb, GC_EXTRA_PARAMS)
571 void * result = GC_malloc_atomic_ignore_off_page(
572 SIZET_SAT_ADD(lb, DEBUG_BYTES));
575 GC_err_printf("GC_debug_malloc_atomic_ignore_off_page(%lu)"
576 " returning NULL (%s:%d)\n", (unsigned long)lb, s, i);
579 if (!GC_debugging_started) {
580 GC_start_debugging();
582 ADD_CALL_CHAIN(result, ra);
583 return (GC_store_debug_info(result, (word)lb, s, i));
586 STATIC void * GC_debug_generic_malloc(size_t lb, int knd, GC_EXTRA_PARAMS)
588 void * result = GC_generic_malloc(SIZET_SAT_ADD(lb, DEBUG_BYTES), knd);
590 if (NULL == result) {
592 "GC_debug_generic_malloc(%lu, %d) returning NULL (%s:%d)\n",
593 (unsigned long)lb, knd, s, i);
596 if (!GC_debugging_started) {
597 GC_start_debugging();
599 ADD_CALL_CHAIN(result, ra);
600 return GC_store_debug_info(result, (word)lb, s, i);
604 /* An allocation function for internal use. Normally internally */
605 /* allocated objects do not have debug information. But in this */
606 /* case, we need to make sure that all objects have debug headers. */
607 /* We assume debugging was started in collector initialization, and */
608 /* we already hold the GC lock. */
609 GC_INNER void * GC_debug_generic_malloc_inner(size_t lb, int k)
611 void * result = GC_generic_malloc_inner(
612 SIZET_SAT_ADD(lb, DEBUG_BYTES), k);
615 GC_err_printf("GC internal allocation (%lu bytes) returning NULL\n",
619 if (!GC_debugging_started) {
620 GC_start_debugging_inner();
622 ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
623 return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", 0));
626 GC_INNER void * GC_debug_generic_malloc_inner_ignore_off_page(size_t lb,
629 void * result = GC_generic_malloc_inner_ignore_off_page(
630 SIZET_SAT_ADD(lb, DEBUG_BYTES), k);
633 GC_err_printf("GC internal allocation (%lu bytes) returning NULL\n",
637 if (!GC_debugging_started) {
638 GC_start_debugging_inner();
640 ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
641 return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", 0));
643 #endif /* DBG_HDRS_ALL */
645 #ifdef STUBBORN_ALLOC
646 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_stubborn(size_t lb,
649 void * result = GC_malloc_stubborn(SIZET_SAT_ADD(lb, DEBUG_BYTES));
652 GC_err_printf("GC_debug_malloc_stubborn(%lu)"
653 " returning NULL (%s:%d)\n", (unsigned long)lb, s, i);
656 if (!GC_debugging_started) {
657 GC_start_debugging();
659 ADD_CALL_CHAIN(result, ra);
660 return (GC_store_debug_info(result, (word)lb, s, i));
663 GC_API void GC_CALL GC_debug_change_stubborn(const void *p)
665 const void * q = GC_base_C(p);
669 ABORT_ARG1("GC_debug_change_stubborn: bad arg", ": %p", p);
672 if (hhdr -> hb_obj_kind != STUBBORN) {
673 ABORT_ARG1("GC_debug_change_stubborn: arg not stubborn", ": %p", p);
675 GC_change_stubborn(q);
678 GC_API void GC_CALL GC_debug_end_stubborn_change(const void *p)
680 const void * q = GC_base_C(p);
684 ABORT_ARG1("GC_debug_end_stubborn_change: bad arg", ": %p", p);
687 if (hhdr -> hb_obj_kind != STUBBORN) {
688 ABORT_ARG1("GC_debug_end_stubborn_change: arg not stubborn",
691 GC_end_stubborn_change(q);
694 #else /* !STUBBORN_ALLOC */
696 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_stubborn(size_t lb,
699 return GC_debug_malloc(lb, OPT_RA s, i);
702 GC_API void GC_CALL GC_debug_change_stubborn(
703 const void * p GC_ATTR_UNUSED) {}
705 GC_API void GC_CALL GC_debug_end_stubborn_change(
706 const void * p GC_ATTR_UNUSED) {}
707 #endif /* !STUBBORN_ALLOC */
709 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_atomic(size_t lb,
712 void * result = GC_malloc_atomic(SIZET_SAT_ADD(lb, DEBUG_BYTES));
715 GC_err_printf("GC_debug_malloc_atomic(%lu) returning NULL (%s:%d)\n",
716 (unsigned long)lb, s, i);
719 if (!GC_debugging_started) {
720 GC_start_debugging();
722 ADD_CALL_CHAIN(result, ra);
723 return (GC_store_debug_info(result, (word)lb, s, i));
726 GC_API GC_ATTR_MALLOC char * GC_CALL GC_debug_strdup(const char *str,
733 GC_err_printf("strdup(NULL) behavior is undefined\n");
737 lb = strlen(str) + 1;
738 copy = GC_debug_malloc_atomic(lb, OPT_RA s, i);
745 BCOPY(str, copy, lb);
749 GC_API GC_ATTR_MALLOC char * GC_CALL GC_debug_strndup(const char *str,
750 size_t size, GC_EXTRA_PARAMS)
753 size_t len = strlen(str); /* str is expected to be non-NULL */
756 copy = GC_debug_malloc_atomic(len + 1, OPT_RA s, i);
763 BCOPY(str, copy, len);
768 #ifdef GC_REQUIRE_WCSDUP
769 # include <wchar.h> /* for wcslen() */
771 GC_API GC_ATTR_MALLOC wchar_t * GC_CALL GC_debug_wcsdup(const wchar_t *str,
774 size_t lb = (wcslen(str) + 1) * sizeof(wchar_t);
775 wchar_t *copy = GC_debug_malloc_atomic(lb, OPT_RA s, i);
782 BCOPY(str, copy, lb);
785 #endif /* GC_REQUIRE_WCSDUP */
787 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_uncollectable(size_t lb,
790 void * result = GC_malloc_uncollectable(
791 SIZET_SAT_ADD(lb, UNCOLLECTABLE_DEBUG_BYTES));
794 GC_err_printf("GC_debug_malloc_uncollectable(%lu)"
795 " returning NULL (%s:%d)\n", (unsigned long)lb, s, i);
798 if (!GC_debugging_started) {
799 GC_start_debugging();
801 ADD_CALL_CHAIN(result, ra);
802 return (GC_store_debug_info(result, (word)lb, s, i));
805 #ifdef GC_ATOMIC_UNCOLLECTABLE
806 GC_API GC_ATTR_MALLOC void * GC_CALL
807 GC_debug_malloc_atomic_uncollectable(size_t lb, GC_EXTRA_PARAMS)
809 void * result = GC_malloc_atomic_uncollectable(
810 SIZET_SAT_ADD(lb, UNCOLLECTABLE_DEBUG_BYTES));
813 GC_err_printf("GC_debug_malloc_atomic_uncollectable(%lu)"
814 " returning NULL (%s:%d)\n", (unsigned long)lb, s, i);
817 if (!GC_debugging_started) {
818 GC_start_debugging();
820 ADD_CALL_CHAIN(result, ra);
821 return (GC_store_debug_info(result, (word)lb, s, i));
823 #endif /* GC_ATOMIC_UNCOLLECTABLE */
825 #ifndef GC_FREED_MEM_MARKER
826 # if CPP_WORDSZ == 32
827 # define GC_FREED_MEM_MARKER 0xdeadbeef
829 # define GC_FREED_MEM_MARKER GC_WORD_C(0xEFBEADDEdeadbeef)
833 GC_API void GC_CALL GC_debug_free(void * p)
840 # if defined(REDIRECT_MALLOC) \
841 && ((defined(NEED_CALLINFO) && defined(GC_HAVE_BUILTIN_BACKTRACE)) \
842 || defined(GC_LINUX_THREADS) || defined(GC_SOLARIS_THREADS) \
844 /* In some cases, we should ignore objects that do not belong */
845 /* to the GC heap. See the comment in GC_free. */
846 if (!GC_is_heap_ptr(p)) return;
848 ABORT_ARG1("Invalid pointer passed to free()", ": %p", p);
850 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
852 "GC_debug_free called on pointer %p w/o debugging info\n", p);
854 # ifndef SHORT_DBG_HDRS
855 ptr_t clobbered = GC_check_annotated_obj((oh *)base);
856 word sz = GC_size(base);
857 if (clobbered != 0) {
858 GC_have_errors = TRUE;
859 if (((oh *)base) -> oh_sz == sz) {
860 GC_print_smashed_obj(
861 "GC_debug_free: found previously deallocated (?) object at",
863 return; /* ignore double free */
865 GC_print_smashed_obj("GC_debug_free: found smashed location at",
869 /* Invalidate size (mark the object as deallocated) */
870 ((oh *)base) -> oh_sz = sz;
871 # endif /* SHORT_DBG_HDRS */
874 # ifndef SHORT_DBG_HDRS
875 && ((ptr_t)p - (ptr_t)base != sizeof(oh) || !GC_findleak_delay_free)
881 if (hhdr -> hb_obj_kind == UNCOLLECTABLE
882 # ifdef GC_ATOMIC_UNCOLLECTABLE
883 || hhdr -> hb_obj_kind == AUNCOLLECTABLE
889 size_t obj_sz = BYTES_TO_WORDS(hhdr -> hb_sz - sizeof(oh));
891 for (i = 0; i < obj_sz; ++i)
892 ((word *)p)[i] = GC_FREED_MEM_MARKER;
893 GC_ASSERT((word *)p + i == (word *)(base + hhdr -> hb_sz));
895 } /* !GC_find_leak */
898 #if defined(THREADS) && defined(DBG_HDRS_ALL)
899 /* Used internally; we assume it's called correctly. */
900 GC_INNER void GC_debug_free_inner(void * p)
902 ptr_t base = GC_base(p);
903 GC_ASSERT((ptr_t)p - (ptr_t)base == sizeof(oh));
905 if (!base) ABORT("Invalid GC_debug_free_inner argument");
907 # ifndef SHORT_DBG_HDRS
908 /* Invalidate size */
909 ((oh *)base) -> oh_sz = GC_size(base);
915 GC_API void * GC_CALL GC_debug_realloc(void * p, size_t lb, GC_EXTRA_PARAMS)
922 return GC_debug_malloc(lb, OPT_RA s, i);
924 if (0 == lb) /* and p != NULL */ {
929 # ifdef GC_ADD_CALLER
931 GC_caller_func_offset(ra, &s, &i);
936 ABORT_ARG1("Invalid pointer passed to realloc()", ": %p", p);
938 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
940 "GC_debug_realloc called on pointer %p w/o debugging info\n", p);
941 return(GC_realloc(p, lb));
944 switch (hhdr -> hb_obj_kind) {
945 # ifdef STUBBORN_ALLOC
947 result = GC_debug_malloc_stubborn(lb, OPT_RA s, i);
951 result = GC_debug_malloc(lb, OPT_RA s, i);
954 result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
957 result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
959 # ifdef GC_ATOMIC_UNCOLLECTABLE
961 result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
965 result = NULL; /* initialized to prevent warning. */
966 ABORT_RET("GC_debug_realloc: encountered bad kind");
969 if (result != NULL) {
971 # ifdef SHORT_DBG_HDRS
972 old_sz = GC_size(base) - sizeof(oh);
974 old_sz = ((oh *)base) -> oh_sz;
976 BCOPY(p, result, old_sz < lb ? old_sz : lb);
982 GC_API GC_ATTR_MALLOC void * GC_CALL
983 GC_debug_generic_or_special_malloc(size_t lb, int knd, GC_EXTRA_PARAMS)
986 # ifdef STUBBORN_ALLOC
988 return GC_debug_malloc_stubborn(lb, OPT_RA s, i);
991 return GC_debug_malloc_atomic(lb, OPT_RA s, i);
993 return GC_debug_malloc(lb, OPT_RA s, i);
995 return GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
996 # ifdef GC_ATOMIC_UNCOLLECTABLE
998 return GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
1001 return GC_debug_generic_malloc(lb, knd, OPT_RA s, i);
1005 #ifndef SHORT_DBG_HDRS
1007 /* List of smashed (clobbered) locations. We defer printing these, */
1008 /* since we can't always print them nicely with the allocation lock */
1009 /* held. We put them here instead of in GC_arrays, since it may be */
1010 /* useful to be able to look at them with the debugger. */
1012 # define MAX_SMASHED 20
1014 STATIC ptr_t GC_smashed[MAX_SMASHED] = {0};
1015 STATIC unsigned GC_n_smashed = 0;
1017 STATIC void GC_add_smashed(ptr_t smashed)
1019 GC_ASSERT(GC_is_marked(GC_base(smashed)));
1020 /* FIXME: Prevent adding an object while printing smashed list. */
1021 GC_smashed[GC_n_smashed] = smashed;
1022 if (GC_n_smashed < MAX_SMASHED - 1) ++GC_n_smashed;
1023 /* In case of overflow, we keep the first MAX_SMASHED-1 */
1024 /* entries plus the last one. */
1025 GC_have_errors = TRUE;
1028 /* Print all objects on the list. Clear the list. */
1029 STATIC void GC_print_all_smashed_proc(void)
1033 GC_ASSERT(I_DONT_HOLD_LOCK());
1034 if (GC_n_smashed == 0) return;
1035 GC_err_printf("GC_check_heap_block: found %u smashed heap objects:\n",
1037 for (i = 0; i < GC_n_smashed; ++i) {
1038 ptr_t base = (ptr_t)GC_base(GC_smashed[i]);
1041 if (!base) ABORT("Invalid GC_smashed element");
1043 GC_print_smashed_obj("", base + sizeof(oh), GC_smashed[i]);
1049 /* Check all marked objects in the given block for validity */
1050 /* Avoid GC_apply_to_each_object for performance reasons. */
1051 STATIC void GC_check_heap_block(struct hblk *hbp, word dummy GC_ATTR_UNUSED)
1053 struct hblkhdr * hhdr = HDR(hbp);
1054 size_t sz = hhdr -> hb_sz;
1059 if (sz > MAXOBJBYTES) {
1062 plim = hbp->hb_body + HBLKSIZE - sz;
1064 /* go through all words in block */
1065 for (bit_no = 0; (word)p <= (word)plim;
1066 bit_no += MARK_BIT_OFFSET(sz), p += sz) {
1067 if (mark_bit_from_hdr(hhdr, bit_no) && GC_HAS_DEBUG_INFO((ptr_t)p)) {
1068 ptr_t clobbered = GC_check_annotated_obj((oh *)p);
1070 GC_add_smashed(clobbered);
1075 /* This assumes that all accessible objects are marked, and that */
1076 /* I hold the allocation lock. Normally called by collector. */
1077 STATIC void GC_check_heap_proc(void)
1079 GC_STATIC_ASSERT((sizeof(oh) & (GRANULE_BYTES - 1)) == 0);
1080 /* FIXME: Should we check for twice that alignment? */
1081 GC_apply_to_all_blocks(GC_check_heap_block, 0);
1084 GC_INNER GC_bool GC_check_leaked(ptr_t base)
1091 # if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
1092 (*(word *)base & 1) != 0 &&
1094 GC_has_other_debug_info(base) >= 0)
1095 return TRUE; /* object has leaked */
1097 /* Validate freed object's content. */
1098 p = (word *)(base + sizeof(oh));
1099 obj_sz = BYTES_TO_WORDS(HDR(base)->hb_sz - sizeof(oh));
1100 for (i = 0; i < obj_sz; ++i)
1101 if (p[i] != GC_FREED_MEM_MARKER) {
1102 GC_set_mark_bit(base); /* do not reclaim it in this cycle */
1103 GC_add_smashed((ptr_t)(&p[i])); /* alter-after-free detected */
1104 break; /* don't report any other smashed locations in the object */
1107 return FALSE; /* GC_debug_free() has been called */
1110 #endif /* !SHORT_DBG_HDRS */
1112 #ifndef GC_NO_FINALIZATION
1115 GC_finalization_proc cl_fn;
1119 STATIC void * GC_make_closure(GC_finalization_proc fn, void * data)
1121 struct closure * result =
1122 # ifdef DBG_HDRS_ALL
1123 (struct closure *) GC_debug_malloc(sizeof (struct closure),
1126 (struct closure *) GC_malloc(sizeof (struct closure));
1129 result -> cl_fn = fn;
1130 result -> cl_data = data;
1132 return((void *)result);
1135 /* An auxiliary fns to make finalization work correctly with displaced */
1136 /* pointers introduced by the debugging allocators. */
1137 STATIC void GC_CALLBACK GC_debug_invoke_finalizer(void * obj, void * data)
1139 struct closure * cl = (struct closure *) data;
1140 (*(cl -> cl_fn))((void *)((char *)obj + sizeof(oh)), cl -> cl_data);
1143 /* Special finalizer_proc value to detect GC_register_finalizer() failure. */
1144 #define OFN_UNSET ((GC_finalization_proc)~(signed_word)0)
1146 /* Set ofn and ocd to reflect the values we got back. */
1147 static void store_old(void *obj, GC_finalization_proc my_old_fn,
1148 struct closure *my_old_cd, GC_finalization_proc *ofn,
1151 if (0 != my_old_fn) {
1152 if (my_old_fn == OFN_UNSET) {
1153 /* register_finalizer() failed; (*ofn) and (*ocd) are unchanged. */
1156 if (my_old_fn != GC_debug_invoke_finalizer) {
1157 GC_err_printf("Debuggable object at %p had a non-debug finalizer\n",
1159 /* This should probably be fatal. */
1161 if (ofn) *ofn = my_old_cd -> cl_fn;
1162 if (ocd) *ocd = my_old_cd -> cl_data;
1170 GC_API void GC_CALL GC_debug_register_finalizer(void * obj,
1171 GC_finalization_proc fn,
1172 void * cd, GC_finalization_proc *ofn,
1175 GC_finalization_proc my_old_fn = OFN_UNSET;
1177 ptr_t base = GC_base(obj);
1179 /* We won't collect it, hence finalizer wouldn't be run. */
1184 if ((ptr_t)obj - base != sizeof(oh)) {
1185 GC_err_printf("GC_debug_register_finalizer called with"
1186 " non-base-pointer %p\n", obj);
1189 GC_register_finalizer(base, 0, 0, &my_old_fn, &my_old_cd);
1191 cd = GC_make_closure(fn, cd);
1192 if (cd == 0) return; /* out of memory */
1193 GC_register_finalizer(base, GC_debug_invoke_finalizer,
1194 cd, &my_old_fn, &my_old_cd);
1196 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1199 GC_API void GC_CALL GC_debug_register_finalizer_no_order
1200 (void * obj, GC_finalization_proc fn,
1201 void * cd, GC_finalization_proc *ofn,
1204 GC_finalization_proc my_old_fn = OFN_UNSET;
1206 ptr_t base = GC_base(obj);
1208 /* We won't collect it, hence finalizer wouldn't be run. */
1213 if ((ptr_t)obj - base != sizeof(oh)) {
1214 GC_err_printf("GC_debug_register_finalizer_no_order called with"
1215 " non-base-pointer %p\n", obj);
1218 GC_register_finalizer_no_order(base, 0, 0, &my_old_fn, &my_old_cd);
1220 cd = GC_make_closure(fn, cd);
1221 if (cd == 0) return; /* out of memory */
1222 GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
1223 cd, &my_old_fn, &my_old_cd);
1225 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1228 GC_API void GC_CALL GC_debug_register_finalizer_unreachable
1229 (void * obj, GC_finalization_proc fn,
1230 void * cd, GC_finalization_proc *ofn,
1233 GC_finalization_proc my_old_fn = OFN_UNSET;
1235 ptr_t base = GC_base(obj);
1237 /* We won't collect it, hence finalizer wouldn't be run. */
1242 if ((ptr_t)obj - base != sizeof(oh)) {
1243 GC_err_printf("GC_debug_register_finalizer_unreachable called with"
1244 " non-base-pointer %p\n", obj);
1247 GC_register_finalizer_unreachable(base, 0, 0, &my_old_fn, &my_old_cd);
1249 cd = GC_make_closure(fn, cd);
1250 if (cd == 0) return; /* out of memory */
1251 GC_register_finalizer_unreachable(base, GC_debug_invoke_finalizer,
1252 cd, &my_old_fn, &my_old_cd);
1254 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1257 GC_API void GC_CALL GC_debug_register_finalizer_ignore_self
1258 (void * obj, GC_finalization_proc fn,
1259 void * cd, GC_finalization_proc *ofn,
1262 GC_finalization_proc my_old_fn = OFN_UNSET;
1264 ptr_t base = GC_base(obj);
1266 /* We won't collect it, hence finalizer wouldn't be run. */
1271 if ((ptr_t)obj - base != sizeof(oh)) {
1272 GC_err_printf("GC_debug_register_finalizer_ignore_self called with"
1273 " non-base-pointer %p\n", obj);
1276 GC_register_finalizer_ignore_self(base, 0, 0, &my_old_fn, &my_old_cd);
1278 cd = GC_make_closure(fn, cd);
1279 if (cd == 0) return; /* out of memory */
1280 GC_register_finalizer_ignore_self(base, GC_debug_invoke_finalizer,
1281 cd, &my_old_fn, &my_old_cd);
1283 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1286 #endif /* !GC_NO_FINALIZATION */
1288 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_replacement(size_t lb)
1290 return GC_debug_malloc(lb, GC_DBG_EXTRAS);
1293 GC_API void * GC_CALL GC_debug_realloc_replacement(void *p, size_t lb)
1295 return GC_debug_realloc(p, lb, GC_DBG_EXTRAS);