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 (alternate_target >= GC_least_plausible_heap_addr
119 && alternate_target <= GC_greatest_plausible_heap_addr
120 && (target < GC_least_plausible_heap_addr
121 || target > 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();
146 if (GC_heapsize > RAND_MAX) {
147 heap_offset *= RAND_MAX;
148 heap_offset += RANDOM();
150 heap_offset %= GC_heapsize;
151 /* This doesn't yield a uniform distribution, especially if */
152 /* e.g. RAND_MAX = 1.5* GC_heapsize. But for typical cases, */
153 /* it's not too bad. */
154 for (i = 0; i < GC_n_heap_sects; ++ i) {
155 size_t size = GC_heap_sects[i].hs_bytes;
156 if (heap_offset < size) {
157 return GC_heap_sects[i].hs_start + heap_offset;
162 ABORT("GC_generate_random_heap_address: size inconsistency");
167 /* Generate a random address inside a valid marked heap object. */
168 GC_API void * GC_CALL GC_generate_random_valid_address(void)
173 result = GC_generate_random_heap_address();
174 base = GC_base(result);
175 } while (base == 0 || !GC_is_marked(base));
179 /* Print back trace for p */
180 GC_API void GC_CALL GC_print_backtrace(void *p)
188 GC_print_heap_obj(GC_base(current));
191 source = GC_get_back_ptr_info(current, &base, &offset);
192 if (GC_UNREFERENCED == source) {
193 GC_err_printf("Reference could not be found\n");
196 if (GC_NO_SPACE == source) {
197 GC_err_printf("No debug info in object: Can't find reference\n");
200 GC_err_printf("Reachable via %d levels of pointers from ", i);
202 case GC_REFD_FROM_ROOT:
203 GC_err_printf("root at %p\n\n", base);
205 case GC_REFD_FROM_REG:
206 GC_err_printf("root in register\n\n");
208 case GC_FINALIZER_REFD:
209 GC_err_printf("list of finalizable objects\n\n");
211 case GC_REFD_FROM_HEAP:
212 GC_err_printf("offset %ld in object:\n", (unsigned long)offset);
213 /* Take GC_base(base) to get real base, i.e. header. */
214 GC_print_heap_obj(GC_base(base));
218 GC_err_printf("INTERNAL ERROR: UNEXPECTED SOURCE!!!!\n");
226 /* Force a garbage collection and generate a backtrace from a */
227 /* random heap address. */
228 GC_INNER void GC_generate_random_backtrace_no_gc(void)
231 current = GC_generate_random_valid_address();
232 GC_printf("\n****Chosen address %p in object\n", current);
233 GC_print_backtrace(current);
236 GC_API void GC_CALL GC_generate_random_backtrace(void)
238 if (GC_try_to_collect(GC_never_stop_func) == 0) {
239 GC_err_printf("Cannot generate a backtrace: "
240 "garbage collection is disabled!\n");
243 GC_generate_random_backtrace_no_gc();
246 #endif /* KEEP_BACK_PTRS */
248 # define CROSSES_HBLK(p, sz) \
249 (((word)(p + sizeof(oh) + sz - 1) ^ (word)p) >= HBLKSIZE)
251 /* Store debugging info into p. Return displaced pointer. */
252 /* This version assumes we do hold the allocation lock. */
253 STATIC ptr_t GC_store_debug_info_inner(ptr_t p, word sz, const char *string,
256 word * result = (word *)((oh *)p + 1);
258 GC_ASSERT(GC_size(p) >= sizeof(oh) + sz);
259 GC_ASSERT(!(SMALL_OBJ(sz) && CROSSES_HBLK(p, sz)));
260 # ifdef KEEP_BACK_PTRS
261 ((oh *)p) -> oh_back_ptr = HIDE_BACK_PTR(NOT_MARKED);
263 # ifdef MAKE_BACK_GRAPH
264 ((oh *)p) -> oh_bg_ptr = HIDE_BACK_PTR((ptr_t)0);
266 ((oh *)p) -> oh_string = string;
267 ((oh *)p) -> oh_int = (word)linenum;
268 # ifndef SHORT_DBG_HDRS
269 ((oh *)p) -> oh_sz = sz;
270 ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
271 ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
272 result[SIMPLE_ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
274 return((ptr_t)result);
277 GC_INNER ptr_t GC_store_debug_info(ptr_t p, word sz, const char *string,
284 result = GC_store_debug_info_inner(p, sz, string, linenum);
289 #ifndef SHORT_DBG_HDRS
290 /* Check the object with debugging info at ohdr. */
291 /* Return NULL if it's OK. Else return clobbered */
293 STATIC ptr_t GC_check_annotated_obj(oh *ohdr)
295 ptr_t body = (ptr_t)(ohdr + 1);
296 word gc_sz = GC_size((ptr_t)ohdr);
297 if (ohdr -> oh_sz + DEBUG_BYTES > gc_sz) {
298 return((ptr_t)(&(ohdr -> oh_sz)));
300 if (ohdr -> oh_sf != (START_FLAG ^ (word)body)) {
301 return((ptr_t)(&(ohdr -> oh_sf)));
303 if (((word *)ohdr)[BYTES_TO_WORDS(gc_sz)-1] != (END_FLAG ^ (word)body)) {
304 return((ptr_t)((word *)ohdr + BYTES_TO_WORDS(gc_sz)-1));
306 if (((word *)body)[SIMPLE_ROUNDED_UP_WORDS(ohdr -> oh_sz)]
307 != (END_FLAG ^ (word)body)) {
308 return((ptr_t)((word *)body + SIMPLE_ROUNDED_UP_WORDS(ohdr->oh_sz)));
312 #endif /* !SHORT_DBG_HDRS */
314 STATIC GC_describe_type_fn GC_describe_type_fns[MAXOBJKINDS] = {0};
316 GC_API void GC_CALL GC_register_describe_type_fn(int kind,
317 GC_describe_type_fn fn)
319 GC_describe_type_fns[kind] = fn;
322 /* Print a type description for the object whose client-visible address */
324 STATIC void GC_print_type(ptr_t p)
326 hdr * hhdr = GC_find_header(p);
327 char buffer[GC_TYPE_DESCR_LEN + 1];
328 int kind = hhdr -> hb_obj_kind;
330 if (0 != GC_describe_type_fns[kind] && GC_is_marked(GC_base(p))) {
331 /* This should preclude free list objects except with */
332 /* thread-local allocation. */
333 buffer[GC_TYPE_DESCR_LEN] = 0;
334 (GC_describe_type_fns[kind])(p, buffer);
335 GC_ASSERT(buffer[GC_TYPE_DESCR_LEN] == 0);
340 GC_err_puts("PTRFREE");
343 GC_err_puts("NORMAL");
346 GC_err_puts("UNCOLLECTABLE");
348 # ifdef ATOMIC_UNCOLLECTABLE
350 GC_err_puts("ATOMIC UNCOLLECTABLE");
354 GC_err_puts("STUBBORN");
357 GC_err_printf("kind=%d descr=0x%lx", kind,
358 (unsigned long)(hhdr -> hb_descr));
363 #define GET_OH_LINENUM(ohdr) ((int)(ohdr)->oh_int)
365 /* Print a human-readable description of the object to stderr. p points */
366 /* 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);
371 GC_ASSERT(I_DONT_HOLD_LOCK());
373 if (!ohdr) ABORT("Invalid GC_print_obj argument");
375 GC_err_printf("%p (", ((ptr_t)ohdr + sizeof(oh)));
376 GC_err_puts(ohdr -> oh_string);
377 # ifdef SHORT_DBG_HDRS
378 GC_err_printf(":%d, ", GET_OH_LINENUM(ohdr));
380 GC_err_printf(":%d, sz=%lu, ",
381 GET_OH_LINENUM(ohdr), (unsigned long)(ohdr -> oh_sz));
383 GC_print_type((ptr_t)(ohdr + 1));
385 PRINT_CALL_CHAIN(ohdr);
388 STATIC void GC_debug_print_heap_obj_proc(ptr_t p)
390 GC_ASSERT(I_DONT_HOLD_LOCK());
391 if (GC_HAS_DEBUG_INFO(p)) {
394 GC_default_print_heap_obj_proc(p);
398 #ifndef SHORT_DBG_HDRS
399 /* Use GC_err_printf and friends to print a description of the object */
400 /* whose client-visible address is p, and which was smashed at */
401 /* clobbered_addr. */
402 STATIC void GC_print_smashed_obj(const char *msg, ptr_t p,
403 ptr_t clobbered_addr)
405 oh * ohdr = (oh *)GC_base(p);
407 GC_ASSERT(I_DONT_HOLD_LOCK());
409 if (!ohdr) ABORT("Invalid GC_print_smashed_obj argument");
411 if (clobbered_addr <= (ptr_t)(&(ohdr -> oh_sz))
412 || ohdr -> oh_string == 0) {
414 "%s %p in or near object at %p(<smashed>, appr. sz = %lu)\n",
415 msg, clobbered_addr, p,
416 (unsigned long)(GC_size((ptr_t)ohdr) - DEBUG_BYTES));
418 GC_err_printf("%s %p in or near object at %p (%s:%d, sz=%lu)\n",
419 msg, clobbered_addr, p,
420 (word)(ohdr -> oh_string) < HBLKSIZE ? "(smashed string)" :
421 ohdr -> oh_string[0] == '\0' ? "EMPTY(smashed?)" :
423 GET_OH_LINENUM(ohdr), (unsigned long)(ohdr -> oh_sz));
424 PRINT_CALL_CHAIN(ohdr);
429 #ifndef SHORT_DBG_HDRS
430 STATIC void GC_check_heap_proc (void);
431 STATIC void GC_print_all_smashed_proc (void);
433 STATIC void GC_do_nothing(void) {}
436 GC_INNER void GC_start_debugging(void)
438 # ifndef SHORT_DBG_HDRS
439 GC_check_heap = GC_check_heap_proc;
440 GC_print_all_smashed = GC_print_all_smashed_proc;
442 GC_check_heap = GC_do_nothing;
443 GC_print_all_smashed = GC_do_nothing;
445 GC_print_heap_obj = GC_debug_print_heap_obj_proc;
446 GC_debugging_started = TRUE;
447 GC_register_displacement((word)sizeof(oh));
450 size_t GC_debug_header_size = sizeof(oh);
452 GC_API void GC_CALL GC_debug_register_displacement(size_t offset)
454 GC_register_displacement(offset);
455 GC_register_displacement((word)sizeof(oh) + offset);
458 GC_API void * GC_CALL GC_debug_malloc(size_t lb, GC_EXTRA_PARAMS)
461 /* Note that according to malloc() specification, if size is 0 then */
462 /* malloc() returns either NULL, or a unique pointer value that can */
463 /* later be successfully passed to free(). We always do the latter. */
464 result = GC_malloc(lb + DEBUG_BYTES);
467 GC_err_printf("GC_debug_malloc(%lu) returning NULL (",
470 GC_err_printf(":%ld)\n", (unsigned long)i);
473 if (!GC_debugging_started) {
474 GC_start_debugging();
476 ADD_CALL_CHAIN(result, ra);
477 return (GC_store_debug_info(result, (word)lb, s, i));
480 GC_API void * GC_CALL GC_debug_malloc_ignore_off_page(size_t lb,
483 void * result = GC_malloc_ignore_off_page(lb + DEBUG_BYTES);
486 GC_err_printf("GC_debug_malloc_ignore_off_page(%lu) returning NULL (",
489 GC_err_printf(":%lu)\n", (unsigned long)i);
492 if (!GC_debugging_started) {
493 GC_start_debugging();
495 ADD_CALL_CHAIN(result, ra);
496 return (GC_store_debug_info(result, (word)lb, s, i));
499 GC_API void * GC_CALL GC_debug_malloc_atomic_ignore_off_page(size_t lb,
502 void * result = GC_malloc_atomic_ignore_off_page(lb + DEBUG_BYTES);
505 GC_err_printf("GC_debug_malloc_atomic_ignore_off_page(%lu)"
506 " returning NULL (", (unsigned long)lb);
508 GC_err_printf(":%lu)\n", (unsigned long)i);
511 if (!GC_debugging_started) {
512 GC_start_debugging();
514 ADD_CALL_CHAIN(result, ra);
515 return (GC_store_debug_info(result, (word)lb, s, i));
519 /* An allocation function for internal use. Normally internally */
520 /* allocated objects do not have debug information. But in this */
521 /* case, we need to make sure that all objects have debug headers. */
522 /* We assume debugging was started in collector initialization, and */
523 /* we already hold the GC lock. */
524 GC_INNER void * GC_debug_generic_malloc_inner(size_t lb, int k)
526 void * result = GC_generic_malloc_inner(lb + DEBUG_BYTES, k);
529 GC_err_printf("GC internal allocation (%lu bytes) returning NULL\n",
533 ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
534 return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", 0));
537 GC_INNER void * GC_debug_generic_malloc_inner_ignore_off_page(size_t lb,
540 void * result = GC_generic_malloc_inner_ignore_off_page(
541 lb + DEBUG_BYTES, k);
544 GC_err_printf("GC internal allocation (%lu bytes) returning NULL\n",
548 ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
549 return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", 0));
551 #endif /* DBG_HDRS_ALL */
553 #ifdef STUBBORN_ALLOC
554 GC_API void * GC_CALL GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
556 void * result = GC_malloc_stubborn(lb + DEBUG_BYTES);
559 GC_err_printf("GC_debug_malloc(%lu) returning NULL (",
562 GC_err_printf(":%lu)\n", (unsigned long)i);
565 if (!GC_debugging_started) {
566 GC_start_debugging();
568 ADD_CALL_CHAIN(result, ra);
569 return (GC_store_debug_info(result, (word)lb, s, i));
572 GC_API void GC_CALL GC_debug_change_stubborn(void *p)
574 void * q = GC_base(p);
578 GC_err_printf("Bad argument: %p to GC_debug_change_stubborn\n", p);
579 ABORT("GC_debug_change_stubborn: bad arg");
582 if (hhdr -> hb_obj_kind != STUBBORN) {
583 GC_err_printf("GC_debug_change_stubborn arg not stubborn: %p\n", p);
584 ABORT("GC_debug_change_stubborn: arg not stubborn");
586 GC_change_stubborn(q);
589 GC_API void GC_CALL GC_debug_end_stubborn_change(void *p)
591 void * q = GC_base(p);
595 GC_err_printf("Bad argument: %p to GC_debug_end_stubborn_change\n", p);
596 ABORT("GC_debug_end_stubborn_change: bad arg");
599 if (hhdr -> hb_obj_kind != STUBBORN) {
600 GC_err_printf("debug_end_stubborn_change arg not stubborn: %p\n", p);
601 ABORT("GC_debug_end_stubborn_change: arg not stubborn");
603 GC_end_stubborn_change(q);
606 #else /* !STUBBORN_ALLOC */
608 GC_API void * GC_CALL GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
610 return GC_debug_malloc(lb, OPT_RA s, i);
614 GC_API void GC_CALL GC_debug_change_stubborn(void *p) {}
617 GC_API void GC_CALL GC_debug_end_stubborn_change(void *p) {}
618 #endif /* !STUBBORN_ALLOC */
620 GC_API void * GC_CALL GC_debug_malloc_atomic(size_t lb, GC_EXTRA_PARAMS)
622 void * result = GC_malloc_atomic(lb + DEBUG_BYTES);
625 GC_err_printf("GC_debug_malloc_atomic(%lu) returning NULL (",
628 GC_err_printf(":%lu)\n", (unsigned long)i);
631 if (!GC_debugging_started) {
632 GC_start_debugging();
634 ADD_CALL_CHAIN(result, ra);
635 return (GC_store_debug_info(result, (word)lb, s, i));
638 GC_API char * GC_CALL GC_debug_strdup(const char *str, GC_EXTRA_PARAMS)
644 GC_err_printf("strdup(NULL) behavior is undefined\n");
648 lb = strlen(str) + 1;
649 copy = GC_debug_malloc_atomic(lb, OPT_RA s, i);
659 /* strcpy() is deprecated in WinCE */
660 memcpy(copy, str, lb);
665 GC_API char * GC_CALL GC_debug_strndup(const char *str, size_t size,
669 size_t len = strlen(str); /* str is expected to be non-NULL */
672 copy = GC_debug_malloc_atomic(len + 1, OPT_RA s, i);
679 BCOPY(str, copy, len);
684 #ifdef GC_REQUIRE_WCSDUP
685 # include <wchar.h> /* for wcslen() */
687 GC_API wchar_t * GC_CALL GC_debug_wcsdup(const wchar_t *str, GC_EXTRA_PARAMS)
689 size_t lb = (wcslen(str) + 1) * sizeof(wchar_t);
690 wchar_t *copy = GC_debug_malloc_atomic(lb, OPT_RA s, i);
697 BCOPY(str, copy, lb);
700 #endif /* GC_REQUIRE_WCSDUP */
702 GC_API void * GC_CALL GC_debug_malloc_uncollectable(size_t lb,
705 void * result = GC_malloc_uncollectable(lb + UNCOLLECTABLE_DEBUG_BYTES);
708 GC_err_printf("GC_debug_malloc_uncollectable(%lu) returning NULL (",
711 GC_err_printf(":%lu)\n", (unsigned long)i);
714 if (!GC_debugging_started) {
715 GC_start_debugging();
717 ADD_CALL_CHAIN(result, ra);
718 return (GC_store_debug_info(result, (word)lb, s, i));
721 #ifdef ATOMIC_UNCOLLECTABLE
722 GC_API void * GC_CALL GC_debug_malloc_atomic_uncollectable(size_t lb,
726 GC_malloc_atomic_uncollectable(lb + UNCOLLECTABLE_DEBUG_BYTES);
730 "GC_debug_malloc_atomic_uncollectable(%lu) returning NULL (",
733 GC_err_printf(":%lu)\n", (unsigned long)i);
736 if (!GC_debugging_started) {
737 GC_start_debugging();
739 ADD_CALL_CHAIN(result, ra);
740 return (GC_store_debug_info(result, (word)lb, s, i));
742 #endif /* ATOMIC_UNCOLLECTABLE */
744 #ifndef GC_FREED_MEM_MARKER
745 # if CPP_WORDSZ == 32
746 # define GC_FREED_MEM_MARKER 0xdeadbeef
748 # define GC_FREED_MEM_MARKER GC_WORD_C(0xEFBEADDEdeadbeef)
752 GC_API void GC_CALL GC_debug_free(void * p)
759 GC_err_printf("Attempt to free invalid pointer %p\n", p);
760 ABORT("Invalid pointer passed to free()");
762 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
764 "GC_debug_free called on pointer %p w/o debugging info\n", p);
766 # ifndef SHORT_DBG_HDRS
767 ptr_t clobbered = GC_check_annotated_obj((oh *)base);
768 word sz = GC_size(base);
769 if (clobbered != 0) {
770 GC_have_errors = TRUE;
771 if (((oh *)base) -> oh_sz == sz) {
772 GC_print_smashed_obj(
773 "GC_debug_free: found previously deallocated (?) object at",
775 return; /* ignore double free */
777 GC_print_smashed_obj("GC_debug_free: found smashed location at",
781 /* Invalidate size (mark the object as deallocated) */
782 ((oh *)base) -> oh_sz = sz;
783 # endif /* SHORT_DBG_HDRS */
786 # ifndef SHORT_DBG_HDRS
787 && ((ptr_t)p - (ptr_t)base != sizeof(oh) || !GC_findleak_delay_free)
793 if (hhdr -> hb_obj_kind == UNCOLLECTABLE
794 # ifdef ATOMIC_UNCOLLECTABLE
795 || hhdr -> hb_obj_kind == AUNCOLLECTABLE
801 size_t obj_sz = BYTES_TO_WORDS(hhdr -> hb_sz - sizeof(oh));
803 for (i = 0; i < obj_sz; ++i)
804 ((word *)p)[i] = GC_FREED_MEM_MARKER;
805 GC_ASSERT((word *)p + i == (word *)(base + hhdr -> hb_sz));
807 } /* !GC_find_leak */
810 #if defined(THREADS) && defined(DBG_HDRS_ALL)
811 /* Used internally; we assume it's called correctly. */
812 GC_INNER void GC_debug_free_inner(void * p)
814 ptr_t base = GC_base(p);
815 GC_ASSERT((ptr_t)p - (ptr_t)base == sizeof(oh));
817 if (!base) ABORT("Invalid GC_debug_free_inner argument");
819 # ifndef SHORT_DBG_HDRS
820 /* Invalidate size */
821 ((oh *)base) -> oh_sz = GC_size(base);
827 GC_API void * GC_CALL GC_debug_realloc(void * p, size_t lb, GC_EXTRA_PARAMS)
833 return(GC_debug_malloc(lb, OPT_RA s, i));
837 GC_err_printf("Attempt to reallocate invalid pointer %p\n", p);
838 ABORT("Invalid pointer passed to realloc()");
840 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
842 "GC_debug_realloc called on pointer %p w/o debugging info\n", p);
843 return(GC_realloc(p, lb));
846 switch (hhdr -> hb_obj_kind) {
847 # ifdef STUBBORN_ALLOC
849 result = GC_debug_malloc_stubborn(lb, OPT_RA s, i);
853 result = GC_debug_malloc(lb, OPT_RA s, i);
856 result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
859 result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
861 # ifdef ATOMIC_UNCOLLECTABLE
863 result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
867 result = NULL; /* initialized to prevent warning. */
868 GC_err_printf("GC_debug_realloc: encountered bad kind\n");
872 if (result != NULL) {
874 # ifdef SHORT_DBG_HDRS
875 old_sz = GC_size(base) - sizeof(oh);
877 old_sz = ((oh *)base) -> oh_sz;
879 BCOPY(p, result, old_sz < lb ? old_sz : lb);
885 #ifndef SHORT_DBG_HDRS
887 /* List of smashed (clobbered) locations. We defer printing these, */
888 /* since we can't always print them nicely with the allocation lock */
889 /* held. We put them here instead of in GC_arrays, since it may be */
890 /* useful to be able to look at them with the debugger. */
892 # define MAX_SMASHED 20
894 STATIC ptr_t GC_smashed[MAX_SMASHED] = {0};
895 STATIC unsigned GC_n_smashed = 0;
897 STATIC void GC_add_smashed(ptr_t smashed)
899 GC_ASSERT(GC_is_marked(GC_base(smashed)));
900 /* FIXME: Prevent adding an object while printing smashed list. */
901 GC_smashed[GC_n_smashed] = smashed;
902 if (GC_n_smashed < MAX_SMASHED - 1) ++GC_n_smashed;
903 /* In case of overflow, we keep the first MAX_SMASHED-1 */
904 /* entries plus the last one. */
905 GC_have_errors = TRUE;
908 /* Print all objects on the list. Clear the list. */
909 STATIC void GC_print_all_smashed_proc(void)
913 GC_ASSERT(I_DONT_HOLD_LOCK());
914 if (GC_n_smashed == 0) return;
915 GC_err_printf("GC_check_heap_block: found smashed heap objects:\n");
916 for (i = 0; i < GC_n_smashed; ++i) {
917 GC_print_smashed_obj("", (ptr_t)GC_base(GC_smashed[i]) + sizeof(oh),
925 /* Check all marked objects in the given block for validity */
926 /* Avoid GC_apply_to_each_object for performance reasons. */
928 STATIC void GC_check_heap_block(struct hblk *hbp, word dummy)
930 struct hblkhdr * hhdr = HDR(hbp);
931 size_t sz = hhdr -> hb_sz;
936 if (sz > MAXOBJBYTES) {
939 plim = hbp->hb_body + HBLKSIZE - sz;
941 /* go through all words in block */
942 for (bit_no = 0; p <= plim; bit_no += MARK_BIT_OFFSET(sz), p += sz) {
943 if (mark_bit_from_hdr(hhdr, bit_no) && GC_HAS_DEBUG_INFO((ptr_t)p)) {
944 ptr_t clobbered = GC_check_annotated_obj((oh *)p);
946 GC_add_smashed(clobbered);
951 /* This assumes that all accessible objects are marked, and that */
952 /* I hold the allocation lock. Normally called by collector. */
953 STATIC void GC_check_heap_proc(void)
955 GC_STATIC_ASSERT((sizeof(oh) & (GRANULE_BYTES - 1)) == 0);
956 /* FIXME: Should we check for twice that alignment? */
957 GC_apply_to_all_blocks(GC_check_heap_block, 0);
960 GC_INNER GC_bool GC_check_leaked(ptr_t base)
967 # if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
968 (*(word *)base & 1) != 0 &&
970 GC_has_other_debug_info(base) >= 0)
971 return TRUE; /* object has leaked */
973 /* Validate freed object's content. */
974 p = (word *)(base + sizeof(oh));
975 obj_sz = BYTES_TO_WORDS(HDR(base)->hb_sz - sizeof(oh));
976 for (i = 0; i < obj_sz; ++i)
977 if (p[i] != GC_FREED_MEM_MARKER) {
978 GC_set_mark_bit(base); /* do not reclaim it in this cycle */
979 GC_add_smashed((ptr_t)(&p[i])); /* alter-after-free detected */
980 break; /* don't report any other smashed locations in the object */
983 return FALSE; /* GC_debug_free() has been called */
986 #endif /* !SHORT_DBG_HDRS */
989 GC_finalization_proc cl_fn;
993 STATIC void * GC_make_closure(GC_finalization_proc fn, void * data)
995 struct closure * result =
997 (struct closure *) GC_debug_malloc(sizeof (struct closure),
1000 (struct closure *) GC_malloc(sizeof (struct closure));
1003 result -> cl_fn = fn;
1004 result -> cl_data = data;
1006 return((void *)result);
1009 /* An auxiliary fns to make finalization work correctly with displaced */
1010 /* pointers introduced by the debugging allocators. */
1011 STATIC void GC_CALLBACK GC_debug_invoke_finalizer(void * obj, void * data)
1013 struct closure * cl = (struct closure *) data;
1014 (*(cl -> cl_fn))((void *)((char *)obj + sizeof(oh)), cl -> cl_data);
1017 /* Special finalizer_proc value to detect GC_register_finalizer() failure. */
1018 #define OFN_UNSET (GC_finalization_proc)(signed_word)-1
1020 /* Set ofn and ocd to reflect the values we got back. */
1021 static void store_old(void *obj, GC_finalization_proc my_old_fn,
1022 struct closure *my_old_cd, GC_finalization_proc *ofn,
1025 if (0 != my_old_fn) {
1026 if (my_old_fn == OFN_UNSET) {
1027 /* register_finalizer() failed; (*ofn) and (*ocd) are unchanged. */
1030 if (my_old_fn != GC_debug_invoke_finalizer) {
1031 GC_err_printf("Debuggable object at %p had a non-debug finalizer\n",
1033 /* This should probably be fatal. */
1035 if (ofn) *ofn = my_old_cd -> cl_fn;
1036 if (ocd) *ocd = my_old_cd -> cl_data;
1044 GC_API void GC_CALL GC_debug_register_finalizer(void * obj,
1045 GC_finalization_proc fn,
1046 void * cd, GC_finalization_proc *ofn,
1049 GC_finalization_proc my_old_fn = OFN_UNSET;
1051 ptr_t base = GC_base(obj);
1053 /* We won't collect it, hence finalizer wouldn't be run. */
1058 if ((ptr_t)obj - base != sizeof(oh)) {
1060 "GC_debug_register_finalizer called with non-base-pointer %p\n",
1064 GC_register_finalizer(base, 0, 0, &my_old_fn, &my_old_cd);
1066 cd = GC_make_closure(fn, cd);
1067 if (cd == 0) return; /* out of memory */
1068 GC_register_finalizer(base, GC_debug_invoke_finalizer,
1069 cd, &my_old_fn, &my_old_cd);
1071 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1074 GC_API void GC_CALL GC_debug_register_finalizer_no_order
1075 (void * obj, GC_finalization_proc fn,
1076 void * cd, GC_finalization_proc *ofn,
1079 GC_finalization_proc my_old_fn = OFN_UNSET;
1081 ptr_t base = GC_base(obj);
1083 /* We won't collect it, hence finalizer wouldn't be run. */
1088 if ((ptr_t)obj - base != sizeof(oh)) {
1090 "GC_debug_register_finalizer_no_order called with "
1091 "non-base-pointer %p\n",
1095 GC_register_finalizer_no_order(base, 0, 0, &my_old_fn, &my_old_cd);
1097 cd = GC_make_closure(fn, cd);
1098 if (cd == 0) return; /* out of memory */
1099 GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
1100 cd, &my_old_fn, &my_old_cd);
1102 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1105 GC_API void GC_CALL GC_debug_register_finalizer_unreachable
1106 (void * obj, GC_finalization_proc fn,
1107 void * cd, GC_finalization_proc *ofn,
1110 GC_finalization_proc my_old_fn = OFN_UNSET;
1112 ptr_t base = GC_base(obj);
1114 /* We won't collect it, hence finalizer wouldn't be run. */
1119 if ((ptr_t)obj - base != sizeof(oh)) {
1121 "GC_debug_register_finalizer_unreachable called with "
1122 "non-base-pointer %p\n",
1126 GC_register_finalizer_unreachable(base, 0, 0, &my_old_fn, &my_old_cd);
1128 cd = GC_make_closure(fn, cd);
1129 if (cd == 0) return; /* out of memory */
1130 GC_register_finalizer_unreachable(base, GC_debug_invoke_finalizer,
1131 cd, &my_old_fn, &my_old_cd);
1133 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1136 GC_API void GC_CALL GC_debug_register_finalizer_ignore_self
1137 (void * obj, GC_finalization_proc fn,
1138 void * cd, GC_finalization_proc *ofn,
1141 GC_finalization_proc my_old_fn = OFN_UNSET;
1143 ptr_t base = GC_base(obj);
1145 /* We won't collect it, hence finalizer wouldn't be run. */
1150 if ((ptr_t)obj - base != sizeof(oh)) {
1152 "GC_debug_register_finalizer_ignore_self called with "
1153 "non-base-pointer %p\n", obj);
1156 GC_register_finalizer_ignore_self(base, 0, 0, &my_old_fn, &my_old_cd);
1158 cd = GC_make_closure(fn, cd);
1159 if (cd == 0) return; /* out of memory */
1160 GC_register_finalizer_ignore_self(base, GC_debug_invoke_finalizer,
1161 cd, &my_old_fn, &my_old_cd);
1163 store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1166 GC_API void * GC_CALL GC_debug_malloc_replacement(size_t lb)
1168 return GC_debug_malloc(lb, GC_DBG_RA "unknown", 0);
1171 GC_API void * GC_CALL GC_debug_realloc_replacement(void *p, size_t lb)
1173 return GC_debug_realloc(p, lb, GC_DBG_RA "unknown", 0);