This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memtrace.cpp
547 lines (482 loc) · 14.6 KB
/
memtrace.cpp
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
/*********************************
Memoriaszivargas-detektor
Keszitette: Peregi Tamas, BME IIT, 2011
Kanari: Szeberenyi Imre, 2013.
VS 2012: Szeberényi Imre, 2015.,
mem_dump: 2016.
meset felszabaditaskor: 2018.
typo: 2019.
*********************************/
/*definialni kell, ha nem paracssorbol allitjuk be (-DMEMTRACE) */
#ifndef MEMTRACE
#define MEMTRACE
#endif // MEMTRACE
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS 1
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#ifdef MEMTRACE
#define FROM_MEMTRACE_CPP
#include "memtrace.h"
#define FMALLOC 0
#define FCALLOC 1
#define FREALLOC 2
#define FFREE 3
#define FNEW 4
#define FDELETE 5
#define FNEWARR 6
#define FDELETEARR 7
#define COMP(a,d) (((a)<=3 && (d)<=3) || ((d)==(a)+1))
#define PU(p) ((char*)p+CANARY_LEN) // mem pointerbol user poi
#define P(pu) ((char*)pu-CANARY_LEN) // user pointerbol mem poi
#define XSTR(s) STR(s)
#define STR(s) #s
/*******************************************************************/
/* Segedfuggvenyek es egyebek */
/*******************************************************************/
START_NAMESPACE
static FILE *fperror;
#ifdef MEMTRACE_TO_MEMORY
static const unsigned int CANARY_LEN = 64;
#else
static const unsigned int CANARY_LEN = 0;
#endif
static const unsigned char canary_byte1 = 'k';
static const unsigned char canary_byte2 = 'K';
static unsigned char random_byte;
typedef enum {FALSE,TRUE} BOOL;
static const char * pretty[] = {"malloc(", "calloc(", "realloc(", "free(",
"new", "delete", "new[]", "delete[]"};
static const char * basename(const char * s) {
const char *s1,*s2;
s1 = strrchr(s,'/');
if(s1==NULL) s1 = s; else s1++;
s2 = strrchr(s1, '\\');
if(s2==NULL) s2 = s1; else s2++;
return s2;
}
static char *StrCpy(char ** to, const char * from) {
if(from == NULL) {
*to = NULL;
} else {
*to = (char*)malloc(strlen(from)+1);
if(*to) strcpy(*to, from);
}
return *to;
}
static void *canary_malloc(size_t size, unsigned char data) {
char *p = (char *)malloc(size+2*CANARY_LEN);
if (p) {
memset(p, canary_byte1, CANARY_LEN);
memset(p+CANARY_LEN, data, size);
memset(p+CANARY_LEN+size, canary_byte2, CANARY_LEN);
}
return p;
}
static int chk_canary(void *p, size_t size) {
unsigned char *pc = (unsigned char*)p;
unsigned int i;
for (i = 0; i < CANARY_LEN; i++)
if (pc[i] != canary_byte1)
return -1;
pc += CANARY_LEN+size;
for (i = 0; i < CANARY_LEN; i++)
if (pc[i] != canary_byte2)
return 1;
return 0;
}
typedef struct {
int f; /* allocator func */
int line;
char * par_txt;
char * file;
} call_t;
static call_t pack(int f, const char * par_txt, int line, const char * file) {
call_t ret;
ret.f = f;
ret.line = line;
StrCpy(&ret.par_txt, par_txt);
StrCpy(&ret.file, file);
return ret;
}
static void print_call(const char * msg, call_t call) {
if(msg) fprintf(fperror, "%s", msg);
fprintf(fperror, "%s", pretty[call.f]);
fprintf(fperror, "%s", call.par_txt ? call.par_txt : "?");
if (call.f <= 3) fprintf(fperror, ")");
fprintf(fperror," @ %s:", call.file ? basename(call.file) : "?");
fprintf(fperror,"%d\n",call.line ? call.line : 0);
}
/* memoriateruletet dump */
static void dump_memory(void const *mem, size_t size, size_t can_len, FILE* fp) {
unsigned char const *m=(unsigned char const *) mem;
unsigned int s, o;
if (can_len > 0)
fprintf(fp, "Dump (addr: %p kanari hossz: %d):\n", m+can_len, (int)can_len);
else
fprintf(fp, "Dump: (addr: %p) \n", m);
size += 2*can_len;
for (s = 0; s < (size+15)/16; s++) {
fprintf(fp, "%04x:%c ", s*16, s*16 < can_len || s*16 >= size-can_len ? ' ' : '*');
for (o = 0; o < 16; o++) {
if (o == 8) fprintf(fp, " ");
if (s*16+o < size)
fprintf(fp, "%02x ", m[s*16+o]);
else
fprintf(fp, " ");
}
fprintf(fp, " ");
for (o = 0; o < 16; o++) {
if (s*16+o < size)
fprintf(fp, "%c", isprint(m[s*16+o]) ? m[s*16+o] : '.');
else
fprintf(fp, " ");
}
fprintf(fp, "\n");
}
}
void mem_dump(void const *mem, size_t size, FILE* fp) {
dump_memory(mem, size, 0, fp);
}
static BOOL dying;
static void die(const char * msg, void * p, size_t size, call_t * a, call_t * d) {
#ifdef MEMTRACE_ERRFILE
fperror = fopen(XSTR(MEMTRACE_ERRFILE), "w");
#endif
fprintf(fperror,"%s\n",msg);
if (p) {
fprintf(fperror, "\tPointer:\t%p", PU(p));
if (size) fprintf(fperror," (%d byte)", (int)size);
fprintf(fperror,"\n");
}
if (a) print_call("\tFoglalas:\t", *a);
if (d) print_call("\tFelszabaditas:\t", *d);
if (p) dump_memory(p, size, CANARY_LEN, fperror);
dying = TRUE;
exit(120);
}
static void initialize();
END_NAMESPACE
/*******************************************************************/
/* MEMTRACE_TO_MEMORY */
/*******************************************************************/
#ifdef MEMTRACE_TO_MEMORY
START_NAMESPACE
typedef struct _registry_item {
void * p; /* mem pointer*/
size_t size; /* size*/
call_t call;
struct _registry_item * next;
} registry_item;
static registry_item registry; /*sentinel*/
static void print_registry_item(registry_item * p) {
if (p) {
print_registry_item(p->next);
fprintf(fperror, "\t%p%5d byte ",p->p, (int)p->size);
print_call(NULL, p->call);
if(p->call.par_txt) free(p->call.par_txt);
if(p->call.file) free(p->call.file);
free(p);
}
}
/* ha nincs hiba, akkor 0-val tér vissza */
int mem_check(void) {
initialize();
if(dying) return 2; /* címzési hiba */
if(registry.next) {
/*szivarog*/
#ifdef MEMTRACE_ERRFILE
fperror = fopen(XSTR(MEMTRACE_ERRFILE), "w");
#endif
fprintf(fperror, "Szivargas:\n");
print_registry_item(registry.next);
registry.next = NULL;
return 1; /* memória fogyás */
}
return 0;
}
END_NAMESPACE
#endif/*MEMTRACE_TO_MEMORY*/
/*******************************************************************/
/* MEMTRACE_TO_FILE */
/*******************************************************************/
#ifdef MEMTRACE_TO_FILE
START_NAMESPACE
static FILE * trace_file;
END_NAMESPACE
#endif
/*******************************************************************/
/* register/unregister */
/*******************************************************************/
START_NAMESPACE
static int allocated_blks;
int allocated_blocks() { return allocated_blks; }
static BOOL register_memory(void * p, size_t size, call_t call) {
initialize();
allocated_blks++;
#ifdef MEMTRACE_TO_FILE
fprintf(trace_file, "%p\t%d\t%s%s", PU(p), (int)size, pretty[call.f], call.par_txt ? call.par_txt : "?");
if (call.f <= 3) fprintf(trace_file, ")");
fprintf(trace_file, "\t%d\t%s\n", call.line, call.file ? call.file : "?");
fflush(trace_file);
#endif
#ifdef MEMTRACE_TO_MEMORY
{/*C-blokk*/
registry_item * n = (registry_item*)malloc(sizeof(registry_item));
if(n==NULL) return FALSE;
n->p = p;
n->size = size;
n->call = call;
n->next = registry.next;
registry.next = n;
}/*C-blokk*/
#endif
return TRUE;
}
#ifdef MEMTRACE_TO_MEMORY
static registry_item *find_registry_item(void * p) {
registry_item *n = ®istry;
for(; n->next && n->next->p != p ; n=n->next);
return n;
}
#endif
static void unregister_memory(void * p, call_t call) {
initialize();
#ifdef MEMTRACE_TO_FILE
fprintf(trace_file, "%p\t%d\t%s%s", PU(p), -1, pretty[call.f], call.par_txt ? call.par_txt : "?");
if (call.f <= 3) fprintf(trace_file, ")");
fprintf(trace_file,"\t%d\t%s\n",call.line, call.file ? call.file : "?");
fflush(trace_file);
#endif
#ifdef MEMTRACE_TO_MEMORY
{ /*C-blokk*/
registry_item * n = find_registry_item(p);
if(n->next) {
allocated_blks--;
registry_item * r = n->next;
n->next = r->next;
if(COMP(r->call.f,call.f)) {
int chk = chk_canary(r->p, r->size);
if (chk < 0)
die("Blokk elott serult a memoria:", r->p,r->size,&r->call,&call);
if (chk > 0)
die("Blokk utan serult a memoria", r->p,r->size,&r->call,&call);
/*rendben van minden*/
if(call.par_txt) free(call.par_txt);
if(r->call.par_txt) free(r->call.par_txt);
if(call.file) free(call.file);
if(r->call.file) free(r->call.file);
memset(PU(r->p), 'f', r->size);
PU(r->p)[r->size-1] = 0;
free(r);
} else {
/*hibas felszabaditas*/
die("Hibas felszabaditas:",r->p,r->size,&r->call,&call);
}
} else {
die("Nem letezo, vagy mar felszabaditott adat felszabaditasa:", p, 0,NULL,&call);
}
} /*C-blokk*/
#endif
}
END_NAMESPACE
/*******************************************************************/
/* C-stílusú memóriakezelés */
/*******************************************************************/
#ifdef MEMTRACE_C
START_NAMESPACE
void * traced_malloc(size_t size, const char * par_txt, int line, const char * file) {
void * p;
initialize();
p = canary_malloc(size, random_byte);
if (p) {
if(!register_memory(p,size,pack(FMALLOC,par_txt,line,file))) {
free(p);
return NULL;
}
return PU(p);
}
return NULL;
}
void * traced_calloc(size_t count, size_t size, const char * par_txt, int line, const char * file) {
void * p;
initialize();
size *= count;
p = canary_malloc(size, 0);
if(p) {
if(!register_memory(p,size,pack(FCALLOC,par_txt,line,file))) {
free(p);
return NULL;
}
return PU(p);
}
return NULL;
}
void traced_free(void * pu, const char * par_txt, int line, const char * file) {
initialize();
if(pu) {
unregister_memory(P(pu), pack(FFREE,par_txt,line,file));
free(P(pu));
} else {
/*free(NULL) eset*/
#ifdef MEMTRACE_TO_FILE
fprintf(trace_file,"%s\t%d\t%10s\t","NULL",-1,pretty[FFREE]);
fprintf(trace_file,"%d\t%s\n",line,file ? file : "?");
fflush(trace_file);
#endif
#ifndef ALLOW_FREE_NULL
{/*C-blokk*/
call_t call;
call = pack(FFREE,par_txt,line,file);
die("free(NULL) hivasa:",NULL,0,NULL,&call);
}/*C-blokk*/
#endif
}
}
void * traced_realloc(void * old, size_t size, const char * par_txt, int line, const char * file) {
void * p;
size_t oldsize = 0;
registry_item * n;
initialize();
#ifdef MEMTRACE_TO_MEMORY
n = find_registry_item(P(old));
if (n) oldsize = n->next->size;
p = canary_malloc(size, random_byte);
#else
p = realloc(old, size);
#endif
if (p) {
/*Ha sikerult a foglalas, regisztraljuk*/
register_memory(p,size,pack(FREALLOC, par_txt, line,file));
if (old) {
#ifdef MEMTRACE_TO_MEMORY
int cpsize = 2*CANARY_LEN;
if (oldsize < size) cpsize += oldsize;
else cpsize += size;
memcpy(p, P(old), cpsize);
#endif
unregister_memory(P(old), pack(FREALLOC, par_txt, line, file));
#ifdef MEMTRACE_TO_MEMORY
free P(old);
#endif
}
return PU(p);
} else {
return NULL;
}
}
END_NAMESPACE
#endif/*MEMTRACE_C*/
/*******************************************************************/
/* C++-stílusú memóriakezelés */
/*******************************************************************/
#ifdef MEMTRACE_CPP
START_NAMESPACE
std::new_handler _new_handler;
void _set_new_handler(std::new_handler h) {
initialize();
_new_handler = h;
}
static call_t delete_call;
static BOOL delete_called;
void set_delete_call(int line, const char * file) {
initialize();
delete_call=pack(0,"",line,file); /*func értéke lényegtelen, majd felülírjuk*/
delete_called = TRUE;
}
void * traced_new(size_t size, int line, const char * file, int func) {
initialize();
for (;;) {
void * p = canary_malloc(size, random_byte);
if(p) {
register_memory(p,size,pack(func,"",line,file));
return PU(p);
}
if (_new_handler == 0)
throw std::bad_alloc();
_new_handler();
}
}
void traced_delete(void * pu, int func) {
initialize();
if(pu) {
/*kiolvasom call-t, ha van*/
memtrace::call_t call = delete_called ? (delete_call.f=func, delete_call) : pack(func,NULL,0,NULL);
memtrace::unregister_memory(P(pu),call);
free(P(pu));
}
delete_called=FALSE;
}
END_NAMESPACE
void * operator new(size_t size, int line, const char * file) THROW_BADALLOC {
return memtrace::traced_new(size,line,file,FNEW);
}
void * operator new[](size_t size, int line, const char * file) THROW_BADALLOC {
return memtrace::traced_new(size,line,file,FNEWARR);
}
void * operator new(size_t size) THROW_BADALLOC {
return memtrace::traced_new(size,0,NULL,FNEW);
}
void * operator new[](size_t size) THROW_BADALLOC {
return memtrace::traced_new(size,0,NULL,FNEWARR);
}
void operator delete(void * p) THROW_NOTHING {
memtrace::traced_delete(p,FDELETE);
}
void operator delete[](void * p) THROW_NOTHING {
memtrace::traced_delete(p,FDELETEARR);
}
#if __cplusplus >= 201402L
void operator delete(void * p, size_t) THROW_NOTHING {
memtrace::traced_delete(p,FDELETE);
}
void operator delete[](void * p, size_t) THROW_NOTHING {
memtrace::traced_delete(p,FDELETEARR);
}
#endif
/* Visual C++ 2012 miatt kell, mert háklis, hogy nincs megfelelő delete, bár senki sem használja */
void operator delete(void * p, int, const char *) THROW_NOTHING {
memtrace::traced_delete(p,FDELETE);
}
void operator delete[](void * p, int, const char *) THROW_NOTHING {
memtrace::traced_delete(p,FDELETE);
}
#endif/*MEMTRACE_CPP*/
/*******************************************************************/
/* initialize */
/*******************************************************************/
START_NAMESPACE
static void initialize() {
static BOOL first = TRUE;
if(first) {
fperror = stderr;
random_byte = (unsigned char)time(NULL);
first = FALSE;
dying = FALSE;
#ifdef MEMTRACE_TO_MEMORY
registry.next = NULL;
#if !defined(USE_ATEXIT_OBJECT) && defined(MEMTRACE_AUTO)
atexit((void(*)(void))mem_check);
#endif
#endif
#ifdef MEMTRACE_TO_FILE
trace_file = fopen("memtrace.dump","w");
#endif
#ifdef MEMTRACE_CPP
_new_handler = NULL;
delete_called = FALSE;
delete_call = pack(0,NULL,0,NULL);
#endif
}
}
#if defined(MEMTRACE_TO_MEMORY) && defined(USE_ATEXIT_OBJECT)
int atexit_class::counter = 0;
int atexit_class::err = 0;
#endif
END_NAMESPACE
#endif