/src/r-oss-fuzz/harnesses/grep.c
Line | Count | Source |
1 | | /* |
2 | | * libFuzzer harness for R's regex engines. |
3 | | * |
4 | | * Uses fuzzed input as a regex pattern against a fixed character vector, |
5 | | * exercising the PCRE2 (perl = TRUE) backend through every entry point |
6 | | * in grep.c: grep/grepl, sub/gsub (including backreference and |
7 | | * case-conversion replacements), regexpr/gregexpr and regexec, plus |
8 | | * grepRaw with a fixed pattern. The first input byte selects the call; |
9 | | * the rest is the pattern. |
10 | | * |
11 | | * R's default engine, TRE, is deliberately not driven: its upstream is |
12 | | * dormant and it has known compile-time blowups, exponential |
13 | | * backtracking and internal assertions that longjmp out of |
14 | | * tre_compile(), none of which this project pursues (see README.md). |
15 | | * grepRaw() has no perl argument, so its slot uses fixed = TRUE. |
16 | | * |
17 | | * Adapted from r-afl's grep harness. |
18 | | */ |
19 | | |
20 | | #include <stdint.h> |
21 | | #include <string.h> |
22 | | |
23 | | #include "common.h" |
24 | | |
25 | 4.34k | #define FUZZ_MAX_INPUT (1024 * 64) |
26 | 2.50k | #define N_CALLS 16 |
27 | | |
28 | | /* Closures over (pattern, subjects). The first three are the original |
29 | | * harness's calls. */ |
30 | | static const char *const sources[N_CALLS] = { |
31 | | "function(p, x) grepl(p, x, perl = TRUE)", |
32 | | "function(p, x) grep(p, x, perl = TRUE)", |
33 | | "function(p, x) sub(p, \"X\", x, perl = TRUE)", |
34 | | "function(p, x) regexpr(p, x, perl = TRUE, useBytes = TRUE)", |
35 | | "function(p, x) gregexpr(p, x, perl = TRUE, ignore.case = TRUE)", |
36 | | "function(p, x) regexec(p, x, perl = TRUE, ignore.case = TRUE)", |
37 | | "function(p, x) gregexpr(p, x, perl = TRUE)", |
38 | | "function(p, x) regexpr(p, x, fixed = TRUE)", |
39 | | "function(p, x) gsub(p, \"X\", x, perl = TRUE)", |
40 | | "function(p, x) grepRaw(p, charToRaw(x[3L]), all = TRUE, fixed = TRUE)", |
41 | | "function(p, x) grepl(p, x, perl = TRUE, useBytes = TRUE)", |
42 | | "function(p, x) regexec(p, x, perl = TRUE)", |
43 | | "function(p, x) regexpr(p, x, perl = TRUE)", |
44 | | "function(p, x) gsub(p, \"\\\\1\", x, perl = TRUE)", |
45 | | "function(p, x) sub(p, \"X\", x, fixed = TRUE)", |
46 | | "function(p, x) gsub(p, \"\\\\U\\\\1\", x, perl = TRUE)", |
47 | | }; |
48 | | |
49 | | static SEXP x_pat; |
50 | | static SEXP calls[N_CALLS]; |
51 | | |
52 | | int fuzz_target_init(int *argc, char ***argv) |
53 | 12 | { |
54 | 12 | fuzz_init_r(); |
55 | | |
56 | | /* Fixed strings to match the fuzzed pattern against. */ |
57 | 12 | SEXP x; |
58 | 12 | Rf_protect(x = Rf_allocVector(STRSXP, 5)); |
59 | 12 | SET_STRING_ELT(x, 0, Rf_mkChar("hello world")); |
60 | 12 | SET_STRING_ELT(x, 1, Rf_mkChar("foo bar baz 123")); |
61 | 12 | SET_STRING_ELT(x, 2, Rf_mkChar("the quick brown fox jumps over the lazy dog")); |
62 | 12 | SET_STRING_ELT(x, 3, Rf_mkChar("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaab")); |
63 | 12 | SET_STRING_ELT(x, 4, Rf_mkChar("")); |
64 | | |
65 | | /* Reusable pattern container -- each iteration swaps the CHARSXP. */ |
66 | 12 | Rf_protect(x_pat = Rf_allocVector(STRSXP, 1)); |
67 | | |
68 | 168 | for (int i = 0; i < N_CALLS; i++) { |
69 | 156 | SEXP wrapper; |
70 | 156 | Rf_protect(wrapper = fuzz_make_wrapper(sources[i])); |
71 | 156 | Rf_protect(calls[i] = Rf_lang3(wrapper, x_pat, x)); |
72 | 156 | } |
73 | | |
74 | 12 | fuzz_label_calls(calls, N_CALLS); |
75 | | |
76 | | /* Warmup: prime regex engine state before fuzzing. */ |
77 | 12 | SET_STRING_ELT(x_pat, 0, Rf_mkChar("(o)")); |
78 | 168 | for (int i = 0; i < N_CALLS; i++) |
79 | 156 | fuzz_eval_silent(calls[i], R_GlobalEnv); |
80 | | |
81 | 12 | return 0; |
82 | 12 | } |
83 | | |
84 | | int fuzz_target_run(const uint8_t *data, size_t size) |
85 | 2.17k | { |
86 | 2.17k | if (size < 2 || size > FUZZ_MAX_INPUT || memchr(data + 1, '\0', size - 1)) |
87 | 9 | return 0; |
88 | | |
89 | 2.16k | char buf[FUZZ_MAX_INPUT]; |
90 | 2.16k | memcpy(buf, data + 1, size - 1); |
91 | 2.16k | buf[size - 1] = '\0'; |
92 | | |
93 | | /* The repeat guard was written for TRE's bounded-repeat blowup; |
94 | | * PCRE2 rejects nested quantifiers anyway, so keeping it costs |
95 | | * little repeat coverage. See common.h. */ |
96 | 2.16k | if (fuzz_repeat_product_excessive(buf)) |
97 | 9 | return 0; |
98 | | |
99 | 2.15k | if (!fuzz_set_string(x_pat, buf)) |
100 | 0 | return 0; |
101 | | |
102 | 2.15k | fuzz_eval_silent(calls[data[0] % N_CALLS], R_GlobalEnv); |
103 | 2.15k | return 0; |
104 | 2.15k | } |