kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 1 | /* |
| 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" |
| 9 | #include "SkPath.h" |
| 10 | #include "SkPathOps.h" |
| 11 | |
| 12 | const int kLastOp = SkPathOp::kReverseDifference_SkPathOp; |
| 13 | |
| 14 | void BuildPath(Fuzz* fuzz, |
| 15 | SkPath* path, |
| 16 | int last_verb) { |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 17 | while (!fuzz->exhausted()) { |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 18 | // Use a uint8_t to conserve bytes. This makes our "fuzzed bytes footprint" |
| 19 | // smaller, which leads to more efficient fuzzing. |
| 20 | uint8_t operation; |
| 21 | fuzz->next(&operation); |
| 22 | SkScalar a,b,c,d,e,f; |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 23 | |
| 24 | switch (operation % (last_verb + 1)) { |
| 25 | case SkPath::Verb::kMove_Verb: |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 26 | fuzz->next(&a, &b); |
| 27 | path->moveTo(a, b); |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 28 | break; |
| 29 | |
| 30 | case SkPath::Verb::kLine_Verb: |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 31 | fuzz->next(&a, &b); |
| 32 | path->lineTo(a, b); |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 33 | break; |
| 34 | |
| 35 | case SkPath::Verb::kQuad_Verb: |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 36 | fuzz->next(&a, &b, &c, &d); |
| 37 | path->quadTo(a, b, c, d); |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 38 | break; |
| 39 | |
| 40 | case SkPath::Verb::kConic_Verb: |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 41 | fuzz->next(&a, &b, &c, &d, &e); |
| 42 | path->conicTo(a, b, c, d, e); |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 43 | break; |
| 44 | |
| 45 | case SkPath::Verb::kCubic_Verb: |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 46 | fuzz->next(&a, &b, &c, &d, &e, &f); |
| 47 | path->cubicTo(a, b, c, d, e, f); |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 48 | break; |
| 49 | |
| 50 | case SkPath::Verb::kClose_Verb: |
| 51 | path->close(); |
| 52 | break; |
| 53 | |
| 54 | case SkPath::Verb::kDone_Verb: |
| 55 | // In this case, simply exit. |
| 56 | return; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | DEF_FUZZ(Pathop, fuzz) { |
| 62 | SkOpBuilder builder; |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 63 | |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 64 | uint8_t stragglerOp; |
| 65 | fuzz->next(&stragglerOp); |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 66 | SkPath path; |
| 67 | |
| 68 | BuildPath(fuzz, &path, SkPath::Verb::kDone_Verb); |
| 69 | builder.add(path, static_cast<SkPathOp>(stragglerOp % (kLastOp + 1))); |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 70 | |
| 71 | SkPath result; |
| 72 | builder.resolve(&result); |
| 73 | } |