kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [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 "SkString.h" |
| 10 | #include "SkParsePath.h" |
| 11 | #include <stdlib.h> |
| 12 | |
| 13 | // Most of this is taken from random_parse_path.cpp and adapted to use the Fuzz |
| 14 | // instead of SKRandom |
| 15 | |
kjlubick | 14f984b | 2016-10-03 11:49:45 -0700 | [diff] [blame] | 16 | static const struct Legal { |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 17 | char fSymbol; |
| 18 | int fScalars; |
| 19 | } gLegal[] = { |
| 20 | { 'M', 2 }, |
| 21 | { 'H', 1 }, |
| 22 | { 'V', 1 }, |
| 23 | { 'L', 2 }, |
| 24 | { 'Q', 4 }, |
| 25 | { 'T', 2 }, |
| 26 | { 'C', 6 }, |
| 27 | { 'S', 4 }, |
| 28 | { 'A', 4 }, |
| 29 | { 'Z', 0 }, |
| 30 | }; |
| 31 | |
kjlubick | 14f984b | 2016-10-03 11:49:45 -0700 | [diff] [blame] | 32 | static bool gEasy = false; // set to true while debugging to suppress unusual whitespace |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 33 | |
| 34 | // mostly do nothing, then bias towards spaces |
kjlubick | 14f984b | 2016-10-03 11:49:45 -0700 | [diff] [blame] | 35 | static const char gWhiteSpace[] = { 0, 0, 0, 0, 0, 0, 0, 0, ' ', ' ', ' ', ' ', 0x09, 0x0D, 0x0A }; |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 36 | |
| 37 | static void add_white(Fuzz* fuzz, SkString* atom) { |
| 38 | if (gEasy) { |
| 39 | atom->append(" "); |
| 40 | return; |
| 41 | } |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 42 | // Use a uint8_t to conserve bytes. This makes our "fuzzed bytes footprint" |
| 43 | // smaller, which leads to more efficient fuzzing. |
| 44 | uint8_t reps; |
| 45 | fuzz->nextRange(&reps, 0, 2); |
| 46 | for (uint8_t rep = 0; rep < reps; ++rep) { |
| 47 | uint8_t index; |
Kevin Lubick | c9f0cc8 | 2016-11-15 16:07:02 -0500 | [diff] [blame] | 48 | fuzz->nextRange(&index, 0, (int) SK_ARRAY_COUNT(gWhiteSpace) - 1); |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 49 | if (gWhiteSpace[index]) { |
| 50 | atom->append(&gWhiteSpace[index], 1); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 55 | static void add_some_white(Fuzz* fuzz, SkString* atom) { |
| 56 | for(int i = 0; i < 10; i++) { |
| 57 | add_white(fuzz, atom); |
| 58 | } |
| 59 | } |
| 60 | |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 61 | static void add_comma(Fuzz* fuzz, SkString* atom) { |
| 62 | if (gEasy) { |
| 63 | atom->append(","); |
| 64 | return; |
| 65 | } |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 66 | add_white(fuzz, atom); |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 67 | bool b; |
| 68 | fuzz->next(&b); |
| 69 | if (b) { |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 70 | atom->append(","); |
| 71 | } |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 72 | add_some_white(fuzz, atom); |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | SkString MakeRandomParsePathPiece(Fuzz* fuzz) { |
| 76 | SkString atom; |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 77 | uint8_t index; |
Kevin Lubick | c9f0cc8 | 2016-11-15 16:07:02 -0500 | [diff] [blame] | 78 | fuzz->nextRange(&index, 0, (int) SK_ARRAY_COUNT(gLegal) - 1); |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 79 | const Legal& legal = gLegal[index]; |
| 80 | gEasy ? atom.append("\n") : add_white(fuzz, &atom); |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 81 | bool b; |
| 82 | fuzz->next(&b); |
| 83 | char symbol = legal.fSymbol | (b ? 0x20 : 0); |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 84 | atom.append(&symbol, 1); |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 85 | uint8_t reps; |
| 86 | fuzz->nextRange(&reps, 1, 3); |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 87 | for (int rep = 0; rep < reps; ++rep) { |
| 88 | for (int index = 0; index < legal.fScalars; ++index) { |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 89 | SkScalar coord; |
| 90 | fuzz->nextRange(&coord, 0.0f, 100.0f); |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 91 | add_white(fuzz, &atom); |
| 92 | atom.appendScalar(coord); |
| 93 | if (rep < reps - 1 && index < legal.fScalars - 1) { |
| 94 | add_comma(fuzz, &atom); |
| 95 | } else { |
| 96 | add_some_white(fuzz, &atom); |
| 97 | } |
| 98 | if ('A' == legal.fSymbol && 1 == index) { |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 99 | SkScalar s; |
| 100 | fuzz->nextRange(&s, -720.0f, 720.0f); |
| 101 | atom.appendScalar(s); |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 102 | add_comma(fuzz, &atom); |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 103 | fuzz->next(&b); |
| 104 | atom.appendU32(b); |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 105 | add_comma(fuzz, &atom); |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 106 | fuzz->next(&b); |
| 107 | atom.appendU32(b); |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 108 | add_comma(fuzz, &atom); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | return atom; |
| 113 | } |
| 114 | |
| 115 | DEF_FUZZ(ParsePath, fuzz) { |
| 116 | SkPath path; |
| 117 | SkString spec; |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 118 | uint8_t count; |
| 119 | fuzz->nextRange(&count, 0, 40); |
| 120 | for (uint8_t i = 0; i < count; ++i) { |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 121 | spec.append(MakeRandomParsePathPiece(fuzz)); |
| 122 | } |
| 123 | SkDebugf("SkParsePath::FromSVGString(%s, &path);\n",spec.c_str()); |
| 124 | if (!SkParsePath::FromSVGString(spec.c_str(), &path)){ |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 125 | SkDebugf("Could not decode path\n"); |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 126 | } |
| 127 | } |