-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathmemvalidate.cpp
More file actions
485 lines (390 loc) · 14 KB
/
memvalidate.cpp
File metadata and controls
485 lines (390 loc) · 14 KB
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Memory allocation!
//
// $NoKeywords: $
//=============================================================================//
#include "pch_tier0.h"
#ifndef STEAM
#ifdef TIER0_VALIDATE_HEAP
#include <malloc.h>
#include "tier0/dbg.h"
#include "tier0/memalloc.h"
#include "mem_helpers.h"
extern IMemAlloc *g_pActualAlloc;
//-----------------------------------------------------------------------------
// NOTE! This should never be called directly from leaf code
// Just use new,delete,malloc,free etc. They will call into this eventually
//-----------------------------------------------------------------------------
class CValidateAlloc : public IMemAlloc
{
public:
enum
{
HEAP_PREFIX_BUFFER_SIZE = 12,
HEAP_SUFFIX_BUFFER_SIZE = 8,
};
CValidateAlloc();
// Release versions
virtual void *Alloc( size_t nSize );
virtual void *Realloc( void *pMem, size_t nSize );
virtual void Free( void *pMem );
virtual void *Expand_NoLongerSupported( void *pMem, size_t nSize );
// Debug versions
virtual void *Alloc( size_t nSize, const char *pFileName, int nLine );
virtual void *Realloc( void *pMem, size_t nSize, const char *pFileName, int nLine );
virtual void Free( void *pMem, const char *pFileName, int nLine );
virtual void *Expand_NoLongerSupported( void *pMem, size_t nSize, const char *pFileName, int nLine );
// Returns size of a particular allocation
virtual size_t GetSize( void *pMem );
// Force file + line information for an allocation
virtual void PushAllocDbgInfo( const char *pFileName, int nLine );
virtual void PopAllocDbgInfo();
virtual long CrtSetBreakAlloc( long lNewBreakAlloc );
virtual int CrtSetReportMode( int nReportType, int nReportMode );
virtual int CrtIsValidHeapPointer( const void *pMem );
virtual int CrtIsValidPointer( const void *pMem, unsigned int size, int access );
virtual int CrtCheckMemory( void );
virtual int CrtSetDbgFlag( int nNewFlag );
virtual void CrtMemCheckpoint( _CrtMemState *pState );
void* CrtSetReportFile( int nRptType, void* hFile );
void* CrtSetReportHook( void* pfnNewHook );
int CrtDbgReport( int nRptType, const char * szFile,
int nLine, const char * szModule, const char * pMsg );
virtual int heapchk();
virtual void DumpStats() {}
virtual void DumpStatsFileBase( char const *pchFileBase ) {}
virtual bool IsDebugHeap()
{
return true;
}
virtual int GetVersion() { return MEMALLOC_VERSION; }
virtual void CompactHeap();
virtual MemAllocFailHandler_t SetAllocFailHandler( MemAllocFailHandler_t pfnMemAllocFailHandler );
virtual uint32 GetDebugInfoSize() { return 0; }
virtual void SaveDebugInfo( void *pvDebugInfo ) { }
virtual void RestoreDebugInfo( const void *pvDebugInfo ) {}
virtual void InitDebugInfo( void *pvDebugInfo, const char *pchRootFileName, int nLine ) {}
private:
struct HeapPrefix_t
{
HeapPrefix_t *m_pPrev;
HeapPrefix_t *m_pNext;
int m_nSize;
unsigned char m_Prefix[HEAP_PREFIX_BUFFER_SIZE];
};
struct HeapSuffix_t
{
unsigned char m_Suffix[HEAP_SUFFIX_BUFFER_SIZE];
};
private:
// Returns the actual debug info
void GetActualDbgInfo( const char *&pFileName, int &nLine );
// Updates stats
void RegisterAllocation( const char *pFileName, int nLine, size_t nLogicalSize, size_t nActualSize, unsigned nTime );
void RegisterDeallocation( const char *pFileName, int nLine, size_t nLogicalSize, size_t nActualSize, unsigned nTime );
HeapSuffix_t *Suffix( HeapPrefix_t *pPrefix );
void *AllocationStart( HeapPrefix_t *pBase );
HeapPrefix_t *PrefixFromAllocation( void *pAlloc );
const HeapPrefix_t *PrefixFromAllocation( const void *pAlloc );
// Add to the list!
void AddToList( HeapPrefix_t *pHeap, int nSize );
// Remove from the list!
void RemoveFromList( HeapPrefix_t *pHeap );
// Validate the allocation
bool ValidateAllocation( HeapPrefix_t *pHeap );
private:
HeapPrefix_t *m_pFirstAllocation;
char m_pPrefixImage[HEAP_PREFIX_BUFFER_SIZE];
char m_pSuffixImage[HEAP_SUFFIX_BUFFER_SIZE];
};
//-----------------------------------------------------------------------------
// Singleton...
//-----------------------------------------------------------------------------
static CValidateAlloc s_ValidateAlloc;
IMemAlloc *g_pMemAlloc = &s_ValidateAlloc;
//-----------------------------------------------------------------------------
// Constructor.
//-----------------------------------------------------------------------------
CValidateAlloc::CValidateAlloc()
{
m_pFirstAllocation = 0;
memset( m_pPrefixImage, 0xBE, HEAP_PREFIX_BUFFER_SIZE );
memset( m_pSuffixImage, 0xAF, HEAP_SUFFIX_BUFFER_SIZE );
}
//-----------------------------------------------------------------------------
// Accessors...
//-----------------------------------------------------------------------------
inline CValidateAlloc::HeapSuffix_t *CValidateAlloc::Suffix( HeapPrefix_t *pPrefix )
{
return reinterpret_cast<HeapSuffix_t *>( (unsigned char*)( pPrefix + 1 ) + pPrefix->m_nSize );
}
inline void *CValidateAlloc::AllocationStart( HeapPrefix_t *pBase )
{
return static_cast<void *>( pBase + 1 );
}
inline CValidateAlloc::HeapPrefix_t *CValidateAlloc::PrefixFromAllocation( void *pAlloc )
{
if ( !pAlloc )
return NULL;
return ((HeapPrefix_t*)pAlloc) - 1;
}
inline const CValidateAlloc::HeapPrefix_t *CValidateAlloc::PrefixFromAllocation( const void *pAlloc )
{
return ((const HeapPrefix_t*)pAlloc) - 1;
}
//-----------------------------------------------------------------------------
// Add to the list!
//-----------------------------------------------------------------------------
void CValidateAlloc::AddToList( HeapPrefix_t *pHeap, int nSize )
{
pHeap->m_pPrev = NULL;
pHeap->m_pNext = m_pFirstAllocation;
if ( m_pFirstAllocation )
{
m_pFirstAllocation->m_pPrev = pHeap;
}
pHeap->m_nSize = nSize;
m_pFirstAllocation = pHeap;
HeapSuffix_t *pSuffix = Suffix( pHeap );
memcpy( pHeap->m_Prefix, m_pPrefixImage, HEAP_PREFIX_BUFFER_SIZE );
memcpy( pSuffix->m_Suffix, m_pSuffixImage, HEAP_SUFFIX_BUFFER_SIZE );
}
//-----------------------------------------------------------------------------
// Remove from the list!
//-----------------------------------------------------------------------------
void CValidateAlloc::RemoveFromList( HeapPrefix_t *pHeap )
{
if ( !pHeap )
return;
ValidateAllocation( pHeap );
if ( pHeap->m_pPrev )
{
pHeap->m_pPrev->m_pNext = pHeap->m_pNext;
}
else
{
m_pFirstAllocation = pHeap->m_pNext;
}
if ( pHeap->m_pNext )
{
pHeap->m_pNext->m_pPrev = pHeap->m_pPrev;
}
}
//-----------------------------------------------------------------------------
// Validate the allocation
//-----------------------------------------------------------------------------
bool CValidateAlloc::ValidateAllocation( HeapPrefix_t *pHeap )
{
HeapSuffix_t *pSuffix = Suffix( pHeap );
bool bOk = true;
if ( memcmp( pHeap->m_Prefix, m_pPrefixImage, HEAP_PREFIX_BUFFER_SIZE ) )
{
bOk = false;
}
if ( memcmp( pSuffix->m_Suffix, m_pSuffixImage, HEAP_SUFFIX_BUFFER_SIZE ) )
{
bOk = false;
}
if ( !bOk )
{
Warning("Memory trash detected in allocation %X!\n", (void*)(pHeap+1) );
Assert( 0 );
}
return bOk;
}
//-----------------------------------------------------------------------------
// Release versions
//-----------------------------------------------------------------------------
void *CValidateAlloc::Alloc( size_t nSize )
{
Assert( heapchk() == _HEAPOK );
Assert( CrtCheckMemory() );
int nActualSize = nSize + sizeof(HeapPrefix_t) + sizeof(HeapSuffix_t);
HeapPrefix_t *pHeap = (HeapPrefix_t*)g_pActualAlloc->Alloc( nActualSize );
AddToList( pHeap, nSize );
return AllocationStart( pHeap );
}
void *CValidateAlloc::Realloc( void *pMem, size_t nSize )
{
Assert( heapchk() == _HEAPOK );
Assert( CrtCheckMemory() );
HeapPrefix_t *pHeap = PrefixFromAllocation( pMem );
RemoveFromList( pHeap );
int nActualSize = nSize + sizeof(HeapPrefix_t) + sizeof(HeapSuffix_t);
pHeap = (HeapPrefix_t*)g_pActualAlloc->Realloc( pHeap, nActualSize );
AddToList( pHeap, nSize );
return AllocationStart( pHeap );
}
void CValidateAlloc::Free( void *pMem )
{
Assert( heapchk() == _HEAPOK );
Assert( CrtCheckMemory() );
HeapPrefix_t *pHeap = PrefixFromAllocation( pMem );
RemoveFromList( pHeap );
g_pActualAlloc->Free( pHeap );
}
void *CValidateAlloc::Expand_NoLongerSupported( void *pMem, size_t nSize )
{
Assert( heapchk() == _HEAPOK );
Assert( CrtCheckMemory() );
HeapPrefix_t *pHeap = PrefixFromAllocation( pMem );
RemoveFromList( pHeap );
int nActualSize = nSize + sizeof(HeapPrefix_t) + sizeof(HeapSuffix_t);
pHeap = (HeapPrefix_t*)g_pActualAlloc->Expand_NoLongerSupported( pHeap, nActualSize );
AddToList( pHeap, nSize );
return AllocationStart( pHeap );
}
//-----------------------------------------------------------------------------
// Debug versions
//-----------------------------------------------------------------------------
void *CValidateAlloc::Alloc( size_t nSize, const char *pFileName, int nLine )
{
Assert( heapchk() == _HEAPOK );
Assert( CrtCheckMemory() );
int nActualSize = nSize + sizeof(HeapPrefix_t) + sizeof(HeapSuffix_t);
HeapPrefix_t *pHeap = (HeapPrefix_t*)g_pActualAlloc->Alloc( nActualSize, pFileName, nLine );
AddToList( pHeap, nSize );
return AllocationStart( pHeap );
}
void *CValidateAlloc::Realloc( void *pMem, size_t nSize, const char *pFileName, int nLine )
{
Assert( heapchk() == _HEAPOK );
Assert( CrtCheckMemory() );
HeapPrefix_t *pHeap = PrefixFromAllocation( pMem );
RemoveFromList( pHeap );
int nActualSize = nSize + sizeof(HeapPrefix_t) + sizeof(HeapSuffix_t);
pHeap = (HeapPrefix_t*)g_pActualAlloc->Realloc( pHeap, nActualSize, pFileName, nLine );
AddToList( pHeap, nSize );
return AllocationStart( pHeap );
}
void CValidateAlloc::Free( void *pMem, const char *pFileName, int nLine )
{
Assert( heapchk() == _HEAPOK );
Assert( CrtCheckMemory() );
HeapPrefix_t *pHeap = PrefixFromAllocation( pMem );
RemoveFromList( pHeap );
g_pActualAlloc->Free( pHeap, pFileName, nLine );
}
void *CValidateAlloc::Expand_NoLongerSupported( void *pMem, size_t nSize, const char *pFileName, int nLine )
{
Assert( heapchk() == _HEAPOK );
Assert( CrtCheckMemory() );
HeapPrefix_t *pHeap = PrefixFromAllocation( pMem );
RemoveFromList( pHeap );
int nActualSize = nSize + sizeof(HeapPrefix_t) + sizeof(HeapSuffix_t);
pHeap = (HeapPrefix_t*)g_pActualAlloc->Expand_NoLongerSupported( pHeap, nActualSize, pFileName, nLine );
AddToList( pHeap, nSize );
return AllocationStart( pHeap );
}
//-----------------------------------------------------------------------------
// Returns size of a particular allocation
//-----------------------------------------------------------------------------
size_t CValidateAlloc::GetSize( void *pMem )
{
if ( !pMem )
return CalcHeapUsed();
HeapPrefix_t *pHeap = PrefixFromAllocation( pMem );
return pHeap->m_nSize;
}
//-----------------------------------------------------------------------------
// Force file + line information for an allocation
//-----------------------------------------------------------------------------
void CValidateAlloc::PushAllocDbgInfo( const char *pFileName, int nLine )
{
g_pActualAlloc->PushAllocDbgInfo( pFileName, nLine );
}
void CValidateAlloc::PopAllocDbgInfo()
{
g_pActualAlloc->PopAllocDbgInfo( );
}
//-----------------------------------------------------------------------------
// FIXME: Remove when we make our own heap! Crt stuff we're currently using
//-----------------------------------------------------------------------------
long CValidateAlloc::CrtSetBreakAlloc( long lNewBreakAlloc )
{
return g_pActualAlloc->CrtSetBreakAlloc( lNewBreakAlloc );
}
int CValidateAlloc::CrtSetReportMode( int nReportType, int nReportMode )
{
return g_pActualAlloc->CrtSetReportMode( nReportType, nReportMode );
}
int CValidateAlloc::CrtIsValidHeapPointer( const void *pMem )
{
const HeapPrefix_t *pHeap = PrefixFromAllocation( pMem );
return g_pActualAlloc->CrtIsValidHeapPointer( pHeap );
}
int CValidateAlloc::CrtIsValidPointer( const void *pMem, unsigned int size, int access )
{
const HeapPrefix_t *pHeap = PrefixFromAllocation( pMem );
return g_pActualAlloc->CrtIsValidPointer( pHeap, size, access );
}
int CValidateAlloc::CrtCheckMemory( void )
{
return g_pActualAlloc->CrtCheckMemory( );
}
int CValidateAlloc::CrtSetDbgFlag( int nNewFlag )
{
return g_pActualAlloc->CrtSetDbgFlag( nNewFlag );
}
void CValidateAlloc::CrtMemCheckpoint( _CrtMemState *pState )
{
g_pActualAlloc->CrtMemCheckpoint( pState );
}
void* CValidateAlloc::CrtSetReportFile( int nRptType, void* hFile )
{
return g_pActualAlloc->CrtSetReportFile( nRptType, hFile );
}
void* CValidateAlloc::CrtSetReportHook( void* pfnNewHook )
{
return g_pActualAlloc->CrtSetReportHook( pfnNewHook );
}
int CValidateAlloc::CrtDbgReport( int nRptType, const char * szFile,
int nLine, const char * szModule, const char * pMsg )
{
return g_pActualAlloc->CrtDbgReport( nRptType, szFile, nLine, szModule, pMsg );
}
int CValidateAlloc::heapchk()
{
bool bOk = true;
// Validate the heap
HeapPrefix_t *pHeap = m_pFirstAllocation;
for( pHeap = m_pFirstAllocation; pHeap; pHeap = pHeap->m_pNext )
{
if ( !ValidateAllocation( pHeap ) )
{
bOk = false;
}
}
#ifdef _WIN32
return bOk ? _HEAPOK : 0;
#elif POSIX
return bOk;
#else
#error
#endif
}
// Returns the actual debug info
void CValidateAlloc::GetActualDbgInfo( const char *&pFileName, int &nLine )
{
g_pActualAlloc->GetActualDbgInfo( pFileName, nLine );
}
// Updates stats
void CValidateAlloc::RegisterAllocation( const char *pFileName, int nLine, size_t nLogicalSize, size_t nActualSize, unsigned nTime )
{
g_pActualAlloc->RegisterAllocation( pFileName, nLine, nLogicalSize, nActualSize, nTime );
}
void CValidateAlloc::RegisterDeallocation( const char *pFileName, int nLine, size_t nLogicalSize, size_t nActualSize, unsigned nTime )
{
g_pActualAlloc->RegisterDeallocation( pFileName, nLine, nLogicalSize, nActualSize, nTime );
}
void CValidateAlloc::CompactHeap()
{
g_pActualAlloc->CompactHeap();
}
MemAllocFailHandler_t CValidateAlloc::SetAllocFailHandler( MemAllocFailHandler_t pfnMemAllocFailHandler )
{
return g_pActualAlloc->SetAllocFailHandler( pfnMemAllocFailHandler );
}
#endif // TIER0_VALIDATE_HEAP
#endif // STEAM