cef528a661d686dd72476b079f64818920e620fe
[ghc-hetmet.git] / ghc / rts / Sanity.c
1 /* -----------------------------------------------------------------------------
2  * $Id: Sanity.c,v 1.5 1999/01/18 15:18:06 simonm Exp $
3  *
4  * Sanity checking code for the heap and stack.
5  *
6  * Used when debugging: check that the stack looks reasonable.
7  *
8  *    - All things that are supposed to be pointers look like pointers.
9  *
10  *    - Objects in text space are marked as static closures, those
11  *      in the heap are dynamic.
12  *
13  * ---------------------------------------------------------------------------*/
14
15 #include "Rts.h"
16
17 #ifdef DEBUG
18
19 #include "RtsFlags.h"
20 #include "RtsUtils.h"
21 #include "BlockAlloc.h"
22 #include "Sanity.h"
23
24 #define LOOKS_LIKE_PTR(r) \
25   (IS_DATA_PTR(r) || ((IS_USER_PTR(r) && Bdescr((P_)r)->free != (void *)-1)))
26
27 /* -----------------------------------------------------------------------------
28    Check stack sanity
29    -------------------------------------------------------------------------- */
30
31 StgOffset checkStackClosure( StgClosure* c );
32
33 StgOffset checkStackObject( StgPtr sp );
34
35 void      checkStackChunk( StgPtr sp, StgPtr stack_end );
36
37 static StgOffset checkSmallBitmap(  StgPtr payload, StgNat32 bitmap );
38
39 static StgOffset checkLargeBitmap( StgPtr payload, 
40                                    StgLargeBitmap* large_bitmap );
41
42 void checkClosureShallow( StgClosure* p );
43
44 static StgOffset 
45 checkSmallBitmap( StgPtr payload, StgNat32 bitmap )
46 {
47     StgOffset i;
48
49     i = 0;
50     for(; bitmap != 0; ++i, bitmap >>= 1 ) {
51         if ((bitmap & 1) == 0) {
52             checkClosure(stgCast(StgClosure*,payload[i]));
53         }
54     }
55     return i;
56 }
57
58
59 static StgOffset 
60 checkLargeBitmap( StgPtr payload, StgLargeBitmap* large_bitmap )
61 {
62     StgNat32 bmp;
63     StgOffset i;
64
65     i = 0;
66     for (bmp=0; bmp<large_bitmap->size; bmp++) {
67         StgNat32 bitmap = large_bitmap->bitmap[bmp];
68         for(; bitmap != 0; ++i, bitmap >>= 1 ) {
69             if ((bitmap & 1) == 0) {
70                 checkClosure(stgCast(StgClosure*,payload[i]));
71             }
72         }
73     }
74     return i;
75 }
76
77 StgOffset 
78 checkStackClosure( StgClosure* c )
79 {    
80     const StgInfoTable* info = get_itbl(c);
81
82     /* All activation records have 'bitmap' style layout info. */
83     switch (info->type) {
84     case RET_DYN: /* Dynamic bitmap: the mask is stored on the stack */
85         {
86             StgRetDyn* r = stgCast(StgRetDyn*,c);
87             return sizeofW(StgRetDyn) + 
88                    checkSmallBitmap(r->payload,r->liveness);
89         }
90     case RET_BCO: /* small bitmap (<= 32 entries) */
91     case RET_SMALL:
92     case RET_VEC_SMALL:
93     case UPDATE_FRAME:
94     case CATCH_FRAME:
95     case STOP_FRAME:
96     case SEQ_FRAME:
97             return sizeofW(StgClosure) + 
98                    checkSmallBitmap((StgPtr)c->payload,info->layout.bitmap);
99     case RET_BIG: /* large bitmap (> 32 entries) */
100     case RET_VEC_BIG:
101             return sizeofW(StgClosure) + 
102                    checkLargeBitmap((StgPtr)c->payload,
103                                     info->layout.large_bitmap);
104     case FUN:
105     case FUN_STATIC: /* probably a slow-entry point return address: */
106             return 1;
107     default:
108             /* if none of the above, maybe it's a closure which looks a
109              * little like an infotable
110              */
111             checkClosureShallow(*stgCast(StgClosure**,c));
112             return 1;
113             /* barf("checkStackClosure: weird activation record found on stack (%p).",c); */
114     }
115 }
116
117 /*
118  * check that it looks like a valid closure - without checking its payload
119  * used to avoid recursion between checking PAPs and checking stack
120  * chunks.
121  */
122  
123 void 
124 checkClosureShallow( StgClosure* p )
125 {
126     ASSERT(LOOKS_LIKE_GHC_INFO(p->header.info));
127
128     /* Is it a static closure (i.e. in the data segment)? */
129     if (LOOKS_LIKE_STATIC(p)) {
130         ASSERT(closure_STATIC(p));
131     } else {
132         ASSERT(!closure_STATIC(p));
133         ASSERT(LOOKS_LIKE_PTR(p));
134     }
135 }
136
137 /* check an individual stack object */
138 StgOffset 
139 checkStackObject( StgPtr sp )
140 {
141     if (IS_ARG_TAG(*sp)) {
142         /* Tagged words might be "stubbed" pointers, so there's no
143          * point checking to see whether they look like pointers or
144          * not (some of them will).
145          */
146         return ARG_SIZE(*sp) + 1;
147     } else if (LOOKS_LIKE_GHC_INFO(*stgCast(StgPtr*,sp))) {
148         return checkStackClosure(stgCast(StgClosure*,sp));
149     } else { /* must be an untagged closure pointer in the stack */
150         checkClosureShallow(*stgCast(StgClosure**,sp));
151         return 1;
152     }
153 }
154
155 /* check sections of stack between update frames */
156 void 
157 checkStackChunk( StgPtr sp, StgPtr stack_end )
158 {
159     StgPtr p;
160
161     p = sp;
162     while (p < stack_end) {
163         p += checkStackObject( p );
164     }
165     ASSERT( p == stack_end );
166 }
167
168 StgOffset 
169 checkClosure( StgClosure* p )
170 {
171     const StgInfoTable *info;
172
173 #ifndef INTERPRETER    
174     ASSERT(LOOKS_LIKE_GHC_INFO(p->header.info));
175 #endif
176
177     /* Is it a static closure (i.e. in the data segment)? */
178     if (LOOKS_LIKE_STATIC(p)) {
179         ASSERT(closure_STATIC(p));
180     } else {
181         ASSERT(!closure_STATIC(p));
182         ASSERT(LOOKS_LIKE_PTR(p));
183     }
184
185     info = get_itbl(p);
186     switch (info->type) {
187     case BCO:
188         {
189             StgBCO* bco = stgCast(StgBCO*,p);
190             nat i;
191             for(i=0; i < bco->n_ptrs; ++i) {
192                 ASSERT(LOOKS_LIKE_PTR(bcoConstPtr(bco,i)));
193             }
194             return bco_sizeW(bco);
195         }
196
197     case MVAR:
198       { 
199         StgMVar *mvar = (StgMVar *)p;
200         ASSERT(LOOKS_LIKE_PTR(mvar->head));
201         ASSERT(LOOKS_LIKE_PTR(mvar->tail));
202         ASSERT(LOOKS_LIKE_PTR(mvar->value));
203         return sizeofW(StgMVar);
204       }
205
206     case FUN:
207     case THUNK:
208     case CONSTR:
209     case IND_PERM:
210     case IND_OLDGEN:
211     case IND_OLDGEN_PERM:
212     case CAF_UNENTERED:
213     case CAF_ENTERED:
214     case CAF_BLACKHOLE:
215     case BLACKHOLE:
216     case BLACKHOLE_BQ:
217     case FOREIGN:
218     case MUT_VAR:
219     case CONSTR_INTLIKE:
220     case CONSTR_CHARLIKE:
221     case CONSTR_STATIC:
222     case CONSTR_NOCAF_STATIC:
223     case THUNK_STATIC:
224     case FUN_STATIC:
225     case IND_STATIC:
226         {
227             nat i;
228             for (i = 0; i < info->layout.payload.ptrs; i++) {
229                 ASSERT(LOOKS_LIKE_PTR(payloadPtr(p,i)));
230             }
231             return sizeW_fromITBL(info);
232         }
233
234     case WEAK:
235       /* deal with these specially - the info table isn't
236        * representative of the actual layout.
237        */
238       { StgWeak *w = (StgWeak *)p;
239         ASSERT(LOOKS_LIKE_PTR(w->key));
240         ASSERT(LOOKS_LIKE_PTR(w->value));
241         ASSERT(LOOKS_LIKE_PTR(w->finaliser));
242         if (w->link) {
243           ASSERT(LOOKS_LIKE_PTR(w->link));
244         }
245         return sizeW_fromITBL(info);
246       }
247
248     case THUNK_SELECTOR:
249             ASSERT(LOOKS_LIKE_PTR(stgCast(StgSelector*,p)->selectee));
250             return sizeofW(StgHeader) + MIN_UPD_SIZE;
251
252     case IND:
253         { 
254             /* we don't expect to see any of these after GC
255              * but they might appear during execution
256              */
257             P_ q;
258             StgInd *ind = stgCast(StgInd*,p);
259             ASSERT(LOOKS_LIKE_PTR(ind->indirectee));
260             q = (P_)p + sizeofW(StgInd);
261             while (!*q) { q++; }; /* skip padding words (see GC.c: evacuate())*/
262             return q - (P_)p;
263         }
264
265     case RET_BCO:
266     case RET_SMALL:
267     case RET_VEC_SMALL:
268     case RET_BIG:
269     case RET_VEC_BIG:
270     case RET_DYN:
271     case UPDATE_FRAME:
272     case STOP_FRAME:
273     case CATCH_FRAME:
274     case SEQ_FRAME:
275             barf("checkClosure: stack frame");
276
277     case AP_UPD: /* we can treat this as being the same as a PAP */
278     case PAP:
279         { 
280             StgPAP *pap = stgCast(StgPAP*,p);
281             ASSERT(LOOKS_LIKE_PTR(pap->fun));
282             checkStackChunk((StgPtr)pap->payload, 
283                             (StgPtr)pap->payload + pap->n_args
284                             );
285             return pap_sizeW(pap);
286         }
287
288     case ARR_WORDS:
289     case MUT_ARR_WORDS:
290             return arr_words_sizeW(stgCast(StgArrWords*,p));
291
292     case MUT_ARR_PTRS:
293     case MUT_ARR_PTRS_FROZEN:
294         {
295             StgMutArrPtrs* a = stgCast(StgMutArrPtrs*,p);
296             nat i;
297             for (i = 0; i < a->ptrs; i++) {
298                 ASSERT(LOOKS_LIKE_PTR(a->payload[i]));
299             }
300             return mut_arr_ptrs_sizeW(a);
301         }
302
303     case TSO:
304         checkTSO((StgTSO *)p);
305         return tso_sizeW((StgTSO *)p);
306
307     case BLOCKED_FETCH:
308     case FETCH_ME:
309     case EVACUATED:
310             barf("checkClosure: unimplemented/strange closure type");
311     default:
312             barf("checkClosure");
313     }
314 #undef LOOKS_LIKE_PTR
315 }
316
317 /* -----------------------------------------------------------------------------
318    Check Heap Sanity
319
320    After garbage collection, the live heap is in a state where we can
321    run through and check that all the pointers point to the right
322    place.  This function starts at a given position and sanity-checks
323    all the objects in the remainder of the chain.
324    -------------------------------------------------------------------------- */
325
326 extern void 
327 checkHeap(bdescr *bd, StgPtr start)
328 {
329     StgPtr p;
330
331     if (start == NULL) {
332       p = bd->start;
333     } else {
334       p = start;
335     }
336
337     while (bd != NULL) {
338       while (p < bd->free) {
339         nat size = checkClosure(stgCast(StgClosure*,p));
340         /* This is the smallest size of closure that can live in the heap. */
341         ASSERT( size >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
342         p += size;
343         while (p < bd->free &&
344                *p && !LOOKS_LIKE_GHC_INFO(*p)) { p++; } /* skip over slop */
345       }
346       bd = bd->link;
347       if (bd != NULL) {
348         p = bd->start;
349       }
350     }
351 }
352
353 extern void
354 checkChain(bdescr *bd)
355 {
356   while (bd != NULL) {
357     checkClosure((StgClosure *)bd->start);
358     bd = bd->link;
359   }
360 }
361
362 /* check stack - making sure that update frames are linked correctly */
363 void 
364 checkStack(StgPtr sp, StgPtr stack_end, StgUpdateFrame* su )
365 {
366     /* check everything down to the first update frame */
367     checkStackChunk( sp, stgCast(StgPtr,su) );
368     while ( stgCast(StgPtr,su) < stack_end) {
369         sp = stgCast(StgPtr,su);
370         switch (get_itbl(su)->type) {
371         case UPDATE_FRAME:
372                 su = su->link;
373                 break;
374         case SEQ_FRAME:
375                 su = stgCast(StgSeqFrame*,su)->link;
376                 break;
377         case CATCH_FRAME:
378                 su = stgCast(StgCatchFrame*,su)->link;
379                 break;
380         case STOP_FRAME:
381                 /* not quite: ASSERT(stgCast(StgPtr,su) == stack_end); */
382                 return;
383         default:
384                 barf("checkStack: weird record found on update frame list.");
385         }
386         checkStackChunk( sp, stgCast(StgPtr,su) );
387     }
388     ASSERT(stgCast(StgPtr,su) == stack_end);
389 }
390
391 extern void
392 checkTSO(StgTSO *tso)
393 {
394     StgPtr sp = tso->sp;
395     StgPtr stack = tso->stack;
396     StgUpdateFrame* su = tso->su;
397     StgOffset stack_size = tso->stack_size;
398     StgPtr stack_end = stack + stack_size;
399
400     if (tso->whatNext == ThreadComplete ||  tso->whatNext == ThreadKilled) {
401       /* The garbage collector doesn't bother following any pointers
402        * from dead threads, so don't check sanity here.  
403        */
404       return;
405     }
406
407     ASSERT(stack <= sp && sp < stack_end);
408     ASSERT(sp <= stgCast(StgPtr,su));
409
410     checkStack(sp, stack_end, su);
411 }
412
413 /* -----------------------------------------------------------------------------
414    Check Blackhole Sanity
415
416    Test whether an object is already on the update list.
417    It isn't necessarily an rts error if it is - it might be a programming
418    error.
419
420    Future versions might be able to test for a blackhole without traversing
421    the update frame list.
422
423    -------------------------------------------------------------------------- */
424 rtsBool isBlackhole( StgTSO* tso, StgClosure* p )
425 {
426   StgUpdateFrame* su = tso->su;
427   do {
428     switch (get_itbl(su)->type) {
429     case UPDATE_FRAME:
430       if (su->updatee == p) {
431         return rtsTrue;
432       } else {
433         su = su->link;
434       }
435       break;
436     case SEQ_FRAME:
437       su = stgCast(StgSeqFrame*,su)->link;
438       break;
439     case CATCH_FRAME:
440       su = stgCast(StgCatchFrame*,su)->link;
441       break;
442     case STOP_FRAME:
443       return rtsFalse;
444     default:
445       barf("isBlackhole: weird record found on update frame list.");
446     }
447   } while (1);
448 }
449
450 #endif /* DEBUG */