/src/r-oss-fuzz/harnesses/io.c
Line | Count | Source |
1 | | /* |
2 | | * Exercise readBin() / writeBin() / readChar() / writeChar() over raw |
3 | | * vectors and in-memory raw connections. |
4 | | * |
5 | | * The first input byte selects a slot. The first N_BIN slots are the |
6 | | * original typed readBin round trips (read, write back, read again, |
7 | | * and abort if the two reads differ). The rest wrap the input in R |
8 | | * closures that reach the string readers (readChar, readBin of |
9 | | * "character"), the rawConnection layer (seek, readLines, the |
10 | | * connection-backed readBin), and the writeChar encoder -- none of |
11 | | * which the round trips touched. |
12 | | */ |
13 | | |
14 | | #include <stdint.h> |
15 | | #include <string.h> |
16 | | |
17 | | #include "common.h" |
18 | | |
19 | 1.49k | #define FUZZ_MAX_INPUT (1024 * 64) |
20 | 4.21k | #define N_BIN 8 |
21 | 1.58k | #define N_WRAP 10 |
22 | 1.54k | #define N_CALLS (N_BIN + N_WRAP) |
23 | | |
24 | | static SEXP read_calls[N_BIN]; |
25 | | static SEXP write_calls[N_BIN]; |
26 | | static SEXP wrap_calls[N_WRAP]; |
27 | | |
28 | | /* Connections are closed on exit from each closure: a read error must |
29 | | * not leak one of R's 128 connection slots across iterations. |
30 | | * |
31 | | * readLines() runs with warn = FALSE, as in the connections and zip |
32 | | * targets: it warns once per line holding an embedded nul, and every |
33 | | * warning goes through the R-level .signalSimpleWarning() even under |
34 | | * options(warn = -1), so a 64KB input of "\0\n" lines spent over 25 s |
35 | | * signalling warnings on the bots. */ |
36 | | static const char *const wrap_sources[N_WRAP] = { |
37 | | "function(x) readChar(x, c(1L, 5L, 16L, 64L), useBytes = TRUE)", |
38 | | "function(x) readChar(x, c(1L, 5L, 16L, 64L))", |
39 | | "function(x) { con <- rawConnection(x); on.exit(close(con)); " |
40 | | "readChar(con, c(1L, 5L, 16L, 64L)) }", |
41 | | "function(x) { s <- readChar(x, c(4L, 8L, 16L), useBytes = TRUE); " |
42 | | "r <- writeChar(s, raw(), nchars = c(4L, 8L, 16L), eos = NULL); " |
43 | | "readChar(r, c(4L, 8L, 16L), useBytes = TRUE) }", |
44 | | "function(x) { con <- rawConnection(x); on.exit(close(con)); " |
45 | | "readBin(con, \"character\", n = 64L) }", |
46 | | "function(x) { con <- rawConnection(x); on.exit(close(con)); " |
47 | | "readBin(con, \"integer\", n = 16384L, size = 2L, signed = FALSE) }", |
48 | | "function(x) readBin(x, \"integer\", n = 16384L, size = 1L, signed = TRUE)", |
49 | | "function(x) { con <- rawConnection(x); on.exit(close(con)); seek(con, 3L); " |
50 | | "readBin(con, \"double\", n = 16384L, size = 4L, endian = \"big\") }", |
51 | | "function(x) { con <- rawConnection(x); on.exit(close(con)); " |
52 | | "readLines(con, warn = FALSE) }", |
53 | | "function(x) { con <- rawConnection(x); on.exit(close(con)); " |
54 | | "readBin(con, \"numeric\", n = 16384L, endian = \"swap\") }", |
55 | | }; |
56 | | |
57 | | typedef struct { |
58 | | int selected; |
59 | | const uint8_t *data; |
60 | | size_t size; |
61 | | } io_input_t; |
62 | | |
63 | | static void run_io(void *data) |
64 | 359 | { |
65 | 359 | io_input_t *input = data; |
66 | 359 | int error = 0; |
67 | 359 | SEXP raw, value, encoded, roundtrip; |
68 | 359 | Rf_protect(raw = Rf_allocVector(RAWSXP, (R_xlen_t)input->size)); |
69 | 359 | memcpy(RAW(raw), input->data, input->size); |
70 | 359 | SETCADR(read_calls[input->selected], raw); |
71 | | |
72 | 359 | fuzz_label_enter(read_calls[input->selected]); |
73 | 359 | value = R_tryEvalSilent(read_calls[input->selected], R_GlobalEnv, &error); |
74 | 359 | if (!error) { |
75 | 359 | SETCADR(write_calls[input->selected], value); |
76 | 359 | fuzz_label_enter(write_calls[input->selected]); |
77 | 359 | encoded = R_tryEvalSilent(write_calls[input->selected], R_GlobalEnv, |
78 | 359 | &error); |
79 | 359 | } |
80 | 359 | if (!error) { |
81 | 359 | SETCADR(read_calls[input->selected], encoded); |
82 | 359 | fuzz_label_enter(read_calls[input->selected]); |
83 | 359 | roundtrip = R_tryEvalSilent(read_calls[input->selected], R_GlobalEnv, |
84 | 359 | &error); |
85 | 359 | } |
86 | 359 | if (!error) { |
87 | 359 | Rf_protect(roundtrip); |
88 | 359 | if (!R_compute_identical(value, roundtrip, IDENT_USE_CLOENV)) |
89 | 0 | abort(); |
90 | 359 | Rf_unprotect(1); |
91 | 359 | } |
92 | 359 | Rf_unprotect(1); |
93 | 359 | } |
94 | | |
95 | | int fuzz_target_init(int *argc, char ***argv) |
96 | 2 | { |
97 | 2 | (void)argc; |
98 | 2 | (void)argv; |
99 | 2 | fuzz_init_r(); |
100 | | |
101 | 2 | SEXP what[N_BIN], sizes[N_BIN], endian[N_BIN]; |
102 | 2 | Rcomplex zero; |
103 | 2 | zero.r = 0.0; |
104 | 2 | zero.i = 0.0; |
105 | 2 | Rf_protect(what[0] = Rf_allocVector(RAWSXP, 0)); |
106 | 2 | Rf_protect(what[1] = Rf_ScalarInteger(0)); |
107 | 2 | Rf_protect(what[2] = Rf_ScalarInteger(0)); |
108 | 2 | Rf_protect(what[3] = Rf_ScalarReal(0)); |
109 | 2 | Rf_protect(what[4] = Rf_ScalarReal(0)); |
110 | 2 | Rf_protect(what[5] = Rf_ScalarComplex(zero)); |
111 | 2 | Rf_protect(what[6] = Rf_mkString("")); |
112 | 2 | Rf_protect(what[7] = Rf_ScalarLogical(FALSE)); |
113 | | |
114 | 2 | int size_values[N_BIN] = { 1, 1, 4, 4, 8, 16, 1, 4 }; |
115 | 2 | const char *endian_values[N_BIN] = { |
116 | 2 | "little", "big", "little", "big", "little", "big", "little", |
117 | 2 | "little" |
118 | 2 | }; |
119 | 2 | SEXP placeholder, count, output; |
120 | 2 | Rf_protect(placeholder = Rf_allocVector(RAWSXP, 0)); |
121 | 2 | Rf_protect(count = Rf_ScalarInteger(16384)); |
122 | 2 | Rf_protect(output = Rf_allocVector(RAWSXP, 0)); |
123 | 2 | SEXP read_bin = Rf_install("readBin"); |
124 | 2 | SEXP write_bin = Rf_install("writeBin"); |
125 | | |
126 | 18 | for (int i = 0; i < N_BIN; i++) { |
127 | 16 | Rf_protect(sizes[i] = Rf_ScalarInteger(size_values[i])); |
128 | 16 | Rf_protect(endian[i] = Rf_mkString(endian_values[i])); |
129 | 16 | Rf_protect(read_calls[i] = Rf_lang6(read_bin, placeholder, what[i], count, |
130 | 16 | sizes[i], endian[i])); |
131 | 16 | SET_TAG(CDR(CDR(CDDR(read_calls[i]))), Rf_install("size")); |
132 | 16 | SET_TAG(CDR(CDR(CDR(CDDR(read_calls[i])))), Rf_install("endian")); |
133 | | |
134 | 16 | Rf_protect(write_calls[i] = Rf_lang5(write_bin, R_NilValue, output, |
135 | 16 | sizes[i], endian[i])); |
136 | 16 | SET_TAG(CDDDR(write_calls[i]), Rf_install("size")); |
137 | 16 | SET_TAG(CDR(CDDDR(write_calls[i])), Rf_install("endian")); |
138 | 16 | } |
139 | | |
140 | 22 | for (int i = 0; i < N_WRAP; i++) { |
141 | 20 | SEXP wrapper; |
142 | 20 | Rf_protect(wrapper = fuzz_make_wrapper(wrap_sources[i])); |
143 | 20 | Rf_protect(wrap_calls[i] = Rf_lang2(wrapper, placeholder)); |
144 | 20 | } |
145 | | |
146 | 18 | for (int i = 0; i < N_BIN; i++) { |
147 | 16 | fuzz_label_call(read_calls[i], i, N_CALLS); |
148 | 16 | fuzz_label_call(write_calls[i], i, N_CALLS); |
149 | 16 | } |
150 | 22 | for (int i = 0; i < N_WRAP; i++) |
151 | 20 | fuzz_label_call(wrap_calls[i], N_BIN + i, N_CALLS); |
152 | 2 | return 0; |
153 | 2 | } |
154 | | |
155 | | int fuzz_target_run(const uint8_t *data, size_t size) |
156 | 1.50k | { |
157 | 1.50k | if (size < 2 || size > FUZZ_MAX_INPUT) |
158 | 14 | return 0; |
159 | | |
160 | 1.48k | int selected = data[0] % N_CALLS; |
161 | 1.48k | if (selected < N_BIN) { |
162 | 359 | io_input_t input = { selected, data + 1, size - 1 }; |
163 | 359 | R_ToplevelExec(run_io, &input); |
164 | 359 | return 0; |
165 | 359 | } |
166 | | |
167 | 1.13k | SEXP call = wrap_calls[selected - N_BIN]; |
168 | 1.13k | if (!fuzz_set_raw_arg(call, data + 1, size - 1)) |
169 | 0 | return 0; |
170 | | |
171 | 1.13k | fuzz_eval_silent(call, R_GlobalEnv); |
172 | 1.13k | return 0; |
173 | 1.13k | } |