Coverage Report

Created: 2026-11-30 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/r-oss-fuzz/harnesses/load.c
Line
Count
Source
1
/*
2
 * libFuzzer harness for load() -- R's .RData reader.
3
 *
4
 * load() reads a five-byte magic and, for RD[ABX][2-9], hands the rest
5
 * of the file to the serialization code that unserialize() already
6
 * fuzzes.  Anything else falls through to .Internal(load()) and the
7
 * readers in src/main/saveload.c: the pre-R-1.4.0 formats (RDA1, RDB1,
8
 * RDX1 via NewAsciiLoad/NewBinaryLoad/NewXdrLoad) and the even older
9
 * numeric-magic layouts (1971, 1972, 1975-1977 via DataLoad).  Those
10
 * readers are ~1700 lines that no test suite exercises, yet they still
11
 * run on any .RData file handed to load(), which users routinely do with
12
 * files from the network.
13
 *
14
 * Both slots load into a fresh environment so nothing accumulates across
15
 * iterations.  The entry point only takes a path, so the input is staged
16
 * in a per-process scratch file (see common.h).
17
 */
18
19
#include <stdint.h>
20
#include <string.h>
21
22
/* RDX2/RDX3 files reach the same reader as the unserialize target, and
23
 * it allocates each vector at its declared length before reading an
24
 * element, so a few bytes of input can declare a list of millions.
25
 * Nested copies stay live while each inner one is allocated, and every
26
 * allocation collects all of them, so the cost grows faster than the
27
 * cap: one 17KB input (oss-fuzz 5422240515293184) took 1.7s per
28
 * execution at the shared 1Gb default and 0.8s at 512Mb.  Use
29
 * unserialize.c's cap; its comment covers the coverage trade. */
30
1
#define FUZZ_R_MAX_VSIZE "512Mb"
31
32
#include "common.h"
33
34
40.7k
#define FUZZ_MAX_INPUT (1024 * 64)
35
40.7k
#define N_CALLS 2
36
37
static char *scratch_path;
38
static SEXP calls[N_CALLS];
39
40
int fuzz_target_init(int *argc, char ***argv)
41
2
{
42
2
    (void)argc;
43
2
    (void)argv;
44
2
    fuzz_init_r();
45
46
2
    scratch_path = fuzz_scratch_file("load");
47
2
    if (scratch_path == NULL)
48
0
        abort();
49
50
    /* [0] the public entry point: R-level magic check, then either
51
     *     loadFromConn2 (v2/v3) or the legacy .Internal(load) fallback.
52
     * [1] the legacy fallback directly, so the fuzzer does not have to
53
     *     get past the R-level grepl() on the magic to reach it. */
54
2
    static const char *const sources[N_CALLS] = {
55
2
        "function(path) load(path, envir = new.env())",
56
2
        "function(path) .Internal(load(path, new.env()))",
57
2
    };
58
2
    SEXP path;
59
2
    Rf_protect(path = Rf_mkString(scratch_path));
60
6
    for (int i = 0; i < N_CALLS; i++) {
61
4
        SEXP wrapper;
62
4
        Rf_protect(wrapper = fuzz_make_wrapper(sources[i]));
63
4
        Rf_protect(calls[i] = Rf_lang2(wrapper, path));
64
4
    }
65
2
    fuzz_label_calls(calls, N_CALLS);
66
2
    return 0;
67
2
}
68
69
int fuzz_target_run(const uint8_t *data, size_t size)
70
40.7k
{
71
40.7k
    if (size < 2 || size > FUZZ_MAX_INPUT)
72
26
        return 0;
73
74
40.7k
    if (!fuzz_write_scratch(scratch_path, data + 1, size - 1))
75
0
        return 0;
76
77
40.7k
    fuzz_eval_silent(calls[data[0] % N_CALLS], R_GlobalEnv);
78
40.7k
    return 0;
79
40.7k
}