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)) {
94 AO_store((volatile AO_t *)&((oh *)dest)->oh_back_ptr,
95 (AO_t)HIDE_BACK_PTR(source));
97 ((oh *)dest) -> oh_back_ptr = HIDE_BACK_PTR(source);
102 GC_INNER void GC_marked_for_finalization(ptr_t dest)
104 GC_store_back_pointer(MARKED_FOR_FINALIZATION, dest);
107 /* Store information about the object referencing dest in *base_p */
109 /* source is root ==> *base_p = address, *offset_p = 0 */
110 /* source is heap object ==> *base_p != 0, *offset_p = offset */
111 /* Returns 1 on success, 0 if source couldn't be determined. */
112 /* Dest can be any address within a heap object. */
113 GC_API GC_ref_kind GC_CALL GC_get_back_ptr_info(void *dest, void **base_p,
116 oh * hdr = (oh *)GC_base(dest);
121 /* Explicitly instruct the code analysis tool that */
122 /* GC_get_back_ptr_info is not expected to be called with an */
123 /* incorrect "dest" value. */
124 if (!hdr) ABORT("Invalid GC_get_back_ptr_info argument");
126 if (!GC_HAS_DEBUG_INFO((ptr_t) hdr)) return GC_NO_SPACE;
127 bp = (ptr_t)GC_REVEAL_POINTER(hdr -> oh_back_ptr);
128 if (MARKED_FOR_FINALIZATION == bp) return GC_FINALIZER_REFD;
129 if (MARKED_FROM_REGISTER == bp) return GC_REFD_FROM_REG;
130 if (NOT_MARKED == bp) return GC_UNREFERENCED;
132 /* Heuristically try to fix off by 1 errors we introduced by */
133 /* insisting on even addresses. */
135 ptr_t alternate_ptr = bp + 1;
136 ptr_t target = *(ptr_t *)bp;
137 ptr_t alternate_target = *(ptr_t *)alternate_ptr;
139 if ((word)alternate_target >= (word)GC_least_plausible_heap_addr
140 && (word)alternate_target <= (word)GC_greatest_plausible_heap_addr
141 && ((word)target < (word)GC_least_plausible_heap_addr
142 || (word)target > (word)GC_greatest_plausible_heap_addr)) {
147 bp_base = (ptr_t)GC_base(bp);
148 if (NULL == bp_base) {
151 return GC_REFD_FROM_ROOT;
153 if (GC_HAS_DEBUG_INFO(bp_base)) bp_base += sizeof(oh);
155 *offset_p = bp - bp_base;
156 return GC_REFD_FROM_HEAP;
160 /* Generate a random heap address. */
161 /* The resulting address is in the heap, but */
162 /* not necessarily inside a valid object. */
163 GC_API void * GC_CALL GC_generate_random_heap_address(void)
166 word heap_offset = RANDOM();
168 if (GC_heapsize > GC_RAND_MAX) {
169 heap_offset *= GC_RAND_MAX;
170 heap_offset += RANDOM();
172 heap_offset %= GC_heapsize;
173 /* This doesn't yield a uniform distribution, especially if */
174 /* e.g. RAND_MAX = 1.5* GC_heapsize. But for typical cases, */
175 /* it's not too bad. */
179 if (i >= GC_n_heap_sects)
180 ABORT("GC_generate_random_heap_address: size inconsistency");
182 size = GC_heap_sects[i].hs_bytes;
183 if (heap_offset < size) {
189 return GC_heap_sects[i].hs_start + heap_offset;
192 /* Generate a random address inside a valid marked heap object. */
193 GC_API void * GC_CALL GC_generate_random_valid_address(void)
198 result = (ptr_t)GC_generate_random_heap_address();
199 base = (ptr_t)GC_base(result);
200 } while (NULL == base || !GC_is_marked(base));
204 /* Print back trace for p */
205 GC_API void GC_CALL GC_print_backtrace(void *p)
213 GC_print_heap_obj((ptr_t)GC_base(current));
216 source = GC_get_back_ptr_info(current, &base, &offset);
217 if (GC_UNREFERENCED == source) {
218 GC_err_printf("Reference could not be found\n");
221 if (GC_NO_SPACE == source) {
222 GC_err_printf("No debug info in object: Can't find reference\n");
225 GC_err_printf("Reachable via %d levels of pointers from ", i);
227 case GC_REFD_FROM_ROOT:
228 GC_err_printf("root at %p\n\n", base);
230 case GC_REFD_FROM_REG:
231 GC_err_printf("root in register\n\n");
233 case GC_FINALIZER_REFD:
234 GC_err_printf("list of finalizable objects\n\n");
236 case GC_REFD_FROM_HEAP:
237 GC_err_printf("offset %ld in object:\n", (long)offset);
238 /* Take GC_base(base) to get real base, i.e. header. */
239 GC_print_heap_obj((ptr_t)GC_base(base));
242 GC_err_printf("INTERNAL ERROR: UNEXPECTED SOURCE!!!!\n");
250 /* Force a garbage collection and generate/print a backtrace */
251 /* from a random heap address. */
252 GC_INNER void GC_generate_random_backtrace_no_gc(void)
255 current = GC_generate_random_valid_address();
256 GC_printf("\n****Chosen address %p in object\n", current);
257 GC_print_backtrace(current);
260 GC_API void GC_CALL GC_generate_random_backtrace(void)
262 if (GC_try_to_collect(GC_never_stop_func) == 0) {
263 GC_err_printf("Cannot generate a backtrace: "
264 "garbage collection is disabled!\n");
267 GC_generate_random_backtrace_no_gc();
270 #endif /* KEEP_BACK_PTRS */
272 # define CROSSES_HBLK(p, sz) \
273 (((word)((p) + sizeof(oh) + (sz) - 1) ^ (word)(p)) >= HBLKSIZE)
275 GC_INNER void *GC_store_debug_info_inner(void *p, word sz GC_ATTR_UNUSED,
276 const char *string, int linenum)
278 word * result = (word *)((oh *)p + 1);
280 GC_ASSERT(I_HOLD_LOCK());
281 GC_ASSERT(GC_size(p) >= sizeof(oh) + sz);
282 GC_ASSERT(!(SMALL_OBJ(sz) && CROSSES_HBLK((ptr_t)p, sz)));
283 # ifdef KEEP_BACK_PTRS
284 ((oh *)p) -> oh_back_ptr = HIDE_BACK_PTR(NOT_MARKED);
286 # ifdef MAKE_BACK_GRAPH
287 ((oh *)p) -> oh_bg_ptr = HIDE_BACK_PTR((ptr_t)0);
289 ((oh *)p) -> oh_string = string;
290 ((oh *)p) -> oh_int = (word)linenum;
291 # ifndef SHORT_DBG_HDRS
292 ((oh *)p) -> oh_sz = sz;
293 ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
294 ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
295 result[SIMPLE_ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
300 /* Check the allocation is successful, store debugging info into p, */
301 /* start the debugging mode (if not yet), and return displaced pointer. */
302 static void *store_debug_info(void *p, size_t lb,
303 const char *fn, GC_EXTRA_PARAMS)
309 GC_err_printf("%s(%lu) returning NULL (%s:%d)\n",
310 fn, (unsigned long)lb, s, i);
314 if (!GC_debugging_started)
315 GC_start_debugging_inner();
316 ADD_CALL_CHAIN(p, ra);
317 result = GC_store_debug_info_inner(p, (word)lb, s, i);
322 #ifndef SHORT_DBG_HDRS
323 /* Check the object with debugging info at ohdr. */
324 /* Return NULL if it's OK. Else return clobbered */
326 STATIC ptr_t GC_check_annotated_obj(oh *ohdr)
328 ptr_t body = (ptr_t)(ohdr + 1);
329 word gc_sz = GC_size((ptr_t)ohdr);
330 if (ohdr -> oh_sz + DEBUG_BYTES > gc_sz) {
331 return((ptr_t)(&(ohdr -> oh_sz)));
333 if (ohdr -> oh_sf != (START_FLAG ^ (word)body)) {
334 return((ptr_t)(&(ohdr -> oh_sf)));
336 if (((word *)ohdr)[BYTES_TO_WORDS(gc_sz)-1] != (END_FLAG ^ (word)body)) {
337 return((ptr_t)((word *)ohdr + BYTES_TO_WORDS(gc_sz)-1));
339 if (((word *)body)[SIMPLE_ROUNDED_UP_WORDS(ohdr -> oh_sz)]
340 != (END_FLAG ^ (word)body)) {
341 return((ptr_t)((word *)body + SIMPLE_ROUNDED_UP_WORDS(ohdr->oh_sz)));
345 #endif /* !SHORT_DBG_HDRS */
347 STATIC GC_describe_type_fn GC_describe_type_fns[MAXOBJKINDS] = {0};
349 GC_API void GC_CALL GC_register_describe_type_fn(int kind,
350 GC_describe_type_fn fn)
352 GC_describe_type_fns[kind] = fn;
355 #define GET_OH_LINENUM(ohdr) ((int)(ohdr)->oh_int)
357 #ifndef SHORT_DBG_HDRS
358 # define IF_NOT_SHORTDBG_HDRS(x) x
359 # define COMMA_IFNOT_SHORTDBG_HDRS(x) /* comma */, x
361 # define IF_NOT_SHORTDBG_HDRS(x) /* empty */
362 # define COMMA_IFNOT_SHORTDBG_HDRS(x) /* empty */
365 /* Print a human-readable description of the object to stderr. */
366 /* p points to somewhere inside an object with the debugging info. */
367 STATIC void GC_print_obj(ptr_t p)
369 oh * ohdr = (oh *)GC_base(p);
373 const char *kind_str;
374 char buffer[GC_TYPE_DESCR_LEN + 1];
376 GC_ASSERT(I_DONT_HOLD_LOCK());
378 if (!ohdr) ABORT("Invalid GC_print_obj argument");
381 q = (ptr_t)(ohdr + 1);
382 /* Print a type description for the object whose client-visible */
384 hhdr = GC_find_header(q);
385 kind = hhdr -> hb_obj_kind;
386 if (0 != GC_describe_type_fns[kind] && GC_is_marked(ohdr)) {
387 /* This should preclude free list objects except with */
388 /* thread-local allocation. */
389 buffer[GC_TYPE_DESCR_LEN] = 0;
390 (GC_describe_type_fns[kind])(q, buffer);
391 GC_ASSERT(buffer[GC_TYPE_DESCR_LEN] == 0);
396 kind_str = "PTRFREE";
402 kind_str = "UNCOLLECTABLE";
404 # ifdef GC_ATOMIC_UNCOLLECTABLE
406 kind_str = "ATOMIC_UNCOLLECTABLE";
411 /* The alternative is to use snprintf(buffer) but it is */
412 /* not quite portable (see vsnprintf in misc.c). */
416 if (NULL != kind_str) {
417 GC_err_printf("%p (%s:%d," IF_NOT_SHORTDBG_HDRS(" sz=%lu,") " %s)\n",
418 (void *)((ptr_t)ohdr + sizeof(oh)),
419 ohdr->oh_string, GET_OH_LINENUM(ohdr) /*, */
420 COMMA_IFNOT_SHORTDBG_HDRS((unsigned long)ohdr->oh_sz),
423 GC_err_printf("%p (%s:%d," IF_NOT_SHORTDBG_HDRS(" sz=%lu,")
424 " kind=%d descr=0x%lx)\n",
425 (void *)((ptr_t)ohdr + sizeof(oh)),
426 ohdr->oh_string, GET_OH_LINENUM(ohdr) /*, */
427 COMMA_IFNOT_SHORTDBG_HDRS((unsigned long)ohdr->oh_sz),
428 kind, (unsigned long)hhdr->hb_descr);
430 PRINT_CALL_CHAIN(ohdr);
433 STATIC void GC_debug_print_heap_obj_proc(ptr_t p)
435 GC_ASSERT(I_DONT_HOLD_LOCK());
436 if (GC_HAS_DEBUG_INFO(p)) {
439 GC_default_print_heap_obj_proc(p);
443 #ifndef SHORT_DBG_HDRS
444 /* Use GC_err_printf and friends to print a description of the object */
445 /* whose client-visible address is p, and which was smashed at */
446 /* clobbered_addr. */
447 STATIC void GC_print_smashed_obj(const char *msg, void *p,
448 ptr_t clobbered_addr)
450 oh * ohdr = (oh *)GC_base(p);
452 GC_ASSERT(I_DONT_HOLD_LOCK());
454 if (!ohdr) ABORT("Invalid GC_print_smashed_obj argument");
456 if ((word)clobbered_addr <= (word)(&ohdr->oh_sz)
457 || ohdr -> oh_string == 0) {
459 "%s %p in or near object at %p(<smashed>, appr. sz = %lu)\n",
460 msg, (void *)clobbered_addr, p,
461 (unsigned long)(GC_size((ptr_t)ohdr) - DEBUG_BYTES));
463 GC_err_printf("%s %p in or near object at %p (%s:%d, sz=%lu)\n",
464 msg, (void *)clobbered_addr, p,
465 (word)(ohdr -> oh_string) < HBLKSIZE ? "(smashed string)" :
466 ohdr -> oh_string[0] == '\0' ? "EMPTY(smashed?)" :
468 GET_OH_LINENUM(ohdr), (unsigned long)(ohdr -> oh_sz));
469 PRINT_CALL_CHAIN(ohdr);
473 STATIC void GC_check_heap_proc (void);
474 STATIC void GC_print_all_smashed_proc (void);
476 STATIC void GC_do_nothing(void) {}
479 GC_INNER void GC_start_debugging_inner(void)
481 GC_ASSERT(I_HOLD_LOCK());
482 # ifndef SHORT_DBG_HDRS
483 GC_check_heap = GC_check_heap_proc;
484 GC_print_all_smashed = GC_print_all_smashed_proc;
486 GC_check_heap = GC_do_nothing;
487 GC_print_all_smashed = GC_do_nothing;
489 GC_print_heap_obj = GC_debug_print_heap_obj_proc;
490 GC_debugging_started = TRUE;
491 GC_register_displacement_inner((word)sizeof(oh));
494 size_t GC_debug_header_size = sizeof(oh);
496 GC_API void GC_CALL GC_debug_register_displacement(size_t offset)
501 GC_register_displacement_inner(offset);
502 GC_register_displacement_inner((word)sizeof(oh) + offset);
507 # if defined(HAVE_DLADDR) && defined(GC_HAVE_RETURN_ADDR_PARENT)
510 STATIC void GC_caller_func_offset(word ad, const char **symp, int *offp)
514 if (ad && dladdr((void *)ad, &caller) && caller.dli_sname != NULL) {
515 *symp = caller.dli_sname;
516 *offp = (int)((char *)ad - (char *)caller.dli_saddr);
523 # define GC_caller_func_offset(ad, symp, offp) (void)(*(symp) = "unknown")
525 #endif /* GC_ADD_CALLER */
527 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc(size_t lb,
532 /* Note that according to malloc() specification, if size is 0 then */
533 /* malloc() returns either NULL, or a unique pointer value that can */
534 /* later be successfully passed to free(). We always do the latter. */
535 result = GC_malloc(SIZET_SAT_ADD(lb, DEBUG_BYTES));
536 # ifdef GC_ADD_CALLER
538 GC_caller_func_offset(ra, &s, &i);
541 return store_debug_info(result, lb, "GC_debug_malloc", OPT_RA s, i);
544 GC_API GC_ATTR_MALLOC void * GC_CALL
545 GC_debug_malloc_ignore_off_page(size_t lb, GC_EXTRA_PARAMS)
547 void * result = GC_malloc_ignore_off_page(SIZET_SAT_ADD(lb, DEBUG_BYTES));
549 return store_debug_info(result, lb, "GC_debug_malloc_ignore_off_page",
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));
559 return store_debug_info(result, lb,
560 "GC_debug_malloc_atomic_ignore_off_page",
564 STATIC void * GC_debug_generic_malloc(size_t lb, int knd, GC_EXTRA_PARAMS)
566 void * result = GC_generic_malloc(SIZET_SAT_ADD(lb, DEBUG_BYTES), knd);
568 return store_debug_info(result, lb, "GC_debug_generic_malloc",
573 /* An allocation function for internal use. Normally internally */
574 /* allocated objects do not have debug information. But in this */
575 /* case, we need to make sure that all objects have debug headers. */
576 GC_INNER void * GC_debug_generic_malloc_inner(size_t lb, int k)
580 GC_ASSERT(I_HOLD_LOCK());
581 result = GC_generic_malloc_inner(SIZET_SAT_ADD(lb, DEBUG_BYTES), k);
582 if (NULL == result) {
583 GC_err_printf("GC internal allocation (%lu bytes) returning NULL\n",
587 if (!GC_debugging_started) {
588 GC_start_debugging_inner();
590 ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
591 return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", 0));
594 GC_INNER void * GC_debug_generic_malloc_inner_ignore_off_page(size_t lb,
599 GC_ASSERT(I_HOLD_LOCK());
600 result = GC_generic_malloc_inner_ignore_off_page(
601 SIZET_SAT_ADD(lb, DEBUG_BYTES), k);
602 if (NULL == result) {
603 GC_err_printf("GC internal allocation (%lu bytes) returning NULL\n",
607 if (!GC_debugging_started) {
608 GC_start_debugging_inner();
610 ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
611 return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", 0));
613 #endif /* DBG_HDRS_ALL */
615 GC_API void * GC_CALL GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
617 return GC_debug_malloc(lb, OPT_RA s, i);
620 GC_API void GC_CALL GC_debug_change_stubborn(
621 const void * p GC_ATTR_UNUSED) {}
623 GC_API void GC_CALL GC_debug_end_stubborn_change(const void *p)
625 const void * q = GC_base_C(p);
628 ABORT_ARG1("GC_debug_end_stubborn_change: bad arg", ": %p", p);
630 GC_end_stubborn_change(q);
633 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_atomic(size_t lb,
636 void * result = GC_malloc_atomic(SIZET_SAT_ADD(lb, DEBUG_BYTES));
638 return store_debug_info(result, lb, "GC_debug_malloc_atomic",
642 GC_API GC_ATTR_MALLOC char * GC_CALL GC_debug_strdup(const char *str,
649 GC_err_printf("strdup(NULL) behavior is undefined\n");
653 lb = strlen(str) + 1;
654 copy = (char *)GC_debug_malloc_atomic(lb, OPT_RA s, i);
661 BCOPY(str, copy, lb);
665 GC_API GC_ATTR_MALLOC char * GC_CALL GC_debug_strndup(const char *str,
666 size_t size, GC_EXTRA_PARAMS)
669 size_t len = strlen(str); /* str is expected to be non-NULL */
672 copy = (char *)GC_debug_malloc_atomic(len + 1, OPT_RA s, i);
680 BCOPY(str, copy, len);
685 #ifdef GC_REQUIRE_WCSDUP
686 # include <wchar.h> /* for wcslen() */
688 GC_API GC_ATTR_MALLOC wchar_t * GC_CALL GC_debug_wcsdup(const wchar_t *str,
691 size_t lb = (wcslen(str) + 1) * sizeof(wchar_t);
692 wchar_t *copy = (wchar_t *)GC_debug_malloc_atomic(lb, OPT_RA s, i);
699 BCOPY(str, copy, lb);
702 #endif /* GC_REQUIRE_WCSDUP */
704 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_uncollectable(size_t lb,
707 void * result = GC_malloc_uncollectable(
708 SIZET_SAT_ADD(lb, UNCOLLECTABLE_DEBUG_BYTES));
710 return store_debug_info(result, lb, "GC_debug_malloc_uncollectable",
714 #ifdef GC_ATOMIC_UNCOLLECTABLE
715 GC_API GC_ATTR_MALLOC void * GC_CALL
716 GC_debug_malloc_atomic_uncollectable(size_t lb, GC_EXTRA_PARAMS)
718 void * result = GC_malloc_atomic_uncollectable(
719 SIZET_SAT_ADD(lb, UNCOLLECTABLE_DEBUG_BYTES));
721 return store_debug_info(result, lb,
722 "GC_debug_malloc_atomic_uncollectable",
725 #endif /* GC_ATOMIC_UNCOLLECTABLE */
727 #ifndef GC_FREED_MEM_MARKER
728 # if CPP_WORDSZ == 32
729 # define GC_FREED_MEM_MARKER 0xdeadbeef
731 # define GC_FREED_MEM_MARKER GC_WORD_C(0xEFBEADDEdeadbeef)
735 GC_API void GC_CALL GC_debug_free(void * p)
740 base = (ptr_t)GC_base(p);
742 # if defined(REDIRECT_MALLOC) \
743 && ((defined(NEED_CALLINFO) && defined(GC_HAVE_BUILTIN_BACKTRACE)) \
744 || defined(GC_LINUX_THREADS) || defined(GC_SOLARIS_THREADS) \
746 /* In some cases, we should ignore objects that do not belong */
747 /* to the GC heap. See the comment in GC_free. */
748 if (!GC_is_heap_ptr(p)) return;
750 ABORT_ARG1("Invalid pointer passed to free()", ": %p", p);
752 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
753 # if defined(REDIRECT_FREE) && defined(USE_PROC_FOR_LIBRARIES)
754 /* TODO: Suppress the warning if free() caller is in libpthread */
758 "GC_debug_free called on pointer %p w/o debugging info\n", p);
760 # ifndef SHORT_DBG_HDRS
761 ptr_t clobbered = GC_check_annotated_obj((oh *)base);
762 word sz = GC_size(base);
763 if (clobbered != 0) {
764 GC_have_errors = TRUE;
765 if (((oh *)base) -> oh_sz == sz) {
766 GC_print_smashed_obj(
767 "GC_debug_free: found previously deallocated (?) object at",
769 return; /* ignore double free */
771 GC_print_smashed_obj("GC_debug_free: found smashed location at",
775 /* Invalidate size (mark the object as deallocated) */
776 ((oh *)base) -> oh_sz = sz;
777 # endif /* SHORT_DBG_HDRS */
780 # ifndef SHORT_DBG_HDRS
781 && ((ptr_t)p - (ptr_t)base != sizeof(oh) || !GC_findleak_delay_free)
787 if (hhdr -> hb_obj_kind == UNCOLLECTABLE
788 # ifdef GC_ATOMIC_UNCOLLECTABLE
789 || hhdr -> hb_obj_kind == AUNCOLLECTABLE
795 word obj_sz = BYTES_TO_WORDS(hhdr->hb_sz - sizeof(oh));
797 for (i = 0; i < obj_sz; ++i)
798 ((word *)p)[i] = GC_FREED_MEM_MARKER;
799 GC_ASSERT((word *)p + i == (word *)(base + hhdr -> hb_sz));
801 } /* !GC_find_leak */
804 #if defined(THREADS) && defined(DBG_HDRS_ALL)
805 /* Used internally; we assume it's called correctly. */
806 GC_INNER void GC_debug_free_inner(void * p)
808 ptr_t base = (ptr_t)GC_base(p);
809 GC_ASSERT((ptr_t)p - (ptr_t)base == sizeof(oh));
811 if (!base) ABORT("Invalid GC_debug_free_inner argument");
813 # ifndef SHORT_DBG_HDRS
814 /* Invalidate size */
815 ((oh *)base) -> oh_sz = GC_size(base);
821 GC_API void * GC_CALL GC_debug_realloc(void * p, size_t lb, GC_EXTRA_PARAMS)
828 return GC_debug_malloc(lb, OPT_RA s, i);
830 if (0 == lb) /* and p != NULL */ {
835 # ifdef GC_ADD_CALLER
837 GC_caller_func_offset(ra, &s, &i);
842 ABORT_ARG1("Invalid pointer passed to realloc()", ": %p", p);
844 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
846 "GC_debug_realloc called on pointer %p w/o debugging info\n", p);
847 return(GC_realloc(p, lb));
850 switch (hhdr -> hb_obj_kind) {
852 result = GC_debug_malloc(lb, OPT_RA s, i);
855 result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
858 result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
860 # ifdef GC_ATOMIC_UNCOLLECTABLE
862 result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
866 result = NULL; /* initialized to prevent warning. */
867 ABORT_RET("GC_debug_realloc: encountered bad kind");
870 if (result != NULL) {
872 # ifdef SHORT_DBG_HDRS
873 old_sz = GC_size(base) - sizeof(oh);
875 old_sz = ((oh *)base) -> oh_sz;
878 BCOPY(p, result, old_sz < lb ? old_sz : lb);
884 GC_API GC_ATTR_MALLOC void * GC_CALL
885 GC_debug_generic_or_special_malloc(size_t lb, int knd, GC_EXTRA_PARAMS)
889 return GC_debug_malloc_atomic(lb, OPT_RA s, i);
891 return GC_debug_malloc(lb, OPT_RA s, i);
893 return GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
894 # ifdef GC_ATOMIC_UNCOLLECTABLE
896 return GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
899 return GC_debug_generic_malloc(lb, knd, OPT_RA s, i);
903 #ifndef SHORT_DBG_HDRS
905 /* List of smashed (clobbered) locations. We defer printing these, */
906 /* since we can't always print them nicely with the allocation lock */
907 /* held. We put them here instead of in GC_arrays, since it may be */
908 /* useful to be able to look at them with the debugger. */
910 # define MAX_SMASHED 20
912 STATIC ptr_t GC_smashed[MAX_SMASHED] = {0};
913 STATIC unsigned GC_n_smashed = 0;
915 STATIC void GC_add_smashed(ptr_t smashed)
917 GC_ASSERT(GC_is_marked(GC_base(smashed)));
918 /* FIXME: Prevent adding an object while printing smashed list. */
919 GC_smashed[GC_n_smashed] = smashed;
920 if (GC_n_smashed < MAX_SMASHED - 1) ++GC_n_smashed;
921 /* In case of overflow, we keep the first MAX_SMASHED-1 */
922 /* entries plus the last one. */
923 GC_have_errors = TRUE;
926 /* Print all objects on the list. Clear the list. */
927 STATIC void GC_print_all_smashed_proc(void)
931 GC_ASSERT(I_DONT_HOLD_LOCK());
932 if (GC_n_smashed == 0) return;
933 GC_err_printf("GC_check_heap_block: found %u smashed heap objects:\n",
935 for (i = 0; i < GC_n_smashed; ++i) {
936 ptr_t base = (ptr_t)GC_base(GC_smashed[i]);
939 if (!base) ABORT("Invalid GC_smashed element");
941 GC_print_smashed_obj("", base + sizeof(oh), GC_smashed[i]);
947 /* Check all marked objects in the given block for validity */
948 /* Avoid GC_apply_to_each_object for performance reasons. */
949 STATIC void GC_check_heap_block(struct hblk *hbp, word dummy GC_ATTR_UNUSED)
951 struct hblkhdr * hhdr = HDR(hbp);
952 word sz = hhdr -> hb_sz;
957 if (sz > MAXOBJBYTES) {
960 plim = hbp->hb_body + HBLKSIZE - sz;
962 /* go through all words in block */
963 for (bit_no = 0; (word)p <= (word)plim;
964 bit_no += MARK_BIT_OFFSET(sz), p += sz) {
965 if (mark_bit_from_hdr(hhdr, bit_no) && GC_HAS_DEBUG_INFO((ptr_t)p)) {
966 ptr_t clobbered = GC_check_annotated_obj((oh *)p);
968 GC_add_smashed(clobbered);
973 /* This assumes that all accessible objects are marked, and that */
974 /* I hold the allocation lock. Normally called by collector. */
975 STATIC void GC_check_heap_proc(void)
977 GC_STATIC_ASSERT((sizeof(oh) & (GRANULE_BYTES - 1)) == 0);
978 /* FIXME: Should we check for twice that alignment? */
979 GC_apply_to_all_blocks(GC_check_heap_block, 0);
982 GC_INNER GC_bool GC_check_leaked(ptr_t base)
989 # if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
990 (*(word *)base & 1) != 0 &&
992 GC_has_other_debug_info(base) >= 0)
993 return TRUE; /* object has leaked */
995 /* Validate freed object's content. */
996 p = (word *)(base + sizeof(oh));
997 obj_sz = BYTES_TO_WORDS(HDR(base)->hb_sz - sizeof(oh));
998 for (i = 0; i < obj_sz; ++i)
999 if (p[i] != GC_FREED_MEM_MARKER) {
1000 GC_set_mark_bit(base); /* do not reclaim it in this cycle */
1001 GC_add_smashed((ptr_t)(&p[i])); /* alter-after-free detected */
1002 break; /* don't report any other smashed locations in the object */
1005 return FALSE; /* GC_debug_free() has been called */
1008 #endif /* !SHORT_DBG_HDRS */
1010 #ifndef GC_NO_FINALIZATION
1013 GC_finalization_proc cl_fn;
1017 STATIC void * GC_make_closure(GC_finalization_proc fn, void * data)
1019 struct closure * result =
1020 # ifdef DBG_HDRS_ALL
1021 (struct closure *) GC_debug_malloc(sizeof (struct closure),
1024 (struct closure *) GC_malloc(sizeof (struct closure));
1027 result -> cl_fn = fn;
1028 result -> cl_data = data;
1030 return((void *)result);
1033 /* An auxiliary fns to make finalization work correctly with displaced */
1034 /* pointers introduced by the debugging allocators. */
1035 STATIC void GC_CALLBACK GC_debug_invoke_finalizer(void * obj, void * data)
1037 struct closure * cl = (struct closure *) data;
1038 (*(cl -> cl_fn))((void *)((char *)obj + sizeof(oh)), cl -> cl_data);
1041 /* Special finalizer_proc value to detect GC_register_finalizer() failure. */
1042 #define OFN_UNSET ((GC_finalization_proc)~(signed_word)0)
1044 /* Set ofn and ocd to reflect the values we got back. */
1045 static void store_old(void *obj, GC_finalization_proc my_old_fn,
1046 struct closure *my_old_cd, GC_finalization_proc *ofn,
1049 if (0 != my_old_fn) {
1050 if (my_old_fn == OFN_UNSET) {
1051 /* register_finalizer() failed; (*ofn) and (*ocd) are unchanged. */
1054 if (my_old_fn != GC_debug_invoke_finalizer) {
1055 GC_err_printf("Debuggable object at %p had a non-debug finalizer\n",
1057 /* This should probably be fatal. */
1059 if (ofn) *ofn = my_old_cd -> cl_fn;
1060 if (ocd) *ocd = my_old_cd -> cl_data;
1068 GC_API void GC_CALL GC_debug_register_finalizer(void * obj,
1069 GC_finalization_proc fn,
1070 void * cd, GC_finalization_proc *ofn,
1073 GC_finalization_proc my_old_fn = OFN_UNSET;
1075 ptr_t base = (ptr_t)GC_base(obj);
1077 /* We won't collect it, hence finalizer wouldn't be run. */
1082 if ((ptr_t)obj - base != sizeof(oh)) {
1083 GC_err_printf("GC_debug_register_finalizer called with"
1084 " non-base-pointer %p\n", obj);
1087 GC_register_finalizer(base, 0, 0, &my_old_fn, &my_old_cd);
1089 cd = GC_make_closure(fn, cd);
1090 if (cd == 0) return; /* out of memory */
1091 GC_register_finalizer(base, GC_debug_invoke_finalizer,
1092 cd, &my_old_fn, &my_old_cd);
1094 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1097 GC_API void GC_CALL GC_debug_register_finalizer_no_order
1098 (void * obj, GC_finalization_proc fn,
1099 void * cd, GC_finalization_proc *ofn,
1102 GC_finalization_proc my_old_fn = OFN_UNSET;
1104 ptr_t base = (ptr_t)GC_base(obj);
1106 /* We won't collect it, hence finalizer wouldn't be run. */
1111 if ((ptr_t)obj - base != sizeof(oh)) {
1112 GC_err_printf("GC_debug_register_finalizer_no_order called with"
1113 " non-base-pointer %p\n", obj);
1116 GC_register_finalizer_no_order(base, 0, 0, &my_old_fn, &my_old_cd);
1118 cd = GC_make_closure(fn, cd);
1119 if (cd == 0) return; /* out of memory */
1120 GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
1121 cd, &my_old_fn, &my_old_cd);
1123 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1126 GC_API void GC_CALL GC_debug_register_finalizer_unreachable
1127 (void * obj, GC_finalization_proc fn,
1128 void * cd, GC_finalization_proc *ofn,
1131 GC_finalization_proc my_old_fn = OFN_UNSET;
1133 ptr_t base = (ptr_t)GC_base(obj);
1135 /* We won't collect it, hence finalizer wouldn't be run. */
1140 if ((ptr_t)obj - base != sizeof(oh)) {
1141 GC_err_printf("GC_debug_register_finalizer_unreachable called with"
1142 " non-base-pointer %p\n", obj);
1145 GC_register_finalizer_unreachable(base, 0, 0, &my_old_fn, &my_old_cd);
1147 cd = GC_make_closure(fn, cd);
1148 if (cd == 0) return; /* out of memory */
1149 GC_register_finalizer_unreachable(base, GC_debug_invoke_finalizer,
1150 cd, &my_old_fn, &my_old_cd);
1152 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1155 GC_API void GC_CALL GC_debug_register_finalizer_ignore_self
1156 (void * obj, GC_finalization_proc fn,
1157 void * cd, GC_finalization_proc *ofn,
1160 GC_finalization_proc my_old_fn = OFN_UNSET;
1162 ptr_t base = (ptr_t)GC_base(obj);
1164 /* We won't collect it, hence finalizer wouldn't be run. */
1169 if ((ptr_t)obj - base != sizeof(oh)) {
1170 GC_err_printf("GC_debug_register_finalizer_ignore_self called with"
1171 " non-base-pointer %p\n", obj);
1174 GC_register_finalizer_ignore_self(base, 0, 0, &my_old_fn, &my_old_cd);
1176 cd = GC_make_closure(fn, cd);
1177 if (cd == 0) return; /* out of memory */
1178 GC_register_finalizer_ignore_self(base, GC_debug_invoke_finalizer,
1179 cd, &my_old_fn, &my_old_cd);
1181 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1184 #endif /* !GC_NO_FINALIZATION */
1186 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_replacement(size_t lb)
1188 return GC_debug_malloc(lb, GC_DBG_EXTRAS);
1191 GC_API void * GC_CALL GC_debug_realloc_replacement(void *p, size_t lb)
1193 return GC_debug_realloc(p, lb, GC_DBG_EXTRAS);