Flutter Engine
The Flutter Engine
FuzzDrawFunctions.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2016 Mozilla Foundation
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"
11#include "include/core/SkFont.h"
13#include "include/core/SkPath.h"
19
20static const int kBmpSize = 24;
21static const int kMaxX = 250;
22static const int kMaxY = 250;
23static const int kPtsLen = 10;
24static const int kTxtLen = 5;
25
26static void init_string(Fuzz* fuzz, char* str, size_t bufSize) {
27 for (size_t i = 0; i < bufSize-1; ++i) {
28 fuzz->nextRange(&str[i], 0x20, 0x7E); // printable ASCII
29 }
30 str[bufSize-1] = '\0';
31}
32
33// make_paint mostly borrowed from FilterFuzz.cpp
34static void init_paint(Fuzz* fuzz, SkPaint* p) {
35 bool b;
36 fuzz->next(&b);
37 p->setAntiAlias(b);
38
39 uint8_t tmp_u8;
40 fuzz->nextRange(&tmp_u8, 0, (int)SkBlendMode::kLastMode);
41 p->setBlendMode(static_cast<SkBlendMode>(tmp_u8));
42
43 SkColor co;
44 fuzz->next(&co);
45 p->setColor(co);
46
47 fuzz->next(&b);
48 p->setDither(b);
49
50 fuzz->nextRange(&tmp_u8, 0, (int)SkPaint::kLast_Cap);
51 p->setStrokeCap(static_cast<SkPaint::Cap>(tmp_u8));
52
53 fuzz->nextRange(&tmp_u8, 0, (int)SkPaint::kLast_Join);
54 p->setStrokeJoin(static_cast<SkPaint::Join>(tmp_u8));
55
56 SkScalar sc;
57 fuzz->next(&sc);
58 p->setStrokeMiter(sc);
59
60 fuzz->next(&sc);
61 p->setStrokeWidth(sc);
62
63 fuzz->nextRange(&tmp_u8, 0, (int)SkPaint::kStrokeAndFill_Style);
64 p->setStyle(static_cast<SkPaint::Style>(tmp_u8));
65}
66
67static void init_bitmap(Fuzz* fuzz, SkBitmap* bmp) {
68 uint8_t colorType;
70 // ColorType needs to match what the system configuration is.
72 colorType = kN32_SkColorType;
73 }
74 bool b;
75 fuzz->next(&b);
80 if (!bmp->tryAllocPixels(info)) {
81 SkDEBUGF("Bitmap not allocated\n");
82 }
83 SkColor c;
84 fuzz->next(&c);
85 bmp->eraseColor(c);
86
87 fuzz->next(&b);
88 SkPaint p;
89 if (b) {
90 init_paint(fuzz, &p);
91 }
92 else {
93 fuzz->next(&c);
94 p.setColor(c);
95 }
96}
97
98static void init_surface(Fuzz* fuzz, sk_sp<SkSurface>* s) {
99 uint8_t x, y;
100 fuzz->nextRange(&x, 1, kMaxX);
101 fuzz->nextRange(&y, 1, kMaxY);
103
104 if (!*s) {
105 // Was possibly too big for the memory constrained fuzzing environments
106 *s = SkSurfaces::Null(x, y);
107 }
108}
109
110
111static void fuzz_drawText(Fuzz* fuzz, sk_sp<SkTypeface> typeface) {
112 SkFont font(std::move(typeface));
113 SkPaint p;
114 init_paint(fuzz, &p);
116 init_surface(fuzz, &surface);
117
118 char text[kTxtLen];
119 init_string(fuzz, text, kTxtLen);
120
121 SkScalar x, y;
122 fuzz->next(&x, &y);
123 // populate pts array
124 SkPoint pts[kPtsLen];
125 for (uint8_t i = 0; i < kPtsLen; ++i) {
126 pts[i].set(x, y);
127 x += font.getSize();
128 }
129
130 bool b;
131 fuzz->next(&b);
132 font.setForceAutoHinting(b);
133 fuzz->next(&b);
134 font.setEmbeddedBitmaps(b);
135 fuzz->next(&b);
136 font.setEmbolden(b);
137 fuzz->next(&b);
139 fuzz->next(&b);
140 font.setLinearMetrics(b);
141 fuzz->next(&b);
142 font.setSubpixel(b);
143 fuzz->next(&x);
144 font.setScaleX(x);
145 fuzz->next(&x);
146 font.setSkewX(x);
147 fuzz->next(&x);
148 font.setSize(x);
149
150 SkCanvas* cnv = surface->getCanvas();
151 fuzz->next(&x);
152 fuzz->next(&y);
154}
155
156static void fuzz_drawCircle(Fuzz* fuzz) {
157 SkPaint p;
158 init_paint(fuzz, &p);
160 init_surface(fuzz, &surface);
161
162 SkScalar a, b, c;
163 fuzz->next(&a, &b, &c);
164 surface->getCanvas()->drawCircle(a, b, c, p);
165}
166
167static void fuzz_drawLine(Fuzz* fuzz) {
168 SkPaint p;
169 init_paint(fuzz, &p);
171 init_surface(fuzz, &surface);
172
173 SkScalar a, b, c, d;
174 fuzz->next(&a, &b, &c, &d);
175 surface->getCanvas()->drawLine(a, b, c, d, p);
176}
177
178static void fuzz_drawRect(Fuzz* fuzz) {
179 SkPaint p;
180 init_paint(fuzz, &p);
182 init_surface(fuzz, &surface);
183
184 SkScalar a, b, c, d;
185 fuzz->next(&a, &b, &c, &d);
186 SkRect r;
187 r = SkRect::MakeXYWH(a, b, c, d);
188
189 SkCanvas* cnv = surface->getCanvas();
190 cnv->drawRect(r, p);
191
192 bool bl;
193 fuzz->next(&bl);
194 fuzz->next(&a, &b, &c, &d);
195 r = SkRect::MakeXYWH(a, b, c, d);
196 cnv->clipRect(r, SkClipOp::kIntersect, bl);
197}
198
199static void fuzz_drawPath(Fuzz* fuzz) {
200 SkPaint p;
201 init_paint(fuzz, &p);
203 init_surface(fuzz, &surface);
204
205 // TODO(kjlubick): put the ability to fuzz a path in shared file, with
206 // other common things (e.g. rects, lines)
207 uint8_t i, j;
208 fuzz->nextRange(&i, 0, 10); // set i to number of operations to perform
209 SkPath path;
210 SkScalar a, b, c, d, e, f;
211 for (int k = 0; k < i; ++k) {
212 fuzz->nextRange(&j, 0, 5); // set j to choose operation to perform
213 switch (j) {
214 case 0:
215 fuzz->next(&a, &b);
216 path.moveTo(a, b);
217 break;
218 case 1:
219 fuzz->next(&a, &b);
220 path.lineTo(a, b);
221 break;
222 case 2:
223 fuzz->next(&a, &b, &c, &d);
224 path.quadTo(a, b, c, d);
225 break;
226 case 3:
227 fuzz->next(&a, &b, &c, &d, &e);
228 path.conicTo(a, b, c, d, e);
229 break;
230 case 4:
231 fuzz->next(&a, &b, &c, &d, &e, &f);
232 path.cubicTo(a, b, c, d, e, f);
233 break;
234 case 5:
235 fuzz->next(&a, &b, &c, &d, &e);
236 path.arcTo(a, b, c, d, e);
237 break;
238 }
239 }
240 path.close();
241
242 SkCanvas* cnv = surface->getCanvas();
243 cnv->drawPath(path, p);
244
245 bool bl;
246 fuzz->next(&bl);
248}
249
250static void fuzz_drawImage(Fuzz* fuzz) {
251 SkPaint p;
252 init_paint(fuzz, &p);
254 init_surface(fuzz, &surface);
255 SkBitmap bmp;
256 init_bitmap(fuzz, &bmp);
257
259
260 bool bl;
261 fuzz->next(&bl);
262 SkScalar a, b;
263 fuzz->next(&a, &b);
264 if (bl) {
265 surface->getCanvas()->drawImage(image, a, b, SkSamplingOptions(), &p);
266 }
267 else {
269 fuzz->next(&a, &b);
271 uint8_t x;
272 fuzz->nextRange(&x, 0, 1);
274 surface->getCanvas()->drawImageRect(image.get(), src, dst, SkSamplingOptions(), &p, cst);
275 }
276}
277
278static void fuzz_drawPaint(Fuzz* fuzz) {
279 SkPaint l, p;
280 init_paint(fuzz, &p);
282 init_surface(fuzz, &surface);
283
284 surface->getCanvas()->drawPaint(p);
285}
286
287DEF_FUZZ(DrawFunctions, fuzz) {
288 uint8_t i;
289 fuzz->next(&i);
290
291 switch(i) {
292 case 0: {
294 if (f == nullptr) {
295 SkDebugf("Could not initialize font.\n");
296 fuzz->signalBug();
297 }
298 SkDEBUGF("Fuzz DrawText\n");
299 fuzz_drawText(fuzz, f);
300 return;
301 }
302 case 1:
303 SkDEBUGF("Fuzz DrawRect\n");
304 fuzz_drawRect(fuzz);
305 return;
306 case 2:
307 SkDEBUGF("Fuzz DrawCircle\n");
308 fuzz_drawCircle(fuzz);
309 return;
310 case 3:
311 SkDEBUGF("Fuzz DrawLine\n");
312 fuzz_drawLine(fuzz);
313 return;
314 case 4:
315 SkDEBUGF("Fuzz DrawPath\n");
316 fuzz_drawPath(fuzz);
317 return;
318 case 5:
319 SkDEBUGF("Fuzz DrawImage/DrawImageRect\n");
320 fuzz_drawImage(fuzz);
321 return;
322 case 6:
323 SkDEBUGF("Fuzz DrawPaint\n");
324 fuzz_drawPaint(fuzz);
325 return;
326 }
327}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
static const int kTxtLen
static void fuzz_drawPaint(Fuzz *fuzz)
static void fuzz_drawRect(Fuzz *fuzz)
static void fuzz_drawImage(Fuzz *fuzz)
static void fuzz_drawCircle(Fuzz *fuzz)
static void init_bitmap(Fuzz *fuzz, SkBitmap *bmp)
static void fuzz_drawText(Fuzz *fuzz, sk_sp< SkTypeface > typeface)
static void init_surface(Fuzz *fuzz, sk_sp< SkSurface > *s)
static void init_string(Fuzz *fuzz, char *str, size_t bufSize)
DEF_FUZZ(DrawFunctions, fuzz)
static void fuzz_drawPath(Fuzz *fuzz)
static void fuzz_drawLine(Fuzz *fuzz)
static const int kMaxY
static void init_paint(Fuzz *fuzz, SkPaint *p)
static const int kBmpSize
static const int kPtsLen
static const int kMaxX
@ kOpaque_SkAlphaType
pixel is opaque
Definition: SkAlphaType.h:28
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
SkBlendMode
Definition: SkBlendMode.h:38
@ kLastMode
last valid value
SkColorType
Definition: SkColorType.h:19
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition: SkColorType.h:26
@ kLastEnum_SkColorType
last valid value
Definition: SkColorType.h:56
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
uint32_t SkColor
Definition: SkColor.h:37
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
#define SkDEBUGF(...)
Definition: SkDebug.h:24
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
Definition: Fuzz.h:24
void next(T *t)
Definition: Fuzz.h:64
void nextRange(T *, Min, Max)
Definition: Fuzz.h:119
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
bool tryAllocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:271
void eraseColor(SkColor4f) const
Definition: SkBitmap.cpp:442
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
SrcRectConstraint
Definition: SkCanvas.h:1541
void clipPath(const SkPath &path, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1456
void drawPath(const SkPath &path, const SkPaint &paint)
Definition: SkCanvas.cpp:1747
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
Definition: SkCanvas.cpp:2484
Definition: SkFont.h:35
@ kAntiAlias
may have transparent pixels on glyph edges
@ kSubpixelAntiAlias
glyph positioned in pixel using transparency
@ kLast_Cap
largest Cap value
Definition: SkPaint.h:337
@ kStrokeAndFill_Style
sets to stroke and fill geometry
Definition: SkPaint.h:195
@ kLast_Join
equivalent to the largest value for Join
Definition: SkPaint.h:362
Definition: SkPath.h:59
static sk_sp< SkTextBlob > MakeFromPosText(const void *text, size_t byteLength, const SkPoint pos[], const SkFont &font, SkTextEncoding encoding=SkTextEncoding::kUTF8)
Definition: SkTextBlob.cpp:803
T * get() const
Definition: SkRefCnt.h:303
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition: main.cc:19
VkSurfaceKHR surface
Definition: main.cc:49
float SkScalar
Definition: extension.cpp:12
static bool b
struct MyStruct s
struct MyStruct a[10]
std::u16string text
double y
double x
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SK_API sk_sp< SkSurface > Null(int width, int height)
sk_sp< SkTypeface > DefaultPortableTypeface()
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
font
Font Metadata and Metrics.
dst
Definition: cp.py:12
SkSamplingOptions(SkFilterMode::kLinear))
static SkImageInfo MakeN32Premul(int width, int height)
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
void set(float x, float y)
Definition: SkPoint_impl.h:200
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609