1 // Copyright 2016 The Chromium Authors
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 "testing/libfuzzer/fuzzers/skia_path_common.h"
9 #include "third_party/skia/include/core/SkCanvas.h"
10 #include "third_party/skia/include/core/SkPaint.h"
11 #include "third_party/skia/include/core/SkPath.h"
12 #include "third_party/skia/include/core/SkPathUtils.h"
13 #include "third_party/skia/include/core/SkSurface.h"
14
15
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)16 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
17 uint8_t w, h, anti_alias;
18 if (!read<uint8_t>(&data, &size, &w))
19 return 0;
20 if (!read<uint8_t>(&data, &size, &h))
21 return 0;
22 if (!read<uint8_t>(&data, &size, &anti_alias))
23 return 0;
24
25 SkScalar a, b, c, d;
26 if (!read<SkScalar>(&data, &size, &a))
27 return 0;
28 if (!read<SkScalar>(&data, &size, &b))
29 return 0;
30 if (!read<SkScalar>(&data, &size, &c))
31 return 0;
32 if (!read<SkScalar>(&data, &size, &d))
33 return 0;
34
35 // In this case, we specifically don't want to include kDone_Verb.
36 SkPath path;
37 BuildPath(&data, &size, &path, SkPath::Verb::kClose_Verb);
38
39 // Try a few potentially interesting things with our path.
40 path.contains(a, b);
41 path.conservativelyContainsRect(SkRect::MakeLTRB(a, b, c, d));
42
43 SkPaint paint_fill;
44 paint_fill.setStyle(SkPaint::Style::kFill_Style);
45 paint_fill.setAntiAlias(anti_alias & 1);
46
47 SkPaint paint_stroke;
48 paint_stroke.setStyle(SkPaint::Style::kStroke_Style);
49 paint_stroke.setStrokeWidth(1);
50 paint_stroke.setAntiAlias(anti_alias & 1);
51
52 SkPath dst_path;
53 skpathutils::FillPathWithPaint(path, paint_stroke, &dst_path, nullptr);
54
55 // Width and height should never be 0.
56 auto surface(
57 SkSurfaces::Raster(SkImageInfo::MakeN32Premul(w ? w : 1, h ? h : 1)));
58
59 surface->getCanvas()->drawPath(path, paint_fill);
60 surface->getCanvas()->drawPath(path, paint_stroke);
61
62 return 0;
63 }
64