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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
|
// LeakFinder.cpp
// Finds memory leaks rather effectively
// _X: downloaded from http://www.codeproject.com/Articles/3134/Memory-Leak-and-Exception-Trace-CRT-and-COM-Leaks - the real link is in the comments, RC11 version
/**********************************************************************
*
* LEAKFINDER.CPP
*
*
*
* History:
* 2010-04-15 RC10 - Updated to VC10 RTM
* Fixed Bug: Application Verifier, thanks to handsinmypocket!
* http://www.codeproject.com/KB/applications/leakfinder.aspx?msg=3439751#xx3439751xx
* 2008-08-04 RC6 - Updated to VC9 RTM
* Fixed Bug: Missing "ole32.lib" LIB
* http://www.codeproject.com/KB/applications/leakfinder.aspx?msg=2253980#xx2253980xx
* Fixed Bug: Compiled with "WIN32_LEAN_AND_MEAN"
* http://www.codeproject.com/KB/applications/leakfinder.aspx?msg=1824718#xx1824718xx
* Fixed Bug: Compiling with "/Wall"
* http://www.codeproject.com/KB/threads/StackWalker.aspx?msg=2638243#xx2638243xx
* Removed "#pragma init_seg (compiler)" from h-file
*
* 2005-12-30 RC5 - Now again VC8 RTM compatible
* - Added Xml-Output (like in the old Leakfinder)
* YOu need to define XML_LEAK_FINDER to activate it
* So you can use the LeakAnalyseTool from
* http://www.codeproject.com/tools/leakfinder.asp
*
* 2005-12-13 RC4 - Merged with the new "StackWalker"-project on
* http://www.codeproject.com/threads/StackWalker.asp
*
* 2005-08-01 RC3 - Merged with the new "StackWalker"-project on
* http://www.codeproject.com/threads/StackWalker.asp
*
* 2005-07-05 RC2 - First version with x86, IA64 and x64 support
*
* 2005-07-04 RC1 - Added "OutputOptions"
* - New define "INIT_LEAK_FINDER_VERBOSE" to
* display more info (for error reporting)
*
* 2005-07-01 Beta3 - Workaround for a bug in the new dbghelp.dll
* (version 6.5.3.7 from 2005-05-30; StakWalk64 no
* refused to produce an callstack on x86 systems
* if the context is NULL or has some registers set
* to 0 (for example Esp). This is against the
* documented behaviour of StackWalk64...)
* - First version with x64-support
*
* 2005-06-16 Beta1 First public release with the following features:
* - Completely rewritten in C++ (object oriented)
* - CRT-Leak-Report
* - COM-Leak-Report
* - Report is done via "OutputDebugString" so
* the line can directly selected in the debugger
* and is opening the corresponding file/line of
* the allocation
* - Tried to support x64 systems, bud had some
* trouble wih StackWalk64
* See: http://blog.kalmbachnet.de/?postid=43
*
* LICENSE (http://www.opensource.org/licenses/bsd-license.php)
*
* Copyright (c) 2005-2010, Jochen Kalmbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 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.
* Neither the name of Jochen Kalmbach nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.
*
**********************************************************************/
#include <windows.h>
#include <objidl.h> // Needed if compiled with "WIN32_LEAN_AND_MEAN"
#include <tchar.h>
#include <crtdbg.h>
#include <stdio.h>
#include <string>
#include <vector>
#include "LeakFinder.h"
// Currently only tested with MS VC++ 5 to 10
#if (_MSC_VER < 1100) || (_MSC_VER > 1700)
#error Only MS VC++ 5/6/7/7.1/8/9 supported. Check if the '_CrtMemBlockHeader' has not changed with this compiler!
#endif
/* _X: MSVC 2012 (MSC 1700) seems to use a different allocation scheme for STL containers,
* allocating lots of small objects and running out of memory very soon
* Thus for MSVC 2012 we cut the callstack buffer length in half
*/
// Controlling the callstack depth
#if (_MSC_VER < 1700)
#define MAX_CALLSTACK_LEN_BUF 0x2000
#else
#define MAX_CALLSTACK_LEN_BUF 0x1000
#endif
#define IGNORE_CRT_ALLOC
// disable 64-bit compatibility-checks (because we explicite have here either x86 or x64!)
#pragma warning(disable:4312) // warning C4312: 'type cast' : conversion from 'DWORD' to 'LPCVOID' of greater size
#pragma warning(disable:4826)
// secure-CRT_functions are only available starting with VC8
#if _MSC_VER < 1400
#define _snprintf_s _snprintf
#define _tcscat_s _tcscat
#endif
static std::string SimpleXMLEncode(LPCSTR szText)
{
std::string szRet;
for (size_t i=0; i<strlen(szText); i++)
{
switch(szText[i])
{
case '&':
szRet.append("&");
break;
case '<':
szRet.append("<");
break;
case '>':
szRet.append(">");
break;
case '"':
szRet.append(""");
break;
case '\'':
szRet.append("'");
break;
default:
szRet += szText[i];
}
}
return szRet;
}
LeakFinderOutput::LeakFinderOutput(int options, LPCSTR szSymPath)
: StackWalker(options, szSymPath)
{
}
void LeakFinderOutput::OnLeakSearchStart(LPCSTR szLeakFinderName)
{
CHAR buffer[1024];
_snprintf_s(buffer, 1024, "######## %s ########\n", szLeakFinderName);
this->OnOutput(buffer);
}
void LeakFinderOutput::OnLeakStartEntry(LPCSTR szKeyName, SIZE_T nDataSize)
{
CHAR buffer[1024];
_snprintf_s(buffer, 1024, "--------------- Key: %s, %d bytes ---------\n", szKeyName, nDataSize);
this->OnOutput(buffer);
}
void LeakFinderOutput::OnCallstackEntry(CallstackEntryType eType, CallstackEntry &entry)
{
if ( (eType != lastEntry) && (entry.offset != 0) )
{
if ( ((this->m_options & LeakFinderShowCompleteCallstack) == 0) && (
(strstr(entry.lineFileName, "afxmem.cpp") != NULL) ||
(strstr(entry.lineFileName, "dbgheap.c") != NULL) ||
(strstr(entry.lineFileName, "new.cpp") != NULL) ||
(strstr(entry.lineFileName, "newop.cpp") != NULL) ||
(strstr(entry.lineFileName, "leakfinder.cpp") != NULL) ||
(strstr(entry.lineFileName, "stackwalker.cpp") != NULL)
) )
{
return;
}
}
StackWalker::OnCallstackEntry(eType, entry);
}
// ####################################################################
// XML-Output
LeakFinderXmlOutput::LeakFinderXmlOutput()
{
TCHAR szXMLFileName[1024];
GetModuleFileName(NULL, szXMLFileName, sizeof(szXMLFileName) / sizeof(TCHAR));
_tcscat_s(szXMLFileName, _T(".mem.xml-leaks"));
#if _MSC_VER < 1400
m_fXmlFile = _tfopen(szXMLFileName, _T("w"));
#else
m_fXmlFile = NULL;
_tfopen_s(&m_fXmlFile, szXMLFileName, _T("w"));
#endif
if (m_fXmlFile != NULL)
{
SYSTEMTIME st;
GetLocalTime(&st);
fprintf(m_fXmlFile, "<MEMREPORT date=\"%.2d/%.2d/%.4d\" time=\"%.2d:%.2d:%.2d\">\n",
st.wMonth, st.wDay, st.wYear,
st.wHour, st.wMinute, st.wSecond);
}
else
{
MessageBox(NULL, _T("Could not open xml-logfile for leakfinder!"), _T("Warning"), MB_ICONHAND);
}
}
LeakFinderXmlOutput::LeakFinderXmlOutput(LPCTSTR szFileName) :
m_Progress(10)
{
#if _MSC_VER < 1400
m_fXmlFile = _tfopen(szFileName, _T("w"));
#else
m_fXmlFile = NULL;
_tfopen_s(&m_fXmlFile, szFileName, _T("w"));
#endif
if (m_fXmlFile == NULL)
{
MessageBox(NULL, _T("Could not open xml-logfile for leakfinder!"), _T("Warning"), MB_ICONHAND);
}
else
{
fprintf(m_fXmlFile, "<MEMREPORT>\n");
}
}
LeakFinderXmlOutput::~LeakFinderXmlOutput()
{
if (m_fXmlFile != NULL)
{
// Write the ending-tags and close the file
fprintf(m_fXmlFile, "</MEMREPORT>\n");
fclose(m_fXmlFile);
}
m_fXmlFile = NULL;
}
void LeakFinderXmlOutput::OnLeakSearchStart(LPCSTR sszLeakFinderName)
{
}
void LeakFinderXmlOutput::OnLeakStartEntry(LPCSTR szKeyName, SIZE_T nDataSize)
{
if (m_fXmlFile != NULL)
{
fprintf(m_fXmlFile, "\t<LEAK requestID=\"%s\" size=\"%d\">\n", SimpleXMLEncode(szKeyName).c_str(), nDataSize);
}
if (--m_Progress == 0)
{
m_Progress = 100;
putc('.', stdout);
}
}
void LeakFinderXmlOutput::OnCallstackEntry(CallstackEntryType eType, CallstackEntry &entry)
{
if (m_fXmlFile != NULL)
{
if (eType != lastEntry)
{
fprintf(m_fXmlFile, "\t\t<STACKENTRY decl=\"%s\" decl_offset=\"%+ld\" ", SimpleXMLEncode(entry.undName).c_str(), entry.offsetFromSmybol);
fprintf(m_fXmlFile, "srcfile=\"%s\" line=\"%d\" line_offset=\"%+ld\" ", SimpleXMLEncode(entry.lineFileName).c_str(), entry.lineNumber, entry.offsetFromLine);
fprintf(m_fXmlFile, "module=\"%s\" base=\"%08lx\" ", SimpleXMLEncode(entry.moduleName).c_str(), entry.baseOfImage);
fprintf(m_fXmlFile, "/>\n");
}
else
{
fprintf(m_fXmlFile, "\t</LEAK>\n");
}
}
}
// ##########################################################################
// ##########################################################################
// ##########################################################################
// Base class for storing contexts in a hashtable
template <typename HASHTABLE_KEY> class ContextHashtableBase
{
public:
ContextHashtableBase(SIZE_T sizeOfHastable, LPCSTR finderName)
{
SIZE_T s = sizeOfHastable*sizeof(AllocHashEntryType);
m_hHeap = HeapCreate(0, 10*1024 + s, 0);
if (m_hHeap == NULL)
throw;
pAllocHashTable = (AllocHashEntryType*) own_malloc(s);
sAllocEntries = sizeOfHastable;
m_finderName = own_strdup(finderName);
}
protected:
virtual ~ContextHashtableBase()
{
if (pAllocHashTable != NULL)
own_free(pAllocHashTable);
pAllocHashTable = NULL;
own_free(m_finderName);
m_finderName = NULL;
if (m_hHeap != NULL)
HeapDestroy(m_hHeap);
}
__inline LPVOID own_malloc(SIZE_T size)
{
return HeapAlloc(m_hHeap, HEAP_ZERO_MEMORY, size);
}
__inline VOID own_free(LPVOID memblock)
{
HeapFree(m_hHeap, 0, memblock);
}
__inline CHAR *own_strdup(const char *str)
{
size_t len = strlen(str)+1;
CHAR *c = (CHAR*)own_malloc(len);
#if _MSC_VER >= 1400
strcpy_s(c, len, str);
#else
strcpy(c, str);
#endif
return c;
}
// Disables this leak-finder
virtual LONG Disable() = 0;
// enables the leak-finder again...
virtual LONG Enable() = 0;
protected:
// Entry for each allocation
typedef struct AllocHashEntryType {
HASHTABLE_KEY key;
SIZE_T nDataSize; // Size of the allocated memory
struct AllocHashEntryType *Next;
CONTEXT c;
PVOID pStackBaseAddr;
SIZE_T nMaxStackSize;
PVOID pCallstackOffset;
SIZE_T nCallstackLen;
char pcCallstackAddr[MAX_CALLSTACK_LEN_BUF]; // min of both values...
} AllocHashEntryType;
protected:
virtual SIZE_T HashFunction(HASHTABLE_KEY &key) = 0;
virtual BOOL IsKeyEmpty(HASHTABLE_KEY &key) = 0;
virtual VOID SetEmptyKey(HASHTABLE_KEY &key) = 0;
virtual VOID GetKeyAsString(HASHTABLE_KEY &key, CHAR *szName, SIZE_T nBufferLen) = 0;
//virtual SIZE_T GetNativeBytes(HASHTABLE_KEY &key, CHAR *szName, SIZE_T nBufferLen) { return 0; }
public:
VOID Insert(HASHTABLE_KEY &key, CONTEXT &context, SIZE_T nDataSize)
{
SIZE_T HashIdx;
AllocHashEntryType *pHashEntry;
// generate hash-value
HashIdx = HashFunction(key);
pHashEntry = &pAllocHashTable[HashIdx];
if (IsKeyEmpty(pHashEntry->key) != FALSE) {
// Entry is empty...
}
else {
// Entry is not empy! make a list of entries for this hash value...
while(pHashEntry->Next != NULL) {
pHashEntry = pHashEntry->Next;
}
pHashEntry->Next = (AllocHashEntryType*) own_malloc(sizeof(AllocHashEntryType));
g_CurrentMemUsage += CRTTable::AllocHashEntryTypeSize;
pHashEntry = pHashEntry->Next;
if (pHashEntry == NULL)
{
// Exhausted the available memory?
return;
}
}
pHashEntry->key = key;
pHashEntry->nDataSize = nDataSize;
pHashEntry->Next = NULL;
#ifdef _M_IX86
pHashEntry->pCallstackOffset = (LPVOID) min(context.Ebp, context.Esp);
#elif _M_X64
pHashEntry->pCallstackOffset = (LPVOID) min(context.Rdi, context.Rsp);
#elif _M_IA64
pHashEntry->pCallstackOffset = (LPVOID) min(context.IntSp, context.RsBSP);
#else
#error "Platform not supported!"
#endif
pHashEntry->c = context;
// Query the max. stack-area:
MEMORY_BASIC_INFORMATION MemBuffer;
if(VirtualQuery((LPCVOID) pHashEntry->pCallstackOffset, &MemBuffer, sizeof(MemBuffer)) > 0)
{
pHashEntry->pStackBaseAddr = MemBuffer.BaseAddress;
pHashEntry->nMaxStackSize = MemBuffer.RegionSize;
}
else
{
pHashEntry->pStackBaseAddr = 0;
pHashEntry->nMaxStackSize = 0;
}
SIZE_T bytesToRead = MAX_CALLSTACK_LEN_BUF;
if (pHashEntry->nMaxStackSize > 0)
{
SIZE_T len = ((SIZE_T) pHashEntry->pStackBaseAddr + pHashEntry->nMaxStackSize) - (SIZE_T)pHashEntry->pCallstackOffset;
bytesToRead = min(len, MAX_CALLSTACK_LEN_BUF);
}
// Now read the callstack:
if (ReadProcessMemory(GetCurrentProcess(), (LPCVOID) pHashEntry->pCallstackOffset, &(pHashEntry->pcCallstackAddr), bytesToRead, &(pHashEntry->nCallstackLen)) == 0)
{
// Could not read memory...
pHashEntry->nCallstackLen = 0;
pHashEntry->pCallstackOffset = 0;
} // read callstack
} // Insert
BOOL Remove(HASHTABLE_KEY &key)
{
SIZE_T HashIdx;
AllocHashEntryType *pHashEntry, *pHashEntryLast;
// get the Hash-Value
HashIdx = HashFunction(key);
pHashEntryLast = NULL;
pHashEntry = &pAllocHashTable[HashIdx];
while(pHashEntry != NULL) {
if (pHashEntry->key == key) {
// release my memory
if (pHashEntryLast == NULL) {
// It is an entry in the table, so do not release this memory
if (pHashEntry->Next == NULL) {
// It was the last entry, so empty the table entry
SetEmptyKey(pAllocHashTable[HashIdx].key);
//memset(&pAllocHashTable[HashIdx], 0, sizeof(pAllocHashTable[HashIdx]));
}
else {
// There are some more entries, so shorten the list
AllocHashEntryType *pTmp = pHashEntry->Next;
*pHashEntry = *(pHashEntry->Next);
own_free(pTmp);
g_CurrentMemUsage -= CRTTable::AllocHashEntryTypeSize;
}
return TRUE;
}
else {
// now, I am in an dynamic allocated entry (it was a collision)
pHashEntryLast->Next = pHashEntry->Next;
own_free(pHashEntry);
g_CurrentMemUsage -= CRTTable::AllocHashEntryTypeSize;
return TRUE;
}
}
pHashEntryLast = pHashEntry;
pHashEntry = pHashEntry->Next;
}
// if we are here, we could not find the RequestID
return FALSE;
}
AllocHashEntryType *Find(HASHTABLE_KEY &key)
{
SIZE_T HashIdx;
AllocHashEntryType *pHashEntry;
// get the Hash-Value
HashIdx = HashFunction(key);
pHashEntry = &pAllocHashTable[HashIdx];
while(pHashEntry != NULL) {
if (pHashEntry->key == key) {
return pHashEntry;
}
pHashEntry = pHashEntry->Next;
}
// entry was not found!
return NULL;
}
// For the followong static-var See comment in "ShowCallstack"...
static BOOL CALLBACK ReadProcessMemoryFromHashEntry64(
HANDLE hProcess, // hProcess must be a pointer to an hash-entry!
DWORD64 lpBaseAddress,
PVOID lpBuffer,
DWORD nSize,
LPDWORD lpNumberOfBytesRead,
LPVOID pUserData // optional data, which was passed in "ShowCallstack"
)
{
*lpNumberOfBytesRead = 0;
AllocHashEntryType *pHashEntry = (AllocHashEntryType*) pUserData;
if (pHashEntry == NULL)
{
return FALSE;
}
if ( ( (DWORD64)lpBaseAddress >= (DWORD64)pHashEntry->pCallstackOffset) && ((DWORD64)lpBaseAddress <= ((DWORD64)pHashEntry->pCallstackOffset+pHashEntry->nCallstackLen)) ) {
// Memory is located in saved Callstack:
// Calculate the offset
DWORD dwOffset = (DWORD) ((DWORD64)lpBaseAddress - (DWORD64)pHashEntry->pCallstackOffset);
DWORD dwSize = __min(nSize, MAX_CALLSTACK_LEN_BUF-dwOffset);
memcpy(lpBuffer, &(pHashEntry->pcCallstackAddr[dwOffset]), dwSize);
*lpNumberOfBytesRead = dwSize;
if (dwSize != nSize)
{
return FALSE;
}
*lpNumberOfBytesRead = nSize;
return TRUE;
}
if (*lpNumberOfBytesRead == 0) // Memory could not be found
{
if ( ( (DWORD64)lpBaseAddress < (DWORD64)pHashEntry->pStackBaseAddr) || ((DWORD64)lpBaseAddress > ((DWORD64)pHashEntry->pStackBaseAddr+pHashEntry->nMaxStackSize)) )
{
// Stackwalking is done by reading the "real memory" (normally this happens when the StackWalk64 tries to read some code)
SIZE_T st = 0;
BOOL bRet = ReadProcessMemory(hProcess, (LPCVOID) lpBaseAddress, lpBuffer, nSize, &st);
*lpNumberOfBytesRead = (DWORD) st;
return bRet;
}
}
return TRUE;
}
VOID ShowLeaks(LeakFinderOutput &leakFinderOutput)
{
SIZE_T ulTemp;
AllocHashEntryType *pHashEntry;
ULONG ulCount = 0;
SIZE_T ulLeaksByte = 0;
leakFinderOutput.OnLeakSearchStart(this->m_finderName);
// Move throu every entry
CHAR keyName[1024];
for(ulTemp = 0; ulTemp < this->sAllocEntries; ulTemp++) {
pHashEntry = &pAllocHashTable[ulTemp];
if (IsKeyEmpty(pHashEntry->key) == FALSE) {
while(pHashEntry != NULL) {
ulCount++;
CONTEXT c;
memcpy(&c, &(pHashEntry->c), sizeof(CONTEXT));
this->GetKeyAsString(pHashEntry->key, keyName, 1024);
leakFinderOutput.OnLeakStartEntry(keyName, pHashEntry->nDataSize);
leakFinderOutput.ShowCallstack(GetCurrentThread(), &c, ReadProcessMemoryFromHashEntry64, pHashEntry);
// Count the number of leaky bytes
ulLeaksByte += pHashEntry->nDataSize;
pHashEntry = pHashEntry->Next;
} // while
}
}
}
AllocHashEntryType *pAllocHashTable;
SIZE_T sAllocEntries;
HANDLE m_hHeap;
LPSTR m_finderName;
bool m_bSupressUselessLines;
}; // template <typename HASHTABLE_KEY> class ContextHashtableBase
// ##########################################################################
// ##########################################################################
// ##########################################################################
// Specialization for CRT-Leaks:
// VC5 has excluded all types in release-builds
#ifdef _DEBUG
// The follwoing is copied from dbgint.h:
// <CRT_INTERNALS>
/*
* For diagnostic purpose, blocks are allocated with extra information and
* stored in a doubly-linked list. This makes all blocks registered with
* how big they are, when they were allocated, and what they are used for.
*/
// forward declaration:
#ifndef _M_CEE_PURE
#define MyAllocHookCallingConvention __cdecl
#endif
#if _MSC_VER >= 1400
#ifdef _M_CEE
#define MyAllocHookCallingConvention __clrcall
#endif
#endif
static int MyAllocHookCallingConvention MyAllocHook(int nAllocType, void *pvData,
size_t nSize, int nBlockUse, long lRequest,
#if _MSC_VER <= 1100 // Special case for VC 5 and before
const char * szFileName,
#else
const unsigned char * szFileName,
#endif
int nLine);
static _CRT_ALLOC_HOOK s_pfnOldCrtAllocHook = NULL;
static LONG s_CrtDisableCount = 0;
static LONG s_lMallocCalled = 0;
class CRTTable : public ContextHashtableBase<LONG>
{
public:
CRTTable() : ContextHashtableBase<LONG>(1021, "CRT-Leaks")
{
// save the previous alloc hook
s_pfnOldCrtAllocHook = _CrtSetAllocHook(MyAllocHook);
}
virtual ~CRTTable()
{
_CrtSetAllocHook(s_pfnOldCrtAllocHook);
}
virtual LONG Disable()
{
return InterlockedIncrement(&s_CrtDisableCount);
}
virtual LONG Enable()
{
return InterlockedDecrement(&s_CrtDisableCount);
}
virtual SIZE_T HashFunction(LONG &key)
{
// I couldn´t find any better and faster
return key % sAllocEntries;
}
virtual BOOL IsKeyEmpty(LONG &key)
{
if (key == 0)
return TRUE;
return FALSE;
}
virtual VOID SetEmptyKey(LONG &key)
{
key = 0;
}
virtual VOID GetKeyAsString(LONG &key, CHAR *szName, SIZE_T nBufferLen)
{
#if _MSC_VER < 1400
_snprintf_s(szName, nBufferLen, "%d", key);
#else
_snprintf_s(szName, nBufferLen, nBufferLen, "%d", key);
#endif
}
static const int AllocHashEntryTypeSize = sizeof(AllocHashEntryType);
protected:
CHAR *m_pBuffer;
SIZE_T m_maxBufferLen;
SIZE_T m_bufferLen;
}; // class CRTTable
#define nNoMansLandSize 4
typedef struct _CrtMemBlockHeader
{
struct _CrtMemBlockHeader * pBlockHeaderNext;
struct _CrtMemBlockHeader * pBlockHeaderPrev;
char * szFileName;
int nLine;
#ifdef _WIN64
/* These items are reversed on Win64 to eliminate gaps in the struct
* and ensure that sizeof(struct)%16 == 0, so 16-byte alignment is
* maintained in the debug heap.
*/
int nBlockUse;
size_t nDataSize;
#else /* _WIN64 */
size_t nDataSize;
int nBlockUse;
#endif /* _WIN64 */
long lRequest;
unsigned char gap[nNoMansLandSize];
/* followed by:
* unsigned char data[nDataSize];
* unsigned char anotherGap[nNoMansLandSize];
*/
} _CrtMemBlockHeader;
#define pbData(pblock) ((unsigned char *)((_CrtMemBlockHeader *)pblock + 1))
#define pHdr(pbData) (((_CrtMemBlockHeader *)pbData)-1)
// </CRT_INTERNALS>
static CRTTable *g_pCRTTable = NULL;
size_t g_CurrentMemUsage = 0;
// MyAllocHook is Single-Threaded, that means the the calls are serialized in the calling function!
static int MyAllocHook(int nAllocType, void *pvData,
size_t nSize, int nBlockUse, long lRequest,
#if _MSC_VER <= 1100 // Special case for VC 5
const char * szFileName,
#else
const unsigned char * szFileName,
#endif
int nLine)
{
//static TCHAR *operation[] = { _T(""), _T("ALLOCATIONG"), _T("RE-ALLOCATING"), _T("FREEING") };
//static TCHAR *blockType[] = { _T("Free"), _T("Normal"), _T("CRT"), _T("Ignore"), _T("Client") };
#ifdef IGNORE_CRT_ALLOC
if (_BLOCK_TYPE(nBlockUse) == _CRT_BLOCK) // Ignore internal C runtime library allocations
return TRUE;
#endif
extern int _crtDbgFlag;
if ( ((_CRTDBG_ALLOC_MEM_DF & _crtDbgFlag) == 0) && ( (nAllocType == _HOOK_ALLOC) || (nAllocType == _HOOK_REALLOC) ) )
{
// Someone has disabled that the runtime should log this allocation
// so we do not log this allocation
if (s_pfnOldCrtAllocHook != NULL)
s_pfnOldCrtAllocHook(nAllocType, pvData, nSize, nBlockUse, lRequest, szFileName, nLine);
return TRUE;
}
// Handle the Disable/Enable setting
if (InterlockedExchangeAdd(&s_CrtDisableCount, 0) != 0)
{
return TRUE;
}
// Prevent from reentrat calls
if (InterlockedIncrement(&s_lMallocCalled) > 1)
{
// I was already called
InterlockedDecrement(&s_lMallocCalled);
// call the previous alloc hook
if (s_pfnOldCrtAllocHook != NULL)
s_pfnOldCrtAllocHook(nAllocType, pvData, nSize, nBlockUse, lRequest, szFileName, nLine);
return TRUE;
}
_ASSERT( (nAllocType == _HOOK_ALLOC) || (nAllocType == _HOOK_REALLOC) || (nAllocType == _HOOK_FREE) );
_ASSERT( ( _BLOCK_TYPE(nBlockUse) >= 0 ) && ( _BLOCK_TYPE(nBlockUse) < 5 ) );
if (nAllocType == _HOOK_FREE)
{
// freeing
// Try to get the header information
if (_CrtIsValidHeapPointer(pvData)) { // it is a valid Heap-Pointer
// get the ID
_CrtMemBlockHeader *pHead;
// get a pointer to memory block header
pHead = pHdr(pvData);
nSize = pHead->nDataSize;
lRequest = pHead->lRequest; // This is the ID!
if (pHead->nBlockUse == _IGNORE_BLOCK)
{
InterlockedDecrement(&s_lMallocCalled);
if (s_pfnOldCrtAllocHook != NULL)
{
s_pfnOldCrtAllocHook(nAllocType, pvData, nSize, nBlockUse, lRequest, szFileName, nLine);
}
return TRUE;
}
}
if (lRequest != 0)
{
// RequestID was found
size_t temp = g_CurrentMemUsage;
g_CurrentMemUsage -= nSize ;
g_pCRTTable->Remove(lRequest);
if (g_CurrentMemUsage > temp)
{
printf("********************************************\n");
printf("** Server detected underflow in memory **\n");
printf("** usage counter. Something is not right. **\n");
printf("** Writing memory dump into memdump.xml **\n");
printf("********************************************\n");
printf("Please wait\n");
LeakFinderXmlOutput Output("memdump.xml");
DumpUsedMemory(&Output);
printf("\nMemory dump complete. Server will now abort.\n");
abort();
}
}
} // freeing
if (nAllocType == _HOOK_REALLOC)
{
// re-allocating
// Try to get the header information
if (_CrtIsValidHeapPointer(pvData)) { // it is a valid Heap-Pointer
BOOL bRet;
LONG lReallocRequest;
// get the ID
_CrtMemBlockHeader *pHead;
// get a pointer to memory block header
pHead = pHdr(pvData);
// Try to find the RequestID in the Hash-Table, mark it that it was freed
lReallocRequest = pHead->lRequest;
size_t temp = g_CurrentMemUsage;
g_CurrentMemUsage -= pHead->nDataSize;
bRet = g_pCRTTable->Remove(lReallocRequest);
if (g_CurrentMemUsage > temp)
{
printf("********************************************\n");
printf("** Server detected underflow in memory **\n");
printf("** usage counter. Something is not right. **\n");
printf("** Writing memory dump into memdump.xml **\n");
printf("********************************************\n");
printf("Please wait\n");
LeakFinderXmlOutput Output("memdump.xml");
DumpUsedMemory(&Output);
printf("\nMemory dump complete. Server will now abort.\n");
abort();
}
} // ValidHeapPointer
} // re-allocating
//if ( (g_ulShowStackAtAlloc < 3) && (nAllocType == _HOOK_FREE) ) {
if (nAllocType == _HOOK_FREE)
{
InterlockedDecrement(&s_lMallocCalled);
// call the previous alloc hook
if (s_pfnOldCrtAllocHook != NULL)
{
s_pfnOldCrtAllocHook(nAllocType, pvData, nSize, nBlockUse, lRequest, szFileName, nLine);
}
return TRUE;
}
CONTEXT c;
GET_CURRENT_CONTEXT(c, CONTEXT_FULL);
// Only insert in the Hash-Table if it is not a "freeing"
if (nAllocType != _HOOK_FREE)
{
if (lRequest != 0) // Always a valid RequestID should be provided (see comments in the header)
{
//No need to check for overflow since we are checking if we are getting higher than 1gb.
//If we change this, then we probably would want an overflow check.
g_CurrentMemUsage += nSize ;
g_pCRTTable->Insert(lRequest, c, nSize);
if (g_CurrentMemUsage > 1536 * 1024* 1024)
{
printf("******************************************\n");
printf("** Server reached 1.5 GiB memory usage, **\n");
printf("** something is probably wrong. **\n");
printf("** Writing memory dump into memdump.xml **\n");
printf("******************************************\n");
printf("Please wait\n");
LeakFinderXmlOutput Output("memdump.xml");
DumpUsedMemory(&Output);
printf("\nMemory dump complete. Server will now abort.\n");
abort();
}
}
}
InterlockedDecrement(&s_lMallocCalled);
// call the previous alloc hook
if (s_pfnOldCrtAllocHook != NULL)
s_pfnOldCrtAllocHook(nAllocType, pvData, nSize, nBlockUse, lRequest, szFileName, nLine);
return TRUE; // allow the memory operation to proceed
} // MyAllocHook
#endif // _DEBUG
// ##########################################################################
// ##########################################################################
// ##########################################################################
// Init/Deinit functions
HRESULT InitLeakFinder()
{
#ifdef _DEBUG
g_pCRTTable = new CRTTable();
#endif
return S_OK;
}
void DumpUsedMemory(LeakFinderOutput * output)
{
LeakFinderOutput *pLeakFinderOutput = output;
#ifdef _DEBUG
g_pCRTTable->Disable();
#endif
if (pLeakFinderOutput == NULL)
{
pLeakFinderOutput = new LeakFinderOutput();
}
// explicitly load the modules:
pLeakFinderOutput->LoadModules();
#ifdef _DEBUG
g_pCRTTable->ShowLeaks(*pLeakFinderOutput);
#endif
if (output == NULL)
{
delete pLeakFinderOutput;
}
}
void DeinitLeakFinder(LeakFinderOutput *output)
{
DumpUsedMemory(output);
#ifdef _DEBUG
delete g_pCRTTable;
g_pCRTTable = NULL;
#endif
}
void DeinitLeakFinder()
{
DeinitLeakFinder(NULL);
}
|