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/decompress.c
Line
Count
Source
1
/*
2
 * libFuzzer harness for R's memDecompress / compression wrappers.
3
 *
4
 * Feeds raw bytes to memDecompress() for each supported type: gzip,
5
 * bzip2, xz, and "unknown" (magic-byte auto-detection).  The target is
6
 * not the compression libraries (OSS-Fuzz fuzzes those directly) but R's
7
 * wrapper layer in src/main/connections.c: buffer sizing, error recovery,
8
 * and R_alloc output handling.
9
 *
10
 * Adapted from r-afl's decompress harness.
11
 */
12
13
#include <stdint.h>
14
#include <string.h>
15
16
/* The default 1Gb vector limit stays.  memDecompress keeps every
17
 * doubling retry live on the R_alloc stack and then copies the result
18
 * into a RAWSXP, so a stream expanding to N bytes holds about 3N: a 64KB
19
 * bzip2 stream of zeros (1.25GB) needs ~3.75GB.  Raising the limit to
20
 * 4Gb once covered that, but ClusterFuzz runs with -rss_limit_mb=2560,
21
 * so such inputs were killed as out-of-memory (most decompress sessions
22
 * in early November 2026) rather than ending in R's clean "cannot
23
 * allocate" error.  The sizing paths past 2.5GB are unreachable on the
24
 * bots either way; under 1Gb every retry is exercised for outputs up
25
 * to roughly 250MB (measured with bzip2 streams of zeros). */
26
27
#include "common.h"
28
29
23.2k
#define FUZZ_MAX_INPUT (1024 * 64)
30
31
static SEXP calls[4];
32
33
int fuzz_target_init(int *argc, char ***argv)
34
8
{
35
8
    fuzz_init_r();
36
37
8
    SEXP sym_memDecompress = Rf_install("memDecompress");
38
39
8
    SEXP str_gzip, str_bzip2, str_xz, str_unknown;
40
8
    Rf_protect(str_gzip    = Rf_mkString("gzip"));
41
8
    Rf_protect(str_bzip2   = Rf_mkString("bzip2"));
42
8
    Rf_protect(str_xz      = Rf_mkString("xz"));
43
8
    Rf_protect(str_unknown = Rf_mkString("unknown"));
44
45
    /* One call per compression type, sharing a placeholder RAWSXP that
46
     * each iteration swaps out via SETCADR. */
47
8
    SEXP placeholder;
48
8
    Rf_protect(placeholder = Rf_allocVector(RAWSXP, 1));
49
50
8
    SEXP type_strs[4] = { str_gzip, str_bzip2, str_xz, str_unknown };
51
124
    for (int i = 0; i < 4; i++) {
52
116
        Rf_protect(calls[i] = Rf_lang3(sym_memDecompress, placeholder,
53
116
                                       type_strs[i]));
54
116
        SET_TAG(CDDR(calls[i]), Rf_install("type"));
55
116
    }
56
57
8
    fuzz_label_calls(calls, 4);
58
59
    /* Warmup: a minimal gzip stream (1f 8b header + empty deflate). */
60
8
    {
61
8
        int error = 0;
62
8
        unsigned char gz[] = {0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
63
8
                              0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
64
8
                              0x00, 0x00, 0x00, 0x00};
65
8
        SEXP w_raw;
66
8
        Rf_protect(w_raw = Rf_allocVector(RAWSXP, sizeof(gz)));
67
8
        memcpy(RAW(w_raw), gz, sizeof(gz));
68
124
        for (int i = 0; i < 4; i++) {
69
116
            SETCADR(calls[i], w_raw);
70
116
            R_tryEval(calls[i], R_GlobalEnv, &error);
71
116
            error = 0;
72
116
        }
73
8
        Rf_unprotect(1); /* w_raw */
74
8
    }
75
76
8
    return 0;
77
8
}
78
79
int fuzz_target_run(const uint8_t *data, size_t size)
80
23.2k
{
81
23.2k
    if (size < 2 || size > FUZZ_MAX_INPUT)
82
39
        return 0;
83
84
    /* First byte selects the decompressor; the rest is the payload.
85
     * One type per input keeps libFuzzer's coverage feedback clean and
86
     * avoids cross-library state bleed. */
87
23.1k
    int idx = data[0] & 0x03;
88
89
23.1k
    if (!fuzz_set_raw_arg(calls[idx], data + 1, size - 1))
90
0
        return 0;
91
92
23.1k
    fuzz_eval_data_t ed = { .call = calls[idx], .env = R_GlobalEnv };
93
23.1k
    R_ToplevelExec(fuzz_do_eval, &ed);
94
95
23.1k
    return 0;
96
23.1k
}