-
Notifications
You must be signed in to change notification settings - Fork 0
/
test1.c
284 lines (250 loc) · 5.9 KB
/
test1.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
278
279
280
281
282
283
284
/* Malloc unit test
*
* Don Porter - COMP 530 - UNC Chapel Hill
*
* This utility allocates and frees memory in different patterns, keeping
* track of the requested sizes, overwriting with junk, and looking for errors
* or inconsistencies.
*/
#include <assert.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FREE_POISON 0xab
#define ALLOC_POISON 0xcd
// We keep track of allocated memory in a structure on the data segment,
// so we don't need to malloc our bookeeping too
struct thingy {
char *memory;
size_t size;
char pattern;
bool valid;
};
#define MAX_THINGIES 256
static struct thingy thingies[MAX_THINGIES];
/* Check that all thingies have a correct value */
static bool values_ok (void) {
int i, j;
for (i = 0; i < MAX_THINGIES; i++) {
if (thingies[i].valid) {
for (j = 0; j < thingies[i].size; j++) {
if (thingies[i].memory[j] != thingies[i].pattern)
return false;
}
}
}
return true;
}
/* Allocate a thingy */
static void gimme (size_t size, bool check_poison) {
void *x = malloc(size);
int i = 0;
bool found = false;
if (check_poison) {
for (i = 0; i < size; i++) {
char *y = (char *) x;
assert (y[i] == (char) ALLOC_POISON);
}
}
for (i = 0; i < MAX_THINGIES; i++) {
if (!thingies[i].valid) {
if (!found) {
found = true;
// Add an entry to track this allocation
thingies[i].memory = x;
thingies[i].size = size;
thingies[i].pattern = (char) i;
// Fill this element with junk
memset(x, (char) i, size);
thingies[i].valid = true;
}
} else {
// Assert that we are not allocating twice
assert(thingies[i].memory != x);
// Check that the expected pattern is preserved
int j;
for (j = 0; j < thingies[i].size; j++) {
assert(thingies[i].memory[j] == thingies[i].pattern);
}
}
}
// Should always find a slot
assert(found);
}
/* Free a thingy, and clear bookkeeping */
static void heego (void *x) {
int i = 0;
bool found = false;
for (i = 0; i < MAX_THINGIES; i++) {
if (thingies[i].valid) {
// Check that the expected pattern is preserved
int j;
for (j = 0; j < thingies[i].size; j++) {
assert(thingies[i].memory[j] == thingies[i].pattern);
}
if (thingies[i].memory == x) {
assert (!found);
found = true;
thingies[i].valid = false;
free(x);
}
}
}
// Should always find the thing we free
assert(found);
}
// Test 1: I'll take 32 of everything!
static void test1 (bool test_poison) {
int i, j;
for (i = 0; i < 32; i++) {
for (j = 0; j < 7; j++) {
gimme(1 << (j + 5), test_poison);
}
}
assert(values_ok());
}
// Test 2: freed memory gets recycled
static void test2 (bool test_poison) {
int i, j;
for (i = 0; i < 32; i++) {
for (j = 0; j < 7; j++) {
gimme(1 << (j + 5), test_poison);
}
}
assert(values_ok());
// Free everything
for (i = 0; i < 224; i++) {
assert(thingies[i].valid);
heego(thingies[i].memory);
}
}
// Test 1c: Allocate, free every other object, re-allocate - look for corruptions
static void test3 (bool test_poison) {
int i, j;
for (i = 0; i < 32; i++) {
for (j = 0; j < 7; j++) {
gimme(1 << (j + 5), test_poison);
}
}
assert(values_ok());
// Release every other object
for (i = 0; i < 224; i++) {
assert(thingies[i].valid);
if (i % 2) {
heego(thingies[i].memory);
// If test_poison and object is smaller than 2048, see if it is poisoned correctly.
// We should be able to do this safely, since one object will hold the superblock
if (test_poison && thingies[i].size < 2048) {
// Skip first 8 bytes, for next pointer
for (j = 8; j < thingies[i].size; j++) {
char *y = (char *) thingies[i].memory;
assert (y[j] == (char) FREE_POISON);
}
}
}
}
assert(values_ok());
// Reallocate every other object
for (i = 0; i < 224; i++) {
if (i % 2) {
assert(!thingies[i].valid);
gimme(i, test_poison);
}
}
assert(values_ok());
// Free everything
for (i = 0; i < 224; i++) {
assert(thingies[i].valid);
heego(thingies[i].memory);
}
}
// Test 1e: Check that freeing enough superblocks
// actually releases one to the OS
// Register a signal handler, and catch/handle the fault
static bool testing_free = false;
void handle_sigsegv(int sig) {
if (testing_free) {
printf("Test %d completed ok\n\n", 7);
exit(0);
} else {
printf("Got an unexpected signal. Uh oh\n");
exit(-1);
}
}
static void test4 (void) {
int i;
for (i = 0; i < 3; i++) {
gimme(2048, true);
}
// Register the signal handler
__sighandler_t rv = signal(SIGSEGV, handle_sigsegv);
assert(rv != SIG_ERR);
// Free the three objects
for (i = 0; i < 3; i++) {
heego(thingies[i].memory);
}
testing_free = true;
// Try touching the three objects
for (i = 0; i < 3; i++) {
char *y = (char *)thingies[i].memory;
y[8] = '\0';
}
// Should not get here
assert(0);
}
// Self test for starter code
static void test5 (void) {
// Allocate a few big thingies, and some small ones
void *x = malloc(8192);
void *z = malloc(32);
void *y = malloc(4096);
// Free it
free(x);
free(z);
free(y);
}
int main(int argc, char **argv) {
int i = 0;
if (argc < 2) {
printf("Must give at least 1 argument to select test.\n");
return -1;
}
// Initialize the array to invalid
for (i = 0; i < MAX_THINGIES; i++) {
thingies[i].valid = false;
}
int test = atoi(argv[1]);
switch (test) {
case 1:
test1(false);
break;
case 2:
test2(false);
break;
case 3:
test3(false);
break;
case 4:
test1(true);
break;
case 5:
test2(true);
break;
case 6:
test3(true);
break;
case 7:
test4();
break;
case 8:
test5();
break;
default:
printf("Unknown test\n");
return -1;
}
printf("Test %d completed ok\n\n", test);
return 0;
}