/
DynamicCheckUp.cpp
1104 lines (939 loc) · 27.5 KB
/
DynamicCheckUp.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Author: Rui Varela <rui.filipe.varela@gmail.com>
* http://ryven.kicks.ass.googlepages.com
*
* Build instructions:
* - unpack DynamicCheckUp compressed file
* - make
* - optionally run the DynamicCheckUp unit test :
* ./DynamicCheckUp ./Dynamic_DCU_UnitTest
* - optionally run the DynamicCheckUp on every day applications
* ./DynamicCheckUp $(which ls)
* ./DynamicCheckUp $(which glxinfo)
* ./DynamicCheckUp $(which glxgears)
*
* How To Use:
* - compile the target application with debug symbols (-g)
* - run a dynamic check-up on the application :
* ./DynamicCheckUp ./MyTargetApplication Parameter_1 Parameter_2 Parameter_3
* - DynamicCheckUp log will be written to "memory_check_up.txt"
*
* Makefile Flags
* - DCU_THREAD_SAFE
* Use thread synchronization for memory requests and releases.
* - DCU_C_MEMORY_CHECK
* CheckUp C memory functions (malloc, calloc, realloc, free)
* WARNING : C memory check-up is not accurate, and problems may be incorrectly reported
* - DCU_ECHO
* Echo information while running application (only useful for static linking debug)
* - DCU_ABORT_ON_RELEASE_NOT_REQUESTED_MEMORY
* Aborts application and reporst when a memory release (free, delete) occurs on not requested memory (new, malloc, calloc)
* - DDCU_ABORT_ON_MEMORY_OVERWRITE
* Aborts application and reports when a memory overwrite occurs
*
* Revisions:
* - xx.12.08 - Main code development.
* - 15.01.09 - Main leak detection code.
* - 20.01.09 - Locate several leaks on the same allocation frame.
* - Parse DCU_OUTPUT_FILE and resolve hex addresses to function names, filenames and lines.
* - 21.01.09 - DCU_FreeNullType problem detection implementation.
* - DCU_RequestZeroMemoryType problem detection implementation.
* - 26.01.09 - DCU_MismatchOperationType problem detection implementation.
* - 27.01.09 - DCU_ReleaseUnallocatedType problem detection implementation.
* - Memory Overwrite Protection implementation.
* - 28.10.09 - Major revision on system. Use of public domain dlmalloc allocator as underlying memory allocator
* - __builtin_return_address was replaced with backtrace :)
* - The first call to backtrace calls malloc, so we must call it explicity on DCU_initialize()
* - Support for x64 systems.
* - added memalign (but it is not used by the memory management system)
*
*
*/
static unsigned char DCU_flags = 0;
void DCU_initialize();
void DCU_shutdown();
static struct DCU_Bootstrap
{
DCU_Bootstrap() { DCU_initialize(); }
~DCU_Bootstrap() { DCU_shutdown(); }
} DCU_BootstrapObject;
#define USE_LOCKS 1
#define MSPACES 1
#define ONLY_MSPACES 1
#define NO_MALLINFO 1
//#define FOOTERS 1
#define DEFAULT_GRANULARITY (1 * 1024 * 1024)
#include "malloc.c.h"
#include <cstdio>
#include <cstring>
#include <cstdarg>
#include <signal.h>
#include <execinfo.h>
class DCU_MutexScopedLock
{
public:
DCU_MutexScopedLock(pthread_mutex_t& mutex) :
mutex_(mutex)
{
#ifdef DCU_THREAD_SAFE
pthread_mutex_lock(&mutex_);
#endif //DCU_THREAD_SAFE
}
~DCU_MutexScopedLock()
{
#ifdef DCU_THREAD_SAFE
pthread_mutex_unlock(&mutex_);
#endif //DCU_THREAD_SAFE
}
private:
pthread_mutex_t& mutex_;
};
/*
* OVERWRITE_DETECTION_DATA
* OVERWRITE_DETECTION_DATA_SIZE
* Allocate extra memory to detect memory over_write
*
* ALLOCATION_VALUE
* Value used to initialize data.
* Detects correct class initialization.
* Helps to get segmentation fault on bad initializations.
*
* DEALLOCATION_VALUE
* Value set on memory which will be deleted.
*
* Thanks to LeakTracker public domain project Homepage: <http://www.andreasen.org/LeakTracer/>
* Authors:
* Erwin S. Andreasen <erwin@andreasen.org>
* Henner Zeller <H.Zeller@acm.org>
*
*/
//#define OVERWRITE_DETECTION_DATA "\x42\x41\x44\x21" //BAD!
#define OVERWRITE_DETECTION_DATA "\xAA\xBB\xCC\xDD"
#define OVERWRITE_DETECTION_DATA_SIZE (sizeof(OVERWRITE_DETECTION_DATA) - 1)
#define ALLOCATION_VALUE 0xAA
#define DEALLOCATION_VALUE 0xEE
#define DCU_DYNAMIC_OPERATION_TYPES 8
enum DCU_DynamicOperationType
{
DCU_MallocType,
DCU_FreeType,
DCU_ReallocType,
DCU_CallocType,
DCU_NewType,
DCU_DeleteType,
DCU_NewArrayType,
DCU_DeleteArrayType
};
static const char* DCU_OperationTypeNames[] =
{
"Malloc",
"Free",
"Realloc",
"Calloc",
"new",
"delete",
"new[]",
"delete[]"
};
#define DCU_DYNAMIC_PROBLEM_TYPES 6
enum DCU_ProblemType
{
DCU_LeakType,
DCU_ReleaseUnallocatedType,
DCU_MismatchOperationType,
DCU_FreeNullType,
DCU_RequestZeroMemoryType,
DCU_MemoryOverWriteType
};
static const char* DCU_ProblemTypenames[] =
{
"Memory Leak",
"Release Unallocated Memory",
"Mismatch Memory Allocation/Deletion",
"Free Null Pointer",
"Request Zero Memory",
"Memory Over-Write",
};
typedef void* DCU_Pointer;
typedef void const* DCU_ConstPointer;
typedef unsigned long DCU_MemoryInt;
typedef long DCU_SignedMemoryInt;
struct DCU_MemoryStats
{
DCU_MemoryInt count;
DCU_MemoryInt total_memory;
DCU_MemoryInt max_value;
};
#define DCU_STACK_TRACE_SIZE 8
struct DCU_OperationInfo
{
DCU_OperationInfo *next;
DCU_DynamicOperationType type;
DCU_ConstPointer memory_address;
size_t size;
DCU_ConstPointer stack[DCU_STACK_TRACE_SIZE];
};
struct DCU_ProblemInfo
{
DCU_ProblemInfo *next;
DCU_ProblemType type;
size_t size;
size_t count;
DCU_MemoryInt total_memory;
DCU_ConstPointer allocation_stack[DCU_STACK_TRACE_SIZE];
DCU_ConstPointer deallocation_stack[DCU_STACK_TRACE_SIZE];
};
#define DCU_INITIALIZED 1
#define DCU_TRACING 2
#define DCU_FINISHED 4
#define DCU_MUTEX_INITED 8
#define DCU_SET_FLAG(flag) (DCU_flags |= flag)
#define DCU_CLEAR_FLAG(flag) (DCU_flags &= ~flag)
#define DCU_STATE(flag) (DCU_flags & flag)
#define DCU_OUTPUT_FILE "memory_check_up.txt"
#define DCU_FALLBACK_STREAM stdout
typedef size_t HastIterator;
#define DCU_HASH_TABLE_SIZE 35323 //prime number, for many allocations use 343051
#define DCU_HASH_FUNCTION(address) ( HastIterator(address) % HastIterator(DCU_HASH_TABLE_SIZE) )
static mspace memory_space;
#define DCU_malloc(size) mspace_malloc(memory_space, size)
#define DCU_free(p) mspace_free(memory_space, p)
#define DCU_realloc(p, size) mspace_realloc(memory_space, p, size)
#define DCU_calloc(nmemb, size) mspace_calloc(memory_space, nmemb, size)
#define DCU_memalign(msp, alignment, bytes) mspace_memalign(memory_space, alignment, bytes)
#define DCU_STREAM_BUFFER_SIZE 512
static FILE* DCU_stream;
static char stream_trace_buffer[DCU_STREAM_BUFFER_SIZE];
static pthread_mutex_t DCU_mutex;
static DCU_OperationInfo** DCU_memory;
static DCU_ProblemInfo* DCU_problems;
static DCU_MemoryStats DCU_memory_stats[DCU_DYNAMIC_OPERATION_TYPES];
static DCU_MemoryStats DCU_memory_stats_c;
static DCU_MemoryStats DCU_memory_stats_new;
static DCU_MemoryStats DCU_memory_stats_new_array;
static DCU_ConstPointer DCU_null_stack[DCU_STACK_TRACE_SIZE];
void* DCU_requestMemory(DCU_DynamicOperationType const& type, size_t size, void* pointer);
void DCU_releaseMemory(DCU_DynamicOperationType const& type, void* pointer);
void DCU_analyzeMemory();
void DCU_reportMemoryStatus();
DCU_OperationInfo* DCU_createOperation();
DCU_ProblemInfo* DCU_createProblem();
//
// Generic DCU_OperationInfo Linked-List Management
//
void DCU_emptyOperationList(DCU_OperationInfo** list);
void DCU_removeOperationFromList(DCU_OperationInfo** list, DCU_OperationInfo* element);
DCU_OperationInfo* DCU_findOperationOnList(DCU_OperationInfo* list, DCU_ConstPointer memory_address);
void DCU_addOperationToList(DCU_OperationInfo** list, DCU_OperationInfo* element);
//
// Generic DCU_ProblemInfo Linked-List Management
//
void DCU_emptyProblemList(DCU_ProblemInfo** list);
void DCU_addProblemToList(DCU_ProblemInfo** list, DCU_ProblemInfo* element);
DCU_ProblemInfo* DCU_findProblem(DCU_ProblemInfo** list, DCU_ProblemType const type,
DCU_ConstPointer allocation_stack[DCU_STACK_TRACE_SIZE],
DCU_ConstPointer deallocation_stack[DCU_STACK_TRACE_SIZE]);
//
// Hash table management
//
void DCU_addMemory(DCU_OperationInfo* element);
DCU_OperationInfo* DCU_findMemory(DCU_ConstPointer memory_address);
void DCU_removeMemory(DCU_OperationInfo* element);
void DCU_emptyMemory();
void DCU_createStackTrace(DCU_ConstPointer stack[DCU_STACK_TRACE_SIZE]);
bool DCU_stacksMatch(DCU_ConstPointer lhs[DCU_STACK_TRACE_SIZE], DCU_ConstPointer rhs[DCU_STACK_TRACE_SIZE]);
void DCU_abort(char const* message, ...);
void DCU_write(char const* message, ...);
//
// Implementation
//
void DCU_initialize()
{
if (DCU_STATE(DCU_INITIALIZED))
{
return;
}
if (DCU_STATE(DCU_MUTEX_INITED))
{
DCU_MutexScopedLock lock(DCU_mutex);
if (DCU_STATE(DCU_INITIALIZED))
{
return;
}
else
{
fprintf(DCU_FALLBACK_STREAM, "DynamicCheckUp Concurrency error.\n");
_exit(1);
}
}
if (pthread_mutex_init(&DCU_mutex, 0) < 0)
{
fprintf(DCU_FALLBACK_STREAM, "DynamicCheckUp unable to initialize mutex\n");
_exit(1);
}
else
{
DCU_SET_FLAG(DCU_MUTEX_INITED);
}
{
DCU_MutexScopedLock lock(DCU_mutex);
memory_space = create_mspace(0, 0);
DCU_SET_FLAG(DCU_INITIALIZED);
//
// init backtrace so it wont recursively call malloc
//
DCU_Pointer stack[DCU_STACK_TRACE_SIZE];
backtrace(stack, DCU_STACK_TRACE_SIZE);
//
// Init Tracing data
//
DCU_stream = DCU_FALLBACK_STREAM;
memset(DCU_memory_stats, 0, sizeof(DCU_MemoryStats) * DCU_DYNAMIC_OPERATION_TYPES);
memset(&DCU_memory_stats_new, 0, sizeof(DCU_MemoryStats));
memset(&DCU_memory_stats_new_array, 0, sizeof(DCU_MemoryStats));
memset(&DCU_memory_stats_c, 0, sizeof(DCU_MemoryStats));
memset(DCU_null_stack, 0, sizeof(DCU_null_stack));
//
// Operations HashTable
//
DCU_memory = (DCU_OperationInfo**) DCU_malloc( DCU_HASH_TABLE_SIZE * sizeof(DCU_OperationInfo*) );
memset(DCU_memory, 0, DCU_HASH_TABLE_SIZE * sizeof(DCU_OperationInfo*));
//
// Problems Linked-List
//
DCU_problems = 0;
//
// Open Log File
//
DCU_stream = fopen(DCU_OUTPUT_FILE, "w");
if (DCU_stream < 0)
{
fprintf(DCU_FALLBACK_STREAM, "DynamicCheckUp: Unable to open %s: %m\n", DCU_OUTPUT_FILE);
DCU_stream = DCU_FALLBACK_STREAM;
}
else
{
int flags = fcntl(fileno(DCU_stream), F_GETFD, 0);
if (flags >= 0)
{
flags |= FD_CLOEXEC;
fcntl(fileno(DCU_stream), F_SETFD, flags);
}
setvbuf(DCU_stream, stream_trace_buffer, _IOFBF, DCU_STREAM_BUFFER_SIZE);
}
DCU_SET_FLAG(DCU_TRACING);
}
DCU_write("DynamicCheckUp Started\n");
}
void DCU_shutdown()
{
if (!DCU_STATE(DCU_FINISHED))
{
{
DCU_MutexScopedLock lock(DCU_mutex);
DCU_CLEAR_FLAG(DCU_TRACING);
DCU_analyzeMemory();
DCU_reportMemoryStatus();
DCU_emptyMemory();
DCU_free(DCU_memory);
DCU_emptyProblemList(&DCU_problems);
}
if (DCU_stream != DCU_FALLBACK_STREAM)
{
fclose(DCU_stream);
DCU_stream = DCU_FALLBACK_STREAM;
}
pthread_mutex_destroy(&DCU_mutex);
DCU_SET_FLAG(DCU_FINISHED);
}
}
void* DCU_requestMemory(DCU_DynamicOperationType const& type, size_t size, void* pointer)
{
DCU_initialize();
void* out = 0;
// DCU_write("Request Type: %d Size:\t%d", type, size);
if (!size && ((type == DCU_CallocType) || (type == DCU_MallocType) || (type == DCU_NewType) || (type == DCU_NewArrayType)))
{
DCU_ConstPointer stack[DCU_STACK_TRACE_SIZE];
DCU_createStackTrace(stack);
if (DCU_STATE(DCU_TRACING))
{
DCU_MutexScopedLock lock(DCU_mutex);
DCU_ProblemInfo* problem = DCU_findProblem(&DCU_problems, DCU_RequestZeroMemoryType, stack, DCU_null_stack);
if (!problem)
{
problem = DCU_createProblem();
problem->type = DCU_RequestZeroMemoryType;
memcpy(problem->allocation_stack, stack, DCU_STACK_TRACE_SIZE * sizeof(DCU_ConstPointer));
DCU_addProblemToList(&DCU_problems, problem);
}
problem->count += 1;
}
return out;
}
if (type == DCU_CallocType)
{
out = DCU_malloc(size + OVERWRITE_DETECTION_DATA_SIZE);
if (out)
{
memset(out, 0, size);
}
}
else if (type == DCU_ReallocType)
{
bool old_allocation_found = false;
size_t old_size = 0;
if (DCU_STATE(DCU_TRACING))
{
DCU_MutexScopedLock lock(DCU_mutex);
DCU_OperationInfo* operation = DCU_findMemory(pointer);
if (operation)
{
old_allocation_found = true;
old_size = operation->size;
DCU_removeMemory(operation);
DCU_memory_stats[DCU_FreeType].count++;
DCU_memory_stats[DCU_FreeType].total_memory += operation->size;
}
}
out = DCU_malloc(size + OVERWRITE_DETECTION_DATA_SIZE);
if (out)
{
#ifdef ALLOCATION_VALUE
memset(out, ALLOCATION_VALUE, size + OVERWRITE_DETECTION_DATA_SIZE);
#endif
if (old_allocation_found)
{
memcpy(out, pointer, ((size > old_size) ? old_size : size));
}
}
if (old_allocation_found)
{
DCU_free(pointer);
}
}
else
{
out = DCU_malloc(size + OVERWRITE_DETECTION_DATA_SIZE);
#ifdef ALLOCATION_VALUE
memset(out, ALLOCATION_VALUE, size + OVERWRITE_DETECTION_DATA_SIZE);
#endif
}
if (out)
{
#ifdef OVERWRITE_DETECTION_DATA
memcpy((char*)(out) + size, OVERWRITE_DETECTION_DATA, OVERWRITE_DETECTION_DATA_SIZE);
#endif
DCU_MutexScopedLock lock(DCU_mutex);
if (DCU_STATE(DCU_TRACING))
{
DCU_OperationInfo* operation = DCU_createOperation();
operation->memory_address = out;
operation->type = type;
operation->size = size;
DCU_createStackTrace(operation->stack);
DCU_addMemory(operation);
DCU_memory_stats[type].count++;
DCU_memory_stats[type].total_memory += size;
if (size > DCU_memory_stats[type].max_value)
{
DCU_memory_stats[type].max_value = size;
}
}
}
// DCU_write(" Done\n");
return out;
}
void DCU_releaseMemory(DCU_DynamicOperationType const& type, void* pointer)
{
DCU_initialize();
// DCU_write("Release Type: %d Address:10%p", type, pointer);
if (pointer)
{
{
DCU_MutexScopedLock lock(DCU_mutex);
if (DCU_STATE(DCU_TRACING))
{
DCU_OperationInfo* operation = DCU_findMemory(pointer);
if (operation)
{
DCU_memory_stats[type].count++;
DCU_memory_stats[type].total_memory += operation->size;
#ifdef OVERWRITE_DETECTION_DATA
if (memcmp((char*)(pointer) + operation->size, OVERWRITE_DETECTION_DATA, OVERWRITE_DETECTION_DATA_SIZE))
{
DCU_ConstPointer stack[DCU_STACK_TRACE_SIZE];
DCU_createStackTrace(stack);
DCU_ProblemInfo* problem = DCU_findProblem(&DCU_problems, DCU_MemoryOverWriteType, operation->stack, stack);
if (!problem)
{
problem = DCU_createProblem();
problem->type = DCU_MemoryOverWriteType;
memcpy(problem->allocation_stack, operation->stack, DCU_STACK_TRACE_SIZE * sizeof(DCU_ConstPointer));
memcpy(problem->deallocation_stack, stack, DCU_STACK_TRACE_SIZE * sizeof(DCU_ConstPointer));
DCU_addProblemToList(&DCU_problems, problem);
}
problem->count += 1;
#ifdef DCU_ABORT_ON_MEMORY_OVERWRITE
DCU_abort("Abnormal program termination : 'Memory Overwrite Detected'\n");
return;
#endif //DDCU_ABORT_ON_MEMORY_OVERWRITE
}
#endif
#ifdef DEALLOCATION_VALUE
memset(pointer, DEALLOCATION_VALUE, operation->size + OVERWRITE_DETECTION_DATA_SIZE);
#endif //DEALLOCATION_VALUE
//
// Check for mismatch operations
//
bool mismatched_release = true;
if (type == DCU_FreeType)
{
mismatched_release = ! ((operation->type == DCU_MallocType) || (operation->type == DCU_CallocType) || (operation->type == DCU_ReallocType));
}
else if (type == DCU_DeleteType)
{
mismatched_release = (operation->type != DCU_NewType);
}
else if (type == DCU_DeleteArrayType)
{
mismatched_release = (operation->type != DCU_NewArrayType);
}
if (mismatched_release)
{
DCU_ConstPointer stack[DCU_STACK_TRACE_SIZE];
DCU_createStackTrace(stack);
DCU_ProblemInfo* problem = DCU_findProblem(&DCU_problems, DCU_MismatchOperationType, operation->stack, stack);
if (!problem)
{
problem = DCU_createProblem();
problem->type = DCU_MismatchOperationType;
memcpy(problem->allocation_stack, operation->stack, DCU_STACK_TRACE_SIZE * sizeof(DCU_ConstPointer));
memcpy(problem->deallocation_stack, stack, DCU_STACK_TRACE_SIZE * sizeof(DCU_ConstPointer));
DCU_addProblemToList(&DCU_problems, problem);
}
problem->count += 1;
}
}
else
{
//
// Releasing unallocated data
//
DCU_ConstPointer stack[DCU_STACK_TRACE_SIZE];
DCU_createStackTrace(stack);
DCU_ProblemInfo* problem = DCU_findProblem(&DCU_problems, DCU_ReleaseUnallocatedType, DCU_null_stack, stack);
if (!problem)
{
problem = DCU_createProblem();
problem->type = DCU_ReleaseUnallocatedType;
memcpy(problem->deallocation_stack, stack, DCU_STACK_TRACE_SIZE * sizeof(DCU_ConstPointer));
DCU_addProblemToList(&DCU_problems, problem);
}
problem->count += 1;
#ifdef DCU_ABORT_ON_RELEASE_NOT_REQUESTED_MEMORY
DCU_abort("Abnormal program termination : 'Release Unallocated Memory'\n");
return;
#endif //DCU_ABORT_ON_RELEASE_NOT_REQUESTED_MEMORY
}
DCU_removeMemory(operation);
}
}
DCU_free(pointer);
}
else // pointer is null
{
#ifdef DCU_C_MEMORY_CHECK
if (type == DCU_FreeType)
{
DCU_MutexScopedLock lock(DCU_mutex);
if (DCU_STATE(DCU_TRACING))
{
DCU_ConstPointer stack[DCU_STACK_TRACE_SIZE];
DCU_createStackTrace(stack);
DCU_ProblemInfo* problem = DCU_findProblem(&DCU_problems, DCU_FreeNullType, DCU_null_stack, stack);
if (!problem)
{
problem = DCU_createProblem();
problem->type = DCU_FreeNullType;
memcpy(problem->deallocation_stack, stack, DCU_STACK_TRACE_SIZE * sizeof(DCU_ConstPointer));
DCU_addProblemToList(&DCU_problems, problem);
}
problem->count += 1;
}
}
#endif //DCU_C_MEMORY_CHECK
}
// DCU_write(" Done\n");
}
void DCU_analyzeMemory()
{
//
// Memory Stats
//
for (unsigned int i = 0; i != DCU_DYNAMIC_OPERATION_TYPES; ++i)
{
switch (i)
{
case DCU_MallocType:
case DCU_ReallocType:
case DCU_CallocType:
{
DCU_memory_stats_c.count += DCU_memory_stats[i].count;
DCU_memory_stats_c.total_memory += DCU_memory_stats[i].total_memory;
}
break;
case DCU_FreeType:
{
DCU_memory_stats_c.count -= DCU_memory_stats[i].count;
DCU_memory_stats_c.total_memory -= DCU_memory_stats[i].total_memory;
}
break;
case DCU_NewType:
{
DCU_memory_stats_new.count += DCU_memory_stats[i].count;
DCU_memory_stats_new.total_memory += DCU_memory_stats[i].total_memory;
}
break;
case DCU_DeleteType:
{
DCU_memory_stats_new.count -= DCU_memory_stats[i].count;
DCU_memory_stats_new.total_memory -= DCU_memory_stats[i].total_memory;
}
break;
case DCU_NewArrayType:
{
DCU_memory_stats_new_array.count += DCU_memory_stats[i].count;
DCU_memory_stats_new_array.total_memory += DCU_memory_stats[i].total_memory;
}
break;
case DCU_DeleteArrayType:
{
DCU_memory_stats_new_array.count -= DCU_memory_stats[i].count;
DCU_memory_stats_new_array.total_memory -= DCU_memory_stats[i].total_memory;
}
break;
default:
break;
}
}
//
// Detect Memory Leaks
//
for (HastIterator hash_index = 0; hash_index != DCU_HASH_TABLE_SIZE; ++hash_index)
{
DCU_OperationInfo* iterator = DCU_memory[hash_index];
while(iterator)
{
DCU_ProblemInfo* problem = DCU_findProblem(&DCU_problems, DCU_LeakType, iterator->stack, DCU_null_stack);
if (!problem)
{
problem = DCU_createProblem();
problem->type = DCU_LeakType;
memcpy(problem->allocation_stack, iterator->stack, DCU_STACK_TRACE_SIZE * sizeof(DCU_ConstPointer));
DCU_addProblemToList(&DCU_problems, problem);
}
problem->count += 1;
problem->size = iterator->size;
problem->total_memory += iterator->size;
iterator = iterator->next;
}
}
}
void DCU_reportMemoryStatus()
{
DCU_write("DynamicCheckUp Memory Report\n");
DCU_write("----------------------------------------------------------------\n");
DCU_write("%15s %15s %15s %15s\n", "->", "operations", "total mem", "max value");
for (unsigned int i = 0; i != DCU_DYNAMIC_OPERATION_TYPES; ++i)
{
#ifndef DCU_C_MEMORY_CHECK
if (i < (unsigned int)(DCU_NewType))
{
continue;
}
#endif //DCU_C_MEMORY_CHECK
DCU_write("%15s %15d %15d %15d\n",
DCU_OperationTypeNames[i],
DCU_memory_stats[i].count, DCU_memory_stats[i].total_memory, DCU_memory_stats[i].max_value);
}
DCU_write("\nDynamic Memory Balance\n");
DCU_write("----------------------------------------------------------------\n");
#ifdef DCU_C_MEMORY_CHECK
DCU_write("%15s %15d %15d\n", "C Memory", DCU_memory_stats_c.count, DCU_memory_stats_c.total_memory);
#endif //DCU_C_MEMORY_CHECK
DCU_write("%15s %15d %15d\n", "New Del", DCU_memory_stats_new.count, DCU_memory_stats_new.total_memory);
DCU_write("%15s %15d %15d\n", "New Del[]", DCU_memory_stats_new_array.count, DCU_memory_stats_new_array.total_memory);
DCU_write("\nProblems\n");
DCU_write("----------------------------------------------------------------\n");
DCU_ProblemInfo* iterator = DCU_problems;
while (iterator)
{
bool needs_allocation_stack = false;
bool needs_deallocation_stack = false;
DCU_write("{\n");
DCU_write("[%d] %s\n", iterator->type, DCU_ProblemTypenames[ iterator->type ]);
DCU_write("Count: %d\n", iterator->count);
if (iterator->type == DCU_LeakType)
{
DCU_write("Total Memory Lost: %d \n", iterator->total_memory);
needs_allocation_stack = true;
}
if ((iterator->type == DCU_RequestZeroMemoryType) ||
(iterator->type == DCU_MismatchOperationType) ||
(iterator->type == DCU_MemoryOverWriteType))
{
needs_allocation_stack = true;
}
if ((iterator->type == DCU_FreeNullType) ||
(iterator->type == DCU_MismatchOperationType) ||
(iterator->type == DCU_ReleaseUnallocatedType) ||
(iterator->type == DCU_MemoryOverWriteType))
{
needs_deallocation_stack = true;
}
if (needs_allocation_stack)
{
DCU_write("Allocation Stack: ");
for (unsigned int frame = 0; frame != DCU_STACK_TRACE_SIZE; ++frame)
{
if (iterator->allocation_stack[frame])
{
DCU_write("%p ", iterator->allocation_stack[frame]);
}
}
DCU_write("\n");
}
if (needs_deallocation_stack)
{
DCU_write("Deallocation Stack: ");
for (unsigned int frame = 0; frame != DCU_STACK_TRACE_SIZE; ++frame)
{
if (iterator->deallocation_stack[frame])
{
DCU_write("%p ", iterator->deallocation_stack[frame]);
}
}
DCU_write("\n");
}
DCU_write("}\n");
iterator = iterator->next;
}
}
void DCU_abort(char const* message, ...)
{
va_list argp;
va_start(argp, message);
#ifdef DCU_ECHO
if (DCU_stream != DCU_FALLBACK_STREAM)
{
vfprintf(DCU_FALLBACK_STREAM, message, argp);
}
#endif //DDCU_ECHO
vfprintf(DCU_stream, message, argp);
va_end(argp);
DCU_shutdown();
kill(getpid(), SIGKILL);
// just in case someone catches kill
_exit(-1);
}
void DCU_write(char const* message, ...)
{
va_list argp;
va_start(argp, message);
#ifdef DCU_ECHO
if (DCU_stream != DCU_FALLBACK_STREAM)
{
vfprintf(DCU_FALLBACK_STREAM, message, argp);
}
#endif //DDCU_ECHO
vfprintf(DCU_stream, message, argp);
va_end(argp);
}
DCU_OperationInfo* DCU_createOperation()
{
DCU_OperationInfo* element = (DCU_OperationInfo*) DCU_malloc( sizeof(DCU_OperationInfo) );
memset(element, 0,sizeof(DCU_OperationInfo) );
return element;
}
DCU_ProblemInfo* DCU_createProblem()
{
DCU_ProblemInfo* element = (DCU_ProblemInfo*) DCU_malloc( sizeof(DCU_ProblemInfo) );
memset(element, 0,sizeof(DCU_ProblemInfo) );
return element;
}
//
// Operation management
//
void DCU_emptyOperationList(DCU_OperationInfo** list)
{
DCU_OperationInfo* remove = 0;
while (*list)
{
remove = *list;
*list = (*list)->next;
DCU_free(remove);
}
}
void DCU_removeOperationFromList(DCU_OperationInfo** list, DCU_OperationInfo* element)
{
if (element)
{
DCU_OperationInfo* iterator = *list;
if (iterator == element) // is the first element?
{
*list = element->next;
DCU_free(element);
}
else
{
while (iterator && (iterator->next != element))
{
iterator = iterator->next;
}
if (iterator)
{
iterator->next = element->next;
DCU_free(element);
}
}
}
}
DCU_OperationInfo* DCU_findOperationOnList(DCU_OperationInfo* list, DCU_ConstPointer memory_address)
{
DCU_OperationInfo* iterator = list;
while( iterator && (iterator->memory_address != memory_address) )
{
iterator = iterator->next;
}
return iterator;
}
void DCU_addOperationToList(DCU_OperationInfo** list, DCU_OperationInfo* element)
{
element->next = *list;
*list = element;
}
inline void DCU_addMemory(DCU_OperationInfo* element)
{
if (element)
{
ptrdiff_t hash_table_index = DCU_HASH_FUNCTION(element->memory_address);
DCU_addOperationToList(&DCU_memory[hash_table_index], element);
}
}
inline DCU_OperationInfo* DCU_findMemory(DCU_ConstPointer memory_address)
{
HastIterator hash_table_index = DCU_HASH_FUNCTION(memory_address);
return DCU_findOperationOnList(DCU_memory[hash_table_index], memory_address);
}
inline void DCU_removeMemory(DCU_OperationInfo* element)
{
if (element)
{
HastIterator hash_table_index = DCU_HASH_FUNCTION(element->memory_address);
DCU_removeOperationFromList(&DCU_memory[hash_table_index], element);
}
}
inline void DCU_emptyMemory()
{
for (HastIterator i = 0; i != DCU_HASH_TABLE_SIZE; ++i)
{
DCU_emptyOperationList(&DCU_memory[i]);
}
}
//
// Generic DCU_ProblemInfo Linked-List Management
//
void DCU_emptyProblemList(DCU_ProblemInfo** list)
{
DCU_ProblemInfo* remove = 0;
while (*list)
{
remove = *list;
*list = (*list)->next;
DCU_free(remove);
}
}