2018-03-09 19:02:46 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "fuzz/Fuzz.h"
|
|
|
|
#include "fuzz/FuzzCommon.h"
|
2018-03-09 19:02:46 +00:00
|
|
|
|
2018-09-17 18:46:57 +00:00
|
|
|
// We don't always want to test NaNs and infinities.
|
|
|
|
static void fuzz_nice_float(Fuzz* fuzz, float* f) {
|
|
|
|
float v;
|
|
|
|
fuzz->next(&v);
|
|
|
|
constexpr float kLimit = 1.0e35f; // FLT_MAX?
|
|
|
|
*f = (v == v && v <= kLimit && v >= -kLimit) ? v : 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
static void fuzz_nice_float(Fuzz* fuzz, float* f, Args... rest) {
|
|
|
|
fuzz_nice_float(fuzz, f);
|
|
|
|
fuzz_nice_float(fuzz, rest...);
|
|
|
|
}
|
|
|
|
|
2018-09-25 16:16:53 +00:00
|
|
|
static void fuzz_nice_rect(Fuzz* fuzz, SkRect* r) {
|
|
|
|
fuzz_nice_float(fuzz, &r->fLeft, &r->fTop, &r->fRight, &r->fBottom);
|
|
|
|
r->sort();
|
|
|
|
}
|
2018-09-17 18:46:57 +00:00
|
|
|
|
2018-03-09 19:02:46 +00:00
|
|
|
// allows some float values for path points
|
2018-09-25 16:16:53 +00:00
|
|
|
void FuzzNicePath(Fuzz* fuzz, SkPath* path, int maxOps) {
|
2018-12-17 17:57:53 +00:00
|
|
|
if (maxOps <= 0 || fuzz->exhausted() || path->countPoints() > 100000) {
|
2018-09-12 16:06:18 +00:00
|
|
|
return;
|
2018-03-09 19:02:46 +00:00
|
|
|
}
|
|
|
|
uint8_t fillType;
|
2019-11-26 17:17:17 +00:00
|
|
|
fuzz->nextRange(&fillType, 0, (uint8_t)SkPathFillType::kInverseEvenOdd);
|
|
|
|
path->setFillType((SkPathFillType)fillType);
|
2018-03-09 19:02:46 +00:00
|
|
|
uint8_t numOps;
|
2018-09-12 16:06:18 +00:00
|
|
|
fuzz->nextRange(&numOps, 0, maxOps);
|
2018-03-09 19:02:46 +00:00
|
|
|
for (uint8_t i = 0; i < numOps; ++i) {
|
2018-12-17 17:57:53 +00:00
|
|
|
// When we start adding the path to itself, the fuzzer can make an
|
|
|
|
// exponentially long path, which causes timeouts.
|
|
|
|
if (path->countPoints() > 100000) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// How many items in the switch statement below.
|
|
|
|
constexpr uint8_t PATH_OPERATIONS = 32;
|
2018-03-09 19:02:46 +00:00
|
|
|
uint8_t op;
|
2018-12-17 17:57:53 +00:00
|
|
|
fuzz->nextRange(&op, 0, PATH_OPERATIONS);
|
2018-09-12 16:06:18 +00:00
|
|
|
bool test;
|
|
|
|
SkPath p;
|
|
|
|
SkMatrix m;
|
|
|
|
SkRRect rr;
|
|
|
|
SkRect r;
|
2019-11-22 18:34:02 +00:00
|
|
|
SkPathDirection dir;
|
2018-09-12 16:06:18 +00:00
|
|
|
unsigned int ui;
|
2018-03-09 19:02:46 +00:00
|
|
|
SkScalar a, b, c, d, e, f;
|
|
|
|
switch (op) {
|
|
|
|
case 0:
|
|
|
|
fuzz_nice_float(fuzz, &a, &b);
|
|
|
|
path->moveTo(a, b);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
fuzz_nice_float(fuzz, &a, &b);
|
2018-09-12 16:06:18 +00:00
|
|
|
path->rMoveTo(a, b);
|
2018-03-09 19:02:46 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2018-09-12 16:06:18 +00:00
|
|
|
fuzz_nice_float(fuzz, &a, &b);
|
|
|
|
path->lineTo(a, b);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
fuzz_nice_float(fuzz, &a, &b);
|
|
|
|
path->rLineTo(a, b);
|
|
|
|
break;
|
|
|
|
case 4:
|
2018-03-09 19:02:46 +00:00
|
|
|
fuzz_nice_float(fuzz, &a, &b, &c, &d);
|
|
|
|
path->quadTo(a, b, c, d);
|
|
|
|
break;
|
2018-09-12 16:06:18 +00:00
|
|
|
case 5:
|
|
|
|
fuzz_nice_float(fuzz, &a, &b, &c, &d);
|
|
|
|
path->rQuadTo(a, b, c, d);
|
|
|
|
break;
|
|
|
|
case 6:
|
2018-03-09 19:02:46 +00:00
|
|
|
fuzz_nice_float(fuzz, &a, &b, &c, &d, &e);
|
|
|
|
path->conicTo(a, b, c, d, e);
|
|
|
|
break;
|
2018-09-12 16:06:18 +00:00
|
|
|
case 7:
|
|
|
|
fuzz_nice_float(fuzz, &a, &b, &c, &d, &e);
|
|
|
|
path->rConicTo(a, b, c, d, e);
|
|
|
|
break;
|
|
|
|
case 8:
|
2018-03-09 19:02:46 +00:00
|
|
|
fuzz_nice_float(fuzz, &a, &b, &c, &d, &e, &f);
|
|
|
|
path->cubicTo(a, b, c, d, e, f);
|
|
|
|
break;
|
2018-09-12 16:06:18 +00:00
|
|
|
case 9:
|
|
|
|
fuzz_nice_float(fuzz, &a, &b, &c, &d, &e, &f);
|
|
|
|
path->rCubicTo(a, b, c, d, e, f);
|
|
|
|
break;
|
|
|
|
case 10:
|
2018-03-09 19:02:46 +00:00
|
|
|
fuzz_nice_float(fuzz, &a, &b, &c, &d, &e);
|
|
|
|
path->arcTo(a, b, c, d, e);
|
|
|
|
break;
|
2018-09-12 16:06:18 +00:00
|
|
|
case 11:
|
|
|
|
fuzz_nice_float(fuzz, &a, &b);
|
2018-09-25 16:16:53 +00:00
|
|
|
fuzz_nice_rect(fuzz, &r);
|
|
|
|
fuzz->next(&test);
|
2018-09-12 16:06:18 +00:00
|
|
|
path->arcTo(r, a, b, test);
|
|
|
|
break;
|
|
|
|
case 12:
|
2018-03-09 19:02:46 +00:00
|
|
|
path->close();
|
|
|
|
break;
|
2018-09-12 16:06:18 +00:00
|
|
|
case 13:
|
2018-09-25 16:16:53 +00:00
|
|
|
fuzz_nice_rect(fuzz, &r);
|
2018-09-13 12:22:15 +00:00
|
|
|
fuzz->nextRange(&ui, 0, 1);
|
2019-11-22 18:34:02 +00:00
|
|
|
dir = static_cast<SkPathDirection>(ui);
|
2018-09-12 16:06:18 +00:00
|
|
|
path->addRect(r, dir);
|
|
|
|
break;
|
|
|
|
case 14:
|
2018-09-13 12:22:15 +00:00
|
|
|
fuzz->nextRange(&ui, 0, 1);
|
2019-11-22 18:34:02 +00:00
|
|
|
dir = static_cast<SkPathDirection>(ui);
|
2018-09-25 16:16:53 +00:00
|
|
|
fuzz_nice_rect(fuzz, &r);
|
|
|
|
fuzz->next(&ui);
|
2018-09-12 16:06:18 +00:00
|
|
|
path->addRect(r, dir, ui);
|
|
|
|
break;
|
|
|
|
case 15:
|
2018-09-13 12:22:15 +00:00
|
|
|
fuzz->nextRange(&ui, 0, 1);
|
2019-11-22 18:34:02 +00:00
|
|
|
dir = static_cast<SkPathDirection>(ui);
|
2018-09-25 16:16:53 +00:00
|
|
|
fuzz_nice_rect(fuzz, &r);
|
2018-09-12 16:06:18 +00:00
|
|
|
path->addOval(r, dir);
|
|
|
|
break;
|
|
|
|
case 16:
|
2018-09-13 12:22:15 +00:00
|
|
|
fuzz->nextRange(&ui, 0, 1);
|
2019-11-22 18:34:02 +00:00
|
|
|
dir = static_cast<SkPathDirection>(ui);
|
2018-09-25 16:16:53 +00:00
|
|
|
fuzz_nice_rect(fuzz, &r);
|
|
|
|
fuzz->next(&ui);
|
2018-09-12 16:06:18 +00:00
|
|
|
path->addOval(r, dir, ui);
|
|
|
|
break;
|
|
|
|
case 17:
|
2018-09-13 12:22:15 +00:00
|
|
|
fuzz->nextRange(&ui, 0, 1);
|
2019-11-22 18:34:02 +00:00
|
|
|
dir = static_cast<SkPathDirection>(ui);
|
2018-09-12 16:06:18 +00:00
|
|
|
fuzz_nice_float(fuzz, &a, &b, &c);
|
|
|
|
path->addCircle(a, b, c, dir);
|
|
|
|
break;
|
|
|
|
case 18:
|
2018-09-25 16:16:53 +00:00
|
|
|
fuzz_nice_rect(fuzz, &r);
|
2018-09-12 16:06:18 +00:00
|
|
|
fuzz_nice_float(fuzz, &a, &b);
|
|
|
|
path->addArc(r, a, b);
|
|
|
|
break;
|
|
|
|
case 19:
|
|
|
|
fuzz_nice_float(fuzz, &a, &b);
|
2018-09-25 16:16:53 +00:00
|
|
|
fuzz_nice_rect(fuzz, &r);
|
2018-09-13 12:22:15 +00:00
|
|
|
fuzz->nextRange(&ui, 0, 1);
|
2019-11-22 18:34:02 +00:00
|
|
|
dir = static_cast<SkPathDirection>(ui);
|
2019-09-14 19:13:23 +00:00
|
|
|
path->addRoundRect(r, a, b, dir);
|
2018-09-12 16:06:18 +00:00
|
|
|
break;
|
|
|
|
case 20:
|
2018-09-24 15:33:55 +00:00
|
|
|
FuzzNiceRRect(fuzz, &rr);
|
2018-09-13 12:22:15 +00:00
|
|
|
fuzz->nextRange(&ui, 0, 1);
|
2019-11-22 18:34:02 +00:00
|
|
|
dir = static_cast<SkPathDirection>(ui);
|
2018-09-12 16:06:18 +00:00
|
|
|
path->addRRect(rr, dir);
|
|
|
|
break;
|
|
|
|
case 21:
|
2018-09-13 12:22:15 +00:00
|
|
|
fuzz->nextRange(&ui, 0, 1);
|
2019-11-22 18:34:02 +00:00
|
|
|
dir = static_cast<SkPathDirection>(ui);
|
2018-09-24 15:33:55 +00:00
|
|
|
FuzzNiceRRect(fuzz, &rr);
|
2018-09-12 16:06:18 +00:00
|
|
|
path->addRRect(rr, dir, ui);
|
|
|
|
break;
|
|
|
|
case 22: {
|
2018-09-13 12:22:15 +00:00
|
|
|
fuzz->nextRange(&ui, 0, 1);
|
|
|
|
SkPath::AddPathMode mode = static_cast<SkPath::AddPathMode>(ui);
|
2018-09-25 16:16:53 +00:00
|
|
|
FuzzNiceMatrix(fuzz, &m);
|
|
|
|
FuzzNicePath(fuzz, &p, maxOps-1);
|
2018-09-12 16:06:18 +00:00
|
|
|
path->addPath(p, m, mode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 23: {
|
2018-09-13 12:22:15 +00:00
|
|
|
fuzz->nextRange(&ui, 0, 1);
|
|
|
|
SkPath::AddPathMode mode = static_cast<SkPath::AddPathMode>(ui);
|
2018-09-25 16:16:53 +00:00
|
|
|
FuzzNiceMatrix(fuzz, &m);
|
2018-09-12 16:06:18 +00:00
|
|
|
path->addPath(*path, m, mode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 24:
|
2018-09-25 16:16:53 +00:00
|
|
|
FuzzNicePath(fuzz, &p, maxOps-1);
|
2018-09-12 16:06:18 +00:00
|
|
|
path->reverseAddPath(p);
|
|
|
|
break;
|
|
|
|
case 25:
|
|
|
|
path->addPath(*path);
|
|
|
|
break;
|
|
|
|
case 26:
|
|
|
|
path->reverseAddPath(*path);
|
|
|
|
break;
|
|
|
|
case 27:
|
|
|
|
fuzz_nice_float(fuzz, &a, &b);
|
|
|
|
path->offset(a, b, path);
|
|
|
|
break;
|
|
|
|
case 28:
|
2018-09-25 16:16:53 +00:00
|
|
|
FuzzNicePath(fuzz, &p, maxOps-1);
|
2018-09-12 16:06:18 +00:00
|
|
|
fuzz_nice_float(fuzz, &a, &b);
|
|
|
|
p.offset(a, b, path);
|
|
|
|
break;
|
|
|
|
case 29:
|
2018-09-25 16:16:53 +00:00
|
|
|
FuzzNiceMatrix(fuzz, &m);
|
2018-09-12 16:06:18 +00:00
|
|
|
path->transform(m, path);
|
|
|
|
break;
|
|
|
|
case 30:
|
2018-09-25 16:16:53 +00:00
|
|
|
FuzzNicePath(fuzz, &p, maxOps-1);
|
|
|
|
FuzzNiceMatrix(fuzz, &m);
|
2018-09-12 16:06:18 +00:00
|
|
|
p.transform(m, path);
|
|
|
|
break;
|
|
|
|
case 31:
|
|
|
|
fuzz_nice_float(fuzz, &a, &b);
|
|
|
|
path->setLastPt(a, b);
|
|
|
|
break;
|
2018-12-17 17:57:53 +00:00
|
|
|
case PATH_OPERATIONS:
|
2018-09-12 16:06:18 +00:00
|
|
|
path->shrinkToFit();
|
|
|
|
break;
|
|
|
|
|
2018-03-09 19:02:46 +00:00
|
|
|
default:
|
|
|
|
SkASSERT(false);
|
|
|
|
break;
|
|
|
|
}
|
2018-09-25 16:16:53 +00:00
|
|
|
SkASSERTF( path->isValid(), "path->isValid() failed at op %d, case %d", i, op);
|
2018-03-09 19:02:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// allows all float values for path points
|
2018-09-25 16:16:53 +00:00
|
|
|
void FuzzEvilPath(Fuzz* fuzz, SkPath* path, int last_verb) {
|
2018-03-09 19:02:46 +00:00
|
|
|
while (!fuzz->exhausted()) {
|
|
|
|
// Use a uint8_t to conserve bytes. This makes our "fuzzed bytes footprint"
|
|
|
|
// smaller, which leads to more efficient fuzzing.
|
|
|
|
uint8_t operation;
|
|
|
|
fuzz->next(&operation);
|
|
|
|
SkScalar a,b,c,d,e,f;
|
|
|
|
|
|
|
|
switch (operation % (last_verb + 1)) {
|
|
|
|
case SkPath::Verb::kMove_Verb:
|
|
|
|
fuzz->next(&a, &b);
|
|
|
|
path->moveTo(a, b);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SkPath::Verb::kLine_Verb:
|
|
|
|
fuzz->next(&a, &b);
|
|
|
|
path->lineTo(a, b);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SkPath::Verb::kQuad_Verb:
|
|
|
|
fuzz->next(&a, &b, &c, &d);
|
|
|
|
path->quadTo(a, b, c, d);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SkPath::Verb::kConic_Verb:
|
|
|
|
fuzz->next(&a, &b, &c, &d, &e);
|
|
|
|
path->conicTo(a, b, c, d, e);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SkPath::Verb::kCubic_Verb:
|
|
|
|
fuzz->next(&a, &b, &c, &d, &e, &f);
|
|
|
|
path->cubicTo(a, b, c, d, e, f);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SkPath::Verb::kClose_Verb:
|
|
|
|
path->close();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SkPath::Verb::kDone_Verb:
|
|
|
|
// In this case, simply exit.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-17 18:46:57 +00:00
|
|
|
|
|
|
|
void FuzzNiceRRect(Fuzz* fuzz, SkRRect* rr) {
|
|
|
|
SkRect r;
|
2018-09-25 16:16:53 +00:00
|
|
|
fuzz_nice_rect(fuzz, &r);
|
|
|
|
|
2018-09-17 18:46:57 +00:00
|
|
|
SkVector radii[4];
|
|
|
|
for (SkVector& vec : radii) {
|
|
|
|
fuzz->nextRange(&vec.fX, 0.0f, 1.0f);
|
|
|
|
vec.fX *= 0.5f * r.width();
|
|
|
|
fuzz->nextRange(&vec.fY, 0.0f, 1.0f);
|
|
|
|
vec.fY *= 0.5f * r.height();
|
|
|
|
}
|
|
|
|
rr->setRectRadii(r, radii);
|
2018-09-24 15:33:55 +00:00
|
|
|
SkASSERT(rr->isValid());
|
2018-09-17 18:46:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FuzzNiceMatrix(Fuzz* fuzz, SkMatrix* m) {
|
|
|
|
constexpr int kArrayLength = 9;
|
|
|
|
SkScalar buffer[kArrayLength];
|
|
|
|
int matrixType;
|
|
|
|
fuzz->nextRange(&matrixType, 0, 4);
|
|
|
|
switch (matrixType) {
|
|
|
|
case 0: // identity
|
|
|
|
*m = SkMatrix::I();
|
|
|
|
return;
|
|
|
|
case 1: // translate
|
|
|
|
fuzz->nextRange(&buffer[0], -4000.0f, 4000.0f);
|
|
|
|
fuzz->nextRange(&buffer[1], -4000.0f, 4000.0f);
|
2020-05-21 16:11:27 +00:00
|
|
|
*m = SkMatrix::Translate(buffer[0], buffer[1]);
|
2018-09-17 18:46:57 +00:00
|
|
|
return;
|
|
|
|
case 2: // translate + scale
|
|
|
|
fuzz->nextRange(&buffer[0], -400.0f, 400.0f);
|
|
|
|
fuzz->nextRange(&buffer[1], -400.0f, 400.0f);
|
|
|
|
fuzz->nextRange(&buffer[2], -4000.0f, 4000.0f);
|
|
|
|
fuzz->nextRange(&buffer[3], -4000.0f, 4000.0f);
|
2020-05-21 16:11:27 +00:00
|
|
|
*m = SkMatrix::Scale(buffer[0], buffer[1]);
|
2018-09-17 18:46:57 +00:00
|
|
|
m->postTranslate(buffer[2], buffer[3]);
|
|
|
|
return;
|
|
|
|
case 3: // affine
|
|
|
|
fuzz->nextN(buffer, 6);
|
|
|
|
m->setAffine(buffer);
|
|
|
|
return;
|
|
|
|
case 4: // perspective
|
|
|
|
fuzz->nextN(buffer, kArrayLength);
|
|
|
|
m->set9(buffer);
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
SkASSERT(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FuzzNiceRegion(Fuzz* fuzz, SkRegion* region, int maxN) {
|
|
|
|
uint8_t N;
|
|
|
|
fuzz->nextRange(&N, 0, maxN);
|
|
|
|
for (uint8_t i = 0; i < N; ++i) {
|
|
|
|
SkIRect r;
|
|
|
|
SkRegion::Op op;
|
|
|
|
// Avoid the sentinal value used by Region.
|
|
|
|
fuzz->nextRange(&r.fLeft, -2147483646, 2147483646);
|
|
|
|
fuzz->nextRange(&r.fTop, -2147483646, 2147483646);
|
|
|
|
fuzz->nextRange(&r.fRight, -2147483646, 2147483646);
|
|
|
|
fuzz->nextRange(&r.fBottom, -2147483646, 2147483646);
|
|
|
|
r.sort();
|
2018-11-19 17:21:46 +00:00
|
|
|
fuzz->nextRange(&op, 0, SkRegion::kLastOp);
|
2018-09-17 18:46:57 +00:00
|
|
|
if (!region->op(r, op)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|