-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalloc.c
292 lines (231 loc) · 6.81 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
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
285
286
287
288
289
290
291
292
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <execinfo.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <checksum.h>
#define COUNT_OF(x) sizeof(x)/sizeof((x)[0])
static size_t page_size;
static const char *log_dir, *log_backtrace;
struct mgmt_t {
char *base;
size_t len;
uint32_t crc;
};
enum { mgmt_size = sizeof(struct mgmt_t) };
static void
Warnx(const char *format, ...) {
if( NULL == getenv("MMALLOC_VERBOSE") ) return;
va_list argp;
va_start(argp, format);
vwarnx(format, argp);
}
static char log_filename[PATH_MAX];
void
mlog( const char func[], int line, const void *ptr ) {
static size_t nalloc = 0;
const struct mgmt_t *mgmt = (void*)((const char*)ptr - mgmt_size);
char *s = log_filename; // re-use filename buffer
int fd;
if( ! log_dir ) return;
sprintf(log_filename, "%s/%p", log_dir, mgmt->base);
const int flags = O_WRONLY | O_CREAT | O_APPEND;
const mode_t mode = S_IRUSR | S_IWUSR;
if( (fd = open(log_filename, flags, mode)) == -1 ) {
warn("%s", log_filename);
return;
}
/*
* If called with a function and line, report it to the log file.
* If not, write the backtrace instead.
*/
++nalloc;
if( func ) {
int len = sprintf(s, "%s:%d [%zu]: %zu bytes allocated at %p, %zu total\n",
func, line, nalloc,
(mgmt->base + mgmt->len) - (char*)ptr,
ptr,
mgmt->len);
if( -1 == write(fd, s, len) ) {
err(EXIT_FAILURE, "%s", func);
}
} else { // print backtrace
static char symbols[1024 * 24];
void *bt[24];
int len = sprintf(s, "#%zu: %zu bytes allocated at %p, %zu total\n",
nalloc,
(mgmt->base + mgmt->len) - (char*)ptr,
ptr,
mgmt->len);
if( -1 == write(fd, s, len) ) {
err(EXIT_FAILURE, "%s", func);
}
int nbt = backtrace(bt, COUNT_OF(bt));
backtrace_symbols_fd(bt, nbt, fd);
}
if( -1 == close(fd) ) {
err(EXIT_FAILURE, "%s", func);
}
}
static void
munlog( const void *ptr ) {
if( ! log_dir ) return;
if( log_backtrace ) return;
const struct mgmt_t *mgmt = (void*)((const char*)ptr - mgmt_size);
sprintf(log_filename, "%s/%p", log_dir, mgmt->base);
if( -1 == unlink(log_filename) ) {
warn(__func__);
} else {
Warnx("removed %s", log_filename);
}
}
static inline size_t
allocation_size( size_t size ) {
return (1 + (mgmt_size + size) / page_size) * page_size;
}
// compute the pointer returned by malloc for page(s) described by mgmt_t.
static inline void *
malloc_at( const struct mgmt_t mgmt, size_t size ) {
return mgmt.base + mgmt.len - size;
}
/*
* Allocate 1 or more pages of memory. Put allocation description
* "behind" the returned pointer. Return pointer to memory, just as
* malloc(3).
*/
static char *
map_anon( size_t size ) {
enum { offset = 0,
prot = PROT_READ | PROT_WRITE,
flags = MAP_PRIVATE | MAP_ANONYMOUS };
if( page_size == 0 ) {
if( (page_size = sysconf(_SC_PAGESIZE)) == -1 ) {
err(EXIT_FAILURE, "sysconf _SC_PAGESIZE");
}
Warnx("_SC_PAGESIZE: %ld bytes, mgmt: %zu bytes", page_size, mgmt_size);
if( (log_dir = getenv("MMALLOC_LOGDIR")) != NULL ) {
size_t len;
if( (len = strlen(log_dir)) > PATH_MAX - 32 ) {
errx(EXIT_FAILURE, "MMALLOC_LOGDIR name '%s' is %zu characters long, "
"cannot be more than %d = PATH_MAX - 32",
log_dir, len, PATH_MAX - 32);
}
}
log_backtrace = getenv("MMALLOC_BACKTRACE");
}
assert(page_size > 0);
struct mgmt_t mgmt = { .len = allocation_size(size) };
assert(mgmt.len >= mgmt_size + size);
mgmt.base = mmap(NULL, mgmt.len, prot, flags, -1, 0);
if( mgmt.base == NULL ) {
err(EXIT_FAILURE, "%s:%d: mmap", __func__, __LINE__ );
}
char *p = malloc_at(mgmt, size) - mgmt_size;
if( true ) {
char *pend = mgmt.base + mgmt.len;
Warnx("returning %p, %zu bytes back from %p",
p + mgmt_size, pend - (p + mgmt_size), pend);
}
memcpy(p, &mgmt, mgmt_size);
char *pcrc = p + ( (char*)&mgmt.crc - (char*)&mgmt );
size_t len = pcrc - mgmt.base; // allocated space up to the CRC
mgmt.crc = crc_32(mgmt.base, len);
memcpy(pcrc, &mgmt.crc, sizeof(mgmt.crc)); // pcrc might not be aligned
#if 0
warnx( "protecting %zu bytes (%p-%p) + CRC (%zu bytes)\n"
"for size %zu at %p, ending at %p, total block %zu",
len, mgmt.base, pcrc, sizeof(mgmt.crc),
size, p + mgmt_size, p + mgmt_size + size,
(p + mgmt_size + size) - mgmt.base );
#endif
if( log_backtrace ) {
mlog(NULL, 0, p + mgmt_size);
}
return p + mgmt_size;
}
void *
mmalloc(size_t size) {
return map_anon(size);
}
void
mfree(void *ptr) {
if( ptr == NULL ) {
return;
}
struct mgmt_t mgmt;
memcpy(&mgmt, (char*)ptr - mgmt_size, mgmt_size);
// verify ptr is between base and base+len
if( !( mgmt.base < (char*)ptr && (char*)ptr < mgmt.base + mgmt.len) ) {
errx(EXIT_FAILURE, "%s:%d: allocation corrupted before '%x' at %p",
__func__, __LINE__, *(char*)ptr, ptr );
}
// verify ptr is on same page as mgmt.base
if( (char*)ptr - mgmt.base > page_size ) {
errx(EXIT_FAILURE, "%s:%d: internal pointer frobbed: "
"%p is %zu from %p, should be <= %zu ",
__func__, __LINE__,
mgmt.base, (char*)ptr - mgmt.base, ptr, page_size );
}
// verify pointer, length, and unused portion are unmodified
struct mgmt_t *p = (void*)((char*)ptr - mgmt_size);
size_t len = (char*)&p->crc - mgmt.base;
mgmt.crc = crc_32(mgmt.base, len);
if( 0 != memcmp(&p->crc, &mgmt.crc, sizeof(mgmt.crc)) ) {
errx(EXIT_FAILURE, "%s:%d: internal page frobbed: "
"0x%x != 0x%x for %p, len %zu ",
__func__, __LINE__,
mgmt.crc, p->crc, p, len );
}
for( char *p = mgmt.base; p < ((char*)ptr - mgmt_size); p++ ) {
if( *p != '\0' ) {
errx(EXIT_FAILURE, "%s:%d: nonzero memory "
"'%x' at %p, %zu bytes before %p", __func__, __LINE__,
*p, p, (char*)ptr - p, ptr );
}
}
if( -1 == munmap(mgmt.base, mgmt.len) ) {
err(EXIT_FAILURE, "%s:%d: munmap", __func__, __LINE__);
}
munlog(ptr);
}
void *
mcalloc(size_t nelem, size_t size) {
return map_anon(nelem * size);
}
static inline size_t
min( size_t a, size_t b) {
return a < b? a : b;
}
void *
mrealloc(void *ptr, size_t size) {
if( NULL == ptr ) return map_anon(size);
struct mgmt_t old;
memcpy(&old, (char*)ptr - mgmt_size, mgmt_size);
void *p = map_anon(size);
memcpy(p, old.base, min(size, old.len));
mfree(ptr);
return p;
}
size_t
mmsize( void *ptr ) {
const struct mgmt_t *pmgmt = (struct mgmt_t*)ptr - 1;
return pmgmt->len;
}
size_t
mmrequired( size_t size ) {
size += mgmt_size;
if( size < page_size ) return page_size;
return page_size + page_size * (size / page_size);
}