-
Notifications
You must be signed in to change notification settings - Fork 25
/
allocer.cpp
1302 lines (1137 loc) · 32.2 KB
/
allocer.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
// 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)
#include "heob.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <direct.h>
#include <conio.h>
#include <fcntl.h>
#include <float.h>
#include <signal.h>
#if defined(__GNUC__) && __GNUC__>=7
#pragma GCC diagnostic ignored "-Walloc-size-larger-than="
#endif
#if !defined(_MSC_VER) || _MSC_VER<1900
__declspec(dllimport) void *operator new[]( size_t );
__declspec(dllimport) void operator delete[]( void* );
#endif
extern "C" __declspec(dllimport) void *dll_alloc( size_t );
extern "C" __declspec(dllimport) void *dll_memory( void );
extern "C" __declspec(dllimport) int *dll_int( void );
extern "C" __declspec(dllimport) int *dll_arr( void );
extern "C" __declspec(dllimport) DWORD dll_thread_id( void );
extern "C" __declspec(dllimport) void do_nothing( void* );
extern "C" __declspec(dllimport) char *dll_static_char( void );
extern "C" __declspec(dllimport) void dll_enter_critical_section( void );
static LONG WINAPI exceptionWalker( LPEXCEPTION_POINTERS ep )
{
printf( "handled exception code: %08lX\n",
ep->ExceptionRecord->ExceptionCode );
fflush( stdout );
static int wasHere = 0;
if( wasHere ||
ep->ExceptionRecord->ExceptionCode!=STATUS_ACCESS_VIOLATION )
ExitProcess( -1 );
wasHere++;
#ifndef _WIN64
ep->ContextRecord->Eip += 10;
#else
ep->ContextRecord->Rip += 10;
#endif
return( EXCEPTION_CONTINUE_EXECUTION );
}
static DWORD WINAPI workerThread( LPVOID arg )
{
int alloc_count = (UINT_PTR)arg;
DWORD sum = 0;
unsigned char **allocs = (unsigned char**)malloc(
alloc_count*sizeof(unsigned char*) );
for( int i=0; i<alloc_count; i++ )
allocs[i] = (unsigned char*)malloc( 16 );
for( int i=0; i<alloc_count; i++ )
sum += allocs[i][0];
for( int i=0; i<alloc_count; i++ )
free( allocs[i] );
free( allocs );
return( sum );
}
const DWORD EXCEPTION_THREAD_NAME = 0x406D1388;
#pragma pack(push,8)
typedef struct tagTHREADNAME_INFO
{
DWORD dwType;
LPCSTR szName;
DWORD dwThreadID;
DWORD dwFlags;
} THREADNAME_INFO;
#pragma pack(pop)
static LONG CALLBACK ignoreNameException( PEXCEPTION_POINTERS ExceptionInfo )
{
if( ExceptionInfo->ExceptionRecord->ExceptionCode==EXCEPTION_THREAD_NAME )
return EXCEPTION_CONTINUE_EXECUTION;
return EXCEPTION_CONTINUE_SEARCH;
}
static void SetThreadName( DWORD dwThreadID,const char *threadName )
{
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = threadName;
info.dwThreadID = dwThreadID;
info.dwFlags = 0;
PVOID veh = AddVectoredExceptionHandler( 1,ignoreNameException );
RaiseException( EXCEPTION_THREAD_NAME,0,
sizeof(info)/sizeof(ULONG_PTR),(ULONG_PTR*)&info );
RemoveVectoredExceptionHandler( veh );
}
static DWORD WINAPI namedThread( LPVOID arg )
{
if( arg )
SetThreadName( -1,"self named thread" );
char *leak = (char*)malloc( 10+(arg?0:16) );
return leak[0];
}
static void SetThreadDesc( HANDLE thread,const wchar_t *td )
{
HMODULE kernel32 = GetModuleHandle( "kernel32.dll" );
typedef HRESULT WINAPI func_SetThreadDescription( HANDLE,PCWSTR );
func_SetThreadDescription *fSetThreadDescription =
(func_SetThreadDescription*)GetProcAddress(
kernel32,"SetThreadDescription" );
if( !fSetThreadDescription )
{
// fallback to SetThreadName()
HMODULE ntdll = GetModuleHandle( "ntdll.dll" );
typedef enum
{
ThreadBasicInformation,
}
THREADINFOCLASS;
typedef LONG NTAPI func_NtQueryInformationThread(
HANDLE,THREADINFOCLASS,PVOID,ULONG,PULONG );
func_NtQueryInformationThread *fNtQueryInformationThread =
(func_NtQueryInformationThread*)GetProcAddress(
ntdll,"NtQueryInformationThread" );
if( fNtQueryInformationThread )
{
typedef struct _CLIENT_ID
{
PVOID UniqueProcess;
PVOID UniqueThread;
}
CLIENT_ID;
typedef DWORD KPRIORITY;
typedef struct _THREAD_BASIC_INFORMATION
{
LONG ExitStatus;
void *TebBaseAddress;
CLIENT_ID ClientId;
KAFFINITY AffinityMask;
KPRIORITY Priority;
KPRIORITY BasePriority;
}
THREAD_BASIC_INFORMATION;
THREAD_BASIC_INFORMATION tbi;
RtlZeroMemory( &tbi,sizeof(THREAD_BASIC_INFORMATION) );
if( fNtQueryInformationThread(thread,
ThreadBasicInformation,&tbi,sizeof(tbi),NULL)==0 )
{
DWORD threadId = (DWORD)(ULONG_PTR)tbi.ClientId.UniqueThread;
int len;
char *tn;
if( (len=WideCharToMultiByte(CP_ACP,0,td,-1,NULL,0,NULL,NULL))>0 &&
(tn=(char*)HeapAlloc(GetProcessHeap(),0,len)) )
{
WideCharToMultiByte( CP_ACP,0,td,-1,tn,len,NULL,NULL );
SetThreadName( threadId,tn );
HeapFree( GetProcessHeap(),0,tn );
}
}
}
return;
}
fSetThreadDescription( thread,td );
}
static DWORD WINAPI descThread( LPVOID arg )
{
if( arg )
SetThreadDesc( GetCurrentThread(),L"self named thread" );
char *leak = (char*)malloc( 10+(arg?0:16) );
return leak[0];
}
static DWORD WINAPI raceThread( LPVOID arg )
{
char *r = NULL;
int c = 0;
for( int i=1; i<=1000; i++ )
{
if( arg )
r = (char*)realloc( r,i*10 );
else
r = (char*)malloc( i*10 );
c += r[0];
if( !arg )
free( r );
}
if( arg )
free( r );
return( c );
}
typedef struct
{
char *ptr;
size_t size;
} heob_allocation;
typedef heob_allocation *func_heob_find( void* );
typedef size_t func_heob_raise_free( void* );
func_heob_find *heob_find_allocation;
func_heob_find *heob_find_freed;
func_heob_find *heob_find_nearest_allocation;
func_heob_find *heob_find_nearest_freed;
static void heob_find( char *ptr )
{
heob_allocation *a;
const char *found_text = NULL;
if( (a=heob_find_allocation(ptr)) )
found_text = "allocated";
else if( (a=heob_find_freed(ptr)) )
found_text = "freed";
else
{
a = heob_find_nearest_allocation( ptr );
heob_allocation *f = heob_find_nearest_freed( ptr );
if( a && (!f || a->ptr>f->ptr) && ptr-a->ptr<16384 )
found_text = "near allocated";
else if( f && ptr-f->ptr<16384 )
{
a = f;
found_text = "near freed";
}
}
if( found_text )
{
intptr_t d = ptr - a->ptr;
printf( "pointer 0x%p is %s: size=%u; offset=%s%d\n",
ptr,found_text,(unsigned)a->size,d>0?"+":"",(int)d );
}
else
printf( "pointer 0x%p not found\n",ptr );
}
static DWORD WINAPI freeThread( LPVOID arg )
{
void **data = (void**)arg;
WaitForSingleObject( data[0],INFINITE );
void *ptr = realloc( data[1],32 );
free( ptr );
return( 0 );
}
static DWORD WINAPI sleepThread( LPVOID arg )
{
Sleep( (UINT_PTR)arg );
return( 0 );
}
static DWORD WINAPI overflowThread( LPVOID arg )
{
SetThreadName( -1,"overflow thread" );
int *ptr = (int*)arg;
return( ptr[5] );
}
static DWORD WINAPI abandonCriticalSectionThread( LPVOID )
{
dll_enter_critical_section();
return( 0 );
}
static void segv_handler( int )
{
printf( "crash\n" );
TerminateProcess( GetCurrentProcess(),1 );
}
#ifdef _WIN64
extern "C" LONG CALLBACK signalException( PEXCEPTION_POINTERS ExceptionInfo )
{
if( ExceptionInfo->ExceptionRecord->ExceptionCode!=STATUS_ACCESS_VIOLATION )
return( EXCEPTION_CONTINUE_SEARCH );
void (*crash_handler)( int ) = signal( SIGSEGV,SIG_DFL );
if( crash_handler==SIG_DFL )
return( EXCEPTION_CONTINUE_SEARCH );
crash_handler( SIGSEGV );
return( EXCEPTION_CONTINUE_EXECUTION );
}
#endif
int choose( int arg )
{
printf( "allocer: main()\n" );
fflush( stdout );
char *mem = (char*)malloc( 15 );
mem[0] = 0;
switch( arg )
{
case 1:
{
// memory leaks
char *copy = strdup( "abcd" );
volatile char *zeroes = (char*)calloc( 2,500 );
wchar_t *wcopy = wcsdup( L"efgh" );
mem[1] = copy[0];
mem[2] = zeroes[0];
mem[3] = wcopy[0];
char *newChars = new char[50];
mem[4] = newChars[0];
chdir( "\\" );
char *cwd = _getcwd( NULL,0 );
mem[5] = cwd[0];
wchar_t *wcwd = _wgetcwd( NULL,0 );
mem[6] = wcwd[0];
cwd = _getdcwd( 0,NULL,0 );
mem[7] = cwd[0];
wcwd = _wgetdcwd( 0,NULL,0 );
mem[8] = wcwd[0];
char *fp = _fullpath( NULL,".",0 );
mem[9] = fp[0];
wchar_t *wfp = _wfullpath( NULL,L".",0 );
mem[10] = wfp[0];
}
break;
case 2:
// access after allowed area
mem[1] = mem[20];
mem[25] = 5;
break;
case 3:
// access before allowed area
mem[1] = mem[-10];
mem[-5] = 3;
break;
case 4:
// allocation size alignment
{
int sum = 0;
char t[100];
int i;
t[0] = 0;
char *tc[64];
for( i=0; i<64; i++ )
{
tc[i] = strdup( t );
sum += tc[i][0];
strcat( t,"x" );
}
mem[1] = sum + tc[0][1];
}
break;
case 5:
// failed allocation
{
#define BIGNUM ((size_t)1 << (sizeof(void*)*8-1))
char *big = (char*)malloc( BIGNUM );
mem[1] = big[0];
}
break;
case 6:
// reference pointer after being freed
{
char *tmp = (char*)malloc( 15 );
char *tmp2;
mem[1] = tmp[0];
free( tmp );
tmp2 = (char*)malloc( 15 );
printf( "ptr1=0x%p; ptr2=0x%p -> %s\n",
tmp,tmp2,tmp==tmp2?"same":"different" );
fflush( stdout );
mem[2] = tmp[1];
mem[3] = tmp2[0];
free( tmp2 );
}
break;
case 7:
// multiple free / free of invalid pointer
free( mem );
free( (void*)(size_t)0x80000000 );
break;
case 8:
// mismatch of allocation/release method
printf( "%s",mem );
delete mem;
mem = new char;
mem[0] = 0;
printf( "%s",mem );
delete[] mem;
mem = new char[100];
mem[0] = 0;
break;
case 9:
// missing return address of strcmp()
{
mem[15] = 'a';
char *s = strdup( "abc" );
if( !strcmp(mem+15,s) )
strcat( mem,"a" );
free( s );
}
break;
case 10:
// leak in dll
{
char *leak = (char*)dll_alloc( 10 );
mem[1] = leak[0];
#ifndef _WIN64
#define BITS "32"
#else
#define BITS "64"
#endif
HMODULE mod = LoadLibrary( "dll-alloc-shared" BITS ".dll" );
if( mod )
{
typedef void *dll_alloc_func( size_t );
dll_alloc_func *func =
(dll_alloc_func*)GetProcAddress( mod,"dll_alloc" );
if( func )
{
leak = (char*)func( 20 );
mem[2] = leak[0];
}
FreeLibrary( mod );
}
}
break;
case 11:
// exit-call
exit( arg );
case 12:
// exception handler
{
SetUnhandledExceptionFilter( &exceptionWalker );
void *ptr = (void*)&choose;
*(int*)ptr = 5;
}
break;
case 13:
// multiple free
free( mem );
break;
case 14:
// different page protection size
{
struct BigStruct
{
char c[5000];
};
BigStruct *bs = (BigStruct*)malloc( sizeof(BigStruct) );
mem[1] = bs[1].c[4500];
}
break;
case 15:
// leak types
{
char *indirectly_reachable = (char*)malloc( 16 );
static char **reachable;
reachable = (char**)malloc( sizeof(char*) );
*reachable = indirectly_reachable;
mem[1] = reachable[0][0];
*(char****)indirectly_reachable = &reachable;
char *indirectly_lost = (char*)malloc( 32 );
size_t s = 0;
char *indirectly_lost2 = (char*)malloc( s );
char *volatile *lost = (char**)malloc( sizeof(char*) );
*(char**)indirectly_lost = indirectly_lost2;
*lost = indirectly_lost;
mem[2] = lost[0][0];
char *indirectly_kinda_reachable = (char*)malloc( 64 );
static char **kinda_reachable;
kinda_reachable = (char**)malloc( 16 );
*kinda_reachable = indirectly_kinda_reachable + 5;
mem[3] = kinda_reachable[0][0];
kinda_reachable++;
*(char****)indirectly_kinda_reachable = &kinda_reachable;
char **jointly_lost1 = (char**)malloc( 48 );
char **jointly_lost2 = (char**)malloc( 48 );
char *jointly_kinda_lost = (char*)malloc( 48 );
*jointly_lost1 = (char*)jointly_lost2;
*jointly_lost2 = (char*)jointly_lost1;
jointly_lost1[1] = jointly_kinda_lost + 10;
mem[4] = jointly_lost1[0][0];
char **self_reference = (char**)malloc( 80 );
*self_reference = (char*)self_reference + 5;
mem[5] = **self_reference;
}
break;
case 16:
// access near freed block
{
struct BigStruct
{
char c[5000];
};
BigStruct *bs = (BigStruct*)malloc( sizeof(BigStruct) );
mem[1] = bs[0].c[0];
free( bs );
mem[1] = bs[1].c[4500];
}
break;
case 17:
// memory leak contents
{
char *copy = strdup( "this is a memory leak" );
wchar_t *wcopy = wcsdup( L"this is a bigger memory leak" );
volatile char *emptyness = (char*)malloc( 33 );
emptyness[3] = '.';
mem[1] = copy[0];
mem[2] = wcopy[0];
mem[3] = emptyness[0];
}
break;
case 18:
// merge identical memory leaks
{
char *free_me = NULL;
for( int i=0; i<6; i++ )
{
char *copy = strdup( "memory leak X" );
mem[i+1] = copy[0];
copy[12] = '0' + i;
if( !i ) free_me = copy;
if( i==2 ) free( free_me );
}
}
break;
case 19:
// realloc() of memory allocated in dll initialization
{
void *memory = dll_memory();
char *new_memory = (char*)realloc( memory,501 );
new_memory[0] = 'a';
}
break;
case 20:
// calloc() multiplication overflow
{
#ifndef _WIN64
#define HALF_OVERFLOW 0x80000005
#else
#define HALF_OVERFLOW 0x8000000000000005
#endif
errno = 0;
char *m = (char*)calloc( HALF_OVERFLOW,2 );
arg = errno;
if( m ) mem[1] = m[0];
}
break;
case 21:
// delete memory allocated in dll initialization
{
int *one_int = dll_int();
delete one_int;
int *arr_int = dll_arr();
delete[] arr_int;
}
break;
case 22:
// self-termination
TerminateProcess( GetCurrentProcess(),arg );
break;
case 23:
// initial value
{
char *emptyness = (char*)malloc( 30 );
mem[1] = emptyness[0];
}
break;
#define THREAD_COUNT 8
#define ALLOC_COUNT (256*1024)
case 24:
// benchmark: single thread
workerThread( (LPVOID)(THREAD_COUNT*ALLOC_COUNT) );
break;
case 25:
// benchmark: multi-threading
{
HANDLE threads[THREAD_COUNT];
for( int i=0; i<THREAD_COUNT; i++ )
threads[i] = CreateThread( NULL,0,
&workerThread,(LPVOID)ALLOC_COUNT,0,NULL );
WaitForMultipleObjects( THREAD_COUNT,threads,TRUE,INFINITE );
for( int i=0; i<THREAD_COUNT; i++ )
CloseHandle( threads[i] );
}
break;
case 26:
// thread names
{
SetThreadName( -1,"main thread" );
char *mainLeak = (char*)malloc( 11 );
mem[1] = mainLeak[0];
HANDLE thread = CreateThread(
NULL,0,&namedThread,(void*)1,0,NULL );
WaitForSingleObject( thread,INFINITE );
CloseHandle( thread );
DWORD threadId;
thread = CreateThread(
NULL,0,&namedThread,NULL,CREATE_SUSPENDED,&threadId );
SetThreadName( threadId,"remotely named thread" );
ResumeThread( thread );
WaitForSingleObject( thread,INFINITE );
CloseHandle( thread );
}
break;
case 27:
// benchmark: leak type detection
{
unsigned char v = 0;
for( int i=0; i<4000; i++ )
{
unsigned char *b = (unsigned char*)malloc( 10000 );
v += b[0];
}
mem[1] = v;
}
break;
case 28:
// leak until escape
while( 1 )
{
int c = _getch();
if( c==27 ) break;
if( c=='n' || c=='f' || c=='c' || c=='s' )
{
if( heob_control(HEOB_LEAK_RECORDING_STATE)!=HEOB_NOT_FOUND )
{
int cmd;
if( c=='n' )
cmd = HEOB_LEAK_RECORDING_START;
else if( c=='f' )
cmd = HEOB_LEAK_RECORDING_STOP;
else if( c=='c' )
cmd = HEOB_LEAK_RECORDING_CLEAR;
else
cmd = HEOB_LEAK_RECORDING_SHOW;
heob_control( cmd );
}
continue;
}
else if( c=='i' )
{
free( (void*)0x1 );
continue;
}
unsigned char *b = (unsigned char*)malloc( 15 );
b[0] = c;
mem[1] += b[0];
}
break;
case 29:
// compare thread id with dll
printf( "thread id: %s\n",
GetCurrentThreadId()==dll_thread_id()?"same":"different" );
break;
case 30:
// suspended sub-process
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
RtlZeroMemory( &si,sizeof(STARTUPINFO) );
RtlZeroMemory( &pi,sizeof(PROCESS_INFORMATION) );
si.cb = sizeof(STARTUPINFO);
char commandLine[20];
sprintf( commandLine,"allocer%d 28",8*(int)sizeof(void*) );
BOOL result = CreateProcess( NULL,commandLine,NULL,NULL,FALSE,
CREATE_SUSPENDED,NULL,NULL,&si,&pi );
if( !result ) break;
printf( "processId: %lu\n",pi.dwProcessId );
printf( "threadId: %lu\n",pi.dwThreadId );
fflush( stdout );
WaitForSingleObject( pi.hProcess,INFINITE );
CloseHandle( pi.hThread );
CloseHandle( pi.hProcess );
}
break;
case 31:
case 50:
// free of invalid pointer
{
int d = 8/sizeof(void*);
unsigned char **ref = (unsigned char**)malloc(
100*d*sizeof(void*) );
ref[0*d] = (unsigned char*)malloc( 10 ) + ( arg==31?1:0 );
ref[1*d] = (unsigned char*)malloc( 20 ) - 2;
unsigned char *noref = (unsigned char*)malloc( 30 );
ref[2*d] = (unsigned char*)(size_t)0xabcdef01;
ref[3*d] = (unsigned char*)&ref;
ref[4*d] = (unsigned char*)dll_static_char();
do_nothing( ref );
mem[1] = ref[3*d]!=NULL;
mem[2] = noref[0];
free( ref[1*d]+2 );
free( ref[0*d] );
free( ref[1*d] );
free( noref - (arg==31?1:0) );
free( ref[2*d] );
free( ref[3*d] );
free( ref[4*d] );
free( ref );
}
break;
case 32:
// free allocation of different CRT
{
HMODULE msvcr100 = LoadLibrary( "msvcr100.dll" );
if( msvcr100 )
{
typedef void *func_malloc( size_t );
func_malloc *f_malloc =
(func_malloc*)GetProcAddress( msvcr100,"malloc" );
if( f_malloc )
{
void *ptr = f_malloc( 128 );
free( ptr );
}
FreeLibrary( msvcr100 );
}
}
break;
case 33:
// failed re-allocation
{
char *small = (char*)malloc( 16 );
mem[1] = small[0];
errno = 0;
char *big = (char*)realloc( small,BIGNUM );
arg = errno;
if( big ) small = big;
mem[2] = small[0];
}
break;
case 34:
// 0-sized allocation
{
size_t s = 0;
char *m0 = (char*)malloc( s );
printf( "m0: 0x%p\n",m0 );
free( m0 );
char *m16 = (char*)malloc( 16 );
char *r0 = (char*)realloc( m16,0 );
printf( "r0: 0x%p\n",r0 );
free( r0 );
}
break;
case 35:
// control leak recording
{
if( heob_control(HEOB_LEAK_RECORDING_STATE)!=HEOB_NOT_FOUND )
{
printf( "recording: %d\n",heob_control(HEOB_LEAK_RECORDING_STATE) );
printf( "leak count: %d\n",heob_control(HEOB_LEAK_COUNT) );
heob_control( HEOB_LEAK_RECORDING_CLEAR );
heob_control( HEOB_LEAK_RECORDING_STOP );
char *leakNo = (char*)malloc( 16 );
mem[1] = leakNo[0];
heob_control( HEOB_LEAK_RECORDING_START );
printf( "leak count: %d\n",heob_control(HEOB_LEAK_COUNT) );
char *leakYes = (char*)malloc( 16 );
mem[2] = leakYes[0];
leakYes = strdup( "string leak" );
mem[3] = leakYes[0];
printf( "leak count: %d\n",heob_control(HEOB_LEAK_COUNT) );
fflush( stdout );
heob_control( HEOB_LEAK_RECORDING_SHOW );
leakYes = (char*)malloc( 16 );
mem[4] = leakYes[0];
}
else
printf( "heob.exe is not running\n" );
}
break;
case 36:
// _msize
printf( "size of mem: %d\n",(int)_msize(mem) );
break;
case 37:
case 38:
// check for free/realloc race condition
{
HANDLE thread = CreateThread(
NULL,0,&raceThread,arg==37?(void*)1:NULL,0,NULL );
for( int i=1; i<=1000; i++ )
free( (void*)(size_t)i );
WaitForSingleObject( thread,INFINITE );
CloseHandle( thread );
}
break;
case 39:
// query heob for allocation information
{
HMODULE heob = GetModuleHandle( "heob" BITS ".exe" );
heob_find_allocation = heob ? (func_heob_find*)GetProcAddress(
heob,"heob_find_allocation" ) : NULL;
heob_find_freed = heob ? (func_heob_find*)GetProcAddress(
heob,"heob_find_freed" ) : NULL;
heob_find_nearest_allocation = heob ? (func_heob_find*)GetProcAddress(
heob,"heob_find_nearest_allocation" ) : NULL;
heob_find_nearest_freed = heob ? (func_heob_find*)GetProcAddress(
heob,"heob_find_nearest_freed" ) : NULL;
if( heob_find_allocation && heob_find_freed &&
heob_find_nearest_allocation && heob_find_nearest_freed )
{
char *ptr = (char*)malloc( 10 );
heob_find( ptr );
heob_find( ptr-10 );
heob_find( ptr+3 );
heob_find( ptr+100 );
heob_find( ptr+8192 );
free( ptr );
heob_find( ptr );
heob_find( ptr-10 );
heob_find( ptr+3 );
heob_find( ptr+100 );
heob_find( ptr+8192 );
heob_find( (char*)0x5 );
}
}
break;
case 40:
// manipulated access rights
{
char *s = strdup( "abc" );
DWORD prot;
VirtualProtect( s,4,PAGE_NOACCESS,&prot );
mem[1] = s[1];
free( s );
}
break;
case 41:
// NULL function call
{
void (*null_function)( void ) = NULL;
null_function();
}
break;
case 42:
// children process with different bitness
{
char commandLine[20];
sprintf( commandLine,"allocer%d.exe 1",sizeof(void*)==4?64:32 );
system( commandLine );
}
break;
case 43:
// free uninitialized pointer
{
mem[4] = 0;
char **ptr_buf = (char**)malloc( sizeof(char*) );
free( *ptr_buf );
}
break;
case 44:
// benchmark: many memory leaks
{
int i;
int sum = 0;
for( i=0; i<100000; i++ )
{
char *leak = (char*)malloc( 16 );
sum += leak[0];
}
mem[1] = sum;
}
break;
case 45:
// fake uncaught VC c++ exception
{
static DWORD ofsBuffer[8];
static struct {
void *ptrs[2];
char decoratedName[32];
} exceptionType;
strcpy( exceptionType.decoratedName,".?AVfake_exception@heob@@" );
#ifndef _WIN64
#define MOD_REL(ptr) ((DWORD)ptr)
#else
ULONG_PTR mod = (ULONG_PTR)GetModuleHandle( NULL );
#define MOD_REL(ptr) ((ULONG_PTR)ptr-mod)
#endif
ofsBuffer[3] = MOD_REL( &ofsBuffer[4] );
ofsBuffer[5] = MOD_REL( &ofsBuffer[6] );
ofsBuffer[7] = MOD_REL( &exceptionType );
char exceptionObject[] = "fake exception";
ULONG_PTR exceptionParams[] = {
0x19930520,
(ULONG_PTR)exceptionObject,
(ULONG_PTR)ofsBuffer,
#ifdef _WIN64
mod,
#endif
};
RaiseException( 0xe06d7363,EXCEPTION_NONCONTINUABLE,
sizeof(exceptionParams)/sizeof(ULONG_PTR),exceptionParams );
}
break;
case 46: