2 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
15 * These are checking routines calls to which could be inserted by a
16 * preprocessor to validate C pointer arithmetic.
19 #include "private/gc_pmark.h"
21 void GC_default_same_obj_print_proc(void * p, void * q)
23 GC_err_printf("%p and %p are not in the same object\n", p, q);
24 ABORT("GC_same_obj test failed");
27 void (*GC_same_obj_print_proc) (void *, void *)
28 = GC_default_same_obj_print_proc;
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 void * GC_same_obj(void *p, void *q)
46 if (!GC_is_initialized) GC_init();
49 if (divHBLKSZ((word)p) != divHBLKSZ((word)q)
50 && HDR((word)q) != 0) {
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;
60 while (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
61 h = FORWARDED_ADDR(h, hhdr);
64 limit = (ptr_t)h + hhdr -> hb_sz;
65 if ((ptr_t)p >= limit || (ptr_t)q >= limit || (ptr_t)q < (ptr_t)h ) {
71 if (sz > MAXOBJBYTES) {
72 base = (ptr_t)HBLKPTR(p);
74 if ((ptr_t)p >= limit) {
79 size_t pdispl = HBLKDISPL(p);
82 if (HBLKPTR(p) != HBLKPTR(q)) goto fail;
83 /* W/o this check, we might miss an error if */
84 /* q points to the first object on a page, and */
85 /* points just before the page. */
86 base = (ptr_t)p - offset;
89 /* [base, limit) delimits the object containing p, if any. */
90 /* If p is not inside a valid object, then either q is */
91 /* also outside any valid object, or it is outside */
93 if ((ptr_t)q >= limit || (ptr_t)q < base) {
98 (*GC_same_obj_print_proc)((ptr_t)p, (ptr_t)q);
102 void GC_default_is_valid_displacement_print_proc (void *p)
104 GC_err_printf("%p does not point to valid object displacement\n", p);
105 ABORT("GC_is_valid_displacement test failed");
108 void (*GC_is_valid_displacement_print_proc)(void *) =
109 GC_default_is_valid_displacement_print_proc;
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 void * GC_is_valid_displacement(void *p)
125 if (!GC_is_initialized) GC_init();
127 if (hhdr == 0) return(p);
129 if (GC_all_interior_pointers) {
130 while (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
131 h = FORWARDED_ADDR(h, hhdr);
135 if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
139 pdispl = HBLKDISPL(p);
140 offset = pdispl % sz;
141 if ((sz > MAXOBJBYTES && (ptr_t)p >= (ptr_t)h + sz)
142 || !GC_valid_offsets[offset]
143 || (ptr_t)p - offset + sz > (ptr_t)(h + 1)) {
148 (*GC_is_valid_displacement_print_proc)((ptr_t)p);
152 void GC_default_is_visible_print_proc(void * p)
154 GC_err_printf("%p is not a GC visible pointer location\n", p);
155 ABORT("GC_is_visible test failed");
158 void (*GC_is_visible_print_proc)(void * p) = GC_default_is_visible_print_proc;
160 /* Could p be a stack address? */
161 GC_bool GC_on_stack(ptr_t p)
167 # ifdef STACK_GROWS_DOWN
168 if ((ptr_t)p >= (ptr_t)(&dummy) && (ptr_t)p < GC_stackbottom ) {
172 if ((ptr_t)p <= (ptr_t)(&dummy) && (ptr_t)p > GC_stackbottom ) {
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 multithreaded worlds. */
188 void * GC_is_visible(void *p)
192 if ((word)p & (ALIGNMENT - 1)) goto fail;
193 if (!GC_is_initialized) GC_init();
196 if (hhdr != 0 && GC_base(p) == 0) {
199 /* May be inside thread stack. We can't do much. */
203 /* Check stack first: */
204 if (GC_on_stack(p)) return(p);
209 if (GC_is_static_root(p)) return(p);
210 /* Else do it again correctly: */
211 # if (defined(DYNAMIC_LOADING) || defined(MSWIN32) || \
212 defined(MSWINCE) || defined(PCR))
213 GC_register_dynamic_libraries();
214 result = GC_is_static_root(p);
215 if (result) return(p);
219 /* p points to the heap. */
221 ptr_t base = GC_base(p); /* Should be manually inlined? */
223 if (base == 0) goto fail;
224 if (HBLKPTR(base) != HBLKPTR(p)) hhdr = HDR((word)p);
225 descr = hhdr -> hb_descr;
227 switch(descr & GC_DS_TAGS) {
229 if ((word)((ptr_t)p - (ptr_t)base) > (word)descr) goto fail;
232 if ((ptr_t)p - (ptr_t)base
233 >= WORDS_TO_BYTES(BITMAP_BITS)
234 || ((word)p & (sizeof(word) - 1))) goto fail;
235 if (!((1 << (WORDSZ - ((ptr_t)p - (ptr_t)base) - 1))
239 /* We could try to decipher this partially. */
240 /* For now we just punt. */
242 case GC_DS_PER_OBJECT:
243 if ((signed_word)descr >= 0) {
244 descr = *(word *)((ptr_t)base + (descr & ~GC_DS_TAGS));
246 ptr_t type_descr = *(ptr_t *)base;
247 descr = *(word *)(type_descr
248 - (descr - (GC_DS_PER_OBJECT
249 - GC_INDIR_PER_OBJ_BIAS)));
257 (*GC_is_visible_print_proc)((ptr_t)p);
262 void * GC_pre_incr (void **p, size_t how_much)
265 void * result = GC_same_obj((void *)((word)initial + how_much), initial);
267 if (!GC_all_interior_pointers) {
268 (void) GC_is_valid_displacement(result);
270 return (*p = result);
273 void * GC_post_incr (void **p, size_t how_much)
276 void * result = GC_same_obj((void *)((word)initial + how_much), initial);
278 if (!GC_all_interior_pointers) {
279 (void) GC_is_valid_displacement(result);