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);
764 BCOPY(str, copy, len);
769 #ifdef GC_REQUIRE_WCSDUP
770 # include <wchar.h> /* for wcslen() */
772 GC_API GC_ATTR_MALLOC wchar_t * GC_CALL GC_debug_wcsdup(const wchar_t *str,
775 size_t lb = (wcslen(str) + 1) * sizeof(wchar_t);
776 wchar_t *copy = GC_debug_malloc_atomic(lb, OPT_RA s, i);
783 BCOPY(str, copy, lb);
786 #endif /* GC_REQUIRE_WCSDUP */
788 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_uncollectable(size_t lb,
791 void * result = GC_malloc_uncollectable(
792 SIZET_SAT_ADD(lb, UNCOLLECTABLE_DEBUG_BYTES));
795 GC_err_printf("GC_debug_malloc_uncollectable(%lu)"
796 " returning NULL (%s:%d)\n", (unsigned long)lb, s, i);
799 if (!GC_debugging_started) {
800 GC_start_debugging();
802 ADD_CALL_CHAIN(result, ra);
803 return (GC_store_debug_info(result, (word)lb, s, i));
806 #ifdef GC_ATOMIC_UNCOLLECTABLE
807 GC_API GC_ATTR_MALLOC void * GC_CALL
808 GC_debug_malloc_atomic_uncollectable(size_t lb, GC_EXTRA_PARAMS)
810 void * result = GC_malloc_atomic_uncollectable(
811 SIZET_SAT_ADD(lb, UNCOLLECTABLE_DEBUG_BYTES));
814 GC_err_printf("GC_debug_malloc_atomic_uncollectable(%lu)"
815 " returning NULL (%s:%d)\n", (unsigned long)lb, s, i);
818 if (!GC_debugging_started) {
819 GC_start_debugging();
821 ADD_CALL_CHAIN(result, ra);
822 return (GC_store_debug_info(result, (word)lb, s, i));
824 #endif /* GC_ATOMIC_UNCOLLECTABLE */
826 #ifndef GC_FREED_MEM_MARKER
827 # if CPP_WORDSZ == 32
828 # define GC_FREED_MEM_MARKER 0xdeadbeef
830 # define GC_FREED_MEM_MARKER GC_WORD_C(0xEFBEADDEdeadbeef)
834 GC_API void GC_CALL GC_debug_free(void * p)
841 # if defined(REDIRECT_MALLOC) \
842 && ((defined(NEED_CALLINFO) && defined(GC_HAVE_BUILTIN_BACKTRACE)) \
843 || defined(GC_LINUX_THREADS) || defined(GC_SOLARIS_THREADS) \
845 /* In some cases, we should ignore objects that do not belong */
846 /* to the GC heap. See the comment in GC_free. */
847 if (!GC_is_heap_ptr(p)) return;
849 ABORT_ARG1("Invalid pointer passed to free()", ": %p", p);
851 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
853 "GC_debug_free called on pointer %p w/o debugging info\n", p);
855 # ifndef SHORT_DBG_HDRS
856 ptr_t clobbered = GC_check_annotated_obj((oh *)base);
857 word sz = GC_size(base);
858 if (clobbered != 0) {
859 GC_have_errors = TRUE;
860 if (((oh *)base) -> oh_sz == sz) {
861 GC_print_smashed_obj(
862 "GC_debug_free: found previously deallocated (?) object at",
864 return; /* ignore double free */
866 GC_print_smashed_obj("GC_debug_free: found smashed location at",
870 /* Invalidate size (mark the object as deallocated) */
871 ((oh *)base) -> oh_sz = sz;
872 # endif /* SHORT_DBG_HDRS */
875 # ifndef SHORT_DBG_HDRS
876 && ((ptr_t)p - (ptr_t)base != sizeof(oh) || !GC_findleak_delay_free)
882 if (hhdr -> hb_obj_kind == UNCOLLECTABLE
883 # ifdef GC_ATOMIC_UNCOLLECTABLE
884 || hhdr -> hb_obj_kind == AUNCOLLECTABLE
890 size_t obj_sz = BYTES_TO_WORDS(hhdr -> hb_sz - sizeof(oh));
892 for (i = 0; i < obj_sz; ++i)
893 ((word *)p)[i] = GC_FREED_MEM_MARKER;
894 GC_ASSERT((word *)p + i == (word *)(base + hhdr -> hb_sz));
896 } /* !GC_find_leak */
899 #if defined(THREADS) && defined(DBG_HDRS_ALL)
900 /* Used internally; we assume it's called correctly. */
901 GC_INNER void GC_debug_free_inner(void * p)
903 ptr_t base = GC_base(p);
904 GC_ASSERT((ptr_t)p - (ptr_t)base == sizeof(oh));
906 if (!base) ABORT("Invalid GC_debug_free_inner argument");
908 # ifndef SHORT_DBG_HDRS
909 /* Invalidate size */
910 ((oh *)base) -> oh_sz = GC_size(base);
916 GC_API void * GC_CALL GC_debug_realloc(void * p, size_t lb, GC_EXTRA_PARAMS)
923 return GC_debug_malloc(lb, OPT_RA s, i);
925 if (0 == lb) /* and p != NULL */ {
930 # ifdef GC_ADD_CALLER
932 GC_caller_func_offset(ra, &s, &i);
937 ABORT_ARG1("Invalid pointer passed to realloc()", ": %p", p);
939 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
941 "GC_debug_realloc called on pointer %p w/o debugging info\n", p);
942 return(GC_realloc(p, lb));
945 switch (hhdr -> hb_obj_kind) {
946 # ifdef STUBBORN_ALLOC
948 result = GC_debug_malloc_stubborn(lb, OPT_RA s, i);
952 result = GC_debug_malloc(lb, OPT_RA s, i);
955 result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
958 result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
960 # ifdef GC_ATOMIC_UNCOLLECTABLE
962 result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
966 result = NULL; /* initialized to prevent warning. */
967 ABORT_RET("GC_debug_realloc: encountered bad kind");
970 if (result != NULL) {
972 # ifdef SHORT_DBG_HDRS
973 old_sz = GC_size(base) - sizeof(oh);
975 old_sz = ((oh *)base) -> oh_sz;
978 BCOPY(p, result, old_sz < lb ? old_sz : lb);
984 GC_API GC_ATTR_MALLOC void * GC_CALL
985 GC_debug_generic_or_special_malloc(size_t lb, int knd, GC_EXTRA_PARAMS)
988 # ifdef STUBBORN_ALLOC
990 return GC_debug_malloc_stubborn(lb, OPT_RA s, i);
993 return GC_debug_malloc_atomic(lb, OPT_RA s, i);
995 return GC_debug_malloc(lb, OPT_RA s, i);
997 return GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
998 # ifdef GC_ATOMIC_UNCOLLECTABLE
1000 return GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
1003 return GC_debug_generic_malloc(lb, knd, OPT_RA s, i);
1007 #ifndef SHORT_DBG_HDRS
1009 /* List of smashed (clobbered) locations. We defer printing these, */
1010 /* since we can't always print them nicely with the allocation lock */
1011 /* held. We put them here instead of in GC_arrays, since it may be */
1012 /* useful to be able to look at them with the debugger. */
1014 # define MAX_SMASHED 20
1016 STATIC ptr_t GC_smashed[MAX_SMASHED] = {0};
1017 STATIC unsigned GC_n_smashed = 0;
1019 STATIC void GC_add_smashed(ptr_t smashed)
1021 GC_ASSERT(GC_is_marked(GC_base(smashed)));
1022 /* FIXME: Prevent adding an object while printing smashed list. */
1023 GC_smashed[GC_n_smashed] = smashed;
1024 if (GC_n_smashed < MAX_SMASHED - 1) ++GC_n_smashed;
1025 /* In case of overflow, we keep the first MAX_SMASHED-1 */
1026 /* entries plus the last one. */
1027 GC_have_errors = TRUE;
1030 /* Print all objects on the list. Clear the list. */
1031 STATIC void GC_print_all_smashed_proc(void)
1035 GC_ASSERT(I_DONT_HOLD_LOCK());
1036 if (GC_n_smashed == 0) return;
1037 GC_err_printf("GC_check_heap_block: found %u smashed heap objects:\n",
1039 for (i = 0; i < GC_n_smashed; ++i) {
1040 ptr_t base = (ptr_t)GC_base(GC_smashed[i]);
1043 if (!base) ABORT("Invalid GC_smashed element");
1045 GC_print_smashed_obj("", base + sizeof(oh), GC_smashed[i]);
1051 /* Check all marked objects in the given block for validity */
1052 /* Avoid GC_apply_to_each_object for performance reasons. */
1053 STATIC void GC_check_heap_block(struct hblk *hbp, word dummy GC_ATTR_UNUSED)
1055 struct hblkhdr * hhdr = HDR(hbp);
1056 size_t sz = hhdr -> hb_sz;
1061 if (sz > MAXOBJBYTES) {
1064 plim = hbp->hb_body + HBLKSIZE - sz;
1066 /* go through all words in block */
1067 for (bit_no = 0; (word)p <= (word)plim;
1068 bit_no += MARK_BIT_OFFSET(sz), p += sz) {
1069 if (mark_bit_from_hdr(hhdr, bit_no) && GC_HAS_DEBUG_INFO((ptr_t)p)) {
1070 ptr_t clobbered = GC_check_annotated_obj((oh *)p);
1072 GC_add_smashed(clobbered);
1077 /* This assumes that all accessible objects are marked, and that */
1078 /* I hold the allocation lock. Normally called by collector. */
1079 STATIC void GC_check_heap_proc(void)
1081 GC_STATIC_ASSERT((sizeof(oh) & (GRANULE_BYTES - 1)) == 0);
1082 /* FIXME: Should we check for twice that alignment? */
1083 GC_apply_to_all_blocks(GC_check_heap_block, 0);
1086 GC_INNER GC_bool GC_check_leaked(ptr_t base)
1093 # if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
1094 (*(word *)base & 1) != 0 &&
1096 GC_has_other_debug_info(base) >= 0)
1097 return TRUE; /* object has leaked */
1099 /* Validate freed object's content. */
1100 p = (word *)(base + sizeof(oh));
1101 obj_sz = BYTES_TO_WORDS(HDR(base)->hb_sz - sizeof(oh));
1102 for (i = 0; i < obj_sz; ++i)
1103 if (p[i] != GC_FREED_MEM_MARKER) {
1104 GC_set_mark_bit(base); /* do not reclaim it in this cycle */
1105 GC_add_smashed((ptr_t)(&p[i])); /* alter-after-free detected */
1106 break; /* don't report any other smashed locations in the object */
1109 return FALSE; /* GC_debug_free() has been called */
1112 #endif /* !SHORT_DBG_HDRS */
1114 #ifndef GC_NO_FINALIZATION
1117 GC_finalization_proc cl_fn;
1121 STATIC void * GC_make_closure(GC_finalization_proc fn, void * data)
1123 struct closure * result =
1124 # ifdef DBG_HDRS_ALL
1125 (struct closure *) GC_debug_malloc(sizeof (struct closure),
1128 (struct closure *) GC_malloc(sizeof (struct closure));
1131 result -> cl_fn = fn;
1132 result -> cl_data = data;
1134 return((void *)result);
1137 /* An auxiliary fns to make finalization work correctly with displaced */
1138 /* pointers introduced by the debugging allocators. */
1139 STATIC void GC_CALLBACK GC_debug_invoke_finalizer(void * obj, void * data)
1141 struct closure * cl = (struct closure *) data;
1142 (*(cl -> cl_fn))((void *)((char *)obj + sizeof(oh)), cl -> cl_data);
1145 /* Special finalizer_proc value to detect GC_register_finalizer() failure. */
1146 #define OFN_UNSET ((GC_finalization_proc)~(signed_word)0)
1148 /* Set ofn and ocd to reflect the values we got back. */
1149 static void store_old(void *obj, GC_finalization_proc my_old_fn,
1150 struct closure *my_old_cd, GC_finalization_proc *ofn,
1153 if (0 != my_old_fn) {
1154 if (my_old_fn == OFN_UNSET) {
1155 /* register_finalizer() failed; (*ofn) and (*ocd) are unchanged. */
1158 if (my_old_fn != GC_debug_invoke_finalizer) {
1159 GC_err_printf("Debuggable object at %p had a non-debug finalizer\n",
1161 /* This should probably be fatal. */
1163 if (ofn) *ofn = my_old_cd -> cl_fn;
1164 if (ocd) *ocd = my_old_cd -> cl_data;
1172 GC_API void GC_CALL GC_debug_register_finalizer(void * obj,
1173 GC_finalization_proc fn,
1174 void * cd, GC_finalization_proc *ofn,
1177 GC_finalization_proc my_old_fn = OFN_UNSET;
1179 ptr_t base = GC_base(obj);
1181 /* We won't collect it, hence finalizer wouldn't be run. */
1186 if ((ptr_t)obj - base != sizeof(oh)) {
1187 GC_err_printf("GC_debug_register_finalizer called with"
1188 " non-base-pointer %p\n", obj);
1191 GC_register_finalizer(base, 0, 0, &my_old_fn, &my_old_cd);
1193 cd = GC_make_closure(fn, cd);
1194 if (cd == 0) return; /* out of memory */
1195 GC_register_finalizer(base, GC_debug_invoke_finalizer,
1196 cd, &my_old_fn, &my_old_cd);
1198 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1201 GC_API void GC_CALL GC_debug_register_finalizer_no_order
1202 (void * obj, GC_finalization_proc fn,
1203 void * cd, GC_finalization_proc *ofn,
1206 GC_finalization_proc my_old_fn = OFN_UNSET;
1208 ptr_t base = GC_base(obj);
1210 /* We won't collect it, hence finalizer wouldn't be run. */
1215 if ((ptr_t)obj - base != sizeof(oh)) {
1216 GC_err_printf("GC_debug_register_finalizer_no_order called with"
1217 " non-base-pointer %p\n", obj);
1220 GC_register_finalizer_no_order(base, 0, 0, &my_old_fn, &my_old_cd);
1222 cd = GC_make_closure(fn, cd);
1223 if (cd == 0) return; /* out of memory */
1224 GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
1225 cd, &my_old_fn, &my_old_cd);
1227 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1230 GC_API void GC_CALL GC_debug_register_finalizer_unreachable
1231 (void * obj, GC_finalization_proc fn,
1232 void * cd, GC_finalization_proc *ofn,
1235 GC_finalization_proc my_old_fn = OFN_UNSET;
1237 ptr_t base = GC_base(obj);
1239 /* We won't collect it, hence finalizer wouldn't be run. */
1244 if ((ptr_t)obj - base != sizeof(oh)) {
1245 GC_err_printf("GC_debug_register_finalizer_unreachable called with"
1246 " non-base-pointer %p\n", obj);
1249 GC_register_finalizer_unreachable(base, 0, 0, &my_old_fn, &my_old_cd);
1251 cd = GC_make_closure(fn, cd);
1252 if (cd == 0) return; /* out of memory */
1253 GC_register_finalizer_unreachable(base, GC_debug_invoke_finalizer,
1254 cd, &my_old_fn, &my_old_cd);
1256 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1259 GC_API void GC_CALL GC_debug_register_finalizer_ignore_self
1260 (void * obj, GC_finalization_proc fn,
1261 void * cd, GC_finalization_proc *ofn,
1264 GC_finalization_proc my_old_fn = OFN_UNSET;
1266 ptr_t base = GC_base(obj);
1268 /* We won't collect it, hence finalizer wouldn't be run. */
1273 if ((ptr_t)obj - base != sizeof(oh)) {
1274 GC_err_printf("GC_debug_register_finalizer_ignore_self called with"
1275 " non-base-pointer %p\n", obj);
1278 GC_register_finalizer_ignore_self(base, 0, 0, &my_old_fn, &my_old_cd);
1280 cd = GC_make_closure(fn, cd);
1281 if (cd == 0) return; /* out of memory */
1282 GC_register_finalizer_ignore_self(base, GC_debug_invoke_finalizer,
1283 cd, &my_old_fn, &my_old_cd);
1285 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1288 #endif /* !GC_NO_FINALIZATION */
1290 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_replacement(size_t lb)
1292 return GC_debug_malloc(lb, GC_DBG_EXTRAS);
1295 GC_API void * GC_CALL GC_debug_realloc_replacement(void *p, size_t lb)
1297 return GC_debug_realloc(p, lb, GC_DBG_EXTRAS);