-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerror.c
executable file
·188 lines (138 loc) · 2.55 KB
/
generror.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <assert.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include "mmalloc.h"
#define OUT_LOOP_COUNT 200
#define LOOP_COUNT 1000
static const char *cmd = 0;
int alloc_free_chain();
void func1();
void func2();
void func3();
void corrupt_block(void* ptr, int block_size, int offset)
{
unsigned char *p = (unsigned char *)ptr;
p += block_size + offset;
*(p+4) = 'e';
*(p+5) = 0xAA;
}
void test_stress()
{
int k = 0;
while(k++ < OUT_LOOP_COUNT)
alloc_free_chain();
}
void test_corrupt()
{
char* p = (char *)malloc(40);
strcpy(p, "testblockdata");
corrupt_block(p, 40, 0);
}
void test_leak()
{
char* leak = (char *)malloc(100);
leak[0] = 0;
}
void test_usefreed()
{
char* p = (char *)malloc(100);
free(p);
p[4] = 'j';
}
void test_strdup()
{
static const char* s1 = "string one";
static const char* s2 = "string two";
char *s1_copy, *s2_copy;
char *s1_mcopy, *s2_mcopy;
char *stmp;
mmalloc_state_t m_state1;
s1_copy = strdup(s1);
s2_copy = strdup(s2);
s1_mcopy = strndup(s1, 5);
s2_mcopy = strndup(s2, 5);
m_state1 = mmalloc_get_state();
stmp = strndup(s1, 5);
mmalloc_report_state(m_state1);
free(stmp);
free(s1_copy);
free(s2_copy);
free(s1_mcopy);
free(s2_mcopy);
}
void test_zerosize(void)
{
void *a, *b;
a = malloc(0);
assert(a);
b = malloc(0);
assert(b);
mmalloc_report_stats();
free(a);
free(b);
free(NULL);
}
void func1()
{
func2();
}
void func2()
{
func3();
}
void func3()
{
#define CHECK_TEST( x ) if(strcmp( #x, cmd) == 0) { printf("Running test: %s\n", #x ); test_##x(); ; printf("Test %s done.\n", #x ); return;}
CHECK_TEST( leak );
CHECK_TEST( stress );
CHECK_TEST( corrupt );
CHECK_TEST( usefreed );
CHECK_TEST( strdup );
CHECK_TEST( zerosize );
printf("no such test as: %s\n", cmd);
#undef CHECK_TEST
}
int main(int argc, char *argv[])
{
if(argc != 2)
{
printf("Usage: %s [leak|stress|corrupt|usefreed|strdup|zerosize]\n", argv[0]);
return 1;
}
cmd = argv[1];
func1();
mmalloc_exit(0);
printf("%s exit\n", argv[0]);
return 0;
}
int alloc_free_chain()
{
static char* p[LOOP_COUNT];
int i;
int block_size;
int rand_count;
if( (rand() % 20) == 0)
alloc_free_chain();
rand_count = rand() % LOOP_COUNT;
for(i = 0; i < rand_count; i++)
{
if( (rand() % 5) == 0)
block_size = rand() % (1024*8);
else
block_size = rand() % 32;
p[i] = (char *)mmalloc( block_size );
if(!p[i])
printf("Error in alloc\n");
}
for(i = 0; i < rand_count; i++)
{
if(p[i])
mfree(p[i]);
}
return 0;
}