forked from johnsonjh/duma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
duma.c
2534 lines (2181 loc) · 76.6 KB
/
duma.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
/*
* DUMA - Red-Zone memory allocator.
* Copyright (C) 2006 Michael Eddington <meddington@gmail.com>
* Copyright (C) 2002-2009 Hayati Ayguen <h_ayguen@web.de>, Procitec GmbH
* Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>
* License: GNU GPL (GNU General Public License, see COPYING-GPL)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* FILE CONTENTS:
* --------------
* This is a special version of malloc() and company for debugging software
* that is suspected of overrunning or underrunning the boundaries of a
* malloc buffer, or touching free memory.
*
* It arranges for each malloc buffer to be followed (or preceded)
* in the address space by an inaccessable virtual memory page,
* and for free memory to be inaccessable. If software touches the
* inaccessable page, it will get an immediate segmentation
* fault. It is then trivial to uncover the offending code using a debugger.
*
* An advantage of this product over most malloc debuggers is that this one
* detects reading out of bounds as well as writing, and this one stops on
* the exact instruction that causes the error, rather than waiting until the
* next boundary check.
*
* There is one product that debugs malloc buffer overruns
* better than DUMA: "Purify" from Purify Systems, and that's only
* a small part of what Purify does. I'm not affiliated with Purify, I just
* respect a job well done.
*
* This version of malloc() should not be linked into production software,
* since it tremendously increases the time and memory overhead of malloc().
* Each malloc buffer will consume a minimum of two virtual memory pages,
* this is 16 kilobytes on many systems. On some systems it will be necessary
* to increase the amount of swap space in order to debug large programs that
* perform lots of allocation, because of the per-buffer overhead.
*/
#ifndef DUMA_NO_DUMA
#include <errno.h>
#include <limits.h>
#include <memory.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#else
#define WIN32_LEAN_AND_MEAN 1
#include <io.h>
#include <winbase.h>
#include <windows.h>
#ifndef __CYGWIN__
/* already defined in cygwin headers */
typedef unsigned u_int;
#endif
#endif
#ifdef _MSC_VER
#include <crtdbg.h>
#endif
#include "duma.h"
#include "duma_sem.h"
#include "noduma.h"
#include "paging.h"
#include "print.h"
#include "verinfo.h" /* generated from make_git_source_version.sh */
#if defined(WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__) && \
!defined(__MINGW64__)
DUMA_EXTERN_C void StackTraceCleanup();
DUMA_EXTERN_C void printStackTrace(char *buffer, int bufferSize,
char *mapFilename);
#endif
static const char version[] =
#ifndef GIT_SOURCE_VERSION
"DUMA ("
#else
GIT_SOURCE_VERSION
#endif
#ifdef DUMA_SO_LIBRARY
"shared library"
#elif DUMA_DLL_LIBRARY
"DLL library"
#elif DUMA_DETOURS
"detours"
#else
"static library"
#endif
#ifdef DUMA_NO_GLOBAL_MALLOC_FREE
", NO_GLOBAL_MALLOC_FREE"
#endif
#ifdef DUMA_NO_THREAD_SAFETY
", NO_THREAD_SAFETY"
#endif
#ifdef DUMA_NO_CPP_SUPPORT
", NO_CPP_SUPPORT"
#endif
#ifdef DUMA_NO_LEAKDETECTION
", NO_LEAKDETECTION"
#endif
#ifdef DUMA_EXPLICIT_INIT
", EXPLICIT_INIT"
#endif
#ifdef DUMA_PREFER_ATEXIT
", PREFER_ATEXIT"
#endif
#ifdef DUMA_PREFER_GETENV
", PREFER_GETENV"
#endif
")\n"
"Copyright (C) 2006 Michael Eddington <meddington@gmail.com>\n"
"Copyright (C) 2002-2009 Hayati Ayguen <h_ayguen@web.de>, Procitec GmbH\n"
"Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>\n\n";
#ifndef DUMA_NO_LEAKDETECTION
static const char unknown_file[] = "UNKNOWN (use #include \"duma.h\")";
#define DUMA_PARAMLIST_FL , const char *filename, int lineno
#define DUMA_PARAMS_FL , filename, lineno
#define DUMA_PARAMS_UK , unknown_file, 0
#else
#define DUMA_PARAMLIST_FL
#define DUMA_PARAMS_FL
#define DUMA_PARAMS_UK
#endif
#ifndef DUMA_PREFER_GETENV
#define DUMA_GETENV duma_getenv
#else
#define DUMA_GETENV getenv
#endif
/* Variable: MEMORY_CREATION_SIZE
*
* MEMORY_CREATION_SIZE is the amount of memory to get from the operating
* system at one time. We'll break that memory down into smaller pieces for
* malloc buffers. One megabyte is probably a good value.
*/
#define MEMORY_CREATION_SIZE 1024 * 1024
/* Enum: _DUMA_SlotState
*
* State of slot values (empty, free, etc)
*/
enum _DUMA_SlotState {
DUMAST_EMPTY /* slot not in use */
,
DUMAST_FREE /* internal memory reserved, unused by user */
,
DUMAST_IN_USE /* memory in use by allocator; see following enum AllocType */
,
DUMAST_ALL_PROTECTED /* memory no more used by allocator; memory is not
deallocated but protected */
,
DUMAST_BEGIN_PROTECTED /* most memory deallocated, but not page covering
* userAddress: slot holds userAddress, userSize and
* allocator.
*/
};
enum _DUMA_Slot_FileSource {
DUMAFS_EMPTY /* no filename, lineno */
,
DUMAFS_ALLOCATION /* filename, lineno from allocation */
,
DUMAFS_DEALLOCATION /* filename, lineno from deallocation */
};
enum _DUMA_InitState {
DUMAIS_UNINITIALIZED = 0x1611 /* not initialized */
,
DUMAIS_IN_CONSTRUCTOR /* in constructor _duma_init() */
,
DUMAIS_OUT_CONSTRUCTOR /* construction _duma_init() finished */
,
DUMAIS_IN_INIT /* in initializer duma_init() */
,
DUMAIS_OUT_INIT /* initialization duma_init() finished */
};
/*
* Struct Slot contains all of the information about a malloc buffer except
* for the contents of its memory.
*/
struct _DUMA_Slot {
void *internalAddress;
void *userAddress;
void *protAddress;
size_t internalSize;
size_t userSize;
/* save (some) space in production */
unsigned short state : 16;
#ifdef DUMA_NO_LEAKDETECTION
unsigned short allocator : 16;
#else
unsigned short allocator : 8;
unsigned short fileSource : 8;
#endif
#ifndef DUMA_NO_LEAKDETECTION
char *filename; /* filename of allocation */
int lineno; /* linenumber of allocation
* -1 == memory was allocated before duma_init()
* 0 == no leak information present
* >0 == leak information present
*/
#endif
/* Feature currently only works on win32 */
#if defined(WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__) && \
!defined(__MINGW64__)
char *stacktrace; /* stacktrace of allocation */
#endif
#ifdef DUMA_EXPLICIT_INIT
int slackfill;
#endif
};
enum _DUMA_AllocType {
DUMAAT_INTERNAL,
DUMAAT_MALLOC,
DUMAAT_NEW_ELEM,
DUMAAT_NEW_ARRAY,
DUMAAT_MEMBER_NEW_ELEM,
DUMAAT_MEMBER_NEW_ARRAY
};
enum _DUMA_AllocStd { DUMAAS_C, DUMAAS_CPP };
static const struct _DUMA_AllocDesc {
char *name;
enum _DUMA_AllocType type;
enum _DUMA_AllocStd std;
} _duma_allocDesc[] = {
{"duma allocate()", DUMAAT_INTERNAL, DUMAAS_C},
{"duma deallocate()", DUMAAT_INTERNAL, DUMAAS_C},
{"malloc()", DUMAAT_MALLOC, DUMAAS_C},
{"calloc()", DUMAAT_MALLOC, DUMAAS_C},
{"free()", DUMAAT_MALLOC, DUMAAS_C},
{"memalign()", DUMAAT_MALLOC, DUMAAS_C},
{"posix_memalign()", DUMAAT_MALLOC, DUMAAS_C},
{"realloc()", DUMAAT_MALLOC, DUMAAS_C},
{"valloc()", DUMAAT_MALLOC, DUMAAS_C},
{"strdup()", DUMAAT_MALLOC, DUMAAS_C},
{"scalar new", DUMAAT_NEW_ELEM, DUMAAS_CPP},
{"scalar delete", DUMAAT_NEW_ELEM, DUMAAS_CPP},
{"vector new[]", DUMAAT_NEW_ARRAY, DUMAAS_CPP},
{"vector delete[]", DUMAAT_NEW_ARRAY, DUMAAS_CPP},
{"member scalar new", DUMAAT_MEMBER_NEW_ELEM, DUMAAS_CPP},
{"member scalar delete", DUMAAT_MEMBER_NEW_ELEM, DUMAAS_CPP},
{"member vector new[]", DUMAAT_MEMBER_NEW_ARRAY, DUMAAS_CPP},
{"member vector delete[]", DUMAAT_MEMBER_NEW_ARRAY, DUMAAS_CPP}};
#ifdef DUMA_EXPLICIT_INIT
#define IF__DUMA_INIT_DONE if (DUMAIS_OUT_INIT == _duma_s.init_state)
#else
#define IF__DUMA_INIT_DONE
#endif
/* Collection of all global static non const variables for DUMA */
static struct _DUMA_GlobalStaticVars {
/* Protection Space A */
char acSpaceA[2 * DUMA_PAGE_SIZE];
int DUMA_IN_DUMA;
/* Variable: DUMA_DISABLE_BANNER
*
* DUMA_DISABLE_BANNER is a global variable used to control whether DUMA
* prints its usual startup message. Default is 0, meaning that the startup
* message gets printed.
*/
int DISABLE_BANNER;
/* Variable: DUMA_SKIPCOUNT_INIT
*
* DUMA_SKIPCOUNT_INIT controls after how many DUMA allocations the full
* internal initialization is done. Default is 0.
*/
int SKIPCOUNT_INIT;
/* Variable: CHECK_FREQ
*
* DUMA_CHECK_FREQ controls the frequency to check all memory blocks no man's
* land. The frequency counter is incremented at each memory allocation and
* deallocation. Whenever the counter reaches the value of DUMA_CHECK_FREQ the
* check is performed. 0 means no checks. 1 means to check always. Be careful
* with this value, it may get very time consuming. Default is 0.
*/
int CHECK_FREQ;
/* Variable: DUMA_REPORT_ALL_LEAKS
*
* DUMA_REPORT_ALL_LEAKS is a global variable used to control whether DUMA
* should all leaks - even without source filename/line number. Default is 0,
* meaning that only leaks with source information will get reported.
*/
int REPORT_ALL_LEAKS;
/* Variable: DUMA_SLACKFILL
*
* DUMA_SLACKFILL is set to 0-255. The slack / no mans land of all new
* allocated memory is filled with the specified value. default is set to
* initialise with 0xAA (=binary 10101010) initialisation to 0!
*/
int SLACKFILL;
/* Variable: DUMA_PROTECT_FREE
*
* DUMA_PROTECT_FREE is used to control the disposition of memory that is
* released using free(). It is all-caps so that its name
* matches the name of the environment variable that is used to set it.
* If its value is non-zero, memory released by free is made inaccessable.
* Any software that touches free memory will then get a segmentation fault.
* Depending on your application and your resources you may tell
* DUMA not to use this memory ever again by setting a negative
* value f.e. -1.
* You can tell DUMA to limit the sum of protected memory by setting
* a positive value, which is interpreted in kB.
* If its value is zero, freed memory will be available for reallocation,
* but will still be inaccessable until it is reallocated.
*/
long PROTECT_FREE;
/* Variable: DUMA_MAX_ALLOC
*
* DUMA_MAX_ALLOC is used to control the maximum memory print of the program
* in total: When the sum of allocated and protected memory would exceed
* this value in kB, the protected memory is freed/deleted.
*/
long MAX_ALLOC;
/* Variable: DUMA_MALLOC_0_STRATEGY
*
* DUMA_MALLOC_0_STRATEGY how DUMA should behave on malloc(0).
* 0 - like having former ALLOW_MALLOC_0 = 0 ==> abort program with
* segfault 1 - return NULL pointer 2 - return always the same pointer to some
* protected page 3 - return unique protected page (=default) ATTENTION: only
* 1 and 3 are ANSI conform. But value 1 will break most programs, cause value
* 3 is the usual one, the system libraries implement
*/
int MALLOC_0_STRATEGY;
/* Variable: DUMA_NEW_0_STRATEGY
*
* DUMA_NEW_0_STRATEGY how DUMA should behave on C++ operator new or new[]
* with size 0
* 2 - return always the same pointer to some protected page
* 3 - return unique protected page (=default)
* ATTENTION: only 3 is standard conform. Value 2 may break some but will
* work for most programs. With value 2 you may reduce the memory consumption.
*/
int NEW_0_STRATEGY;
/* Variable: DUMA_MALLOC_FAILEXIT
*
* DUMA_MALLOC_FAILEXIT controls the behaviour of DUMA when
* malloc() fails and would return NULL. But most applications don't
* check the return value for errors ... so
* default to Exit on Fail
*/
int MALLOC_FAILEXIT;
/* Variable: DUMA_FREE_ACCESS
*
* DUMA_FREE_ACCESS is set if DUMA is to write access memory before
* freeing it. This makes easier using watch expressions in debuggers as the
* process is interrupted even if the memory is going to be freed.
*/
int FREE_ACCESS;
/* Variable: DUMA_SHOW_ALLOC
*
* DUMA_SHOW_ALLOC is set if DUMA is to print all allocations
* and deallocations to the console. Although this generates a lot
* of messages, the option can be useful to detect inefficient code
* containing many allocations / deallocations
*/
int SHOW_ALLOC;
/* Variable: DUMA_SUPPRESS_ATEXIT
*
* DUMA_SUPPRESS_ATEXIT is set if DUMA is to suppress the installation of
* an exit handler, called at the exit of the main program. This handler
* allows for the detection of memory areas that have not been freed correctly
* before program exit, so the handler's installation should *normally* not be
* suppressed. One reason for doing so regardless are some buggy environments,
* where calls to the atexit()-function hang.
*/
int SUPPRESS_ATEXIT;
/* Variable: DUMA_MEMCPY_OVERLAP
*
* DUMA_MEMCPY_OVERLAP is set if the platform memcpy implementation allows
* copying overlapping regions when source > destination.
*/
int MEMCPY_OVERLAP;
/* Variable: _duma_allocListSize
*
* _duma_allocListSize is the size of the allocation list. This will always
* be a multiple of the page size.
*/
size_t allocListSize;
/* Variable: slotCount
*
* slotCount is the number of Slot structures in allocationList.
*/
size_t slotCount;
/* Variable: unUsedSlots
*
* unUsedSlots is the number of Slot structures that are currently available
* to represent new malloc buffers. When this number gets too low, we will
* create new slots.
*/
size_t unUsedSlots;
/* Variable: slotsPerPage
*
* slotsPerPage is the number of slot structures that fit in a virtual
* memory page.
*/
size_t slotsPerPage;
/* Variable: sumAllocatedMem
*
* internal variable: sum of allocated -freed +protected memory in kB
*/
long sumAllocatedMem;
/* Variable: sumTotalAllocatedMem
*
* internal variable: sum of allocated memory in kB
*/
long sumTotalAllocatedMem;
/* Variable: sumProtectedMem
*
* internal variable: sum of protected memory in kB
*/
long sumProtectedMem;
/* Variable: numDeallocs
*
* internal variable: number of deallocations processed so far
*/
long numDeallocs;
/* Variable: numAllocs
*
* internal variable: number of allocations processed so far
*/
long numAllocs;
/* Variable checkFreqCounter
*
* number of (de)allocations since last checks
*/
int checkFreqCounter;
/* Variable: duma_init_state
*
* internal variable: state of initialization
*/
enum _DUMA_InitState init_state;
/* memory block for malloc() or new with size 0 */
void *null_block;
/* Protection Space B */
char acSpaceB[2 * DUMA_PAGE_SIZE];
}
_duma_s =
{
"Static Protection Space Front" /* Protection Space A */
,
0 /* int DUMA_IN_DUMA; */
,
0 /* Variable: DISABLE_BANNER */
,
0 /* Variable: SKIPCOUNT_INIT */
,
0 /* Variable: CHECK_FREQ */
,
0 /* Variable: REPORT_ALL_LEAKS */
,
0xAA /* Variable: SLACKFILL */
,
-1L /* Variable: PROTECT_FREE */
,
-1L /* Variable: MAX_ALLOC */
,
3 /* Variable: MALLOC_0_STRATEGY; see above */
,
3 /* Variable: NEW_0_STRATEGY; see above */
,
1 /* Variable: MALLOC_FAILEXIT */
,
0 /* Variable: FREE_ACCESS */
,
0 /* Variable: SHOW_ALLOC */
,
0 /* Variable: SUPPRESS_ATEXIT */
,
0 /* Variable: MEMCPY_OVERLAP */
,
0 /* Variable: allocListSize */
,
0 /* Variable: slotCount */
,
0 /* Variable: unUsedSlots */
,
0 /* Variable: slotsPerPage */
,
0L /* Variable: sumAllocatedMem */
,
0L /* Variable: sumTotalAllocatedMem */
,
0L /* Variable: sumProtectedMem */
,
0L /* Variable: numDeallocs */
,
0L /* Variable: numAllocs */
,
0 /* Variable: checkFreqCounter */
,
DUMAIS_UNINITIALIZED /* Variable: duma_init_done */
,
(void *)0 /* Variable: null_block */
,
"Static Protection Space Back" /* Protection Space B */
};
DUMA_GLOBALVARS_T _duma_g = {
"Global Protection Space Front" /* Protection Space A */
,
(void *)0 /* Variable: allocList */
,
(void *)0 /* Variable: null_addr */
,
{DUMA_MIN_ALIGNMENT,
0 /* PROTECT_BELOW */
,
255 /* FILL */
#if !defined(DUMA_NO_CPP_SUPPORT) && !defined(DUMA_NO_LEAKDETECTION)
,
0 /* Magic */
,
0 /* DelPtr */
,
{0},
{0}
#endif
}
,
"Global Protection Space Back" /* Protection Space B */
};
/* Variable: DUMA_OUTPUT_DEBUG
*
* DUMA_OUTPUT_DEBUG is a global variable used to control if DUMA
* output is printed to the win32 debugging console. Default is 0,
* meaning that output is not by default sent to the debugging console.
*
* OS: WIN32 Only
*/
int DUMA_OUTPUT_DEBUG = 0;
/* Variable: DUMA_OUTPUT_STDOUT
*
* DUMA_OUTPUT_STDOUT is a global variable used to control if DUMA
* output is printed to STDOUT. Default is 0,
* meaning that output is not by default sent to STDOUT.
*/
int DUMA_OUTPUT_STDOUT = 0;
/* Variable: DUMA_OUTPUT_STDERR
*
* DUMA_OUTPUT_STDERR is a global variable used to control if DUMA
* output is printed to STDERR. Default is 1,
* meaning that output is by default sent to STDERR.
*/
int DUMA_OUTPUT_STDERR = 1;
/* Variable: DUMA_OUTPUT_FILE
*
* DUMA_OUTPUT_FILE is a global variable used to control if DUMA
* output is printed to a specified file. Default is NULL,
* meaning that output is not by default sent to a file.
*/
char *DUMA_OUTPUT_FILE = NULL;
/* char* DUMA_OUTPUT_FILE = "c:\\duma.txt"; */
/* Variable: DUMA_OUTPUT_STACKTRACE
*
* DUMA_OUTPUT_STACKTRACE is a global variable used to control if DUMA
* outputs a stacktrace of the allocation that is not free'd. Default is 0,
* meaning that this option is disabled.
*
* OS: WIN32 Only
*/
int DUMA_OUTPUT_STACKTRACE = 0;
/* Variable: DUMA_OUTPUT_STACKTRACE_MAPFILE
*
* DUMA_OUTPUT_STACKTRACE_MAPFILE is a global variable used to control
* what mapfile is used for stack traces. This is needed when using
* detours and duma. Default is NULL, indicating the system will try
* and guess.
*
* OS: WIN32 Only
*/
char *DUMA_OUTPUT_STACKTRACE_MAPFILE = NULL;
/*
* include helper functions
*/
#include "duma_hlp.h"
#ifndef DUMA_NO_LEAKDETECTION
/*
* declare exit function
*/
void
#if (defined(DUMA_GNU_INIT_ATTR) && !defined(DUMA_PREFER_ATEXIT))
__attribute((destructor))
#endif
_duma_exit(void);
#endif
/* Function: _duma_assert
*
* Print message and halt program execution in crazy way.
*/
void _duma_assert(const char *exprstr, const char *filename, int lineno) {
int *pcAddr = 0;
DUMA_Print("\nDUMA: DUMA_ASSERT(%s) failed at\n%s(%i)\n", exprstr, filename,
lineno);
/* this is "really" bad, but it works. assert() from assert.h system header
* stops only the current thread but the program goes on running under MS
* Visual C++. This way the program definitely halts.
*/
while (1)
*pcAddr++ = 0;
}
#ifndef DUMA_PREFER_GETENV
extern char **environ;
/* Function: duma_getenv
*
* replacement for standard C library function
*/
static const char *duma_getenv(const char *varname) {
const char *ret = NULL;
int varno = 0;
if (!varname)
return ret;
if (varname[0] == '\0')
return ret;
while (environ[varno]) {
const char *v = environ[varno++];
int idx = 0;
while (varname[idx] != '\0' && v[idx] == varname[idx])
++idx;
if (idx > 0 && varname[idx] == '\0' && v[idx] == '=')
return v + (idx + 1);
}
return ret;
}
#endif
static void duma_getenvvars(DUMA_TLSVARS_T *duma_tls) {
const char *string;
/*
* Import the user's environment specification of the default
* alignment for malloc(). We want that alignment to be under
* user control, since smaller alignment lets us catch more bugs,
* however some software will break if malloc() returns a buffer
* that is not word-aligned.
*
* I would like
* alignment to be zero so that we could catch all one-byte
* overruns, however if malloc() is asked to allocate an odd-size
* buffer and returns an address that is not word-aligned, or whose
* size is not a multiple of the word size, software breaks.
* This was the case with the Sun string-handling routines,
* which can do word fetches up to three bytes beyond the end of a
* string. I handle this problem in part by providing
* byte-reference-only versions of the string library functions, but
* there are other functions that break, too. Some in X Windows, one
* in Sam Leffler's TIFF library, and doubtless many others.
*/
if ((string = DUMA_GETENV("DUMA_ALIGNMENT")) != 0) {
duma_tls->ALIGNMENT = (size_t)atoi(string);
/* we could check for DUMA_MIN_ALIGNMENT. should we do so? */
if (!duma_tls->ALIGNMENT)
duma_tls->ALIGNMENT = 1;
}
/*
* See if the user wants to protect the address space below a buffer,
* rather than that above a buffer.
*/
if ((string = DUMA_GETENV("DUMA_PROTECT_BELOW")) != 0)
duma_tls->PROTECT_BELOW = (atoi(string) != 0);
/* Should we report all leaks? */
if ((string = DUMA_GETENV("DUMA_REPORT_ALL_LEAKS")) != 0)
_duma_s.REPORT_ALL_LEAKS = (atoi(string) != 0);
/*
* See if the user wants to protect memory that has been freed until
* the program exits, rather than until it is re-allocated.
* =-1 protect as much free'd memory as possible
* =0 do not protect free'd memory
* =N protect memory up to N kB
*/
if ((string = DUMA_GETENV("DUMA_PROTECT_FREE")) != 0)
_duma_s.PROTECT_FREE = atol(string);
/*
* See if the user has a memory usage limit. This controls the maximum
* memory print of the program in total: The sum of allocated and protected
* memory won't exceed this limit.
* =-1 use as much memory as possible
* =N limit total memory usage to N kB
*/
if ((string = DUMA_GETENV("DUMA_MAX_ALLOC")) != 0)
_duma_s.MAX_ALLOC = atol(string);
/*
* See what strategy the user wants for malloc(0).
*/
if ((string = DUMA_GETENV("MALLOC_0_STRATEGY")) != 0) {
int tmp = atoi(string);
if (tmp >= 0 && tmp <= 3)
_duma_s.MALLOC_0_STRATEGY = tmp;
}
/*
* See what strategy the user wants for C++ operator new with size zero.
*/
if ((string = DUMA_GETENV("NEW_0_STRATEGY")) != 0) {
int tmp = atoi(string);
if (tmp >= 2 && tmp <= 3)
_duma_s.NEW_0_STRATEGY = tmp;
}
/*
* See if the user wants to exit on malloc() failure
*/
if ((string = DUMA_GETENV("DUMA_MALLOC_FAILEXIT")) != 0)
_duma_s.MALLOC_FAILEXIT = (atoi(string) != 0);
/*
* See if the user wants to write access freed memory
*/
if ((string = DUMA_GETENV("DUMA_FREE_ACCESS")) != 0)
_duma_s.FREE_ACCESS = (atoi(string) != 0);
/*
* Check if we should be filling new memory with a value.
*/
if ((string = DUMA_GETENV("DUMA_FILL")) != 0) {
duma_tls->FILL = atoi(string);
if (-1 != duma_tls->FILL)
duma_tls->FILL &= 255;
}
/*
* Check with which value the memories no mans land is filled
*/
if ((string = DUMA_GETENV("DUMA_SLACKFILL")) != 0)
_duma_s.SLACKFILL = atoi(string);
_duma_s.SLACKFILL &= 255;
/*
* See if the user wants to see allocations / frees
*/
if ((string = DUMA_GETENV("DUMA_SHOW_ALLOC")) != 0)
_duma_s.SHOW_ALLOC = (atoi(string) != 0);
/*
* See if the user wants to call atexit()
*/
if ((string = DUMA_GETENV("DUMA_SUPPRESS_ATEXIT")) != 0)
_duma_s.SUPPRESS_ATEXIT = (atoi(string) != 0);
/*
* See if user allows memcpy region overlapping
*/
if ((string = DUMA_GETENV("DUMA_MEMCPY_OVERLAP")) != 0)
_duma_s.MEMCPY_OVERLAP = (atoi(string) != 0);
/*
* DUMA_OUTPUT_STACKTRACE is a global variable used to control if DUMA
* outputs a stacktrace of the allocation that is not free'd. Default is 0,
* meaning that this option is disabled.
*/
if ((string = DUMA_GETENV("DUMA_OUTPUT_STACKTRACE")) != 0)
DUMA_OUTPUT_STACKTRACE = (atoi(string) != 0);
/*
* DUMA_OUTPUT_STACKTRACE is a global variable used to control if DUMA
* outputs a stacktrace of the allocation that is not free'd. Default is 0,
* meaning that this option is disabled.
*/
if ((string = DUMA_GETENV("DUMA_OUTPUT_STACKTRACE_MAPFILE")) != 0)
#ifndef DUMA_NO_LEAKDETECTION
DUMA_OUTPUT_STACKTRACE_MAPFILE = _duma_strdup(string, __FILE__, __LINE__);
#else
DUMA_OUTPUT_STACKTRACE_MAPFILE = _duma_strdup(string);
#endif
/*
* DUMA_OUTPUT_DEBUG is a global variable used to control if DUMA
* output is printed to the win32 debugging console. Default is 0,
* meaning that output is not by default sent to the debugging console.
*/
if ((string = DUMA_GETENV("DUMA_OUTPUT_DEBUG")) != 0)
DUMA_OUTPUT_DEBUG = (atoi(string) != 0);
/*
* DUMA_OUTPUT_STDOUT is a global variable used to control if DUMA
* output is printed to STDOUT. Default is 0,
* meaning that output is not by default sent to STDOUT.
*/
if ((string = DUMA_GETENV("DUMA_OUTPUT_STDOUT")) != 0)
DUMA_OUTPUT_STDOUT = (atoi(string) != 0);
/*
* DUMA_OUTPUT_STDERR is a global variable used to control if DUMA
* output is printed to STDERR. Default is 1,
* meaning that output is by default sent to STDERR.
*/
if ((string = DUMA_GETENV("DUMA_OUTPUT_STDERR")) != 0)
DUMA_OUTPUT_STDERR = (atoi(string) != 0);
/*
* DUMA_OUTPUT_FILE is a global variable used to control if DUMA
* output is printed to a specified file. Default is NULL,
* meaning that output is not by default sent to a file.
*/
if ((string = DUMA_GETENV("DUMA_OUTPUT_FILE")) != 0)
#ifndef DUMA_NO_LEAKDETECTION
DUMA_OUTPUT_FILE = _duma_strdup(string, __FILE__, __LINE__);
#else
DUMA_OUTPUT_FILE = _duma_strdup(string);
#endif
/* Get Value for DUMA_SKIPCOUNT_INIT */
if ((string = DUMA_GETENV("DUMA_SKIPCOUNT_INIT")) != 0)
_duma_s.SKIPCOUNT_INIT = (atoi(string) != 0);
/* Get Value for DUMA_CHECK_FREQ */
if ((string = DUMA_GETENV("DUMA_CHECK_FREQ")) != 0) {
int tmp = atoi(string);
if (tmp > 0)
_duma_s.CHECK_FREQ = tmp;
}
/* Should we send banner? */
if ((string = DUMA_GETENV("DUMA_DISABLE_BANNER")) != 0)
_duma_s.DISABLE_BANNER = (atoi(string) != 0);
if (!_duma_s.DISABLE_BANNER)
DUMA_Print(version);
}
/* Function: duma_init
*
* duma_init sets configuration settings. Can sometimes cause problems
* when called from _duma_init.
*
* duma_init is called from _duma_init unless DUMA_EXPLICIT_INIT
* is defined at compile time.
*
* See Also: <_duma_init>
*/
#ifndef DUMA_EXPLICIT_INIT
static
#endif
void
duma_init(void) {
void *testAlloc;
DUMA_TLSVARS_T *duma_tls;
/* avoid double call, when initialization already in progress */
if (_duma_s.init_state >= DUMAIS_IN_INIT &&
_duma_s.init_state <= DUMAIS_OUT_INIT)
return;
else {
#if DUMA_DETOURS
_duma_init();
#endif
_duma_s.init_state = DUMAIS_IN_INIT;
}
duma_tls = GET_DUMA_TLSVARS();
duma_getenvvars(duma_tls);
#if (!defined(DUMA_NO_LEAKDETECTION) && \
(defined(DUMA_PREFER_ATEXIT) || !defined(DUMA_GNU_INIT_ATTR)))
/*
* Register atexit()
* a) when we have Leak Detection and atexit() is preferred over
* GNU_INIT_ATTR b) when we have Leak Detection and GNU_INIT_ATTR is not set
*/
#ifndef DUMA_NO_HANG_MSG
if (0 == _duma_s.SUPPRESS_ATEXIT)
DUMA_Print("\nDUMA: Registering with atexit().\n"
#ifdef WIN32
"DUMA: If this hangs, change the library initialization order "
"with DUMA_EXPLICIT_INIT.\n");
#else
"DUMA: If this hangs, change the library load/init order with "
"DUMA_EXPLICIT_INIT or LD_PRELOAD.\n");
#endif
else
DUMA_Print("\nDUMA: Skipping registering with atexit(). Set "
"DUMA_SUPPRESS_ATEXIT to 0 to register.\n");
#endif /* DUMA_NO_HANG_MSG */
if (!_duma_s.SUPPRESS_ATEXIT) {
if (atexit(_duma_exit))
DUMA_Abort("Cannot register exit function.\n");
#ifndef DUMA_NO_HANG_MSG
DUMA_Print("DUMA: Registration was successful.\n");
#endif /* DUMA_NO_HANG_MSG */
}
#endif /* ( !defined(DUMA_NO_LEAKDETECTION) && ( defined(DUMA_PREFER_ATEXIT) \
|| !defined(DUMA_GNU_INIT_ATTR) ) ) */
/* initialize semaphoring */
DUMA_INIT_SEMAPHORE();
#ifndef DUMA_NO_GLOBAL_MALLOC_FREE
/*
* Check whether malloc and free is available
*/
testAlloc = malloc(123);
if (_duma_s.numAllocs == 0)
DUMA_Abort("malloc() is not bound to duma.\nDUMA Aborting: Preload lib "
"with 'LD_PRELOAD=libduma.so <prog>'.\n");