-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalloc.c
192 lines (166 loc) · 4.63 KB
/
alloc.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
#define _GNU_SOURCE /* mremap */
#include <sys/mman.h>
#include <linux/mman.h> /* MAP_UNINITIALIZED */
#include <stdbool.h>
#include "alloc.h"
#define _dtor0_ __attribute__((destructor (110)))
#ifndef NDEBUG
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define LOG(...) printf(__VA_ARGS__)
#define WARN(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define LOG(...)
#define WARN(...)
#endif
typedef struct my_header {
size_t size;
bool used;
struct my_header *next;
} header_t;
static _dtor0_ void destroy(void);
static inline void *get_data(void *mem);
static inline header_t *get_header(void *mem);
static inline size_t get_real_size(int size);
#ifdef SAFE_ALLOC
static inline void enable_prot(header_t *h, size_t size, int flags);
#else
#define enable_prot(...)
#endif
static inline void *alloc(const size_t size, int flags);
static header_t *head = NULL; // our list of memory chunks
/** Private Interface **/
static _dtor0_ void destroy(void) {
while (head) {
header_t *tmp = head;
head = head->next;
/* Only unmap freed blocks */
if (!tmp->used) {
if (munmap(tmp, tmp->size) != 0) {
WARN("%s\n", strerror(errno));
}
}
}
}
static inline void *get_data(void *mem) {
return mem + sizeof(header_t);
}
static inline header_t *get_header(void *mem) {
return mem - sizeof(header_t);
}
static inline size_t get_real_size(int size) {
return sizeof(header_t) + size;
}
#ifdef SAFE_ALLOC
static inline void enable_prot(header_t *h, size_t size, int flags) {
if (h && !h->used) {
if (mprotect(h, size == 0 ? h->size : size, flags) != 0) {
WARN("%s\n", strerror(errno));
}
}
}
#endif
static inline void *alloc(const size_t size, int flags) {
if (size - sizeof(header_t) == 0) {
return NULL;
}
header_t **tmp = &head;
header_t *prev = NULL;
while (*tmp) {
if (!(*tmp)->used && (*tmp)->size >= size) {
LOG("Suitable unused memory found: %p with size: %lu\n", (*tmp), (*tmp)->size);
/* Re-enable read and write on memory block */
enable_prot(*tmp, size, PROT_READ | PROT_WRITE);
/*
* Avoid calling mprotect(PROT_READ) before returning
* as we haven't touched prev->next ptr
*/
prev = NULL;
goto found;
}
prev = *tmp;
tmp = &(*tmp)->next;
}
/* We are modifying previous node -> next pointer! */
enable_prot(prev, 0, PROT_READ | PROT_WRITE);
void *ret = NULL;
*tmp = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | flags, -1, 0);
if (*tmp != MAP_FAILED) {
LOG("Malloc'd %p with size: %lu\n", *tmp, size);
(*tmp)->size = size;
(*tmp)->next = NULL;
found:
(*tmp)->used = true;
ret = get_data(*tmp);
} else {
WARN("%s\n", strerror(errno));
}
/* We have modified previous node -> next pointer. We can protect it again now. */
enable_prot(prev, 0, PROT_READ);
return ret;
}
/** Public Interface **/
void *malloc_f(size_t size) {
/* Avoid zeroing mapping, faster */
return alloc(get_real_size(size), MAP_UNINITIALIZED);
}
void *calloc_f(int num, size_t size) {
/* Require zeroing mapping, safer */
return alloc(get_real_size(num * size), 0);
}
void free_f(void *ptr) {
if (ptr) {
header_t *priv = get_header(ptr);
priv->used = false;
/* Make this block's data protected (read-only) */
enable_prot(priv, 0, PROT_READ);
}
}
/*
* Algorithm:
* * Store current node in priv ptr
* * Call mremap
* * If it fails, return NULL
* * If it succeeds, store newly allocated mapping in place of old node.
*
* Note: as we're modifying prev->next pointer when storing newly allocated mapping,
* we should enable PROT_WRITE perm on prev mapping if needed.
*
* This may seem overcomplicated; remember we cannot access old mapping after it is remapped.
*/
void *realloc_f(void *ptr, size_t size) {
/* Mimic malloc if NULL ptr as param */
if (!ptr) {
return malloc_f(size);
}
/* Mimic free if 0 size is requested */
if (size == 0) {
free_f(ptr);
return NULL;
}
header_t *priv = get_header(ptr);
const size_t new_size = get_real_size(size);
/* Find current node and store pointer to prev node */
header_t **tmp = &head;
header_t *prev = NULL;
while (*tmp != priv) {
prev = *tmp;
tmp = &(*tmp)->next;
}
LOG("Realloc'ng %p with size %lu.\n", priv, new_size);
void *temp = mremap(priv, priv->size, new_size, MREMAP_MAYMOVE);
if (temp != MAP_FAILED) {
if (temp != priv) {
/* We are modifying previous node -> next pointer! Make it writable */
enable_prot(prev, 0, PROT_READ | PROT_WRITE);
*tmp = temp;
/* We have modified previous node -> next pointer. We can protect it again now. */
enable_prot(prev, 0, PROT_READ);
}
(*tmp)->size = new_size;
return get_data(*tmp);
}
WARN("%s\n", strerror(errno));
return NULL;
}