1 /* -----------------------------------------------------------------------------
2 * $Id: Sanity.c,v 1.3 1999/01/13 17:25:43 simonm Exp $
4 * Sanity checking code for the heap and stack.
6 * Used when debugging: check that the stack looks reasonable.
8 * - All things that are supposed to be pointers look like pointers.
10 * - Objects in text space are marked as static closures, those
11 * in the heap are dynamic.
13 * ---------------------------------------------------------------------------*/
21 #include "BlockAlloc.h"
24 #define LOOKS_LIKE_PTR(r) \
25 (IS_DATA_PTR(r) || ((IS_USER_PTR(r) && Bdescr((P_)r)->free != (void *)-1)))
27 /* -----------------------------------------------------------------------------
29 -------------------------------------------------------------------------- */
31 StgOffset checkStackClosure( StgClosure* c );
33 StgOffset checkStackObject( StgPtr sp );
35 void checkStackChunk( StgPtr sp, StgPtr stack_end );
37 static StgOffset checkSmallBitmap( StgPtr payload, StgNat32 bitmap );
39 static StgOffset checkLargeBitmap( StgPtr payload,
40 StgLargeBitmap* large_bitmap );
42 void checkClosureShallow( StgClosure* p );
45 checkSmallBitmap( StgPtr payload, StgNat32 bitmap )
50 for(; bitmap != 0; ++i, bitmap >>= 1 ) {
51 if ((bitmap & 1) == 0) {
52 checkClosure(stgCast(StgClosure*,payload[i]));
60 checkLargeBitmap( StgPtr payload, StgLargeBitmap* large_bitmap )
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]));
78 checkStackClosure( StgClosure* c )
80 const StgInfoTable* info = get_itbl(c);
82 /* All activation records have 'bitmap' style layout info. */
84 case RET_DYN: /* Dynamic bitmap: the mask is stored on the stack */
86 StgRetDyn* r = stgCast(StgRetDyn*,c);
87 return sizeofW(StgRetDyn) +
88 checkSmallBitmap(r->payload,r->liveness);
90 case RET_BCO: /* small bitmap (<= 32 entries) */
97 return sizeofW(StgClosure) +
98 checkSmallBitmap((StgPtr)c->payload,info->layout.bitmap);
99 case RET_BIG: /* large bitmap (> 32 entries) */
101 return sizeofW(StgClosure) +
102 checkLargeBitmap((StgPtr)c->payload,
103 info->layout.large_bitmap);
105 case FUN_STATIC: /* probably a slow-entry point return address: */
108 /* if none of the above, maybe it's a closure which looks a
109 * little like an infotable
111 checkClosureShallow(*stgCast(StgClosure**,c));
113 /* barf("checkStackClosure: weird activation record found on stack (%p).",c); */
118 * check that it looks like a valid closure - without checking its payload
119 * used to avoid recursion between checking PAPs and checking stack
124 checkClosureShallow( StgClosure* p )
126 ASSERT(LOOKS_LIKE_GHC_INFO(p->header.info));
128 /* Is it a static closure (i.e. in the data segment)? */
129 if (LOOKS_LIKE_STATIC(p)) {
130 ASSERT(closure_STATIC(p));
132 ASSERT(!closure_STATIC(p));
133 ASSERT(LOOKS_LIKE_PTR(p));
137 /* check an individual stack object */
139 checkStackObject( StgPtr sp )
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).
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));
155 /* check sections of stack between update frames */
157 checkStackChunk( StgPtr sp, StgPtr stack_end )
162 while (p < stack_end) {
163 p += checkStackObject( p );
165 ASSERT( p == stack_end );
169 checkClosure( StgClosure* p )
171 const StgInfoTable *info;
174 ASSERT(LOOKS_LIKE_GHC_INFO(p->header.info));
177 /* Is it a static closure (i.e. in the data segment)? */
178 if (LOOKS_LIKE_STATIC(p)) {
179 ASSERT(closure_STATIC(p));
181 ASSERT(!closure_STATIC(p));
182 ASSERT(LOOKS_LIKE_PTR(p));
186 switch (info->type) {
189 StgBCO* bco = stgCast(StgBCO*,p);
191 for(i=0; i < bco->n_ptrs; ++i) {
192 ASSERT(LOOKS_LIKE_PTR(bcoConstPtr(bco,i)));
194 return bco_sizeW(bco);
201 case IND_OLDGEN_PERM:
210 case CONSTR_CHARLIKE:
212 case CONSTR_NOCAF_STATIC:
218 for (i = 0; i < info->layout.payload.ptrs; i++) {
219 ASSERT(LOOKS_LIKE_PTR(payloadPtr(p,i)));
221 return sizeW_fromITBL(info);
225 /* deal with these specially - the info table isn't
226 * representative of the actual layout.
228 { StgWeak *w = (StgWeak *)p;
229 ASSERT(LOOKS_LIKE_PTR(w->key));
230 ASSERT(LOOKS_LIKE_PTR(w->value));
231 ASSERT(LOOKS_LIKE_PTR(w->finaliser));
233 ASSERT(LOOKS_LIKE_PTR(w->link));
235 return sizeW_fromITBL(info);
239 ASSERT(LOOKS_LIKE_PTR(stgCast(StgSelector*,p)->selectee));
240 return sizeofW(StgHeader) + MIN_UPD_SIZE;
244 /* we don't expect to see any of these after GC
245 * but they might appear during execution
248 StgInd *ind = stgCast(StgInd*,p);
249 ASSERT(LOOKS_LIKE_PTR(ind->indirectee));
250 q = (P_)p + sizeofW(StgInd);
251 while (!*q) { q++; }; /* skip padding words (see GC.c: evacuate())*/
265 barf("checkClosure: stack frame");
267 case AP_UPD: /* we can treat this as being the same as a PAP */
270 StgPAP *pap = stgCast(StgPAP*,p);
271 ASSERT(LOOKS_LIKE_PTR(pap->fun));
272 checkStackChunk((StgPtr)pap->payload,
273 (StgPtr)pap->payload + pap->n_args
275 return pap_sizeW(pap);
280 return arr_words_sizeW(stgCast(StgArrWords*,p));
283 case MUT_ARR_PTRS_FROZEN:
285 StgMutArrPtrs* a = stgCast(StgMutArrPtrs*,p);
287 for (i = 0; i < a->ptrs; i++) {
288 ASSERT(LOOKS_LIKE_PTR(a->payload[i]));
290 return mut_arr_ptrs_sizeW(a);
294 checkTSO((StgTSO *)p);
295 return tso_sizeW((StgTSO *)p);
300 barf("checkClosure: unimplemented/strange closure type");
302 barf("checkClosure");
304 #undef LOOKS_LIKE_PTR
307 /* -----------------------------------------------------------------------------
310 After garbage collection, the live heap is in a state where we can
311 run through and check that all the pointers point to the right
312 place. This function starts at a given position and sanity-checks
313 all the objects in the remainder of the chain.
314 -------------------------------------------------------------------------- */
317 checkHeap(bdescr *bd, StgPtr start)
328 while (p < bd->free) {
329 nat size = checkClosure(stgCast(StgClosure*,p));
330 /* This is the smallest size of closure that can live in the heap. */
331 ASSERT( size >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
333 while (*p == 0) { p++; } /* skip over slop */
343 checkChain(bdescr *bd)
346 checkClosure((StgClosure *)bd->start);
351 /* check stack - making sure that update frames are linked correctly */
353 checkStack(StgPtr sp, StgPtr stack_end, StgUpdateFrame* su )
355 /* check everything down to the first update frame */
356 checkStackChunk( sp, stgCast(StgPtr,su) );
357 while ( stgCast(StgPtr,su) < stack_end) {
358 sp = stgCast(StgPtr,su);
359 switch (get_itbl(su)->type) {
364 su = stgCast(StgSeqFrame*,su)->link;
367 su = stgCast(StgCatchFrame*,su)->link;
370 /* not quite: ASSERT(stgCast(StgPtr,su) == stack_end); */
373 barf("checkStack: weird record found on update frame list.");
375 checkStackChunk( sp, stgCast(StgPtr,su) );
377 ASSERT(stgCast(StgPtr,su) == stack_end);
381 checkTSO(StgTSO *tso)
384 StgPtr stack = tso->stack;
385 StgUpdateFrame* su = tso->su;
386 StgOffset stack_size = tso->stack_size;
387 StgPtr stack_end = stack + stack_size;
389 if (tso->whatNext == ThreadComplete || tso->whatNext == ThreadKilled) {
390 /* The garbage collector doesn't bother following any pointers
391 * from dead threads, so don't check sanity here.
396 ASSERT(stack <= sp && sp < stack_end);
397 ASSERT(sp <= stgCast(StgPtr,su));
399 checkStack(sp, stack_end, su);
402 /* -----------------------------------------------------------------------------
403 Check Blackhole Sanity
405 Test whether an object is already on the update list.
406 It isn't necessarily an rts error if it is - it might be a programming
409 Future versions might be able to test for a blackhole without traversing
410 the update frame list.
412 -------------------------------------------------------------------------- */
413 rtsBool isBlackhole( StgTSO* tso, StgClosure* p )
415 StgUpdateFrame* su = tso->su;
417 switch (get_itbl(su)->type) {
419 if (su->updatee == p) {
426 su = stgCast(StgSeqFrame*,su)->link;
429 su = stgCast(StgCatchFrame*,su)->link;
434 barf("isBlackhole: weird record found on update frame list.");