-
Notifications
You must be signed in to change notification settings - Fork 25
/
heob-inj.c
5293 lines (4422 loc) · 129 KB
/
heob-inj.c
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
// Copyright Hannes Domani 2014 - 2024.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// includes {{{
#include "heob-internal.h"
#include <stdint.h>
// }}}
// defines {{{
#define SPLIT_MASK 0x3fff
#define CAPTURE_STACK_TRACE( skip,capture,frames,caller,maxFrames ) \
do { \
void **frames_ = frames; \
int ptrs_ = CaptureStackBackTrace( \
skip,min((maxFrames)-(skip),capture),frames_,NULL ); \
if( !ptrs_ ) frames_[ptrs_++] = caller; \
if( ptrs_<(capture) ) RtlZeroMemory( \
frames_+ptrs_,((capture)-ptrs_)*sizeof(void*) ); \
} while( 0 )
#define ERRNO_NOMEM 12
#define ERRNO_INVAL 22
// }}}
// local data {{{
typedef struct
{
CRITICAL_SECTION cs;
allocation *alloc_a;
int alloc_q;
int alloc_s;
}
splitAllocation;
typedef struct
{
allocation a;
void *frames[PTRS];
#ifndef NO_THREADS
int threadNum;
#endif
}
freed;
typedef struct
{
CRITICAL_SECTION cs;
freed *freed_a;
int freed_q;
int freed_s;
}
splitFreed;
typedef struct
{
const void **start;
const void **end;
}
modMemType;
typedef struct localData
{
func_LoadLibraryA *fLoadLibraryA;
func_LoadLibraryW *fLoadLibraryW;
func_LoadLibraryExA *fLoadLibraryExA;
func_LoadLibraryExW *fLoadLibraryExW;
func_FreeLibrary *fFreeLibrary;
func_GetProcAddress *fGetProcAddress;
func_ExitProcess *fExitProcess;
func_TerminateProcess *fTerminateProcess;
func_FreeLibraryAndExitThread *fFreeLibraryAndExitThread;
func_CreateProcessA *fCreateProcessA;
func_CreateProcessW *fCreateProcessW;
#ifndef NO_THREADS
func_RaiseException *fRaiseException;
func_GetThreadDescription *fGetThreadDescription;
#endif
func_NtQueryInformationThread *fNtQueryInformationThread;
func_signal *fsignal;
func_malloc *fmalloc;
func_calloc *fcalloc;
func_free *ffree;
func_realloc *frealloc;
func_strdup *fstrdup;
func_wcsdup *fwcsdup;
func_malloc *fop_new;
func_free *fop_delete;
func_malloc *fop_new_a;
func_free *fop_delete_a;
func_getcwd *fgetcwd;
func_wgetcwd *fwgetcwd;
func_getdcwd *fgetdcwd;
func_wgetdcwd *fwgetdcwd;
func_fullpath *ffullpath;
func_wfullpath *fwfullpath;
func_tempnam *ftempnam;
func_wtempnam *fwtempnam;
func_free_dbg *ffree_dbg;
func_recalloc *frecalloc;
func_free *ofree;
func_free *oop_delete;
func_free *oop_delete_a;
func_getcwd *ogetcwd;
func_wgetcwd *owgetcwd;
func_getdcwd *ogetdcwd;
func_wgetdcwd *owgetdcwd;
func_fullpath *ofullpath;
func_wfullpath *owfullpath;
func_tempnam *otempnam;
func_wtempnam *owtempnam;
func_errno *oerrno;
int is_cygwin;
HANDLE controlPipe;
HANDLE exceptionWait;
#ifndef NO_DBGHELP
HANDLE miniDumpWait;
#endif
#if USE_STACKWALK
HANDLE heobProcess;
HANDLE samplingStop;
int noStackWalk;
#endif
HMODULE heobMod;
HMODULE kernel32;
HMODULE msvcrt;
HMODULE ucrtbase;
// SIGSEGV handler set by signal(), returned by the next call
void *crtSigSegvHandler;
splitAllocation *splits;
int ptrShift;
allocType newArrAllocMethod;
splitFreed *freeds;
HANDLE heap;
DWORD pageSize;
size_t pageAdd;
HANDLE crtHeap;
int processors;
exceptionInfo *ei;
int maxStackFrames;
int noCRT;
allocation *exitTrace;
UINT exitCode;
#ifndef NO_THREADS
DWORD threadNumTls;
#endif
DWORD freeSizeTls;
options opt;
options globalopt;
wchar_t *specificOptions;
DWORD appCounterID;
uint64_t slackInit64;
int recording;
wchar_t *subOutName;
wchar_t *subXmlName;
wchar_t *subSvgName;
wchar_t *subCurDir;
wchar_t *subSymPath;
CRITICAL_SECTION csMod;
CRITICAL_SECTION csAllocId;
CRITICAL_SECTION csWrite;
CRITICAL_SECTION csFreedMod;
#ifndef NO_THREADS
CRITICAL_SECTION csThreadNum;
#endif
// protected by csMod {{{
HMODULE *mod_a;
int mod_q;
int mod_s;
int mod_d;
HMODULE *crt_mod_a;
int crt_mod_q;
int crt_mod_s;
modMemType *mod_mem_a;
int mod_mem_q;
int mod_mem_s;
// }}}
// protected by csAllocId {{{
size_t cur_id;
size_t raise_id;
size_t *raise_alloc_a;
// }}}
// protected by csWrite {{{
HANDLE master;
// }}}
// protected by csFreedMod {{{
HMODULE *freed_mod_a;
int freed_mod_q;
int freed_mod_s;
int inExit;
// }}}
// protected by csThreadNum {{{
#ifndef NO_THREADS
int threadNum;
#endif
// }}}
}
localData;
static localData g_ld;
#define GET_REMOTEDATA( ld ) localData *ld = &g_ld
// }}}
// function prototypes {{{
static void addModule( HMODULE mod );
static void replaceModFuncs( void );
#ifndef NO_THREADS
static void writeThreadDescs( void );
#endif
// }}}
// process exit {{{
static NORETURN void exitWait( UINT c,int terminate )
{
GET_REMOTEDATA( rd );
if( terminate || rd->opt.dlls!=4 )
{
EnterCriticalSection( &rd->csWrite );
FlushFileBuffers( rd->master );
CloseHandle( rd->master );
rd->master = INVALID_HANDLE_VALUE;
LeaveCriticalSection( &rd->csWrite );
}
#if USE_STACKWALK
if( rd->heobProcess )
{
CloseHandle( rd->heobProcess );
rd->heobProcess = INVALID_HANDLE_VALUE;
}
#endif
rd->opt.raiseException = 0;
if( terminate<2 && rd->opt.newConsole&1 )
{
HANDLE in = GetStdHandle( STD_INPUT_HANDLE );
if( FlushConsoleInputBuffer(in) )
{
HANDLE out = GetStdHandle( STD_OUTPUT_HANDLE );
DWORD written;
const char *exitText =
"\n\n-------------------- APPLICATION EXIT --------------------\n";
WriteFile( out,exitText,lstrlen(exitText),&written,NULL );
STARTUPINFOW si;
RtlZeroMemory( &si,sizeof(STARTUPINFOW) );
si.cb = sizeof(STARTUPINFOW);
PROCESS_INFORMATION pi;
RtlZeroMemory( &pi,sizeof(PROCESS_INFORMATION) );
wchar_t pause[] = L"cmd /c pause";
if( rd->fCreateProcessW(NULL,pause,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi) )
{
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
else
{
INPUT_RECORD ir;
DWORD didread;
while( ReadConsoleInput(in,&ir,1,&didread) &&
(ir.EventType!=KEY_EVENT || !ir.Event.KeyEvent.bKeyDown ||
ir.Event.KeyEvent.wVirtualKeyCode==VK_SHIFT ||
ir.Event.KeyEvent.wVirtualKeyCode==VK_CAPITAL ||
ir.Event.KeyEvent.wVirtualKeyCode==VK_CONTROL ||
ir.Event.KeyEvent.wVirtualKeyCode==VK_MENU ||
ir.Event.KeyEvent.wVirtualKeyCode==VK_LWIN ||
ir.Event.KeyEvent.wVirtualKeyCode==VK_RWIN) );
}
}
}
if( !terminate )
rd->fExitProcess( c );
else
rd->fTerminateProcess( GetCurrentProcess(),c );
UNREACHABLE;
}
// }}}
// utility functions {{{
static NORETURN void exitOutOfMemory( int needLock )
{
GET_REMOTEDATA( rd );
if( needLock )
EnterCriticalSection( &rd->csWrite );
DWORD written;
int type = WRITE_MAIN_ALLOC_FAIL;
WriteFile( rd->master,&type,sizeof(int),&written,NULL );
LeaveCriticalSection( &rd->csWrite );
exitWait( 1,1 );
}
static void *add_realloc( void *ptr,int *count_p,int add,size_t blockSize,
CRITICAL_SECTION *cs )
{
GET_REMOTEDATA( rd );
int count_n = *count_p + add;
void *ptr_n;
if( !ptr )
ptr_n = HeapAlloc( rd->heap,0,count_n*blockSize );
else
ptr_n = HeapReAlloc( rd->heap,0,ptr,count_n*blockSize );
if( UNLIKELY(!ptr_n) )
{
if( !cs ) return( NULL );
int needLock = cs!=&rd->csWrite;
if( needLock )
LeaveCriticalSection( cs );
exitOutOfMemory( needLock );
}
*count_p = count_n;
return( ptr_n );
}
static inline void set_errno( int e )
{
GET_REMOTEDATA( rd );
*rd->oerrno() = e;
}
static wchar_t *wdup( const wchar_t *w,HANDLE heap )
{
if( !w || !w[0] ) return( NULL );
wchar_t *wd = HeapAlloc( heap,0,2*lstrlenW(w)+2 );
lstrcpyW( wd,w );
return( wd );
}
// }}}
// send module information {{{
static void writeModsFind( modInfo **p_mi_a,int *p_mi_q )
{
GET_REMOTEDATA( rd );
HMODULE ntdll = GetModuleHandle( "ntdll.dll" );
typedef LONG NTAPI func_LdrLockLoaderLock( ULONG,PULONG,PULONG_PTR );
typedef LONG NTAPI func_LdrUnlockLoaderLock( ULONG,ULONG_PTR );
func_LdrLockLoaderLock *fLdrLockLoaderLock =
rd->fGetProcAddress( ntdll,"LdrLockLoaderLock" );
func_LdrUnlockLoaderLock *fLdrUnlockLoaderLock =
rd->fGetProcAddress( ntdll,"LdrUnlockLoaderLock" );
ULONG_PTR ldrLockCookie;
fLdrLockLoaderLock( 0,NULL,&ldrLockCookie );
PEB *peb = GET_PEB();
PEB_LDR_DATA *ldrData = peb->Ldr;
LIST_ENTRY *head = &ldrData->InMemoryOrderModuleList;
LIST_ENTRY *entry = head;
int mi_q = 0;
do
{
LDR_DATA_TABLE_ENTRY *ldrEntry = CONTAINING_RECORD(
entry,LDR_DATA_TABLE_ENTRY,InMemoryOrderModuleList );
if( ldrEntry->DllBase )
mi_q++;
entry = entry->Flink;
}
while( entry!=head );
modInfo *mi_a = HeapAlloc( rd->heap,HEAP_ZERO_MEMORY,mi_q*sizeof(modInfo) );
if( !mi_a )
{
fLdrUnlockLoaderLock( 0,ldrLockCookie );
return;
}
mi_q = 0;
do
{
LDR_DATA_TABLE_ENTRY *ldrEntry = CONTAINING_RECORD(
entry,LDR_DATA_TABLE_ENTRY,InMemoryOrderModuleList );
if( ldrEntry->DllBase )
{
modInfo *mi = mi_a + mi_q;
mi->base = (size_t)ldrEntry->DllBase;
mi->size = ldrEntry->SizeOfImage;
mi->timestamp = ldrEntry->TimeDateStamp;
int count = ldrEntry->FullDllName.Length/2;
if( count>0 && count<MAX_PATH )
{
RtlMoveMemory( mi->path,ldrEntry->FullDllName.Buffer,count*2 );
mi->path[count] = 0;
mi_q++;
}
}
entry = entry->Flink;
}
while( entry!=head );
fLdrUnlockLoaderLock( 0,ldrLockCookie );
if( rd->opt.exceptionDetails>0 && (rd->opt.exceptionDetails&4) )
{
int i;
for( i=0; i<mi_q; i++ )
{
modInfo *mi = mi_a + i;
HMODULE mod = (HMODULE)mi->base;
HRSRC src = FindResource(
mod,MAKEINTRESOURCE(VS_VERSION_INFO),RT_VERSION );
if( !src || SizeofResource(mod,src)<40+sizeof(VS_FIXEDFILEINFO) )
continue;
HGLOBAL g = LoadResource( mod,src );
if( !g ) continue;
char *res = LockResource( g );
if( !res ) continue;
VS_FIXEDFILEINFO *ver = (VS_FIXEDFILEINFO*)( res+40 );
if( ver->dwSignature!=VS_FFI_SIGNATURE ) continue;
mi->versionMS = ver->dwFileVersionMS;
mi->versionLS = ver->dwFileVersionLS;
}
}
*p_mi_q = mi_q;
*p_mi_a = mi_a;
#ifndef NO_THREADS
writeThreadDescs();
#endif
}
static void writeModsSend( modInfo *mi_a,int mi_q )
{
GET_REMOTEDATA( rd );
int type = WRITE_MODS;
DWORD written;
WriteFile( rd->master,&type,sizeof(int),&written,NULL );
WriteFile( rd->master,&mi_q,sizeof(int),&written,NULL );
if( mi_q )
WriteFile( rd->master,mi_a,mi_q*sizeof(modInfo),&written,NULL );
if( mi_a )
HeapFree( rd->heap,0,mi_a );
}
static void writeAllocs( allocation *alloc_a,int alloc_q,int type )
{
GET_REMOTEDATA( rd );
int mi_q = 0;
modInfo *mi_a = NULL;
writeModsFind( &mi_a,&mi_q );
EnterCriticalSection( &rd->csWrite );
writeModsSend( mi_a,mi_q );
DWORD written;
WriteFile( rd->master,&type,sizeof(int),&written,NULL );
WriteFile( rd->master,alloc_a,alloc_q*sizeof(allocation),&written,NULL );
LeaveCriticalSection( &rd->csWrite );
}
// }}}
// memory allocation tracking {{{
static NOINLINE int allocSizeAndState(
void *p,funcType ft,size_t *s,size_t *id )
{
GET_REMOTEDATA( rd );
int splitIdx = (((uintptr_t)p)>>rd->ptrShift)&SPLIT_MASK;
splitAllocation *sa = rd->splits + splitIdx;
int prevEnable = -1;
size_t freeSize = -1;
size_t freeId = 0;
EnterCriticalSection( &sa->cs );
int i;
for( i=sa->alloc_q-1; i>=0; i-- )
{
allocation *a = sa->alloc_a + i;
if( a->ptr!=p ) continue;
if( UNLIKELY(a->ftFreed!=FT_COUNT) )
{
int j;
for( j=i-1; j>=0; j-- )
{
allocation *aj = sa->alloc_a + j;
if( aj->ptr==p && aj->ftFreed==FT_COUNT ) break;
}
if( j>=0 ) a = sa->alloc_a + j;
else
{
prevEnable = 0;
break;
}
}
prevEnable = 1;
a->ftFreed = ft;
freeSize = a->size;
freeId = a->id;
break;
}
LeaveCriticalSection( &sa->cs );
*s = freeSize;
if( id ) *id = freeId;
return( prevEnable );
}
static NOINLINE size_t heap_block_size( HANDLE heap,void *ptr )
{
if( !heap ) return( -1 );
PROCESS_HEAP_ENTRY phe;
phe.lpData = NULL;
size_t s = -1;
char *p = ptr;
HeapLock( heap );
while( HeapWalk(heap,&phe) )
{
if( !(phe.wFlags&PROCESS_HEAP_ENTRY_BUSY) ||
p<(char*)phe.lpData || p>=(char*)phe.lpData+phe.cbData )
continue;
if( p==(char*)phe.lpData )
s = phe.cbData;
else
{
s = (char*)phe.lpData + phe.cbData - p;
if( s>4 ) s -= 4;
}
uintptr_t align = MEMORY_ALLOCATION_ALIGNMENT;
s += ( align - (s%align) )%align;
break;
}
HeapUnlock( heap );
return( s );
}
static NOINLINE int trackFree(
void *free_ptr,allocType at,funcType ft,int failed_realloc,size_t id,
void *caller )
{
int ret = 1;
if( free_ptr )
{
GET_REMOTEDATA( rd );
size_t freeSize = -1;
#ifndef NO_THREADS
int threadNum = (int)(uintptr_t)TlsGetValue( rd->threadNumTls );
if( UNLIKELY(!threadNum) && rd->inExit )
{
TlsSetValue( rd->freeSizeTls,(void*)freeSize );
return( ret );
}
#endif
allocation fa;
int splitIdx = (((uintptr_t)free_ptr)>>rd->ptrShift)&SPLIT_MASK;
splitAllocation *sa = rd->splits + splitIdx;
EnterCriticalSection( &sa->cs );
int i;
for( i=sa->alloc_q-1; i>=0 && sa->alloc_a[i].ptr!=free_ptr; i-- );
int successfulFree = 1;
if( UNLIKELY(i<0) ) successfulFree = 0;
else if( UNLIKELY(
// realloc()
(id && sa->alloc_a[i].id!=id) ||
// free()
(!id && sa->alloc_a[i].ftFreed!=FT_COUNT)) )
{
// check for multiple entries of the same pointer,
// which is possible if there is a malloc() call
// between realloc() and trackFree() inside new_realloc(),
// and malloc() returns the pointer that realloc() just freed
int j;
for( j=i-1; j>=0 && (sa->alloc_a[j].ptr!=free_ptr ||
(id && sa->alloc_a[j].id!=id) ||
(!id && sa->alloc_a[j].ftFreed!=FT_COUNT)); j-- );
if( j>=0 ) i = j;
else successfulFree = 0;
}
// successful free {{{
if( LIKELY(successfulFree) )
{
allocation *a = sa->alloc_a + i;
RtlMoveMemory( &fa,a,sizeof(allocation) );
if( UNLIKELY(failed_realloc) )
a->ftFreed = FT_COUNT;
else
{
sa->alloc_q--;
if( i<sa->alloc_q )
RtlMoveMemory( a,&sa->alloc_a[sa->alloc_q],sizeof(allocation) );
}
LeaveCriticalSection( &sa->cs );
freeSize = fa.size;
if( UNLIKELY(fa.ftFreed==FT_BLOCKED) )
{
allocation *aa = HeapAlloc( rd->heap,0,2*sizeof(allocation) );
if( UNLIKELY(!aa) )
exitOutOfMemory( 1 );
RtlMoveMemory( aa,&fa,sizeof(allocation) );
CAPTURE_STACK_TRACE( 2,PTRS,aa[1].frames,caller,rd->maxStackFrames );
aa[1].ptr = free_ptr;
aa[1].size = 0;
aa[1].at = at;
aa[1].lt = LT_LOST;
aa[1].ft = ft;
#ifndef NO_THREADS
aa[1].threadNum = threadNum;
#endif
writeAllocs( aa,2,WRITE_FREE_WHILE_REALLOC );
HeapFree( rd->heap,0,aa );
if( rd->opt.raiseException )
DebugBreak();
}
if( UNLIKELY(fa.raiseFree) && !failed_realloc )
DebugBreak();
// freed memory information {{{
if( rd->opt.protectFree && !failed_realloc )
{
fa.ftFreed = ft;
splitFreed *sf = rd->freeds + splitIdx;
EnterCriticalSection( &sf->cs );
if( sf->freed_q>=sf->freed_s )
sf->freed_a = add_realloc(
sf->freed_a,&sf->freed_s,64,sizeof(freed),&sf->cs );
freed *f = sf->freed_a + sf->freed_q;
RtlMoveMemory( &f->a,&fa,sizeof(allocation) );
#ifndef NO_THREADS
f->threadNum = threadNum;
#endif
CAPTURE_STACK_TRACE( 2,PTRS,f->frames,caller,rd->maxStackFrames );
sf->freed_q++;
LeaveCriticalSection( &sf->cs );
}
// }}}
// mismatching allocation/release method {{{
if( UNLIKELY(rd->opt.allocMethod && fa.at!=at) )
{
allocation *aa = HeapAlloc( rd->heap,0,2*sizeof(allocation) );
if( UNLIKELY(!aa) )
exitOutOfMemory( 1 );
RtlMoveMemory( aa,&fa,sizeof(allocation) );
CAPTURE_STACK_TRACE( 2,PTRS,aa[1].frames,caller,rd->maxStackFrames );
aa[1].ptr = free_ptr;
aa[1].size = 0;
aa[1].at = at;
aa[1].lt = LT_LOST;
aa[1].ft = ft;
#ifndef NO_THREADS
aa[1].threadNum = threadNum;
#endif
writeAllocs( aa,2,WRITE_WRONG_DEALLOC );
HeapFree( rd->heap,0,aa );
if( rd->opt.raiseException>1 )
DebugBreak();
}
// }}}
}
// }}}
// free of invalid pointer {{{
else
{
if( i>=0 )
{
allocation *a = sa->alloc_a + i;
RtlMoveMemory( &fa,a,sizeof(allocation) );
a->ftFreed = FT_BLOCKED;
}
LeaveCriticalSection( &sa->cs );
EnterCriticalSection( &rd->csFreedMod );
int inExit = rd->inExit;
LeaveCriticalSection( &rd->csFreedMod );
allocation *aa = HeapAlloc(
rd->heap,HEAP_ZERO_MEMORY,4*sizeof(allocation) );
if( UNLIKELY(!aa) )
exitOutOfMemory( 1 );
// double free {{{
if( i>=0 )
{
// this block was realloc()'d at the same time in another thread
CAPTURE_STACK_TRACE( 2,PTRS,aa[0].frames,caller,rd->maxStackFrames );
aa[0].ft = ft;
#ifndef NO_THREADS
aa[0].threadNum = threadNum;
#endif
RtlMoveMemory( &aa[1],&fa,sizeof(allocation) );
aa[2].ft = fa.ftFreed;
writeAllocs( aa,3,WRITE_DOUBLE_FREE );
if( rd->opt.raiseException )
DebugBreak();
}
else if( rd->opt.protectFree )
{
splitFreed *sf = rd->freeds + splitIdx;
EnterCriticalSection( &sf->cs );
for( i=sf->freed_q-1; i>=0 && sf->freed_a[i].a.ptr!=free_ptr; i-- );
if( i>=0 )
{
freed *f = &sf->freed_a[i];
RtlMoveMemory( &aa[1],&f->a,sizeof(allocation) );
RtlMoveMemory( aa[2].frames,f->frames,PTRS*sizeof(void*) );
#ifndef NO_THREADS
aa[2].threadNum = f->threadNum;
#endif
LeaveCriticalSection( &sf->cs );
CAPTURE_STACK_TRACE( 2,PTRS,aa[0].frames,caller,rd->maxStackFrames );
aa[0].ft = ft;
#ifndef NO_THREADS
aa[0].threadNum = threadNum;
#endif
aa[2].ft = aa[1].ftFreed;
writeAllocs( aa,3,WRITE_DOUBLE_FREE );
if( rd->opt.raiseException )
DebugBreak();
}
else
LeaveCriticalSection( &sf->cs );
}
// }}}
if( i>=0 );
else if( heap_block_size(rd->crtHeap,free_ptr)!=(size_t)-1 )
{
if( at==AT_MALLOC )
rd->ofree( free_ptr );
else if( ft==FT_OP_DELETE )
rd->oop_delete( free_ptr );
else if( ft==FT_OP_DELETE_A )
rd->oop_delete_a( free_ptr );
}
else if( !inExit )
{
aa->ptr = free_ptr;
aa->size = 0;
aa->at = at;
aa->lt = LT_LOST;
aa->ft = ft;
#ifndef NO_THREADS
aa->threadNum = threadNum;
#endif
CAPTURE_STACK_TRACE( 2,PTRS,aa->frames,caller,rd->maxStackFrames );
int protect = rd->opt.protect;
uintptr_t ptr = (uintptr_t)free_ptr;
size_t pageAdd = rd->pageAdd;
DWORD pageSize = rd->pageSize;
int j;
int foundAlloc = 0;
int foundRef = ptr==(uintptr_t)rd->opt.init;
#ifndef _WIN64
foundRef |= ptr==(uintptr_t)(rd->opt.init>>32);
#endif
// block address with offset {{{
for( j=0; j<=SPLIT_MASK; j++ )
{
sa = rd->splits + j;
EnterCriticalSection( &sa->cs );
allocation *alloc_a = sa->alloc_a;
int alloc_q = sa->alloc_q;
for( i=0; i<alloc_q; i++ )
{
allocation *a = alloc_a + i;
uintptr_t p = (uintptr_t)a->ptr;
size_t s = a->size;
if( !foundAlloc )
{
uintptr_t realStart;
uintptr_t realEnd;
size_t slackSize;
if( protect==1 )
{
slackSize = p%pageSize;
realStart = p - slackSize;
realEnd = p + s + pageSize*pageAdd;
}
else
{
slackSize = ( pageSize - (s%pageSize) )%pageSize;
realStart = p - pageSize*pageAdd;
realEnd = p + s + slackSize;
}
if( ptr>=realStart && ptr<realEnd )
{
RtlMoveMemory( &aa[1],a,sizeof(allocation) );
foundAlloc = 1;
if( foundRef ) break;
}
}
if( !foundRef && a->ftFreed==FT_COUNT )
{
uintptr_t *refP = a->ptr;
size_t refS = s/sizeof(void*);
size_t k;
for( k=0; k<refS; k++ )
{
if( refP[k]!=ptr ) continue;
RtlMoveMemory( &aa[3],a,sizeof(allocation) );
// in [2], because it's the only big enough unused field
aa[2].size = k*sizeof(void*);
foundRef = 1;
break;
}
if( foundAlloc && foundRef ) break;
}
}
LeaveCriticalSection( &sa->cs );
if( foundAlloc && foundRef ) break;
}
// }}}
// freed block address with offset {{{
if( rd->opt.protectFree && !foundAlloc )
{
for( j=0; j<=SPLIT_MASK; j++ )
{
splitFreed *sf = rd->freeds + j;
EnterCriticalSection( &sf->cs );
freed *freed_a = sf->freed_a;
int freed_q = sf->freed_q;
for( i=0; i<freed_q; i++ )
{
freed *ff = freed_a + i;
allocation *a = &ff->a;
uintptr_t p = (uintptr_t)a->ptr;
size_t s = a->size;
if( !foundAlloc )
{
uintptr_t realStart;
uintptr_t realEnd;
size_t slackSize;
if( protect==1 )
{
slackSize = p%pageSize;
realStart = p - slackSize;
realEnd = p + s + pageSize*pageAdd;
}
else
{
slackSize = ( pageSize - (s%pageSize) )%pageSize;
realStart = p - pageSize*pageAdd;
realEnd = p + s + slackSize;
}
if( ptr>=realStart && ptr<realEnd )
{
RtlMoveMemory( &aa[1],a,sizeof(allocation) );
aa[2].ptr = aa[1].ptr;
RtlMoveMemory( aa[2].frames,ff->frames,PTRS*sizeof(void*) );
aa[2].ft = ff->a.ftFreed;
#ifndef NO_THREADS
aa[2].threadNum = ff->threadNum;
#endif
foundAlloc = 1;
break;
}
}
}
LeaveCriticalSection( &sf->cs );
if( foundAlloc ) break;
}
}
// }}}
// stack address {{{
if( !foundAlloc )
{
TEB *teb = GET_TEB();
if( free_ptr>=teb->StackLimit && free_ptr<teb->StackBase )
{
// is otherwise unused since !foundAlloc
aa[1].id = 1;
if( ptr%sizeof(uintptr_t) )
ptr -= ptr%sizeof(uintptr_t);
void **frame = FRAME_ADDRESS();
void **endFrame = (void**)ptr;
int frameIdx = 0;
void *prevFrame = NULL;
void *curFrame = aa[0].frames[frameIdx];
while( curFrame && frame<endFrame )
{
if( *frame==curFrame )