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, uncollectable 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)
146 word heap_offset = RANDOM();
148 if (GC_heapsize > RAND_MAX) {
149 heap_offset *= RAND_MAX;
150 heap_offset += RANDOM();
152 heap_offset %= GC_heapsize;
153 /* This doesn't yield a uniform distribution, especially if */
154 /* e.g. RAND_MAX = 1.5* GC_heapsize. But for typical cases, */
155 /* it's not too bad. */
157 if (i >= GC_n_heap_sects)
158 ABORT("GC_generate_random_heap_address: size inconsistency");
160 size = GC_heap_sects[i].hs_bytes;
161 if (heap_offset < size) {
167 return GC_heap_sects[i].hs_start + heap_offset;
170 /* Generate a random address inside a valid marked heap object. */
171 GC_API void * GC_CALL GC_generate_random_valid_address(void)
176 result = GC_generate_random_heap_address();
177 base = GC_base(result);
178 } while (base == 0 || !GC_is_marked(base));
182 /* Print back trace for p */
183 GC_API void GC_CALL GC_print_backtrace(void *p)
191 GC_print_heap_obj(GC_base(current));
194 source = GC_get_back_ptr_info(current, &base, &offset);
195 if (GC_UNREFERENCED == source) {
196 GC_err_printf("Reference could not be found\n");
199 if (GC_NO_SPACE == source) {
200 GC_err_printf("No debug info in object: Can't find reference\n");
203 GC_err_printf("Reachable via %d levels of pointers from ", i);
205 case GC_REFD_FROM_ROOT:
206 GC_err_printf("root at %p\n\n", base);
208 case GC_REFD_FROM_REG:
209 GC_err_printf("root in register\n\n");
211 case GC_FINALIZER_REFD:
212 GC_err_printf("list of finalizable objects\n\n");
214 case GC_REFD_FROM_HEAP:
215 GC_err_printf("offset %ld in object:\n", (long)offset);
216 /* Take GC_base(base) to get real base, i.e. header. */
217 GC_print_heap_obj(GC_base(base));
221 GC_err_printf("INTERNAL ERROR: UNEXPECTED SOURCE!!!!\n");
229 /* Force a garbage collection and generate a backtrace from a */
230 /* 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 /* Print a type description for the object whose client-visible address */
327 STATIC void GC_print_type(ptr_t p)
329 hdr * hhdr = GC_find_header(p);
330 char buffer[GC_TYPE_DESCR_LEN + 1];
331 int kind = hhdr -> hb_obj_kind;
333 if (0 != GC_describe_type_fns[kind] && GC_is_marked(GC_base(p))) {
334 /* This should preclude free list objects except with */
335 /* thread-local allocation. */
336 buffer[GC_TYPE_DESCR_LEN] = 0;
337 (GC_describe_type_fns[kind])(p, buffer);
338 GC_ASSERT(buffer[GC_TYPE_DESCR_LEN] == 0);
343 GC_err_puts("PTRFREE");
346 GC_err_puts("NORMAL");
349 GC_err_puts("UNCOLLECTABLE");
351 # ifdef ATOMIC_UNCOLLECTABLE
353 GC_err_puts("ATOMIC UNCOLLECTABLE");
357 GC_err_puts("STUBBORN");
360 GC_err_printf("kind=%d descr=0x%lx", kind,
361 (unsigned long)(hhdr -> hb_descr));
366 #define GET_OH_LINENUM(ohdr) ((int)(ohdr)->oh_int)
368 /* Print a human-readable description of the object to stderr. p points */
369 /* to somewhere inside an object with the debugging info. */
370 STATIC void GC_print_obj(ptr_t p)
372 oh * ohdr = (oh *)GC_base(p);
374 GC_ASSERT(I_DONT_HOLD_LOCK());
376 if (!ohdr) ABORT("Invalid GC_print_obj argument");
378 GC_err_printf("%p (", ((ptr_t)ohdr + sizeof(oh)));
379 GC_err_puts(ohdr -> oh_string);
380 # ifdef SHORT_DBG_HDRS
381 GC_err_printf(":%d, ", GET_OH_LINENUM(ohdr));
383 GC_err_printf(":%d, sz=%lu, ",
384 GET_OH_LINENUM(ohdr), (unsigned long)(ohdr -> oh_sz));
386 GC_print_type((ptr_t)(ohdr + 1));
388 PRINT_CALL_CHAIN(ohdr);
391 STATIC void GC_debug_print_heap_obj_proc(ptr_t p)
393 GC_ASSERT(I_DONT_HOLD_LOCK());
394 if (GC_HAS_DEBUG_INFO(p)) {
397 GC_default_print_heap_obj_proc(p);
401 #ifndef SHORT_DBG_HDRS
402 /* Use GC_err_printf and friends to print a description of the object */
403 /* whose client-visible address is p, and which was smashed at */
404 /* clobbered_addr. */
405 STATIC void GC_print_smashed_obj(const char *msg, ptr_t p,
406 ptr_t clobbered_addr)
408 oh * ohdr = (oh *)GC_base(p);
410 GC_ASSERT(I_DONT_HOLD_LOCK());
412 if (!ohdr) ABORT("Invalid GC_print_smashed_obj argument");
414 if ((word)clobbered_addr <= (word)(&ohdr->oh_sz)
415 || ohdr -> oh_string == 0) {
417 "%s %p in or near object at %p(<smashed>, appr. sz = %lu)\n",
418 msg, clobbered_addr, p,
419 (unsigned long)(GC_size((ptr_t)ohdr) - DEBUG_BYTES));
421 GC_err_printf("%s %p in or near object at %p (%s:%d, sz=%lu)\n",
422 msg, clobbered_addr, p,
423 (word)(ohdr -> oh_string) < HBLKSIZE ? "(smashed string)" :
424 ohdr -> oh_string[0] == '\0' ? "EMPTY(smashed?)" :
426 GET_OH_LINENUM(ohdr), (unsigned long)(ohdr -> oh_sz));
427 PRINT_CALL_CHAIN(ohdr);
432 #ifndef SHORT_DBG_HDRS
433 STATIC void GC_check_heap_proc (void);
434 STATIC void GC_print_all_smashed_proc (void);
436 STATIC void GC_do_nothing(void) {}
439 GC_INNER void GC_start_debugging(void)
441 # ifndef SHORT_DBG_HDRS
442 GC_check_heap = GC_check_heap_proc;
443 GC_print_all_smashed = GC_print_all_smashed_proc;
445 GC_check_heap = GC_do_nothing;
446 GC_print_all_smashed = GC_do_nothing;
448 GC_print_heap_obj = GC_debug_print_heap_obj_proc;
449 GC_debugging_started = TRUE;
450 GC_register_displacement((word)sizeof(oh));
453 size_t GC_debug_header_size = sizeof(oh);
455 GC_API void GC_CALL GC_debug_register_displacement(size_t offset)
457 GC_register_displacement(offset);
458 GC_register_displacement((word)sizeof(oh) + offset);
461 GC_API void * GC_CALL GC_debug_malloc(size_t lb, GC_EXTRA_PARAMS)
464 /* Note that according to malloc() specification, if size is 0 then */
465 /* malloc() returns either NULL, or a unique pointer value that can */
466 /* later be successfully passed to free(). We always do the latter. */
467 result = GC_malloc(lb + DEBUG_BYTES);
470 GC_err_printf("GC_debug_malloc(%lu) returning NULL (",
473 GC_err_printf(":%d)\n", i);
476 if (!GC_debugging_started) {
477 GC_start_debugging();
479 ADD_CALL_CHAIN(result, ra);
480 return (GC_store_debug_info(result, (word)lb, s, i));
483 GC_API void * GC_CALL GC_debug_malloc_ignore_off_page(size_t lb,
486 void * result = GC_malloc_ignore_off_page(lb + DEBUG_BYTES);
489 GC_err_printf("GC_debug_malloc_ignore_off_page(%lu) returning NULL (",
492 GC_err_printf(":%d)\n", i);
495 if (!GC_debugging_started) {
496 GC_start_debugging();
498 ADD_CALL_CHAIN(result, ra);
499 return (GC_store_debug_info(result, (word)lb, s, i));
502 GC_API void * GC_CALL GC_debug_malloc_atomic_ignore_off_page(size_t lb,
505 void * result = GC_malloc_atomic_ignore_off_page(lb + DEBUG_BYTES);
508 GC_err_printf("GC_debug_malloc_atomic_ignore_off_page(%lu)"
509 " returning NULL (", (unsigned long)lb);
511 GC_err_printf(":%d)\n", i);
514 if (!GC_debugging_started) {
515 GC_start_debugging();
517 ADD_CALL_CHAIN(result, ra);
518 return (GC_store_debug_info(result, (word)lb, s, i));
522 /* An allocation function for internal use. Normally internally */
523 /* allocated objects do not have debug information. But in this */
524 /* case, we need to make sure that all objects have debug headers. */
525 /* We assume debugging was started in collector initialization, and */
526 /* we already hold the GC lock. */
527 GC_INNER void * GC_debug_generic_malloc_inner(size_t lb, int k)
529 void * result = GC_generic_malloc_inner(lb + DEBUG_BYTES, k);
532 GC_err_printf("GC internal allocation (%lu bytes) returning NULL\n",
536 ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
537 return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", 0));
540 GC_INNER void * GC_debug_generic_malloc_inner_ignore_off_page(size_t lb,
543 void * result = GC_generic_malloc_inner_ignore_off_page(
544 lb + DEBUG_BYTES, k);
547 GC_err_printf("GC internal allocation (%lu bytes) returning NULL\n",
551 ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
552 return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", 0));
554 #endif /* DBG_HDRS_ALL */
556 #ifdef STUBBORN_ALLOC
557 GC_API void * GC_CALL GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
559 void * result = GC_malloc_stubborn(lb + DEBUG_BYTES);
562 GC_err_printf("GC_debug_malloc(%lu) returning NULL (",
565 GC_err_printf(":%d)\n", i);
568 if (!GC_debugging_started) {
569 GC_start_debugging();
571 ADD_CALL_CHAIN(result, ra);
572 return (GC_store_debug_info(result, (word)lb, s, i));
575 GC_API void GC_CALL GC_debug_change_stubborn(const void *p)
577 const void * q = GC_base_C(p);
581 GC_err_printf("Bad argument: %p to GC_debug_change_stubborn\n", p);
582 ABORT("GC_debug_change_stubborn: bad arg");
585 if (hhdr -> hb_obj_kind != STUBBORN) {
586 GC_err_printf("GC_debug_change_stubborn arg not stubborn: %p\n", p);
587 ABORT("GC_debug_change_stubborn: arg not stubborn");
589 GC_change_stubborn(q);
592 GC_API void GC_CALL GC_debug_end_stubborn_change(const void *p)
594 const void * q = GC_base_C(p);
598 GC_err_printf("Bad argument: %p to GC_debug_end_stubborn_change\n", p);
599 ABORT("GC_debug_end_stubborn_change: bad arg");
602 if (hhdr -> hb_obj_kind != STUBBORN) {
603 GC_err_printf("debug_end_stubborn_change arg not stubborn: %p\n", p);
604 ABORT("GC_debug_end_stubborn_change: arg not stubborn");
606 GC_end_stubborn_change(q);
609 #else /* !STUBBORN_ALLOC */
611 GC_API void * GC_CALL GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
613 return GC_debug_malloc(lb, OPT_RA s, i);
616 GC_API void GC_CALL GC_debug_change_stubborn(
617 const void * p GC_ATTR_UNUSED) {}
619 GC_API void GC_CALL GC_debug_end_stubborn_change(
620 const void * p GC_ATTR_UNUSED) {}
621 #endif /* !STUBBORN_ALLOC */
623 GC_API void * GC_CALL GC_debug_malloc_atomic(size_t lb, GC_EXTRA_PARAMS)
625 void * result = GC_malloc_atomic(lb + DEBUG_BYTES);
628 GC_err_printf("GC_debug_malloc_atomic(%lu) returning NULL (",
631 GC_err_printf(":%d)\n", i);
634 if (!GC_debugging_started) {
635 GC_start_debugging();
637 ADD_CALL_CHAIN(result, ra);
638 return (GC_store_debug_info(result, (word)lb, s, i));
641 GC_API char * GC_CALL GC_debug_strdup(const char *str, GC_EXTRA_PARAMS)
647 GC_err_printf("strdup(NULL) behavior is undefined\n");
651 lb = strlen(str) + 1;
652 copy = GC_debug_malloc_atomic(lb, OPT_RA s, i);
662 /* strcpy() is deprecated in WinCE */
663 memcpy(copy, str, lb);
668 GC_API char * GC_CALL GC_debug_strndup(const char *str, size_t size,
672 size_t len = strlen(str); /* str is expected to be non-NULL */
675 copy = GC_debug_malloc_atomic(len + 1, OPT_RA s, i);
682 BCOPY(str, copy, len);
687 #ifdef GC_REQUIRE_WCSDUP
688 # include <wchar.h> /* for wcslen() */
690 GC_API wchar_t * GC_CALL GC_debug_wcsdup(const wchar_t *str, GC_EXTRA_PARAMS)
692 size_t lb = (wcslen(str) + 1) * sizeof(wchar_t);
693 wchar_t *copy = GC_debug_malloc_atomic(lb, OPT_RA s, i);
700 BCOPY(str, copy, lb);
703 #endif /* GC_REQUIRE_WCSDUP */
705 GC_API void * GC_CALL GC_debug_malloc_uncollectable(size_t lb,
708 void * result = GC_malloc_uncollectable(lb + UNCOLLECTABLE_DEBUG_BYTES);
711 GC_err_printf("GC_debug_malloc_uncollectable(%lu) returning NULL (",
714 GC_err_printf(":%d)\n", i);
717 if (!GC_debugging_started) {
718 GC_start_debugging();
720 ADD_CALL_CHAIN(result, ra);
721 return (GC_store_debug_info(result, (word)lb, s, i));
724 #ifdef ATOMIC_UNCOLLECTABLE
725 GC_API void * GC_CALL GC_debug_malloc_atomic_uncollectable(size_t lb,
729 GC_malloc_atomic_uncollectable(lb + UNCOLLECTABLE_DEBUG_BYTES);
733 "GC_debug_malloc_atomic_uncollectable(%lu) returning NULL (",
736 GC_err_printf(":%d)\n", i);
739 if (!GC_debugging_started) {
740 GC_start_debugging();
742 ADD_CALL_CHAIN(result, ra);
743 return (GC_store_debug_info(result, (word)lb, s, i));
745 #endif /* ATOMIC_UNCOLLECTABLE */
747 #ifndef GC_FREED_MEM_MARKER
748 # if CPP_WORDSZ == 32
749 # define GC_FREED_MEM_MARKER 0xdeadbeef
751 # define GC_FREED_MEM_MARKER GC_WORD_C(0xEFBEADDEdeadbeef)
755 GC_API void GC_CALL GC_debug_free(void * p)
762 GC_err_printf("Attempt to free invalid pointer %p\n", p);
763 ABORT("Invalid pointer passed to free()");
765 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
767 "GC_debug_free called on pointer %p w/o debugging info\n", p);
769 # ifndef SHORT_DBG_HDRS
770 ptr_t clobbered = GC_check_annotated_obj((oh *)base);
771 word sz = GC_size(base);
772 if (clobbered != 0) {
773 GC_have_errors = TRUE;
774 if (((oh *)base) -> oh_sz == sz) {
775 GC_print_smashed_obj(
776 "GC_debug_free: found previously deallocated (?) object at",
778 return; /* ignore double free */
780 GC_print_smashed_obj("GC_debug_free: found smashed location at",
784 /* Invalidate size (mark the object as deallocated) */
785 ((oh *)base) -> oh_sz = sz;
786 # endif /* SHORT_DBG_HDRS */
789 # ifndef SHORT_DBG_HDRS
790 && ((ptr_t)p - (ptr_t)base != sizeof(oh) || !GC_findleak_delay_free)
796 if (hhdr -> hb_obj_kind == UNCOLLECTABLE
797 # ifdef ATOMIC_UNCOLLECTABLE
798 || hhdr -> hb_obj_kind == AUNCOLLECTABLE
804 size_t obj_sz = BYTES_TO_WORDS(hhdr -> hb_sz - sizeof(oh));
806 for (i = 0; i < obj_sz; ++i)
807 ((word *)p)[i] = GC_FREED_MEM_MARKER;
808 GC_ASSERT((word *)p + i == (word *)(base + hhdr -> hb_sz));
810 } /* !GC_find_leak */
813 #if defined(THREADS) && defined(DBG_HDRS_ALL)
814 /* Used internally; we assume it's called correctly. */
815 GC_INNER void GC_debug_free_inner(void * p)
817 ptr_t base = GC_base(p);
818 GC_ASSERT((ptr_t)p - (ptr_t)base == sizeof(oh));
820 if (!base) ABORT("Invalid GC_debug_free_inner argument");
822 # ifndef SHORT_DBG_HDRS
823 /* Invalidate size */
824 ((oh *)base) -> oh_sz = GC_size(base);
830 GC_API void * GC_CALL GC_debug_realloc(void * p, size_t lb, GC_EXTRA_PARAMS)
836 return(GC_debug_malloc(lb, OPT_RA s, i));
840 GC_err_printf("Attempt to reallocate invalid pointer %p\n", p);
841 ABORT("Invalid pointer passed to realloc()");
843 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
845 "GC_debug_realloc called on pointer %p w/o debugging info\n", p);
846 return(GC_realloc(p, lb));
849 switch (hhdr -> hb_obj_kind) {
850 # ifdef STUBBORN_ALLOC
852 result = GC_debug_malloc_stubborn(lb, OPT_RA s, i);
856 result = GC_debug_malloc(lb, OPT_RA s, i);
859 result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
862 result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
864 # ifdef ATOMIC_UNCOLLECTABLE
866 result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
870 result = NULL; /* initialized to prevent warning. */
871 GC_err_printf("GC_debug_realloc: encountered bad kind\n");
872 ABORT_RET("Bad kind");
875 if (result != NULL) {
877 # ifdef SHORT_DBG_HDRS
878 old_sz = GC_size(base) - sizeof(oh);
880 old_sz = ((oh *)base) -> oh_sz;
882 BCOPY(p, result, old_sz < lb ? old_sz : lb);
888 #ifndef SHORT_DBG_HDRS
890 /* List of smashed (clobbered) locations. We defer printing these, */
891 /* since we can't always print them nicely with the allocation lock */
892 /* held. We put them here instead of in GC_arrays, since it may be */
893 /* useful to be able to look at them with the debugger. */
895 # define MAX_SMASHED 20
897 STATIC ptr_t GC_smashed[MAX_SMASHED] = {0};
898 STATIC unsigned GC_n_smashed = 0;
900 STATIC void GC_add_smashed(ptr_t smashed)
902 GC_ASSERT(GC_is_marked(GC_base(smashed)));
903 /* FIXME: Prevent adding an object while printing smashed list. */
904 GC_smashed[GC_n_smashed] = smashed;
905 if (GC_n_smashed < MAX_SMASHED - 1) ++GC_n_smashed;
906 /* In case of overflow, we keep the first MAX_SMASHED-1 */
907 /* entries plus the last one. */
908 GC_have_errors = TRUE;
911 /* Print all objects on the list. Clear the list. */
912 STATIC void GC_print_all_smashed_proc(void)
916 GC_ASSERT(I_DONT_HOLD_LOCK());
917 if (GC_n_smashed == 0) return;
918 GC_err_printf("GC_check_heap_block: found smashed heap objects:\n");
919 for (i = 0; i < GC_n_smashed; ++i) {
920 GC_print_smashed_obj("", (ptr_t)GC_base(GC_smashed[i]) + sizeof(oh),
928 /* Check all marked objects in the given block for validity */
929 /* Avoid GC_apply_to_each_object for performance reasons. */
930 STATIC void GC_check_heap_block(struct hblk *hbp, word dummy GC_ATTR_UNUSED)
932 struct hblkhdr * hhdr = HDR(hbp);
933 size_t sz = hhdr -> hb_sz;
938 if (sz > MAXOBJBYTES) {
941 plim = hbp->hb_body + HBLKSIZE - sz;
943 /* go through all words in block */
944 for (bit_no = 0; (word)p <= (word)plim;
945 bit_no += MARK_BIT_OFFSET(sz), p += sz) {
946 if (mark_bit_from_hdr(hhdr, bit_no) && GC_HAS_DEBUG_INFO((ptr_t)p)) {
947 ptr_t clobbered = GC_check_annotated_obj((oh *)p);
949 GC_add_smashed(clobbered);
954 /* This assumes that all accessible objects are marked, and that */
955 /* I hold the allocation lock. Normally called by collector. */
956 STATIC void GC_check_heap_proc(void)
958 GC_STATIC_ASSERT((sizeof(oh) & (GRANULE_BYTES - 1)) == 0);
959 /* FIXME: Should we check for twice that alignment? */
960 GC_apply_to_all_blocks(GC_check_heap_block, 0);
963 GC_INNER GC_bool GC_check_leaked(ptr_t base)
970 # if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
971 (*(word *)base & 1) != 0 &&
973 GC_has_other_debug_info(base) >= 0)
974 return TRUE; /* object has leaked */
976 /* Validate freed object's content. */
977 p = (word *)(base + sizeof(oh));
978 obj_sz = BYTES_TO_WORDS(HDR(base)->hb_sz - sizeof(oh));
979 for (i = 0; i < obj_sz; ++i)
980 if (p[i] != GC_FREED_MEM_MARKER) {
981 GC_set_mark_bit(base); /* do not reclaim it in this cycle */
982 GC_add_smashed((ptr_t)(&p[i])); /* alter-after-free detected */
983 break; /* don't report any other smashed locations in the object */
986 return FALSE; /* GC_debug_free() has been called */
989 #endif /* !SHORT_DBG_HDRS */
991 #ifndef GC_NO_FINALIZATION
994 GC_finalization_proc cl_fn;
998 STATIC void * GC_make_closure(GC_finalization_proc fn, void * data)
1000 struct closure * result =
1001 # ifdef DBG_HDRS_ALL
1002 (struct closure *) GC_debug_malloc(sizeof (struct closure),
1005 (struct closure *) GC_malloc(sizeof (struct closure));
1008 result -> cl_fn = fn;
1009 result -> cl_data = data;
1011 return((void *)result);
1014 /* An auxiliary fns to make finalization work correctly with displaced */
1015 /* pointers introduced by the debugging allocators. */
1016 STATIC void GC_CALLBACK GC_debug_invoke_finalizer(void * obj, void * data)
1018 struct closure * cl = (struct closure *) data;
1019 (*(cl -> cl_fn))((void *)((char *)obj + sizeof(oh)), cl -> cl_data);
1022 /* Special finalizer_proc value to detect GC_register_finalizer() failure. */
1023 #define OFN_UNSET (GC_finalization_proc)(signed_word)-1
1025 /* Set ofn and ocd to reflect the values we got back. */
1026 static void store_old(void *obj, GC_finalization_proc my_old_fn,
1027 struct closure *my_old_cd, GC_finalization_proc *ofn,
1030 if (0 != my_old_fn) {
1031 if (my_old_fn == OFN_UNSET) {
1032 /* register_finalizer() failed; (*ofn) and (*ocd) are unchanged. */
1035 if (my_old_fn != GC_debug_invoke_finalizer) {
1036 GC_err_printf("Debuggable object at %p had a non-debug finalizer\n",
1038 /* This should probably be fatal. */
1040 if (ofn) *ofn = my_old_cd -> cl_fn;
1041 if (ocd) *ocd = my_old_cd -> cl_data;
1049 GC_API void GC_CALL GC_debug_register_finalizer(void * obj,
1050 GC_finalization_proc fn,
1051 void * cd, GC_finalization_proc *ofn,
1054 GC_finalization_proc my_old_fn = OFN_UNSET;
1056 ptr_t base = GC_base(obj);
1058 /* We won't collect it, hence finalizer wouldn't be run. */
1063 if ((ptr_t)obj - base != sizeof(oh)) {
1065 "GC_debug_register_finalizer called with non-base-pointer %p\n",
1069 GC_register_finalizer(base, 0, 0, &my_old_fn, &my_old_cd);
1071 cd = GC_make_closure(fn, cd);
1072 if (cd == 0) return; /* out of memory */
1073 GC_register_finalizer(base, GC_debug_invoke_finalizer,
1074 cd, &my_old_fn, &my_old_cd);
1076 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1079 GC_API void GC_CALL GC_debug_register_finalizer_no_order
1080 (void * obj, GC_finalization_proc fn,
1081 void * cd, GC_finalization_proc *ofn,
1084 GC_finalization_proc my_old_fn = OFN_UNSET;
1086 ptr_t base = GC_base(obj);
1088 /* We won't collect it, hence finalizer wouldn't be run. */
1093 if ((ptr_t)obj - base != sizeof(oh)) {
1095 "GC_debug_register_finalizer_no_order called with "
1096 "non-base-pointer %p\n",
1100 GC_register_finalizer_no_order(base, 0, 0, &my_old_fn, &my_old_cd);
1102 cd = GC_make_closure(fn, cd);
1103 if (cd == 0) return; /* out of memory */
1104 GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
1105 cd, &my_old_fn, &my_old_cd);
1107 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1110 GC_API void GC_CALL GC_debug_register_finalizer_unreachable
1111 (void * obj, GC_finalization_proc fn,
1112 void * cd, GC_finalization_proc *ofn,
1115 GC_finalization_proc my_old_fn = OFN_UNSET;
1117 ptr_t base = GC_base(obj);
1119 /* We won't collect it, hence finalizer wouldn't be run. */
1124 if ((ptr_t)obj - base != sizeof(oh)) {
1126 "GC_debug_register_finalizer_unreachable called with "
1127 "non-base-pointer %p\n",
1131 GC_register_finalizer_unreachable(base, 0, 0, &my_old_fn, &my_old_cd);
1133 cd = GC_make_closure(fn, cd);
1134 if (cd == 0) return; /* out of memory */
1135 GC_register_finalizer_unreachable(base, GC_debug_invoke_finalizer,
1136 cd, &my_old_fn, &my_old_cd);
1138 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1141 GC_API void GC_CALL GC_debug_register_finalizer_ignore_self
1142 (void * obj, GC_finalization_proc fn,
1143 void * cd, GC_finalization_proc *ofn,
1146 GC_finalization_proc my_old_fn = OFN_UNSET;
1148 ptr_t base = GC_base(obj);
1150 /* We won't collect it, hence finalizer wouldn't be run. */
1155 if ((ptr_t)obj - base != sizeof(oh)) {
1157 "GC_debug_register_finalizer_ignore_self called with "
1158 "non-base-pointer %p\n", obj);
1161 GC_register_finalizer_ignore_self(base, 0, 0, &my_old_fn, &my_old_cd);
1163 cd = GC_make_closure(fn, cd);
1164 if (cd == 0) return; /* out of memory */
1165 GC_register_finalizer_ignore_self(base, GC_debug_invoke_finalizer,
1166 cd, &my_old_fn, &my_old_cd);
1168 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1171 #endif /* !GC_NO_FINALIZATION */
1173 GC_API void * GC_CALL GC_debug_malloc_replacement(size_t lb)
1175 return GC_debug_malloc(lb, GC_DBG_RA "unknown", 0);
1178 GC_API void * GC_CALL GC_debug_realloc_replacement(void *p, size_t lb)
1180 return GC_debug_realloc(p, lb, GC_DBG_RA "unknown", 0);