-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathfuzz_patch.cpp
349 lines (325 loc) · 8.41 KB
/
fuzz_patch.cpp
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
/*
* ____ ____ _____
* | _ \| _ \| ___| _ ________
* | |_) | |_) | |_ | | | |_ /_ /
* | _ <| _ <| _|| |_| |/ / / /
* |_| \_\_| \_\_| \__,_/___/___|
*
* Copyright (C) National University of Singapore
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
typedef unsigned __int128 HASH;
static bool option_save = false; // Save-all mode?
static MSG *messages_push_back(MSG *H, MSG *M);
static MSG *messages_free(MSG *H);
static void messages_save(FILE *stream, MSG *H);
static MSG *messages_load(const char *filename, FILE *stream);
struct PATCH // Patch representation
{
MSG *head; // Patch messages
PATCH *next; // Next patch in corpus
const char *filename; // Patch filename
bool discard; // Discard this patch?
bool disk; // Patch is saved to the disk?
bool cov; // Patch has new coverage?
/*
* Push a new message onto this patch.
*/
void push_back(MSG *M)
{
head = messages_push_back(head, M);
}
/*
* Initialize a patch (the "constructor").
*/
void init(void)
{
head = NULL;
next = NULL;
filename = NULL;
discard = disk = cov = false;
}
/*
* Reset a patch (the "destructor").
*/
void reset(void)
{
head = messages_free(head);
if (filename != NULL)
{
if (!option_save)
(void)unlink(filename);
pfree((void *)filename);
filename = NULL;
}
init();
}
/*
* Saves the patch to disk.
*/
bool save(const char *patchname)
{
assert(filename == NULL && !disk);
int fd = open(patchname, O_WRONLY | O_CREAT | O_EXCL, 0664);
if (fd < 0)
{
if (errno == EEXIST)
return false;
error("failed to open file \"%s\" for writing: %s", patchname,
strerror(errno));
}
size_t len = strlen(patchname);
filename = (char *)pmalloc(len+1);
memcpy((void *)filename, patchname, len+1);
FILE *stream = fdopen(fd, "w");
if (stream == NULL)
error("failed to open file \"%s\" for writing: %s", filename,
strerror(errno));
messages_save(stream, head);
fclose(stream);
return true;
}
/*
* Loads the patch from the disk.
*/
void load(void)
{
if (!disk || filename == nullptr)
return;
FILE *stream = fopen(filename, "r");
if (stream == NULL)
error("failed to open file \"%s\" for reading: %s", filename,
strerror(errno));
head = messages_load(filename, stream);
fclose(stream);
disk = false;
}
/*
* Unloads the patch from memory.
*/
void unload(void)
{
if (filename == nullptr)
return;
head = messages_free(head);
disk = true;
}
};
struct CORPUS // Set of patches
{
PATCH head; // Head == empty patch
PATCH *tail;
void init(void)
{
tail = &head;
}
/*
* Insert a patch into the corpus.
*/
void insert(HASH K, PATCH *P)
{
// Create the patch filename:
PRINTER Q;
Q.format("out/queue/m%u/", P->head->id);
if (mkdir(Q.str(), 0777) < 0 && errno != EEXIST)
error("failed to make directory \"%s\": %s", Q.str(),
strerror(errno));
Q.format("m%.5u_%.16lx%.16lx.patch", P->head->id,
(uint64_t)(K >> 64), (uint64_t)K);
(void)P->save(Q.str());
P->next = NULL;
tail->next = P;
tail = P;
P->unload();
}
/*
* Garbage collect the corpus.
*/
void gc(void)
{
PATCH *P = &head, *Q = head.next;
while (Q != NULL)
{
PATCH *N = Q->next;
if (Q->discard)
{
Q->reset();
pfree((void *)Q);
P->next = N;
}
else
P = Q;
Q = N;
}
tail = P;
}
};
/*
* Push back onto messages.
*/
static MSG *messages_push_back(MSG *H, MSG *M)
{
if (H == NULL)
{
M->next = M->prev = M;
H = M;
}
else
{
M->next = H;
M->prev = H->prev;
H->prev->next = M;
H->prev = M;
}
return H;
}
/*
* Free messages.
*/
static MSG *messages_free(MSG *H)
{
MSG *M = H;
if (H != NULL)
{
do
{
MSG *N = M->next;
pfree(M);
M = N;
}
while (M != H);
}
return NULL;
}
/*
* Write a message to the stream.
*/
static void message_write(FILE *stream, const MSG *M)
{
fprintf(stream, "PATCH id=#%d len=%zu port=%d src=",
M->id, M->len, M->port);
const char *name = port_name(M->port);
for (size_t i = 0; name[i] != '\0'; i++)
fputc((isspace(name[i])? '.': name[i]), stream);
fputs(" data=\n", stream);
fwrite(M->payload, sizeof(uint8_t), M->len, stream);
fputc('\n', stream);
}
/*
* Save messages to the disk.
*/
static void messages_save(FILE *stream, MSG *H)
{
MSG *M = H;
if (H != NULL)
{
do
{
message_write(stream, M);
M = M->next;
}
while (M != H);
}
}
/*
* Load messages from the disk.
*/
static MSG *messages_load(const char *filename, FILE *stream)
{
MSG *H = NULL;
while (true)
{
int id, port;
size_t len;
char c, tmp[1001];
if (fscanf(stream, " PATCH id=#%d len=%zu port=%d src=%1000s data=%c",
&id, &len, &port, tmp, &c) != 5 || c != '\n')
break;
MSG *M = (MSG *)pmalloc(sizeof(MSG) + len);
if (M == NULL)
error("failed to allocate message: %s", strerror(errno));
M->port = port;
M->error = 0;
M->outbound = false;
M->id = id;
M->len = len;
if (fread(M->payload, sizeof(uint8_t), M->len, stream) != M->len)
error("failed to read data from \"%s\": %s", filename,
strerror(errno));
H = messages_push_back(H, M);
}
if (!feof(stream) || ferror(stream))
error("failed to parse \"%s\"", filename);
return H;
}
/*
* Save a single message to the disk.
*/
static void patch_append(const char *filename, const MSG *M)
{
FILE *stream = fopen(filename, "a");
if (stream == NULL)
error("failed to open file \"%s\" for appending: %s", filename,
strerror(errno));
message_write(stream, M);
fclose(stream);
}
/*
* Save a patch to disk.
*/
static bool patch_save(const char *filename, const PATCH *P)
{
if (syscall(SYS_access, filename, /*F_OK=*/0) == 0)
return false; // Already exists
FILE *stream = fopen(filename, "w");
if (stream == NULL)
error("failed to open file \"%s\" for writing: %s", filename,
strerror(errno));
messages_save(stream, P->head);
fclose(stream);
return true;
}
/*
* Load a patch from disk.
*/
static PATCH *patch_load(const char *filename)
{
FILE *stream = fopen(filename, "r");
if (stream == NULL)
error("failed to open file \"%s\" for reading: %s", filename,
strerror(errno));
PATCH *P = (PATCH *)xmalloc(sizeof(PATCH));
P->init();
P->head = messages_load(filename, stream);
fclose(stream);
return P;
}
/*
* Get the next (patched) message.
*/
static MSG *patch_next(MSG *M, PATCH *P)
{
if (P == NULL || P->head == NULL || M->id != P->head->id)
return M;
M = P->head;
if (M == M->next)
P->head = NULL;
else
{
P->head = M->next;
P->head->prev = M->prev;
}
return M;
}