Remove a note in README.macros that work in progress
[platform/upstream/libgc.git] / checksums.c
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 #define NSUMS 10000
21
22 #define OFFSET 0x10000
23
24 typedef struct {
25         GC_bool new_valid;
26         word old_sum;
27         word new_sum;
28         struct hblk * block;    /* Block to which this refers + OFFSET  */
29                                 /* to hide it from collector.           */
30 } page_entry;
31
32 page_entry GC_sums[NSUMS];
33
34 STATIC word GC_faulted[NSUMS] = { 0 };
35                 /* Record of pages on which we saw a write fault.       */
36
37 STATIC size_t GC_n_faulted = 0;
38
39 #if defined(MPROTECT_VDB) && !defined(DARWIN)
40   void GC_record_fault(struct hblk * h)
41   {
42     word page = (word)h & ~(GC_page_size - 1);
43
44     GC_ASSERT(GC_page_size != 0);
45     if (GC_n_faulted >= NSUMS) ABORT("write fault log overflowed");
46     GC_faulted[GC_n_faulted++] = page;
47   }
48 #endif
49
50 STATIC GC_bool GC_was_faulted(struct hblk *h)
51 {
52     size_t i;
53     word page = (word)h & ~(GC_page_size - 1);
54
55     for (i = 0; i < GC_n_faulted; ++i) {
56         if (GC_faulted[i] == page) return TRUE;
57     }
58     return FALSE;
59 }
60
61 STATIC word GC_checksum(struct hblk *h)
62 {
63     word *p = (word *)h;
64     word *lim = (word *)(h+1);
65     word result = 0;
66
67     while ((word)p < (word)lim) {
68         result += *p++;
69     }
70     return(result | 0x80000000 /* doesn't look like pointer */);
71 }
72
73 int GC_n_dirty_errors = 0;
74 int GC_n_faulted_dirty_errors = 0;
75 unsigned long GC_n_clean = 0;
76 unsigned long GC_n_dirty = 0;
77
78 STATIC void GC_update_check_page(struct hblk *h, int index)
79 {
80     page_entry *pe = GC_sums + index;
81     hdr * hhdr = HDR(h);
82     struct hblk *b;
83
84     if (pe -> block != 0 && pe -> block != h + OFFSET) ABORT("goofed");
85     pe -> old_sum = pe -> new_sum;
86     pe -> new_sum = GC_checksum(h);
87 #   if !defined(MSWIN32) && !defined(MSWINCE)
88         if (pe -> new_sum != 0x80000000 && !GC_page_was_ever_dirty(h)) {
89             GC_err_printf("GC_page_was_ever_dirty(%p) is wrong\n", (void *)h);
90         }
91 #   endif
92     if (GC_page_was_dirty(h)) {
93         GC_n_dirty++;
94     } else {
95         GC_n_clean++;
96     }
97     b = h;
98     while (IS_FORWARDING_ADDR_OR_NIL(hhdr) && hhdr != 0) {
99         b -= (word)hhdr;
100         hhdr = HDR(b);
101     }
102     if (pe -> new_valid
103         && hhdr != 0 && hhdr -> hb_descr != 0 /* may contain pointers */
104         && pe -> old_sum != pe -> new_sum) {
105         if (!GC_page_was_dirty(h) || !GC_page_was_ever_dirty(h)) {
106             GC_bool was_faulted = GC_was_faulted(h);
107             /* Set breakpoint here */GC_n_dirty_errors++;
108             if (was_faulted) GC_n_faulted_dirty_errors++;
109         }
110     }
111     pe -> new_valid = TRUE;
112     pe -> block = h + OFFSET;
113 }
114
115 word GC_bytes_in_used_blocks = 0;
116
117 STATIC void GC_add_block(struct hblk *h, word dummy GC_ATTR_UNUSED)
118 {
119    hdr * hhdr = HDR(h);
120
121    GC_bytes_in_used_blocks += (hhdr->hb_sz + HBLKSIZE-1) & ~(HBLKSIZE-1);
122 }
123
124 STATIC void GC_check_blocks(void)
125 {
126     word bytes_in_free_blocks = GC_large_free_bytes;
127
128     GC_bytes_in_used_blocks = 0;
129     GC_apply_to_all_blocks(GC_add_block, (word)0);
130     GC_COND_LOG_PRINTF("GC_bytes_in_used_blocks = %lu,"
131                        " bytes_in_free_blocks = %lu, heapsize = %lu\n",
132                        (unsigned long)GC_bytes_in_used_blocks,
133                        (unsigned long)bytes_in_free_blocks,
134                        (unsigned long)GC_heapsize);
135     if (GC_bytes_in_used_blocks + bytes_in_free_blocks != GC_heapsize) {
136         GC_err_printf("LOST SOME BLOCKS!!\n");
137     }
138 }
139
140 /* Should be called immediately after GC_read_dirty.    */
141 void GC_check_dirty(void)
142 {
143     int index;
144     unsigned i;
145     struct hblk *h;
146     ptr_t start;
147
148     GC_check_blocks();
149
150     GC_n_dirty_errors = 0;
151     GC_n_faulted_dirty_errors = 0;
152     GC_n_clean = 0;
153     GC_n_dirty = 0;
154
155     index = 0;
156     for (i = 0; i < GC_n_heap_sects; i++) {
157         start = GC_heap_sects[i].hs_start;
158         for (h = (struct hblk *)start;
159              (word)h < (word)(start + GC_heap_sects[i].hs_bytes); h++) {
160              GC_update_check_page(h, index);
161              index++;
162              if (index >= NSUMS) goto out;
163         }
164     }
165 out:
166     GC_COND_LOG_PRINTF("Checked %lu clean and %lu dirty pages\n",
167                        GC_n_clean, GC_n_dirty);
168     if (GC_n_dirty_errors > 0) {
169         GC_err_printf("Found %d dirty bit errors (%d were faulted)\n",
170                       GC_n_dirty_errors, GC_n_faulted_dirty_errors);
171     }
172     for (i = 0; i < GC_n_faulted; ++i) {
173         GC_faulted[i] = 0; /* Don't expose block pointers to GC */
174     }
175     GC_n_faulted = 0;
176 }
177
178 #endif /* CHECKSUMS */