• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 "include/core/SkCanvas.h"
9 #include "include/core/SkPaint.h"
10 #include "include/core/SkPath.h"
11 #include "include/core/SkSurface.h"
12 #include "src/core/SkReadBuffer.h"
13 
FuzzPathDeserialize(SkReadBuffer & buf)14 void FuzzPathDeserialize(SkReadBuffer& buf) {
15     SkPath path;
16     buf.readPath(&path);
17     if (!buf.isValid()) {
18         return;
19     }
20 
21     auto s = SkSurface::MakeRasterN32Premul(128, 128);
22     if (!s) {
23         // May return nullptr in memory-constrained fuzzing environments
24         return;
25     }
26     s->getCanvas()->drawPath(path, SkPaint());
27 }
28 
29 #if defined(IS_FUZZING_WITH_LIBFUZZER)
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)30 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
31     if (size < 4) {
32         return 0;
33     }
34     uint32_t packed;
35     memcpy(&packed, data, 4);
36     unsigned version = packed & 0xFF;
37     if (version != 4) {
38         // Chrome only will produce version 4, so guide the fuzzer to
39         // only focus on those branches.
40         return 0;
41     }
42     SkReadBuffer buf(data, size);
43     FuzzPathDeserialize(buf);
44     return 0;
45 }
46 #endif
47