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 */
61 # if defined(__GLIBC__) || defined(SOLARIS) \
62 || defined(HPUX) || defined(IRIX5) || defined(OSF1)
63 # define RANDOM() random()
65 # define RANDOM() (long)rand()
68 /* Store back pointer to source in dest, if that appears to be possible. */
69 /* This is not completely safe, since we may mistakenly conclude that */
70 /* dest has a debugging wrapper. But the error probability is very */
71 /* small, and this shouldn't be used in production code. */
72 /* We assume that dest is the real base pointer. Source will usually */
73 /* be a pointer to the interior of an object. */
74 GC_INNER void GC_store_back_pointer(ptr_t source, ptr_t dest)
76 if (GC_HAS_DEBUG_INFO(dest)) {
77 ((oh *)dest) -> oh_back_ptr = HIDE_BACK_PTR(source);
81 GC_INNER void GC_marked_for_finalization(ptr_t dest)
83 GC_store_back_pointer(MARKED_FOR_FINALIZATION, dest);
86 /* Store information about the object referencing dest in *base_p */
88 /* source is root ==> *base_p = address, *offset_p = 0 */
89 /* source is heap object ==> *base_p != 0, *offset_p = offset */
90 /* Returns 1 on success, 0 if source couldn't be determined. */
91 /* Dest can be any address within a heap object. */
92 GC_API GC_ref_kind GC_CALL GC_get_back_ptr_info(void *dest, void **base_p,
95 oh * hdr = (oh *)GC_base(dest);
100 /* Explicitly instruct the code analysis tool that */
101 /* GC_get_back_ptr_info is not expected to be called with an */
102 /* incorrect "dest" value. */
103 if (!hdr) ABORT("Invalid GC_get_back_ptr_info argument");
105 if (!GC_HAS_DEBUG_INFO((ptr_t) hdr)) return GC_NO_SPACE;
106 bp = GC_REVEAL_POINTER(hdr -> oh_back_ptr);
107 if (MARKED_FOR_FINALIZATION == bp) return GC_FINALIZER_REFD;
108 if (MARKED_FROM_REGISTER == bp) return GC_REFD_FROM_REG;
109 if (NOT_MARKED == bp) return GC_UNREFERENCED;
111 /* Heuristically try to fix off by 1 errors we introduced by */
112 /* insisting on even addresses. */
114 ptr_t alternate_ptr = bp + 1;
115 ptr_t target = *(ptr_t *)bp;
116 ptr_t alternate_target = *(ptr_t *)alternate_ptr;
118 if ((word)alternate_target >= (word)GC_least_plausible_heap_addr
119 && (word)alternate_target <= (word)GC_greatest_plausible_heap_addr
120 && ((word)target < (word)GC_least_plausible_heap_addr
121 || (word)target > (word)GC_greatest_plausible_heap_addr)) {
126 bp_base = GC_base(bp);
130 return GC_REFD_FROM_ROOT;
132 if (GC_HAS_DEBUG_INFO(bp_base)) bp_base += sizeof(oh);
134 *offset_p = bp - bp_base;
135 return GC_REFD_FROM_HEAP;
139 /* Generate a random heap address. */
140 /* The resulting address is in the heap, but */
141 /* not necessarily inside a valid object. */
142 GC_API void * GC_CALL GC_generate_random_heap_address(void)
145 word heap_offset = RANDOM();
147 if (GC_heapsize > RAND_MAX) {
148 heap_offset *= RAND_MAX;
149 heap_offset += RANDOM();
151 heap_offset %= GC_heapsize;
152 /* This doesn't yield a uniform distribution, especially if */
153 /* e.g. RAND_MAX = 1.5* GC_heapsize. But for typical cases, */
154 /* it's not too bad. */
158 if (i >= GC_n_heap_sects)
159 ABORT("GC_generate_random_heap_address: size inconsistency");
161 size = GC_heap_sects[i].hs_bytes;
162 if (heap_offset < size) {
168 return GC_heap_sects[i].hs_start + heap_offset;
171 /* Generate a random address inside a valid marked heap object. */
172 GC_API void * GC_CALL GC_generate_random_valid_address(void)
177 result = GC_generate_random_heap_address();
178 base = GC_base(result);
179 } while (base == 0 || !GC_is_marked(base));
183 /* Print back trace for p */
184 GC_API void GC_CALL GC_print_backtrace(void *p)
192 GC_print_heap_obj(GC_base(current));
195 source = GC_get_back_ptr_info(current, &base, &offset);
196 if (GC_UNREFERENCED == source) {
197 GC_err_printf("Reference could not be found\n");
200 if (GC_NO_SPACE == source) {
201 GC_err_printf("No debug info in object: Can't find reference\n");
204 GC_err_printf("Reachable via %d levels of pointers from ", i);
206 case GC_REFD_FROM_ROOT:
207 GC_err_printf("root at %p\n\n", base);
209 case GC_REFD_FROM_REG:
210 GC_err_printf("root in register\n\n");
212 case GC_FINALIZER_REFD:
213 GC_err_printf("list of finalizable objects\n\n");
215 case GC_REFD_FROM_HEAP:
216 GC_err_printf("offset %ld in object:\n", (long)offset);
217 /* Take GC_base(base) to get real base, i.e. header. */
218 GC_print_heap_obj(GC_base(base));
221 GC_err_printf("INTERNAL ERROR: UNEXPECTED SOURCE!!!!\n");
229 /* Force a garbage collection and generate/print a backtrace */
230 /* from a random heap address. */
231 GC_INNER void GC_generate_random_backtrace_no_gc(void)
234 current = GC_generate_random_valid_address();
235 GC_printf("\n****Chosen address %p in object\n", current);
236 GC_print_backtrace(current);
239 GC_API void GC_CALL GC_generate_random_backtrace(void)
241 if (GC_try_to_collect(GC_never_stop_func) == 0) {
242 GC_err_printf("Cannot generate a backtrace: "
243 "garbage collection is disabled!\n");
246 GC_generate_random_backtrace_no_gc();
249 #endif /* KEEP_BACK_PTRS */
251 # define CROSSES_HBLK(p, sz) \
252 (((word)((p) + sizeof(oh) + (sz) - 1) ^ (word)(p)) >= HBLKSIZE)
254 /* Store debugging info into p. Return displaced pointer. */
255 /* This version assumes we do hold the allocation lock. */
256 STATIC ptr_t GC_store_debug_info_inner(ptr_t p, word sz GC_ATTR_UNUSED,
257 const char *string, int linenum)
259 word * result = (word *)((oh *)p + 1);
261 GC_ASSERT(GC_size(p) >= sizeof(oh) + sz);
262 GC_ASSERT(!(SMALL_OBJ(sz) && CROSSES_HBLK(p, sz)));
263 # ifdef KEEP_BACK_PTRS
264 ((oh *)p) -> oh_back_ptr = HIDE_BACK_PTR(NOT_MARKED);
266 # ifdef MAKE_BACK_GRAPH
267 ((oh *)p) -> oh_bg_ptr = HIDE_BACK_PTR((ptr_t)0);
269 ((oh *)p) -> oh_string = string;
270 ((oh *)p) -> oh_int = (word)linenum;
271 # ifndef SHORT_DBG_HDRS
272 ((oh *)p) -> oh_sz = sz;
273 ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
274 ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
275 result[SIMPLE_ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
277 return((ptr_t)result);
280 GC_INNER ptr_t GC_store_debug_info(ptr_t p, word sz, const char *string,
287 result = GC_store_debug_info_inner(p, sz, string, linenum);
292 #ifndef SHORT_DBG_HDRS
293 /* Check the object with debugging info at ohdr. */
294 /* Return NULL if it's OK. Else return clobbered */
296 STATIC ptr_t GC_check_annotated_obj(oh *ohdr)
298 ptr_t body = (ptr_t)(ohdr + 1);
299 word gc_sz = GC_size((ptr_t)ohdr);
300 if (ohdr -> oh_sz + DEBUG_BYTES > gc_sz) {
301 return((ptr_t)(&(ohdr -> oh_sz)));
303 if (ohdr -> oh_sf != (START_FLAG ^ (word)body)) {
304 return((ptr_t)(&(ohdr -> oh_sf)));
306 if (((word *)ohdr)[BYTES_TO_WORDS(gc_sz)-1] != (END_FLAG ^ (word)body)) {
307 return((ptr_t)((word *)ohdr + BYTES_TO_WORDS(gc_sz)-1));
309 if (((word *)body)[SIMPLE_ROUNDED_UP_WORDS(ohdr -> oh_sz)]
310 != (END_FLAG ^ (word)body)) {
311 return((ptr_t)((word *)body + SIMPLE_ROUNDED_UP_WORDS(ohdr->oh_sz)));
315 #endif /* !SHORT_DBG_HDRS */
317 STATIC GC_describe_type_fn GC_describe_type_fns[MAXOBJKINDS] = {0};
319 GC_API void GC_CALL GC_register_describe_type_fn(int kind,
320 GC_describe_type_fn fn)
322 GC_describe_type_fns[kind] = fn;
325 #define GET_OH_LINENUM(ohdr) ((int)(ohdr)->oh_int)
327 #ifndef SHORT_DBG_HDRS
328 # define IF_NOT_SHORTDBG_HDRS(x) x
329 # define COMMA_IFNOT_SHORTDBG_HDRS(x) /* comma */, x
331 # define IF_NOT_SHORTDBG_HDRS(x) /* empty */
332 # define COMMA_IFNOT_SHORTDBG_HDRS(x) /* empty */
335 /* Print a human-readable description of the object to stderr. */
336 /* p points to somewhere inside an object with the debugging info. */
337 STATIC void GC_print_obj(ptr_t p)
339 oh * ohdr = (oh *)GC_base(p);
344 char buffer[GC_TYPE_DESCR_LEN + 1];
346 GC_ASSERT(I_DONT_HOLD_LOCK());
348 if (!ohdr) ABORT("Invalid GC_print_obj argument");
351 q = (ptr_t)(ohdr + 1);
352 /* Print a type description for the object whose client-visible */
354 hhdr = GC_find_header(q);
355 kind = hhdr -> hb_obj_kind;
356 if (0 != GC_describe_type_fns[kind] && GC_is_marked(ohdr)) {
357 /* This should preclude free list objects except with */
358 /* thread-local allocation. */
359 buffer[GC_TYPE_DESCR_LEN] = 0;
360 (GC_describe_type_fns[kind])(q, buffer);
361 GC_ASSERT(buffer[GC_TYPE_DESCR_LEN] == 0);
366 kind_str = "PTRFREE";
372 kind_str = "UNCOLLECTABLE";
374 # ifdef GC_ATOMIC_UNCOLLECTABLE
376 kind_str = "ATOMIC_UNCOLLECTABLE";
380 kind_str = "STUBBORN";
384 /* The alternative is to use snprintf(buffer) but it is */
385 /* not quite portable (see vsnprintf in misc.c). */
389 if (NULL != kind_str) {
390 GC_err_printf("%p (%s:%d," IF_NOT_SHORTDBG_HDRS(" sz=%lu,") " %s)\n",
391 (ptr_t)ohdr + sizeof(oh),
392 ohdr->oh_string, GET_OH_LINENUM(ohdr) /*, */
393 COMMA_IFNOT_SHORTDBG_HDRS((unsigned long)ohdr->oh_sz),
396 GC_err_printf("%p (%s:%d," IF_NOT_SHORTDBG_HDRS(" sz=%lu,")
397 " kind=%d descr=0x%lx)\n", (ptr_t)ohdr + sizeof(oh),
398 ohdr->oh_string, GET_OH_LINENUM(ohdr) /*, */
399 COMMA_IFNOT_SHORTDBG_HDRS((unsigned long)ohdr->oh_sz),
400 kind, (unsigned long)hhdr->hb_descr);
402 PRINT_CALL_CHAIN(ohdr);
405 STATIC void GC_debug_print_heap_obj_proc(ptr_t p)
407 GC_ASSERT(I_DONT_HOLD_LOCK());
408 if (GC_HAS_DEBUG_INFO(p)) {
411 GC_default_print_heap_obj_proc(p);
415 #ifndef SHORT_DBG_HDRS
416 /* Use GC_err_printf and friends to print a description of the object */
417 /* whose client-visible address is p, and which was smashed at */
418 /* clobbered_addr. */
419 STATIC void GC_print_smashed_obj(const char *msg, ptr_t p,
420 ptr_t clobbered_addr)
422 oh * ohdr = (oh *)GC_base(p);
424 GC_ASSERT(I_DONT_HOLD_LOCK());
426 if (!ohdr) ABORT("Invalid GC_print_smashed_obj argument");
428 if ((word)clobbered_addr <= (word)(&ohdr->oh_sz)
429 || ohdr -> oh_string == 0) {
431 "%s %p in or near object at %p(<smashed>, appr. sz = %lu)\n",
432 msg, clobbered_addr, p,
433 (unsigned long)(GC_size((ptr_t)ohdr) - DEBUG_BYTES));
435 GC_err_printf("%s %p in or near object at %p (%s:%d, sz=%lu)\n",
436 msg, clobbered_addr, p,
437 (word)(ohdr -> oh_string) < HBLKSIZE ? "(smashed string)" :
438 ohdr -> oh_string[0] == '\0' ? "EMPTY(smashed?)" :
440 GET_OH_LINENUM(ohdr), (unsigned long)(ohdr -> oh_sz));
441 PRINT_CALL_CHAIN(ohdr);
446 #ifndef SHORT_DBG_HDRS
447 STATIC void GC_check_heap_proc (void);
448 STATIC void GC_print_all_smashed_proc (void);
450 STATIC void GC_do_nothing(void) {}
453 STATIC void GC_start_debugging_inner(void)
455 GC_ASSERT(I_HOLD_LOCK());
456 # ifndef SHORT_DBG_HDRS
457 GC_check_heap = GC_check_heap_proc;
458 GC_print_all_smashed = GC_print_all_smashed_proc;
460 GC_check_heap = GC_do_nothing;
461 GC_print_all_smashed = GC_do_nothing;
463 GC_print_heap_obj = GC_debug_print_heap_obj_proc;
464 GC_debugging_started = TRUE;
465 GC_register_displacement_inner((word)sizeof(oh));
468 GC_INNER void GC_start_debugging(void)
473 GC_start_debugging_inner();
477 size_t GC_debug_header_size = sizeof(oh);
479 GC_API void GC_CALL GC_debug_register_displacement(size_t offset)
484 GC_register_displacement_inner(offset);
485 GC_register_displacement_inner((word)sizeof(oh) + offset);
490 # if defined(HAVE_DLADDR) && defined(GC_RETURN_ADDR_PARENT)
493 STATIC void GC_caller_func_offset(word ad, const char **symp, int *offp)
497 if (ad && dladdr((void *)ad, &caller) && caller.dli_sname != NULL) {
498 *symp = caller.dli_sname;
499 *offp = (int)((char *)ad - (char *)caller.dli_saddr);
506 # define GC_caller_func_offset(ad, symp, offp) (void)(*(symp) = "unknown")
508 #endif /* GC_ADD_CALLER */
510 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc(size_t lb,
515 /* Note that according to malloc() specification, if size is 0 then */
516 /* malloc() returns either NULL, or a unique pointer value that can */
517 /* later be successfully passed to free(). We always do the latter. */
518 result = GC_malloc(SIZET_SAT_ADD(lb, DEBUG_BYTES));
519 # ifdef GC_ADD_CALLER
521 GC_caller_func_offset(ra, &s, &i);
525 GC_err_printf("GC_debug_malloc(%lu) returning NULL (%s:%d)\n",
526 (unsigned long)lb, s, i);
529 if (!GC_debugging_started) {
530 GC_start_debugging();
532 ADD_CALL_CHAIN(result, ra);
533 return (GC_store_debug_info(result, (word)lb, s, i));
536 GC_API GC_ATTR_MALLOC void * GC_CALL
537 GC_debug_malloc_ignore_off_page(size_t lb, GC_EXTRA_PARAMS)
539 void * result = GC_malloc_ignore_off_page(SIZET_SAT_ADD(lb, DEBUG_BYTES));
542 GC_err_printf("GC_debug_malloc_ignore_off_page(%lu)"
543 " returning NULL (%s:%d)\n", (unsigned long)lb, s, i);
546 if (!GC_debugging_started) {
547 GC_start_debugging();
549 ADD_CALL_CHAIN(result, ra);
550 return (GC_store_debug_info(result, (word)lb, s, i));
553 GC_API GC_ATTR_MALLOC void * GC_CALL
554 GC_debug_malloc_atomic_ignore_off_page(size_t lb, GC_EXTRA_PARAMS)
556 void * result = GC_malloc_atomic_ignore_off_page(
557 SIZET_SAT_ADD(lb, DEBUG_BYTES));
560 GC_err_printf("GC_debug_malloc_atomic_ignore_off_page(%lu)"
561 " returning NULL (%s:%d)\n", (unsigned long)lb, s, i);
564 if (!GC_debugging_started) {
565 GC_start_debugging();
567 ADD_CALL_CHAIN(result, ra);
568 return (GC_store_debug_info(result, (word)lb, s, i));
571 STATIC void * GC_debug_generic_malloc(size_t lb, int knd, GC_EXTRA_PARAMS)
573 void * result = GC_generic_malloc(SIZET_SAT_ADD(lb, DEBUG_BYTES), knd);
575 if (NULL == result) {
577 "GC_debug_generic_malloc(%lu, %d) returning NULL (%s:%d)\n",
578 (unsigned long)lb, knd, s, i);
581 if (!GC_debugging_started) {
582 GC_start_debugging();
584 ADD_CALL_CHAIN(result, ra);
585 return GC_store_debug_info(result, (word)lb, s, i);
589 /* An allocation function for internal use. Normally internally */
590 /* allocated objects do not have debug information. But in this */
591 /* case, we need to make sure that all objects have debug headers. */
592 /* We assume debugging was started in collector initialization, and */
593 /* we already hold the GC lock. */
594 GC_INNER void * GC_debug_generic_malloc_inner(size_t lb, int k)
596 void * result = GC_generic_malloc_inner(
597 SIZET_SAT_ADD(lb, DEBUG_BYTES), k);
600 GC_err_printf("GC internal allocation (%lu bytes) returning NULL\n",
604 if (!GC_debugging_started) {
605 GC_start_debugging_inner();
607 ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
608 return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", 0));
611 GC_INNER void * GC_debug_generic_malloc_inner_ignore_off_page(size_t lb,
614 void * result = GC_generic_malloc_inner_ignore_off_page(
615 SIZET_SAT_ADD(lb, DEBUG_BYTES), k);
618 GC_err_printf("GC internal allocation (%lu bytes) returning NULL\n",
622 if (!GC_debugging_started) {
623 GC_start_debugging_inner();
625 ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
626 return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", 0));
628 #endif /* DBG_HDRS_ALL */
630 #ifdef STUBBORN_ALLOC
631 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_stubborn(size_t lb,
634 void * result = GC_malloc_stubborn(SIZET_SAT_ADD(lb, DEBUG_BYTES));
637 GC_err_printf("GC_debug_malloc_stubborn(%lu)"
638 " returning NULL (%s:%d)\n", (unsigned long)lb, s, i);
641 if (!GC_debugging_started) {
642 GC_start_debugging();
644 ADD_CALL_CHAIN(result, ra);
645 return (GC_store_debug_info(result, (word)lb, s, i));
648 GC_API void GC_CALL GC_debug_change_stubborn(const void *p)
650 const void * q = GC_base_C(p);
654 ABORT_ARG1("GC_debug_change_stubborn: bad arg", ": %p", p);
657 if (hhdr -> hb_obj_kind != STUBBORN) {
658 ABORT_ARG1("GC_debug_change_stubborn: arg not stubborn", ": %p", p);
660 GC_change_stubborn(q);
663 GC_API void GC_CALL GC_debug_end_stubborn_change(const void *p)
665 const void * q = GC_base_C(p);
669 ABORT_ARG1("GC_debug_end_stubborn_change: bad arg", ": %p", p);
672 if (hhdr -> hb_obj_kind != STUBBORN) {
673 ABORT_ARG1("GC_debug_end_stubborn_change: arg not stubborn",
676 GC_end_stubborn_change(q);
679 #else /* !STUBBORN_ALLOC */
681 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_stubborn(size_t lb,
684 return GC_debug_malloc(lb, OPT_RA s, i);
687 GC_API void GC_CALL GC_debug_change_stubborn(
688 const void * p GC_ATTR_UNUSED) {}
690 GC_API void GC_CALL GC_debug_end_stubborn_change(
691 const void * p GC_ATTR_UNUSED) {}
692 #endif /* !STUBBORN_ALLOC */
694 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_atomic(size_t lb,
697 void * result = GC_malloc_atomic(SIZET_SAT_ADD(lb, DEBUG_BYTES));
700 GC_err_printf("GC_debug_malloc_atomic(%lu) returning NULL (%s:%d)\n",
701 (unsigned long)lb, s, i);
704 if (!GC_debugging_started) {
705 GC_start_debugging();
707 ADD_CALL_CHAIN(result, ra);
708 return (GC_store_debug_info(result, (word)lb, s, i));
711 GC_API GC_ATTR_MALLOC char * GC_CALL GC_debug_strdup(const char *str,
718 GC_err_printf("strdup(NULL) behavior is undefined\n");
722 lb = strlen(str) + 1;
723 copy = GC_debug_malloc_atomic(lb, OPT_RA s, i);
730 BCOPY(str, copy, lb);
734 GC_API GC_ATTR_MALLOC char * GC_CALL GC_debug_strndup(const char *str,
735 size_t size, GC_EXTRA_PARAMS)
738 size_t len = strlen(str); /* str is expected to be non-NULL */
741 copy = GC_debug_malloc_atomic(len + 1, OPT_RA s, i);
748 BCOPY(str, copy, len);
753 #ifdef GC_REQUIRE_WCSDUP
754 # include <wchar.h> /* for wcslen() */
756 GC_API GC_ATTR_MALLOC wchar_t * GC_CALL GC_debug_wcsdup(const wchar_t *str,
759 size_t lb = (wcslen(str) + 1) * sizeof(wchar_t);
760 wchar_t *copy = GC_debug_malloc_atomic(lb, OPT_RA s, i);
767 BCOPY(str, copy, lb);
770 #endif /* GC_REQUIRE_WCSDUP */
772 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_uncollectable(size_t lb,
775 void * result = GC_malloc_uncollectable(
776 SIZET_SAT_ADD(lb, UNCOLLECTABLE_DEBUG_BYTES));
779 GC_err_printf("GC_debug_malloc_uncollectable(%lu)"
780 " returning NULL (%s:%d)\n", (unsigned long)lb, s, i);
783 if (!GC_debugging_started) {
784 GC_start_debugging();
786 ADD_CALL_CHAIN(result, ra);
787 return (GC_store_debug_info(result, (word)lb, s, i));
790 #ifdef GC_ATOMIC_UNCOLLECTABLE
791 GC_API GC_ATTR_MALLOC void * GC_CALL
792 GC_debug_malloc_atomic_uncollectable(size_t lb, GC_EXTRA_PARAMS)
794 void * result = GC_malloc_atomic_uncollectable(
795 SIZET_SAT_ADD(lb, UNCOLLECTABLE_DEBUG_BYTES));
798 GC_err_printf("GC_debug_malloc_atomic_uncollectable(%lu)"
799 " returning NULL (%s:%d)\n", (unsigned long)lb, s, i);
802 if (!GC_debugging_started) {
803 GC_start_debugging();
805 ADD_CALL_CHAIN(result, ra);
806 return (GC_store_debug_info(result, (word)lb, s, i));
808 #endif /* GC_ATOMIC_UNCOLLECTABLE */
810 #ifndef GC_FREED_MEM_MARKER
811 # if CPP_WORDSZ == 32
812 # define GC_FREED_MEM_MARKER 0xdeadbeef
814 # define GC_FREED_MEM_MARKER GC_WORD_C(0xEFBEADDEdeadbeef)
818 GC_API void GC_CALL GC_debug_free(void * p)
825 ABORT_ARG1("Invalid pointer passed to free()", ": %p", p);
827 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
829 "GC_debug_free called on pointer %p w/o debugging info\n", p);
831 # ifndef SHORT_DBG_HDRS
832 ptr_t clobbered = GC_check_annotated_obj((oh *)base);
833 word sz = GC_size(base);
834 if (clobbered != 0) {
835 GC_have_errors = TRUE;
836 if (((oh *)base) -> oh_sz == sz) {
837 GC_print_smashed_obj(
838 "GC_debug_free: found previously deallocated (?) object at",
840 return; /* ignore double free */
842 GC_print_smashed_obj("GC_debug_free: found smashed location at",
846 /* Invalidate size (mark the object as deallocated) */
847 ((oh *)base) -> oh_sz = sz;
848 # endif /* SHORT_DBG_HDRS */
851 # ifndef SHORT_DBG_HDRS
852 && ((ptr_t)p - (ptr_t)base != sizeof(oh) || !GC_findleak_delay_free)
858 if (hhdr -> hb_obj_kind == UNCOLLECTABLE
859 # ifdef GC_ATOMIC_UNCOLLECTABLE
860 || hhdr -> hb_obj_kind == AUNCOLLECTABLE
866 size_t obj_sz = BYTES_TO_WORDS(hhdr -> hb_sz - sizeof(oh));
868 for (i = 0; i < obj_sz; ++i)
869 ((word *)p)[i] = GC_FREED_MEM_MARKER;
870 GC_ASSERT((word *)p + i == (word *)(base + hhdr -> hb_sz));
872 } /* !GC_find_leak */
875 #if defined(THREADS) && defined(DBG_HDRS_ALL)
876 /* Used internally; we assume it's called correctly. */
877 GC_INNER void GC_debug_free_inner(void * p)
879 ptr_t base = GC_base(p);
880 GC_ASSERT((ptr_t)p - (ptr_t)base == sizeof(oh));
882 if (!base) ABORT("Invalid GC_debug_free_inner argument");
884 # ifndef SHORT_DBG_HDRS
885 /* Invalidate size */
886 ((oh *)base) -> oh_sz = GC_size(base);
892 GC_API void * GC_CALL GC_debug_realloc(void * p, size_t lb, GC_EXTRA_PARAMS)
899 return GC_debug_malloc(lb, OPT_RA s, i);
901 if (0 == lb) /* and p != NULL */ {
906 # ifdef GC_ADD_CALLER
908 GC_caller_func_offset(ra, &s, &i);
913 ABORT_ARG1("Invalid pointer passed to realloc()", ": %p", p);
915 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
917 "GC_debug_realloc called on pointer %p w/o debugging info\n", p);
918 return(GC_realloc(p, lb));
921 switch (hhdr -> hb_obj_kind) {
922 # ifdef STUBBORN_ALLOC
924 result = GC_debug_malloc_stubborn(lb, OPT_RA s, i);
928 result = GC_debug_malloc(lb, OPT_RA s, i);
931 result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
934 result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
936 # ifdef GC_ATOMIC_UNCOLLECTABLE
938 result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
942 result = NULL; /* initialized to prevent warning. */
943 ABORT_RET("GC_debug_realloc: encountered bad kind");
946 if (result != NULL) {
948 # ifdef SHORT_DBG_HDRS
949 old_sz = GC_size(base) - sizeof(oh);
951 old_sz = ((oh *)base) -> oh_sz;
953 BCOPY(p, result, old_sz < lb ? old_sz : lb);
959 GC_API GC_ATTR_MALLOC void * GC_CALL
960 GC_debug_generic_or_special_malloc(size_t lb, int knd, GC_EXTRA_PARAMS)
963 # ifdef STUBBORN_ALLOC
965 return GC_debug_malloc_stubborn(lb, OPT_RA s, i);
968 return GC_debug_malloc_atomic(lb, OPT_RA s, i);
970 return GC_debug_malloc(lb, OPT_RA s, i);
972 return GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
973 # ifdef GC_ATOMIC_UNCOLLECTABLE
975 return GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
978 return GC_debug_generic_malloc(lb, knd, OPT_RA s, i);
982 #ifndef SHORT_DBG_HDRS
984 /* List of smashed (clobbered) locations. We defer printing these, */
985 /* since we can't always print them nicely with the allocation lock */
986 /* held. We put them here instead of in GC_arrays, since it may be */
987 /* useful to be able to look at them with the debugger. */
989 # define MAX_SMASHED 20
991 STATIC ptr_t GC_smashed[MAX_SMASHED] = {0};
992 STATIC unsigned GC_n_smashed = 0;
994 STATIC void GC_add_smashed(ptr_t smashed)
996 GC_ASSERT(GC_is_marked(GC_base(smashed)));
997 /* FIXME: Prevent adding an object while printing smashed list. */
998 GC_smashed[GC_n_smashed] = smashed;
999 if (GC_n_smashed < MAX_SMASHED - 1) ++GC_n_smashed;
1000 /* In case of overflow, we keep the first MAX_SMASHED-1 */
1001 /* entries plus the last one. */
1002 GC_have_errors = TRUE;
1005 /* Print all objects on the list. Clear the list. */
1006 STATIC void GC_print_all_smashed_proc(void)
1010 GC_ASSERT(I_DONT_HOLD_LOCK());
1011 if (GC_n_smashed == 0) return;
1012 GC_err_printf("GC_check_heap_block: found %u smashed heap objects:\n",
1014 for (i = 0; i < GC_n_smashed; ++i) {
1015 ptr_t base = (ptr_t)GC_base(GC_smashed[i]);
1018 if (!base) ABORT("Invalid GC_smashed element");
1020 GC_print_smashed_obj("", base + sizeof(oh), GC_smashed[i]);
1026 /* Check all marked objects in the given block for validity */
1027 /* Avoid GC_apply_to_each_object for performance reasons. */
1028 STATIC void GC_check_heap_block(struct hblk *hbp, word dummy GC_ATTR_UNUSED)
1030 struct hblkhdr * hhdr = HDR(hbp);
1031 size_t sz = hhdr -> hb_sz;
1036 if (sz > MAXOBJBYTES) {
1039 plim = hbp->hb_body + HBLKSIZE - sz;
1041 /* go through all words in block */
1042 for (bit_no = 0; (word)p <= (word)plim;
1043 bit_no += MARK_BIT_OFFSET(sz), p += sz) {
1044 if (mark_bit_from_hdr(hhdr, bit_no) && GC_HAS_DEBUG_INFO((ptr_t)p)) {
1045 ptr_t clobbered = GC_check_annotated_obj((oh *)p);
1047 GC_add_smashed(clobbered);
1052 /* This assumes that all accessible objects are marked, and that */
1053 /* I hold the allocation lock. Normally called by collector. */
1054 STATIC void GC_check_heap_proc(void)
1056 GC_STATIC_ASSERT((sizeof(oh) & (GRANULE_BYTES - 1)) == 0);
1057 /* FIXME: Should we check for twice that alignment? */
1058 GC_apply_to_all_blocks(GC_check_heap_block, 0);
1061 GC_INNER GC_bool GC_check_leaked(ptr_t base)
1068 # if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
1069 (*(word *)base & 1) != 0 &&
1071 GC_has_other_debug_info(base) >= 0)
1072 return TRUE; /* object has leaked */
1074 /* Validate freed object's content. */
1075 p = (word *)(base + sizeof(oh));
1076 obj_sz = BYTES_TO_WORDS(HDR(base)->hb_sz - sizeof(oh));
1077 for (i = 0; i < obj_sz; ++i)
1078 if (p[i] != GC_FREED_MEM_MARKER) {
1079 GC_set_mark_bit(base); /* do not reclaim it in this cycle */
1080 GC_add_smashed((ptr_t)(&p[i])); /* alter-after-free detected */
1081 break; /* don't report any other smashed locations in the object */
1084 return FALSE; /* GC_debug_free() has been called */
1087 #endif /* !SHORT_DBG_HDRS */
1089 #ifndef GC_NO_FINALIZATION
1092 GC_finalization_proc cl_fn;
1096 STATIC void * GC_make_closure(GC_finalization_proc fn, void * data)
1098 struct closure * result =
1099 # ifdef DBG_HDRS_ALL
1100 (struct closure *) GC_debug_malloc(sizeof (struct closure),
1103 (struct closure *) GC_malloc(sizeof (struct closure));
1106 result -> cl_fn = fn;
1107 result -> cl_data = data;
1109 return((void *)result);
1112 /* An auxiliary fns to make finalization work correctly with displaced */
1113 /* pointers introduced by the debugging allocators. */
1114 STATIC void GC_CALLBACK GC_debug_invoke_finalizer(void * obj, void * data)
1116 struct closure * cl = (struct closure *) data;
1117 (*(cl -> cl_fn))((void *)((char *)obj + sizeof(oh)), cl -> cl_data);
1120 /* Special finalizer_proc value to detect GC_register_finalizer() failure. */
1121 #define OFN_UNSET ((GC_finalization_proc)~(signed_word)0)
1123 /* Set ofn and ocd to reflect the values we got back. */
1124 static void store_old(void *obj, GC_finalization_proc my_old_fn,
1125 struct closure *my_old_cd, GC_finalization_proc *ofn,
1128 if (0 != my_old_fn) {
1129 if (my_old_fn == OFN_UNSET) {
1130 /* register_finalizer() failed; (*ofn) and (*ocd) are unchanged. */
1133 if (my_old_fn != GC_debug_invoke_finalizer) {
1134 GC_err_printf("Debuggable object at %p had a non-debug finalizer\n",
1136 /* This should probably be fatal. */
1138 if (ofn) *ofn = my_old_cd -> cl_fn;
1139 if (ocd) *ocd = my_old_cd -> cl_data;
1147 GC_API void GC_CALL GC_debug_register_finalizer(void * obj,
1148 GC_finalization_proc fn,
1149 void * cd, GC_finalization_proc *ofn,
1152 GC_finalization_proc my_old_fn = OFN_UNSET;
1154 ptr_t base = GC_base(obj);
1156 /* We won't collect it, hence finalizer wouldn't be run. */
1161 if ((ptr_t)obj - base != sizeof(oh)) {
1162 GC_err_printf("GC_debug_register_finalizer called with"
1163 " non-base-pointer %p\n", obj);
1166 GC_register_finalizer(base, 0, 0, &my_old_fn, &my_old_cd);
1168 cd = GC_make_closure(fn, cd);
1169 if (cd == 0) return; /* out of memory */
1170 GC_register_finalizer(base, GC_debug_invoke_finalizer,
1171 cd, &my_old_fn, &my_old_cd);
1173 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1176 GC_API void GC_CALL GC_debug_register_finalizer_no_order
1177 (void * obj, GC_finalization_proc fn,
1178 void * cd, GC_finalization_proc *ofn,
1181 GC_finalization_proc my_old_fn = OFN_UNSET;
1183 ptr_t base = GC_base(obj);
1185 /* We won't collect it, hence finalizer wouldn't be run. */
1190 if ((ptr_t)obj - base != sizeof(oh)) {
1191 GC_err_printf("GC_debug_register_finalizer_no_order called with"
1192 " non-base-pointer %p\n", obj);
1195 GC_register_finalizer_no_order(base, 0, 0, &my_old_fn, &my_old_cd);
1197 cd = GC_make_closure(fn, cd);
1198 if (cd == 0) return; /* out of memory */
1199 GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
1200 cd, &my_old_fn, &my_old_cd);
1202 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1205 GC_API void GC_CALL GC_debug_register_finalizer_unreachable
1206 (void * obj, GC_finalization_proc fn,
1207 void * cd, GC_finalization_proc *ofn,
1210 GC_finalization_proc my_old_fn = OFN_UNSET;
1212 ptr_t base = GC_base(obj);
1214 /* We won't collect it, hence finalizer wouldn't be run. */
1219 if ((ptr_t)obj - base != sizeof(oh)) {
1220 GC_err_printf("GC_debug_register_finalizer_unreachable called with"
1221 " non-base-pointer %p\n", obj);
1224 GC_register_finalizer_unreachable(base, 0, 0, &my_old_fn, &my_old_cd);
1226 cd = GC_make_closure(fn, cd);
1227 if (cd == 0) return; /* out of memory */
1228 GC_register_finalizer_unreachable(base, GC_debug_invoke_finalizer,
1229 cd, &my_old_fn, &my_old_cd);
1231 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1234 GC_API void GC_CALL GC_debug_register_finalizer_ignore_self
1235 (void * obj, GC_finalization_proc fn,
1236 void * cd, GC_finalization_proc *ofn,
1239 GC_finalization_proc my_old_fn = OFN_UNSET;
1241 ptr_t base = GC_base(obj);
1243 /* We won't collect it, hence finalizer wouldn't be run. */
1248 if ((ptr_t)obj - base != sizeof(oh)) {
1249 GC_err_printf("GC_debug_register_finalizer_ignore_self called with"
1250 " non-base-pointer %p\n", obj);
1253 GC_register_finalizer_ignore_self(base, 0, 0, &my_old_fn, &my_old_cd);
1255 cd = GC_make_closure(fn, cd);
1256 if (cd == 0) return; /* out of memory */
1257 GC_register_finalizer_ignore_self(base, GC_debug_invoke_finalizer,
1258 cd, &my_old_fn, &my_old_cd);
1260 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1263 #endif /* !GC_NO_FINALIZATION */
1265 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_replacement(size_t lb)
1267 return GC_debug_malloc(lb, GC_DBG_EXTRAS);
1270 GC_API void * GC_CALL GC_debug_realloc_replacement(void *p, size_t lb)
1272 return GC_debug_realloc(p, lb, GC_DBG_EXTRAS);