blob: 7105f7aebd61d74210f91d1f54c7f5ea9ae30124 [file] [log] [blame]
kjlubick5bd98a22016-02-18 06:27:38 -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"
9#include "SkString.h"
10#include "SkParsePath.h"
Hal Canary8a001442018-09-19 11:31:27 -040011
kjlubick5bd98a22016-02-18 06:27:38 -080012#include <stdlib.h>
13
14// Most of this is taken from random_parse_path.cpp and adapted to use the Fuzz
15// instead of SKRandom
16
kjlubick14f984b2016-10-03 11:49:45 -070017static const struct Legal {
kjlubick5bd98a22016-02-18 06:27:38 -080018 char fSymbol;
19 int fScalars;
20} gLegal[] = {
21 { 'M', 2 },
22 { 'H', 1 },
23 { 'V', 1 },
24 { 'L', 2 },
25 { 'Q', 4 },
26 { 'T', 2 },
27 { 'C', 6 },
28 { 'S', 4 },
29 { 'A', 4 },
30 { 'Z', 0 },
31};
32
kjlubick14f984b2016-10-03 11:49:45 -070033static bool gEasy = false; // set to true while debugging to suppress unusual whitespace
kjlubick5bd98a22016-02-18 06:27:38 -080034
35// mostly do nothing, then bias towards spaces
kjlubick14f984b2016-10-03 11:49:45 -070036static const char gWhiteSpace[] = { 0, 0, 0, 0, 0, 0, 0, 0, ' ', ' ', ' ', ' ', 0x09, 0x0D, 0x0A };
kjlubick5bd98a22016-02-18 06:27:38 -080037
38static void add_white(Fuzz* fuzz, SkString* atom) {
39 if (gEasy) {
40 atom->append(" ");
41 return;
42 }
Kevin Lubick416b2482016-11-10 16:17:49 -050043 // Use a uint8_t to conserve bytes. This makes our "fuzzed bytes footprint"
44 // smaller, which leads to more efficient fuzzing.
45 uint8_t reps;
46 fuzz->nextRange(&reps, 0, 2);
47 for (uint8_t rep = 0; rep < reps; ++rep) {
48 uint8_t index;
Kevin Lubickc9f0cc82016-11-15 16:07:02 -050049 fuzz->nextRange(&index, 0, (int) SK_ARRAY_COUNT(gWhiteSpace) - 1);
kjlubick5bd98a22016-02-18 06:27:38 -080050 if (gWhiteSpace[index]) {
51 atom->append(&gWhiteSpace[index], 1);
52 }
53 }
54}
55
Kevin Lubick2f535ce2016-11-01 15:01:12 -040056static void add_some_white(Fuzz* fuzz, SkString* atom) {
57 for(int i = 0; i < 10; i++) {
58 add_white(fuzz, atom);
59 }
60}
61
kjlubick5bd98a22016-02-18 06:27:38 -080062static void add_comma(Fuzz* fuzz, SkString* atom) {
63 if (gEasy) {
64 atom->append(",");
65 return;
66 }
kjlubick5bd98a22016-02-18 06:27:38 -080067 add_white(fuzz, atom);
Kevin Lubick416b2482016-11-10 16:17:49 -050068 bool b;
69 fuzz->next(&b);
70 if (b) {
kjlubick5bd98a22016-02-18 06:27:38 -080071 atom->append(",");
72 }
Kevin Lubick2f535ce2016-11-01 15:01:12 -040073 add_some_white(fuzz, atom);
kjlubick5bd98a22016-02-18 06:27:38 -080074}
75
76SkString MakeRandomParsePathPiece(Fuzz* fuzz) {
77 SkString atom;
Kevin Lubick416b2482016-11-10 16:17:49 -050078 uint8_t index;
Kevin Lubickc9f0cc82016-11-15 16:07:02 -050079 fuzz->nextRange(&index, 0, (int) SK_ARRAY_COUNT(gLegal) - 1);
kjlubick5bd98a22016-02-18 06:27:38 -080080 const Legal& legal = gLegal[index];
81 gEasy ? atom.append("\n") : add_white(fuzz, &atom);
Kevin Lubick416b2482016-11-10 16:17:49 -050082 bool b;
83 fuzz->next(&b);
84 char symbol = legal.fSymbol | (b ? 0x20 : 0);
kjlubick5bd98a22016-02-18 06:27:38 -080085 atom.append(&symbol, 1);
Kevin Lubick416b2482016-11-10 16:17:49 -050086 uint8_t reps;
87 fuzz->nextRange(&reps, 1, 3);
kjlubick5bd98a22016-02-18 06:27:38 -080088 for (int rep = 0; rep < reps; ++rep) {
89 for (int index = 0; index < legal.fScalars; ++index) {
Kevin Lubick416b2482016-11-10 16:17:49 -050090 SkScalar coord;
91 fuzz->nextRange(&coord, 0.0f, 100.0f);
kjlubick5bd98a22016-02-18 06:27:38 -080092 add_white(fuzz, &atom);
93 atom.appendScalar(coord);
94 if (rep < reps - 1 && index < legal.fScalars - 1) {
95 add_comma(fuzz, &atom);
96 } else {
97 add_some_white(fuzz, &atom);
98 }
99 if ('A' == legal.fSymbol && 1 == index) {
Kevin Lubick416b2482016-11-10 16:17:49 -0500100 SkScalar s;
101 fuzz->nextRange(&s, -720.0f, 720.0f);
102 atom.appendScalar(s);
kjlubick5bd98a22016-02-18 06:27:38 -0800103 add_comma(fuzz, &atom);
Kevin Lubick416b2482016-11-10 16:17:49 -0500104 fuzz->next(&b);
105 atom.appendU32(b);
kjlubick5bd98a22016-02-18 06:27:38 -0800106 add_comma(fuzz, &atom);
Kevin Lubick416b2482016-11-10 16:17:49 -0500107 fuzz->next(&b);
108 atom.appendU32(b);
kjlubick5bd98a22016-02-18 06:27:38 -0800109 add_comma(fuzz, &atom);
110 }
111 }
112 }
113 return atom;
114}
115
116DEF_FUZZ(ParsePath, fuzz) {
117 SkPath path;
118 SkString spec;
Kevin Lubick416b2482016-11-10 16:17:49 -0500119 uint8_t count;
120 fuzz->nextRange(&count, 0, 40);
121 for (uint8_t i = 0; i < count; ++i) {
kjlubick5bd98a22016-02-18 06:27:38 -0800122 spec.append(MakeRandomParsePathPiece(fuzz));
123 }
124 SkDebugf("SkParsePath::FromSVGString(%s, &path);\n",spec.c_str());
125 if (!SkParsePath::FromSVGString(spec.c_str(), &path)){
Kevin Lubick2f535ce2016-11-01 15:01:12 -0400126 SkDebugf("Could not decode path\n");
kjlubick5bd98a22016-02-18 06:27:38 -0800127 }
128}