/src/r-oss-fuzz/harnesses/nmath.c
Line | Count | Source |
1 | | /* |
2 | | * libFuzzer harness for R's mathematical library, src/nmath: the |
3 | | * density, distribution and quantile functions behind d*, p* and q*, |
4 | | * and the special functions (gamma and beta families, psigamma, |
5 | | * choose, the Bessel functions). |
6 | | * |
7 | | * These are 8.5k lines of numerical code at 1% line coverage, fed by |
8 | | * nothing but R's startup. Their failure modes are the ones a fuzzer |
9 | | * finds well: iterations that never converge for some corner of the |
10 | | * parameter space (a hang), and work arrays sized from a parameter |
11 | | * (bessel_i/k/j/y's nb, signrank's and wilcox's count tables). Doubles |
12 | | * from raw bytes reach every corner, NaN, infinities, subnormals and |
13 | | * huge magnitudes included. The random variate generators (r*) are |
14 | | * left out: they would also fuzz the RNG state, which is a target of |
15 | | * its own. |
16 | | * |
17 | | * Input layout: byte 0 selects the function, byte 1 holds flags (log, |
18 | | * lower.tail, whether an optional ncp/nranges parameter is passed), and |
19 | | * the rest is little-endian doubles: one per parameter, then the x (or |
20 | | * q or p) vector, at most 64 of them. |
21 | | * |
22 | | * Some functions are slow by design in parts of their domain, and the |
23 | | * slot skips those inputs rather than time out on them: phyper/qhyper |
24 | | * walk the support one term at a time (qhyper.c says "Slow, but safe"), |
25 | | * the signrank and wilcox count tables grow with the square of n |
26 | | * (m * n for wilcox) and take time to match. qnbinom has two: with |
27 | | * size < 1 it searches in time linear in the quantile (0.3 s for |
28 | | * mu = 1e8, no answer within minutes for mu = 1e10, in R 4.6.1), and |
29 | | * for any size it never returns once mu passes somewhere between 1e150 |
30 | | * and 1e170, about where the variance overflows; so its mean is kept |
31 | | * under 1e6 and 1e150 respectively. The non-central densities sum a series |
32 | | * whose length grows with the square root of ncp (dchisq: 0.6 s at |
33 | | * ncp = 1e16, over 15 s at 1e20), so ncp is kept under 1e10. |
34 | | */ |
35 | | |
36 | | #include <stdint.h> |
37 | | #include <string.h> |
38 | | |
39 | | #include "common.h" |
40 | | |
41 | 12.2k | #define FUZZ_MAX_INPUT (2 + 8 * (3 + 64)) |
42 | | |
43 | | static const char nmath_source[] = R"( |
44 | | local({ |
45 | | dists <- list( |
46 | | norm = c("mean", "sd"), lnorm = c("meanlog", "sdlog"), |
47 | | unif = c("min", "max"), exp = "rate", gamma = c("shape", "rate"), |
48 | | beta = c("shape1", "shape2", "ncp"), chisq = c("df", "ncp"), |
49 | | t = c("df", "ncp"), f = c("df1", "df2", "ncp"), |
50 | | cauchy = c("location", "scale"), logis = c("location", "scale"), |
51 | | weibull = c("shape", "scale"), binom = c("size", "prob"), |
52 | | nbinom = c("size", "prob"), nbinom = c("size", "mu"), |
53 | | pois = "lambda", geom = "prob", hyper = c("m", "n", "k"), |
54 | | signrank = "n", wilcox = c("m", "n")) |
55 | | specs <- list() |
56 | | for (i in seq_along(dists)) |
57 | | for (kind in c("d", "p", "q")) |
58 | | specs[[length(specs) + 1L]] <- list( |
59 | | fun = get(paste0(kind, names(dists)[i])), kind = kind, |
60 | | dist = names(dists)[i], params = dists[[i]]) |
61 | | specs <- c(specs, list( |
62 | | list(fun = ptukey, kind = "p", dist = "tukey", |
63 | | params = c("nmeans", "df", "nranges")), |
64 | | list(fun = qtukey, kind = "q", dist = "tukey", |
65 | | params = c("nmeans", "df", "nranges")))) |
66 | | special <- list( |
67 | | gamma = NULL, lgamma = NULL, digamma = NULL, trigamma = NULL, |
68 | | factorial = NULL, lfactorial = NULL, beta = "b", lbeta = "b", |
69 | | choose = "k", lchoose = "k", psigamma = "deriv", |
70 | | besselJ = "nu", besselY = "nu", besselI = "nu", besselK = "nu") |
71 | | for (f in names(special)) |
72 | | specs[[length(specs) + 1L]] <- list(fun = get(f), kind = "s", |
73 | | dist = f, params = special[[f]]) |
74 | | optional <- c("ncp", "nranges") |
75 | | |
76 | | function(k, flags, raw) { |
77 | | s <- specs[[k %% length(specs) + 1L]] |
78 | | d <- readBin(raw, "double", n = length(raw) %/% 8L, size = 8L, |
79 | | endian = "little") |
80 | | params <- s$params |
81 | | if (flags %/% 4L %% 2L == 1L && any(params %in% optional)) |
82 | | params <- params[!params %in% optional] |
83 | | np <- length(params) |
84 | | if (length(d) <= np) |
85 | | return(NULL) |
86 | | p <- d[seq_len(np)] |
87 | | x <- d[-seq_len(np)] |
88 | | |
89 | | if (s$dist == "hyper" && any(!is.finite(c(p, x)) | abs(c(p, x)) > 1e6)) |
90 | | return(NULL) |
91 | | if ("ncp" %in% params && !(abs(p[params == "ncp"]) <= 1e10)) |
92 | | return(NULL) |
93 | | if (s$kind == "q" && s$dist == "nbinom") { |
94 | | mean <- if (params[2L] == "mu") p[2L] |
95 | | else p[1L] * (1 - p[2L]) / p[2L] |
96 | | if (!(abs(mean) <= if (p[1L] >= 1) 1e150 else 1e6)) |
97 | | return(NULL) |
98 | | } |
99 | | if (s$dist == "signrank" && !(abs(p[1L]) <= 1000)) |
100 | | return(NULL) |
101 | | if (s$dist == "wilcox" && !all(abs(p) <= 200)) |
102 | | return(NULL) |
103 | | |
104 | | args <- c(list(x), as.list(p)) |
105 | | names(args) <- c("", params) |
106 | | if (s$kind == "s" && s$dist %in% c("besselI", "besselK")) |
107 | | args$expon.scaled <- flags %% 2L == 1L |
108 | | else if (s$kind == "d") |
109 | | args$log <- flags %% 2L == 1L |
110 | | else if (s$kind != "s") { |
111 | | args$lower.tail <- flags %/% 2L %% 2L == 1L |
112 | | args$log.p <- flags %% 2L == 1L |
113 | | } |
114 | | do.call(s$fun, args) |
115 | | } |
116 | | }) |
117 | | )"; |
118 | | |
119 | | static SEXP call; |
120 | | |
121 | | int fuzz_target_init(int *argc, char ***argv) |
122 | 2 | { |
123 | 2 | (void)argc; |
124 | 2 | (void)argv; |
125 | 2 | fuzz_init_r(); |
126 | | |
127 | 2 | SEXP fn, function_index, flags; |
128 | 2 | Rf_protect(fn = fuzz_make_wrapper(nmath_source)); |
129 | 2 | Rf_protect(function_index = Rf_ScalarInteger(0)); |
130 | 2 | Rf_protect(flags = Rf_ScalarInteger(0)); |
131 | 2 | Rf_protect(call = Rf_lang4(fn, function_index, flags, R_NilValue)); |
132 | 2 | fuzz_label_calls(&call, 1); |
133 | | |
134 | | /* Warmup: load stats and its distribution code. */ |
135 | 2 | SEXP raw; |
136 | 2 | Rf_protect(raw = Rf_allocVector(RAWSXP, 16)); |
137 | 2 | memset(RAW(raw), 0, 16); |
138 | 2 | SETCADDDR(call, raw); |
139 | 2 | fuzz_eval_silent(call, R_GlobalEnv); |
140 | 2 | return 0; |
141 | 2 | } |
142 | | |
143 | | int fuzz_target_run(const uint8_t *data, size_t size) |
144 | 12.2k | { |
145 | 12.2k | if (size < 2 + 16 || size > FUZZ_MAX_INPUT) |
146 | 10 | return 0; |
147 | | |
148 | 12.2k | INTEGER(CADR(call))[0] = data[0]; |
149 | 12.2k | INTEGER(CADDR(call))[0] = data[1]; |
150 | | |
151 | | /* fuzz_set_raw_arg sets the argument after the cell it is given, so |
152 | | * passing the flags' cell stages the raw vector as the third. */ |
153 | 12.2k | if (!fuzz_set_raw_arg(CDDR(call), data + 2, size - 2)) |
154 | 0 | return 0; |
155 | | |
156 | 12.2k | fuzz_eval_silent(call, R_GlobalEnv); |
157 | 12.2k | return 0; |
158 | 12.2k | } |