/src/r-oss-fuzz/harnesses/parse.c
Line | Count | Source |
1 | | /* |
2 | | * libFuzzer harness for R's parser. |
3 | | * |
4 | | * Feeds input to the lexer and parser without evaluating the result. |
5 | | * Targets parser bugs: crashes, OOB reads, infinite loops, stack |
6 | | * overflows in deeply nested input. |
7 | | * |
8 | | * The first input byte selects the route; the rest is the source text. |
9 | | * Slot 0 is the bare R_ParseVector of the original harness. The others |
10 | | * go through R-level parse() so the srcref machinery runs, then push the |
11 | | * parsed expressions through deparse() and back, str2lang(), and the |
12 | | * deparse control options -- deparse.c is the parser's inverse and had |
13 | | * only ever been reached incidentally. Nothing here evaluates what was |
14 | | * parsed. |
15 | | * |
16 | | * Adapted from r-afl's parse harness. |
17 | | */ |
18 | | |
19 | | #include <stdint.h> |
20 | | #include <string.h> |
21 | | |
22 | | #include "common.h" |
23 | | |
24 | 65.7k | #define FUZZ_MAX_INPUT (1024 * 16) |
25 | 32.8k | #define N_CALLS 7 |
26 | | |
27 | | /* |
28 | | * The R sources use raw string literals, a GNU extension that clang (19 |
29 | | * and later) and gcc accept in their default gnu C modes. They are arrays |
30 | | * rather than pointers so the table below can take their addresses in a |
31 | | * static initializer. |
32 | | * |
33 | | * The round-trip and srcref slots run R closures per top-level expression |
34 | | * (the vapply; deparse's useSource path calls as.character() on each |
35 | | * srcref, in both of its passes). 7k one-character expressions in 16 KB |
36 | | * took 27 s on the bots, past libFuzzer's 25 s timeout, without adding |
37 | | * coverage, so both skip inputs with more than 256 expressions. |
38 | | */ |
39 | | static const char parse_data_source[] = R"( |
40 | | function(x) { |
41 | | e <- parse(text = x, keep.source = TRUE) |
42 | | getParseData(e) |
43 | | } |
44 | | )"; |
45 | | |
46 | | static const char deparse_roundtrip_source[] = R"( |
47 | | function(x) { |
48 | | e <- parse(text = x, keep.source = FALSE) |
49 | | if (length(e) > 256L) |
50 | | return(NULL) |
51 | | d <- vapply(as.list(e), function(ex) { |
52 | | paste(deparse(ex, control = "all"), collapse = "\n") |
53 | | }, "") |
54 | | identical(e, parse(text = d, keep.source = FALSE)) |
55 | | } |
56 | | )"; |
57 | | |
58 | | static const char str2lang_source[] = R"( |
59 | | function(x) str2lang(x) |
60 | | )"; |
61 | | |
62 | | static const char str2expression_source[] = R"( |
63 | | function(x) str2expression(x) |
64 | | )"; |
65 | | |
66 | | static const char deparse_options_source[] = R"( |
67 | | function(x) { |
68 | | e <- parse(text = x, keep.source = FALSE) |
69 | | deparse( |
70 | | e, |
71 | | width.cutoff = 20L, |
72 | | backtick = TRUE, |
73 | | control = c("keepInteger", "quoteExpressions", |
74 | | "showAttributes", "digits17") |
75 | | ) |
76 | | } |
77 | | )"; |
78 | | |
79 | | static const char deparse_srcref_source[] = R"( |
80 | | function(x) { |
81 | | e <- parse(text = x, keep.source = TRUE) |
82 | | if (length(e) > 256L) |
83 | | return(NULL) |
84 | | deparse(e, control = c("useSource", "keepNA", "niceNames")) |
85 | | } |
86 | | )"; |
87 | | |
88 | | /* Slot 0 is handled in C; sources[0] is a placeholder so slot numbers |
89 | | * line up with the table. */ |
90 | | static const char *const sources[N_CALLS] = { |
91 | | NULL, |
92 | | parse_data_source, |
93 | | deparse_roundtrip_source, |
94 | | str2lang_source, |
95 | | str2expression_source, |
96 | | deparse_options_source, |
97 | | deparse_srcref_source, |
98 | | }; |
99 | | |
100 | | static SEXP x_str; |
101 | | static SEXP calls[N_CALLS]; |
102 | | |
103 | | typedef struct { |
104 | | SEXP str; |
105 | | ParseStatus status; |
106 | | } parse_data_t; |
107 | | |
108 | | static void do_parse(void *data) |
109 | 868 | { |
110 | 868 | parse_data_t *pd = (parse_data_t *)data; |
111 | 868 | SEXP parsed; |
112 | 868 | Rf_protect(parsed = R_ParseVector(pd->str, -1, &pd->status, R_NilValue)); |
113 | 868 | Rf_unprotect(1); |
114 | 868 | } |
115 | | |
116 | | static void run_slot(int selected) |
117 | 8.12k | { |
118 | 8.12k | if (selected == 0) { |
119 | 868 | parse_data_t pd; |
120 | 868 | pd.status = PARSE_NULL; |
121 | 868 | pd.str = x_str; |
122 | 868 | R_ToplevelExec(do_parse, &pd); |
123 | 868 | return; |
124 | 868 | } |
125 | | |
126 | 7.25k | fuzz_eval_silent(calls[selected], R_GlobalEnv); |
127 | 7.25k | } |
128 | | |
129 | | int fuzz_target_init(int *argc, char ***argv) |
130 | 2 | { |
131 | 2 | fuzz_init_r(); |
132 | | |
133 | | /* Enable pipe bind (=>) syntax so the fuzzer exercises that path. */ |
134 | 2 | setenv("_R_USE_PIPEBIND_", "true", 0); |
135 | | |
136 | | /* Pre-allocate reusable string container. Each iteration just |
137 | | * swaps the CHARSXP inside via SET_STRING_ELT. */ |
138 | 2 | Rf_protect(x_str = Rf_allocVector(STRSXP, 1)); |
139 | | |
140 | 14 | for (int i = 1; i < N_CALLS; i++) { |
141 | 12 | SEXP wrapper; |
142 | 12 | Rf_protect(wrapper = fuzz_make_wrapper(sources[i])); |
143 | 12 | Rf_protect(calls[i] = Rf_lang2(wrapper, x_str)); |
144 | 12 | } |
145 | | |
146 | 2 | fuzz_label_calls(calls, N_CALLS); |
147 | | |
148 | | /* Warmup: prime the parser's internal state so early iterations |
149 | | * don't diverge from later ones. */ |
150 | 2 | { |
151 | 2 | static const char *warmup[] = { |
152 | 2 | "1+1", |
153 | 2 | "x <- function(a, b) a + b", |
154 | 2 | "if (TRUE) 'yes' else 'no'", |
155 | 2 | "for (i in 1:10) i", |
156 | 2 | "list(a=1, b=\"hello\", c=NULL, d=NA)", |
157 | 2 | "\\(x) x + 1", |
158 | 2 | "1:10 |> rev()", |
159 | 2 | NULL |
160 | 2 | }; |
161 | 16 | for (int i = 0; warmup[i] != NULL; i++) { |
162 | 14 | SET_STRING_ELT(x_str, 0, Rf_mkChar(warmup[i])); |
163 | 112 | for (int slot = 0; slot < N_CALLS; slot++) |
164 | 98 | run_slot(slot); |
165 | 14 | } |
166 | 2 | R_gc(); |
167 | 2 | } |
168 | | |
169 | 2 | return 0; |
170 | 2 | } |
171 | | |
172 | | int fuzz_target_run(const uint8_t *data, size_t size) |
173 | 32.8k | { |
174 | 32.8k | if (size < 2 || size > FUZZ_MAX_INPUT || memchr(data + 1, '\0', size - 1)) |
175 | 147 | return 0; |
176 | | |
177 | | /* Null-terminate the input for R's string API. */ |
178 | 32.7k | char buf[FUZZ_MAX_INPUT]; |
179 | 32.7k | memcpy(buf, data + 1, size - 1); |
180 | 32.7k | buf[size - 1] = '\0'; |
181 | | |
182 | 32.7k | if (!fuzz_set_string(x_str, buf)) |
183 | 0 | return 0; |
184 | | |
185 | 32.7k | run_slot(data[0] % N_CALLS); |
186 | 32.7k | return 0; |
187 | 32.7k | } |