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"
22 void GC_default_same_obj_print_proc(GC_PTR p
, GC_PTR q
)
24 void GC_default_same_obj_print_proc (p
, q
)
28 GC_err_printf2("0x%lx and 0x%lx are not in the same object\n",
29 (unsigned long)p
, (unsigned long)q
);
30 ABORT("GC_same_obj test failed");
33 void (*GC_same_obj_print_proc
) GC_PROTO((GC_PTR
, GC_PTR
))
34 = GC_default_same_obj_print_proc
;
36 /* Check that p and q point to the same object. Call */
37 /* *GC_same_obj_print_proc if they don't. */
38 /* Returns the first argument. (Return value may be hard */
39 /* to use,due to typing issues. But if we had a suitable */
40 /* preprocessor ...) */
41 /* Succeeds if neither p nor q points to the heap. */
42 /* We assume this is performance critical. (It shouldn't */
43 /* be called by production code, but this can easily make */
44 /* debugging intolerably slow.) */
46 GC_PTR
GC_same_obj(register void *p
, register void *q
)
48 GC_PTR
GC_same_obj(p
, q
)
52 register struct hblk
*h
;
54 register ptr_t base
, limit
;
57 if (!GC_is_initialized
) GC_init();
60 if (divHBLKSZ((word
)p
) != divHBLKSZ((word
)q
)
61 && HDR((word
)q
) != 0) {
66 /* If it's a pointer to the middle of a large object, move it */
67 /* to the beginning. */
68 if (IS_FORWARDING_ADDR_OR_NIL(hhdr
)) {
69 h
= HBLKPTR(p
) - (word
)hhdr
;
71 while (IS_FORWARDING_ADDR_OR_NIL(hhdr
)) {
72 h
= FORWARDED_ADDR(h
, hhdr
);
75 limit
= (ptr_t
)((word
*)h
+ hhdr
-> hb_sz
);
76 if ((ptr_t
)p
>= limit
|| (ptr_t
)q
>= limit
|| (ptr_t
)q
< (ptr_t
)h
) {
81 sz
= WORDS_TO_BYTES(hhdr
-> hb_sz
);
82 if (sz
> MAXOBJBYTES
) {
83 base
= (ptr_t
)HBLKPTR(p
);
85 if ((ptr_t
)p
>= limit
) {
89 register int map_entry
;
90 register int pdispl
= HBLKDISPL(p
);
92 map_entry
= MAP_ENTRY((hhdr
-> hb_map
), pdispl
);
93 if (map_entry
> CPP_MAX_OFFSET
) {
94 map_entry
= BYTES_TO_WORDS(pdispl
) % BYTES_TO_WORDS(sz
);
95 if (HBLKPTR(p
) != HBLKPTR(q
)) goto fail
;
96 /* W/o this check, we might miss an error if */
97 /* q points to the first object on a page, and */
98 /* points just before the page. */
100 base
= (char *)((word
)p
& ~(WORDS_TO_BYTES(1) - 1));
101 base
-= WORDS_TO_BYTES(map_entry
);
104 /* [base, limit) delimits the object containing p, if any. */
105 /* If p is not inside a valid object, then either q is */
106 /* also outside any valid object, or it is outside */
108 if ((ptr_t
)q
>= limit
|| (ptr_t
)q
< base
) {
113 (*GC_same_obj_print_proc
)((ptr_t
)p
, (ptr_t
)q
);
118 void GC_default_is_valid_displacement_print_proc (GC_PTR p
)
120 void GC_default_is_valid_displacement_print_proc (p
)
124 GC_err_printf1("0x%lx does not point to valid object displacement\n",
126 ABORT("GC_is_valid_displacement test failed");
129 void (*GC_is_valid_displacement_print_proc
) GC_PROTO((GC_PTR
)) =
130 GC_default_is_valid_displacement_print_proc
;
132 /* Check that if p is a pointer to a heap page, then it points to */
133 /* a valid displacement within a heap object. */
134 /* Uninteresting with GC_all_interior_pointers. */
135 /* Always returns its argument. */
136 /* Note that we don't lock, since nothing relevant about the header */
137 /* should change while we have a valid object pointer to the block. */
139 void * GC_is_valid_displacement(void *p
)
141 char *GC_is_valid_displacement(p
)
146 register word pdispl
;
147 register struct hblk
*h
;
148 register map_entry_type map_entry
;
151 if (!GC_is_initialized
) GC_init();
153 if (hhdr
== 0) return(p
);
155 if (GC_all_interior_pointers
) {
156 while (IS_FORWARDING_ADDR_OR_NIL(hhdr
)) {
157 h
= FORWARDED_ADDR(h
, hhdr
);
161 if (IS_FORWARDING_ADDR_OR_NIL(hhdr
)) {
164 sz
= WORDS_TO_BYTES(hhdr
-> hb_sz
);
165 pdispl
= HBLKDISPL(p
);
166 map_entry
= MAP_ENTRY((hhdr
-> hb_map
), pdispl
);
167 if (map_entry
== OBJ_INVALID
168 || sz
> MAXOBJBYTES
&& (ptr_t
)p
>= (ptr_t
)h
+ sz
) {
173 (*GC_is_valid_displacement_print_proc
)((ptr_t
)p
);
178 void GC_default_is_visible_print_proc(GC_PTR p
)
180 void GC_default_is_visible_print_proc(p
)
184 GC_err_printf1("0x%lx is not a GC visible pointer location\n",
186 ABORT("GC_is_visible test failed");
189 void (*GC_is_visible_print_proc
) GC_PROTO((GC_PTR p
)) =
190 GC_default_is_visible_print_proc
;
192 /* Could p be a stack address? */
193 GC_bool
GC_on_stack(p
)
200 # ifdef STACK_GROWS_DOWN
201 if ((ptr_t
)p
>= (ptr_t
)(&dummy
) && (ptr_t
)p
< GC_stackbottom
) {
205 if ((ptr_t
)p
<= (ptr_t
)(&dummy
) && (ptr_t
)p
> GC_stackbottom
) {
213 /* Check that p is visible */
214 /* to the collector as a possibly pointer containing location. */
215 /* If it isn't invoke *GC_is_visible_print_proc. */
216 /* Returns the argument in all cases. May erroneously succeed */
217 /* in hard cases. (This is intended for debugging use with */
218 /* untyped allocations. The idea is that it should be possible, though */
219 /* slow, to add such a call to all indirect pointer stores.) */
220 /* Currently useless for multithreaded worlds. */
222 void * GC_is_visible(void *p
)
224 char *GC_is_visible(p
)
230 if ((word
)p
& (ALIGNMENT
- 1)) goto fail
;
231 if (!GC_is_initialized
) GC_init();
234 if (hhdr
!= 0 && GC_base(p
) == 0) {
237 /* May be inside thread stack. We can't do much. */
241 /* Check stack first: */
242 if (GC_on_stack(p
)) return(p
);
247 if (GC_is_static_root(p
)) return(p
);
248 /* Else do it again correctly: */
249 # if (defined(DYNAMIC_LOADING) || defined(MSWIN32) || \
250 defined(MSWINCE) || defined (CYGWIN32) || defined(PCR)) \
253 GC_register_dynamic_libraries();
254 result
= GC_is_static_root(p
);
256 if (result
) return(p
);
260 /* p points to the heap. */
262 ptr_t base
= GC_base(p
); /* Should be manually inlined? */
264 if (base
== 0) goto fail
;
265 if (HBLKPTR(base
) != HBLKPTR(p
)) hhdr
= HDR((word
)p
);
266 descr
= hhdr
-> hb_descr
;
268 switch(descr
& GC_DS_TAGS
) {
270 if ((word
)((ptr_t
)p
- (ptr_t
)base
) > (word
)descr
) goto fail
;
273 if ((ptr_t
)p
- (ptr_t
)base
274 >= WORDS_TO_BYTES(BITMAP_BITS
)
275 || ((word
)p
& (sizeof(word
) - 1))) goto fail
;
276 if (!((1 << (WORDSZ
- ((ptr_t
)p
- (ptr_t
)base
) - 1))
280 /* We could try to decipher this partially. */
281 /* For now we just punt. */
283 case GC_DS_PER_OBJECT
:
284 if ((signed_word
)descr
>= 0) {
285 descr
= *(word
*)((ptr_t
)base
+ (descr
& ~GC_DS_TAGS
));
287 ptr_t type_descr
= *(ptr_t
*)base
;
288 descr
= *(word
*)(type_descr
289 - (descr
- (GC_DS_PER_OBJECT
290 - GC_INDIR_PER_OBJ_BIAS
)));
298 (*GC_is_visible_print_proc
)((ptr_t
)p
);
303 GC_PTR
GC_pre_incr (p
, how_much
)
308 GC_PTR result
= GC_same_obj((GC_PTR
)((word
)initial
+ how_much
), initial
);
310 if (!GC_all_interior_pointers
) {
311 (void) GC_is_valid_displacement(result
);
313 return (*p
= result
);
316 GC_PTR
GC_post_incr (p
, how_much
)
321 GC_PTR result
= GC_same_obj((GC_PTR
)((word
)initial
+ how_much
), initial
);
323 if (!GC_all_interior_pointers
) {
324 (void) GC_is_valid_displacement(result
);