Coverage Report

Created: 2026-11-13 07:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/r-oss-fuzz/harnesses/connections.c
Line
Count
Source
1
/*
2
 * Exercise R's connection layer (src/main/connections.c) as a reader of
3
 * untrusted files.
4
 *
5
 * memDecompress, readBin and readLines(textConnection) already fuzz their
6
 * narrow entry points; what they never reach is the machinery around
7
 * them: the encoding= re-encoding layer (dummy_fgetc), the compressed-file
8
 * readers (gzfile/bzfile/xzfile/zstdfile and gzcon), rawConnection, the
9
 * line buffering behind readLines/readChar/scan, seek, pushBack, and the
10
 * write side (writeLines/writeChar/rawConnectionValue).  Each input is
11
 * staged in a scratch file and read back through one of those paths.
12
 *
13
 * Every slot is an R closure that closes its connection on exit: R caps
14
 * the connection table at 128, so a leak would quietly turn every later
15
 * iteration into the same "all connections are in use" error.
16
 */
17
18
#include <stdint.h>
19
#include <string.h>
20
21
#include "common.h"
22
23
5.98k
#define FUZZ_MAX_INPUT (1024 * 64)
24
25
/* Largest decompressed size a text-reading slot may see.  readLines(n =)
26
 * and scan(nmax =) bound the number of lines and fields, not their
27
 * length, and bzip2 reaches ratios near a million to one, so a few
28
 * hundred input bytes can inflate to a single line of tens of megabytes,
29
 * which takes longer than the timeout to read under ASAN.  One MiB is
30
 * far past every buffer size and growth step in the readers. */
31
2
#define FUZZ_MAX_INFLATED (1024 * 1024)
32
33
/* Encodings handed to file(encoding=).  "" means no re-encoding, the
34
 * others go through iconv in dummy_fgetc; UTF-8-BOM and UCS-2LE take the
35
 * byte-order-mark branches. */
36
6.20k
#define N_ENCODINGS 9
37
static const char *const encodings[N_ENCODINGS] = {
38
    "", "UTF-8", "latin1", "UTF-16LE", "UTF-16BE", "UTF-16", "UCS-2LE",
39
    "CP1252", "UTF-8-BOM"
40
};
41
42
6.03k
#define N_OPENERS 4
43
static const char *const openers[N_OPENERS] = {
44
    "gzfile", "bzfile", "xzfile", "zstdfile"
45
};
46
47
/* Slot layout, in the order the calls are built below:
48
 *   [0,9)    readLines(file(path, encoding), skipNul = FALSE)
49
 *   [9,18)   readLines(..., skipNul = TRUE)
50
 *   [18,27)  readChar(file(path, "r", encoding), useBytes = TRUE)
51
 *   [27,36)  readChar(..., useBytes = FALSE)
52
 *   [36,45)  scan(file(path, encoding), what = "")
53
 *   [45,54)  open(con, "rt") then chunked readLines/seek/readChar
54
 *   [54,58)  <opener>(path, "rb") + readBin
55
 *   [58,62)  <opener>(path, "rt") + readLines
56
 *   62       gzcon(file(path, "rb")) + readBin
57
 *   63       rawConnection(x) reads, seek, readChar
58
 *   64       pushBack over a textConnection of the lines
59
 *   65       write side: rawConnection/textConnection/gzcon writers
60
 */
61
5.98k
#define N_CALLS (6 * N_ENCODINGS + 2 * N_OPENERS + 4)
62
63
static SEXP calls[N_CALLS];
64
/* The size check run before a slot, or NULL where the slot's reads are
65
 * already bounded (readChar, readBin, the raw-vector slots). */
66
static SEXP probes[N_CALLS];
67
static char *scratch_path;
68
69
/* Whether the scratch file inflates to at most cap bytes through opener.
70
 * file() only switches to a decompressing reader when it is created with
71
 * open = "", "r" or "rt", as the text slots create it; file(path, "rb")
72
 * would count the compressed bytes, so the connection is opened after it
73
 * is created. */
