Coverage Report

Created: 2026-11-13 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/r-oss-fuzz/harnesses/character.c
Line
Count
Source
1
/*
2
 * Exercise byte-, character-, width-, substring-, case-, translation- and
3
 * quoting operations on strings (src/main/character.c, raw.c, and the
4
 * strsplit entry of grep.c).
5
 *
6
 * Input layout: the first byte selects a call slot, the rest is the
7
 * payload.  Slots 0-8 take the whole payload as the string (the original
8
 * shape of this target, so the stored corpus keeps its meaning).  Slots 9
9
 * and up split the payload at its first newline into an auxiliary string
10
 * (regex or fixed split pattern, chartr translation spec, replacement
11
 * text) before the newline and the subject string after it; without a
12
 * newline a default auxiliary string is used.
13
 *
14
 * Several slots hand R the same bytes marked as UTF-8 or latin1 instead
15
 * of native.  R does not validate the mark (users can set it with
16
 * Encoding<-), so this reaches the multibyte branches and their handling
17
 * of invalid sequences without changing the process locale.
18
 */
19
20
#include <stdint.h>
21
#include <string.h>
22
23
#include "common.h"
24
25
8.92k
#define FUZZ_MAX_INPUT (1024 * 64)
26
4.47k
#define N_FIXED_CALLS 9
27
4.45k
#define N_CALLS 34
28
29
4.45k
#define DEFAULT_AUX "[ ,;]+"
30
31
/* Which input vectors a slot reads; only those are refilled. */
32
4.47k
#define NEED_X      0x01
33
4.44k
#define NEED_XU     0x02
34
4.43k
#define NEED_XL     0x04
35
4.43k
#define NEED_AUX    0x08
36
4.43k
#define NEED_AUXU   0x10
37
4.46k
#define NEED_REGEX  0x20   /* aux is a regex: apply the repeat guard */
38
4.43k
#define NEED_CHARTR 0x40   /* aux is a chartr spec: apply the table guard */
39
40
/*
41
 * Guard against chartr()'s range expansion.
42
 *
43
 * In a multibyte locale do_chartr() expands every range of 'old' (and of
44
 * 'new') into one entry of a table it then sorts.  The table is bounded
45
 * per range by the 1114111 code points (patches/0049) but not per spec:
46
 * a 6.5 KB 'old' repeating "2-\U000E0078" 93 times is an 85-million-entry
47
 * table, 340 MB and a 17 s sort under ASAN (64 s on the bot).  A spec's
48
 * cost is linear in the size it declares, so refuse one whose entries,
49
 * counted the way do_chartr() parses ranges, exceed FUZZ_MAX_CHARTR_TABLE.
50
 * The count is taken on a lenient UTF-8 decode that also accepts the
51
 * withdrawn RFC 2279 5- and 6-byte forms: glibc's mbstowcs() still does,
52
 * so a native-encoded spec can name a code point up to 0x7FFFFFFF and
53
 * "W-\xfc\x84\xbb\xb3\xbb\x80" is an 83-million-entry range (patches/0049
54
 * only validates UTF-8-marked specs).  Decoding the lead byte as a 4-byte
55
 * form counted that range as 1.07 million.
56
 */
