Kevin Lubick | f034d11 | 2018-02-08 14:31:24 -0500 | [diff] [blame] | 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 "SkCanvas.h" |
| 9 | #include "SkPaint.h" |
| 10 | #include "SkPath.h" |
| 11 | #include "SkReadBuffer.h" |
| 12 | #include "SkSurface.h" |
| 13 | |
| 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) |
| 30 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
Kevin Lubick | 37c0f71 | 2018-02-22 15:49:31 -0500 | [diff] [blame] | 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 | } |
Kevin Lubick | f034d11 | 2018-02-08 14:31:24 -0500 | [diff] [blame] | 42 | SkReadBuffer buf(data, size); |
| 43 | FuzzPathDeserialize(buf); |
| 44 | return 0; |
| 45 | } |
| 46 | #endif |