forked from robertdavidgraham/wc2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wcdiff.c
442 lines (383 loc) · 11.1 KB
/
wcdiff.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* Compares the output of two 'wc' programs and where they differ. It
* searches for the shortest string that will result in a difference
* between the programs. */
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/wait.h>
#include <signal.h>
/**
* Tests the filename to make sure it exists
*/
int is_exists(const char *filename)
{
struct stat st;
if (stat(filename, &st) != 0) {
fprintf(stderr, "[-] %s: %s\n", filename, strerror(errno));
return 0;
}
return 1;
}
/**
* Tests the filename to make sure it's an executable file
*/
int is_executable(const char *filename)
{
struct stat st;
if (stat(filename, &st) != 0) {
fprintf(stderr, "[-] %s: %s\n", filename, strerror(errno));
return 0;
}
if (!(st.st_mode & S_IXUSR)) {
fprintf(stderr, "[-] %s: not executable\n", filename);
return 0;
}
return 1;
}
struct handles
{
int child_stdin;
int child_stdout;
int child_stderr;
};
struct handles
my_spawn(const char *progname, const char *parms)
{
int pid;
int pipe_stdout[2];
int pipe_stderr[2];
int pipe_stdin[2];
struct handles handles = {0,0,0};
int err;
char **new_argv;
err = pipe(pipe_stdout);
if (err < 0) {
fprintf(stderr, "[-] pipe(): %s\n", strerror(errno));
return handles;
}
err = pipe(pipe_stderr);
if (err < 0) {
fprintf(stderr, "[-] pipe(): %s\n", strerror(errno));
return handles;
}
err = pipe(pipe_stdin);
if (err < 0) {
fprintf(stderr, "[-] pipe(): %s\n", strerror(errno));
return handles;
}
/* Configure the parent end of the pipes be be non-inheritable.
* In other words, none of the children can read from these
* pipes, nor will they exist in child process space */
/*fcntl(pipe_stdout[0], F_SETFD, FD_CLOEXEC);
fcntl(pipe_stderr[0], F_SETFD, FD_CLOEXEC);
fcntl(pipe_stdin[0], F_SETFD, FD_CLOEXEC);*/
pid = fork();
if (pid == -1) {
fprintf(stderr, "[-] fork(): %s\n", strerror(errno));
return handles;
}
/* Setup child parameters */
new_argv = malloc(10 * sizeof(char*));
new_argv[0] = (char *)progname;
new_argv[1] = (char *)parms;
new_argv[2] = NULL;
new_argv[3] = NULL;
if (pid == 0) {
int err;
close(pipe_stdin[1]);
close(pipe_stdout[0]);
close(pipe_stderr[0]);
dup2(pipe_stdin[0], 0);
dup2(pipe_stdout[1], 1);
dup2(pipe_stderr[1], 2);
/* Now execute our child with new program */
err = execvp(progname, new_argv);
if (err) {
fprintf(stderr, "[+] execvp(%s) failed: %s\n", progname, strerror(errno));
return handles;
}
} else {
handles.child_stdin = pipe_stdin[1];
handles.child_stdout = pipe_stdout[0];
handles.child_stderr = pipe_stderr[0];
close(pipe_stdout[1]);
close(pipe_stderr[1]);
close(pipe_stdin[0]);
/* we are the parent */
return handles;
}
return handles;
}
struct wcresults {
long line_count;
long word_count;
long char_count;
};
static long
get_integer(int fd)
{
int state = 0;
long result = -1;
for (;;) {
char c;
int count;
count = read(fd, &c, 1);
if (count <= 0)
return -1;
switch (state) {
case 0:
if (c == '\n')
return -1;
if (isspace(c))
continue;
if (isdigit(c)) {
result = c - '0';
state++;
continue;
}
return -1;
case 1:
if (isspace(c))
return result;
if (!isdigit(c))
return -1;
result = result * 10 + (c - '0');
break;
}
}
return result;
}
/**
* Read the results, which are a series of integers from the
* 'wc' program. */
struct wcresults
get_results(int fd)
{
struct wcresults results;
results.line_count = get_integer(fd);
results.word_count = get_integer(fd);
results.char_count = get_integer(fd);
return results;
}
static unsigned
r_rand(unsigned *seed)
{
static const unsigned a = 214013;
static const unsigned c = 2531011;
*seed = (*seed) * a + c;
return (*seed)>>16 & 0x7fff;
}
static size_t
cleanup_children(void)
{
size_t children_count = 0;
for (;;) {
int pid;
/* Reap children.
* The first parameter is set to -1 to indicate that we want
* information about ANY of our children processes.
* The second paremeter is set to NULL to indicate that we
* aren't interested in knowing the status/result code from
* the process.
* The third parameter is WNOHHANG, meaning that we want to return
* immediately
*/
pid = waitpid(-1, 0, WNOHANG);
if (pid > 0) {
/* If we get back a valid PID, that means the child process
* has terminated. We want to decrement our count by one
* then loop around looking for more child processes. */
children_count++;
continue;
} else if (pid == 0) {
/* if none of our children are currently exited, then this
* value of zero is returned. */
break;
} else if (pid == -1 && errno == ECHILD) {
/* In this condition, there are no child processes. In this
* case, we just want to handle this the same as pid=0 */
break;
} else if (pid < 0) {
/* Some extraordinary error occured */
exit(1);
}
}
return 0;
}
long
word_count(const char *progname, const char *parms, const unsigned char *buf, size_t length)
{
struct handles h;
size_t offset = 0;
struct wcresults results;
h = my_spawn(progname, parms);
if (h.child_stdin <= 0 || h.child_stdout <= 0) {
fprintf(stderr, "[-] spawn failed\n");
return -1;
}
/* keep writing until we've written the entire chunk */
while (offset < length) {
size_t count;
count = write(h.child_stdin, buf+offset, length-offset);
if (count == 0) {
perror("write()");
exit(1);
}
offset += count;
}
/* Close the handle, telling the child process that input has ended */
close(h.child_stdin);
/* Read the results from the child */
results = get_results(h.child_stdout);
close(h.child_stdout);
close(h.child_stderr);
cleanup_children();
return results.word_count;
}
unsigned
utf8_len(unsigned char c)
{
if (c < 0x80) {
return 1;
} else if ((c&0xE0) == 0xC0) {
return 2;
} else if ((c&0xF0) == 0xE0) {
return 3;
} else if ((c&0xF1) == 0xF0) {
return 4;
} else
return 0;
}
void print_diff(const unsigned char *buf, size_t offset, size_t max)
{
size_t i;
for (i=offset; i<max; i++) {
printf("0x%02x - len(%u)\n", buf[i], utf8_len(buf[i]));
}
}
int main(int argc, char *argv[])
{
unsigned char *buf;
unsigned seed = 0;
unsigned length = 1024 * 1024;
unsigned i;
unsigned min = 0;
unsigned max = length;
unsigned min_diff, max_diff;
long x, y;
const char *progname1 = "./wc2";
const char *progname2 = "./wc2";
const char *parms1 = NULL;
char *parms2 = NULL;
signal(SIGPIPE, SIG_IGN);
/* The first parameter is the program we are testing against 'wc' */
if (argc < 2 || !is_executable(argv[1])) {
fprintf(stderr, "[-] first parameter must be an executable program\n");
return 1;
} else
progname2 = argv[1];
/* The second parameter is option parameters */
if (argc >= 3) {
parms1 = argv[2];
} else
parms1 = NULL;
if (parms1) {
parms2 = malloc(strlen(parms1) + 2);
strcpy(parms2, parms1);
strcat(parms2, "P");
}
/* Create a buffer of deterministically random data. This will be the
* same data whenever we run this program, on any CPu, any OS, and
* any compiler */
buf = malloc(length);
for (i=0; i<length; i++)
buf[i] = r_rand(&seed);
/* First, make sure there's a difference */
x = word_count(progname1, parms1, buf, max);
y = word_count(progname2, parms2, buf, max);
if (x == -1) {
fprintf(stderr, "[-] %s: failed\n", progname1);
exit(1);
}
if (y == -1) {
fprintf(stderr, "[-] %s: failed\n", progname2);
exit(1);
}
if (x == y) {
fprintf(stderr, "[+] difference: none! (identical results)\n");
return 0;
} else {
fprintf(stderr, "[+] difference: %s=%lu %s=%lu\n", progname1, x, progname2, y);
}
/* Now search for one past the first difference */
min = 0;
max = length;
while (min < max) {
unsigned half = (max - min)/2 + min;
fprintf(stderr, "max=%8lu\b\b\b\b\b\b\b\b\b\b\b\b", (unsigned long)(max));
x = word_count(progname1, parms1, buf, half);
y = word_count(progname2, parms2, buf, half);
if (x == y) {
/* we went too far, so we need to go back */
min = half + 1;
} else {
/* we didn't go far enough */
max = half;
}
}
max_diff = max;
fprintf(stderr, "max=%8lu\n", (unsigned long)(max_diff));
/* Now search for one before the first difference */
min = 0;
max = max_diff;
while (min < max) {
unsigned half = (max - min)/2 + min;
fprintf(stderr, "min=%8lu\b\b\b\b\b\b\b\b\b\b\b\b", (unsigned long)(min));
/* fprintf(stderr, "min=%u, half=%u, max=%u\n", min, half, max); */
x = word_count(progname1, parms1, buf + half, max_diff - half);
y = word_count(progname2, parms2, buf + half, max_diff - half);
if (x == y) {
/* we went too far, so we need to go back */
max = half;
} else {
/* we didn't go far enough */
min = half + 1;
}
}
min_diff = min;
/* If they are equal, reduce min by one */
if (min_diff > 0) {
x = word_count(progname1, parms1, buf + min_diff, max_diff - min_diff);
y = word_count(progname2, parms2, buf + min_diff, max_diff - min_diff);
if (x == y) {
min_diff--;
}
}
fprintf(stderr, "min=%8lu\n", (unsigned long)(min_diff));
/* print results */
x = word_count(progname1, parms1, buf + min_diff, max_diff - min_diff);
y = word_count(progname2, parms2, buf + min_diff, max_diff - min_diff);
printf("Diff string: wc=%ld wc2=%ld \n", x, y);
/* print_diff(buf, min_diff, max_diff); */
printf("\"");
for (i=min_diff; i<max_diff; i++) {
printf("\\\\x%02x", buf[i]);
}
printf("\"\n");
/*
if (argc < 3 || argv[2][0] != '-') {
fprintf(stderr, "[-] second parameter must be option like -m or -w\n");
return 1;
}
if (argc < 4 || !is_exists(argv[3])) {
fprintf(stderr, "[-] third parameter must be file\n");
return 1;
}
*/
return 0;
}