-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
executable file
·128 lines (95 loc) · 1.51 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include "mmalloc.h"
#define OUT_LOOP_COUNT 200
#define LOOP_COUNT 100000
void corrupt_block(void* ptr, int block_size, int offset)
{
unsigned char *p = (unsigned char *)ptr;
p += block_size + offset;
#if 0
*p = 'a';
*(p+1) = 'b';
*(p+2) = 'c';
*(p+3) = 'd';
#endif
#if 1
*(p+4) = 'e';
*(p+5) = 0xAA;
#endif
#if 0
*(p+6) = 'g';
*(p+7) = 'h';
*(p+8) = 'i';
*(p+9) = 'j';
#endif
}
void func1();
void func2();
void func3();
void func1()
{
func2();
}
void func2()
{
func3();
}
void func3()
{
char* p = mmalloc(120);
corrupt_block(p, 120, 0);
mcheck_regions(0);
mcheck_regions(0);
mcheck_regions(0);
}
int alloc_free_chain();
int main(int argc, char *argv[])
{
int k = 0;
int pid = getpid();
printf("pid = %d\n", pid);
srand(pid); /* 30538); */
mmalloc_init();
#if 1
func1();
mmalloc_cleanup(1);
return 0;
#endif
while(k++ < OUT_LOOP_COUNT)
{
alloc_free_chain();
}
mmalloc_cleanup(1);
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] = 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;
}