Debian Codegraph @

fuzz/FuzzPathop.cpp

raw
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/Fuzz.h"
9 #include "fuzz/FuzzCommon.h"
10 #include "include/core/SkPath.h"
11 #include "include/core/SkRect.h"
12 #include "include/pathops/SkPathOps.h"
13 #include "src/pathops/SkPathOpsCommon.h"
14
15 #include <utility>
16
17 const uint8_t MAX_OPS = 20;
18
19 DEF_FUZZ(Pathop, fuzz) {
20
21 uint8_t choice;
22 fuzz->nextRange(&choice, 0, 4);
23 switch (choice) {
24 case 0: {
25 uint8_t ops;
26 fuzz->nextRange(&ops, 0, MAX_OPS);
27 SkOpBuilder builder;
28 for (uint8_t i = 0; i < ops && !fuzz->exhausted(); i++) {
29 SkPath path = FuzzEvilPath(fuzz, SkPath::Verb::kDone_Verb);
30 SkPathFillType ft;
31 fuzz->nextRange(&ft, 0, (int)SkPathFillType::kInverseEvenOdd);
32 path.setFillType(ft);
33
34 SkPathOp op;
35 fuzz->nextRange(&op, 0, SkPathOp::kReverseDifference_SkPathOp);
36 builder.add(path, op);
37 }
38
39 SkPath result;
40 builder.resolve(&result);
41 break;
42 }
43 case 1: {
44 SkPath path = FuzzEvilPath(fuzz, SkPath::Verb::kDone_Verb);
45 SkPathFillType ft;
46 fuzz->nextRange(&ft, 0, (int)SkPathFillType::kInverseEvenOdd);
47 std::ignore = Simplify(path.makeFillType(ft));
48 break;
49 }
50 case 2: {
51 SkPath path = FuzzEvilPath(fuzz, SkPath::Verb::kDone_Verb);
52 SkPathFillType ft;
53 fuzz->nextRange(&ft, 0, SkPathFillType::kInverseEvenOdd);
54 path.setFillType(ft);
55
56 SkPath path2 = FuzzEvilPath(fuzz, SkPath::Verb::kDone_Verb);
57 fuzz->nextRange(&ft, 0, SkPathFillType::kInverseEvenOdd);
58 path.setFillType(ft);
59
60 SkPathOp op;
61 fuzz->nextRange(&op, 0, SkPathOp::kReverseDifference_SkPathOp);
62
63 SkPath result;
64 uint8_t pickOutput;
65 fuzz->nextRange(&pickOutput, 0, 2);
66 if (pickOutput == 1) {
67 result = path;
68 } else if (pickOutput == 2) {
69 result = path2;
70 }
71 if (auto res = Op(path, path2, op)) {
72 result = *res;
73 }
74 break;
75 }
76 case 3: {
77 SkPath path = FuzzEvilPath(fuzz, SkPath::Verb::kDone_Verb);
78 SkPathFillType ft;
79 fuzz->nextRange(&ft, 0, SkPathFillType::kInverseEvenOdd);
80 path.setFillType(ft);
81
82 SkPath result;
83 bool isSame;
84 fuzz->next(&isSame);
85 if (isSame) {
86 result = path;
87 }
88 std::ignore = AsWinding(path);
89 break;
90 }
91 case 4: {
92 SkPath path = FuzzEvilPath(fuzz, SkPath::Verb::kDone_Verb);
93 SkPathFillType ft;
94 fuzz->nextRange(&ft, 0, SkPathFillType::kInverseEvenOdd);
95 path.setFillType(ft);
96
97 SkRect result;
98 ComputeTightBounds(path, &result);
99 break;
100 }
101 default: {
102 SkASSERT(false);
103 break;
104 }
105 }
106 }
107
108
109 const int kLastOp = SkPathOp::kReverseDifference_SkPathOp;
110
111 SkPath BuildPath(Fuzz* fuzz) {
112 SkPathBuilder builder;
113 while (!fuzz->exhausted()) {
114 // Use a uint8_t to conserve bytes. This makes our "fuzzed bytes footprint"
115 // smaller, which leads to more efficient fuzzing.
116 uint8_t operation;
117 fuzz->next(&operation);
118 SkScalar a,b,c,d,e,f;
119
120 switch (operation % (SkPath::Verb::kDone_Verb + 1)) {
121 case SkPath::Verb::kMove_Verb:
122 if (fuzz->remainingSize() < (2*sizeof(SkScalar))) {
123 fuzz->deplete();
124 return builder.detach();
125 }
126 fuzz->next(&a, &b);
127 builder.moveTo(a, b);
128 break;
129
130 case SkPath::Verb::kLine_Verb:
131 if (fuzz->remainingSize() < (2*sizeof(SkScalar))) {
132 fuzz->deplete();
133 return builder.detach();
134 }
135 fuzz->next(&a, &b);
136 builder.lineTo(a, b);
137 break;
138
139 case SkPath::Verb::kQuad_Verb:
140 if (fuzz->remainingSize() < (4*sizeof(SkScalar))) {
141 fuzz->deplete();
142 return builder.detach();
143 }
144 fuzz->next(&a, &b, &c, &d);
145 builder.quadTo(a, b, c, d);
146 break;
147
148 case SkPath::Verb::kConic_Verb:
149 if (fuzz->remainingSize() < (5*sizeof(SkScalar))) {
150 fuzz->deplete();
151 return builder.detach();
152 }
153 fuzz->next(&a, &b, &c, &d, &e);
154 if (!(e > 0)) {
155 e = 1.0f;
156 }
157 builder.conicTo(a, b, c, d, e);
158 break;
159
160 case SkPath::Verb::kCubic_Verb:
161 if (fuzz->remainingSize() < (6*sizeof(SkScalar))) {
162 fuzz->deplete();
163 return builder.detach();
164 }
165 fuzz->next(&a, &b, &c, &d, &e, &f);
166 builder.cubicTo(a, b, c, d, e, f);
167 break;
168
169 case SkPath::Verb::kClose_Verb:
170 builder.close();
171 break;
172
173 case SkPath::Verb::kDone_Verb:
174 // In this case, simply exit.
175 return builder.detach();
176 }
177 }
178 return builder.detach();
179 }
180
181 DEF_FUZZ(LegacyChromiumPathop, fuzz) {
182 // See https://cs.chromium.org/chromium/src/testing/libfuzzer/fuzzers/skia_pathop_fuzzer.cc
183 SkOpBuilder builder;
184 while (!fuzz->exhausted()) {
185 uint8_t op;
186 fuzz->next(&op);
187 if (fuzz->exhausted()) {
188 break;
189 }
190
191 SkPath path = BuildPath(fuzz);
192 builder.add(path, static_cast<SkPathOp>(op % (kLastOp + 1)));
193 }
194
195 SkPath result;
196 builder.resolve(&result);
197 }