-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzz.c
More file actions
174 lines (148 loc) · 4.32 KB
/
fuzz.c
File metadata and controls
174 lines (148 loc) · 4.32 KB
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
#include "hdl.h"
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
// Source: skeeto
static uint8_t rand_byte(uint64_t *s)
{
*s = *s * 0x3243f6a8885a308d + 1;
return *s >> 56;
}
uint64_t gen_seed()
{
union {
uint8_t bytes[8];
uint64_t u64;
} v;
for (int i = 0; i < 8; i++) {
v.bytes[i] = rand() % 256;
}
return v.u64;
}
// from https://stackoverflow.com/questions/322938/recommended-way-to-initialize-srand
unsigned long mix(unsigned long a, unsigned long b, unsigned long c)
{
a=a-b; a=a-c; a=a^(c >> 13);
b=b-c; b=b-a; b=b^(a << 8);
c=c-a; c=c-b; c=c^(b >> 13);
a=a-b; a=a-c; a=a^(c >> 12);
b=b-c; b=b-a; b=b^(a << 16);
c=c-a; c=c-b; c=c^(b >> 5);
a=a-b; a=a-c; a=a^(c >> 3);
b=b-c; b=b-a; b=b^(a << 10);
c=c-a; c=c-b; c=c^(b >> 15);
return c;
}
struct fragment
{
int32_t offset;
int32_t len;
bool final;
};
void shuffle(struct fragment *l, int n, uint64_t *seed)
{
for (int i = n - 1; i > 0; i--) {
int j = rand_byte(seed) % (i+1);
struct fragment t = l[i];
l[i] = l[j];
l[j] = t;
}
}
struct fragment random_fragment(int32_t max_len, uint64_t *seed)
{
union {
uint8_t bytes[4];
uint32_t u32;
} v;
struct fragment f = {0};
for (int i = 0; i < 4; i++) v.bytes[i] = rand_byte(seed);
f.offset = v.u32 % max_len;
for (int i = 0; i < 4; i++) v.bytes[i] = rand_byte(seed);
f.len = v.u32 % (max_len - f.offset) + 1;
if (f.offset + f.len >= max_len) {
f.len = max_len - f.offset;
f.final = true;
}
return f;
}
void print_hole_descriptor_list(struct hole_descriptor_list *hdl)
{
if (!hdl) {
printf("<empty>\n");
}
while (hdl) {
printf("%10s: %10d\n", "first", hdl->first);
printf("%10s: %10d\n", "last", hdl->last);
for (struct frag_list *fl = hdl->frag_head; fl != NULL; fl = fl->next) {
printf("%10s (%d,%d)\n", "--", fl->offset, fl->len);
}
hdl = hdl->next;
}
}
int main(int argc, char **argv)
{
uint64_t s;
srand(mix(clock(), getpid(), time(NULL)));
//{
// struct fragment *l = calloc(10, sizeof(struct fragment));
// for (int i = 0; i < 10; i++) {
// l[i].offset = 1 * i;
// l[i].len = 10 - i;
// l[i].final = true;
// }
// l[9].final = true;
// s = gen_seed();
// struct hole_descriptor_list *hdl;
// hole_descriptor_list_init(&hdl);
// if (!hdl) return 1;
// shuffle(l, 10, &s);
// for(int i = 0; i < 10; i++) {
// printf("--- inserting (%d,%d)%s\n", l[i].offset, l[i].len, l[i].final ? "F" : "");
// hole_descriptor_list_add(hdl, l[i].offset, l[i].len, l[i].final, NULL);
// print_hole_descriptor_list(hdl);
// }
//}
//{
// struct fragment *l = calloc(10, sizeof(struct fragment));
// for (int i = 0; i < 10; i++) {
// l[i].offset = 10 * i;
// l[i].len = 10;
// }
// l[9].final = true;
// s = gen_seed();
// struct hole_descriptor_list *hdl;
// hole_descriptor_list_init(&hdl);
// if (!hdl) return 1;
// shuffle(l, 10, &s);
// for(int i = 0; i < 10; i++) {
// printf("--- inserting (%d,%d)%s\n", l[i].offset, l[i].len, l[i].final ? "F" : "");
// hole_descriptor_list_add(hdl, l[i].offset, l[i].len, l[i].final, NULL);
// print_hole_descriptor_list(hdl);
// }
//}
{
struct fragment *l = calloc(50, sizeof(struct fragment));
for (int i = 0; i < 10; i++) {
l[i].offset = 10 * i;
l[i].len = 10;
}
l[9].final = true;
s = gen_seed();
for (int i = 10; i < 50; i++) {
l[i] = random_fragment(100, &s);
}
struct hole_descriptor_list *hdl;
hole_descriptor_list_init(&hdl);
if (!hdl) return 1;
shuffle(l, 50, &s);
for(int i = 0; i < 50; i++) {
printf("--- inserting (%d,%d)%s\n", l[i].offset, l[i].len, l[i].final ? "F" : "");
hole_descriptor_list_add(hdl, l[i].offset, l[i].len, l[i].final, NULL);
print_hole_descriptor_list(hdl);
}
}
return 0;
}