1 /*
2  * Copyright 2018 Google, LLC
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/SkData.h"
10 #include "include/core/SkImage.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkSurface.h"
13 
FuzzImageDecode(sk_sp<SkData> bytes)14 bool FuzzImageDecode(sk_sp<SkData> bytes) {
15     auto img = SkImage::MakeFromEncoded(bytes);
16     if (nullptr == img.get()) {
17         return false;
18     }
19 
20     auto s = SkSurface::MakeRasterN32Premul(128, 128);
21     if (!s) {
22         // May return nullptr in memory-constrained fuzzing environments
23         return false;
24     }
25 
26     s->getCanvas()->drawImage(img, 0, 0);
27     return true;
28 }
29 
30 // TODO(kjlubick): remove IS_FUZZING... after https://crrev.com/c/2410304 lands
31 #if defined(SK_BUILD_FOR_LIBFUZZER) || defined(IS_FUZZING_WITH_LIBFUZZER)
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)32 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33     if (size > 10240) {
34         return 0;
35     }
36     auto bytes = SkData::MakeWithoutCopy(data, size);
37     FuzzImageDecode(bytes);
38     return 0;
39 }
40 #endif
41