-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalloc-testing.c
261 lines (175 loc) · 5.36 KB
/
alloc-testing.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
/*
Copyright (c) 2005-2008, Simon Howard
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define ALLOC_TESTING_C
#include "alloc-testing.h"
/* All allocated blocks are given this magic number */
#define ALLOC_TEST_MAGIC 0x72ec82d2
/* This value is written to memory after it is freshly allocated, to ensure
* that code under test does not rely on memory being initialised by
* malloc(). */
#define MALLOC_PATTERN 0xBAADF00D
/* This value is written to memory after it is freed, to ensure that code
* does not rely on memory that has been freed. */
#define FREE_PATTERN 0xDEADBEEF
/**
* All blocks allocated by the testing framework are preceded by a structure
* of this type.
*/
typedef struct _BlockHeader BlockHeader;
struct _BlockHeader {
unsigned int magic_number;
size_t bytes;
};
/* Count of the current number of allocated bytes. */
static size_t allocated_bytes = 0;
/* Limit on number of allocations that are possible. Each time an allocation
* is made, this is decremented. When it reaches zero, no more allocations
* are allowed. If this has a negative value, the limit is disabled. */
signed int allocation_limit = -1;
/* Get the block header for an allocated pointer. */
static BlockHeader *alloc_test_get_header(void *ptr)
{
BlockHeader *result;
/* Go back from the start of the memory block to get the header. */
result = ((BlockHeader *) ptr) - 1;
assert(result->magic_number == ALLOC_TEST_MAGIC);
return result;
}
/* Overwrite a block of memory with a repeated pattern. */
static void alloc_test_overwrite(void *ptr, size_t length,
unsigned int pattern)
{
unsigned char *byte_ptr;
int pattern_seq;
unsigned char b;
size_t i;
byte_ptr = (unsigned char *) ptr;
for (i=0; i<length; ++i) {
pattern_seq = (int) (i & 3);
b = (unsigned char) ((pattern >> (8 * pattern_seq)) & 0xff);
byte_ptr[i] = b;
}
}
/* Base malloc function used by other functions. */
void *alloc_test_malloc(size_t bytes)
{
BlockHeader *header;
void *ptr;
/* Check if we have reached the allocation limit. */
if (allocation_limit == 0) {
return NULL;
}
/* Allocate the requested block with enough room for the block header
* as well. */
header = (BlockHeader *) malloc(sizeof(BlockHeader) + bytes);
if (header == NULL) {
return NULL;
}
header->magic_number = ALLOC_TEST_MAGIC;
header->bytes = bytes;
/* Fill memory with MALLOC_PATTERN, to ensure that code under test
* does not rely on memory being initialised to zero. */
ptr = header + 1;
alloc_test_overwrite(ptr, bytes, MALLOC_PATTERN);
/* Update counter */
allocated_bytes += bytes;
/* Decrease the allocation limit */
if (allocation_limit > 0) {
--allocation_limit;
}
/* Skip past the header and return the block itself */
return header + 1;
}
/* Base free function */
void alloc_test_free(void *ptr)
{
BlockHeader *header;
size_t block_size;
/* Must accept NULL as a valid pointer to free. */
if (ptr == NULL) {
return;
}
/* Get the block header and do a sanity check */
header = alloc_test_get_header(ptr);
block_size = header->bytes;
assert(allocated_bytes >= block_size);
/* Trash the allocated block to foil any code that relies on memory
* that has been freed. */
alloc_test_overwrite(ptr, header->bytes, FREE_PATTERN);
/* Trash the magic number in the block header to stop the same block
* from being freed again. */
header->magic_number = 0;
/* Free the allocated memory. */
free(header);
/* Update counter */
allocated_bytes -= block_size;
}
void *alloc_test_realloc(void *ptr, size_t bytes)
{
BlockHeader *header;
void *new_ptr;
size_t bytes_to_copy;
/* Allocate the new block */
new_ptr = alloc_test_malloc(bytes);
if (new_ptr == NULL) {
return NULL;
}
/* Copy over the old data and free the old block, if there was any. */
if (ptr != NULL) {
header = alloc_test_get_header(ptr);
bytes_to_copy = header->bytes;
if (bytes_to_copy > bytes) {
bytes_to_copy = bytes;
}
memcpy(new_ptr, ptr, bytes_to_copy);
alloc_test_free(ptr);
}
return new_ptr;
}
void *alloc_test_calloc(size_t nmemb, size_t bytes)
{
void *result;
size_t total_bytes = nmemb * bytes;
/* Allocate the block. */
result = alloc_test_malloc(total_bytes);
if (result == NULL) {
return NULL;
}
/* Initialise to zero. */
memset(result, 0, total_bytes);
return result;
}
char *alloc_test_strdup(const char *string)
{
char *result;
result = (char *) alloc_test_malloc(strlen(string) + 1);
if (result == NULL) {
return NULL;
}
strcpy(result, string);
return result;
}
void alloc_test_set_limit(signed int alloc_count)
{
allocation_limit = alloc_count;
}
size_t alloc_test_get_allocated(void)
{
return allocated_bytes;
}