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