]> granicus.if.org Git - gc/blob - dbg_mlc.c
Refactoring of CMake script to use ANDROID/APPLE/CYGWIN/MSYS variables
[gc] / dbg_mlc.c
1 /*
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
7  *
8  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
10  *
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.
16  */
17
18 #include "private/dbg_mlc.h"
19
20 #ifndef MSWINCE
21 # include <errno.h>
22 #endif
23 #include <string.h>
24
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     */
28   /* of the heap.                                                 */
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)
37   {
38     ptr_t body = (ptr_t)((oh *)p + 1);
39     word sz = GC_size(p);
40
41     if (HBLKPTR(p) != HBLKPTR((ptr_t)body)
42         || sz < DEBUG_BYTES + EXTRA_BYTES) {
43       return 0;
44     }
45     if (((oh *)p) -> oh_sf != (START_FLAG ^ (word)body)
46         && ((word *)p)[BYTES_TO_WORDS(sz)-1] != (END_FLAG ^ (word)body)) {
47       return 0;
48     }
49     if (((oh *)p)->oh_sz == sz) {
50       /* Object may have had debug info, but has been deallocated     */
51       return -1;
52     }
53     return 1;
54   }
55 #endif /* !SHORT_DBG_HDRS */
56
57 #ifdef LINT2
58   long GC_random(void)
59   {
60     static unsigned seed = 1; /* not thread-safe */
61
62     /* Linear congruential pseudo-random numbers generator.     */
63     seed = (seed * 1103515245U + 12345) & GC_RAND_MAX; /* overflow is ok */
64     return (long)seed;
65   }
66 #endif
67
68 #ifdef KEEP_BACK_PTRS
69
70 #ifdef LINT2
71 # define RANDOM() GC_random()
72 #else
73 # include <stdlib.h>
74 # define GC_RAND_MAX RAND_MAX
75
76 # if defined(__GLIBC__) || defined(SOLARIS) \
77      || defined(HPUX) || defined(IRIX5) || defined(OSF1)
78 #   define RANDOM() random()
79 # else
80 #   define RANDOM() (long)rand()
81 # endif
82 #endif /* !LINT2 */
83
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)
91   {
92     if (GC_HAS_DEBUG_INFO(dest)) {
93 #     ifdef PARALLEL_MARK
94         AO_store((volatile AO_t *)&((oh *)dest)->oh_back_ptr,
95                  (AO_t)HIDE_BACK_PTR(source));
96 #     else
97         ((oh *)dest) -> oh_back_ptr = HIDE_BACK_PTR(source);
98 #     endif
99     }
100   }
101
102   GC_INNER void GC_marked_for_finalization(ptr_t dest)
103   {
104     GC_store_back_pointer(MARKED_FOR_FINALIZATION, dest);
105   }
106
107   /* Store information about the object referencing dest in *base_p     */
108   /* and *offset_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,
114                                                   size_t *offset_p)
115   {
116     oh * hdr = (oh *)GC_base(dest);
117     ptr_t bp;
118     ptr_t bp_base;
119
120 #   ifdef LINT2
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");
125 #   endif
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;
131 #   if ALIGNMENT == 1
132       /* Heuristically try to fix off by 1 errors we introduced by      */
133       /* insisting on even addresses.                                   */
134       {
135         ptr_t alternate_ptr = bp + 1;
136         ptr_t target = *(ptr_t *)bp;
137         ptr_t alternate_target = *(ptr_t *)alternate_ptr;
138
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)) {
143             bp = alternate_ptr;
144         }
145       }
146 #   endif
147     bp_base = (ptr_t)GC_base(bp);
148     if (NULL == bp_base) {
149       *base_p = bp;
150       *offset_p = 0;
151       return GC_REFD_FROM_ROOT;
152     } else {
153       if (GC_HAS_DEBUG_INFO(bp_base)) bp_base += sizeof(oh);
154       *base_p = bp_base;
155       *offset_p = bp - bp_base;
156       return GC_REFD_FROM_HEAP;
157     }
158   }
159
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)
164   {
165     size_t i;
166     word heap_offset = RANDOM();
167
168     if (GC_heapsize > GC_RAND_MAX) {
169         heap_offset *= GC_RAND_MAX;
170         heap_offset += RANDOM();
171     }
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.                                            */
176     for (i = 0;; ++i) {
177         size_t size;
178
179         if (i >= GC_n_heap_sects)
180           ABORT("GC_generate_random_heap_address: size inconsistency");
181
182         size = GC_heap_sects[i].hs_bytes;
183         if (heap_offset < size) {
184             break;
185         } else {
186             heap_offset -= size;
187         }
188     }
189     return GC_heap_sects[i].hs_start + heap_offset;
190   }
191
192   /* Generate a random address inside a valid marked heap object. */
193   GC_API void * GC_CALL GC_generate_random_valid_address(void)
194   {
195     ptr_t result;
196     ptr_t base;
197     do {
198       result = (ptr_t)GC_generate_random_heap_address();
199       base = (ptr_t)GC_base(result);
200     } while (NULL == base || !GC_is_marked(base));
201     return result;
202   }
203
204   /* Print back trace for p */
205   GC_API void GC_CALL GC_print_backtrace(void *p)
206   {
207     void *current = p;
208     int i;
209     GC_ref_kind source;
210     size_t offset;
211     void *base;
212
213     GC_print_heap_obj((ptr_t)GC_base(current));
214
215     for (i = 0; ; ++i) {
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");
219         goto out;
220       }
221       if (GC_NO_SPACE == source) {
222         GC_err_printf("No debug info in object: Can't find reference\n");
223         goto out;
224       }
225       GC_err_printf("Reachable via %d levels of pointers from ", i);
226       switch(source) {
227         case GC_REFD_FROM_ROOT:
228           GC_err_printf("root at %p\n\n", base);
229           goto out;
230         case GC_REFD_FROM_REG:
231           GC_err_printf("root in register\n\n");
232           goto out;
233         case GC_FINALIZER_REFD:
234           GC_err_printf("list of finalizable objects\n\n");
235           goto out;
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));
240           break;
241         default:
242           GC_err_printf("INTERNAL ERROR: UNEXPECTED SOURCE!!!!\n");
243           goto out;
244       }
245       current = base;
246     }
247     out:;
248   }
249
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)
253   {
254     void * current;
255     current = GC_generate_random_valid_address();
256     GC_printf("\n****Chosen address %p in object\n", current);
257     GC_print_backtrace(current);
258   }
259
260   GC_API void GC_CALL GC_generate_random_backtrace(void)
261   {
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");
265       return;
266     }
267     GC_generate_random_backtrace_no_gc();
268   }
269
270 #endif /* KEEP_BACK_PTRS */
271
272 # define CROSSES_HBLK(p, sz) \
273         (((word)((p) + sizeof(oh) + (sz) - 1) ^ (word)(p)) >= HBLKSIZE)
274
275 GC_INNER void *GC_store_debug_info_inner(void *p, word sz GC_ATTR_UNUSED,
276                                          const char *string, int linenum)
277 {
278     word * result = (word *)((oh *)p + 1);
279
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);
285 #   endif
286 #   ifdef MAKE_BACK_GRAPH
287       ((oh *)p) -> oh_bg_ptr = HIDE_BACK_PTR((ptr_t)0);
288 #   endif
289     ((oh *)p) -> oh_string = string;
290     ((oh *)p) -> oh_int = 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;
296 #   endif
297     return result;
298 }
299
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)
304 {
305     void *result;
306     DCL_LOCK_STATE;
307
308     if (NULL == p) {
309         GC_err_printf("%s(%lu) returning NULL (%s:%d)\n",
310                       fn, (unsigned long)lb, s, i);
311         return NULL;
312     }
313     LOCK();
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);
318     UNLOCK();
319     return result;
320 }
321
322 #ifndef SHORT_DBG_HDRS
323   /* Check the object with debugging info at ohdr.      */
324   /* Return NULL if it's OK.  Else return clobbered     */
325   /* address.                                           */
326   STATIC ptr_t GC_check_annotated_obj(oh *ohdr)
327   {
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)));
332     }
333     if (ohdr -> oh_sf != (START_FLAG ^ (word)body)) {
334         return((ptr_t)(&(ohdr -> oh_sf)));
335     }
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]);
338     }
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)]);
342     }
343     return(0);
344   }
345 #endif /* !SHORT_DBG_HDRS */
346
347 STATIC GC_describe_type_fn GC_describe_type_fns[MAXOBJKINDS] = {0};
348
349 GC_API void GC_CALL GC_register_describe_type_fn(int kind,
350                                                  GC_describe_type_fn fn)
351 {
352   GC_describe_type_fns[kind] = fn;
353 }
354
355 #define GET_OH_LINENUM(ohdr) ((int)(ohdr)->oh_int)
356
357 #ifndef SHORT_DBG_HDRS
358 # define IF_NOT_SHORTDBG_HDRS(x) x
359 # define COMMA_IFNOT_SHORTDBG_HDRS(x) /* comma */, x
360 #else
361 # define IF_NOT_SHORTDBG_HDRS(x) /* empty */
362 # define COMMA_IFNOT_SHORTDBG_HDRS(x) /* empty */
363 #endif
364
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)
368 {
369     oh * ohdr = (oh *)GC_base(p);
370     ptr_t q;
371     hdr * hhdr;
372     int kind;
373     const char *kind_str;
374     char buffer[GC_TYPE_DESCR_LEN + 1];
375
376     GC_ASSERT(I_DONT_HOLD_LOCK());
377 #   ifdef LINT2
378       if (!ohdr) ABORT("Invalid GC_print_obj argument");
379 #   endif
380
381     q = (ptr_t)(ohdr + 1);
382     /* Print a type description for the object whose client-visible     */
383     /* address is q.                                                    */
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);
392         kind_str = buffer;
393     } else {
394         switch(kind) {
395           case PTRFREE:
396             kind_str = "PTRFREE";
397             break;
398           case NORMAL:
399             kind_str = "NORMAL";
400             break;
401           case UNCOLLECTABLE:
402             kind_str = "UNCOLLECTABLE";
403             break;
404 #         ifdef GC_ATOMIC_UNCOLLECTABLE
405             case AUNCOLLECTABLE:
406               kind_str = "ATOMIC_UNCOLLECTABLE";
407               break;
408 #         endif
409           default:
410             kind_str = NULL;
411                 /* The alternative is to use snprintf(buffer) but it is */
412                 /* not quite portable (see vsnprintf in misc.c).        */
413         }
414     }
415
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),
421                       kind_str);
422     } else {
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);
429     }
430     PRINT_CALL_CHAIN(ohdr);
431 }
432
433 STATIC void GC_debug_print_heap_obj_proc(ptr_t p)
434 {
435     GC_ASSERT(I_DONT_HOLD_LOCK());
436     if (GC_HAS_DEBUG_INFO(p)) {
437         GC_print_obj(p);
438     } else {
439         GC_default_print_heap_obj_proc(p);
440     }
441 }
442
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)
449   {
450     oh * ohdr = (oh *)GC_base(p);
451
452     GC_ASSERT(I_DONT_HOLD_LOCK());
453 #   ifdef LINT2
454       if (!ohdr) ABORT("Invalid GC_print_smashed_obj argument");
455 #   endif
456     if ((word)clobbered_addr <= (word)(&ohdr->oh_sz)
457         || ohdr -> oh_string == 0) {
458         GC_err_printf(
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));
462     } else {
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?)" :
467                                                 ohdr -> oh_string,
468                 GET_OH_LINENUM(ohdr), (unsigned long)(ohdr -> oh_sz));
469         PRINT_CALL_CHAIN(ohdr);
470     }
471   }
472
473   STATIC void GC_check_heap_proc (void);
474   STATIC void GC_print_all_smashed_proc (void);
475 #else
476   STATIC void GC_do_nothing(void) {}
477 #endif
478
479 GC_INNER void GC_start_debugging_inner(void)
480 {
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;
485 # else
486     GC_check_heap = GC_do_nothing;
487     GC_print_all_smashed = GC_do_nothing;
488 # endif
489   GC_print_heap_obj = GC_debug_print_heap_obj_proc;
490   GC_debugging_started = TRUE;
491   GC_register_displacement_inner((word)sizeof(oh));
492 # if defined(CPPCHECK)
493     GC_noop1(GC_debug_header_size);
494 # endif
495 }
496
497 const size_t GC_debug_header_size = sizeof(oh);
498
499 GC_API size_t GC_CALL GC_get_debug_header_size(void) {
500   return sizeof(oh);
501 }
502
503 GC_API void GC_CALL GC_debug_register_displacement(size_t offset)
504 {
505   DCL_LOCK_STATE;
506
507   LOCK();
508   GC_register_displacement_inner(offset);
509   GC_register_displacement_inner((word)sizeof(oh) + offset);
510   UNLOCK();
511 }
512
513 #ifdef GC_ADD_CALLER
514 # if defined(HAVE_DLADDR) && defined(GC_HAVE_RETURN_ADDR_PARENT)
515 #   include <dlfcn.h>
516
517     STATIC void GC_caller_func_offset(word ad, const char **symp, int *offp)
518     {
519       Dl_info caller;
520
521       if (ad && dladdr((void *)ad, &caller) && caller.dli_sname != NULL) {
522         *symp = caller.dli_sname;
523         *offp = (int)((char *)ad - (char *)caller.dli_saddr);
524       }
525       if (NULL == *symp) {
526         *symp = "unknown";
527       }
528     }
529 # else
530 #   define GC_caller_func_offset(ad, symp, offp) (void)(*(symp) = "unknown")
531 # endif
532 #endif /* GC_ADD_CALLER */
533
534 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc(size_t lb,
535                                                      GC_EXTRA_PARAMS)
536 {
537     void * result;
538
539     /* Note that according to malloc() specification, if size is 0 then */
540     /* malloc() returns either NULL, or a unique pointer value that can */
541     /* later be successfully passed to free(). We always do the latter. */
542     result = GC_malloc(SIZET_SAT_ADD(lb, DEBUG_BYTES));
543 #   ifdef GC_ADD_CALLER
544       if (s == NULL) {
545         GC_caller_func_offset(ra, &s, &i);
546       }
547 #   endif
548     return store_debug_info(result, lb, "GC_debug_malloc", OPT_RA s, i);
549 }
550
551 GC_API GC_ATTR_MALLOC void * GC_CALL
552     GC_debug_malloc_ignore_off_page(size_t lb, GC_EXTRA_PARAMS)
553 {
554     void * result = GC_malloc_ignore_off_page(SIZET_SAT_ADD(lb, DEBUG_BYTES));
555
556     return store_debug_info(result, lb, "GC_debug_malloc_ignore_off_page",
557                             OPT_RA s, i);
558 }
559
560 GC_API GC_ATTR_MALLOC void * GC_CALL
561     GC_debug_malloc_atomic_ignore_off_page(size_t lb, GC_EXTRA_PARAMS)
562 {
563     void * result = GC_malloc_atomic_ignore_off_page(
564                                 SIZET_SAT_ADD(lb, DEBUG_BYTES));
565
566     return store_debug_info(result, lb,
567                             "GC_debug_malloc_atomic_ignore_off_page",
568                             OPT_RA s, i);
569 }
570
571 STATIC void * GC_debug_generic_malloc(size_t lb, int knd, GC_EXTRA_PARAMS)
572 {
573     void * result = GC_generic_malloc(SIZET_SAT_ADD(lb, DEBUG_BYTES), knd);
574
575     return store_debug_info(result, lb, "GC_debug_generic_malloc",
576                             OPT_RA s, i);
577 }
578
579 #ifdef DBG_HDRS_ALL
580   /* An allocation function for internal use.  Normally internally      */
581   /* allocated objects do not have debug information.  But in this      */
582   /* case, we need to make sure that all objects have debug headers.    */
583   GC_INNER void * GC_debug_generic_malloc_inner(size_t lb, int k)
584   {
585     void * result;
586
587     GC_ASSERT(I_HOLD_LOCK());
588     result = GC_generic_malloc_inner(SIZET_SAT_ADD(lb, DEBUG_BYTES), k);
589     if (NULL == result) {
590         GC_err_printf("GC internal allocation (%lu bytes) returning NULL\n",
591                        (unsigned long) lb);
592         return(0);
593     }
594     if (!GC_debugging_started) {
595         GC_start_debugging_inner();
596     }
597     ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
598     return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", 0));
599   }
600
601   GC_INNER void * GC_debug_generic_malloc_inner_ignore_off_page(size_t lb,
602                                                                 int k)
603   {
604     void * result;
605
606     GC_ASSERT(I_HOLD_LOCK());
607     result = GC_generic_malloc_inner_ignore_off_page(
608                                 SIZET_SAT_ADD(lb, DEBUG_BYTES), k);
609     if (NULL == result) {
610         GC_err_printf("GC internal allocation (%lu bytes) returning NULL\n",
611                        (unsigned long) lb);
612         return(0);
613     }
614     if (!GC_debugging_started) {
615         GC_start_debugging_inner();
616     }
617     ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
618     return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", 0));
619   }
620 #endif /* DBG_HDRS_ALL */
621
622 #ifndef CPPCHECK
623   GC_API void * GC_CALL GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
624   {
625     return GC_debug_malloc(lb, OPT_RA s, i);
626   }
627
628   GC_API void GC_CALL GC_debug_change_stubborn(
629                                 const void * p GC_ATTR_UNUSED) {}
630 #endif /* !CPPCHECK */
631
632 GC_API void GC_CALL GC_debug_end_stubborn_change(const void *p)
633 {
634     const void * q = GC_base_C(p);
635
636     if (NULL == q) {
637         ABORT_ARG1("GC_debug_end_stubborn_change: bad arg", ": %p", p);
638     }
639     GC_end_stubborn_change(q);
640 }
641
642 GC_API void GC_CALL GC_debug_ptr_store_and_dirty(void *p, const void *q)
643 {
644     *(void **)GC_is_visible(p) = GC_is_valid_displacement((void *)q);
645     GC_debug_end_stubborn_change(p);
646     REACHABLE_AFTER_DIRTY(q);
647 }
648
649 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_atomic(size_t lb,
650                                                             GC_EXTRA_PARAMS)
651 {
652     void * result = GC_malloc_atomic(SIZET_SAT_ADD(lb, DEBUG_BYTES));
653
654     return store_debug_info(result, lb, "GC_debug_malloc_atomic",
655                             OPT_RA s, i);
656 }
657
658 GC_API GC_ATTR_MALLOC char * GC_CALL GC_debug_strdup(const char *str,
659                                                      GC_EXTRA_PARAMS)
660 {
661   char *copy;
662   size_t lb;
663   if (str == NULL) {
664     if (GC_find_leak)
665       GC_err_printf("strdup(NULL) behavior is undefined\n");
666     return NULL;
667   }
668
669   lb = strlen(str) + 1;
670   copy = (char *)GC_debug_malloc_atomic(lb, OPT_RA s, i);
671   if (copy == NULL) {
672 #   ifndef MSWINCE
673       errno = ENOMEM;
674 #   endif
675     return NULL;
676   }
677   BCOPY(str, copy, lb);
678   return copy;
679 }
680
681 GC_API GC_ATTR_MALLOC char * GC_CALL GC_debug_strndup(const char *str,
682                                                 size_t size, GC_EXTRA_PARAMS)
683 {
684   char *copy;
685   size_t len = strlen(str); /* str is expected to be non-NULL  */
686   if (len > size)
687     len = size;
688   copy = (char *)GC_debug_malloc_atomic(len + 1, OPT_RA s, i);
689   if (copy == NULL) {
690 #   ifndef MSWINCE
691       errno = ENOMEM;
692 #   endif
693     return NULL;
694   }
695   if (len > 0)
696     BCOPY(str, copy, len);
697   copy[len] = '\0';
698   return copy;
699 }
700
701 #ifdef GC_REQUIRE_WCSDUP
702 # include <wchar.h> /* for wcslen() */
703
704   GC_API GC_ATTR_MALLOC wchar_t * GC_CALL GC_debug_wcsdup(const wchar_t *str,
705                                                           GC_EXTRA_PARAMS)
706   {
707     size_t lb = (wcslen(str) + 1) * sizeof(wchar_t);
708     wchar_t *copy = (wchar_t *)GC_debug_malloc_atomic(lb, OPT_RA s, i);
709     if (copy == NULL) {
710 #     ifndef MSWINCE
711         errno = ENOMEM;
712 #     endif
713       return NULL;
714     }
715     BCOPY(str, copy, lb);
716     return copy;
717   }
718 #endif /* GC_REQUIRE_WCSDUP */
719
720 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_uncollectable(size_t lb,
721                                                         GC_EXTRA_PARAMS)
722 {
723     void * result = GC_malloc_uncollectable(
724                                 SIZET_SAT_ADD(lb, UNCOLLECTABLE_DEBUG_BYTES));
725
726     return store_debug_info(result, lb, "GC_debug_malloc_uncollectable",
727                             OPT_RA s, i);
728 }
729
730 #ifdef GC_ATOMIC_UNCOLLECTABLE
731   GC_API GC_ATTR_MALLOC void * GC_CALL
732         GC_debug_malloc_atomic_uncollectable(size_t lb, GC_EXTRA_PARAMS)
733   {
734     void * result = GC_malloc_atomic_uncollectable(
735                                 SIZET_SAT_ADD(lb, UNCOLLECTABLE_DEBUG_BYTES));
736
737     return store_debug_info(result, lb,
738                             "GC_debug_malloc_atomic_uncollectable",
739                             OPT_RA s, i);
740   }
741 #endif /* GC_ATOMIC_UNCOLLECTABLE */
742
743 #ifndef GC_FREED_MEM_MARKER
744 # if CPP_WORDSZ == 32
745 #   define GC_FREED_MEM_MARKER 0xdeadbeef
746 # else
747 #   define GC_FREED_MEM_MARKER GC_WORD_C(0xEFBEADDEdeadbeef)
748 # endif
749 #endif
750
751 GC_API void GC_CALL GC_debug_free(void * p)
752 {
753     ptr_t base;
754     if (0 == p) return;
755
756     base = (ptr_t)GC_base(p);
757     if (NULL == base) {
758 #     if defined(REDIRECT_MALLOC) \
759          && ((defined(NEED_CALLINFO) && defined(GC_HAVE_BUILTIN_BACKTRACE)) \
760              || defined(GC_LINUX_THREADS) || defined(GC_SOLARIS_THREADS) \
761              || defined(MSWIN32))
762         /* In some cases, we should ignore objects that do not belong   */
763         /* to the GC heap.  See the comment in GC_free.                 */
764         if (!GC_is_heap_ptr(p)) return;
765 #     endif
766       ABORT_ARG1("Invalid pointer passed to free()", ": %p", p);
767     }
768     if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
769 #     if defined(REDIRECT_FREE) && defined(USE_PROC_FOR_LIBRARIES)
770         /* TODO: Suppress the warning if free() caller is in libpthread */
771         /* or libdl.                                                    */
772 #     endif
773       GC_err_printf(
774                "GC_debug_free called on pointer %p w/o debugging info\n", p);
775     } else {
776 #     ifndef SHORT_DBG_HDRS
777         ptr_t clobbered = GC_check_annotated_obj((oh *)base);
778         word sz = GC_size(base);
779         if (clobbered != 0) {
780           GC_have_errors = TRUE;
781           if (((oh *)base) -> oh_sz == sz) {
782             GC_print_smashed_obj(
783                   "GC_debug_free: found previously deallocated (?) object at",
784                   p, clobbered);
785             return; /* ignore double free */
786           } else {
787             GC_print_smashed_obj("GC_debug_free: found smashed location at",
788                                  p, clobbered);
789           }
790         }
791         /* Invalidate size (mark the object as deallocated) */
792         ((oh *)base) -> oh_sz = sz;
793 #     endif /* SHORT_DBG_HDRS */
794     }
795     if (GC_find_leak
796 #       ifndef SHORT_DBG_HDRS
797           && ((ptr_t)p - (ptr_t)base != sizeof(oh) || !GC_findleak_delay_free)
798 #       endif
799         ) {
800       GC_free(base);
801     } else {
802       hdr * hhdr = HDR(p);
803       if (hhdr -> hb_obj_kind == UNCOLLECTABLE
804 #         ifdef GC_ATOMIC_UNCOLLECTABLE
805             || hhdr -> hb_obj_kind == AUNCOLLECTABLE
806 #         endif
807           ) {
808         GC_free(base);
809       } else {
810         word i;
811         word sz = hhdr -> hb_sz;
812         word obj_sz = BYTES_TO_WORDS(sz - sizeof(oh));
813
814         for (i = 0; i < obj_sz; ++i)
815           ((word *)p)[i] = GC_FREED_MEM_MARKER;
816         GC_ASSERT((word *)p + i == (word *)(base + sz));
817         /* Update the counter even though the real deallocation */
818         /* is deferred.                                         */
819         LOCK();
820         GC_bytes_freed += sz;
821         UNLOCK();
822       }
823     } /* !GC_find_leak */
824 }
825
826 #if defined(THREADS) && defined(DBG_HDRS_ALL)
827   /* Used internally; we assume it's called correctly.    */
828   GC_INNER void GC_debug_free_inner(void * p)
829   {
830     ptr_t base = (ptr_t)GC_base(p);
831     GC_ASSERT((ptr_t)p - (ptr_t)base == sizeof(oh));
832 #   ifdef LINT2
833       if (!base) ABORT("Invalid GC_debug_free_inner argument");
834 #   endif
835 #   ifndef SHORT_DBG_HDRS
836       /* Invalidate size */
837       ((oh *)base) -> oh_sz = GC_size(base);
838 #   endif
839     GC_free_inner(base);
840   }
841 #endif
842
843 GC_API void * GC_CALL GC_debug_realloc(void * p, size_t lb, GC_EXTRA_PARAMS)
844 {
845     void * base;
846     void * result;
847     hdr * hhdr;
848
849     if (p == 0) {
850       return GC_debug_malloc(lb, OPT_RA s, i);
851     }
852     if (0 == lb) /* and p != NULL */ {
853       GC_debug_free(p);
854       return NULL;
855     }
856
857 #   ifdef GC_ADD_CALLER
858       if (s == NULL) {
859         GC_caller_func_offset(ra, &s, &i);
860       }
861 #   endif
862     base = GC_base(p);
863     if (base == 0) {
864         ABORT_ARG1("Invalid pointer passed to realloc()", ": %p", p);
865     }
866     if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
867         GC_err_printf(
868               "GC_debug_realloc called on pointer %p w/o debugging info\n", p);
869         return(GC_realloc(p, lb));
870     }
871     hhdr = HDR(base);
872     switch (hhdr -> hb_obj_kind) {
873       case NORMAL:
874         result = GC_debug_malloc(lb, OPT_RA s, i);
875         break;
876       case PTRFREE:
877         result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
878         break;
879       case UNCOLLECTABLE:
880         result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
881         break;
882 #    ifdef GC_ATOMIC_UNCOLLECTABLE
883       case AUNCOLLECTABLE:
884         result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
885         break;
886 #    endif
887       default:
888         result = NULL; /* initialized to prevent warning. */
889         ABORT_RET("GC_debug_realloc: encountered bad kind");
890     }
891
892     if (result != NULL) {
893       size_t old_sz;
894 #     ifdef SHORT_DBG_HDRS
895         old_sz = GC_size(base) - sizeof(oh);
896 #     else
897         old_sz = ((oh *)base) -> oh_sz;
898 #     endif
899       if (old_sz > 0)
900         BCOPY(p, result, old_sz < lb ? old_sz : lb);
901       GC_debug_free(p);
902     }
903     return(result);
904 }
905
906 GC_API GC_ATTR_MALLOC void * GC_CALL
907     GC_debug_generic_or_special_malloc(size_t lb, int knd, GC_EXTRA_PARAMS)
908 {
909     switch (knd) {
910         case PTRFREE:
911             return GC_debug_malloc_atomic(lb, OPT_RA s, i);
912         case NORMAL:
913             return GC_debug_malloc(lb, OPT_RA s, i);
914         case UNCOLLECTABLE:
915             return GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
916 #     ifdef GC_ATOMIC_UNCOLLECTABLE
917         case AUNCOLLECTABLE:
918             return GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
919 #     endif
920         default:
921             return GC_debug_generic_malloc(lb, knd, OPT_RA s, i);
922     }
923 }
924
925 #ifndef SHORT_DBG_HDRS
926
927 /* List of smashed (clobbered) locations.  We defer printing these,     */
928 /* since we can't always print them nicely with the allocation lock     */
929 /* held.  We put them here instead of in GC_arrays, since it may be     */
930 /* useful to be able to look at them with the debugger.                 */
931 #ifndef MAX_SMASHED
932 # define MAX_SMASHED 20
933 #endif
934 STATIC ptr_t GC_smashed[MAX_SMASHED] = {0};
935 STATIC unsigned GC_n_smashed = 0;
936
937 STATIC void GC_add_smashed(ptr_t smashed)
938 {
939     GC_ASSERT(GC_is_marked(GC_base(smashed)));
940     /* FIXME: Prevent adding an object while printing smashed list.     */
941     GC_smashed[GC_n_smashed] = smashed;
942     if (GC_n_smashed < MAX_SMASHED - 1) ++GC_n_smashed;
943       /* In case of overflow, we keep the first MAX_SMASHED-1   */
944       /* entries plus the last one.                             */
945     GC_have_errors = TRUE;
946 }
947
948 /* Print all objects on the list.  Clear the list.      */
949 STATIC void GC_print_all_smashed_proc(void)
950 {
951     unsigned i;
952
953     GC_ASSERT(I_DONT_HOLD_LOCK());
954     if (GC_n_smashed == 0) return;
955     GC_err_printf("GC_check_heap_block: found %u smashed heap objects:\n",
956                   GC_n_smashed);
957     for (i = 0; i < GC_n_smashed; ++i) {
958         ptr_t base = (ptr_t)GC_base(GC_smashed[i]);
959
960 #       ifdef LINT2
961           if (!base) ABORT("Invalid GC_smashed element");
962 #       endif
963         GC_print_smashed_obj("", base + sizeof(oh), GC_smashed[i]);
964         GC_smashed[i] = 0;
965     }
966     GC_n_smashed = 0;
967 }
968
969 /* Check all marked objects in the given block for validity     */
970 /* Avoid GC_apply_to_each_object for performance reasons.       */
971 STATIC void GC_check_heap_block(struct hblk *hbp, word dummy GC_ATTR_UNUSED)
972 {
973     struct hblkhdr * hhdr = HDR(hbp);
974     word sz = hhdr -> hb_sz;
975     word bit_no;
976     char *p, *plim;
977
978     p = hbp->hb_body;
979     if (sz > MAXOBJBYTES) {
980       plim = p;
981     } else {
982       plim = hbp->hb_body + HBLKSIZE - sz;
983     }
984     /* go through all words in block */
985     for (bit_no = 0; (word)p <= (word)plim;
986          bit_no += MARK_BIT_OFFSET(sz), p += sz) {
987       if (mark_bit_from_hdr(hhdr, bit_no) && GC_HAS_DEBUG_INFO((ptr_t)p)) {
988         ptr_t clobbered = GC_check_annotated_obj((oh *)p);
989         if (clobbered != 0)
990           GC_add_smashed(clobbered);
991       }
992     }
993 }
994
995 /* This assumes that all accessible objects are marked, and that        */
996 /* I hold the allocation lock.  Normally called by collector.           */
997 STATIC void GC_check_heap_proc(void)
998 {
999   GC_STATIC_ASSERT((sizeof(oh) & (GRANULE_BYTES - 1)) == 0);
1000   /* FIXME: Should we check for twice that alignment?   */
1001   GC_apply_to_all_blocks(GC_check_heap_block, 0);
1002 }
1003
1004 GC_INNER GC_bool GC_check_leaked(ptr_t base)
1005 {
1006   word i;
1007   word obj_sz;
1008   word *p;
1009
1010   if (
1011 #     if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
1012         (*(word *)base & 1) != 0 &&
1013 #     endif
1014       GC_has_other_debug_info(base) >= 0)
1015     return TRUE; /* object has leaked */
1016
1017   /* Validate freed object's content. */
1018   p = (word *)(base + sizeof(oh));
1019   obj_sz = BYTES_TO_WORDS(HDR(base)->hb_sz - sizeof(oh));
1020   for (i = 0; i < obj_sz; ++i)
1021     if (p[i] != GC_FREED_MEM_MARKER) {
1022         GC_set_mark_bit(base); /* do not reclaim it in this cycle */
1023         GC_add_smashed((ptr_t)(&p[i])); /* alter-after-free detected */
1024         break; /* don't report any other smashed locations in the object */
1025     }
1026
1027   return FALSE; /* GC_debug_free() has been called */
1028 }
1029
1030 #endif /* !SHORT_DBG_HDRS */
1031
1032 #ifndef GC_NO_FINALIZATION
1033
1034 struct closure {
1035     GC_finalization_proc cl_fn;
1036     void * cl_data;
1037 };
1038
1039 STATIC void * GC_make_closure(GC_finalization_proc fn, void * data)
1040 {
1041     struct closure * result =
1042 #   ifdef DBG_HDRS_ALL
1043       (struct closure *) GC_debug_malloc(sizeof (struct closure),
1044                                          GC_EXTRAS);
1045 #   else
1046       (struct closure *) GC_malloc(sizeof (struct closure));
1047 #   endif
1048     if (result != 0) {
1049       result -> cl_fn = fn;
1050       result -> cl_data = data;
1051     }
1052     return((void *)result);
1053 }
1054
1055 /* An auxiliary fns to make finalization work correctly with displaced  */
1056 /* pointers introduced by the debugging allocators.                     */
1057 STATIC void GC_CALLBACK GC_debug_invoke_finalizer(void * obj, void * data)
1058 {
1059     struct closure * cl = (struct closure *) data;
1060     (*(cl -> cl_fn))((void *)((char *)obj + sizeof(oh)), cl -> cl_data);
1061 }
1062
1063 /* Special finalizer_proc value to detect GC_register_finalizer() failure. */
1064 #define OFN_UNSET ((GC_finalization_proc)~(signed_word)0)
1065
1066 /* Set ofn and ocd to reflect the values we got back.   */
1067 static void store_old(void *obj, GC_finalization_proc my_old_fn,
1068                       struct closure *my_old_cd, GC_finalization_proc *ofn,
1069                       void **ocd)
1070 {
1071     if (0 != my_old_fn) {
1072       if (my_old_fn == OFN_UNSET) {
1073         /* register_finalizer() failed; (*ofn) and (*ocd) are unchanged. */
1074         return;
1075       }
1076       if (my_old_fn != GC_debug_invoke_finalizer) {
1077         GC_err_printf("Debuggable object at %p had a non-debug finalizer\n",
1078                       obj);
1079         /* This should probably be fatal. */
1080       } else {
1081         if (ofn) *ofn = my_old_cd -> cl_fn;
1082         if (ocd) *ocd = my_old_cd -> cl_data;
1083       }
1084     } else {
1085       if (ofn) *ofn = 0;
1086       if (ocd) *ocd = 0;
1087     }
1088 }
1089
1090 GC_API void GC_CALL GC_debug_register_finalizer(void * obj,
1091                                         GC_finalization_proc fn,
1092                                         void * cd, GC_finalization_proc *ofn,
1093                                         void * *ocd)
1094 {
1095     GC_finalization_proc my_old_fn = OFN_UNSET;
1096     void * my_old_cd;
1097     ptr_t base = (ptr_t)GC_base(obj);
1098     if (NULL == base) {
1099         /* We won't collect it, hence finalizer wouldn't be run. */
1100         if (ocd) *ocd = 0;
1101         if (ofn) *ofn = 0;
1102         return;
1103     }
1104     if ((ptr_t)obj - base != sizeof(oh)) {
1105         GC_err_printf("GC_debug_register_finalizer called with"
1106                       " non-base-pointer %p\n", obj);
1107     }
1108     if (0 == fn) {
1109       GC_register_finalizer(base, 0, 0, &my_old_fn, &my_old_cd);
1110     } else {
1111       cd = GC_make_closure(fn, cd);
1112       if (cd == 0) return; /* out of memory */
1113       GC_register_finalizer(base, GC_debug_invoke_finalizer,
1114                             cd, &my_old_fn, &my_old_cd);
1115     }
1116     store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1117 }
1118
1119 GC_API void GC_CALL GC_debug_register_finalizer_no_order
1120                                     (void * obj, GC_finalization_proc fn,
1121                                      void * cd, GC_finalization_proc *ofn,
1122                                      void * *ocd)
1123 {
1124     GC_finalization_proc my_old_fn = OFN_UNSET;
1125     void * my_old_cd;
1126     ptr_t base = (ptr_t)GC_base(obj);
1127     if (NULL == base) {
1128         /* We won't collect it, hence finalizer wouldn't be run. */
1129         if (ocd) *ocd = 0;
1130         if (ofn) *ofn = 0;
1131         return;
1132     }
1133     if ((ptr_t)obj - base != sizeof(oh)) {
1134         GC_err_printf("GC_debug_register_finalizer_no_order called with"
1135                       " non-base-pointer %p\n", obj);
1136     }
1137     if (0 == fn) {
1138       GC_register_finalizer_no_order(base, 0, 0, &my_old_fn, &my_old_cd);
1139     } else {
1140       cd = GC_make_closure(fn, cd);
1141       if (cd == 0) return; /* out of memory */
1142       GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
1143                                      cd, &my_old_fn, &my_old_cd);
1144     }
1145     store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1146 }
1147
1148 GC_API void GC_CALL GC_debug_register_finalizer_unreachable
1149                                     (void * obj, GC_finalization_proc fn,
1150                                      void * cd, GC_finalization_proc *ofn,
1151                                      void * *ocd)
1152 {
1153     GC_finalization_proc my_old_fn = OFN_UNSET;
1154     void * my_old_cd;
1155     ptr_t base = (ptr_t)GC_base(obj);
1156     if (NULL == base) {
1157         /* We won't collect it, hence finalizer wouldn't be run. */
1158         if (ocd) *ocd = 0;
1159         if (ofn) *ofn = 0;
1160         return;
1161     }
1162     if ((ptr_t)obj - base != sizeof(oh)) {
1163         GC_err_printf("GC_debug_register_finalizer_unreachable called with"
1164                       " non-base-pointer %p\n", obj);
1165     }
1166     if (0 == fn) {
1167       GC_register_finalizer_unreachable(base, 0, 0, &my_old_fn, &my_old_cd);
1168     } else {
1169       cd = GC_make_closure(fn, cd);
1170       if (cd == 0) return; /* out of memory */
1171       GC_register_finalizer_unreachable(base, GC_debug_invoke_finalizer,
1172                                         cd, &my_old_fn, &my_old_cd);
1173     }
1174     store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1175 }
1176
1177 GC_API void GC_CALL GC_debug_register_finalizer_ignore_self
1178                                     (void * obj, GC_finalization_proc fn,
1179                                      void * cd, GC_finalization_proc *ofn,
1180                                      void * *ocd)
1181 {
1182     GC_finalization_proc my_old_fn = OFN_UNSET;
1183     void * my_old_cd;
1184     ptr_t base = (ptr_t)GC_base(obj);
1185     if (NULL == base) {
1186         /* We won't collect it, hence finalizer wouldn't be run. */
1187         if (ocd) *ocd = 0;
1188         if (ofn) *ofn = 0;
1189         return;
1190     }
1191     if ((ptr_t)obj - base != sizeof(oh)) {
1192         GC_err_printf("GC_debug_register_finalizer_ignore_self called with"
1193                       " non-base-pointer %p\n", obj);
1194     }
1195     if (0 == fn) {
1196       GC_register_finalizer_ignore_self(base, 0, 0, &my_old_fn, &my_old_cd);
1197     } else {
1198       cd = GC_make_closure(fn, cd);
1199       if (cd == 0) return; /* out of memory */
1200       GC_register_finalizer_ignore_self(base, GC_debug_invoke_finalizer,
1201                                         cd, &my_old_fn, &my_old_cd);
1202     }
1203     store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1204 }
1205
1206 #endif /* !GC_NO_FINALIZATION */
1207
1208 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_replacement(size_t lb)
1209 {
1210     return GC_debug_malloc(lb, GC_DBG_EXTRAS);
1211 }
1212
1213 GC_API void * GC_CALL GC_debug_realloc_replacement(void *p, size_t lb)
1214 {
1215     return GC_debug_realloc(p, lb, GC_DBG_EXTRAS);
1216 }