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