/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 | 6.74k | #define FUZZ_MAX_INPUT (1024 * 64) |
30 | | |
31 | | static SEXP calls[4]; |
32 | | |
33 | | int fuzz_target_init(int *argc, char ***argv) |
34 | 2 | { |
35 | 2 | fuzz_init_r(); |
36 | | |
37 | 2 | SEXP sym_memDecompress = Rf_install("memDecompress"); |
38 | | |
39 | 2 | SEXP str_gzip, str_bzip2, str_xz, str_unknown; |
40 | 2 | Rf_protect(str_gzip = Rf_mkString("gzip")); |
41 | 2 | Rf_protect(str_bzip2 = Rf_mkString("bzip2")); |
42 | 2 | Rf_protect(str_xz = Rf_mkString("xz")); |
43 | 2 | 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 | 2 | SEXP placeholder; |
48 | 2 | Rf_protect(placeholder = Rf_allocVector(RAWSXP, 1)); |
49 | | |
50 | 2 | SEXP type_strs[4] = { str_gzip, str_bzip2, str_xz, str_unknown }; |
51 | 10 | for (int i = 0; i < 4; i++) { |
52 | 8 | Rf_protect(calls[i] = Rf_lang3(sym_memDecompress, placeholder, |
53 | 8 | type_strs[i])); |
54 | 8 | SET_TAG(CDDR(calls[i]), Rf_install("type")); |
55 | 8 | } |
56 | | |
57 | 2 | fuzz_label_calls(calls, 4); |
58 | | |
59 | | /* Warmup: a minimal gzip stream (1f 8b header + empty deflate). */ |
60 | 2 | { |
61 | 2 | int error = 0; |
62 | 2 | unsigned char gz[] = {0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, |
63 | 2 | 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, |
64 | 2 | 0x00, 0x00, 0x00, 0x00}; |
65 | 2 | SEXP w_raw; |
66 | 2 | Rf_protect(w_raw = Rf_allocVector(RAWSXP, sizeof(gz))); |
67 | 2 | memcpy(RAW(w_raw), gz, sizeof(gz)); |
68 | 10 | for (int i = 0; i < 4; i++) { |
69 | 8 | SETCADR(calls[i], w_raw); |
70 | 8 | R_tryEval(calls[i], R_GlobalEnv, &error); |
71 | 8 | error = 0; |
72 | 8 | } |
73 | 2 | Rf_unprotect(1); /* w_raw */ |
74 | 2 | } |
75 | | |
76 | 2 | return 0; |
77 | 2 | } |
78 | | |
79 | | int fuzz_target_run(const uint8_t *data, size_t size) |
80 | 6.75k | { |
81 | 6.75k | if (size < 2 || size > FUZZ_MAX_INPUT) |
82 | 9 | 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 | 6.74k | int idx = data[0] & 0x03; |
88 | | |
89 | 6.74k | if (!fuzz_set_raw_arg(calls[idx], data + 1, size - 1)) |
90 | 0 | return 0; |
91 | | |
92 | 6.74k | fuzz_eval_data_t ed = { .call = calls[idx], .env = R_GlobalEnv }; |
93 | 6.74k | R_ToplevelExec(fuzz_do_eval, &ed); |
94 | | |
95 | 6.74k | return 0; |
96 | 6.74k | } |