Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: skia/tools/path_fuzzer/path_fuzzer.cc

Issue 2036523002: Implement a fuzzer for skia paths. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add a comment Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « skia/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stddef.h>
6 #include <stdint.h>
7
8 #include "third_party/skia/include/core/SkCanvas.h"
9 #include "third_party/skia/include/core/SkPaint.h"
10 #include "third_party/skia/include/core/SkPath.h"
11 #include "third_party/skia/include/core/SkSurface.h"
12
13 const int kOperationCount = 7;
14
15 template <typename T>
16 static bool read(const uint8_t** data, size_t* size, T* value) {
17 if (*size < sizeof(T))
18 return false;
19
20 *value = *reinterpret_cast<const T*>(*data);
21 *data += sizeof(T);
22 *size -= sizeof(T);
23 return true;
24 }
25
26 void BuildPath(const uint8_t* data, size_t size, SkPath* path) {
27 uint8_t operation;
28 SkScalar a, b, c, d, e, f;
29 while (read<uint8_t>(&data, &size, &operation)) {
30 switch (operation % kOperationCount) {
31 case 0:
32 if (!read<SkScalar>(&data, &size, &a) ||
33 !read<SkScalar>(&data, &size, &b))
34 return;
35 path->lineTo(a, b);
36 break;
37
38 case 1:
39 if (!read<SkScalar>(&data, &size, &a) ||
40 !read<SkScalar>(&data, &size, &b) ||
41 !read<SkScalar>(&data, &size, &c) ||
42 !read<SkScalar>(&data, &size, &d))
43 return;
44 path->quadTo(a, b, c, d);
45 break;
46
47 case 2:
48 if (!read<SkScalar>(&data, &size, &a) ||
49 !read<SkScalar>(&data, &size, &b) ||
50 !read<SkScalar>(&data, &size, &c) ||
51 !read<SkScalar>(&data, &size, &d) ||
52 !read<SkScalar>(&data, &size, &e))
53 return;
54 path->conicTo(a, b, c, d, e);
55 break;
56
57 case 3:
58 if (!read<SkScalar>(&data, &size, &a) ||
59 !read<SkScalar>(&data, &size, &b) ||
60 !read<SkScalar>(&data, &size, &c) ||
61 !read<SkScalar>(&data, &size, &d) ||
62 !read<SkScalar>(&data, &size, &e) ||
63 !read<SkScalar>(&data, &size, &f))
64 return;
65 path->cubicTo(a, b, c, d, e, f);
66 break;
67
68 case 5:
69 if (!read<SkScalar>(&data, &size, &a) ||
70 !read<SkScalar>(&data, &size, &b))
71 return;
72 path->moveTo(a, b);
73 break;
74
75 case 6:
76 path->close();
77 break;
78 }
79 }
80 }
81
82 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
83 uint8_t w, h, anti_alias;
84 if (!read<uint8_t>(&data, &size, &w))
85 return 0;
86 if (!read<uint8_t>(&data, &size, &h))
87 return 0;
88 if (!read<uint8_t>(&data, &size, &anti_alias))
89 return 0;
90
91 SkScalar a, b, c, d;
92 if (!read<SkScalar>(&data, &size, &a))
93 return 0;
94 if (!read<SkScalar>(&data, &size, &b))
95 return 0;
96 if (!read<SkScalar>(&data, &size, &c))
97 return 0;
98 if (!read<SkScalar>(&data, &size, &d))
99 return 0;
100
101 // SkPath::readFromMemory does not seem to be able to handle arbitrary input.
102 SkPath path;
103 BuildPath(data, size, &path);
104
105 // Try a few potentially interesting things with our path.
106 path.contains(a, b);
107 path.conservativelyContainsRect(SkRect::MakeLTRB(a, b, c, d));
108
109 SkPaint paint_fill;
110 paint_fill.setStyle(SkPaint::Style::kFill_Style);
111 paint_fill.setAntiAlias(anti_alias & 1);
112
113 SkPaint paint_stroke;
114 paint_stroke.setStyle(SkPaint::Style::kStroke_Style);
115 paint_stroke.setStrokeWidth(1);
116 paint_stroke.setAntiAlias(anti_alias & 1);
117
118 SkPaint paint_stroke_and_fill;
119 paint_stroke_and_fill.setStyle(SkPaint::Style::kStrokeAndFill_Style);
120 paint_stroke_and_fill.setStrokeWidth(1);
121 paint_stroke_and_fill.setAntiAlias(anti_alias & 1);
122
123 SkPath dst_path;
124 paint_stroke.getFillPath(path, &dst_path, nullptr);
125
126 // Width and height should never be 0.
127 auto surface(SkSurface::MakeRasterN32Premul(w + 1, h + 1));
mmoroz 2016/06/03 13:52:14 Since MakeRasterN32Premul() accepts int arguments,
Martin Barbella 2016/06/03 20:10:38 Went with x ? x : 1 instead in the extremely unlik
128
129 surface->getCanvas()->drawPath(path, paint_fill);
130 surface->getCanvas()->drawPath(path, paint_stroke);
131 surface->getCanvas()->drawPath(path, paint_stroke_and_fill);
132
133 return 0;
134 }
OLDNEW
« no previous file with comments | « skia/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698