/
smartall.cc
567 lines (474 loc) · 17.1 KB
/
smartall.cc
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
S M A R T A L L O C
Smart Memory Allocator
Evolved over several years, starting with the initial
SMARTALLOC code for AutoSketch in 1986, guided by the Blind
Watchbreaker, John Walker. Isolated in this general-purpose
form in September of 1989. Updated with be more POSIX
compliant and to include Web-friendly HTML documentation in
October of 1998 by the same culprit. For additional
information and the current version visit the Web page:
http://www.fourmilab.ch/smartall/
*/
#define _LOCKMGR_COMPLIANT
#include "include/bareos.h"
/*
* Use the real routines here
*/
#undef realloc
#undef calloc
#undef malloc
#undef free
uint64_t sm_max_bytes = 0;
uint64_t sm_bytes = 0;
uint32_t sm_max_buffers = 0;
uint32_t sm_buffers = 0;
#ifdef SMARTALLOC
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
extern char my_name[]; /* Daemon name */
#define EOS '\0' /* End of string sentinel */
#define sm_min(a, b) ((a) < (b) ? (a) : (b))
/*
* Queue data structures
*/
/*
* Memory allocation control structures and storage.
*/
struct abufhead {
struct b_queue abq; /* Links on allocated queue */
uint32_t ablen; /* Buffer length in bytes */
const char *abfname; /* File name pointer */
uint32_t ablineno; /* Line number of allocation */
bool abin_use; /* set when malloced and cleared when free */
};
/*
* Allocated buffer queue
*/
static struct b_queue abqueue = {
&abqueue, &abqueue
};
static bool bufimode = false; /* Buffers not tracked when True */
#define HEAD_SIZE BALIGN(sizeof(struct abufhead))
/*
* SMALLOC -- Allocate buffer, enqueing on the orphaned buffer tracking list.
*/
/*
* Special version of error reporting using a static buffer so we don't use
* the normal error reporting which uses dynamic memory e.g. recursivly calls
* these routines again leading to deadlocks.
*/
static void SmartAllocMsg(const char *file, int line, const char *fmt, ...)
{
char buf[256];
va_list arg_ptr;
int len;
len = Bsnprintf(buf, sizeof(buf),
_("%s: ABORTING due to ERROR in %s:%d\n"),
my_name, get_basename(file), line);
va_start(arg_ptr, fmt);
Bvsnprintf(buf + len, sizeof(buf) - len, (char *)fmt, arg_ptr);
va_end(arg_ptr);
DispatchMessage(NULL, M_ABORT, 0, buf);
char *p = 0;
p[0] = 0; /* Generate segmentation violation */
}
static void *smalloc(const char *fname, int lineno, unsigned int nbytes)
{
char *buf;
/*
* Note: Unix MALLOC actually permits a zero length to be
* passed and allocates a valid block with zero user bytes.
* Such a block can later be expanded with realloc(). We
* disallow this based on the belief that it's better to make
* a special case and allocate one byte in the rare case this
* is desired than to miss all the erroneous occurrences where
* buffer length calculation code results in a zero.
*/
ASSERT(nbytes > 0);
nbytes += HEAD_SIZE + 1;
if ((buf = (char *)malloc(nbytes)) != NULL) {
struct abufhead *head = (struct abufhead *)buf;
P(mutex);
/*
* Enqueue buffer on allocated list
*/
qinsert(&abqueue, (struct b_queue *) buf);
head->ablen = nbytes;
head->abfname = bufimode ? NULL : fname;
head->ablineno = (uint32_t)lineno;
head->abin_use = true;
/*
* Emplace end-clobber detector at end of buffer
*/
buf[nbytes - 1] = (uint8_t)((((intptr_t) buf) & 0xFF) ^ 0xC5);
/*
* Increment to user data start
*/
buf += HEAD_SIZE;
if (++sm_buffers > sm_max_buffers) {
sm_max_buffers = sm_buffers;
}
sm_bytes += nbytes;
if (sm_bytes > sm_max_bytes) {
sm_max_bytes = sm_bytes;
}
V(mutex);
} else {
SmartAllocMsg(__FILE__, __LINE__, _("Out of memory\n"));
}
#if SMALLOC_SANITY_CHECK > 0
if (sm_bytes > SMALLOC_SANITY_CHECK) {
SmartAllocMsg(__FILE__, __LINE__, _("Too much memory used."));
}
#endif
return (void *)buf;
}
/* SM_NEW_OWNER -- Update the File and line number for a buffer
This is to accomodate mem_pool. */
void SmNewOwner(const char *fname, int lineno, char *buf)
{
buf -= HEAD_SIZE; /* Decrement to header */
((struct abufhead *)buf)->abfname = bufimode ? NULL : fname;
((struct abufhead *)buf)->ablineno = (uint32_t) lineno;
((struct abufhead *)buf)->abin_use = true;
return;
}
/* SM_FREE -- Update free pool availability. FREE is never called
except through this interface or by Actuallyfree().
free(x) is defined to generate a call to this
routine. */
void sm_free(const char *file, int line, void *fp)
{
char *cp = (char *) fp;
struct b_queue *qp;
uint32_t lineno = line;
if (cp == NULL) {
SmartAllocMsg(__FILE__, __LINE__, _("Attempt to free NULL called from %s:%d\n"), file, lineno);
}
cp -= HEAD_SIZE;
qp = (struct b_queue *)cp;
struct abufhead *head = (struct abufhead *)cp;
P(mutex);
if (!head->abin_use) {
V(mutex);
SmartAllocMsg(__FILE__, __LINE__, _("double free from %s:%d\n"), file, lineno);
}
head->abin_use = false;
/* The following assertions will catch virtually every release
of an address which isn't an allocated buffer. */
if (qp->qnext->qprev != qp) {
V(mutex);
SmartAllocMsg(__FILE__, __LINE__, _("qp->qnext->qprev != qp called from %s:%d\n"), file, lineno);
}
if (qp->qprev->qnext != qp) {
V(mutex);
SmartAllocMsg(__FILE__, __LINE__, _("qp->qprev->qnext != qp called from %s:%d\n"), file, lineno);
}
/* The following assertion detects storing off the end of the
allocated space in the buffer by comparing the end of buffer
checksum with the address of the buffer. */
if (((unsigned char *)cp)[head->ablen - 1] != ((((intptr_t) cp) & 0xFF) ^ 0xC5)) {
V(mutex);
SmartAllocMsg(__FILE__, __LINE__, _("Overrun buffer: len=%d addr=%p allocated: %s:%d called from %s:%d\n"),
head->ablen, fp, get_basename(head->abfname), head->ablineno, file, line);
}
if (sm_buffers > 0) {
sm_buffers--;
sm_bytes -= head->ablen;
}
qdchain(qp);
V(mutex);
/* Now we wipe the contents of the just-released buffer with
"designer garbage" (Duff Kurland's phrase) of alternating
bits. This is intended to ruin the day for any miscreant who
attempts to access data through a pointer into storage that's
been previously released.
Modified, kes May, 2007 to not zap the header. This allows us
to check the in_use bit and detect doubly freed buffers.
*/
memset(cp+HEAD_SIZE, 0xAA, (int)(head->ablen - HEAD_SIZE));
free(cp);
}
/* SM_MALLOC -- Allocate buffer. NULL is returned if no memory
was available. */
void *sm_malloc(const char *fname, int lineno, unsigned int nbytes)
{
void *buf;
if ((buf = smalloc(fname, lineno, nbytes)) != NULL) {
/* To catch sloppy code that assumes buffers obtained from
malloc() are zeroed, we preset the buffer contents to
"designer garbage" consisting of alternating bits. */
memset(buf, 0x55, (int) nbytes);
} else {
SmartAllocMsg(__FILE__, __LINE__, _("Out of memory\n"));
}
return buf;
}
/* SM_CALLOC -- Allocate an array and clear it to zero. */
void *sm_calloc(const char *fname, int lineno,
unsigned int nelem, unsigned int elsize)
{
void *buf;
if ((buf = smalloc(fname, lineno, nelem * elsize)) != NULL) {
memset(buf, 0, (int) (nelem * elsize));
} else {
SmartAllocMsg(__FILE__, __LINE__, _("Out of memory\n"));
}
return buf;
}
/* SM_REALLOC -- Adjust the size of a previously allocated buffer.
Note that the trick of "resurrecting" a previously
freed buffer with realloc() is NOT supported by this
function. Further, because of the need to maintain
our control storage, SM_REALLOC must always allocate
a new block and copy the data in the old block.
This may result in programs which make heavy use of
realloc() running much slower than normally. */
void *sm_realloc(const char *fname, int lineno, void *ptr, unsigned int size)
{
unsigned osize;
void *buf;
char *cp = (char *) ptr;
if (size <= 0) {
e_msg(fname, lineno, M_ABORT, 0, _("sm_realloc size: %d\n"), size);
}
/* If the old block pointer is NULL, treat realloc() as a
malloc(). SVID is silent on this, but many C libraries
permit this. */
if (ptr == NULL) {
return sm_malloc(fname, lineno, size);
}
/* If the old and new sizes are the same, be a nice guy and just
return the buffer passed in. */
cp -= HEAD_SIZE;
struct abufhead *head = (struct abufhead *)cp;
osize = head->ablen - (HEAD_SIZE + 1);
if (size == osize) {
return ptr;
}
/* Sizes differ. Allocate a new buffer of the requested size.
If we can't obtain such a buffer, act as defined in SVID:
return NULL from realloc() and leave the buffer in PTR
intact. */
// sm_buffers--;
// sm_bytes -= head->ablen;
if ((buf = smalloc(fname, lineno, size)) != NULL) {
memcpy(buf, ptr, (int)sm_min(size, osize));
/* If the new buffer is larger than the old, fill the balance
of it with "designer garbage". */
if (size > osize) {
memset(((char *) buf) + osize, 0x55, (int) (size - osize));
}
/* All done. Free and dechain the original buffer. */
sm_free(fname, lineno, ptr);
}
return buf;
}
/* ACTUALLYMALLOC -- Call the system malloc() function to obtain
storage which will eventually be released
by system or library routines not compiled
using SMARTALLOC. */
void *actuallymalloc(unsigned int size)
{
return malloc(size);
}
/* ACTUALLYCALLOC -- Call the system calloc() function to obtain
storage which will eventually be released
by system or library routines not compiled
using SMARTALLOC. */
void *actuallycalloc(unsigned int nelem, unsigned int elsize)
{
return calloc(nelem, elsize);
}
/* ACTUALLYREALLOC -- Call the system realloc() function to obtain
storage which will eventually be released
by system or library routines not compiled
using SMARTALLOC. */
void *actuallyrealloc(void *ptr, unsigned int size)
{
return realloc(ptr, size);
}
/* ACTUALLYFREE -- Interface to system free() function to release
buffers allocated by low-level routines. */
void Actuallyfree(void *cp)
{
free(cp);
}
/* SM_DUMP -- Print orphaned buffers (and dump them if BUFDUMP is
* True).
*/
void sm_dump(bool bufdump, bool in_use)
{
struct abufhead *ap;
P(mutex);
ap = (struct abufhead *)abqueue.qnext;
while (ap != (struct abufhead *) &abqueue) {
if ((ap == NULL) ||
(ap->abq.qnext->qprev != (struct b_queue *) ap) ||
(ap->abq.qprev->qnext != (struct b_queue *) ap)) {
FPmsg1(0, _("\nOrphaned buffers exist. Dump terminated following\n"
" discovery of bad links in chain of orphaned buffers.\n"
" Buffer address with bad links: %p\n"), ap);
break;
}
if (ap->abfname != NULL) {
char errmsg[500];
uint32_t memsize = ap->ablen - (HEAD_SIZE + 1);
char *cp = ((char *)ap) + HEAD_SIZE;
FPmsg6(0, "%s buffer: %s %d bytes at %p from %s:%d\n",
in_use ? "In use" : "Orphaned",
my_name, memsize, cp, get_basename(ap->abfname), ap->ablineno);
if (bufdump) {
char buf[20];
unsigned llen = 0;
errmsg[0] = EOS;
while (memsize) {
if (llen >= 16) {
bstrncat(errmsg, "\n", sizeof(errmsg));
llen = 0;
FPmsg1(0, "%s", errmsg);
errmsg[0] = EOS;
}
Bsnprintf(buf, sizeof(buf), " %02X",
(*cp++) & 0xFF);
bstrncat(errmsg, buf, sizeof(errmsg));
llen++;
memsize--;
}
FPmsg1(0, "%s\n", errmsg);
}
}
ap = (struct abufhead *) ap->abq.qnext;
}
V(mutex);
}
#undef sm_check
/* SM_CHECK -- Check the buffers and dump if any damage exists. */
void sm_check(const char *fname, int lineno, bool bufdump)
{
if (!sm_check_rtn(fname, lineno, bufdump)) {
SmartAllocMsg(__FILE__, __LINE__, _("Damaged buffer found. Called from %s:%d\n"),
get_basename(fname), (uint32_t)lineno);
}
}
#undef sm_check_rtn
/* SM_CHECK_RTN -- Check the buffers and return 1 if OK otherwise 0 */
int sm_check_rtn(const char *fname, int lineno, bool bufdump)
{
struct abufhead *ap;
int bad, badbuf = 0;
P(mutex);
ap = (struct abufhead *) abqueue.qnext;
while (ap != (struct abufhead *)&abqueue) {
bad = 0;
if (ap != NULL) {
if (ap->abq.qnext->qprev != (struct b_queue *)ap) {
bad = 0x1;
}
if (ap->abq.qprev->qnext != (struct b_queue *)ap) {
bad |= 0x2;
}
if (((unsigned char *) ap)[((struct abufhead *)ap)->ablen - 1] !=
((((intptr_t) ap) & 0xFF) ^ 0xC5)) {
bad |= 0x4;
}
} else {
bad = 0x8;
}
badbuf |= bad;
if (bad) {
FPmsg2(0,
_("\nDamaged buffers found at %s:%d\n"), get_basename(fname), (uint32_t)lineno);
if (bad & 0x1) {
FPmsg0(0, _(" discovery of bad prev link.\n"));
}
if (bad & 0x2) {
FPmsg0(0, _(" discovery of bad next link.\n"));
}
if (bad & 0x4) {
FPmsg0(0, _(" discovery of data overrun.\n"));
}
if (bad & 0x8) {
FPmsg0(0, _(" NULL pointer.\n"));
}
if (!ap) {
goto get_out;
}
FPmsg1(0, _(" Buffer address: %p\n"), ap);
if (ap->abfname != NULL) {
uint32_t memsize = ap->ablen - (HEAD_SIZE + 1);
char errmsg[80];
FPmsg4(0, _("Damaged buffer: %6u bytes allocated at line %d of %s %s\n"),
memsize, ap->ablineno, my_name, get_basename(ap->abfname));
if (bufdump) {
unsigned llen = 0;
char *cp = ((char *) ap) + HEAD_SIZE;
errmsg[0] = EOS;
while (memsize) {
if (llen >= 16) {
strcat(errmsg, "\n");
llen = 0;
FPmsg1(0, "%s", errmsg);
errmsg[0] = EOS;
}
if (*cp < 0x20) {
sprintf(errmsg + strlen(errmsg), " %02X",
(*cp++) & 0xFF);
} else {
sprintf(errmsg + strlen(errmsg), " %c ",
(*cp++) & 0xFF);
}
llen++;
memsize--;
}
FPmsg1(0, "%s\n", errmsg);
}
}
}
ap = (struct abufhead *)ap->abq.qnext;
}
get_out:
V(mutex);
return badbuf ? 0 : 1;
}
/* SM_STATIC -- Orphaned buffer detection can be disabled (for such
items as buffers allocated during initialisation) by
calling SmStatic(1). Normal orphaned buffer
detection can be re-enabled with SmStatic(0). Note
that all the other safeguards still apply to buffers
allocated when SmStatic(1) mode is in effect. */
void SmStatic(bool mode)
{
bufimode = mode;
}
/*
* Here we overload C++'s global new and delete operators
* so that the memory is allocated through smartalloc.
*/
#ifdef xxx
void * operator new(size_t size)
{
return sm_malloc(__FILE__, __LINE__, size);
}
void operator delete(void *buf)
{
sm_free(__FILE__, __LINE__, buf);
}
#endif
#endif /* SMARTALLOC */