* Moved boehm-gc from src/ to src/mm/.
[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 #ifdef __STDC__
24 void GC_default_same_obj_print_proc(GC_PTR p, GC_PTR q)
25 #else
26 void GC_default_same_obj_print_proc (p, q)
27 GC_PTR p, q;
28 #endif
29 {
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");
33 }
34
35 void (*GC_same_obj_print_proc) GC_PROTO((GC_PTR, GC_PTR))
36                 = GC_default_same_obj_print_proc;
37
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.)                                 */
47 #ifdef __STDC__
48   GC_PTR GC_same_obj(register void *p, register void *q)
49 #else
50   GC_PTR GC_same_obj(p, q)
51   register char *p, *q;
52 #endif
53 {
54     register struct hblk *h;
55     register hdr *hhdr;
56     register ptr_t base, limit;
57     register word sz;
58     
59     if (!GC_is_initialized) GC_init();
60     hhdr = HDR((word)p);
61     if (hhdr == 0) {
62         if (divHBLKSZ((word)p) != divHBLKSZ((word)q)
63             && HDR((word)q) != 0) {
64             goto fail;
65         }
66         return(p);
67     }
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;
72         hhdr = HDR(h);
73         while (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
74            h = FORWARDED_ADDR(h, hhdr);
75            hhdr = HDR(h);
76         }
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 ) {
79             goto fail;
80         }
81         return(p);
82     }
83     sz = WORDS_TO_BYTES(hhdr -> hb_sz);
84     if (sz > MAXOBJBYTES) {
85       base = (ptr_t)HBLKPTR(p);
86       limit = base + sz;
87       if ((ptr_t)p >= limit) {
88         goto fail;
89       }
90     } else {
91       register int map_entry;
92       register int pdispl = HBLKDISPL(p);
93       
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.                 */
101       }
102       base = (char *)((word)p & ~(WORDS_TO_BYTES(1) - 1));
103       base -= WORDS_TO_BYTES(map_entry);
104       limit = base + sz;
105     }
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          */
109     /* [base, limit).                                           */
110     if ((ptr_t)q >= limit || (ptr_t)q < base) {
111         goto fail;
112     }
113     return(p);
114 fail:
115     (*GC_same_obj_print_proc)((ptr_t)p, (ptr_t)q);
116     return(p);
117 }
118
119 #ifdef __STDC__
120 void GC_default_is_valid_displacement_print_proc (GC_PTR p)
121 #else
122 void GC_default_is_valid_displacement_print_proc (p)
123 GC_PTR p;
124 #endif
125 {
126     GC_err_printf1("0x%lx does not point to valid object displacement\n",
127                    (unsigned long)p);
128     ABORT("GC_is_valid_displacement test failed");
129 }
130
131 void (*GC_is_valid_displacement_print_proc) GC_PROTO((GC_PTR)) = 
132         GC_default_is_valid_displacement_print_proc;
133
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.     */
140 #ifdef __STDC__
141   void * GC_is_valid_displacement(void *p)
142 #else
143   char *GC_is_valid_displacement(p)
144   char *p;
145 #endif
146 {
147     register hdr *hhdr;
148     register word pdispl;
149     register struct hblk *h;
150     register map_entry_type map_entry;
151     register word sz;
152     
153     if (!GC_is_initialized) GC_init();
154     hhdr = HDR((word)p);
155     if (hhdr == 0) return(p);
156     h = HBLKPTR(p);
157     if (GC_all_interior_pointers) {
158         while (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
159            h = FORWARDED_ADDR(h, hhdr);
160            hhdr = HDR(h);
161         }
162     }
163     if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
164         goto fail;
165     }
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) {
171         goto fail;
172     }
173     return(p);
174 fail:
175     (*GC_is_valid_displacement_print_proc)((ptr_t)p);
176     return(p);
177 }
178
179 #ifdef __STDC__
180 void GC_default_is_visible_print_proc(GC_PTR p)
181 #else
182 void GC_default_is_visible_print_proc(p)
183 GC_PTR p;
184 #endif
185 {
186     GC_err_printf1("0x%lx is not a GC visible pointer location\n",
187                    (unsigned long)p);
188     ABORT("GC_is_visible test failed");
189 }
190
191 void (*GC_is_visible_print_proc) GC_PROTO((GC_PTR p)) = 
192         GC_default_is_visible_print_proc;
193
194 /* Could p be a stack address? */
195 GC_bool GC_on_stack(p)
196 ptr_t p;
197 {
198 #   ifdef THREADS
199         return(TRUE);
200 #   else
201         int dummy;
202 #       ifdef STACK_GROWS_DOWN
203             if ((ptr_t)p >= (ptr_t)(&dummy) && (ptr_t)p < GC_stackbottom ) {
204                 return(TRUE);
205             }
206 #       else
207             if ((ptr_t)p <= (ptr_t)(&dummy) && (ptr_t)p > GC_stackbottom ) {
208                 return(TRUE);
209             }
210 #       endif
211         return(FALSE);
212 #   endif
213 }
214
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.                          */
223 #ifdef __STDC__
224   void * GC_is_visible(void *p)
225 #else
226   char *GC_is_visible(p)
227   char *p;
228 #endif
229 {
230     register hdr *hhdr;
231     
232     if ((word)p & (ALIGNMENT - 1)) goto fail;
233     if (!GC_is_initialized) GC_init();
234 #   ifdef THREADS
235         hhdr = HDR((word)p);
236         if (hhdr != 0 && GC_base(p) == 0) {
237             goto fail;
238         } else {
239             /* May be inside thread stack.  We can't do much. */
240             return(p);
241         }
242 #   else
243         /* Check stack first: */
244           if (GC_on_stack(p)) return(p);
245         hhdr = HDR((word)p);
246         if (hhdr == 0) {
247             GC_bool result;
248             
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)) \
253                 && !defined(SRC_M3)
254                 DISABLE_SIGNALS();
255                 GC_register_dynamic_libraries();
256                 result = GC_is_static_root(p);
257                 ENABLE_SIGNALS();
258                 if (result) return(p);
259 #           endif
260             goto fail;
261         } else {
262             /* p points to the heap. */
263             word descr;
264             ptr_t base = GC_base(p);    /* Should be manually inlined? */
265             
266             if (base == 0) goto fail;
267             if (HBLKPTR(base) != HBLKPTR(p)) hhdr = HDR((word)p);
268             descr = hhdr -> hb_descr;
269     retry:
270             switch(descr & GC_DS_TAGS) {
271                 case GC_DS_LENGTH:
272                     if ((word)((ptr_t)p - (ptr_t)base) > (word)descr) goto fail;
273                     break;
274                 case GC_DS_BITMAP:
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))
279                           & descr)) goto fail;
280                     break;
281                 case GC_DS_PROC:
282                     /* We could try to decipher this partially.         */
283                     /* For now we just punt.                            */
284                     break;
285                 case GC_DS_PER_OBJECT:
286                     if ((signed_word)descr >= 0) {
287                       descr = *(word *)((ptr_t)base + (descr & ~GC_DS_TAGS));
288                     } else {
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)));
293                     }
294                     goto retry;
295             }
296             return(p);
297         }
298 #   endif
299 fail:
300     (*GC_is_visible_print_proc)((ptr_t)p);
301     return(p);
302 }
303
304
305 GC_PTR GC_pre_incr (p, how_much)
306 GC_PTR *p;
307 size_t how_much;
308 {
309     GC_PTR initial = *p;
310     GC_PTR result = GC_same_obj((GC_PTR)((word)initial + how_much), initial);
311     
312     if (!GC_all_interior_pointers) {
313         (void) GC_is_valid_displacement(result);
314     }
315     return (*p = result);
316 }
317
318 GC_PTR GC_post_incr (p, how_much)
319 GC_PTR *p;
320 size_t how_much;
321 {
322     GC_PTR initial = *p;
323     GC_PTR result = GC_same_obj((GC_PTR)((word)initial + how_much), initial);
324  
325     if (!GC_all_interior_pointers) {
326         (void) GC_is_valid_displacement(result);
327     }
328     *p = result;
329     return(initial);
330 }