| 1 |
/* |
| 2 |
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved. |
| 3 |
* |
| 4 |
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 5 |
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 6 |
* |
| 7 |
* Permission is hereby granted to use or copy this program |
| 8 |
* for any purpose, provided the above notices are retained on all copies. |
| 9 |
* Permission to modify the code and to distribute modified code is granted, |
| 10 |
* provided the above notices are retained, and a notice that the code was |
| 11 |
* modified is included with the above copyright notice. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "private/gc_pmark.h" |
| 15 |
|
| 16 |
/* |
| 17 |
* These are checking routines calls to which could be inserted by a |
| 18 |
* preprocessor to validate C pointer arithmetic. |
| 19 |
*/ |
| 20 |
|
| 21 |
STATIC void GC_CALLBACK GC_default_same_obj_print_proc(void * p, void * q) |
| 22 |
{ |
| 23 |
ABORT_ARG2("GC_same_obj test failed", |
| 24 |
": %p and %p are not in the same object", p, q); |
| 25 |
} |
| 26 |
|
| 27 |
void (GC_CALLBACK *GC_same_obj_print_proc) (void *, void *) |
| 28 |
= GC_default_same_obj_print_proc; |
| 29 |
|
| 30 |
/* Check that p and q point to the same object. Call */ |
| 31 |
/* *GC_same_obj_print_proc if they don't. */ |
| 32 |
/* Returns the first argument. (Return value may be hard */ |
| 33 |
/* to use due to typing issues. But if we had a suitable */ |
| 34 |
/* preprocessor...) */ |
| 35 |
/* Succeeds if neither p nor q points to the heap. */ |
| 36 |
/* We assume this is performance critical. (It shouldn't */ |
| 37 |
/* be called by production code, but this can easily make */ |
| 38 |
/* debugging intolerably slow.) */ |
| 39 |
GC_API void * GC_CALL GC_same_obj(void *p, void *q) |
| 40 |
{ |
| 41 |
struct hblk *h; |
| 42 |
hdr *hhdr; |
| 43 |
ptr_t base, limit; |
| 44 |
word sz; |
| 45 |
|
| 46 |
if (!EXPECT(GC_is_initialized, TRUE)) GC_init(); |
| 47 |
hhdr = HDR((word)p); |
| 48 |
if (hhdr == 0) { |
| 49 |
if (divHBLKSZ((word)p) != divHBLKSZ((word)q) |
| 50 |
&& HDR((word)q) != 0) { |
| 51 |
goto fail; |
| 52 |
} |
| 53 |
return(p); |
| 54 |
} |
| 55 |
/* If it's a pointer to the middle of a large object, move it */ |
| 56 |
/* to the beginning. */ |
| 57 |
if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) { |
| 58 |
h = HBLKPTR(p) - (word)hhdr; |
| 59 |
hhdr = HDR(h); |
| 60 |
while (IS_FORWARDING_ADDR_OR_NIL(hhdr)) { |
| 61 |
h = FORWARDED_ADDR(h, hhdr); |
| 62 |
hhdr = HDR(h); |
| 63 |
} |
| 64 |
limit = (ptr_t)h + hhdr -> hb_sz; |
| 65 |
if ((word)p >= (word)limit || (word)q >= (word)limit |
| 66 |
|| (word)q < (word)h) { |
| 67 |
goto fail; |
| 68 |
} |
| 69 |
return(p); |
| 70 |
} |
| 71 |
sz = hhdr -> hb_sz; |
| 72 |
if (sz > MAXOBJBYTES) { |
| 73 |
base = (ptr_t)HBLKPTR(p); |
| 74 |
limit = base + sz; |
| 75 |
if ((word)p >= (word)limit) { |
| 76 |
goto fail; |
| 77 |
} |
| 78 |
} else { |
| 79 |
size_t offset; |
| 80 |
size_t pdispl = HBLKDISPL(p); |
| 81 |
|
| 82 |
offset = pdispl % sz; |
| 83 |
if (HBLKPTR(p) != HBLKPTR(q)) goto fail; |
| 84 |
/* W/o this check, we might miss an error if */ |
| 85 |
/* q points to the first object on a page, and */ |
| 86 |
/* points just before the page. */ |
| 87 |
base = (ptr_t)p - offset; |
| 88 |
limit = base + sz; |
| 89 |
} |
| 90 |
/* [base, limit) delimits the object containing p, if any. */ |
| 91 |
/* If p is not inside a valid object, then either q is */ |
| 92 |
/* also outside any valid object, or it is outside */ |
| 93 |
/* [base, limit). */ |
| 94 |
if ((word)q >= (word)limit || (word)q < (word)base) { |
| 95 |
goto fail; |
| 96 |
} |
| 97 |
return(p); |
| 98 |
fail: |
| 99 |
(*GC_same_obj_print_proc)((ptr_t)p, (ptr_t)q); |
| 100 |
return(p); |
| 101 |
} |
| 102 |
|
| 103 |
STATIC void GC_CALLBACK GC_default_is_valid_displacement_print_proc (void *p) |
| 104 |
{ |
| 105 |
ABORT_ARG1("GC_is_valid_displacement test failed", ": %p not valid", p); |
| 106 |
} |
| 107 |
|
| 108 |
void (GC_CALLBACK *GC_is_valid_displacement_print_proc)(void *) = |
| 109 |
GC_default_is_valid_displacement_print_proc; |
| 110 |
|
| 111 |
/* Check that if p is a pointer to a heap page, then it points to */ |
| 112 |
/* a valid displacement within a heap object. */ |
| 113 |
/* Uninteresting with GC_all_interior_pointers. */ |
| 114 |
/* Always returns its argument. */ |
| 115 |
/* Note that we don't lock, since nothing relevant about the header */ |
| 116 |
/* should change while we have a valid object pointer to the block. */ |
| 117 |
GC_API void * GC_CALL GC_is_valid_displacement(void *p) |
| 118 |
{ |
| 119 |
hdr *hhdr; |
| 120 |
word pdispl; |
| 121 |
word offset; |
| 122 |
struct hblk *h; |
| 123 |
word sz; |
| 124 |
|
| 125 |
if (!EXPECT(GC_is_initialized, TRUE)) GC_init(); |
| 126 |
if (NULL == p) return NULL; |
| 127 |
hhdr = HDR((word)p); |
| 128 |
if (hhdr == 0) return(p); |
| 129 |
h = HBLKPTR(p); |
| 130 |
if (GC_all_interior_pointers) { |
| 131 |
while (IS_FORWARDING_ADDR_OR_NIL(hhdr)) { |
| 132 |
h = FORWARDED_ADDR(h, hhdr); |
| 133 |
hhdr = HDR(h); |
| 134 |
} |
| 135 |
} else if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) { |
| 136 |
goto fail; |
| 137 |
} |
| 138 |
sz = hhdr -> hb_sz; |
| 139 |
pdispl = HBLKDISPL(p); |
| 140 |
offset = pdispl % sz; |
| 141 |
if ((sz > MAXOBJBYTES && (word)p >= (word)h + sz) |
| 142 |
|| !GC_valid_offsets[offset] |
| 143 |
|| ((word)p + (sz - offset) > (word)(h + 1) |
| 144 |
&& !IS_FORWARDING_ADDR_OR_NIL(HDR(h + 1)))) { |
| 145 |
goto fail; |
| 146 |
} |
| 147 |
return(p); |
| 148 |
fail: |
| 149 |
(*GC_is_valid_displacement_print_proc)((ptr_t)p); |
| 150 |
return(p); |
| 151 |
} |
| 152 |
|
| 153 |
STATIC void GC_CALLBACK GC_default_is_visible_print_proc(void * p) |
| 154 |
{ |
| 155 |
ABORT_ARG1("GC_is_visible test failed", ": %p not GC-visible", p); |
| 156 |
} |
| 157 |
|
| 158 |
void (GC_CALLBACK *GC_is_visible_print_proc)(void * p) = |
| 159 |
GC_default_is_visible_print_proc; |
| 160 |
|
| 161 |
#ifndef THREADS |
| 162 |
/* Could p be a stack address? */ |
| 163 |
STATIC GC_bool GC_on_stack(void *p) |
| 164 |
{ |
| 165 |
# ifdef STACK_GROWS_DOWN |
| 166 |
if ((word)p >= (word)GC_approx_sp() |
| 167 |
&& (word)p < (word)GC_stackbottom) { |
| 168 |
return(TRUE); |
| 169 |
} |
| 170 |
# else |
| 171 |
if ((word)p <= (word)GC_approx_sp() |
| 172 |
&& (word)p > (word)GC_stackbottom) { |
| 173 |
return(TRUE); |
| 174 |
} |
| 175 |
# endif |
| 176 |
return(FALSE); |
| 177 |
} |
| 178 |
#endif |
| 179 |
|
| 180 |
/* Check that p is visible */ |
| 181 |
/* to the collector as a possibly pointer containing location. */ |
| 182 |
/* If it isn't, invoke *GC_is_visible_print_proc. */ |
| 183 |
/* Returns the argument in all cases. May erroneously succeed */ |
| 184 |
/* in hard cases. (This is intended for debugging use with */ |
| 185 |
/* untyped allocations. The idea is that it should be possible, though */ |
| 186 |
/* slow, to add such a call to all indirect pointer stores.) */ |
| 187 |
/* Currently useless for the multi-threaded worlds. */ |
| 188 |
GC_API void * GC_CALL GC_is_visible(void *p) |
| 189 |
{ |
| 190 |
hdr *hhdr; |
| 191 |
|
| 192 |
if ((word)p & (ALIGNMENT - 1)) goto fail; |
| 193 |
if (!EXPECT(GC_is_initialized, TRUE)) GC_init(); |
| 194 |
# ifdef THREADS |
| 195 |
hhdr = HDR((word)p); |
| 196 |
if (hhdr != 0 && GC_base(p) == 0) { |
| 197 |
goto fail; |
| 198 |
} else { |
| 199 |
/* May be inside thread stack. We can't do much. */ |
| 200 |
return(p); |
| 201 |
} |
| 202 |
# else |
| 203 |
/* Check stack first: */ |
| 204 |
if (GC_on_stack(p)) return(p); |
| 205 |
hhdr = HDR((word)p); |
| 206 |
if (hhdr == 0) { |
| 207 |
if (GC_is_static_root(p)) return(p); |
| 208 |
/* Else do it again correctly: */ |
| 209 |
# if defined(DYNAMIC_LOADING) || defined(MSWIN32) \ |
| 210 |
|| defined(MSWINCE) || defined(CYGWIN32) || defined(PCR) |
| 211 |
if (!GC_no_dls) { |
| 212 |
GC_register_dynamic_libraries(); |
| 213 |
if (GC_is_static_root(p)) return p; |
| 214 |
} |
| 215 |
# endif |
| 216 |
goto fail; |
| 217 |
} else { |
| 218 |
/* p points to the heap. */ |
| 219 |
word descr; |
| 220 |
ptr_t base = (ptr_t)GC_base(p); |
| 221 |
/* TODO: should GC_base be manually inlined? */ |
| 222 |
|
| 223 |
if (NULL == base) goto fail; |
| 224 |
if (HBLKPTR(base) != HBLKPTR(p)) |
| 225 |
hhdr = HDR(base); |
| 226 |
descr = hhdr -> hb_descr; |
| 227 |
retry: |
| 228 |
switch(descr & GC_DS_TAGS) { |
| 229 |
case GC_DS_LENGTH: |
| 230 |
if ((word)p - (word)base >= descr) goto fail; |
| 231 |
break; |
| 232 |
case GC_DS_BITMAP: |
| 233 |
if ((word)p - (word)base >= WORDS_TO_BYTES(BITMAP_BITS) |
| 234 |
|| ((word)p & (sizeof(word) - 1))) goto fail; |
| 235 |
if (!(((word)1 << (WORDSZ - ((ptr_t)p - (ptr_t)base) - 1)) |
| 236 |
& descr)) goto fail; |
| 237 |
break; |
| 238 |
case GC_DS_PROC: |
| 239 |
/* We could try to decipher this partially. */ |
| 240 |
/* For now we just punt. */ |
| 241 |
break; |
| 242 |
case GC_DS_PER_OBJECT: |
| 243 |
if ((signed_word)descr >= 0) { |
| 244 |
descr = *(word *)((ptr_t)base |
| 245 |
+ (descr & ~(word)GC_DS_TAGS)); |
| 246 |
} else { |
| 247 |
ptr_t type_descr = *(ptr_t *)base; |
| 248 |
|
| 249 |
if (EXPECT(NULL == type_descr, FALSE)) |
| 250 |
goto fail; /* see comment in GC_mark_from */ |
| 251 |
descr = *(word *)(type_descr |
| 252 |
- (descr - (word)(GC_DS_PER_OBJECT |
| 253 |
- GC_INDIR_PER_OBJ_BIAS))); |
| 254 |
} |
| 255 |
goto retry; |
| 256 |
} |
| 257 |
return(p); |
| 258 |
} |
| 259 |
# endif |
| 260 |
fail: |
| 261 |
(*GC_is_visible_print_proc)((ptr_t)p); |
| 262 |
return(p); |
| 263 |
} |
| 264 |
|
| 265 |
GC_API void * GC_CALL GC_pre_incr (void **p, ptrdiff_t how_much) |
| 266 |
{ |
| 267 |
void * initial = *p; |
| 268 |
void * result = GC_same_obj((void *)((ptr_t)initial + how_much), initial); |
| 269 |
|
| 270 |
if (!GC_all_interior_pointers) { |
| 271 |
(void) GC_is_valid_displacement(result); |
| 272 |
} |
| 273 |
return (*p = result); |
| 274 |
} |
| 275 |
|
| 276 |
GC_API void * GC_CALL GC_post_incr (void **p, ptrdiff_t how_much) |
| 277 |
{ |
| 278 |
void * initial = *p; |
| 279 |
void * result = GC_same_obj((void *)((ptr_t)initial + how_much), initial); |
| 280 |
|
| 281 |
if (!GC_all_interior_pointers) { |
| 282 |
(void) GC_is_valid_displacement(result); |
| 283 |
} |
| 284 |
*p = result; |
| 285 |
return(initial); |
| 286 |
} |