/
malloc537.c
230 lines (198 loc) · 5.02 KB
/
malloc537.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
/*
*537malloc.c *Written by Nik Ingrassia (ingrassi) and Blake Martin (blakem)
*
*Adds memory checking bits and pieces to standard malloc/realloc/free,
*and defines a new function 537memcheck to make sure a pointer
*has been properly allocated before use!
*/
#include <stdio.h>
#include <stdlib.h>
#include "malloc537.h"
#include "rbtree.h"
/*
* Allocates memory using malloc, and stores a tuple of address and length
* in a hash table.
* Checks for zero size!
*/
/*
* TODO: make sure we remove nodes when we do an insert.
* When we insert a node that fully covers a freed node,
* remove the freed node.
*
* NOTE: our contained_lookup function, which should return a
* freed node that is covered by what we just allocated, doesn't
* seem to work. So we never delete any nodes from the tree.
* Not sure if that's due to the behavior of 537ps, or
* our code.
*/
extern node * root;
/*
* malloc537 is a wrapper around malloc.
* It add the tuple (base, bounds) to a range
* tree to track memory allocation!
*/
void *malloc537(size_t size)
{
void * return_ptr;
node * remove_node = NULL;
if(size == 0)
{
printf("Allocating a pointer of size 0\n");
}
return_ptr = malloc(size);
/*printf("Inserting node: Pointer: %p, bounds %d\n", return_ptr, (int)size);*/
/*
* Need to find all nodes within range base+1 to size, and delete them.
*/
remove_node = contained_lookup(return_ptr, size);
while(remove_node != NULL)
{
delete_node(remove_node->base);
remove_node = contained_lookup(return_ptr, size);
}
/*HERE WE DO AN INSERT!*/
insert(return_ptr, size);
/*Debug! print the tree*/
/*
print(root, 0);
printf("\n");
*/
return return_ptr;
}
/*
* Checks to make sure a 537malloc-family command allocated the memory
* in ptr, and then calls free on the pointer. If an error is detected,
* prints a descriptive message and closes with EXIT_FAILURE.
*/
void free537(void *ptr)
{
node * temp;
temp = lookup(ptr);
/* check for a null pointer to free */
if(ptr == NULL)
{
printf("Trying to free a null pointer!\n");
exit(EXIT_FAILURE);
}
/*
* Check to make sure we have a node at that pointer!
*/
if(temp == NULL)
{
temp = bounds_lookup(ptr);
if(temp == NULL)
{
printf("Pointer at %p was never allocated!", ptr);
exit(EXIT_FAILURE);
}
else
{
printf("Attempting to free pointer at %p, but that pointer is in memory allocated starting at %p with bounds %d.\n", ptr, temp->base, (int)temp->bounds);
exit(EXIT_FAILURE);
}
}
/*
* If we've already freed this node, say that there's a double free! woo.
*/
if(temp->free == 1)
{
printf("Pointer at %p of previous size %i was already freed!\n", ptr, (int)temp->bounds);
exit(EXIT_FAILURE);
}
temp->free = 1;
free(ptr);
/*
print(root, 0);
printf("\n");
*/
}
/*
* Functions similarly to realloc, but does checking on the input
* pointer, and stores output pointer data in our hash table
*/
void *realloc537(void *ptr, size_t size)
{
void * return_pointer;
node * remove_node = NULL;
/* If the pointer is null, this is just a malloc! let malloc537 handle it.*/
if(ptr == NULL)
{
return malloc537(size);
}
/* If the size is null, it's just a free.*/
else if(size == 0)
{
free537(ptr);
return NULL;
}
else
{
/*HERE WE DO A REMOVE/mark as unused/whatever*/
node * temp;
temp = lookup(ptr);
temp->free = 1;
}
return_pointer = realloc(ptr, size);
/* Before we insert, remove any nodes that will be overlapped.*/
/*
* Need to find all nodes within range base+1 to size, and delete them.
*/
remove_node = contained_lookup(return_pointer, size);
while(remove_node != NULL)
{
delete_node(remove_node->base);
remove_node = contained_lookup(return_pointer, size);
}
insert(ptr, size);
/*
print(root, 0);
printf("\n");
*/
return return_pointer;
}
/*
* Checks pointer ptr with address range size to see
* if it has been allocated (and not freed) by 537malloc/537realloc.
* Prints a nice, verbose error message and quits on an error.
*/
void memcheck537(void *ptr, size_t size)
{
/*
*Find the node!
*If it doesn't exist, see if it's in the previous
*node's range and print appropriate error
*Otherwise, check the size.
*If they don't match, print an error.
*/
node * temp = lookup(ptr);
if(temp == NULL)
{
temp = bounds_lookup(ptr);
if(temp == NULL)
{
printf("Pointer at %p was never allocated!\n", ptr);
exit(EXIT_FAILURE);
}
else
{
/* As long as the pointer we're looking up is within the bounds
* of the allocated space, we're fine!
*/
if((long)((long)ptr + size) <= (long)((long)temp->base + temp->bounds))
{
return;
}
else
{
printf("Pointer at %p is inside pointer %p of size %d, but there's not enough room in the allocated space!\n", ptr, temp->base, (int)temp->bounds);
exit(EXIT_FAILURE);
}
}
}
/* If we find a pointer at ptr, check the size! */
else if(size > temp->bounds)
{
printf("Trying to use %d bytes, but the pointer %p only has a size of %d bytes.\n", (int)size, ptr, (int)temp->bounds);
exit(EXIT_FAILURE);
}
}