-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
dmalloc_t.c
4439 lines (3817 loc) · 115 KB
/
dmalloc_t.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
/*
* Test program for malloc code
*
* Copyright 2020 by Gray Watson
*
* This file is part of the dmalloc package.
*
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all
* copies, and that the name of Gray Watson not be used in advertising
* or publicity pertaining to distribution of the document or software
* without specific, written prior permission.
*
* Gray Watson makes no representations about the suitability of the
* software described herein for any purpose. It is provided "as is"
* without express or implied warranty.
*
* The author may be contacted via https://dmalloc.com/
*/
/*
* Test program for the malloc library. Current it is interactive although
* should be script based.
*/
#include <stdio.h> /* for stdin */
#if HAVE_STDLIB_H
# include <stdlib.h> /* for atoi + */
#endif
#if HAVE_STRING_H
# include <string.h>
#endif
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
#include "conf.h"
#include "append.h"
#include "compat.h" /* for loc_snprintf */
#if HAVE_TIME
# ifdef TIME_INCLUDE
# include TIME_INCLUDE
# endif
#endif
#include "dmalloc.h"
#include "dmalloc_argv.h"
#include "dmalloc_rand.h"
#include "arg_check.h"
/*
* NOTE: these are only needed to test certain features of the library.
*/
#include "debug_tok.h"
#include "error_val.h"
#include "heap.h" /* for external testing */
#define INTER_CHAR 'i'
#define DEFAULT_ITERATIONS 10000
#define MAX_POINTERS 1024
#if HAVE_SBRK == 0 && HAVE_MMAP == 0
/* if we have a small memory area then just take 1/10 of the internal space */
#define MAX_ALLOC (INTERNAL_MEMORY_SPACE / 10)
#else
/* otherwise allocate a megabyte */
#define MAX_ALLOC (1024 * 1024)
#endif
#define MIN_AVAIL 10
/* pointer tracking structure */
typedef struct pnt_info_st {
long pi_crc; /* crc of storage */
int pi_size; /* size of storage */
void *pi_pnt; /* pnt to storage */
struct pnt_info_st *pi_next; /* pnt to next */
} pnt_info_t;
static pnt_info_t *pointer_grid;
/* argument variables */
static long default_iter_n = DEFAULT_ITERATIONS; /* # of iters */
static char *env_string = NULL; /* env options */
static int interactive_b = ARGV_FALSE; /* interactive flag */
static int log_trans_b = ARGV_FALSE; /* log transactions */
static int no_special_b = ARGV_FALSE; /* no-special flag */
static long max_alloc = MAX_ALLOC; /* amt of mem to use */
static long max_pointers = MAX_POINTERS; /* # of pnts to use */
static int random_debug_b = ARGV_FALSE; /* random flag */
static int silent_b = ARGV_FALSE; /* silent flag */
static unsigned int seed_random = 0; /* random seed */
static int verbose_b = ARGV_FALSE; /* verbose flag */
static argv_t arg_list[] = {
{ INTER_CHAR, "interactive", ARGV_BOOL_INT, &interactive_b,
NULL, "turn on interactive mode" },
{ 'e', "env-string", ARGV_CHAR_P, &env_string,
"string", "string of env commands to set" },
{ 'l', "log-trans", ARGV_BOOL_INT, &log_trans_b,
NULL, "log transactions via tracking-func" },
{ 'm', "max-alloc", ARGV_SIZE, &max_alloc,
"bytes", "maximum allocation to test" },
{ 'n', "no-special", ARGV_BOOL_INT, &no_special_b,
NULL, "do not run special tests" },
{ 'p', "max-pointers", ARGV_SIZE, &max_pointers,
"pointers", "number of pointers to test" },
{ 'r', "random-debug", ARGV_BOOL_INT, &random_debug_b,
NULL, "randomly change debug flag" },
{ 's', "silent", ARGV_BOOL_INT, &silent_b,
NULL, "do not display messages" },
{ 'S', "seed-random", ARGV_U_INT, &seed_random,
"number", "seed for random function" },
{ 't', "times", ARGV_SIZE, &default_iter_n,
"number", "number of iterations to run" },
{ 'v', "verbose", ARGV_BOOL_INT, &verbose_b,
NULL, "enables verbose messages" },
{ ARGV_LAST }
};
/*
* Hexadecimal STR to address translation
*/
static DMALLOC_PNT hex_to_address(const char *str)
{
PNT_ARITH_TYPE ret;
/* strip off spaces */
for (; *str == ' ' || *str == '\t'; str++) {
}
/* skip a leading 0[xX] */
if (*str == '0' && (*(str + 1) == 'x' || *(str + 1) == 'X')) {
str += 2;
}
for (ret = 0;; str++) {
if (*str >= '0' && *str <= '9') {
ret = ret * 16 + (*str - '0');
}
else if (*str >= 'a' && *str <= 'f') {
ret = ret * 16 + (*str - 'a' + 10);
}
else if (*str >= 'A' && *str <= 'F') {
ret = ret * 16 + (*str - 'A' + 10);
}
else {
break;
}
}
return (DMALLOC_PNT)ret;
}
/*
* Read an address from the user
*/
static DMALLOC_PNT get_address(void)
{
char line[80];
DMALLOC_PNT pnt;
do {
loc_printf("Enter a hex address: ");
if (fgets(line, sizeof(line), stdin) == NULL) {
return NULL;
}
} while (line[0] == '\0');
pnt = hex_to_address(line);
return pnt;
}
/*
* Free a slot from the used_p list and put it on the free list
*/
static void free_slot(const int iter_c, pnt_info_t *slot_p,
pnt_info_t **used_pp, pnt_info_t **free_pp)
{
pnt_info_t *this_p, *prev_p;
if (verbose_b) {
unsigned long diff = ((PNT_ARITH_TYPE)slot_p - (PNT_ARITH_TYPE)pointer_grid);
loc_printf("%d: free'd %d bytes from slot %lu (%p)\n",
iter_c + 1, slot_p->pi_size, diff, slot_p->pi_pnt);
}
slot_p->pi_pnt = NULL;
/* find pnt in the used list */
for (this_p = *used_pp, prev_p = NULL;
this_p != NULL;
prev_p = this_p, this_p = this_p->pi_next) {
if (this_p == slot_p) {
break;
}
}
if (prev_p == NULL) {
*used_pp = slot_p->pi_next;
}
else {
prev_p->pi_next = slot_p->pi_next;
}
slot_p->pi_next = *free_pp;
*free_pp = slot_p;
}
/*
* Try ITER_N random program iterations, returns 1 on success else 0
*/
static int do_random(const int iter_n)
{
char *old_env, env_buf[256];
unsigned int flags;
int iter_c, prev_errno, amount, max_avail, free_c;
int final = 1;
char *chunk_p;
pnt_info_t *free_p, *used_p = NULL;
pnt_info_t *pnt_p;
max_avail = max_alloc;
old_env = dmalloc_debug_current_env(env_buf, sizeof(env_buf));
flags = dmalloc_debug_current();
pointer_grid = (pnt_info_t *)malloc(sizeof(pnt_info_t) * max_pointers);
if (pointer_grid == NULL) {
loc_printf("%s: problems allocating space for %ld pointer slots.\n",
argv_program, max_pointers);
return 0;
}
/* initialize free list */
free_p = pointer_grid;
for (pnt_p = pointer_grid; pnt_p < pointer_grid + max_pointers; pnt_p++) {
pnt_p->pi_size = 0;
pnt_p->pi_pnt = NULL;
pnt_p->pi_next = pnt_p + 1;
}
/* redo the last next pointer */
(pnt_p - 1)->pi_next = NULL;
free_c = max_pointers;
prev_errno = DMALLOC_ERROR_NONE;
for (iter_c = 0; iter_c < iter_n;) {
int which_func, which;
if (dmalloc_errno != prev_errno && ! silent_b) {
loc_printf("ERROR: iter %d, %s (err %d)\n",
iter_c, dmalloc_strerror(dmalloc_errno), dmalloc_errno);
prev_errno = dmalloc_errno;
final = 0;
}
/* special case when doing non-linear stuff, no memory available */
if (max_avail < MIN_AVAIL && free_p == NULL) {
break;
}
if (random_debug_b) {
unsigned int new_flag;
which = _dmalloc_rand() % (sizeof(int) * 8);
new_flag = 1 << which;
flags ^= new_flag;
if (verbose_b) {
loc_printf("%d: debug flags = %#x\n", iter_c + 1, flags);
}
dmalloc_debug(flags);
}
/* decide whether to malloc a new pointer or free/realloc an existing */
which = _dmalloc_rand() % 4;
if ((free_p == NULL
|| which == 3
|| max_avail < MIN_AVAIL
|| free_c == max_pointers)
&& used_p != NULL) {
/* choose a random slot to free */
which = _dmalloc_rand() % (max_pointers - free_c);
for (pnt_p = used_p; which > 0; which--) {
pnt_p = pnt_p->pi_next;
}
free(pnt_p->pi_pnt);
// NOTE: free_slot logs verbose
free_slot(iter_c, pnt_p, &used_p, &free_p);
free_c++;
max_avail += pnt_p->pi_size;
iter_c++;
continue;
}
/* sanity check */
if (free_p == NULL) {
loc_fprintf(stderr, "%s: problem with test program free list\n",
argv_program);
exit(1);
}
/* rest are allocations */
amount = _dmalloc_rand() % (max_avail / 2);
#if ALLOW_ALLOC_ZERO_SIZE == 0
if (amount == 0) {
amount = 1;
}
#endif
which_func = _dmalloc_rand() % 9;
switch (which_func) {
/* malloc */
case 0:
pnt_p = free_p;
pnt_p->pi_pnt = malloc(amount);
if (verbose_b) {
unsigned long diff = ((PNT_ARITH_TYPE)pnt_p - (PNT_ARITH_TYPE)pointer_grid);
loc_printf("%d: malloc %d of max %d into slot %lu. got %p\n",
iter_c + 1, amount, max_avail, diff, pnt_p->pi_pnt);
}
break;
/* calloc */
case 1:
pnt_p = free_p;
pnt_p->pi_pnt = calloc(amount, sizeof(char));
if (verbose_b) {
unsigned long diff = ((PNT_ARITH_TYPE)pnt_p - (PNT_ARITH_TYPE)pointer_grid);
loc_printf("%d: calloc %d of max %d into slot %lu. got %p\n",
iter_c + 1, amount, max_avail, diff, pnt_p->pi_pnt);
}
/* test the returned block to make sure that is has been cleared */
if (pnt_p->pi_pnt != NULL) {
for (chunk_p = pnt_p->pi_pnt;
chunk_p < (char *)pnt_p->pi_pnt + amount;
chunk_p++) {
if (*chunk_p != '\0') {
if (! silent_b) {
loc_printf("calloc of %d was not fully zeroed on iteration #%d\n",
amount, iter_c + 1);
}
break;
}
}
}
break;
/* realloc */
case 2:
if (free_c == max_pointers) {
continue;
}
which = _dmalloc_rand() % (max_pointers - free_c);
for (pnt_p = used_p; which > 0; which--) {
pnt_p = pnt_p->pi_next;
}
pnt_p->pi_pnt = realloc(pnt_p->pi_pnt, amount);
/*
* note that we've free the old size, we'll account for the
* alloc below
*/
max_avail += pnt_p->pi_size;
if (verbose_b) {
unsigned long diff = ((PNT_ARITH_TYPE)pnt_p - (PNT_ARITH_TYPE)pointer_grid);
loc_printf("%d: realloc %d from %d of max %d slot %lu. got %p\n",
iter_c + 1, amount, pnt_p->pi_size, max_avail,
diff, pnt_p->pi_pnt);
}
if (amount == 0) {
free_slot(iter_c, pnt_p, &used_p, &free_p);
free_c++;
pnt_p = NULL;
continue;
}
break;
/* recalloc */
case 3:
if (free_c == max_pointers) {
continue;
}
which = _dmalloc_rand() % (max_pointers - free_c);
for (pnt_p = used_p; which > 0; which--) {
pnt_p = pnt_p->pi_next;
}
pnt_p->pi_pnt = recalloc(pnt_p->pi_pnt, amount);
/*
* note that we've free the old size, we'll account for the
* alloc below
*/
max_avail += pnt_p->pi_size;
if (verbose_b) {
unsigned long diff = ((PNT_ARITH_TYPE)pnt_p - (PNT_ARITH_TYPE)pointer_grid);
loc_printf("%d: recalloc %d from %d of max %d slot %lu. got %p\n",
iter_c + 1, amount, pnt_p->pi_size, max_avail, diff, pnt_p->pi_pnt);
}
/* test the returned block to make sure that is has been cleared */
if (pnt_p->pi_pnt != NULL && amount > pnt_p->pi_size) {
for (chunk_p = (char *)pnt_p->pi_pnt + pnt_p->pi_size;
chunk_p < (char *)pnt_p->pi_pnt + amount;
chunk_p++) {
if (*chunk_p != '\0') {
if (! silent_b) {
loc_printf("recalloc %d from %d was not fully zeroed on iteration #%d\n",
amount, pnt_p->pi_size, iter_c + 1);
}
break;
}
}
}
if (amount == 0) {
free_slot(iter_c, pnt_p, &used_p, &free_p);
free_c++;
pnt_p = NULL;
continue;
}
break;
/* valloc */
case 4:
pnt_p = free_p;
pnt_p->pi_pnt = valloc(amount);
if (verbose_b) {
unsigned long diff = ((PNT_ARITH_TYPE)pnt_p - (PNT_ARITH_TYPE)pointer_grid);
loc_printf("%d: valloc %d of max %d into slot %lu. got %p\n",
iter_c + 1, amount, max_avail, diff, pnt_p->pi_pnt);
}
break;
/* heap alloc */
case 5:
/* do it less often then the other functions */
which = _dmalloc_rand() % 5;
if (which == 3 && amount > 0) {
void *mem;
mem = _dmalloc_heap_alloc(amount);
if (verbose_b) {
loc_printf("%d: heap alloc %d of max %d bytes. got %p\n",
iter_c + 1, amount, max_avail, mem);
}
iter_c++;
}
/* don't store the memory */
continue;
break;
/* heap check in the middle */
case 6:
/* do it less often then the other functions */
which = _dmalloc_rand() % 20;
if (which == 7) {
if (dmalloc_verify(NULL /* check all heap */) != DMALLOC_NOERROR) {
if (! silent_b) {
loc_printf("%d: ERROR dmalloc_verify failed\n", iter_c + 1);
}
final = 0;
}
iter_c++;
}
continue;
break;
#if HAVE_STRDUP
/* strdup */
case 7:
{
char str[] = "this is a test of the emergency broadcasting system, the broadcasters in your area would like you to know that this system has really never been fully tested so we have no idea if it would actually work in the advent of a real disaster.";
amount = _dmalloc_rand() % strlen(str);
str[amount] = '\0';
pnt_p = free_p;
pnt_p->pi_pnt = strdup(str);
if (verbose_b) {
/* the amount includes the \0 */
unsigned long diff = ((PNT_ARITH_TYPE)pnt_p - (PNT_ARITH_TYPE)pointer_grid);
loc_printf("%d: strdup %d of max %d into slot %lu. got %p\n",
iter_c + 1, amount + 1, max_avail, diff, pnt_p->pi_pnt);
}
}
break;
#endif
#if HAVE_STRNDUP
/* strndup */
case 8:
{
char str[] = "this is a test of the emergency broadcasting system, the broadcasters in your area would like you to know that this system has really never been fully tested so we have no idea if it would actually work in the advent of a real disaster.";
amount = _dmalloc_rand() % strlen(str);
pnt_p = free_p;
pnt_p->pi_pnt = strndup(str, amount);
if (verbose_b) {
unsigned long diff = ((PNT_ARITH_TYPE)pnt_p - (PNT_ARITH_TYPE)pointer_grid);
loc_printf("%d: strdup %d of max %d into slot %lu. got %p\n",
iter_c + 1, amount, max_avail, diff, pnt_p->pi_pnt);
}
}
break;
#endif
default:
continue;
break;
}
if (pnt_p->pi_pnt == NULL) {
if (! silent_b) {
loc_printf("%d: ERROR allocation of %d returned error\n", iter_c + 1, amount);
}
final = 0;
iter_c++;
continue;
}
/* set the size and take it off the free-list and put on used list */
pnt_p->pi_size = amount;
if (pnt_p == free_p) {
free_p = pnt_p->pi_next;
pnt_p->pi_next = used_p;
used_p = pnt_p;
free_c--;
}
max_avail -= amount;
iter_c++;
continue;
}
/* free used pointers */
for (pnt_p = pointer_grid; pnt_p < pointer_grid + max_pointers; pnt_p++) {
if (pnt_p->pi_pnt != NULL) {
free(pnt_p->pi_pnt);
}
}
free(pointer_grid);
dmalloc_debug_setup(old_env);
return final;
}
/*
* Do some special tests as soon as we run the test program. Returns
* 1 on success else 0.
*/
static int check_initial_special(void)
{
void *pnt;
int final = 1, iter_c;
char *old_env, env_buf[256];
/********************/
do {
int amount;
/*
* So I ran across a bad check for the seen value versus the
* iteration count. It was only seen when there were a greater
* number of reallocs to the same pointer at the start of the
* program running.
*/
if (! silent_b) {
loc_printf(" Checking realloc(malloc) seen count\n");
}
do {
/* NOTE: must be less than 1024 because below depends on this */
amount = _dmalloc_rand() % 10;
} while (amount == 0);
pnt = malloc(amount);
if (pnt == NULL) {
if (! silent_b) {
loc_printf(" ERROR: could not allocate %d bytes.\n", amount);
}
final = 0;
break;
}
for (iter_c = 0; iter_c < 100; iter_c++) {
/* change the amount */
pnt = realloc(pnt, amount);
if (pnt == NULL) {
if (! silent_b) {
loc_printf(" ERROR: could not reallocate %d bytes.\n", amount);
}
final = 0;
break;
}
}
if (pnt == NULL) {
break;
}
/*
* now try freeing the pointer with a free that provides a return
* value
*/
if (dmalloc_free(__FILE__, __LINE__, pnt,
DMALLOC_FUNC_FREE) != FREE_NOERROR) {
if (! silent_b) {
loc_printf(" ERROR: free of realloc(malloc) pointer %p failed.\n", pnt);
}
final = 0;
}
/* now check the heap to verify tha the freed slot is good */
if (dmalloc_verify(NULL /* check all heap */) != DMALLOC_NOERROR) {
if (! silent_b) {
loc_printf(" ERROR: dmalloc_verify failed\n");
}
final = 0;
}
} while(0);
/********************/
#define NEVER_REUSE_ITERS 20
{
void *new_pnt, *pnts[NEVER_REUSE_ITERS];
unsigned int old_flags, amount, check_c;
if (! silent_b) {
loc_printf(" Checking never-reuse token\n");
}
old_env = dmalloc_debug_current_env(env_buf, sizeof(env_buf));
old_flags = dmalloc_debug_current();
dmalloc_debug(old_flags | DMALLOC_DEBUG_NEVER_REUSE);
for (iter_c = 0; iter_c < NEVER_REUSE_ITERS; iter_c++) {
amount = 1024;
pnts[iter_c] = malloc(amount);
if (pnts[iter_c] == NULL) {
if (! silent_b) {
loc_printf(" ERROR: could not allocate %d bytes.\n", amount);
}
final = 0;
break;
}
}
/* now free them */
for (iter_c = 0; iter_c < NEVER_REUSE_ITERS; iter_c++) {
if (dmalloc_free(__FILE__, __LINE__, pnts[iter_c],
DMALLOC_FUNC_FREE) != FREE_NOERROR) {
if (! silent_b) {
loc_printf(" ERROR: free of pointer %p failed.\n", pnts[iter_c]);
}
final = 0;
}
}
/*
* now allocate them again and make sure we don't get the same
* pointers
*/
for (iter_c = 0; iter_c < NEVER_REUSE_ITERS; iter_c++) {
amount = 1024;
new_pnt = malloc(amount);
if (new_pnt == NULL) {
if (! silent_b) {
loc_printf(" ERROR: could not allocate %d bytes.\n", amount);
}
final = 0;
}
/* did we get a previous pointer? */
for (check_c = 0; check_c < NEVER_REUSE_ITERS; check_c++) {
if (new_pnt == pnts[check_c]) {
if (! silent_b) {
loc_printf(" ERROR: pointer %p was improperly reused.\n", new_pnt);
}
final = 0;
break;
}
}
if (dmalloc_free(__FILE__, __LINE__, new_pnt,
DMALLOC_FUNC_FREE) != FREE_NOERROR) {
if (! silent_b) {
loc_printf(" ERROR: free of pointer %p failed.\n", new_pnt);
}
final = 0;
}
}
dmalloc_debug_setup(old_env);
}
/********************/
return final;
}
/*
* Make sure that some of the arg check stuff works.
*/
static int check_arg_check(void)
{
char *old_env, env_buf[256];
unsigned int old_flags;
char *func;
int our_errno_hold = dmalloc_errno;
int size, final = 1;
char *pnt, *pnt2, hold_ch;
old_env = dmalloc_debug_current_env(env_buf, sizeof(env_buf));
old_flags = dmalloc_debug_current();
if (! silent_b) {
loc_printf(" Checking arg-check functions\n");
}
/*
* enable function checking and remove check-fence which caused
* extra errors
*/
dmalloc_debug((old_flags | DMALLOC_DEBUG_CHECK_FUNCS) & (~DMALLOC_DEBUG_CHECK_FENCE));
size = 5;
pnt = malloc(size);
if (pnt == NULL) {
if (! silent_b) {
loc_printf(" ERROR: could not malloc %d bytes.\n", size);
}
return 0;
}
pnt2 = malloc(size * 2);
if (pnt2 == NULL) {
if (! silent_b) {
loc_printf(" ERROR: could not malloc %d bytes.\n", size * 2);
}
return 0;
}
/*********/
#if HAVE_ATOI
func = "atoi";
if (! silent_b) {
loc_printf(" Checking %s\n", func);
}
/* they should be the same */
dmalloc_errno = DMALLOC_ERROR_NONE;
memcpy(pnt, "1234", size);
if (_dmalloc_atoi(__FILE__, __LINE__, pnt) != 1234) {
if (! silent_b) {
loc_printf(" ERROR: %s overload failed\n", func);
}
final = 0;
}
if (dmalloc_errno != DMALLOC_ERROR_NONE) {
if (! silent_b) {
loc_printf(" ERROR: %s overload should not get error: %s (%d)\n",
func, dmalloc_strerror(dmalloc_errno), dmalloc_errno);
}
final = 0;
}
/* they should be different */
dmalloc_errno = DMALLOC_ERROR_NONE;
memcpy(pnt, "12345", size);
if (_dmalloc_atoi(__FILE__, __LINE__, pnt) != 12345) {
if (! silent_b) {
loc_printf(" ERROR: %s overload failed\n", func);
}
final = 0;
}
if (dmalloc_errno != DMALLOC_ERROR_WOULD_OVERWRITE) {
if (! silent_b) {
loc_printf(" ERROR: %s overload should get error\n", func);
}
final = 0;
}
#endif
/*********/
#if HAVE_ATOL
func = "atol";
if (! silent_b) {
loc_printf(" Checking %s\n", func);
}
/* they should be the same */
dmalloc_errno = DMALLOC_ERROR_NONE;
memcpy(pnt, "1234", size);
if (_dmalloc_atol(__FILE__, __LINE__, pnt) != 1234) {
if (! silent_b) {
loc_printf(" ERROR: %s overload failed\n", func);
}
final = 0;
}
if (dmalloc_errno != DMALLOC_ERROR_NONE) {
if (! silent_b) {
loc_printf(" ERROR: %s overload should not get error: %s (%d)\n",
func, dmalloc_strerror(dmalloc_errno), dmalloc_errno);
}
final = 0;
}
/* they should be different */
dmalloc_errno = DMALLOC_ERROR_NONE;
memcpy(pnt, "12345", size);
if (_dmalloc_atol(__FILE__, __LINE__, pnt) != 12345) {
if (! silent_b) {
loc_printf(" ERROR: %s overload failed\n", func);
}
final = 0;
}
if (dmalloc_errno != DMALLOC_ERROR_WOULD_OVERWRITE) {
if (! silent_b) {
loc_printf(" ERROR: %s overload should get error\n", func);
}
final = 0;
}
#endif
/*********/
#if HAVE_BCMP
func = "bcmp";
if (! silent_b) {
loc_printf(" Checking %s\n", func);
}
/* they should be the same */
dmalloc_errno = DMALLOC_ERROR_NONE;
memset(pnt, 1, size);
memset(pnt2, 1, size);
if (_dmalloc_bcmp(__FILE__, __LINE__, pnt, pnt2, size) != 0) {
if (! silent_b) {
loc_printf(" ERROR: %s overload failed\n", func);
}
final = 0;
}
if (dmalloc_errno != DMALLOC_ERROR_NONE) {
if (! silent_b) {
loc_printf(" ERROR: %s overload should not get error: %s (%d)\n",
func, dmalloc_strerror(dmalloc_errno), dmalloc_errno);
}
final = 0;
}
/* they should be different */
dmalloc_errno = DMALLOC_ERROR_NONE;
memset(pnt, 1, size);
memset(pnt2, 2, size);
if (_dmalloc_bcmp(__FILE__, __LINE__, pnt, pnt2, size) == 0) {
if (! silent_b) {
loc_printf(" ERROR: %s overload failed\n", func);
}
final = 0;
}
if (dmalloc_errno != DMALLOC_ERROR_NONE) {
if (! silent_b) {
loc_printf(" ERROR: %s overload should not get error: %s (%d)\n",
func, dmalloc_strerror(dmalloc_errno), dmalloc_errno);
}
final = 0;
}
/* this should cause an error */
dmalloc_errno = DMALLOC_ERROR_NONE;
_dmalloc_bcmp(__FILE__, __LINE__, pnt, pnt2, size + 1);
if (dmalloc_errno != DMALLOC_ERROR_WOULD_OVERWRITE) {
if (! silent_b) {
loc_printf(" ERROR: %s overload did not register overwrite\n", func);
}
final = 0;
}
#endif
/*********/
#if HAVE_BCOPY
func = "bcopy";
if (! silent_b) {
loc_printf(" Checking %s\n", func);
}
/* this copies the right number of characters into buffer */
dmalloc_errno = DMALLOC_ERROR_NONE;
memset(pnt, 2, size);
_dmalloc_bcopy(__FILE__, __LINE__, pnt, pnt2, size);
if (dmalloc_errno != DMALLOC_ERROR_NONE) {
if (! silent_b) {
loc_printf(" ERROR: %s overload should not get error: %s (%d)\n",
func, dmalloc_strerror(dmalloc_errno), dmalloc_errno);
}
final = 0;
}
/* check to see if it worked */
if (memcmp(pnt, pnt2, size) != 0) {
if (! silent_b) {
loc_printf(" ERROR: %s overload failed\n", func);
}
final = 0;
}
/* this copies too many characters into buffer */
dmalloc_errno = DMALLOC_ERROR_NONE;
memset(pnt, 2, size);
_dmalloc_bcopy(__FILE__, __LINE__, pnt, pnt2, size + 1);
if (dmalloc_errno != DMALLOC_ERROR_WOULD_OVERWRITE) {
if (! silent_b) {
loc_printf(" ERROR: %s overload should get error\n", func);
}
final = 0;
}
#endif
/*********/
#if HAVE_BZERO
func = "bzero";
if (! silent_b) {
loc_printf(" Checking %s\n", func);
}
/* this copies enough characters into buffer */
dmalloc_errno = DMALLOC_ERROR_NONE;
_dmalloc_bzero(__FILE__, __LINE__, pnt, size);
if (dmalloc_errno != DMALLOC_ERROR_NONE) {
if (! silent_b) {
loc_printf(" ERROR: %s overload should not get error: %s (%d)\n",
func, dmalloc_strerror(dmalloc_errno), dmalloc_errno);
}
final = 0;
}
/* this copies too many characters into buffer */
dmalloc_errno = DMALLOC_ERROR_NONE;
hold_ch = *(pnt + size);
_dmalloc_bzero(__FILE__, __LINE__, pnt, size + 1);
if (dmalloc_errno != DMALLOC_ERROR_WOULD_OVERWRITE) {
if (! silent_b) {
loc_printf(" ERROR: %s overload should get error\n", func);
}
final = 0;
}
*(pnt + size) = hold_ch;
#endif
/*********/
#if HAVE_INDEX
func = "index";
if (! silent_b) {
loc_printf(" Checking %s\n", func);
}
/* they should be the same */
dmalloc_errno = DMALLOC_ERROR_NONE;
memcpy(pnt, "1234", size);
if (_dmalloc_index(__FILE__, __LINE__, pnt, '4') != pnt + 3) {
if (! silent_b) {
loc_printf(" ERROR: %s overload failed\n", func);
}
final = 0;
}
if (dmalloc_errno != DMALLOC_ERROR_NONE) {
if (! silent_b) {
loc_printf(" ERROR: %s overload should not get error: %s (%d)\n",
func, dmalloc_strerror(dmalloc_errno), dmalloc_errno);
}
final = 0;
}
/* they should be different */