forked from nelhage/ghostscript-afl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghostscript_fuzzer.c
80 lines (69 loc) · 1.76 KB
/
ghostscript_fuzzer.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
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include "iapi.h"
#include "gserrors.h"
const size_t BUF_SIZE = (1ul << 20);
int main(int argc, char **argv) {
void *minst = 0;
int code = gsapi_new_instance(&minst, NULL);
if (code < 0) {
abort();
}
code = gsapi_set_arg_encoding(minst, GS_ARG_ENCODING_UTF8);
if (code != 0) {
return 0;
}
const char *gsargv[] = {
"gs",
"-q",
"-dSAFER",
"-dNODISPLAY",
"-sOutputFile=/dev/null",
"-sstdout=/dev/null",
"-dBATCH",
"-dNOPAUSE",
0,
};
code = gsapi_init_with_args(minst, sizeof(gsargv)/sizeof(*gsargv)-1, (char**)gsargv);
if (code != 0) {
abort();
}
int exit_code;
gsapi_run_string_begin(minst, 0, &exit_code);
{
const char *input =
(
"nulldevice "
"/== { pop } def "
"/=== { pop } def "
"{ 360 mod exch 360 mod exch arcn } bind /arcn exch def "
"{ 360 mod exch 360 mod exch arc } bind /arc exch def "
"{ pop } /findfont exch def "
);
gsapi_run_string_continue(minst, input, strlen(input), 0, &exit_code);
}
code = gsapi_run_string_end(minst, 0, &exit_code);
if (code != 0) {
abort();
}
#ifdef __AFL_HAVE_MANUAL_CONTROL
__AFL_INIT();
#endif
/* read stdin */
char *buf = malloc(BUF_SIZE);
size_t nread = fread(buf, 1, BUF_SIZE, stdin);
int fd = open("/dev/null", O_RDONLY);
if (fd < 0) {
abort();
}
dup2(fd, 0);
close(fd);
gsapi_run_string_with_length(minst, buf, nread, 0, &exit_code);
free(buf);
return 0;
}