1 /*
2  * Copyright (c) 1992-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 #include "private/gc_priv.h"
15 
16 #ifdef CHECKSUMS
17 
18 /* This is debugging code intended to verify the results of dirty bit   */
19 /* computations. Works only in a single threaded environment.           */
20 /* We assume that stubborn objects are changed only when they are       */
21 /* enabled for writing.  (Certain kinds of writing are actually         */
22 /* safe under other conditions.)                                        */
23 #define NSUMS 10000
24 
25 #define OFFSET 0x10000
26 
27 typedef struct {
28         GC_bool new_valid;
29         word old_sum;
30         word new_sum;
31         struct hblk * block;    /* Block to which this refers + OFFSET  */
32                                 /* to hide it from collector.           */
33 } page_entry;
34 
35 page_entry GC_sums[NSUMS];
36 
37 STATIC word GC_faulted[NSUMS] = { 0 };
38                 /* Record of pages on which we saw a write fault.       */
39 
40 STATIC size_t GC_n_faulted = 0;
41 
GC_record_fault(struct hblk * h)42 void GC_record_fault(struct hblk * h)
43 {
44     word page = (word)h;
45 
46     page += GC_page_size - 1;
47     page &= ~(GC_page_size - 1);
48     if (GC_n_faulted >= NSUMS) ABORT("write fault log overflowed");
49     GC_faulted[GC_n_faulted++] = page;
50 }
51 
GC_was_faulted(struct hblk * h)52 STATIC GC_bool GC_was_faulted(struct hblk *h)
53 {
54     size_t i;
55     word page = (word)h;
56 
57     page += GC_page_size - 1;
58     page &= ~(GC_page_size - 1);
59     for (i = 0; i < GC_n_faulted; ++i) {
60         if (GC_faulted[i] == page) return TRUE;
61     }
62     return FALSE;
63 }
64 
GC_checksum(struct hblk * h)65 STATIC word GC_checksum(struct hblk *h)
66 {
67     word *p = (word *)h;
68     word *lim = (word *)(h+1);
69     word result = 0;
70 
71     while (p < lim) {
72         result += *p++;
73     }
74     return(result | 0x80000000 /* doesn't look like pointer */);
75 }
76 
77 #ifdef STUBBORN_ALLOC
78   /* Check whether a stubborn object from the given block appears on    */
79   /* the appropriate free list.                                         */
GC_on_free_list(struct hblk * h)80   STATIC GC_bool GC_on_free_list(struct hblk *h)
81   {
82     hdr * hhdr = HDR(h);
83     size_t sz = BYTES_TO_WORDS(hhdr -> hb_sz);
84     ptr_t p;
85 
86     if (sz > MAXOBJWORDS) return(FALSE);
87     for (p = GC_sobjfreelist[sz]; p != 0; p = obj_link(p)) {
88         if (HBLKPTR(p) == h) return(TRUE);
89     }
90     return(FALSE);
91   }
92 #endif
93 
94 int GC_n_dirty_errors = 0;
95 int GC_n_faulted_dirty_errors = 0;
96 int GC_n_changed_errors = 0;
97 int GC_n_clean = 0;
98 int GC_n_dirty = 0;
99 
100 GC_INNER GC_bool GC_page_was_ever_dirty(struct hblk * h);
101 
GC_update_check_page(struct hblk * h,int index)102 STATIC void GC_update_check_page(struct hblk *h, int index)
103 {
104     page_entry *pe = GC_sums + index;
105     hdr * hhdr = HDR(h);
106     struct hblk *b;
107 
108     if (pe -> block != 0 && pe -> block != h + OFFSET) ABORT("goofed");
109     pe -> old_sum = pe -> new_sum;
110     pe -> new_sum = GC_checksum(h);
111 #   if !defined(MSWIN32) && !defined(MSWINCE)
112         if (pe -> new_sum != 0x80000000 && !GC_page_was_ever_dirty(h)) {
113             GC_err_printf("GC_page_was_ever_dirty(%p) is wrong\n", h);
114         }
115 #   endif
116     if (GC_page_was_dirty(h)) {
117         GC_n_dirty++;
118     } else {
119         GC_n_clean++;
120     }
121     b = h;
122     while (IS_FORWARDING_ADDR_OR_NIL(hhdr) && hhdr != 0) {
123         b -= (word)hhdr;
124         hhdr = HDR(b);
125     }
126     if (pe -> new_valid
127         && hhdr != 0 && hhdr -> hb_descr != 0 /* may contain pointers */
128         && pe -> old_sum != pe -> new_sum) {
129         if (!GC_page_was_dirty(h) || !GC_page_was_ever_dirty(h)) {
130             GC_bool was_faulted = GC_was_faulted(h);
131             /* Set breakpoint here */GC_n_dirty_errors++;
132             if (was_faulted) GC_n_faulted_dirty_errors++;
133         }
134 #       ifdef STUBBORN_ALLOC
135           if (!HBLK_IS_FREE(hhdr)
136             && hhdr -> hb_obj_kind == STUBBORN
137             && !GC_page_was_changed(h)
138             && !GC_on_free_list(h)) {
139             /* if GC_on_free_list(h) then reclaim may have touched it   */
140             /* without any allocations taking place.                    */
141             /* Set breakpoint here */GC_n_changed_errors++;
142           }
143 #       endif
144     }
145     pe -> new_valid = TRUE;
146     pe -> block = h + OFFSET;
147 }
148 
149 word GC_bytes_in_used_blocks = 0;
150 
151 /*ARGSUSED*/
GC_add_block(struct hblk * h,word dummy)152 STATIC void GC_add_block(struct hblk *h, word dummy)
153 {
154    hdr * hhdr = HDR(h);
155    size_t bytes = hhdr -> hb_sz;
156 
157    bytes += HBLKSIZE-1;
158    bytes &= ~(HBLKSIZE-1);
159    GC_bytes_in_used_blocks += bytes;
160 }
161 
GC_check_blocks(void)162 STATIC void GC_check_blocks(void)
163 {
164     word bytes_in_free_blocks = GC_large_free_bytes;
165 
166     GC_bytes_in_used_blocks = 0;
167     GC_apply_to_all_blocks(GC_add_block, (word)0);
168     if (GC_print_stats)
169       GC_log_printf("GC_bytes_in_used_blocks = %lu,"
170                     " bytes_in_free_blocks = %lu, heapsize = %lu\n",
171                     (unsigned long)GC_bytes_in_used_blocks,
172                     (unsigned long)bytes_in_free_blocks,
173                     (unsigned long)GC_heapsize);
174     if (GC_bytes_in_used_blocks + bytes_in_free_blocks != GC_heapsize) {
175         GC_err_printf("LOST SOME BLOCKS!!\n");
176     }
177 }
178 
179 /* Should be called immediately after GC_read_dirty and GC_read_changed. */
GC_check_dirty(void)180 void GC_check_dirty(void)
181 {
182     int index;
183     unsigned i;
184     struct hblk *h;
185     ptr_t start;
186 
187     GC_check_blocks();
188 
189     GC_n_dirty_errors = 0;
190     GC_n_faulted_dirty_errors = 0;
191     GC_n_changed_errors = 0;
192     GC_n_clean = 0;
193     GC_n_dirty = 0;
194 
195     index = 0;
196     for (i = 0; i < GC_n_heap_sects; i++) {
197         start = GC_heap_sects[i].hs_start;
198         for (h = (struct hblk *)start;
199              h < (struct hblk *)(start + GC_heap_sects[i].hs_bytes);
200              h++) {
201              GC_update_check_page(h, index);
202              index++;
203              if (index >= NSUMS) goto out;
204         }
205     }
206 out:
207     if (GC_print_stats)
208       GC_log_printf("Checked %lu clean and %lu dirty pages\n",
209                     (unsigned long)GC_n_clean, (unsigned long)GC_n_dirty);
210     if (GC_n_dirty_errors > 0) {
211         GC_err_printf("Found %d dirty bit errors (%d were faulted)\n",
212                       GC_n_dirty_errors, GC_n_faulted_dirty_errors);
213     }
214     if (GC_n_changed_errors > 0) {
215         GC_err_printf("Found %lu changed bit errors\n",
216                       (unsigned long)GC_n_changed_errors);
217         GC_err_printf(
218                 "These may be benign (provoked by nonpointer changes)\n");
219 #       ifdef THREADS
220           GC_err_printf(
221             "Also expect 1 per thread currently allocating a stubborn obj\n");
222 #       endif
223     }
224     for (i = 0; i < GC_n_faulted; ++i) {
225         GC_faulted[i] = 0; /* Don't expose block pointers to GC */
226     }
227     GC_n_faulted = 0;
228 }
229 
230 #endif /* CHECKSUMS */
231