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.                                       */
GC_has_other_debug_info(ptr_t p)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
GC_random(void)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.                            */
GC_store_back_pointer(ptr_t source,ptr_t dest)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 
GC_marked_for_finalization(ptr_t dest)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.                      */
GC_get_back_ptr_info(void * dest,void ** base_p,size_t * offset_p)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.     */
GC_generate_random_heap_address(void)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. */
GC_generate_random_valid_address(void)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 */
GC_print_backtrace(void * 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.                                */
GC_generate_random_backtrace_no_gc(void)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 
GC_generate_random_backtrace(void)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 
GC_store_debug_info_inner(void * p,word sz GC_ATTR_UNUSED,const char * string,int linenum)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. */
store_debug_info(void * p,size_t lb,const char * fn,GC_EXTRA_PARAMS)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.                                           */
GC_check_annotated_obj(oh * ohdr)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 
GC_register_describe_type_fn(int kind,GC_describe_type_fn fn)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.      */
GC_print_obj(ptr_t p)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 
GC_debug_print_heap_obj_proc(ptr_t p)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.                                                    */
GC_print_smashed_obj(const char * msg,void * p,ptr_t 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
GC_do_nothing(void)476   STATIC void GC_do_nothing(void) {}
477 #endif
478 
GC_start_debugging_inner(void)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 }
493 
494 size_t GC_debug_header_size = sizeof(oh);
495 
GC_debug_register_displacement(size_t offset)496 GC_API void GC_CALL GC_debug_register_displacement(size_t offset)
497 {
498   DCL_LOCK_STATE;
499 
500   LOCK();
501   GC_register_displacement_inner(offset);
502   GC_register_displacement_inner((word)sizeof(oh) + offset);
503   UNLOCK();
504 }
505 
506 #ifdef GC_ADD_CALLER
507 # if defined(HAVE_DLADDR) && defined(GC_HAVE_RETURN_ADDR_PARENT)
508 #   include <dlfcn.h>
509 
GC_caller_func_offset(word ad,const char ** symp,int * offp)510     STATIC void GC_caller_func_offset(word ad, const char **symp, int *offp)
511     {
512       Dl_info caller;
513 
514       if (ad && dladdr((void *)ad, &caller) && caller.dli_sname != NULL) {
515         *symp = caller.dli_sname;
516         *offp = (int)((char *)ad - (char *)caller.dli_saddr);
517       }
518       if (NULL == *symp) {
519         *symp = "unknown";
520       }
521     }
522 # else
523 #   define GC_caller_func_offset(ad, symp, offp) (void)(*(symp) = "unknown")
524 # endif
525 #endif /* GC_ADD_CALLER */
526 
GC_debug_malloc(size_t lb,GC_EXTRA_PARAMS)527 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc(size_t lb,
528                                                      GC_EXTRA_PARAMS)
529 {
530     void * result;
531 
532     /* Note that according to malloc() specification, if size is 0 then */
533     /* malloc() returns either NULL, or a unique pointer value that can */
534     /* later be successfully passed to free(). We always do the latter. */
535     result = GC_malloc(SIZET_SAT_ADD(lb, DEBUG_BYTES));
536 #   ifdef GC_ADD_CALLER
537       if (s == NULL) {
538         GC_caller_func_offset(ra, &s, &i);
539       }
540 #   endif
541     return store_debug_info(result, lb, "GC_debug_malloc", OPT_RA s, i);
542 }
543 
544 GC_API GC_ATTR_MALLOC void * GC_CALL
GC_debug_malloc_ignore_off_page(size_t lb,GC_EXTRA_PARAMS)545     GC_debug_malloc_ignore_off_page(size_t lb, GC_EXTRA_PARAMS)
546 {
547     void * result = GC_malloc_ignore_off_page(SIZET_SAT_ADD(lb, DEBUG_BYTES));
548 
549     return store_debug_info(result, lb, "GC_debug_malloc_ignore_off_page",
550                             OPT_RA s, i);
551 }
552 
553 GC_API GC_ATTR_MALLOC void * GC_CALL
GC_debug_malloc_atomic_ignore_off_page(size_t lb,GC_EXTRA_PARAMS)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     return store_debug_info(result, lb,
560                             "GC_debug_malloc_atomic_ignore_off_page",
561                             OPT_RA s, i);
562 }
563 
GC_debug_generic_malloc(size_t lb,int knd,GC_EXTRA_PARAMS)564 STATIC void * GC_debug_generic_malloc(size_t lb, int knd, GC_EXTRA_PARAMS)
565 {
566     void * result = GC_generic_malloc(SIZET_SAT_ADD(lb, DEBUG_BYTES), knd);
567 
568     return store_debug_info(result, lb, "GC_debug_generic_malloc",
569                             OPT_RA s, i);
570 }
571 
572 #ifdef DBG_HDRS_ALL
573   /* An allocation function for internal use.  Normally internally      */
574   /* allocated objects do not have debug information.  But in this      */
575   /* case, we need to make sure that all objects have debug headers.    */
GC_debug_generic_malloc_inner(size_t lb,int k)576   GC_INNER void * GC_debug_generic_malloc_inner(size_t lb, int k)
577   {
578     void * result;
579 
580     GC_ASSERT(I_HOLD_LOCK());
581     result = GC_generic_malloc_inner(SIZET_SAT_ADD(lb, DEBUG_BYTES), k);
582     if (NULL == result) {
583         GC_err_printf("GC internal allocation (%lu bytes) returning NULL\n",
584                        (unsigned long) lb);
585         return(0);
586     }
587     if (!GC_debugging_started) {
588         GC_start_debugging_inner();
589     }
590     ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
591     return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", 0));
592   }
593 
GC_debug_generic_malloc_inner_ignore_off_page(size_t lb,int k)594   GC_INNER void * GC_debug_generic_malloc_inner_ignore_off_page(size_t lb,
595                                                                 int k)
596   {
597     void * result;
598 
599     GC_ASSERT(I_HOLD_LOCK());
600     result = GC_generic_malloc_inner_ignore_off_page(
601                                 SIZET_SAT_ADD(lb, DEBUG_BYTES), k);
602     if (NULL == result) {
603         GC_err_printf("GC internal allocation (%lu bytes) returning NULL\n",
604                        (unsigned long) lb);
605         return(0);
606     }
607     if (!GC_debugging_started) {
608         GC_start_debugging_inner();
609     }
610     ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
611     return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", 0));
612   }
613 #endif /* DBG_HDRS_ALL */
614 
GC_debug_malloc_stubborn(size_t lb,GC_EXTRA_PARAMS)615 GC_API void * GC_CALL GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
616 {
617     return GC_debug_malloc(lb, OPT_RA s, i);
618 }
619 
GC_debug_change_stubborn(const void * p GC_ATTR_UNUSED)620 GC_API void GC_CALL GC_debug_change_stubborn(
621                                 const void * p GC_ATTR_UNUSED) {}
622 
GC_debug_end_stubborn_change(const void * p)623 GC_API void GC_CALL GC_debug_end_stubborn_change(const void *p)
624 {
625     const void * q = GC_base_C(p);
626 
627     if (NULL == q) {
628         ABORT_ARG1("GC_debug_end_stubborn_change: bad arg", ": %p", p);
629     }
630     GC_end_stubborn_change(q);
631 }
632 
GC_debug_ptr_store_and_dirty(void * p,const void * q)633 GC_API void GC_CALL GC_debug_ptr_store_and_dirty(void *p, const void *q)
634 {
635     *(void **)GC_is_visible(p) = GC_is_valid_displacement((void *)q);
636     GC_debug_end_stubborn_change(p);
637     REACHABLE_AFTER_DIRTY(q);
638 }
639 
GC_debug_malloc_atomic(size_t lb,GC_EXTRA_PARAMS)640 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_atomic(size_t lb,
641                                                             GC_EXTRA_PARAMS)
642 {
643     void * result = GC_malloc_atomic(SIZET_SAT_ADD(lb, DEBUG_BYTES));
644 
645     return store_debug_info(result, lb, "GC_debug_malloc_atomic",
646                             OPT_RA s, i);
647 }
648 
GC_debug_strdup(const char * str,GC_EXTRA_PARAMS)649 GC_API GC_ATTR_MALLOC char * GC_CALL GC_debug_strdup(const char *str,
650                                                      GC_EXTRA_PARAMS)
651 {
652   char *copy;
653   size_t lb;
654   if (str == NULL) {
655     if (GC_find_leak)
656       GC_err_printf("strdup(NULL) behavior is undefined\n");
657     return NULL;
658   }
659 
660   lb = strlen(str) + 1;
661   copy = (char *)GC_debug_malloc_atomic(lb, OPT_RA s, i);
662   if (copy == NULL) {
663 #   ifndef MSWINCE
664       errno = ENOMEM;
665 #   endif
666     return NULL;
667   }
668   BCOPY(str, copy, lb);
669   return copy;
670 }
671 
GC_debug_strndup(const char * str,size_t size,GC_EXTRA_PARAMS)672 GC_API GC_ATTR_MALLOC char * GC_CALL GC_debug_strndup(const char *str,
673                                                 size_t size, GC_EXTRA_PARAMS)
674 {
675   char *copy;
676   size_t len = strlen(str); /* str is expected to be non-NULL  */
677   if (len > size)
678     len = size;
679   copy = (char *)GC_debug_malloc_atomic(len + 1, OPT_RA s, i);
680   if (copy == NULL) {
681 #   ifndef MSWINCE
682       errno = ENOMEM;
683 #   endif
684     return NULL;
685   }
686   if (len > 0)
687     BCOPY(str, copy, len);
688   copy[len] = '\0';
689   return copy;
690 }
691 
692 #ifdef GC_REQUIRE_WCSDUP
693 # include <wchar.h> /* for wcslen() */
694 
GC_debug_wcsdup(const wchar_t * str,GC_EXTRA_PARAMS)695   GC_API GC_ATTR_MALLOC wchar_t * GC_CALL GC_debug_wcsdup(const wchar_t *str,
696                                                           GC_EXTRA_PARAMS)
697   {
698     size_t lb = (wcslen(str) + 1) * sizeof(wchar_t);
699     wchar_t *copy = (wchar_t *)GC_debug_malloc_atomic(lb, OPT_RA s, i);
700     if (copy == NULL) {
701 #     ifndef MSWINCE
702         errno = ENOMEM;
703 #     endif
704       return NULL;
705     }
706     BCOPY(str, copy, lb);
707     return copy;
708   }
709 #endif /* GC_REQUIRE_WCSDUP */
710 
GC_debug_malloc_uncollectable(size_t lb,GC_EXTRA_PARAMS)711 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_uncollectable(size_t lb,
712                                                         GC_EXTRA_PARAMS)
713 {
714     void * result = GC_malloc_uncollectable(
715                                 SIZET_SAT_ADD(lb, UNCOLLECTABLE_DEBUG_BYTES));
716 
717     return store_debug_info(result, lb, "GC_debug_malloc_uncollectable",
718                             OPT_RA s, i);
719 }
720 
721 #ifdef GC_ATOMIC_UNCOLLECTABLE
722   GC_API GC_ATTR_MALLOC void * GC_CALL
GC_debug_malloc_atomic_uncollectable(size_t lb,GC_EXTRA_PARAMS)723         GC_debug_malloc_atomic_uncollectable(size_t lb, GC_EXTRA_PARAMS)
724   {
725     void * result = GC_malloc_atomic_uncollectable(
726                                 SIZET_SAT_ADD(lb, UNCOLLECTABLE_DEBUG_BYTES));
727 
728     return store_debug_info(result, lb,
729                             "GC_debug_malloc_atomic_uncollectable",
730                             OPT_RA s, i);
731   }
732 #endif /* GC_ATOMIC_UNCOLLECTABLE */
733 
734 #ifndef GC_FREED_MEM_MARKER
735 # if CPP_WORDSZ == 32
736 #   define GC_FREED_MEM_MARKER 0xdeadbeef
737 # else
738 #   define GC_FREED_MEM_MARKER GC_WORD_C(0xEFBEADDEdeadbeef)
739 # endif
740 #endif
741 
GC_debug_free(void * p)742 GC_API void GC_CALL GC_debug_free(void * p)
743 {
744     ptr_t base;
745     if (0 == p) return;
746 
747     base = (ptr_t)GC_base(p);
748     if (NULL == base) {
749 #     if defined(REDIRECT_MALLOC) \
750          && ((defined(NEED_CALLINFO) && defined(GC_HAVE_BUILTIN_BACKTRACE)) \
751              || defined(GC_LINUX_THREADS) || defined(GC_SOLARIS_THREADS) \
752              || defined(MSWIN32))
753         /* In some cases, we should ignore objects that do not belong   */
754         /* to the GC heap.  See the comment in GC_free.                 */
755         if (!GC_is_heap_ptr(p)) return;
756 #     endif
757       ABORT_ARG1("Invalid pointer passed to free()", ": %p", p);
758     }
759     if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
760 #     if defined(REDIRECT_FREE) && defined(USE_PROC_FOR_LIBRARIES)
761         /* TODO: Suppress the warning if free() caller is in libpthread */
762         /* or libdl.                                                    */
763 #     endif
764       GC_err_printf(
765                "GC_debug_free called on pointer %p w/o debugging info\n", p);
766     } else {
767 #     ifndef SHORT_DBG_HDRS
768         ptr_t clobbered = GC_check_annotated_obj((oh *)base);
769         word sz = GC_size(base);
770         if (clobbered != 0) {
771           GC_have_errors = TRUE;
772           if (((oh *)base) -> oh_sz == sz) {
773             GC_print_smashed_obj(
774                   "GC_debug_free: found previously deallocated (?) object at",
775                   p, clobbered);
776             return; /* ignore double free */
777           } else {
778             GC_print_smashed_obj("GC_debug_free: found smashed location at",
779                                  p, clobbered);
780           }
781         }
782         /* Invalidate size (mark the object as deallocated) */
783         ((oh *)base) -> oh_sz = sz;
784 #     endif /* SHORT_DBG_HDRS */
785     }
786     if (GC_find_leak
787 #       ifndef SHORT_DBG_HDRS
788           && ((ptr_t)p - (ptr_t)base != sizeof(oh) || !GC_findleak_delay_free)
789 #       endif
790         ) {
791       GC_free(base);
792     } else {
793       hdr * hhdr = HDR(p);
794       if (hhdr -> hb_obj_kind == UNCOLLECTABLE
795 #         ifdef GC_ATOMIC_UNCOLLECTABLE
796             || hhdr -> hb_obj_kind == AUNCOLLECTABLE
797 #         endif
798           ) {
799         GC_free(base);
800       } else {
801         word i;
802         word sz = hhdr -> hb_sz;
803         word obj_sz = BYTES_TO_WORDS(sz - sizeof(oh));
804 
805         for (i = 0; i < obj_sz; ++i)
806           ((word *)p)[i] = GC_FREED_MEM_MARKER;
807         GC_ASSERT((word *)p + i == (word *)(base + sz));
808         /* Update the counter even though the real deallocation */
809         /* is deferred.                                         */
810         LOCK();
811         GC_bytes_freed += sz;
812         UNLOCK();
813       }
814     } /* !GC_find_leak */
815 }
816 
817 #if defined(THREADS) && defined(DBG_HDRS_ALL)
818   /* Used internally; we assume it's called correctly.    */
GC_debug_free_inner(void * p)819   GC_INNER void GC_debug_free_inner(void * p)
820   {
821     ptr_t base = (ptr_t)GC_base(p);
822     GC_ASSERT((ptr_t)p - (ptr_t)base == sizeof(oh));
823 #   ifdef LINT2
824       if (!base) ABORT("Invalid GC_debug_free_inner argument");
825 #   endif
826 #   ifndef SHORT_DBG_HDRS
827       /* Invalidate size */
828       ((oh *)base) -> oh_sz = GC_size(base);
829 #   endif
830     GC_free_inner(base);
831   }
832 #endif
833 
GC_debug_realloc(void * p,size_t lb,GC_EXTRA_PARAMS)834 GC_API void * GC_CALL GC_debug_realloc(void * p, size_t lb, GC_EXTRA_PARAMS)
835 {
836     void * base;
837     void * result;
838     hdr * hhdr;
839 
840     if (p == 0) {
841       return GC_debug_malloc(lb, OPT_RA s, i);
842     }
843     if (0 == lb) /* and p != NULL */ {
844       GC_debug_free(p);
845       return NULL;
846     }
847 
848 #   ifdef GC_ADD_CALLER
849       if (s == NULL) {
850         GC_caller_func_offset(ra, &s, &i);
851       }
852 #   endif
853     base = GC_base(p);
854     if (base == 0) {
855         ABORT_ARG1("Invalid pointer passed to realloc()", ": %p", p);
856     }
857     if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
858         GC_err_printf(
859               "GC_debug_realloc called on pointer %p w/o debugging info\n", p);
860         return(GC_realloc(p, lb));
861     }
862     hhdr = HDR(base);
863     switch (hhdr -> hb_obj_kind) {
864       case NORMAL:
865         result = GC_debug_malloc(lb, OPT_RA s, i);
866         break;
867       case PTRFREE:
868         result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
869         break;
870       case UNCOLLECTABLE:
871         result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
872         break;
873 #    ifdef GC_ATOMIC_UNCOLLECTABLE
874       case AUNCOLLECTABLE:
875         result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
876         break;
877 #    endif
878       default:
879         result = NULL; /* initialized to prevent warning. */
880         ABORT_RET("GC_debug_realloc: encountered bad kind");
881     }
882 
883     if (result != NULL) {
884       size_t old_sz;
885 #     ifdef SHORT_DBG_HDRS
886         old_sz = GC_size(base) - sizeof(oh);
887 #     else
888         old_sz = ((oh *)base) -> oh_sz;
889 #     endif
890       if (old_sz > 0)
891         BCOPY(p, result, old_sz < lb ? old_sz : lb);
892       GC_debug_free(p);
893     }
894     return(result);
895 }
896 
897 GC_API GC_ATTR_MALLOC void * GC_CALL
GC_debug_generic_or_special_malloc(size_t lb,int knd,GC_EXTRA_PARAMS)898     GC_debug_generic_or_special_malloc(size_t lb, int knd, GC_EXTRA_PARAMS)
899 {
900     switch (knd) {
901         case PTRFREE:
902             return GC_debug_malloc_atomic(lb, OPT_RA s, i);
903         case NORMAL:
904             return GC_debug_malloc(lb, OPT_RA s, i);
905         case UNCOLLECTABLE:
906             return GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
907 #     ifdef GC_ATOMIC_UNCOLLECTABLE
908         case AUNCOLLECTABLE:
909             return GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
910 #     endif
911         default:
912             return GC_debug_generic_malloc(lb, knd, OPT_RA s, i);
913     }
914 }
915 
916 #ifndef SHORT_DBG_HDRS
917 
918 /* List of smashed (clobbered) locations.  We defer printing these,     */
919 /* since we can't always print them nicely with the allocation lock     */
920 /* held.  We put them here instead of in GC_arrays, since it may be     */
921 /* useful to be able to look at them with the debugger.                 */
922 #ifndef MAX_SMASHED
923 # define MAX_SMASHED 20
924 #endif
925 STATIC ptr_t GC_smashed[MAX_SMASHED] = {0};
926 STATIC unsigned GC_n_smashed = 0;
927 
GC_add_smashed(ptr_t smashed)928 STATIC void GC_add_smashed(ptr_t smashed)
929 {
930     GC_ASSERT(GC_is_marked(GC_base(smashed)));
931     /* FIXME: Prevent adding an object while printing smashed list.     */
932     GC_smashed[GC_n_smashed] = smashed;
933     if (GC_n_smashed < MAX_SMASHED - 1) ++GC_n_smashed;
934       /* In case of overflow, we keep the first MAX_SMASHED-1   */
935       /* entries plus the last one.                             */
936     GC_have_errors = TRUE;
937 }
938 
939 /* Print all objects on the list.  Clear the list.      */
GC_print_all_smashed_proc(void)940 STATIC void GC_print_all_smashed_proc(void)
941 {
942     unsigned i;
943 
944     GC_ASSERT(I_DONT_HOLD_LOCK());
945     if (GC_n_smashed == 0) return;
946     GC_err_printf("GC_check_heap_block: found %u smashed heap objects:\n",
947                   GC_n_smashed);
948     for (i = 0; i < GC_n_smashed; ++i) {
949         ptr_t base = (ptr_t)GC_base(GC_smashed[i]);
950 
951 #       ifdef LINT2
952           if (!base) ABORT("Invalid GC_smashed element");
953 #       endif
954         GC_print_smashed_obj("", base + sizeof(oh), GC_smashed[i]);
955         GC_smashed[i] = 0;
956     }
957     GC_n_smashed = 0;
958 }
959 
960 /* Check all marked objects in the given block for validity     */
961 /* Avoid GC_apply_to_each_object for performance reasons.       */
GC_check_heap_block(struct hblk * hbp,word dummy GC_ATTR_UNUSED)962 STATIC void GC_check_heap_block(struct hblk *hbp, word dummy GC_ATTR_UNUSED)
963 {
964     struct hblkhdr * hhdr = HDR(hbp);
965     word sz = hhdr -> hb_sz;
966     word bit_no;
967     char *p, *plim;
968 
969     p = hbp->hb_body;
970     if (sz > MAXOBJBYTES) {
971       plim = p;
972     } else {
973       plim = hbp->hb_body + HBLKSIZE - sz;
974     }
975     /* go through all words in block */
976     for (bit_no = 0; (word)p <= (word)plim;
977          bit_no += MARK_BIT_OFFSET(sz), p += sz) {
978       if (mark_bit_from_hdr(hhdr, bit_no) && GC_HAS_DEBUG_INFO((ptr_t)p)) {
979         ptr_t clobbered = GC_check_annotated_obj((oh *)p);
980         if (clobbered != 0)
981           GC_add_smashed(clobbered);
982       }
983     }
984 }
985 
986 /* This assumes that all accessible objects are marked, and that        */
987 /* I hold the allocation lock.  Normally called by collector.           */
GC_check_heap_proc(void)988 STATIC void GC_check_heap_proc(void)
989 {
990   GC_STATIC_ASSERT((sizeof(oh) & (GRANULE_BYTES - 1)) == 0);
991   /* FIXME: Should we check for twice that alignment?   */
992   GC_apply_to_all_blocks(GC_check_heap_block, 0);
993 }
994 
GC_check_leaked(ptr_t base)995 GC_INNER GC_bool GC_check_leaked(ptr_t base)
996 {
997   word i;
998   word obj_sz;
999   word *p;
1000 
1001   if (
1002 #     if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
1003         (*(word *)base & 1) != 0 &&
1004 #     endif
1005       GC_has_other_debug_info(base) >= 0)
1006     return TRUE; /* object has leaked */
1007 
1008   /* Validate freed object's content. */
1009   p = (word *)(base + sizeof(oh));
1010   obj_sz = BYTES_TO_WORDS(HDR(base)->hb_sz - sizeof(oh));
1011   for (i = 0; i < obj_sz; ++i)
1012     if (p[i] != GC_FREED_MEM_MARKER) {
1013         GC_set_mark_bit(base); /* do not reclaim it in this cycle */
1014         GC_add_smashed((ptr_t)(&p[i])); /* alter-after-free detected */
1015         break; /* don't report any other smashed locations in the object */
1016     }
1017 
1018   return FALSE; /* GC_debug_free() has been called */
1019 }
1020 
1021 #endif /* !SHORT_DBG_HDRS */
1022 
1023 #ifndef GC_NO_FINALIZATION
1024 
1025 struct closure {
1026     GC_finalization_proc cl_fn;
1027     void * cl_data;
1028 };
1029 
GC_make_closure(GC_finalization_proc fn,void * data)1030 STATIC void * GC_make_closure(GC_finalization_proc fn, void * data)
1031 {
1032     struct closure * result =
1033 #   ifdef DBG_HDRS_ALL
1034       (struct closure *) GC_debug_malloc(sizeof (struct closure),
1035                                          GC_EXTRAS);
1036 #   else
1037       (struct closure *) GC_malloc(sizeof (struct closure));
1038 #   endif
1039     if (result != 0) {
1040       result -> cl_fn = fn;
1041       result -> cl_data = data;
1042     }
1043     return((void *)result);
1044 }
1045 
1046 /* An auxiliary fns to make finalization work correctly with displaced  */
1047 /* pointers introduced by the debugging allocators.                     */
GC_debug_invoke_finalizer(void * obj,void * data)1048 STATIC void GC_CALLBACK GC_debug_invoke_finalizer(void * obj, void * data)
1049 {
1050     struct closure * cl = (struct closure *) data;
1051     (*(cl -> cl_fn))((void *)((char *)obj + sizeof(oh)), cl -> cl_data);
1052 }
1053 
1054 /* Special finalizer_proc value to detect GC_register_finalizer() failure. */
1055 #define OFN_UNSET ((GC_finalization_proc)~(signed_word)0)
1056 
1057 /* Set ofn and ocd to reflect the values we got back.   */
store_old(void * obj,GC_finalization_proc my_old_fn,struct closure * my_old_cd,GC_finalization_proc * ofn,void ** ocd)1058 static void store_old(void *obj, GC_finalization_proc my_old_fn,
1059                       struct closure *my_old_cd, GC_finalization_proc *ofn,
1060                       void **ocd)
1061 {
1062     if (0 != my_old_fn) {
1063       if (my_old_fn == OFN_UNSET) {
1064         /* register_finalizer() failed; (*ofn) and (*ocd) are unchanged. */
1065         return;
1066       }
1067       if (my_old_fn != GC_debug_invoke_finalizer) {
1068         GC_err_printf("Debuggable object at %p had a non-debug finalizer\n",
1069                       obj);
1070         /* This should probably be fatal. */
1071       } else {
1072         if (ofn) *ofn = my_old_cd -> cl_fn;
1073         if (ocd) *ocd = my_old_cd -> cl_data;
1074       }
1075     } else {
1076       if (ofn) *ofn = 0;
1077       if (ocd) *ocd = 0;
1078     }
1079 }
1080 
GC_debug_register_finalizer(void * obj,GC_finalization_proc fn,void * cd,GC_finalization_proc * ofn,void ** ocd)1081 GC_API void GC_CALL GC_debug_register_finalizer(void * obj,
1082                                         GC_finalization_proc fn,
1083                                         void * cd, GC_finalization_proc *ofn,
1084                                         void * *ocd)
1085 {
1086     GC_finalization_proc my_old_fn = OFN_UNSET;
1087     void * my_old_cd;
1088     ptr_t base = (ptr_t)GC_base(obj);
1089     if (NULL == base) {
1090         /* We won't collect it, hence finalizer wouldn't be run. */
1091         if (ocd) *ocd = 0;
1092         if (ofn) *ofn = 0;
1093         return;
1094     }
1095     if ((ptr_t)obj - base != sizeof(oh)) {
1096         GC_err_printf("GC_debug_register_finalizer called with"
1097                       " non-base-pointer %p\n", obj);
1098     }
1099     if (0 == fn) {
1100       GC_register_finalizer(base, 0, 0, &my_old_fn, &my_old_cd);
1101     } else {
1102       cd = GC_make_closure(fn, cd);
1103       if (cd == 0) return; /* out of memory */
1104       GC_register_finalizer(base, GC_debug_invoke_finalizer,
1105                             cd, &my_old_fn, &my_old_cd);
1106     }
1107     store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1108 }
1109 
GC_debug_register_finalizer_no_order(void * obj,GC_finalization_proc fn,void * cd,GC_finalization_proc * ofn,void ** ocd)1110 GC_API void GC_CALL GC_debug_register_finalizer_no_order
1111                                     (void * obj, GC_finalization_proc fn,
1112                                      void * cd, GC_finalization_proc *ofn,
1113                                      void * *ocd)
1114 {
1115     GC_finalization_proc my_old_fn = OFN_UNSET;
1116     void * my_old_cd;
1117     ptr_t base = (ptr_t)GC_base(obj);
1118     if (NULL == base) {
1119         /* We won't collect it, hence finalizer wouldn't be run. */
1120         if (ocd) *ocd = 0;
1121         if (ofn) *ofn = 0;
1122         return;
1123     }
1124     if ((ptr_t)obj - base != sizeof(oh)) {
1125         GC_err_printf("GC_debug_register_finalizer_no_order called with"
1126                       " non-base-pointer %p\n", obj);
1127     }
1128     if (0 == fn) {
1129       GC_register_finalizer_no_order(base, 0, 0, &my_old_fn, &my_old_cd);
1130     } else {
1131       cd = GC_make_closure(fn, cd);
1132       if (cd == 0) return; /* out of memory */
1133       GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
1134                                      cd, &my_old_fn, &my_old_cd);
1135     }
1136     store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1137 }
1138 
GC_debug_register_finalizer_unreachable(void * obj,GC_finalization_proc fn,void * cd,GC_finalization_proc * ofn,void ** ocd)1139 GC_API void GC_CALL GC_debug_register_finalizer_unreachable
1140                                     (void * obj, GC_finalization_proc fn,
1141                                      void * cd, GC_finalization_proc *ofn,
1142                                      void * *ocd)
1143 {
1144     GC_finalization_proc my_old_fn = OFN_UNSET;
1145     void * my_old_cd;
1146     ptr_t base = (ptr_t)GC_base(obj);
1147     if (NULL == base) {
1148         /* We won't collect it, hence finalizer wouldn't be run. */
1149         if (ocd) *ocd = 0;
1150         if (ofn) *ofn = 0;
1151         return;
1152     }
1153     if ((ptr_t)obj - base != sizeof(oh)) {
1154         GC_err_printf("GC_debug_register_finalizer_unreachable called with"
1155                       " non-base-pointer %p\n", obj);
1156     }
1157     if (0 == fn) {
1158       GC_register_finalizer_unreachable(base, 0, 0, &my_old_fn, &my_old_cd);
1159     } else {
1160       cd = GC_make_closure(fn, cd);
1161       if (cd == 0) return; /* out of memory */
1162       GC_register_finalizer_unreachable(base, GC_debug_invoke_finalizer,
1163                                         cd, &my_old_fn, &my_old_cd);
1164     }
1165     store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1166 }
1167 
GC_debug_register_finalizer_ignore_self(void * obj,GC_finalization_proc fn,void * cd,GC_finalization_proc * ofn,void ** ocd)1168 GC_API void GC_CALL GC_debug_register_finalizer_ignore_self
1169                                     (void * obj, GC_finalization_proc fn,
1170                                      void * cd, GC_finalization_proc *ofn,
1171                                      void * *ocd)
1172 {
1173     GC_finalization_proc my_old_fn = OFN_UNSET;
1174     void * my_old_cd;
1175     ptr_t base = (ptr_t)GC_base(obj);
1176     if (NULL == base) {
1177         /* We won't collect it, hence finalizer wouldn't be run. */
1178         if (ocd) *ocd = 0;
1179         if (ofn) *ofn = 0;
1180         return;
1181     }
1182     if ((ptr_t)obj - base != sizeof(oh)) {
1183         GC_err_printf("GC_debug_register_finalizer_ignore_self called with"
1184                       " non-base-pointer %p\n", obj);
1185     }
1186     if (0 == fn) {
1187       GC_register_finalizer_ignore_self(base, 0, 0, &my_old_fn, &my_old_cd);
1188     } else {
1189       cd = GC_make_closure(fn, cd);
1190       if (cd == 0) return; /* out of memory */
1191       GC_register_finalizer_ignore_self(base, GC_debug_invoke_finalizer,
1192                                         cd, &my_old_fn, &my_old_cd);
1193     }
1194     store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1195 }
1196 
1197 #endif /* !GC_NO_FINALIZATION */
1198 
GC_debug_malloc_replacement(size_t lb)1199 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_replacement(size_t lb)
1200 {
1201     return GC_debug_malloc(lb, GC_DBG_EXTRAS);
1202 }
1203 
GC_debug_realloc_replacement(void * p,size_t lb)1204 GC_API void * GC_CALL GC_debug_realloc_replacement(void *p, size_t lb)
1205 {
1206     return GC_debug_realloc(p, lb, GC_DBG_EXTRAS);
1207 }
1208