57
1.17M
#define FUZZ_MAX_CHARTR_TABLE 2000000.0
58
59
static size_t fuzz_decode_utf8(const unsigned char *p, unsigned int *cp)
60
3.52M
{
61
3.52M
    unsigned int c = p[0];
62
3.52M
    size_t len = c < 0x80 ? 1 : c < 0xe0 ? 2 : c < 0xf0 ? 3 : c < 0xf8 ? 4
63
125k
        : c < 0xfc ? 5 : 6;
64
3.52M
    unsigned int v = len == 1 ? c : c & (0x7f >> len);
65
3.90M
    for (size_t i = 1; i < len; i++) {
66
394k
        if ((p[i] & 0xc0) != 0x80) {
67
7.78k
            *cp = c;   /* not a sequence: one entry per byte */
68
7.78k
            return 1;
69
7.78k
        }
70
386k
        v = (v << 6) | (p[i] & 0x3f);
71
386k
    }
72
3.51M
    *cp = v;
73
3.51M
    return len;
74
3.52M
}
75
76
static int fuzz_chartr_table_excessive(const char *spec)
77
1.02k
{
78
1.02k
    const unsigned char *p = (const unsigned char *)spec;
79
1.02k
    size_t n = strlen(spec);
80
1.02k
    unsigned int cps[4];
81
1.02k
    size_t lens[4];
82
1.02k
    double entries = 0.0;
83
84
1.02k
    size_t i = 0;
85
1.17M
    while (i < n) {
86
        /* decode up to three characters: a range is "x-y" */
87
1.17M
        size_t k = 0, j = i;
88
4.69M
        while (k < 3 && j < n) {
89
3.52M
            lens[k] = fuzz_decode_utf8(p + j, &cps[k]);
90
3.52M
            j += lens[k];
91
3.52M
            k++;
92
3.52M
        }
93
1.17M
        if (k == 3 && cps[1] == '-') {
94
1.15k
            if (cps[2] >= cps[0])
95
867
                entries += (double)(cps[2] - cps[0]) + 1.0;
96
1.15k
            i += lens[0] + lens[1] + lens[2];
97
1.17M
        } else {
98
1.17M
            entries += 1.0;
99
1.17M
            i += lens[0];
100
1.17M
        }
101
1.17M
        if (entries > FUZZ_MAX_CHARTR_TABLE)
102
9
            return 1;
103
1.17M
    }
104
1.01k
    return 0;
105
1.02k
}
106
107
static SEXP input_string;   /* native */
108
static SEXP input_utf8;     /* same bytes, marked UTF-8 */
109
static SEXP input_latin1;   /* same bytes, marked latin1 */
110
static SEXP aux_string;
111
static SEXP aux_utf8;
112
113
static SEXP calls[N_CALLS];
114
static unsigned needs[N_CALLS];
115
116
static SEXP tagged(SEXP call, int pos, const char *tag)
117
36
{
118
36
    SEXP cell = call;
119
140
    for (int i = 0; i < pos; i++)
120
104
        cell = CDR(cell);
121
36
    SET_TAG(cell, Rf_install(tag));
122
36
    return call;
123
36
}
124
125
int fuzz_target_init(int *argc, char ***argv)
126
2
{
127
2
    (void)argc;
128
2
    (void)argv;
129
2
    fuzz_init_r();
130
131
2
    SEXP bytes, chars, width, true_value, one, end, empty, quote, none;
132
2
    Rf_protect(input_string = Rf_allocVector(STRSXP, 1));
133
2
    Rf_protect(input_utf8 = Rf_allocVector(STRSXP, 1));
134
2
    Rf_protect(input_latin1 = Rf_allocVector(STRSXP, 1));
135
2
    Rf_protect(aux_string = Rf_allocVector(STRSXP, 1));
136
2
    Rf_protect(aux_utf8 = Rf_allocVector(STRSXP, 1));
137
2
    Rf_protect(bytes = Rf_mkString("bytes"));
138
2
    Rf_protect(chars = Rf_mkString("chars"));
139
2
    Rf_protect(width = Rf_mkString("width"));
140
2
    Rf_protect(true_value = Rf_ScalarLogical(TRUE));
141
2
    Rf_protect(one = Rf_ScalarInteger(1));
142
2
    Rf_protect(end = Rf_ScalarInteger(32));
143
2
    Rf_protect(empty = Rf_mkString(""));
144
2
    Rf_protect(quote = Rf_mkString("\""));
145
2
    Rf_protect(none = Rf_mkString("none"));
146
147
2
    SEXP nchar = Rf_install("nchar");
148
2
    Rf_protect(calls[0] = Rf_lang3(nchar, input_string, bytes));
149
2
    SET_TAG(CDDR(calls[0]), Rf_install("type"));
150
2
    Rf_protect(calls[1] = Rf_lang3(nchar, input_string, chars));
151
2
    SET_TAG(CDDR(calls[1]), Rf_install("type"));
152
2
    Rf_protect(calls[2] = Rf_lang3(nchar, input_string, width));
153
2
    SET_TAG(CDDR(calls[2]), Rf_install("type"));
154
2
    Rf_protect(calls[3] = Rf_lang4(Rf_install("substr"), input_string, one, end));
155
2
    Rf_protect(calls[4] = Rf_lang4(Rf_install("substring"), input_string, one, end));
156
2
    Rf_protect(calls[5] = Rf_lang4(Rf_install("strsplit"), input_string, empty,
157
2
                                   true_value));
158
2
    SET_TAG(CDDDR(calls[5]), Rf_install("fixed"));
159
2
    Rf_protect(calls[6] = Rf_lang5(Rf_install("encodeString"), input_string,
160
2
                                   quote, none, true_value));
161
2
    SET_TAG(CDDR(calls[6]), Rf_install("quote"));
162
2
    SET_TAG(CDDDR(calls[6]), Rf_install("justify"));
163
2
    SET_TAG(CDR(CDDDR(calls[6])), Rf_install("na.encode"));
164
2
    Rf_protect(calls[7] = Rf_lang3(Rf_install("make.names"), input_string,
165
2
                                   true_value));
166
2
    SET_TAG(CDDR(calls[7]), Rf_install("unique"));
167
168
2
    SEXP to_raw;
169
2
    Rf_protect(to_raw = Rf_lang2(Rf_install("charToRaw"), input_string));
170
2
    Rf_protect(calls[8] = Rf_lang3(Rf_install("rawToChar"), to_raw, true_value));
171
2
    SET_TAG(CDDR(calls[8]), Rf_install("multiple"));
172
20
    for (int i = 0; i < N_FIXED_CALLS; i++)
173
18
        needs[i] = NEED_X;
174
175
2
    SEXP strsplit = Rf_install("strsplit");
176
2
    SEXP strtoi = Rf_install("strtoi");
177
2
    SEXP toupper = Rf_install("toupper");
178
2
    SEXP chartr = Rf_install("chartr");
179
2
    SEXP utf8ToInt = Rf_install("utf8ToInt");
180
2
    SEXP intToUtf8 = Rf_install("intToUtf8");
181
2
    SEXP two, three, four, ten, sixteen, thirtysix, scale;
182
2
    Rf_protect(two = Rf_ScalarInteger(2));
183
2
    Rf_protect(three = Rf_ScalarInteger(3));
184
2
    Rf_protect(four = Rf_ScalarInteger(4));
185
2
    Rf_protect(ten = Rf_ScalarInteger(10));
186
2
    Rf_protect(sixteen = Rf_ScalarInteger(16));
187
2
    Rf_protect(thirtysix = Rf_ScalarInteger(36));
188
2
    Rf_protect(scale = Rf_ScalarInteger(4099));
189
190
    /* Inner calls are built first and protected, since the outer
191
     * Rf_lang* allocates while holding them. */
192
2
    SEXP upper_aux, upper_aux_utf8, codepoints, scaled_bytes;
193
2
    Rf_protect(upper_aux = Rf_lang2(toupper, aux_string));
194
2
    Rf_protect(upper_aux_utf8 = Rf_lang2(toupper, aux_utf8));
195
2
    Rf_protect(codepoints = Rf_lang2(utf8ToInt, input_string));
196
2
    Rf_protect(scaled_bytes = Rf_lang2(Rf_install("as.integer"), to_raw));
197
2
    Rf_protect(scaled_bytes = Rf_lang3(Rf_install("*"), scaled_bytes, scale));
198
199
2
    int k = N_FIXED_CALLS;
200
201
    /* strsplit: PCRE, fixed and byte-wise matching, on native and
202
     * UTF-8-marked input.  Only the empty fixed split ran before.  The
203
     * default engine, TRE, is left alone on purpose (see README.md), so
204
     * every regex slot passes perl = TRUE. */
205
2
    Rf_protect(calls[k] = tagged(tagged(Rf_lang5(strsplit, input_string,
206
2
                                                 aux_string, true_value,
207
2
                                                 true_value),
208
2
                                        3, "perl"), 4, "useBytes"));
209
2
    needs[k++] = NEED_X | NEED_AUX | NEED_REGEX;
210
2
    Rf_protect(calls[k] = tagged(Rf_lang4(strsplit, input_string, aux_string,
211
2
                                          true_value), 3, "perl"));
212
2
    needs[k++] = NEED_X | NEED_AUX | NEED_REGEX;
213
2
    Rf_protect(calls[k] = tagged(Rf_lang4(strsplit, input_string, aux_string,
214
2
                                          true_value), 3, "fixed"));
215
2
    needs[k++] = NEED_X | NEED_AUX;
216
2
    Rf_protect(calls[k] = tagged(tagged(Rf_lang5(strsplit, input_string,
217
2
                                                 aux_string, true_value,
218
2
                                                 true_value),
219
2
                                        3, "fixed"), 4, "useBytes"));
220
2
    needs[k++] = NEED_X | NEED_AUX;
221
2
    Rf_protect(calls[k] = tagged(tagged(Rf_lang5(strsplit, input_utf8,
222
2
                                                 aux_utf8, true_value,
223
2
                                                 true_value),
224
2
                                        3, "perl"), 4, "useBytes"));
225
2
    needs[k++] = NEED_XU | NEED_AUXU | NEED_REGEX;
226
2
    Rf_protect(calls[k] = tagged(Rf_lang4(strsplit, input_utf8, aux_utf8,
227
2
                                          true_value), 3, "perl"));
228
2
    needs[k++] = NEED_XU | NEED_AUXU | NEED_REGEX;
229
2
    Rf_protect(calls[k] = tagged(Rf_lang4(strsplit, input_utf8, aux_utf8,
230
2
                                          true_value), 3, "fixed"));
231
2
    needs[k++] = NEED_XU | NEED_AUXU;
232
233
    /* nchar on marked strings: the UTF-8 and latin1 branches of R_nchar,
234
     * including allowNA for invalid sequences. */
235
2
    Rf_protect(calls[k] = tagged(tagged(Rf_lang4(nchar, input_utf8, chars,
236
2
                                                 true_value), 2, "type"),
237
2
                                 3, "allowNA"));
238
2
    needs[k++] = NEED_XU;
239
2
    Rf_protect(calls[k] = tagged(tagged(Rf_lang4(nchar, input_utf8, width,
240
2
                                                 true_value), 2, "type"),
241
2
                                 3, "allowNA"));
242
2
    needs[k++] = NEED_XU;
243
2
    Rf_protect(calls[k] = tagged(tagged(Rf_lang4(nchar, input_latin1, chars,
244
2
                                                 true_value), 2, "type"),
245
2
                                 3, "allowNA"));
246
2
    needs[k++] = NEED_XL;
247
248
    /* chartr(old, new, x): old and new must have the same number of
249
     * characters, which toupper preserves, so the spec parser (ranges,
250
     * escapes) runs on fuzzed text while the call stays valid. */
251
2
    Rf_protect(calls[k] = Rf_lang4(chartr, aux_string, upper_aux, input_string));
252
2
    needs[k++] = NEED_X | NEED_AUX | NEED_CHARTR;
253
2
    Rf_protect(calls[k] = Rf_lang4(chartr, aux_utf8, upper_aux_utf8, input_utf8));
254
2
    needs[k++] = NEED_XU | NEED_AUXU | NEED_CHARTR;
255
256
    /* strtoi in the bases with distinct digit handling */
257
2
    Rf_protect(calls[k] = Rf_lang3(strtoi, input_string, ten));
258
2
    needs[k++] = NEED_X;
259
2
    Rf_protect(calls[k] = Rf_lang3(strtoi, input_string, sixteen));
260
2
    needs[k++] = NEED_X;
261
2
    Rf_protect(calls[k] = Rf_lang3(strtoi, input_string, thirtysix));
262
2
    needs[k++] = NEED_X;
263
264
2
    Rf_protect(calls[k] = Rf_lang3(Rf_install("strtrim"), input_utf8, ten));
265
2
    needs[k++] = NEED_XU;
266
2
    Rf_protect(calls[k] = Rf_lang2(toupper, input_utf8));
267
2
    needs[k++] = NEED_XU;
268
2
    Rf_protect(calls[k] = Rf_lang2(Rf_install("tolower"), input_latin1));
269
2
    needs[k++] = NEED_XL;
270
2
    Rf_protect(calls[k] = tagged(Rf_lang3(Rf_install("abbreviate"), input_string,
271
2
                                          four), 2, "minlength"));
272
2
    needs[k++] = NEED_X;
273
274
    /* substr(x, 2, 10) <- aux */
275
2
    Rf_protect(calls[k] = Rf_lang5(Rf_install("substr<-"), input_utf8, two, ten,
276
2
                                   aux_utf8));
277
2
    needs[k++] = NEED_XU | NEED_AUXU;
278
279
2
    Rf_protect(calls[k] = Rf_lang3(Rf_install("strrep"), input_string, three));
280
2
    needs[k++] = NEED_X;
281
282
    /* UTF-8 <-> code point conversion in raw.c, and intToUtf8 fed code
283
     * points scaled out of the bytes to reach surrogates and 4-byte
284
     * forms. */
285
2
    calls[k] = codepoints;
286
2
    needs[k++] = NEED_X;
287
2
    Rf_protect(calls[k] = tagged(Rf_lang3(intToUtf8, codepoints, true_value),
288
2
                                 2, "multiple"));
289
2
    needs[k++] = NEED_X;
290
2
    Rf_protect(calls[k] = Rf_lang2(intToUtf8, scaled_bytes));
291
2
    needs[k++] = NEED_X;
292
293
2
    Rf_protect(calls[k] = Rf_lang4(Rf_install("substr"), input_utf8, two, end));
294
2
    needs[k++] = NEED_XU;
295
296
2
    if (k != N_CALLS)
297
0
        abort();
298
2
    fuzz_label_calls(calls, N_CALLS);
299
300
2
    SET_STRING_ELT(input_string, 0, Rf_mkChar("fuzz"));
301
2
    SET_STRING_ELT(input_utf8, 0, Rf_mkCharCE("fuzz", CE_UTF8));
302
2
    SET_STRING_ELT(input_latin1, 0, Rf_mkCharCE("fuzz", CE_LATIN1));
303
2
    SET_STRING_ELT(aux_string, 0, Rf_mkChar(DEFAULT_AUX));
304
2
    SET_STRING_ELT(aux_utf8, 0, Rf_mkCharCE(DEFAULT_AUX, CE_UTF8));
305
2
    return 0;
306
2
}
307
308
int fuzz_target_run(const uint8_t *data, size_t size)
309
4.46k
{
310
4.46k
    if (size < 2 || size > FUZZ_MAX_INPUT || memchr(data + 1, '\0', size - 1))
311
15
        return 0;
312
313
4.45k
    int selected = data[0] % N_CALLS;
314
4.45k
    unsigned need = needs[selected];
315
316
4.45k
    char buffer[FUZZ_MAX_INPUT];
317
4.45k
    memcpy(buffer, data + 1, size - 1);
318
4.45k
    buffer[size - 1] = '\0';
319
320
4.45k
    const char *aux = DEFAULT_AUX;
321
4.45k
    const char *subject = buffer;
322
4.45k
    if (selected >= N_FIXED_CALLS) {
323
3.85k
        char *nl = memchr(buffer, '\n', size - 1);
324
3.85k
        if (nl != NULL) {
325
1.29k
            *nl = '\0';
326
1.29k
            aux = buffer;
327
1.29k
            subject = nl + 1;
328
1.29k
        }
329
3.85k
    }
330
331
    /* TRE duplicates the AST for counted repeats; see common.h. */
332
4.45k
    if ((need & NEED_REGEX) && fuzz_repeat_product_excessive(aux))
333
17
        return 0;
334
4.43k
    if ((need & NEED_CHARTR) && fuzz_chartr_table_excessive(aux))
335
9
        return 0;
336
337
4.42k
    if ((need & NEED_X) && !fuzz_set_string(input_string, subject))
338
0
        return 0;
339
4.42k
    if ((need & NEED_XU) && !fuzz_set_string_ce(input_utf8, subject, CE_UTF8))
340
0
        return 0;
341
4.42k
    if ((need & NEED_XL) && !fuzz_set_string_ce(input_latin1, subject, CE_LATIN1))
342
0
        return 0;
343
4.42k
    if ((need & NEED_AUX) && !fuzz_set_string(aux_string, aux))
344
0
        return 0;
345
4.42k
    if ((need & NEED_AUXU) && !fuzz_set_string_ce(aux_utf8, aux, CE_UTF8))
346
0
        return 0;
347
348
4.42k
    fuzz_eval_silent(calls[selected], R_GlobalEnv);
349
4.42k
    return 0;
350
4.42k
}