blob: 549194619a7d1b3bd3578fec258fc0eb56f30d5f [file] [log] [blame]
mtklein65e58242016-01-13 12:57:57 -08001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Fuzz.h"
kjlubickdba57342016-01-21 05:03:28 -08009#include "SkCanvas.h"
10#include "SkCodec.h"
mtkleinf5e97822016-01-15 06:19:53 -080011#include "SkCommandLineFlags.h"
kjlubickdba57342016-01-21 05:03:28 -080012#include "SkData.h"
kjlubickdba57342016-01-21 05:03:28 -080013#include "SkImage.h"
14#include "SkImageEncoder.h"
15#include "SkMallocPixelRef.h"
16#include "SkPicture.h"
kjlubicke5654502016-07-19 16:50:03 -070017#include "SkPicture.h"
18#include "SkPicture.h"
kjlubickdba57342016-01-21 05:03:28 -080019#include "SkStream.h"
20
kjlubick5bd98a22016-02-18 06:27:38 -080021#include <cmath>
mtkleina1159422016-01-15 05:46:54 -080022#include <signal.h>
mtkleinf5e97822016-01-15 06:19:53 -080023#include <stdlib.h>
24
kjlubickdba57342016-01-21 05:03:28 -080025DEFINE_string2(bytes, b, "", "A path to a file. This can be the fuzz bytes or a binary to parse.");
mtkleind4387ea2016-01-21 06:13:52 -080026DEFINE_string2(name, n, "", "If --type is 'api', fuzz the API with this name.");
kjlubickdba57342016-01-21 05:03:28 -080027
kjlubick897a8e32016-06-09 07:15:12 -070028DEFINE_string2(type, t, "api", "How to interpret --bytes, either 'image_scale', 'image_mode', 'skp', 'icc', or 'api'.");
kjlubick2a42f482016-02-16 16:14:23 -080029DEFINE_string2(dump, d, "", "If not empty, dump 'image*' or 'skp' types as a PNG with this name.");
kjlubickdba57342016-01-21 05:03:28 -080030
31static int printUsage(const char* name) {
mtkleind4387ea2016-01-21 06:13:52 -080032 SkDebugf("Usage: %s -t <type> -b <path/to/file> [-n api-to-fuzz]\n", name);
kjlubickdba57342016-01-21 05:03:28 -080033 return 1;
34}
kjlubick2a42f482016-02-16 16:14:23 -080035static uint8_t calculate_option(SkData*);
kjlubickdba57342016-01-21 05:03:28 -080036
reed42943c82016-09-12 12:01:44 -070037static int fuzz_api(sk_sp<SkData>);
38static int fuzz_img(sk_sp<SkData>, uint8_t, uint8_t);
39static int fuzz_skp(sk_sp<SkData>);
40static int fuzz_icc(sk_sp<SkData>);
41static int fuzz_color_deserialize(sk_sp<SkData>);
mtklein65e58242016-01-13 12:57:57 -080042
43int main(int argc, char** argv) {
mtkleinf5e97822016-01-15 06:19:53 -080044 SkCommandLineFlags::Parse(argc, argv);
45
mtkleind0b82342016-01-15 07:56:20 -080046 const char* path = FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0];
bungeman38d909e2016-08-02 14:40:46 -070047 sk_sp<SkData> bytes(SkData::MakeFromFileName(path));
kjlubickdba57342016-01-21 05:03:28 -080048 if (!bytes) {
49 SkDebugf("Could not read %s\n", path);
50 return 2;
51 }
mtklein65e58242016-01-13 12:57:57 -080052
bungeman38d909e2016-08-02 14:40:46 -070053 uint8_t option = calculate_option(bytes.get());
kjlubick2a42f482016-02-16 16:14:23 -080054
mtkleind4387ea2016-01-21 06:13:52 -080055 if (!FLAGS_type.isEmpty()) {
56 switch (FLAGS_type[0][0]) {
reed42943c82016-09-12 12:01:44 -070057 case 'a': return fuzz_api(bytes);
kjlubick2a42f482016-02-16 16:14:23 -080058
reed42943c82016-09-12 12:01:44 -070059 case 'c': return fuzz_color_deserialize(bytes);
kjlubick3e3c1a52016-06-23 10:49:27 -070060
kjlubick2a42f482016-02-16 16:14:23 -080061 case 'i':
kjlubick897a8e32016-06-09 07:15:12 -070062 if (FLAGS_type[0][1] == 'c') { //icc
reed42943c82016-09-12 12:01:44 -070063 return fuzz_icc(bytes);
kjlubick897a8e32016-06-09 07:15:12 -070064 }
kjlubick2a42f482016-02-16 16:14:23 -080065 // We only allow one degree of freedom to avoid a search space explosion for afl-fuzz.
66 if (FLAGS_type[0][6] == 's') { // image_scale
reed42943c82016-09-12 12:01:44 -070067 return fuzz_img(bytes, option, 0);
kjlubick2a42f482016-02-16 16:14:23 -080068 }
69 // image_mode
reed42943c82016-09-12 12:01:44 -070070 return fuzz_img(bytes, 0, option);
71 case 's': return fuzz_skp(bytes);
mtkleind4387ea2016-01-21 06:13:52 -080072 }
kjlubickdba57342016-01-21 05:03:28 -080073 }
74 return printUsage(argv[0]);
75}
76
kjlubick2a42f482016-02-16 16:14:23 -080077// This adds up the first 1024 bytes and returns it as an 8 bit integer. This allows afl-fuzz to
78// deterministically excercise different paths, or *options* (such as different scaling sizes or
79// different image modes) without needing to introduce a parameter. This way we don't need a
80// image_scale1, image_scale2, image_scale4, etc fuzzer, we can just have a image_scale fuzzer.
81// Clients are expected to transform this number into a different range, e.g. with modulo (%).
82static uint8_t calculate_option(SkData* bytes) {
83 uint8_t total = 0;
84 const uint8_t* data = bytes->bytes();
85 for (size_t i = 0; i < 1024 && i < bytes->size(); i++) {
86 total += data[i];
87 }
88 return total;
89}
90
reed42943c82016-09-12 12:01:44 -070091int fuzz_api(sk_sp<SkData> bytes) {
mtkleind4387ea2016-01-21 06:13:52 -080092 const char* name = FLAGS_name.isEmpty() ? "" : FLAGS_name[0];
93
mtklein65e58242016-01-13 12:57:57 -080094 for (auto r = SkTRegistry<Fuzzable>::Head(); r; r = r->next()) {
95 auto fuzzable = r->factory();
mtkleind4387ea2016-01-21 06:13:52 -080096 if (0 == strcmp(name, fuzzable.name)) {
mtkleinf5e97822016-01-15 06:19:53 -080097 SkDebugf("Fuzzing %s...\n", fuzzable.name);
98 Fuzz fuzz(bytes);
mtklein65e58242016-01-13 12:57:57 -080099 fuzzable.fn(&fuzz);
kjlubick47d158e2016-02-01 08:23:50 -0800100 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800101 return 0;
mtklein65e58242016-01-13 12:57:57 -0800102 }
103 }
mtkleind4387ea2016-01-21 06:13:52 -0800104
105 SkDebugf("When using --type api, please choose an API to fuzz with --name/-n:\n");
106 for (auto r = SkTRegistry<Fuzzable>::Head(); r; r = r->next()) {
107 auto fuzzable = r->factory();
108 SkDebugf("\t%s\n", fuzzable.name);
109 }
kjlubickdba57342016-01-21 05:03:28 -0800110 return 1;
111}
112
113static void dump_png(SkBitmap bitmap) {
114 if (!FLAGS_dump.isEmpty()) {
115 SkImageEncoder::EncodeFile(FLAGS_dump[0], bitmap, SkImageEncoder::kPNG_Type, 100);
116 SkDebugf("Dumped to %s\n", FLAGS_dump[0]);
117 }
118}
119
reed42943c82016-09-12 12:01:44 -0700120int fuzz_img(sk_sp<SkData> bytes, uint8_t scale, uint8_t mode) {
kjlubick2a42f482016-02-16 16:14:23 -0800121 // We can scale 1x, 2x, 4x, 8x, 16x
122 scale = scale % 5;
kjlubick5bd98a22016-02-18 06:27:38 -0800123 float fscale = (float)pow(2.0f, scale);
124 SkDebugf("Scaling factor: %f\n", fscale);
kjlubick2a42f482016-02-16 16:14:23 -0800125
126 // We have 4 different modes of decoding, just like DM.
127 mode = mode % 4;
128 SkDebugf("Mode: %d\n", mode);
129
130 // This is mostly copied from DMSrcSink's CodecSrc::draw method.
kjlubick47d158e2016-02-01 08:23:50 -0800131 SkDebugf("Decoding\n");
kjlubickdba57342016-01-21 05:03:28 -0800132 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(bytes));
133 if (nullptr == codec.get()) {
kjlubick47d158e2016-02-01 08:23:50 -0800134 SkDebugf("[terminated] Couldn't create codec.\n");
kjlubickdba57342016-01-21 05:03:28 -0800135 return 3;
136 }
137
138 SkImageInfo decodeInfo = codec->getInfo();
kjlubick2a42f482016-02-16 16:14:23 -0800139
140 SkISize size = codec->getScaledDimensions(fscale);
141 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
142
kjlubickdba57342016-01-21 05:03:28 -0800143 // Construct a color table for the decode if necessary
144 SkAutoTUnref<SkColorTable> colorTable(nullptr);
145 SkPMColor* colorPtr = nullptr;
146 int* colorCountPtr = nullptr;
147 int maxColors = 256;
148 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
149 SkPMColor colors[256];
150 colorTable.reset(new SkColorTable(colors, maxColors));
151 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
152 colorCountPtr = &maxColors;
153 }
154
155 SkBitmap bitmap;
156 SkMallocPixelRef::ZeroedPRFactory zeroFactory;
157 SkCodec::Options options;
158 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
159
kjlubick2a42f482016-02-16 16:14:23 -0800160 if (!bitmap.tryAllocPixels(decodeInfo, &zeroFactory, colorTable.get())) {
kjlubick47d158e2016-02-01 08:23:50 -0800161 SkDebugf("[terminated] Could not allocate memory. Image might be too large (%d x %d)",
kjlubick2a42f482016-02-16 16:14:23 -0800162 decodeInfo.width(), decodeInfo.height());
kjlubickdba57342016-01-21 05:03:28 -0800163 return 4;
164 }
165
kjlubick2a42f482016-02-16 16:14:23 -0800166 switch (mode) {
167 case 0: {//kCodecZeroInit_Mode, kCodec_Mode
168 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options,
169 colorPtr, colorCountPtr)) {
170 case SkCodec::kSuccess:
171 SkDebugf("[terminated] Success!\n");
172 break;
173 case SkCodec::kIncompleteInput:
174 SkDebugf("[terminated] Partial Success\n");
175 break;
176 case SkCodec::kInvalidConversion:
177 SkDebugf("Incompatible colortype conversion\n");
178 // Crash to allow afl-fuzz to know this was a bug.
179 raise(SIGSEGV);
180 default:
181 SkDebugf("[terminated] Couldn't getPixels.\n");
182 return 6;
183 }
184 break;
185 }
186 case 1: {//kScanline_Mode
187 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
188 colorCountPtr)) {
189 SkDebugf("[terminated] Could not start scanline decoder\n");
190 return 7;
191 }
192
193 void* dst = bitmap.getAddr(0, 0);
194 size_t rowBytes = bitmap.rowBytes();
195 uint32_t height = decodeInfo.height();
196 switch (codec->getScanlineOrder()) {
197 case SkCodec::kTopDown_SkScanlineOrder:
198 case SkCodec::kBottomUp_SkScanlineOrder:
kjlubick2a42f482016-02-16 16:14:23 -0800199 // We do not need to check the return value. On an incomplete
200 // image, memory will be filled with a default value.
201 codec->getScanlines(dst, height, rowBytes);
202 break;
203 case SkCodec::kOutOfOrder_SkScanlineOrder: {
204 for (int y = 0; y < decodeInfo.height(); y++) {
205 int dstY = codec->outputScanline(y);
206 void* dstPtr = bitmap.getAddr(0, dstY);
207 // We complete the loop, even if this call begins to fail
208 // due to an incomplete image. This ensures any uninitialized
209 // memory will be filled with the proper value.
210 codec->getScanlines(dstPtr, 1, bitmap.rowBytes());
211 }
212 break;
213 }
214 }
kjlubick47d158e2016-02-01 08:23:50 -0800215 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800216 break;
kjlubick2a42f482016-02-16 16:14:23 -0800217 }
218 case 2: { //kStripe_Mode
219 const int height = decodeInfo.height();
220 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
221 // does not align with image blocks.
222 const int stripeHeight = 37;
223 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
224
225 // Decode odd stripes
226 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
227 colorCountPtr)
228 || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
229 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
230 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
231 // to run this test for image types that do not have this scanline ordering.
232 SkDebugf("[terminated] Could not start top-down scanline decoder\n");
233 return 8;
234 }
235
236 for (int i = 0; i < numStripes; i += 2) {
237 // Skip a stripe
238 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
239 codec->skipScanlines(linesToSkip);
240
241 // Read a stripe
242 const int startY = (i + 1) * stripeHeight;
243 const int linesToRead = SkTMin(stripeHeight, height - startY);
244 if (linesToRead > 0) {
245 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
246 }
247 }
248
249 // Decode even stripes
250 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo, nullptr,
251 colorPtr, colorCountPtr);
252 if (SkCodec::kSuccess != startResult) {
253 SkDebugf("[terminated] Failed to restart scanline decoder with same parameters.\n");
254 return 9;
255 }
256 for (int i = 0; i < numStripes; i += 2) {
257 // Read a stripe
258 const int startY = i * stripeHeight;
259 const int linesToRead = SkTMin(stripeHeight, height - startY);
260 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
261
262 // Skip a stripe
263 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
264 if (linesToSkip > 0) {
265 codec->skipScanlines(linesToSkip);
266 }
267 }
268 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800269 break;
kjlubick2a42f482016-02-16 16:14:23 -0800270 }
271 case 3: { //kSubset_Mode
272 // Arbitrarily choose a divisor.
273 int divisor = 2;
274 // Total width/height of the image.
275 const int W = codec->getInfo().width();
276 const int H = codec->getInfo().height();
277 if (divisor > W || divisor > H) {
278 SkDebugf("[terminated] Cannot codec subset: divisor %d is too big "
279 "with dimensions (%d x %d)\n", divisor, W, H);
280 return 10;
281 }
282 // subset dimensions
283 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
284 const int w = SkAlign2(W / divisor);
285 const int h = SkAlign2(H / divisor);
286 SkIRect subset;
287 SkCodec::Options opts;
288 opts.fSubset = &subset;
289 SkBitmap subsetBm;
290 // We will reuse pixel memory from bitmap.
291 void* pixels = bitmap.getPixels();
292 // Keep track of left and top (for drawing subsetBm into canvas). We could use
293 // fscale * x and fscale * y, but we want integers such that the next subset will start
294 // where the last one ended. So we'll add decodeInfo.width() and height().
295 int left = 0;
296 for (int x = 0; x < W; x += w) {
297 int top = 0;
298 for (int y = 0; y < H; y+= h) {
299 // Do not make the subset go off the edge of the image.
300 const int preScaleW = SkTMin(w, W - x);
301 const int preScaleH = SkTMin(h, H - y);
302 subset.setXYWH(x, y, preScaleW, preScaleH);
303 // And fscale
304 // FIXME: Should we have a version of getScaledDimensions that takes a subset
305 // into account?
306 decodeInfo = decodeInfo.makeWH(
307 SkTMax(1, SkScalarRoundToInt(preScaleW * fscale)),
308 SkTMax(1, SkScalarRoundToInt(preScaleH * fscale)));
309 size_t rowBytes = decodeInfo.minRowBytes();
310 if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes, colorTable.get(),
311 nullptr, nullptr)) {
312 SkDebugf("[terminated] Could not install pixels.\n");
313 return 11;
314 }
315 const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes,
316 &opts, colorPtr, colorCountPtr);
317 switch (result) {
318 case SkCodec::kSuccess:
319 case SkCodec::kIncompleteInput:
320 SkDebugf("okay\n");
321 break;
322 case SkCodec::kInvalidConversion:
323 if (0 == (x|y)) {
324 // First subset is okay to return unimplemented.
325 SkDebugf("[terminated] Incompatible colortype conversion\n");
326 return 12;
327 }
328 // If the first subset succeeded, a later one should not fail.
329 // fall through to failure
330 case SkCodec::kUnimplemented:
331 if (0 == (x|y)) {
332 // First subset is okay to return unimplemented.
333 SkDebugf("[terminated] subset codec not supported\n");
334 return 13;
335 }
336 // If the first subset succeeded, why would a later one fail?
337 // fall through to failure
338 default:
339 SkDebugf("[terminated] subset codec failed to decode (%d, %d, %d, %d) "
340 "with dimensions (%d x %d)\t error %d\n",
341 x, y, decodeInfo.width(), decodeInfo.height(),
342 W, H, result);
343 return 14;
344 }
345 // translate by the scaled height.
346 top += decodeInfo.height();
347 }
348 // translate by the scaled width.
349 left += decodeInfo.width();
350 }
351 SkDebugf("[terminated] Success!\n");
352 break;
353 }
kjlubickdba57342016-01-21 05:03:28 -0800354 default:
kjlubick2a42f482016-02-16 16:14:23 -0800355 SkDebugf("[terminated] Mode not implemented yet\n");
kjlubickdba57342016-01-21 05:03:28 -0800356 }
357
358 dump_png(bitmap);
mtkleinf5e97822016-01-15 06:19:53 -0800359 return 0;
mtklein65e58242016-01-13 12:57:57 -0800360}
361
reed42943c82016-09-12 12:01:44 -0700362int fuzz_skp(sk_sp<SkData> bytes) {
kjlubickdba57342016-01-21 05:03:28 -0800363 SkMemoryStream stream(bytes);
364 SkDebugf("Decoding\n");
reedca2622b2016-03-18 07:25:55 -0700365 sk_sp<SkPicture> pic(SkPicture::MakeFromStream(&stream));
kjlubickdba57342016-01-21 05:03:28 -0800366 if (!pic) {
kjlubick47d158e2016-02-01 08:23:50 -0800367 SkDebugf("[terminated] Couldn't decode as a picture.\n");
kjlubickdba57342016-01-21 05:03:28 -0800368 return 3;
369 }
370 SkDebugf("Rendering\n");
371 SkBitmap bitmap;
372 if (!FLAGS_dump.isEmpty()) {
373 SkIRect size = pic->cullRect().roundOut();
374 bitmap.allocN32Pixels(size.width(), size.height());
375 }
376 SkCanvas canvas(bitmap);
377 canvas.drawPicture(pic);
kjlubick47d158e2016-02-01 08:23:50 -0800378 SkDebugf("[terminated] Success! Decoded and rendered an SkPicture!\n");
kjlubickdba57342016-01-21 05:03:28 -0800379 dump_png(bitmap);
380 return 0;
381}
mtklein65e58242016-01-13 12:57:57 -0800382
reed42943c82016-09-12 12:01:44 -0700383int fuzz_icc(sk_sp<SkData> bytes) {
kjlubick897a8e32016-06-09 07:15:12 -0700384 sk_sp<SkColorSpace> space(SkColorSpace::NewICC(bytes->data(), bytes->size()));
385 if (!space) {
386 SkDebugf("[terminated] Couldn't decode ICC.\n");
387 return 1;
388 }
389 SkDebugf("[terminated] Success! Decoded ICC.\n");
390 return 0;
391}
392
reed42943c82016-09-12 12:01:44 -0700393int fuzz_color_deserialize(sk_sp<SkData> bytes) {
kjlubick3e3c1a52016-06-23 10:49:27 -0700394 sk_sp<SkColorSpace> space(SkColorSpace::Deserialize(bytes->data(), bytes->size()));
395 if (!space) {
396 SkDebugf("[terminated] Couldn't deserialize Colorspace.\n");
397 return 1;
398 }
399 SkDebugf("[terminated] Success! deserialized Colorspace.\n");
400 return 0;
401}
402
reed42943c82016-09-12 12:01:44 -0700403Fuzz::Fuzz(sk_sp<SkData> bytes) : fBytes(bytes), fNextByte(0) {}
mtklein65e58242016-01-13 12:57:57 -0800404
kjlubick43195932016-04-05 12:48:47 -0700405void Fuzz::signalBug () { SkDebugf("Signal bug\n"); raise(SIGSEGV); }
406void Fuzz::signalBoring() { SkDebugf("Signal boring\n"); exit(0); }
mtkleina1159422016-01-15 05:46:54 -0800407
kjlubicke5654502016-07-19 16:50:03 -0700408size_t Fuzz::size() { return fBytes->size(); }
409
410size_t Fuzz::remaining() {
411 return fBytes->size() - fNextByte;
412}
413
mtklein24a22c72016-01-14 04:59:42 -0800414template <typename T>
mtkleina1159422016-01-15 05:46:54 -0800415T Fuzz::nextT() {
416 if (fNextByte + sizeof(T) > fBytes->size()) {
417 this->signalBoring();
mtklein24a22c72016-01-14 04:59:42 -0800418 }
mtkleina1159422016-01-15 05:46:54 -0800419
mtklein24a22c72016-01-14 04:59:42 -0800420 T val;
mtkleina1159422016-01-15 05:46:54 -0800421 memcpy(&val, fBytes->bytes() + fNextByte, sizeof(T));
422 fNextByte += sizeof(T);
mtklein24a22c72016-01-14 04:59:42 -0800423 return val;
424}
425
mtkleina1159422016-01-15 05:46:54 -0800426uint8_t Fuzz::nextB() { return this->nextT<uint8_t >(); }
kjlubick5bd98a22016-02-18 06:27:38 -0800427bool Fuzz::nextBool() { return nextB()&1; }
mtkleina1159422016-01-15 05:46:54 -0800428uint32_t Fuzz::nextU() { return this->nextT<uint32_t>(); }
429float Fuzz::nextF() { return this->nextT<float >(); }
mtklein65e58242016-01-13 12:57:57 -0800430
kjlubick43195932016-04-05 12:48:47 -0700431float Fuzz::nextF1() {
432 // This is the same code as is in SkRandom's nextF()
433 unsigned int floatint = 0x3f800000 | (this->nextU() >> 9);
434 float f = SkBits2Float(floatint) - 1.0f;
435 return f;
436}
kjlubick5bd98a22016-02-18 06:27:38 -0800437
438uint32_t Fuzz::nextRangeU(uint32_t min, uint32_t max) {
439 if (min > max) {
440 SkDebugf("Check mins and maxes (%d, %d)\n", min, max);
441 this->signalBoring();
442 }
443 uint32_t range = max - min + 1;
444 if (0 == range) {
445 return this->nextU();
446 } else {
447 return min + this->nextU() % range;
448 }
449}
450float Fuzz::nextRangeF(float min, float max) {
451 if (min > max) {
452 SkDebugf("Check mins and maxes (%f, %f)\n", min, max);
453 this->signalBoring();
454 }
455 float f = std::abs(this->nextF());
456 if (!std::isnormal(f) && f != 0.0) {
457 this->signalBoring();
458 }
459 return min + fmod(f, (max - min + 1));
460}