Debian Codegraph @

src/ikarus-verify-integrity.c

raw
1 /*
2 * Ikarus Scheme -- A compiler for R6RS Scheme.
3 * Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18
19
20 #include "ikarus-data.h"
21 #include <stdlib.h>
22 #include <assert.h>
23
24 static long int
25 page_idx(void* x){
26 unsigned long int xi = (unsigned long int) x;
27 return xi >> pageshift;
28 }
29
30 #define fixnum_mask 3
31 #define pmask 7
32
33
34 #ifndef NDEBUG
35 static int isa_fixnum(ikptr x){
36 return ((fixnum_mask & (int)x) == 0);
37 }
38
39 static int isa_vector(ikptr x){
40 return ( (tagof(x) == vector_tag) &&
41 isa_fixnum(ref(x, -vector_tag)));
42 }
43 #endif
44
45
46
47
48 static void
49 verify_code(char* x, char* base, unsigned int* svec, unsigned int* dvec){
50 assert(ref(x, 0) == code_tag);
51 ikptr rvec = ref(x, disp_code_reloc_vector);
52 assert(isa_vector(rvec));
53 ikptr codesize = ref(x, disp_code_code_size);
54 codesize += 0;
55 assert(unfix(codesize) >= 0);
56 assert(isa_fixnum(codesize));
57 ikptr freevars = ref(x, disp_code_freevars);
58 freevars += 0;
59 assert(isa_fixnum(freevars));
60 assert(unfix(freevars) >= 0);
61
62 unsigned int rs = svec[page_idx((void*)(long)rvec) - page_idx(base)];
63 unsigned int cs = svec[page_idx(x) - page_idx(base)];
64 int cgen = cs&gen_mask;
65 int rgen = rs&gen_mask;
66 if(rgen < cgen){
67 unsigned int d = dvec[page_idx(x) - page_idx(base)];
68 d = d & d;
69 //int off = (((int)x) - align_to_prev_page(x)) / card_size;
70 //int card_mark = (d >> off) & 0xF;
71 assert(d != 0);
72 }
73 }
74
75 static void
76 verify_object(ikptr x, char* base, unsigned int* svec, unsigned int* dvec){
77 x=x; base=base; svec=svec; dvec=dvec; /* no warning */
78 }
79
80
81 static char*
82 verify_code_small(char* p, int s, unsigned int d,
83 char* base, unsigned int* svec, unsigned int* dvec){
84 char* q = p + pagesize;
85 s=s; d=d; /* no warning */
86 while(p < q){
87 ikptr fst = ref(p, 0);
88 if(fst == code_tag){
89 assert(is_fixnum(ref(p, disp_code_code_size)));
90 int code_size = unfix(ref(p, disp_code_code_size));
91 assert(code_size >= 0);
92 verify_code(p, base, svec, dvec);
93 p+=align(code_size + disp_code_data);
94 } else {
95 p = q;
96 }
97 }
98 if(p != q){
99 fprintf(stderr, "code extended beyond a page in %p, %p\n", p, q);
100 assert(0);
101 }
102 return q;
103 }
104
105 static char*
106 verify_code_large(char* p, unsigned int s, unsigned int d,
107 char* base, unsigned int* svec, unsigned int* dvec){
108 s=s; d=d; /* no warning */
109 ikptr fst = ref(p, 0);
110 fst += 0;
111 assert(fst == code_tag);
112 int code_size = unfix(ref(p, disp_code_code_size));
113 assert(code_size >= 0);
114 verify_code(p, base, svec, dvec);
115 assert(align(code_size+disp_code_data) >= pagesize);
116 char* end = p + code_size + disp_code_data;
117 return((char*)align_to_next_page(end));
118 }
119
120 static char*
121 verify_code_page(char* p, unsigned int s, unsigned int d,
122 char* base, unsigned int* svec, unsigned int* dvec){
123 ikptr fst = ref(p, 0);
124 fst += 0;
125 if(fst != code_tag){
126 fprintf(stderr, "non code object with tag %p found\n",
127 (void*)(long)fst);
128 exit(-1);
129 }
130 int code_size = unfix(ref(p, disp_code_code_size));
131 assert(code_size >= 0);
132 int obj_size = align(code_size + disp_code_data);
133 char* result;
134 if(obj_size <= pagesize){
135 result = verify_code_small(p,s,d,base,svec,dvec);
136 } else {
137 result = verify_code_large(p,s,d,base,svec,dvec);
138 }
139 // fprintf(stderr, "code verify incomplete\n");
140 return result;
141 }
142
143
144
145
146 static char*
147 verify_pointers_page(char* p, unsigned int s, unsigned int d,
148 char* base, unsigned int* svec, unsigned int* dvec){
149 s=s; d=d; /* no warning */
150 {
151 int i = 0;
152 while(i < pagesize){
153 verify_object(ref(p, i), base, svec, dvec);
154 i += wordsize;
155 }
156 }
157 //fprintf(stderr, "pointers verif incomplete\n");
158 return p+pagesize;
159 }
160
161 static char*
162 verify_page(char* p, char* base, unsigned int* svec, unsigned int* dvec){
163 int idx = page_idx(p) - page_idx(base);
164 unsigned int s = svec[idx];
165 unsigned int d = dvec[idx];
166 // if(s & dealloc_mask){
167 // return p+pagesize;
168 // }
169 int type = s & type_mask;
170 if(type == hole_type){
171 return p+pagesize;
172 }
173 assert((s & new_gen_mask) == 0);
174 if(type == code_type){
175 return verify_code_page(p,s,d,base,svec,dvec);
176 }
177 else if(type == pointers_type){
178 return verify_pointers_page(p,s,d,base,svec,dvec);
179 }
180 else if(type == weak_pairs_type){
181 return verify_pointers_page(p,s,d,base,svec,dvec);
182 }
183 else if(type == symbols_type){
184 return verify_pointers_page(p,s,d,base,svec,dvec);
185 }
186 else if(type == dat_type){
187 /* nothing to do for data */
188 return p+pagesize;
189 }
190 else if(type == mainheap_type){
191 /* nothing to do for main heap */
192 return p+pagesize;
193 }
194 else if(type == mainstack_type){
195 /* nothing to do for main stack */
196 return p+pagesize;
197 }
198 fprintf(stderr, "type=0x%08x\n", type);
199 exit(-1);
200 }
201
202 void
203 verify_integrity(ikpcb* pcb, char* where){
204 fprintf(stderr, "verifying in %s...\n", where);
205 char* mem_base = (char*)(long)pcb->memory_base;
206 char* mem_end = (char*)(long)pcb->memory_end;
207 unsigned int* seg_vec = pcb->segment_vector_base;
208 unsigned int* dir_vec = pcb->dirty_vector_base;
209 char* mem = mem_base;
210 while(mem < mem_end){
211 mem = verify_page(mem, mem_base, seg_vec, dir_vec);
212 }
213 fprintf(stderr, "verify_ok in %s\n", where);
214 }