blob: 0ba3fc96a26ad6233dbad511d0e1e25ea855f780 [file] [log] [blame]
mtklein65e58242016-01-13 12:57:57 -08001/*
2 * Copyright 2016 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 "Fuzz.h"
kjlubickdba57342016-01-21 05:03:28 -08009#include "SkCanvas.h"
10#include "SkCodec.h"
mtkleinf5e97822016-01-15 06:19:53 -080011#include "SkCommandLineFlags.h"
kjlubickdba57342016-01-21 05:03:28 -080012#include "SkData.h"
kjlubickdba57342016-01-21 05:03:28 -080013#include "SkImage.h"
14#include "SkImageEncoder.h"
15#include "SkMallocPixelRef.h"
Kevin Lubickf80f1152017-01-06 08:26:56 -050016#include "SkOSFile.h"
17#include "SkOSPath.h"
Herb Derbya839fc02017-03-16 12:30:43 -040018#include "SkPaint.h"
Hal Canary62176db2017-02-27 16:42:03 -050019#include "SkPath.h"
Cary Clarkefd99cc2018-06-11 16:25:43 -040020#include "SkPicturePriv.h"
Mike Reedfadbfcd2017-12-06 16:09:20 -050021#include "SkReadBuffer.h"
Hal Canary62176db2017-02-27 16:42:03 -050022#include "SkStream.h"
23#include "SkSurface.h"
Kevin Lubickd46e85f2017-11-21 17:13:35 -050024#include "SkTextBlob.h"
Hal Canary62176db2017-02-27 16:42:03 -050025
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050026#if SK_SUPPORT_GPU
kjlubicke7195772016-10-18 10:06:24 -070027#include "SkSLCompiler.h"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050028#endif
kjlubickdba57342016-01-21 05:03:28 -080029
Hal Canary8a001442018-09-19 11:31:27 -040030#include "sk_tool_utils.h"
31
Hal Canary62176db2017-02-27 16:42:03 -050032#include <iostream>
Kevin Lubickacd456a2018-04-24 13:58:16 -040033#include <map>
Kevin Lubickfffa6412018-04-23 16:44:55 -040034#include <regex>
mtkleina1159422016-01-15 05:46:54 -080035#include <signal.h>
Kevin Lubick2541edf2018-01-11 10:27:14 -050036
Hal Canary62176db2017-02-27 16:42:03 -050037DEFINE_string2(bytes, b, "", "A path to a file or a directory. If a file, the "
38 "contents will be used as the fuzz bytes. If a directory, all files "
39 "in the directory will be used as fuzz bytes for the fuzzer, one at a "
40 "time.");
mtkleind4387ea2016-01-21 06:13:52 -080041DEFINE_string2(name, n, "", "If --type is 'api', fuzz the API with this name.");
Hal Canary62176db2017-02-27 16:42:03 -050042DEFINE_string2(dump, d, "", "If not empty, dump 'image*' or 'skp' types as a "
43 "PNG with this name.");
Kevin Lubick9ff5dc92018-01-09 12:47:33 -050044DEFINE_bool2(verbose, v, false, "Print more information while fuzzing.");
Florin Malita7796f002018-06-08 12:25:38 -040045
46// This cannot be inlined in DEFINE_string2 due to interleaved ifdefs
47static constexpr char g_type_message[] = "How to interpret --bytes, one of:\n"
Kevin Lubick0f3d2a62018-10-17 10:24:44 -040048 "android_codec\n"
Florin Malita7796f002018-06-08 12:25:38 -040049 "animated_image_decode\n"
50 "api\n"
51 "color_deserialize\n"
52 "filter_fuzz (equivalent to Chrome's filter_fuzz_stub)\n"
53 "image_decode\n"
Kevin Lubick0f3d2a62018-10-17 10:24:44 -040054 "image_decode_incremental\n"
Florin Malita7796f002018-06-08 12:25:38 -040055 "image_mode\n"
56 "image_scale\n"
Florin Malita80452be2018-06-19 11:27:20 -040057 "json\n"
Florin Malita7796f002018-06-08 12:25:38 -040058 "path_deserialize\n"
Florin Malita7796f002018-06-08 12:25:38 -040059 "region_deserialize\n"
60 "region_set_path\n"
Florin Malita7796f002018-06-08 12:25:38 -040061 "skp\n"
62 "sksl2glsl\n"
63#if defined(SK_ENABLE_SKOTTIE)
64 "skottie_json\n"
65#endif
66 "textblob";
67
68DEFINE_string2(type, t, "", g_type_message);
kjlubickdba57342016-01-21 05:03:28 -080069
Kevin Lubickfffa6412018-04-23 16:44:55 -040070static int fuzz_file(SkString path, SkString type);
kjlubick2a42f482016-02-16 16:14:23 -080071static uint8_t calculate_option(SkData*);
Kevin Lubickfffa6412018-04-23 16:44:55 -040072static SkString try_auto_detect(SkString path, SkString* name);
kjlubickdba57342016-01-21 05:03:28 -080073
Kevin Lubick0f3d2a62018-10-17 10:24:44 -040074static void fuzz_android_codec(sk_sp<SkData>);
75static void fuzz_animated_img(sk_sp<SkData>);
Kevin Lubickfffa6412018-04-23 16:44:55 -040076static void fuzz_api(sk_sp<SkData> bytes, SkString name);
Kevin Lubickf80f1152017-01-06 08:26:56 -050077static void fuzz_color_deserialize(sk_sp<SkData>);
Kevin Lubickd46e85f2017-11-21 17:13:35 -050078static void fuzz_filter_fuzz(sk_sp<SkData>);
Kevin Lubick0f3d2a62018-10-17 10:24:44 -040079static void fuzz_image_decode(sk_sp<SkData>);
80static void fuzz_image_decode_incremental(sk_sp<SkData>);
Kevin Lubick0168e042017-02-14 13:12:37 -050081static void fuzz_img(sk_sp<SkData>, uint8_t, uint8_t);
Florin Malita80452be2018-06-19 11:27:20 -040082static void fuzz_json(sk_sp<SkData>);
Kevin Lubickf04c50a2017-01-06 13:48:19 -050083static void fuzz_path_deserialize(sk_sp<SkData>);
Kevin Lubickedee1ae2017-02-20 17:47:18 -050084static void fuzz_region_deserialize(sk_sp<SkData>);
Kevin Lubick2541edf2018-01-11 10:27:14 -050085static void fuzz_region_set_path(sk_sp<SkData>);
Kevin Lubick0168e042017-02-14 13:12:37 -050086static void fuzz_skp(sk_sp<SkData>);
Kevin Lubickd46e85f2017-11-21 17:13:35 -050087static void fuzz_textblob_deserialize(sk_sp<SkData>);
Herb Derbya839fc02017-03-16 12:30:43 -040088
Kevin Lubickfffa6412018-04-23 16:44:55 -040089static void print_api_names();
90
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050091#if SK_SUPPORT_GPU
Kevin Lubickf80f1152017-01-06 08:26:56 -050092static void fuzz_sksl2glsl(sk_sp<SkData>);
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050093#endif
mtklein65e58242016-01-13 12:57:57 -080094
Florin Malita0b0d93d2018-05-04 15:01:03 -040095#if defined(SK_ENABLE_SKOTTIE)
96static void fuzz_skottie_json(sk_sp<SkData>);
97#endif
98
mtklein65e58242016-01-13 12:57:57 -080099int main(int argc, char** argv) {
Kevin Lubick9ff5dc92018-01-09 12:47:33 -0500100 SkCommandLineFlags::SetUsage("Usage: fuzz -t <type> -b <path/to/file> [-n api-to-fuzz]\n"
Kevin Lubickfffa6412018-04-23 16:44:55 -0400101 " fuzz -b <path/to/file>\n"
102 "--help lists the valid types. If type is not specified,\n"
103 "fuzz will make a guess based on the name of the file.\n");
mtkleinf5e97822016-01-15 06:19:53 -0800104 SkCommandLineFlags::Parse(argc, argv);
105
Kevin Lubickfffa6412018-04-23 16:44:55 -0400106 SkString path = SkString(FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0]);
107 SkString type = SkString(FLAGS_type.isEmpty() ? "" : FLAGS_type[0]);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500108
Kevin Lubickfffa6412018-04-23 16:44:55 -0400109 if (!sk_isdir(path.c_str())) {
110 return fuzz_file(path, type);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500111 }
112
Kevin Lubickfffa6412018-04-23 16:44:55 -0400113 SkOSFile::Iter it(path.c_str());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500114 for (SkString file; it.next(&file); ) {
Kevin Lubickfffa6412018-04-23 16:44:55 -0400115 SkString p = SkOSPath::Join(path.c_str(), file.c_str());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500116 SkDebugf("Fuzzing %s\n", p.c_str());
Kevin Lubickfffa6412018-04-23 16:44:55 -0400117 int rv = fuzz_file(p, type);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500118 if (rv != 0) {
119 return rv;
120 }
121 }
122 return 0;
123}
124
Kevin Lubickfffa6412018-04-23 16:44:55 -0400125static int fuzz_file(SkString path, SkString type) {
126 sk_sp<SkData> bytes(SkData::MakeFromFileName(path.c_str()));
kjlubickdba57342016-01-21 05:03:28 -0800127 if (!bytes) {
Kevin Lubickfffa6412018-04-23 16:44:55 -0400128 SkDebugf("Could not read %s\n", path.c_str());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500129 return 1;
kjlubickdba57342016-01-21 05:03:28 -0800130 }
mtklein65e58242016-01-13 12:57:57 -0800131
Kevin Lubickfffa6412018-04-23 16:44:55 -0400132 SkString name = SkString(FLAGS_name.isEmpty() ? "" : FLAGS_name[0]);
133
134 if (type.isEmpty()) {
135 type = try_auto_detect(path, &name);
kjlubickdba57342016-01-21 05:03:28 -0800136 }
Kevin Lubickfffa6412018-04-23 16:44:55 -0400137
138 if (type.isEmpty()) {
139 SkDebugf("Could not autodetect type of %s\n", path.c_str());
140 return 1;
141 }
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400142 if (type.equals("android_codec")) {
143 fuzz_android_codec(bytes);
144 return 0;
145 }
Kevin Lubickfffa6412018-04-23 16:44:55 -0400146 if (type.equals("animated_image_decode")) {
147 fuzz_animated_img(bytes);
148 return 0;
149 }
150 if (type.equals("api")) {
151 fuzz_api(bytes, name);
152 return 0;
153 }
154 if (type.equals("color_deserialize")) {
155 fuzz_color_deserialize(bytes);
156 return 0;
157 }
Florin Malita80452be2018-06-19 11:27:20 -0400158 if (type.equals("filter_fuzz")) {
159 fuzz_filter_fuzz(bytes);
160 return 0;
161 }
Kevin Lubickfffa6412018-04-23 16:44:55 -0400162 if (type.equals("image_decode")) {
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400163 fuzz_image_decode(bytes);
164 return 0;
165 }
166 if (type.equals("image_decode_incremental")) {
167 fuzz_image_decode_incremental(bytes);
Kevin Lubickfffa6412018-04-23 16:44:55 -0400168 return 0;
169 }
170 if (type.equals("image_scale")) {
171 uint8_t option = calculate_option(bytes.get());
172 fuzz_img(bytes, option, 0);
173 return 0;
174 }
175 if (type.equals("image_mode")) {
176 uint8_t option = calculate_option(bytes.get());
177 fuzz_img(bytes, 0, option);
178 return 0;
179 }
Florin Malita80452be2018-06-19 11:27:20 -0400180 if (type.equals("json")) {
181 fuzz_json(bytes);
Kevin Lubick9eeede22018-05-03 16:26:10 -0400182 return 0;
183 }
Kevin Lubickfffa6412018-04-23 16:44:55 -0400184 if (type.equals("path_deserialize")) {
185 fuzz_path_deserialize(bytes);
186 return 0;
187 }
188 if (type.equals("region_deserialize")) {
189 fuzz_region_deserialize(bytes);
190 return 0;
191 }
192 if (type.equals("region_set_path")) {
193 fuzz_region_set_path(bytes);
194 return 0;
195 }
196 if (type.equals("pipe")) {
Mike Klein60900b52018-09-21 11:19:45 -0400197 SkDebugf("I would prefer not to.\n");
Kevin Lubickfffa6412018-04-23 16:44:55 -0400198 return 0;
199 }
Florin Malita0b0d93d2018-05-04 15:01:03 -0400200#if defined(SK_ENABLE_SKOTTIE)
Kevin Lubick9eeede22018-05-03 16:26:10 -0400201 if (type.equals("skottie_json")) {
202 fuzz_skottie_json(bytes);
Kevin Lubickfffa6412018-04-23 16:44:55 -0400203 return 0;
204 }
Florin Malita0b0d93d2018-05-04 15:01:03 -0400205#endif
Kevin Lubick9eeede22018-05-03 16:26:10 -0400206 if (type.equals("skp")) {
207 fuzz_skp(bytes);
Kevin Lubickfffa6412018-04-23 16:44:55 -0400208 return 0;
209 }
210 if (type.equals("textblob")) {
211 fuzz_textblob_deserialize(bytes);
212 return 0;
213 }
214#if SK_SUPPORT_GPU
215 if (type.equals("sksl2glsl")) {
216 fuzz_sksl2glsl(bytes);
217 return 0;
218 }
219#endif
Kevin Lubick457fa972018-05-29 09:22:06 -0400220 SkDebugf("Unknown type %s\n", type.c_str());
Kevin Lubick9ff5dc92018-01-09 12:47:33 -0500221 SkCommandLineFlags::PrintUsage();
222 return 1;
kjlubickdba57342016-01-21 05:03:28 -0800223}
224
Kevin Lubickfffa6412018-04-23 16:44:55 -0400225static std::map<std::string, std::string> cf_api_map = {
226 {"api_draw_functions", "DrawFunctions"},
227 {"api_gradients", "Gradients"},
228 {"api_image_filter", "ImageFilter"},
229 {"api_mock_gpu_canvas", "MockGPUCanvas"},
230 {"api_null_canvas", "NullCanvas"},
231 {"api_path_measure", "PathMeasure"},
Kevin Lubickde54d7c2018-09-24 08:48:19 -0400232 {"api_pathop", "Pathop"},
Kevin Lubick57507f12018-10-11 09:35:15 -0400233 {"api_polyutils", "PolyUtils"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400234 {"api_raster_n32_canvas", "RasterN32Canvas"},
235 {"jpeg_encoder", "JPEGEncoder"},
236 {"png_encoder", "PNGEncoder"},
Kevin Lubickf84ded22018-10-23 09:28:48 -0400237 {"skia_pathop_fuzzer", "LegacyChromiumPathop"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400238 {"webp_encoder", "WEBPEncoder"}
239};
240
Kevin Lubick6c560552018-06-20 09:29:23 -0400241// maps clusterfuzz/oss-fuzz -> Skia's name
Kevin Lubickfffa6412018-04-23 16:44:55 -0400242static std::map<std::string, std::string> cf_map = {
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400243 {"android_codec", "android_codec"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400244 {"animated_image_decode", "animated_image_decode"},
245 {"image_decode", "image_decode"},
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400246 {"image_decode_incremental", "image_decode_incremental"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400247 {"image_filter_deserialize", "filter_fuzz"},
248 {"image_filter_deserialize_width", "filter_fuzz"},
249 {"path_deserialize", "path_deserialize"},
250 {"region_deserialize", "region_deserialize"},
251 {"region_set_path", "region_set_path"},
Kevin Lubick6c560552018-06-20 09:29:23 -0400252 {"skjson", "json"},
Kevin Lubickde54d7c2018-09-24 08:48:19 -0400253#if defined(SK_ENABLE_SKOTTIE)
254 {"skottie_json", "skottie_json"},
255#endif
Kevin Lubickfffa6412018-04-23 16:44:55 -0400256 {"textblob_deserialize", "textblob"}
257};
258
259static SkString try_auto_detect(SkString path, SkString* name) {
260 std::cmatch m;
261 std::regex clusterfuzz("clusterfuzz-testcase(-minimized)?-([a-z0-9_]+)-[\\d]+");
262 std::regex skiafuzzer("(api-)?(\\w+)-[a-f0-9]+");
263
264 if (std::regex_search(path.c_str(), m, clusterfuzz)) {
265 std::string type = m.str(2);
Kevin Lubick6c560552018-06-20 09:29:23 -0400266
267 if (cf_api_map.find(type) != cf_api_map.end()) {
268 *name = SkString(cf_api_map[type].c_str());
269 return SkString("api");
Kevin Lubickfffa6412018-04-23 16:44:55 -0400270 } else {
271 if (cf_map.find(type) != cf_map.end()) {
272 return SkString(cf_map[type].c_str());
273 }
274 }
275 } else if (std::regex_search(path.c_str(), m, skiafuzzer)) {
276 std::string a1 = m.str(1);
277 std::string typeOrName = m.str(2);
278 if (a1.length() > 0) {
279 // it's an api fuzzer
280 *name = SkString(typeOrName.c_str());
281 return SkString("api");
282 } else {
283 return SkString(typeOrName.c_str());
284 }
285 }
286
287 return SkString("");
288}
289
Florin Malita80452be2018-06-19 11:27:20 -0400290void FuzzJSON(sk_sp<SkData> bytes);
Florin Malita7796f002018-06-08 12:25:38 -0400291
Florin Malita80452be2018-06-19 11:27:20 -0400292static void fuzz_json(sk_sp<SkData> bytes){
293 FuzzJSON(bytes);
Florin Malita7796f002018-06-08 12:25:38 -0400294 SkDebugf("[terminated] Done parsing!\n");
295}
Florin Malita7796f002018-06-08 12:25:38 -0400296
Florin Malita0b0d93d2018-05-04 15:01:03 -0400297#if defined(SK_ENABLE_SKOTTIE)
Kevin Lubick9eeede22018-05-03 16:26:10 -0400298void FuzzSkottieJSON(sk_sp<SkData> bytes);
299
300static void fuzz_skottie_json(sk_sp<SkData> bytes){
301 FuzzSkottieJSON(bytes);
302 SkDebugf("[terminated] Done animating!\n");
303}
Florin Malita0b0d93d2018-05-04 15:01:03 -0400304#endif
Kevin Lubick9eeede22018-05-03 16:26:10 -0400305
kjlubick2a42f482016-02-16 16:14:23 -0800306// This adds up the first 1024 bytes and returns it as an 8 bit integer. This allows afl-fuzz to
307// deterministically excercise different paths, or *options* (such as different scaling sizes or
308// different image modes) without needing to introduce a parameter. This way we don't need a
309// image_scale1, image_scale2, image_scale4, etc fuzzer, we can just have a image_scale fuzzer.
310// Clients are expected to transform this number into a different range, e.g. with modulo (%).
311static uint8_t calculate_option(SkData* bytes) {
312 uint8_t total = 0;
313 const uint8_t* data = bytes->bytes();
314 for (size_t i = 0; i < 1024 && i < bytes->size(); i++) {
315 total += data[i];
316 }
317 return total;
318}
319
Kevin Lubickfffa6412018-04-23 16:44:55 -0400320static void print_api_names(){
321 SkDebugf("When using --type api, please choose an API to fuzz with --name/-n:\n");
Hal Canary972eba32018-07-30 17:07:07 -0400322 for (const Fuzzable& fuzzable : sk_tools::Registry<Fuzzable>::Range()) {
Kevin Lubickfffa6412018-04-23 16:44:55 -0400323 SkDebugf("\t%s\n", fuzzable.name);
324 }
325}
326
327static void fuzz_api(sk_sp<SkData> bytes, SkString name) {
Hal Canary972eba32018-07-30 17:07:07 -0400328 for (const Fuzzable& fuzzable : sk_tools::Registry<Fuzzable>::Range()) {
Kevin Lubickfffa6412018-04-23 16:44:55 -0400329 if (name.equals(fuzzable.name)) {
mtkleinf5e97822016-01-15 06:19:53 -0800330 SkDebugf("Fuzzing %s...\n", fuzzable.name);
Kevin Lubick1ac8fd22017-03-01 10:42:45 -0500331 Fuzz fuzz(std::move(bytes));
mtklein65e58242016-01-13 12:57:57 -0800332 fuzzable.fn(&fuzz);
kjlubick47d158e2016-02-01 08:23:50 -0800333 SkDebugf("[terminated] Success!\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500334 return;
mtklein65e58242016-01-13 12:57:57 -0800335 }
336 }
mtkleind4387ea2016-01-21 06:13:52 -0800337
Kevin Lubickfffa6412018-04-23 16:44:55 -0400338 print_api_names();
kjlubickdba57342016-01-21 05:03:28 -0800339}
340
341static void dump_png(SkBitmap bitmap) {
342 if (!FLAGS_dump.isEmpty()) {
Hal Canarydb683012016-11-23 08:55:18 -0700343 sk_tool_utils::EncodeImageToFile(FLAGS_dump[0], bitmap, SkEncodedImageFormat::kPNG, 100);
kjlubickdba57342016-01-21 05:03:28 -0800344 SkDebugf("Dumped to %s\n", FLAGS_dump[0]);
345 }
346}
347
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400348bool FuzzAnimatedImage(sk_sp<SkData> bytes);
Kevin Lubick2416f962018-02-12 08:26:39 -0500349
350static void fuzz_animated_img(sk_sp<SkData> bytes) {
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400351 if (FuzzAnimatedImage(bytes)) {
352 SkDebugf("[terminated] Success from decoding/drawing animated image!\n");
353 return;
354 }
355 SkDebugf("[terminated] Could not decode or draw animated image.\n");
Kevin Lubick2416f962018-02-12 08:26:39 -0500356}
357
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400358bool FuzzImageDecode(sk_sp<SkData> bytes);
Kevin Lubick2416f962018-02-12 08:26:39 -0500359
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400360static void fuzz_image_decode(sk_sp<SkData> bytes) {
361 if (FuzzImageDecode(bytes)) {
362 SkDebugf("[terminated] Success from decoding/drawing image!\n");
363 return;
364 }
365 SkDebugf("[terminated] Could not decode or draw image.\n");
Kevin Lubick2416f962018-02-12 08:26:39 -0500366}
367
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400368bool FuzzIncrementalImageDecode(sk_sp<SkData> bytes);
369
370static void fuzz_image_decode_incremental(sk_sp<SkData> bytes) {
371 if (FuzzIncrementalImageDecode(bytes)) {
372 SkDebugf("[terminated] Success using incremental decode!\n");
373 return;
374 }
375 SkDebugf("[terminated] Could not incrementally decode and image.\n");
376}
377
378bool FuzzAndroidCodec(sk_sp<SkData> bytes, uint8_t sampleSize);
379
380static void fuzz_android_codec(sk_sp<SkData> bytes) {
381 Fuzz fuzz(bytes);
382 uint8_t sampleSize;
383 fuzz.nextRange(&sampleSize, 1, 64);
384 bytes = SkData::MakeSubset(bytes.get(), 1, bytes->size() - 1);
385 if (FuzzAndroidCodec(bytes, sampleSize)) {
386 SkDebugf("[terminated] Success on Android Codec sampleSize=%u!\n", sampleSize);
387 return;
388 }
389 SkDebugf("[terminated] Could not use Android Codec sampleSize=%u!\n", sampleSize);
390}
391
392// This is a "legacy" fuzzer that likely does too much. It was based off of how
393// DM reads in images. image_decode, image_decode_incremental and android_codec
394// are more targeted fuzzers that do a subset of what this one does.
Kevin Lubickf80f1152017-01-06 08:26:56 -0500395static void fuzz_img(sk_sp<SkData> bytes, uint8_t scale, uint8_t mode) {
kjlubick2a42f482016-02-16 16:14:23 -0800396 // We can scale 1x, 2x, 4x, 8x, 16x
397 scale = scale % 5;
kjlubick5bd98a22016-02-18 06:27:38 -0800398 float fscale = (float)pow(2.0f, scale);
399 SkDebugf("Scaling factor: %f\n", fscale);
kjlubick2a42f482016-02-16 16:14:23 -0800400
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500401 // We have 5 different modes of decoding.
402 mode = mode % 5;
kjlubick2a42f482016-02-16 16:14:23 -0800403 SkDebugf("Mode: %d\n", mode);
404
405 // This is mostly copied from DMSrcSink's CodecSrc::draw method.
kjlubick47d158e2016-02-01 08:23:50 -0800406 SkDebugf("Decoding\n");
Mike Reedede7bac2017-07-23 15:30:02 -0400407 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(bytes));
kjlubickdba57342016-01-21 05:03:28 -0800408 if (nullptr == codec.get()) {
kjlubick47d158e2016-02-01 08:23:50 -0800409 SkDebugf("[terminated] Couldn't create codec.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500410 return;
kjlubickdba57342016-01-21 05:03:28 -0800411 }
412
413 SkImageInfo decodeInfo = codec->getInfo();
kjlubick2a42f482016-02-16 16:14:23 -0800414 SkISize size = codec->getScaledDimensions(fscale);
415 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
416
kjlubickdba57342016-01-21 05:03:28 -0800417 SkBitmap bitmap;
kjlubickdba57342016-01-21 05:03:28 -0800418 SkCodec::Options options;
419 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
420
Mike Reed086a4272017-07-18 10:53:11 -0400421 if (!bitmap.tryAllocPixelsFlags(decodeInfo, SkBitmap::kZeroPixels_AllocFlag)) {
kjlubick47d158e2016-02-01 08:23:50 -0800422 SkDebugf("[terminated] Could not allocate memory. Image might be too large (%d x %d)",
kjlubick2a42f482016-02-16 16:14:23 -0800423 decodeInfo.width(), decodeInfo.height());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500424 return;
kjlubickdba57342016-01-21 05:03:28 -0800425 }
426
kjlubick2a42f482016-02-16 16:14:23 -0800427 switch (mode) {
428 case 0: {//kCodecZeroInit_Mode, kCodec_Mode
Leon Scroggins571b30f2017-07-11 17:35:31 +0000429 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options)) {
kjlubick2a42f482016-02-16 16:14:23 -0800430 case SkCodec::kSuccess:
431 SkDebugf("[terminated] Success!\n");
432 break;
433 case SkCodec::kIncompleteInput:
434 SkDebugf("[terminated] Partial Success\n");
435 break;
Leon Scroggins III674a1842017-07-06 12:26:09 -0400436 case SkCodec::kErrorInInput:
437 SkDebugf("[terminated] Partial Success with error\n");
438 break;
kjlubick2a42f482016-02-16 16:14:23 -0800439 case SkCodec::kInvalidConversion:
440 SkDebugf("Incompatible colortype conversion\n");
441 // Crash to allow afl-fuzz to know this was a bug.
442 raise(SIGSEGV);
443 default:
444 SkDebugf("[terminated] Couldn't getPixels.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500445 return;
kjlubick2a42f482016-02-16 16:14:23 -0800446 }
447 break;
448 }
449 case 1: {//kScanline_Mode
Leon Scroggins571b30f2017-07-11 17:35:31 +0000450 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)) {
451 SkDebugf("[terminated] Could not start scanline decoder\n");
452 return;
453 }
kjlubick2a42f482016-02-16 16:14:23 -0800454
455 void* dst = bitmap.getAddr(0, 0);
456 size_t rowBytes = bitmap.rowBytes();
457 uint32_t height = decodeInfo.height();
Brian Osmanea176c62018-04-06 15:28:23 -0400458 // We do not need to check the return value. On an incomplete
459 // image, memory will be filled with a default value.
460 codec->getScanlines(dst, height, rowBytes);
kjlubick47d158e2016-02-01 08:23:50 -0800461 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800462 break;
kjlubick2a42f482016-02-16 16:14:23 -0800463 }
464 case 2: { //kStripe_Mode
465 const int height = decodeInfo.height();
466 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
467 // does not align with image blocks.
468 const int stripeHeight = 37;
469 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
470
471 // Decode odd stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000472 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)
kjlubick2a42f482016-02-16 16:14:23 -0800473 || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
474 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
475 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
476 // to run this test for image types that do not have this scanline ordering.
477 SkDebugf("[terminated] Could not start top-down scanline decoder\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500478 return;
kjlubick2a42f482016-02-16 16:14:23 -0800479 }
480
481 for (int i = 0; i < numStripes; i += 2) {
482 // Skip a stripe
483 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
484 codec->skipScanlines(linesToSkip);
485
486 // Read a stripe
487 const int startY = (i + 1) * stripeHeight;
488 const int linesToRead = SkTMin(stripeHeight, height - startY);
489 if (linesToRead > 0) {
490 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
491 }
492 }
493
494 // Decode even stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000495 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo);
kjlubick2a42f482016-02-16 16:14:23 -0800496 if (SkCodec::kSuccess != startResult) {
497 SkDebugf("[terminated] Failed to restart scanline decoder with same parameters.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500498 return;
kjlubick2a42f482016-02-16 16:14:23 -0800499 }
500 for (int i = 0; i < numStripes; i += 2) {
501 // Read a stripe
502 const int startY = i * stripeHeight;
503 const int linesToRead = SkTMin(stripeHeight, height - startY);
504 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
505
506 // Skip a stripe
507 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
508 if (linesToSkip > 0) {
509 codec->skipScanlines(linesToSkip);
510 }
511 }
512 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800513 break;
kjlubick2a42f482016-02-16 16:14:23 -0800514 }
515 case 3: { //kSubset_Mode
516 // Arbitrarily choose a divisor.
517 int divisor = 2;
518 // Total width/height of the image.
519 const int W = codec->getInfo().width();
520 const int H = codec->getInfo().height();
521 if (divisor > W || divisor > H) {
522 SkDebugf("[terminated] Cannot codec subset: divisor %d is too big "
523 "with dimensions (%d x %d)\n", divisor, W, H);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500524 return;
kjlubick2a42f482016-02-16 16:14:23 -0800525 }
526 // subset dimensions
527 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
528 const int w = SkAlign2(W / divisor);
529 const int h = SkAlign2(H / divisor);
530 SkIRect subset;
531 SkCodec::Options opts;
532 opts.fSubset = &subset;
533 SkBitmap subsetBm;
534 // We will reuse pixel memory from bitmap.
535 void* pixels = bitmap.getPixels();
536 // Keep track of left and top (for drawing subsetBm into canvas). We could use
537 // fscale * x and fscale * y, but we want integers such that the next subset will start
538 // where the last one ended. So we'll add decodeInfo.width() and height().
539 int left = 0;
540 for (int x = 0; x < W; x += w) {
541 int top = 0;
542 for (int y = 0; y < H; y+= h) {
543 // Do not make the subset go off the edge of the image.
544 const int preScaleW = SkTMin(w, W - x);
545 const int preScaleH = SkTMin(h, H - y);
546 subset.setXYWH(x, y, preScaleW, preScaleH);
547 // And fscale
548 // FIXME: Should we have a version of getScaledDimensions that takes a subset
549 // into account?
550 decodeInfo = decodeInfo.makeWH(
551 SkTMax(1, SkScalarRoundToInt(preScaleW * fscale)),
552 SkTMax(1, SkScalarRoundToInt(preScaleH * fscale)));
553 size_t rowBytes = decodeInfo.minRowBytes();
Leon Scroggins571b30f2017-07-11 17:35:31 +0000554 if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes)) {
kjlubick2a42f482016-02-16 16:14:23 -0800555 SkDebugf("[terminated] Could not install pixels.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500556 return;
kjlubick2a42f482016-02-16 16:14:23 -0800557 }
558 const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000559 &opts);
kjlubick2a42f482016-02-16 16:14:23 -0800560 switch (result) {
561 case SkCodec::kSuccess:
562 case SkCodec::kIncompleteInput:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400563 case SkCodec::kErrorInInput:
kjlubick2a42f482016-02-16 16:14:23 -0800564 SkDebugf("okay\n");
565 break;
566 case SkCodec::kInvalidConversion:
567 if (0 == (x|y)) {
568 // First subset is okay to return unimplemented.
569 SkDebugf("[terminated] Incompatible colortype conversion\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500570 return;
kjlubick2a42f482016-02-16 16:14:23 -0800571 }
572 // If the first subset succeeded, a later one should not fail.
573 // fall through to failure
574 case SkCodec::kUnimplemented:
575 if (0 == (x|y)) {
576 // First subset is okay to return unimplemented.
577 SkDebugf("[terminated] subset codec not supported\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500578 return;
kjlubick2a42f482016-02-16 16:14:23 -0800579 }
580 // If the first subset succeeded, why would a later one fail?
581 // fall through to failure
582 default:
583 SkDebugf("[terminated] subset codec failed to decode (%d, %d, %d, %d) "
584 "with dimensions (%d x %d)\t error %d\n",
585 x, y, decodeInfo.width(), decodeInfo.height(),
586 W, H, result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500587 return;
kjlubick2a42f482016-02-16 16:14:23 -0800588 }
589 // translate by the scaled height.
590 top += decodeInfo.height();
591 }
592 // translate by the scaled width.
593 left += decodeInfo.width();
594 }
595 SkDebugf("[terminated] Success!\n");
596 break;
597 }
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500598 case 4: { //kAnimated_Mode
599 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
600 if (frameInfos.size() == 0) {
601 SkDebugf("[terminated] Not an animated image\n");
602 break;
603 }
604
605 for (size_t i = 0; i < frameInfos.size(); i++) {
606 options.fFrameIndex = i;
607 auto result = codec->startIncrementalDecode(decodeInfo, bitmap.getPixels(),
608 bitmap.rowBytes(), &options);
609 if (SkCodec::kSuccess != result) {
610 SkDebugf("[terminated] failed to start incremental decode "
611 "in frame %d with error %d\n", i, result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500612 return;
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500613 }
614
615 result = codec->incrementalDecode();
Leon Scroggins III674a1842017-07-06 12:26:09 -0400616 if (result == SkCodec::kIncompleteInput || result == SkCodec::kErrorInInput) {
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500617 SkDebugf("okay\n");
618 // Frames beyond this one will not decode.
619 break;
620 }
621 if (result == SkCodec::kSuccess) {
622 SkDebugf("okay - decoded frame %d\n", i);
623 } else {
624 SkDebugf("[terminated] incremental decode failed with "
625 "error %d\n", result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500626 return;
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500627 }
628 }
629 SkDebugf("[terminated] Success!\n");
630 break;
631 }
kjlubickdba57342016-01-21 05:03:28 -0800632 default:
kjlubick2a42f482016-02-16 16:14:23 -0800633 SkDebugf("[terminated] Mode not implemented yet\n");
kjlubickdba57342016-01-21 05:03:28 -0800634 }
635
636 dump_png(bitmap);
mtklein65e58242016-01-13 12:57:57 -0800637}
638
Kevin Lubickf80f1152017-01-06 08:26:56 -0500639static void fuzz_skp(sk_sp<SkData> bytes) {
Kevin Lubick09757b22017-12-12 12:52:39 -0500640 SkReadBuffer buf(bytes->data(), bytes->size());
kjlubickdba57342016-01-21 05:03:28 -0800641 SkDebugf("Decoding\n");
Cary Clarkefd99cc2018-06-11 16:25:43 -0400642 sk_sp<SkPicture> pic(SkPicturePriv::MakeFromBuffer(buf));
kjlubickdba57342016-01-21 05:03:28 -0800643 if (!pic) {
kjlubick47d158e2016-02-01 08:23:50 -0800644 SkDebugf("[terminated] Couldn't decode as a picture.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500645 return;
kjlubickdba57342016-01-21 05:03:28 -0800646 }
647 SkDebugf("Rendering\n");
648 SkBitmap bitmap;
649 if (!FLAGS_dump.isEmpty()) {
650 SkIRect size = pic->cullRect().roundOut();
651 bitmap.allocN32Pixels(size.width(), size.height());
652 }
653 SkCanvas canvas(bitmap);
654 canvas.drawPicture(pic);
kjlubick47d158e2016-02-01 08:23:50 -0800655 SkDebugf("[terminated] Success! Decoded and rendered an SkPicture!\n");
kjlubickdba57342016-01-21 05:03:28 -0800656 dump_png(bitmap);
kjlubickdba57342016-01-21 05:03:28 -0800657}
mtklein65e58242016-01-13 12:57:57 -0800658
Kevin Lubickf80f1152017-01-06 08:26:56 -0500659static void fuzz_color_deserialize(sk_sp<SkData> bytes) {
kjlubick3e3c1a52016-06-23 10:49:27 -0700660 sk_sp<SkColorSpace> space(SkColorSpace::Deserialize(bytes->data(), bytes->size()));
661 if (!space) {
662 SkDebugf("[terminated] Couldn't deserialize Colorspace.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500663 return;
kjlubick3e3c1a52016-06-23 10:49:27 -0700664 }
665 SkDebugf("[terminated] Success! deserialized Colorspace.\n");
kjlubick3e3c1a52016-06-23 10:49:27 -0700666}
667
Kevin Lubickf034d112018-02-08 14:31:24 -0500668void FuzzPathDeserialize(SkReadBuffer& buf);
Kevin Lubickc9d28292017-11-09 08:12:56 -0500669
Kevin Lubickf034d112018-02-08 14:31:24 -0500670static void fuzz_path_deserialize(sk_sp<SkData> bytes) {
671 SkReadBuffer buf(bytes->data(), bytes->size());
672 FuzzPathDeserialize(buf);
673 SkDebugf("[terminated] path_deserialize didn't crash!\n");
Kevin Lubickf04c50a2017-01-06 13:48:19 -0500674}
675
Kevin Lubick2541edf2018-01-11 10:27:14 -0500676bool FuzzRegionDeserialize(sk_sp<SkData> bytes);
677
Kevin Lubickedee1ae2017-02-20 17:47:18 -0500678static void fuzz_region_deserialize(sk_sp<SkData> bytes) {
Kevin Lubick2541edf2018-01-11 10:27:14 -0500679 if (!FuzzRegionDeserialize(bytes)) {
Kevin Lubickedee1ae2017-02-20 17:47:18 -0500680 SkDebugf("[terminated] Couldn't initialize SkRegion.\n");
681 return;
682 }
Kevin Lubickedee1ae2017-02-20 17:47:18 -0500683 SkDebugf("[terminated] Success! Initialized SkRegion.\n");
684}
685
Kevin Lubickf034d112018-02-08 14:31:24 -0500686void FuzzTextBlobDeserialize(SkReadBuffer& buf);
687
Kevin Lubickd46e85f2017-11-21 17:13:35 -0500688static void fuzz_textblob_deserialize(sk_sp<SkData> bytes) {
Mike Reedfadbfcd2017-12-06 16:09:20 -0500689 SkReadBuffer buf(bytes->data(), bytes->size());
Kevin Lubickf034d112018-02-08 14:31:24 -0500690 FuzzTextBlobDeserialize(buf);
691 SkDebugf("[terminated] textblob didn't crash!\n");
Kevin Lubickd46e85f2017-11-21 17:13:35 -0500692}
693
Kevin Lubick2541edf2018-01-11 10:27:14 -0500694void FuzzRegionSetPath(Fuzz* fuzz);
695
696static void fuzz_region_set_path(sk_sp<SkData> bytes) {
697 Fuzz fuzz(bytes);
698 FuzzRegionSetPath(&fuzz);
699 SkDebugf("[terminated] region_set_path didn't crash!\n");
700}
701
Kevin Lubickf034d112018-02-08 14:31:24 -0500702void FuzzImageFilterDeserialize(sk_sp<SkData> bytes);
703
Herb Derbya839fc02017-03-16 12:30:43 -0400704static void fuzz_filter_fuzz(sk_sp<SkData> bytes) {
Kevin Lubickf034d112018-02-08 14:31:24 -0500705 FuzzImageFilterDeserialize(bytes);
706 SkDebugf("[terminated] filter_fuzz didn't crash!\n");
Herb Derbya839fc02017-03-16 12:30:43 -0400707}
708
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500709#if SK_SUPPORT_GPU
Kevin Lubickf80f1152017-01-06 08:26:56 -0500710static void fuzz_sksl2glsl(sk_sp<SkData> bytes) {
kjlubicke7195772016-10-18 10:06:24 -0700711 SkSL::Compiler compiler;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400712 SkSL::String output;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500713 SkSL::Program::Settings settings;
714 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
715 settings.fCaps = caps.get();
716 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
Brian Osman93ba0a42017-08-14 14:48:10 -0400717 SkSL::String((const char*) bytes->data()),
718 settings);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500719 if (!program || !compiler.toGLSL(*program, &output)) {
kjlubicke7195772016-10-18 10:06:24 -0700720 SkDebugf("[terminated] Couldn't compile input.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500721 return;
kjlubicke7195772016-10-18 10:06:24 -0700722 }
723 SkDebugf("[terminated] Success! Compiled input.\n");
kjlubicke7195772016-10-18 10:06:24 -0700724}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500725#endif