-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker2.c~
277 lines (249 loc) · 8.96 KB
/
checker2.c~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*--------------------------------------------------------------------*/
/* checker2.c */
/* Author: Nathaniel M. Wilson */
/* Author: Narek Galstyan */
/*--------------------------------------------------------------------*/
#include "checker2.h"
#include <stdio.h>
#include <assert.h>
/*In lieu of a boolean data type. */
enum {FALSE, TRUE};
int Checker_isValid(Chunk_T oHeapStart, Chunk_T oHeapEnd,
Chunk_T aoBins[], int iBinCount)
{
Chunk_T oChunk; /* current chunk in traversals */
Chunk_T oPrevChunk; /* previous chunk in traversals */
Chunk_T oNextChunk; /* next chunk in tranversals*/
Chunk_T oTortoiseChunk; /* used in cycle detection */
Chunk_T oHareChunk; /* used in cycel detection */
Chunk_T oFreeListEnd;/* will point to the last chunk in LinkedList */
int iIndex;
/* check for an initialized heap */
if (oHeapStart == NULL)
{
fprintf(stderr, "The heap start is uninitialized\n");
return FALSE;
}
if (oHeapEnd == NULL)
{
fprintf(stderr, "The heap end is uninitialized\n");
return FALSE;
}
/* If the heap is empty, are all free lists empty too? */
if (oHeapStart == oHeapEnd)
{
for (iIndex = 0; iIndex < iBinCount; iIndex++)
{
if (aoBins[iIndex] != NULL)
{
fprintf(stderr,
"The heap is empty, but list %d is not.\n", iIndex);
return FALSE;
}
}
}
/* Traverse memory through forward links. */
for (oChunk = oHeapStart;
oChunk != NULL;
oChunk = Chunk_getNextInMem(oChunk, oHeapEnd))
{
/* Is the chunk valid? */
if (! Chunk_isValid(oChunk, oHeapStart, oHeapEnd))
{
fprintf(stderr, "Traversing memory detected a bad chunk\n");
return FALSE;
}
}
/* Traverse memory through backward links. */
for (oChunk = Chunk_getPrevInMem(oHeapEnd, oHeapStart);
oChunk != NULL;
oChunk = Chunk_getPrevInMem(oChunk, oHeapStart))
{
/* Is the chunk valid? */
if (! Chunk_isValid(oChunk, oHeapStart, oHeapEnd))
{
fprintf(stderr, "Backward traversing memory"
" detected a bad chunk\n");
return FALSE;
}
}
for (iIndex = 0; iIndex < iBinCount; iIndex++)
{
/* Is the list devoid of forward cycles? Use Floyd's algorithm to
find out */
oFreeListEnd = NULL;
oTortoiseChunk = aoBins[iIndex];
oHareChunk = aoBins[iIndex];
if (oHareChunk != NULL) {
oFreeListEnd = oHareChunk;
oHareChunk = Chunk_getNextInList(oHareChunk);
}
while (oHareChunk != NULL)
{
/* by the end of the loop oFreeListEnd stores
* the end of the loop so the backward cycles could be found
*/
oFreeListEnd = oHareChunk;
if (oTortoiseChunk == oHareChunk)
{
fprintf(stderr, "The list has a forward cycle\n");
return FALSE;
}
/* do List links point to meaningful positions?*/
if(!Chunk_isValid(oHareChunk, oHeapStart, oHeapEnd))
{
fprintf(stderr, "Forward link of some element in free"
" list is corrupted\n");
return FALSE;
}
/* Move oTortoiseChunk one step. */
oTortoiseChunk = Chunk_getNextInList(oTortoiseChunk);
/* Move oHareChunk two steps, if possible. */
oHareChunk = Chunk_getNextInList(oHareChunk);
if (oHareChunk != NULL) {
/* do List links point to meaningful positions?*/
if(!Chunk_isValid(oHareChunk, oHeapStart, oHeapEnd))
{
fprintf(stderr, "Forward link of some element in free"
" list is corrupted\n");
return FALSE;
}
oFreeListEnd = oHareChunk;
/* because hare jumps in steps of two */
oHareChunk = Chunk_getNextInList(oHareChunk);
}
}
/* Is the list devoid of backward cycles? Use Floyd's algorithm to
find out.*/
oTortoiseChunk = oFreeListEnd;
oHareChunk = oFreeListEnd;
/* if there is a free list and the length is more than one */
if (oHareChunk != NULL)
{
/* do List links point to meaningful positions?*/
if(!Chunk_isValid(oHareChunk, oHeapStart, oHeapEnd))
{
fprintf(stderr, "Backward link of the last element in the free"
" list is corrupted\n");
return FALSE;
}
oHareChunk = Chunk_getPrevInList(oHareChunk);
}
/* There is no NULL terminator in the begining of the list so
* the start of the free list will serve as a terminator */
while (oHareChunk != NULL)
{
if (oTortoiseChunk == oHareChunk)
{
fprintf(stderr, "The list has a backward cycle\n");
return FALSE;
}
/* Move oTortoiseChunk one step. */
oTortoiseChunk = Chunk_getPrevInList(oTortoiseChunk);
/* Move oHareChunk two steps, if possible. */
/* do List links point to meaningful positions?*/
if(!Chunk_isValid(oHareChunk, oHeapStart, oHeapEnd))
{
fprintf(stderr, "Backward link of some element in free"
" list is corrupted\n");
return FALSE;
}
oHareChunk = Chunk_getPrevInList(oHareChunk);
if (oHareChunk != NULL) {
/* do List links point to meaningful positions?*/
if(!Chunk_isValid(oHareChunk, oHeapStart, oHeapEnd))
{
fprintf(stderr, "Backward link of some element in free"
" list is corrupted\n");
return FALSE;
}
/* run hare run*/
oHareChunk = Chunk_getPrevInList(oHareChunk);
}
}
/* Traverse the free list. */
oPrevChunk = NULL;
oNextChunk = NULL;
for (oChunk = aoBins[iIndex];
oChunk != NULL;
oChunk = Chunk_getNextInList(oChunk))
{
/* NOTE: these are prev and next IN MEMORY!!*/
/* rely on boundary chaking of get***InMem() */
oPrevChunk = Chunk_getPrevInMem(oChunk, oHeapStart);
oNextChunk = Chunk_getNextInMem(oChunk, oHeapEnd);
/* Is the chunk valid? */
if (! Chunk_isValid(oChunk, oHeapStart, oHeapEnd))
{
fprintf(stderr, "Traversing the list detected a bad chunk\n");
return FALSE;
}
/*ensure status bit set correctly*/
if (Chunk_getStatus(oChunk) == CHUNK_INUSE)
{
fprintf(stderr, "chunk in free list marked as in use.");
}
if ((oPrevChunk != NULL) &&
(Chunk_getStatus(oPrevChunk) == CHUNK_FREE))
{
fprintf(stderr, "The heap contains contiguous free chunks"
" just after a memory in free list\n");
return FALSE;
}
/* Is the next chunk in memory in use? */
if ((oNextChunk != NULL) &&
(Chunk_getStatus(oNextChunk) == CHUNK_FREE))
{
fprintf(stderr, "The heap contains contiguous free chunks"
" just before a memory in free list\n");
return FALSE;
}
}
/*Is the Current node in the linked list the next node of
* the previous one and the previous of the next one?*/
oPrevChunk = NULL;
oNextChunk = NULL;
for (oChunk = aoBins[iIndex];
oChunk != NULL;
oChunk = Chunk_getNextInList(oChunk))
{
if(oChunk != aoBins[iIndex]) /* if not in the first iteration*/
oPrevChunk = Chunk_getPrevInList(oChunk);
oNextChunk = Chunk_getNextInList(oChunk);
/* The next of the previous */
if(oPrevChunk != NULL &&
Chunk_getNextInList(oPrevChunk) != oChunk)
{
fprintf(stderr, "Next of the perivous is not the current in"
" the Linked List\n");
return FALSE;
}
/* The previous of the next */
if(oNextChunk != NULL &&
Chunk_getPrevInList(oNextChunk) != oChunk)
{
fprintf(stderr, "Previous of the Next is not the current in"
" the Linked List\n");
return FALSE;
}
}
}
for (iIndex = 0; iIndex < iBinCount; iIndex++)
{
/* make sure that each item in each list has the right size,
skipping the last list */
if (iIndex == 1023) continue;
for (oChunk = aoBins[iIndex];
oChunk != NULL;
oChunk = Chunk_getNextInList(oChunk))
{
if ((int) Chunk_getUnits(oChunk) != iIndex)
{
fprintf(stderr, "chunk with %d units in bin %d",
(int) Chunk_getUnits(oChunk), iIndex);
return FALSE;
}
}
}
return TRUE;
}