/src/gifsicle/test/fuzz/fuzz_main.c
Line | Count | Source |
1 | | /* fuzz_main.c - Fuzz target that runs gifsicle command lines. |
2 | | This file is part of gifsicle's fuzzing harness. See README.md. |
3 | | |
4 | | Each input is a command line followed by up to three input files: |
5 | | |
6 | | ARGS [SEP IN0 [SEP IN1 [SEP PALETTE]]] SEP = "\n#FILE#\n" |
7 | | |
8 | | ARGS is split into arguments at whitespace and NUL bytes. The files are |
9 | | named in0.gif, in1.gif, and palette.gif, and the target runs `gifsicle |
10 | | ARGS` with them, checking the results (see runner.c). Inputs with |
11 | | arguments that contain `/` are skipped, so gifsicle can only touch files |
12 | | in its scratch directory, unless the `/` is in a fraction like |
13 | | `--scale=1/2`. So are inputs with arguments that name a loop |
14 | | extension (gifsicle writes any `--app-extension` as given, but reads |
15 | | NETSCAPE2.0 and ANIMEXTS1.0 extensions as loop counts), and inputs whose |
16 | | files are huge GIFs, since gifsicle's time on those isn't interesting. */ |
17 | | |
18 | | #include "runner.h" |
19 | | #include <string.h> |
20 | | |
21 | 400k | #define SEP "\n#FILE#\n" |
22 | 247k | #define SEPLEN (sizeof(SEP) - 1) |
23 | 61.3k | #define NFILES 3 |
24 | 305k | #define MAX_ARGS 48 /* leaves room for the runner's options */ |
25 | 305k | #define MAX_ARG_LEN 256 |
26 | | |
27 | | static const char* const input_names[NFILES] = { |
28 | | "in0.gif", "in1.gif", "palette.gif" |
29 | | }; |
30 | | |
31 | | /* Returns 1 if the argument [s, e) may contain `/`: its value, after any |
32 | | `--option=`, consists of digits, `.`, `/`, and `x`, and isn't a path |
33 | | leaving the scratch directory. */ |
34 | 429 | static int fraction_arg(const uint8_t* s, const uint8_t* e) { |
35 | 429 | const uint8_t* eq = (const uint8_t*) memchr(s, '=', e - s); |
36 | 429 | if (eq && s[0] == '-') |
37 | 92 | s = eq + 1; |
38 | 429 | if (s == e || *s == '/') |
39 | 5 | return 0; |
40 | 2.99k | for (; s != e; ++s) |
41 | 2.58k | if (!strchr("0123456789./x", *s) || (s[0] == '.' && s + 1 != e && s[1] == '.')) |
42 | 13 | return 0; |
43 | 411 | return 1; |
44 | 424 | } |
45 | | |
46 | 25.3k | static const uint8_t* find_sep(const uint8_t* p, const uint8_t* end) { |
47 | 84.4k | while ((size_t) (end - p) >= SEPLEN) { |
48 | 83.7k | const uint8_t* q = (const uint8_t*) memchr(p, SEP[0], end - p - SEPLEN + 1); |
49 | 83.7k | if (!q) |
50 | 14.5k | return NULL; |
51 | 69.1k | if (memcmp(q, SEP, SEPLEN) == 0) |
52 | 10.0k | return q; |
53 | 59.0k | p = q + 1; |
54 | 59.0k | } |
55 | 744 | return NULL; |
56 | 25.3k | } |
57 | | |
58 | | /* Returns 0 if `data` encodes a command line we are willing to run. */ |
59 | 15.6k | static int decode(const uint8_t* data, size_t size, run_spec* spec) { |
60 | 15.6k | const uint8_t* end = data + size; |
61 | 15.6k | const uint8_t* args_end = find_sep(data, end); |
62 | 15.6k | const uint8_t* p = data; |
63 | 15.6k | int i; |
64 | 15.6k | spec_init(spec); |
65 | | |
66 | | /* arguments */ |
67 | 15.6k | if (!args_end) |
68 | 6.85k | args_end = end; |
69 | 168k | while (p < args_end) { |
70 | 153k | const uint8_t* start; |
71 | 1.44M | while (p < args_end && *p <= ' ') |
72 | 1.29M | ++p; |
73 | 153k | if (p == args_end) |
74 | 1.43k | break; |
75 | 152k | start = p; |
76 | 3.55M | while (p < args_end && *p > ' ') |
77 | 3.39M | ++p; |
78 | 152k | if (spec->argc > MAX_ARGS || p - start > MAX_ARG_LEN |
79 | 152k | || (memchr(start, '/', p - start) && !fraction_arg(start, p))) |
80 | 23 | return -1; /* also covers absolute and parent paths */ |
81 | 152k | spec_arg(spec, "%.*s", (int) (p - start), (const char*) start); |
82 | 152k | if (strstr(spec->argv[spec->argc - 1], "NETSCAPE2.0") |
83 | 152k | || strstr(spec->argv[spec->argc - 1], "ANIMEXTS1.0")) |
84 | 3 | return -1; |
85 | 152k | } |
86 | | |
87 | | /* files */ |
88 | 15.6k | p = args_end; |
89 | 25.6k | for (i = 0; i < NFILES && p < end; ++i) { |
90 | 10.0k | const uint8_t* next; |
91 | 10.0k | p += SEPLEN; |
92 | 10.0k | next = i < NFILES - 1 ? find_sep(p, end) : NULL; |
93 | 10.0k | if (!next) |
94 | 8.78k | next = end; |
95 | 10.0k | if (!fuzz_within_budget(p, next - p)) |
96 | 59 | return -1; |
97 | 10.0k | spec_file(spec, input_names[i], p, next - p); |
98 | 10.0k | p = next; |
99 | 10.0k | } |
100 | 15.5k | return 0; |
101 | 15.6k | } |
102 | | |
103 | 4 | int LLVMFuzzerInitialize(int* argc, char*** argv) { |
104 | 4 | fuzz_initialize(argc, argv, "gifsicle-fuzz-main"); |
105 | 4 | return 0; |
106 | 4 | } |
107 | | |
108 | 15.6k | int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
109 | 15.6k | run_spec spec; |
110 | 15.6k | int ok = decode(data, size, &spec) == 0 |
111 | 15.5k | && fuzz_run(&spec, fuzz_hash(data, size), 0) == 0; |
112 | 15.6k | spec_free(&spec); |
113 | 15.6k | return ok ? 0 : -1; /* -1: not worth adding to the corpus */ |
114 | 15.6k | } |