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.
21 #include "private/gc_pmark.h"
24 void GC_default_same_obj_print_proc(GC_PTR p, GC_PTR q)
26 void GC_default_same_obj_print_proc (p, q)
30 GC_err_printf2("0x%lx and 0x%lx are not in the same object\n",
31 (unsigned long)p, (unsigned long)q);
32 ABORT("GC_same_obj test failed");
35 void (*GC_same_obj_print_proc) GC_PROTO((GC_PTR, GC_PTR))
36 = GC_default_same_obj_print_proc;
38 /* Check that p and q point to the same object. Call */
39 /* *GC_same_obj_print_proc if they don't. */
40 /* Returns the first argument. (Return value may be hard */
41 /* to use,due to typing issues. But if we had a suitable */
42 /* preprocessor ...) */
43 /* Succeeds if neither p nor q points to the heap. */
44 /* We assume this is performance critical. (It shouldn't */
45 /* be called by production code, but this can easily make */
46 /* debugging intolerably slow.) */
48 GC_PTR GC_same_obj(register void *p, register void *q)
50 GC_PTR GC_same_obj(p, q)
54 register struct hblk *h;
56 register ptr_t base, limit;
59 if (!GC_is_initialized) GC_init();
62 if (divHBLKSZ((word)p) != divHBLKSZ((word)q)
63 && HDR((word)q) != 0) {
68 /* If it's a pointer to the middle of a large object, move it */
69 /* to the beginning. */
70 if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
71 h = HBLKPTR(p) - (word)hhdr;
73 while (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
74 h = FORWARDED_ADDR(h, hhdr);
77 limit = (ptr_t)((word *)h + hhdr -> hb_sz);
78 if ((ptr_t)p >= limit || (ptr_t)q >= limit || (ptr_t)q < (ptr_t)h ) {
83 sz = WORDS_TO_BYTES(hhdr -> hb_sz);
84 if (sz > MAXOBJBYTES) {
85 base = (ptr_t)HBLKPTR(p);
87 if ((ptr_t)p >= limit) {
91 register int map_entry;
92 register int pdispl = HBLKDISPL(p);
94 map_entry = MAP_ENTRY((hhdr -> hb_map), pdispl);
95 if (map_entry > CPP_MAX_OFFSET) {
96 map_entry = BYTES_TO_WORDS(pdispl) % BYTES_TO_WORDS(sz);
97 if (HBLKPTR(p) != HBLKPTR(q)) goto fail;
98 /* W/o this check, we might miss an error if */
99 /* q points to the first object on a page, and */
100 /* points just before the page. */
102 base = (char *)((word)p & ~(WORDS_TO_BYTES(1) - 1));
103 base -= WORDS_TO_BYTES(map_entry);
106 /* [base, limit) delimits the object containing p, if any. */
107 /* If p is not inside a valid object, then either q is */
108 /* also outside any valid object, or it is outside */
110 if ((ptr_t)q >= limit || (ptr_t)q < base) {
115 (*GC_same_obj_print_proc)((ptr_t)p, (ptr_t)q);
120 void GC_default_is_valid_displacement_print_proc (GC_PTR p)
122 void GC_default_is_valid_displacement_print_proc (p)
126 GC_err_printf1("0x%lx does not point to valid object displacement\n",
128 ABORT("GC_is_valid_displacement test failed");
131 void (*GC_is_valid_displacement_print_proc) GC_PROTO((GC_PTR)) =
132 GC_default_is_valid_displacement_print_proc;
134 /* Check that if p is a pointer to a heap page, then it points to */
135 /* a valid displacement within a heap object. */
136 /* Uninteresting with GC_all_interior_pointers. */
137 /* Always returns its argument. */
138 /* Note that we don't lock, since nothing relevant about the header */
139 /* should change while we have a valid object pointer to the block. */
141 void * GC_is_valid_displacement(void *p)
143 char *GC_is_valid_displacement(p)
148 register word pdispl;
149 register struct hblk *h;
150 register map_entry_type map_entry;
153 if (!GC_is_initialized) GC_init();
155 if (hhdr == 0) return(p);
157 if (GC_all_interior_pointers) {
158 while (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
159 h = FORWARDED_ADDR(h, hhdr);
163 if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
166 sz = WORDS_TO_BYTES(hhdr -> hb_sz);
167 pdispl = HBLKDISPL(p);
168 map_entry = MAP_ENTRY((hhdr -> hb_map), pdispl);
169 if (map_entry == OBJ_INVALID
170 || sz > MAXOBJBYTES && (ptr_t)p >= (ptr_t)h + sz) {
175 (*GC_is_valid_displacement_print_proc)((ptr_t)p);
180 void GC_default_is_visible_print_proc(GC_PTR p)
182 void GC_default_is_visible_print_proc(p)
186 GC_err_printf1("0x%lx is not a GC visible pointer location\n",
188 ABORT("GC_is_visible test failed");
191 void (*GC_is_visible_print_proc) GC_PROTO((GC_PTR p)) =
192 GC_default_is_visible_print_proc;
194 /* Could p be a stack address? */
195 GC_bool GC_on_stack(p)
202 # ifdef STACK_GROWS_DOWN
203 if ((ptr_t)p >= (ptr_t)(&dummy) && (ptr_t)p < GC_stackbottom ) {
207 if ((ptr_t)p <= (ptr_t)(&dummy) && (ptr_t)p > GC_stackbottom ) {
215 /* Check that p is visible */
216 /* to the collector as a possibly pointer containing location. */
217 /* If it isn't invoke *GC_is_visible_print_proc. */
218 /* Returns the argument in all cases. May erroneously succeed */
219 /* in hard cases. (This is intended for debugging use with */
220 /* untyped allocations. The idea is that it should be possible, though */
221 /* slow, to add such a call to all indirect pointer stores.) */
222 /* Currently useless for multithreaded worlds. */
224 void * GC_is_visible(void *p)
226 char *GC_is_visible(p)
232 if ((word)p & (ALIGNMENT - 1)) goto fail;
233 if (!GC_is_initialized) GC_init();
236 if (hhdr != 0 && GC_base(p) == 0) {
239 /* May be inside thread stack. We can't do much. */
243 /* Check stack first: */
244 if (GC_on_stack(p)) return(p);
249 if (GC_is_static_root(p)) return(p);
250 /* Else do it again correctly: */
251 # if (defined(DYNAMIC_LOADING) || defined(MSWIN32) || \
252 defined(MSWINCE) || defined(PCR)) \
255 GC_register_dynamic_libraries();
256 result = GC_is_static_root(p);
258 if (result) return(p);
262 /* p points to the heap. */
264 ptr_t base = GC_base(p); /* Should be manually inlined? */
266 if (base == 0) goto fail;
267 if (HBLKPTR(base) != HBLKPTR(p)) hhdr = HDR((word)p);
268 descr = hhdr -> hb_descr;
270 switch(descr & GC_DS_TAGS) {
272 if ((word)((ptr_t)p - (ptr_t)base) > (word)descr) goto fail;
275 if ((ptr_t)p - (ptr_t)base
276 >= WORDS_TO_BYTES(BITMAP_BITS)
277 || ((word)p & (sizeof(word) - 1))) goto fail;
278 if (!((1 << (WORDSZ - ((ptr_t)p - (ptr_t)base) - 1))
282 /* We could try to decipher this partially. */
283 /* For now we just punt. */
285 case GC_DS_PER_OBJECT:
286 if ((signed_word)descr >= 0) {
287 descr = *(word *)((ptr_t)base + (descr & ~GC_DS_TAGS));
289 ptr_t type_descr = *(ptr_t *)base;
290 descr = *(word *)(type_descr
291 - (descr - (GC_DS_PER_OBJECT
292 - GC_INDIR_PER_OBJ_BIAS)));
300 (*GC_is_visible_print_proc)((ptr_t)p);
305 GC_PTR GC_pre_incr (p, how_much)
310 GC_PTR result = GC_same_obj((GC_PTR)((word)initial + how_much), initial);
312 if (!GC_all_interior_pointers) {
313 (void) GC_is_valid_displacement(result);
315 return (*p = result);
318 GC_PTR GC_post_incr (p, how_much)
323 GC_PTR result = GC_same_obj((GC_PTR)((word)initial + how_much), initial);
325 if (!GC_all_interior_pointers) {
326 (void) GC_is_valid_displacement(result);