74
static const char wrapper_probe[] =
75
    "function(path, opener, cap) {"
76
    "  con <- opener(path); on.exit(close(con)); open(con, 'rb');"
77
    "  length(readBin(con, 'raw', cap + 1L)) <= cap }";
78
79
/* Every wrapper takes (x, path, ...) so that fuzz_set_raw_arg can swap the
80
 * raw vector in at CADR regardless of which slot runs. */
81
static const char wrapper_lines[] =
82
    "function(x, path, enc, skip) {"
83
    "  con <- file(path, encoding = enc); on.exit(close(con));"
84
    "  readLines(con, n = 1000L, warn = FALSE, skipNul = skip) }";
85
86
static const char wrapper_chars[] =
87
    "function(x, path, enc, ub) {"
88
    "  con <- file(path, 'r', encoding = enc); on.exit(close(con));"
89
    "  readChar(con, c(1L, 10L, 100L, 1000L), useBytes = ub) }";
90
91
static const char wrapper_scan[] =
92
    "function(x, path, enc) {"
93
    "  con <- file(path, encoding = enc); on.exit(close(con));"
94
    "  scan(con, what = '', quiet = TRUE, nmax = 1000L) }";
95
96
static const char wrapper_chunks[] =
97
    "function(x, path, enc) {"
98
    "  con <- file(path, encoding = enc); open(con, 'rt'); on.exit(close(con));"
99
    "  a <- readLines(con, n = 2L, warn = FALSE); isIncomplete(con);"
100
    "  b <- readLines(con, n = 500L, warn = FALSE);"
101
    "  if (isSeekable(con)) seek(con, 0L);"
102
    "  list(a, b, readChar(con, 10L, useBytes = TRUE)) }";
103
104
static const char wrapper_compressed_bin[] =
105
    "function(x, path, opener) {"
106
    "  con <- opener(path, 'rb'); on.exit(close(con));"
107
    "  readBin(con, 'raw', 65536L) }";
108
109
static const char wrapper_compressed_text[] =
110
    "function(x, path, opener) {"
111
    "  con <- opener(path, 'rt'); on.exit(close(con));"
112
    "  readLines(con, n = 1000L, warn = FALSE) }";
113
114
static const char wrapper_gzcon[] =
115
    "function(x, path) {"
116
    "  con <- gzcon(file(path, 'rb')); on.exit(close(con));"
117
    "  readBin(con, 'raw', 65536L) }";
118
119
static const char wrapper_raw[] =
120
    "function(x, path) {"
121
    "  con <- rawConnection(x); on.exit(close(con));"
122
    "  a <- readLines(con, n = 1000L, warn = FALSE);"
123
    "  isSeekable(con); seek(con, 3L);"
124
    "  b <- readChar(con, c(5L, 50L), useBytes = TRUE);"
125
    "  seek(con, 0L, 'start');"
126
    "  list(a, b, readBin(con, 'integer', 100L, size = 2L)) }";
127
128
static const char wrapper_pushback[] =
129
    "function(x, path) {"
130
    "  con <- rawConnection(x); on.exit(close(con));"
131
    "  l <- readLines(con, n = 20L, warn = FALSE);"
132
    "  tc <- textConnection(l); on.exit(close(tc), add = TRUE);"
133
    "  pushBack(rev(l), tc, newLine = FALSE); pushBack(l, tc);"
134
    "  pushBackLength(tc); a <- readLines(tc, n = 5L, warn = FALSE);"
135
    "  clearPushBack(tc); list(a, readLines(tc, warn = FALSE)) }";
136
137
static const char wrapper_write[] =
138
    "function(x, path) {"
139
    "  l <- readLines(rc <- rawConnection(x), n = 50L, warn = FALSE); close(rc);"
140
    "  out <- rawConnection(raw(0), 'w'); on.exit(close(out));"
