1
2 /* A test which involves copying (using realloc) a block containing
3 some partially defined bytes. Really this is to check that
4 copy_address_range_perms in mc_main.c works. I don't think it's a
5 good test - it may well not exercise all the code in
6 copy_address_range_perms. */
7
8 #include <assert.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include "memcheck/memcheck.h"
12
13 typedef unsigned char UChar;
14 typedef unsigned int UInt;
15
16
17 static UInt seed = 0;
myrand(UInt size)18 static inline UInt myrand ( UInt size )
19 {
20 /* From "Numerical Recipes in C" 2nd Edition */
21 seed = 1664525UL * seed + 1013904223UL;
22 return seed % size;
23 }
24
barf(int size,int offset)25 static void barf ( int size, int offset )
26 {
27 printf("pdb-realloc2: fail: size %d, offset %d\n", size,offset);
28 exit(1);
29 }
30
do_test(int size)31 void do_test ( int size )
32 {
33 int i,j,r;
34 UChar* v;
35 UChar* p = malloc(size);
36 assert(p);
37 // fill
38 seed = 0;
39 for (i = 0; i < size; i++) {
40
41 j = myrand( 256 * 25 );
42 //printf("%d\n", j);
43 if (j >= 256 * 13) {
44 // def 1s
45 p[i] = 0xFF;
46 } else
47 if (j >= 256 && j < 256*13) {
48 // def 0s
49 p[i] = 0;
50 } else {
51 // pdb
52 p[i] &= (UChar)j;
53 }
54
55 }
56
57 // copy
58 for (i = 1; i <= 100; i++) {
59 p = realloc(p, size+i);
60 assert(p);
61 }
62
63 // check
64 v = malloc(size+100);
65 assert(v);
66 r = VALGRIND_GET_VBITS(p,v, size+100);
67 assert(r == 1);
68
69 //for (i = 0; i < size+100; i++)
70 // printf("%02x ", (UInt)v[i]);
71 //printf("\n");
72
73 seed = 0;
74 for (i = 0; i < size; i++) {
75
76 j = myrand( 256 * 25 );
77
78 if (j >= 256) {
79 // expecting a defined value
80 if (v[i] != 0)
81 barf(size, i);
82 } else {
83 // expecting a PDB == j
84 if (v[i] != (UChar)j)
85 barf(size,i);
86 }
87
88 }
89
90 // in the extension area, everything should be undefined
91 for (i = 0; i < 100; i++) {
92 if (v[size+i] != 0xFF)
93 barf(size, i);
94 }
95
96 free(v);
97 free(p);
98 }
99
main(void)100 int main ( void )
101 {
102 int z;
103 for (z = 0; z < 100; z++) {
104 printf("pdb_realloc: z = %d\n", z);
105 do_test(z);
106 do_test(z + 173);
107 do_test(z + 1731);
108 }
109 printf("pdb-realloc2: done\n");
110 return 0;
111 }
112