-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsiplog_mem_debug.c
338 lines (305 loc) · 9.21 KB
/
siplog_mem_debug.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Copyright (c) 2014 Sippy Software, Inc., http://www.sippysoft.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/*
* Simple memory debug layer to track any unallocated memory as well as to
* catch any other common mistakes, such as double free or freeing of
* unallocated memory. Our attitude here is "fail with core dump early" if
* some error of inconsistency is found to aid debugging. Some extra smarts
* can be added, such as guard area to detect any buffer overflows.
*/
#include <sys/types.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "siplog.h"
#include "siplog_mem_debug.h"
#undef malloc
#undef free
#undef realloc
#undef strdup
#undef asprintf
#undef vasprintf
#define UNUSED(x) (void)(x)
struct memdeb_stats {
int64_t nalloc;
int64_t nunalloc_baseln;
int64_t nfree;
int64_t nrealloc;
int64_t afails;
};
#define MEMDEB_SIGNATURE 0x8b26e00041dfdec6UL
struct memdeb_node
{
uint64_t magic;
const char *fname;
int linen;
const char *funcn;
struct memdeb_stats mstats;
struct memdeb_node *next;
};
static struct {
const char *funcn;
int max_nunalloc;
const char *why;
} approved_unallocated[] = {
{.funcn = "addr2bindaddr", .max_nunalloc = 100, .why = "Too busy to fix now"},
{.funcn = NULL}
};
static struct memdeb_node *nodes;
static pthread_mutex_t *memdeb_mutex;
static struct memdeb_node *
siplog_memdeb_nget(const char *fname, int linen, const char *funcn, int doalloc)
{
struct memdeb_node *rval, *mnp, *lastnode;
if (memdeb_mutex == NULL) {
memdeb_mutex = malloc(sizeof(pthread_mutex_t));
if (memdeb_mutex == NULL)
abort();
pthread_mutex_init(memdeb_mutex, NULL);
}
pthread_mutex_lock(memdeb_mutex);
lastnode = NULL;
for (mnp = nodes; mnp != NULL; mnp = mnp->next) {
if (mnp->magic != MEMDEB_SIGNATURE) {
/* nodelist is corrupt */
abort();
}
if (mnp->fname == fname && mnp->linen == linen && mnp->funcn == funcn)
return (mnp);
lastnode = mnp;
}
if (doalloc == 0) {
pthread_mutex_unlock(memdeb_mutex);
return (NULL);
}
rval = malloc(sizeof(struct memdeb_node));
if (rval == NULL) {
abort();
}
memset(rval, '\0', sizeof(struct memdeb_node));
rval->magic = MEMDEB_SIGNATURE;
rval->fname = fname;
rval->linen = linen;
rval->funcn = funcn;
if (nodes == NULL) {
nodes = rval;
} else {
lastnode->next = rval;
}
return (rval);
}
void *
siplog_memdeb_malloc(size_t size, const char *fname, int linen, const char *funcn)
{
struct memdeb_node *mnp;
char *rval;
mnp = siplog_memdeb_nget(fname, linen, funcn, 1);
rval = malloc(sizeof(struct memdeb_node *) + size);
if (rval == NULL) {
mnp->mstats.afails++;
pthread_mutex_unlock(memdeb_mutex);
return (NULL);
}
mnp->mstats.nalloc++;
pthread_mutex_unlock(memdeb_mutex);
memcpy(rval, &mnp, sizeof(struct memdeb_node *));
rval += sizeof(struct memdeb_node *);
return (rval);
}
void
siplog_memdeb_free(void *ptr, const char *fname, int linen, const char *funcn)
{
UNUSED(fname);
UNUSED(linen);
UNUSED(funcn);
char *cp;
struct memdeb_node *mnp;
cp = ptr;
cp -= sizeof(struct memdeb_node *);
memcpy(&mnp, cp, sizeof(struct memdeb_node *));
if (mnp->magic != MEMDEB_SIGNATURE) {
/* Free of unallicated pointer or nodelist is corrupt */
abort();
}
pthread_mutex_lock(memdeb_mutex);
mnp->mstats.nfree++;
pthread_mutex_unlock(memdeb_mutex);
return free(cp);
}
void *
siplog_memdeb_realloc(void *ptr, size_t size, const char *fname, int linen, const char *funcn)
{
UNUSED(fname);
UNUSED(linen);
UNUSED(funcn);
char *cp;
struct memdeb_node *mnp;
cp = ptr;
cp -= sizeof(struct memdeb_node *);
memcpy(&mnp, cp, sizeof(struct memdeb_node *));
if (mnp->magic != MEMDEB_SIGNATURE) {
/* Realloc of unallicated pointer or nodelist is corrupt */
abort();
}
cp = realloc(cp, size + sizeof(struct memdeb_node *));
if (cp == NULL) {
pthread_mutex_lock(memdeb_mutex);
mnp->mstats.afails++;
pthread_mutex_unlock(memdeb_mutex);
return (cp);
}
pthread_mutex_lock(memdeb_mutex);
mnp->mstats.nrealloc++;
pthread_mutex_unlock(memdeb_mutex);
return (cp + sizeof(struct memdeb_node *));
}
char *
siplog_memdeb_strdup(const char *ptr, const char *fname, int linen, const char *funcn)
{
struct memdeb_node *mnp;
char *rval;
size_t size;
mnp = siplog_memdeb_nget(fname, linen, funcn, 1);
size = strlen(ptr) + 1;
rval = malloc(size + sizeof(struct memdeb_node *));
if (rval == NULL) {
mnp->mstats.afails++;
pthread_mutex_unlock(memdeb_mutex);
return (NULL);
}
mnp->mstats.nalloc++;
pthread_mutex_unlock(memdeb_mutex);
memcpy(rval, &mnp, sizeof(struct memdeb_node *));
rval += sizeof(struct memdeb_node *);
memcpy(rval, ptr, size);
return (rval);
}
int
siplog_memdeb_asprintf(char **pp, const char *fmt, const char *fname,
int linen, const char *funcn, ...)
{
va_list ap;
int rval;
va_start(ap, funcn);
rval = siplog_memdeb_vasprintf(pp, fmt, fname, linen, funcn, ap);
va_end(ap);
return (rval);
}
int
siplog_memdeb_vasprintf(char **pp, const char *fmt, const char *fname,
int linen, const char *funcn, va_list ap)
{
int rval;
void *tp;
rval = vasprintf(pp, fmt, ap);
if (rval <= 0) {
return (rval);
}
tp = siplog_memdeb_malloc(rval + 1, fname, linen, funcn);
if (tp == NULL) {
free(*pp);
*pp = NULL;
return (-1);
}
memcpy(tp, *pp, rval + 1);
free(*pp);
*pp = tp;
return (rval);
}
static int
is_approved(const char *funcn)
{
int i;
for (i = 0; approved_unallocated[i].funcn != NULL; i++) {
if (strcmp(approved_unallocated[i].funcn, funcn) != 0)
continue;
return (approved_unallocated[i].max_nunalloc);
}
return (0);
}
int
siplog_memdeb_dumpstats(int level, siplog_t handle)
{
struct memdeb_node *mnp;
int errors_found, max_nunalloc;
int64_t nunalloc;
errors_found = 0;
pthread_mutex_lock(memdeb_mutex);
for (mnp = nodes; mnp != NULL; mnp = mnp->next) {
nunalloc = mnp->mstats.nalloc - mnp->mstats.nfree;
if (mnp->mstats.afails == 0) {
if (mnp->mstats.nalloc == 0)
continue;
if (mnp->mstats.nalloc == mnp->mstats.nfree)
continue;
if (nunalloc == mnp->mstats.nunalloc_baseln)
continue;
}
if (nunalloc > 0) {
max_nunalloc = is_approved(mnp->funcn);
if (max_nunalloc > 0 && nunalloc <= max_nunalloc)
continue;
}
if (errors_found == 0) {
siplog_write(level, handle,
"MEMDEB:siplog: suspicious allocations:");
}
errors_found++;
siplog_write(level, handle,
" %s+%d, %s(): nalloc = %ld, nfree = %ld, afails = %ld",
mnp->fname, mnp->linen, mnp->funcn, mnp->mstats.nalloc,
mnp->mstats.nfree, mnp->mstats.afails);
}
pthread_mutex_unlock(memdeb_mutex);
if (errors_found == 0) {
siplog_write(level, handle,
"MEMDEB:siplog: all clear");
} else {
siplog_write(level, handle,
"MEMDEB:siplog: errors found: %d", errors_found);
}
return (errors_found);
}
void
siplog_memdeb_setbaseln(void)
{
struct memdeb_node *mnp;
pthread_mutex_lock(memdeb_mutex);
for (mnp = nodes; mnp != NULL; mnp = mnp->next) {
if (mnp->magic != MEMDEB_SIGNATURE) {
/* Nodelist is corrupt */
abort();
}
if (mnp->mstats.nalloc == 0)
continue;
mnp->mstats.nunalloc_baseln = mnp->mstats.nalloc - mnp->mstats.nfree;
}
pthread_mutex_unlock(memdeb_mutex);
}