-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
192 lines (150 loc) · 5.87 KB
/
test.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
#include <signal.h>
#include <stdalign.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_ALLOCS 1024
#define MAX_ALLOC_SIZE 65536
#define MAX_ITERATIONS 1000000
#define REALLOC_PERCENTAGE 5
#define MAGIC_BYTE 0xc9
#ifdef __alignof_is_defined
#define EXPECTED_ALIGNMENT _Alignof(max_align_t)
#else
#define EXPECTED_ALIGNMENT 1
#endif
int running = 1;
void signal_handler(int sig)
{
running = 0;
}
size_t check_region(unsigned char *region, size_t size)
{
size_t overwritten_bytes = 0;
for (size_t i = 0; i < size; i++) {
if (region[i] != MAGIC_BYTE) {
overwritten_bytes++;
}
}
return overwritten_bytes;
}
int main()
{
unsigned char *array[MAX_ALLOCS] = { 0 };
int array_sizes[MAX_ALLOCS];
int curr_allocated = 0;
size_t total_allocations = 0;
size_t total_reallocations = 0;
size_t total_frees = 0;
size_t total_overwritten_bytes = 0;
size_t total_overwritten_regions = 0;
size_t reallocation_errors = 0;
size_t alignment_errors = 0;
size_t iterations = 0;
printf("memory allocation tester\n");
srand(time(NULL));
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
clock_t tic = clock();
while (running && iterations < MAX_ITERATIONS) {
int r = rand() % MAX_ALLOCS;
if (rand() % 100 < REALLOC_PERCENTAGE && curr_allocated > 0) {
int i;
do {
i = rand() % MAX_ALLOCS;
} while (array[i] == NULL);
unsigned char *element = array[i];
size_t overwritten_bytes = check_region(element, array_sizes[i]);
total_overwritten_bytes += overwritten_bytes;
if (overwritten_bytes > 0) {
printf("error (iteration %zu): overwritten region found at %p, %zu bytes overwritten\n", iterations, element, overwritten_bytes);
total_overwritten_regions++;
}
size_t new_size = rand() % MAX_ALLOC_SIZE;
unsigned char *new_element = realloc(element, new_size);
if ((uintptr_t) new_element % EXPECTED_ALIGNMENT != 0) {
printf("realloc error: expected alignment %lu, got %d\n", EXPECTED_ALIGNMENT, (1 << (ffsll((uintptr_t) new_element) - 1)));
alignment_errors++;
}
overwritten_bytes = check_region(new_element, array_sizes[i] > new_size ? new_size : array_sizes[i]);
if (overwritten_bytes > 0) {
printf("realloc error: miscopied region found at %p\n", element);
reallocation_errors++;
}
memset(new_element, MAGIC_BYTE, new_size);
array[i] = new_element;
array_sizes[i] = new_size;
total_reallocations++;
} else {
// this randomness tries to keep the amount of currently allocated regions at around MAX_ALLOCS / 2
if (r >= curr_allocated) {
for (int i = 0; i < MAX_ALLOCS; i++) {
if (array[i] == NULL) {
int size = rand() % MAX_ALLOC_SIZE;
array[i] = malloc(size);
array_sizes[i] = size;
if ((uintptr_t) array[i] % EXPECTED_ALIGNMENT != 0) {
printf("malloc error: expected alignment %lu, got %d\n", EXPECTED_ALIGNMENT, (1 << (ffsll((uintptr_t) array[i]) - 1)));
alignment_errors++;
}
// fill the allocated area with MAGIC_BYTE
memset(array[i], MAGIC_BYTE, size);
curr_allocated++;
total_allocations++;
break;
}
}
} else {
int i;
do {
i = rand() % MAX_ALLOCS;
} while (array[i] == NULL);
unsigned char *element = array[i];
size_t overwritten_bytes = check_region(element, array_sizes[i]);
total_overwritten_bytes += overwritten_bytes;
if (overwritten_bytes > 0) {
printf("error (iteration %zu): overwritten region found at %p, %zu bytes overwritten\n", iterations, element, overwritten_bytes);
total_overwritten_regions++;
}
memset(element, 0, array_sizes[i]);
array[i] = NULL;
free(element);
curr_allocated--;
total_frees++;
}
}
iterations++;
}
clock_t toc = clock();
for(int i = 0; i < MAX_ALLOCS; i++) {
if (array[i] != NULL) {
unsigned char *element = array[i];
int overwritten = 0;
for (int j = 0; j < array_sizes[i]; j++) {
if (element[j] != MAGIC_BYTE) {
overwritten = 1;
total_overwritten_bytes++;
}
}
if (overwritten) {
printf("error: overwritten region found at %p\n", element);
total_overwritten_regions++;
}
free(element);
}
}
printf("\nstatistics:\n");
printf("iterations: %zu\n", iterations);
printf("time spent: %.3f s\n", (double) (toc - tic) / CLOCKS_PER_SEC);
printf("allocated at the time of stopping: %d\n", curr_allocated);
printf("total allocations: %zu\n", total_allocations);
printf("total reallocations: %zu\n", total_reallocations);
printf("total frees: %zu\n", total_frees);
printf("total bytes overwritten: %zu\n", total_overwritten_bytes);
printf("total regions overwritten: %zu\n", total_overwritten_regions);
printf("total reallocation errors: %zu\n", reallocation_errors);
printf("total alignment errors: %zu\n", alignment_errors);
}