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