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