141
    "  writeLines(l, out); writeChar(l, out, eos = NULL); writeBin(x, out);"
142
    "  seek(out, 0L); truncate(out); writeLines(l, out, sep = '\\r\\n');"
143
    "  v <- rawConnectionValue(out);"
144
    "  tc <- textConnection(NULL, 'w'); on.exit(close(tc), add = TRUE);"
145
    "  writeLines(l, tc); cat(l, file = tc, sep = '|'); t <- textConnectionValue(tc);"
146
    "  gz <- gzcon(rawConnection(raw(0), 'w')); writeBin(x, gz); close(gz);"
147
    "  list(v, t) }";
148
149
int fuzz_target_init(int *argc, char ***argv)
150
2
{
151
2
    (void)argc;
152
2
    (void)argv;
153
2
    fuzz_init_r();
154
155
2
    scratch_path = fuzz_scratch_file("connections");
156
2
    if (scratch_path == NULL)
157
0
        abort();
158
159
2
    SEXP placeholder, path, true_value, false_value;
160
2
    Rf_protect(placeholder = Rf_allocVector(RAWSXP, 1));
161
2
    Rf_protect(path = Rf_mkString(scratch_path));
162
2
    Rf_protect(true_value = Rf_ScalarLogical(TRUE));
163
2
    Rf_protect(false_value = Rf_ScalarLogical(FALSE));
164
165
2
    SEXP w_lines, w_chars, w_scan, w_chunks, w_cbin, w_ctext;
166
2
    Rf_protect(w_lines = fuzz_make_wrapper(wrapper_lines));
167
2
    Rf_protect(w_chars = fuzz_make_wrapper(wrapper_chars));
168
2
    Rf_protect(w_scan = fuzz_make_wrapper(wrapper_scan));
169
2
    Rf_protect(w_chunks = fuzz_make_wrapper(wrapper_chunks));
170
2
    Rf_protect(w_cbin = fuzz_make_wrapper(wrapper_compressed_bin));
171
2
    Rf_protect(w_ctext = fuzz_make_wrapper(wrapper_compressed_text));
172
173
2
    int n = 0;
174
2
    SEXP enc[N_ENCODINGS];
175
20
    for (int i = 0; i < N_ENCODINGS; i++)
176
18
        Rf_protect(enc[i] = Rf_mkString(encodings[i]));
177
178
20
    for (int i = 0; i < N_ENCODINGS; i++)
179
18
        Rf_protect(calls[n++] = Rf_lang5(w_lines, placeholder, path, enc[i],
180
18
                                         false_value));
181
20
    for (int i = 0; i < N_ENCODINGS; i++)
182
18
        Rf_protect(calls[n++] = Rf_lang5(w_lines, placeholder, path, enc[i],
183
18
                                         true_value));
184
20
    for (int i = 0; i < N_ENCODINGS; i++)
185
18
        Rf_protect(calls[n++] = Rf_lang5(w_chars, placeholder, path, enc[i],
186
18
                                         true_value));
187
20
    for (int i = 0; i < N_ENCODINGS; i++)
188
18
        Rf_protect(calls[n++] = Rf_lang5(w_chars, placeholder, path, enc[i],
189
18
                                         false_value));
190
20
    for (int i = 0; i < N_ENCODINGS; i++)
191
18
        Rf_protect(calls[n++] = Rf_lang4(w_scan, placeholder, path, enc[i]));
192
20
    for (int i = 0; i < N_ENCODINGS; i++)
193
18
        Rf_protect(calls[n++] = Rf_lang4(w_chunks, placeholder, path, enc[i]));
194
195
    /* The openers are base functions looked up once; passing the closure
196
     * itself keeps the wrapper free of any eval of a name. */
197
2
    SEXP opener[N_OPENERS];
198
10
    for (int i = 0; i < N_OPENERS; i++)
199
8
        Rf_protect(opener[i] = Rf_findFun(Rf_install(openers[i]), R_BaseEnv));
200
10
    for (int i = 0; i < N_OPENERS; i++)
201
8
        Rf_protect(calls[n++] = Rf_lang4(w_cbin, placeholder, path, opener[i]));
202
10
    for (int i = 0; i < N_OPENERS; i++)
203
8
        Rf_protect(calls[n++] = Rf_lang4(w_ctext, placeholder, path, opener[i]));
204
205
2
    SEXP w_gzcon, w_raw, w_pushback, w_write;
206
2
    Rf_protect(w_gzcon = fuzz_make_wrapper(wrapper_gzcon));
207
2
    Rf_protect(w_raw = fuzz_make_wrapper(wrapper_raw));
208
2
    Rf_protect(w_pushback = fuzz_make_wrapper(wrapper_pushback));
209
2
    Rf_protect(w_write = fuzz_make_wrapper(wrapper_write));
210
2
    Rf_protect(calls[n++] = Rf_lang3(w_gzcon, placeholder, path));
211
2
    Rf_protect(calls[n++] = Rf_lang3(w_raw, placeholder, path));
212
2
    Rf_protect(calls[n++] = Rf_lang3(w_pushback, placeholder, path));
213
2
    Rf_protect(calls[n++] = Rf_lang3(w_write, placeholder, path));
214
2
    if (n != N_CALLS)
215
0
        abort();
216
2
    fuzz_label_calls(calls, N_CALLS);
217
218
    /* readLines and scan through file(), and readLines through each
219
     * opener, are the unbounded readers. */
220
2
    SEXP w_probe, cap, file_opener, probe_file;
221
2
    Rf_protect(w_probe = fuzz_make_wrapper(wrapper_probe));
222
2
    Rf_protect(cap = Rf_ScalarInteger(FUZZ_MAX_INFLATED));
223
2
    Rf_protect(file_opener = Rf_findFun(Rf_install("file"), R_BaseEnv));
224
2
    Rf_protect(probe_file = Rf_lang4(w_probe, path, file_opener, cap));
225
38
    for (int i = 0; i < 2 * N_ENCODINGS; i++)
226
36
        probes[i] = probe_file;
227
38
    for (int i = 4 * N_ENCODINGS; i < 6 * N_ENCODINGS; i++)
228
36
        probes[i] = probe_file;
229
10
    for (int i = 0; i < N_OPENERS; i++)
230
8
        Rf_protect(probes[6 * N_ENCODINGS + N_OPENERS + i] =
231
8
                       Rf_lang4(w_probe, path, opener[i], cap));
232
2
    return 0;
233
2
}
234
235
/* An error in the probe passes: the slot reads through the same decoder,
236
 * so it fails at the same point, within the cap. */
237
static int inflated_within_cap(SEXP probe)
238
1.67k
{
239
1.67k
    int error = 0;
240
1.67k
    fuzz_label_enter(probe);
241
1.67k
    SEXP within = R_tryEvalSilent(probe, R_GlobalEnv, &error);
242
1.67k
    return error || Rf_asLogical(within) == TRUE;
243
1.67k
}
244
245
int fuzz_target_run(const uint8_t *data, size_t size)
246
5.98k
{
247
5.98k
    if (size < 2 || size > FUZZ_MAX_INPUT)
248
11
        return 0;
249
250
5.97k
    int selected = data[0] % N_CALLS;
251
5.97k
    if (!fuzz_write_scratch(scratch_path, data + 1, size - 1))
252
0
        return 0;
253
5.97k
    if (probes[selected] != NULL && !inflated_within_cap(probes[selected]))
254
49
        return 0;
255
5.92k
    if (!fuzz_set_raw_arg(calls[selected], data + 1, size - 1))
256
0
        return 0;
257
258
5.92k
    fuzz_eval_silent(calls[selected], R_GlobalEnv);
259
5.92k
    return 0;
260
5.92k
}