blob: 6f6a535ebaa6cdb711865190dbd726b1adc60e77 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "fuzz/Fuzz.h"
9#include "include/codec/SkCodec.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkData.h"
12#include "include/core/SkImage.h"
13#include "include/core/SkImageEncoder.h"
14#include "include/core/SkMallocPixelRef.h"
15#include "include/core/SkPaint.h"
16#include "include/core/SkPath.h"
17#include "include/core/SkStream.h"
18#include "include/core/SkSurface.h"
19#include "include/core/SkTextBlob.h"
Kevin Lubickd7255a72019-06-03 11:59:23 -040020#include "src/core/SkFontMgrPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/core/SkOSFile.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/core/SkReadBuffer.h"
23#include "src/utils/SkOSPath.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "tools/ToolUtils.h"
Kevin Lubickd7255a72019-06-03 11:59:23 -040025#include "tools/flags/CommandLineFlags.h"
26#include "tools/fonts/TestFontMgr.h"
Hal Canary8a001442018-09-19 11:31:27 -040027
Hal Canary62176db2017-02-27 16:42:03 -050028#include <iostream>
Kevin Lubickacd456a2018-04-24 13:58:16 -040029#include <map>
Kevin Lubickfffa6412018-04-23 16:44:55 -040030#include <regex>
mtkleina1159422016-01-15 05:46:54 -080031#include <signal.h>
Kevin Lubick2541edf2018-01-11 10:27:14 -050032
Mike Klein84836b72019-03-21 11:31:36 -050033static DEFINE_string2(bytes, b, "", "A path to a file or a directory. If a file, the "
34 "contents will be used as the fuzz bytes. If a directory, all files "
35 "in the directory will be used as fuzz bytes for the fuzzer, one at a "
36 "time.");
37static DEFINE_string2(name, n, "", "If --type is 'api', fuzz the API with this name.");
38static DEFINE_string2(dump, d, "", "If not empty, dump 'image*' or 'skp' types as a "
39 "PNG with this name.");
Brian Osmanf4e57372020-07-17 11:37:14 -040040static DEFINE_int(loops, 1, "Run the fuzzer on each input this many times.");
Kevin Lubick9ff5dc92018-01-09 12:47:33 -050041DEFINE_bool2(verbose, v, false, "Print more information while fuzzing.");
Florin Malita7796f002018-06-08 12:25:38 -040042
43// This cannot be inlined in DEFINE_string2 due to interleaved ifdefs
44static constexpr char g_type_message[] = "How to interpret --bytes, one of:\n"
Kevin Lubick0f3d2a62018-10-17 10:24:44 -040045 "android_codec\n"
Florin Malita7796f002018-06-08 12:25:38 -040046 "animated_image_decode\n"
47 "api\n"
48 "color_deserialize\n"
49 "filter_fuzz (equivalent to Chrome's filter_fuzz_stub)\n"
50 "image_decode\n"
Kevin Lubick0f3d2a62018-10-17 10:24:44 -040051 "image_decode_incremental\n"
Florin Malita7796f002018-06-08 12:25:38 -040052 "image_mode\n"
53 "image_scale\n"
Florin Malita80452be2018-06-19 11:27:20 -040054 "json\n"
Florin Malita7796f002018-06-08 12:25:38 -040055 "path_deserialize\n"
Florin Malita7796f002018-06-08 12:25:38 -040056 "region_deserialize\n"
57 "region_set_path\n"
Kevin Lubick2be14d32019-10-21 13:44:48 -040058 "skdescriptor_deserialize\n"
Florin Malita7796f002018-06-08 12:25:38 -040059 "skp\n"
Zepeng Hua5783f32020-07-10 13:36:20 +000060 "skruntimeeffect\n"
Florin Malita7796f002018-06-08 12:25:38 -040061 "sksl2glsl\n"
Zepeng Huedaf3022020-06-12 12:20:59 +000062 "svg_dom\n"
Kevin Lubicke9c1ce82019-03-11 11:09:40 -040063 "sksl2metal\n"
Kevin Lubick0f0a7102019-03-18 16:20:55 -040064 "sksl2pipeline\n"
Kevin Lubicke9c1ce82019-03-11 11:09:40 -040065 "sksl2spirv\n"
Florin Malita7796f002018-06-08 12:25:38 -040066#if defined(SK_ENABLE_SKOTTIE)
67 "skottie_json\n"
68#endif
69 "textblob";
70
Mike Klein84836b72019-03-21 11:31:36 -050071static DEFINE_string2(type, t, "", g_type_message);
kjlubickdba57342016-01-21 05:03:28 -080072
Kevin Lubickfffa6412018-04-23 16:44:55 -040073static int fuzz_file(SkString path, SkString type);
kjlubick2a42f482016-02-16 16:14:23 -080074static uint8_t calculate_option(SkData*);
Kevin Lubickfffa6412018-04-23 16:44:55 -040075static SkString try_auto_detect(SkString path, SkString* name);
kjlubickdba57342016-01-21 05:03:28 -080076
Kevin Lubick0f3d2a62018-10-17 10:24:44 -040077static void fuzz_android_codec(sk_sp<SkData>);
78static void fuzz_animated_img(sk_sp<SkData>);
Kevin Lubickfffa6412018-04-23 16:44:55 -040079static void fuzz_api(sk_sp<SkData> bytes, SkString name);
Kevin Lubickf80f1152017-01-06 08:26:56 -050080static void fuzz_color_deserialize(sk_sp<SkData>);
Kevin Lubickd46e85f2017-11-21 17:13:35 -050081static void fuzz_filter_fuzz(sk_sp<SkData>);
Kevin Lubick0f3d2a62018-10-17 10:24:44 -040082static void fuzz_image_decode(sk_sp<SkData>);
83static void fuzz_image_decode_incremental(sk_sp<SkData>);
Kevin Lubick0168e042017-02-14 13:12:37 -050084static void fuzz_img(sk_sp<SkData>, uint8_t, uint8_t);
Florin Malita80452be2018-06-19 11:27:20 -040085static void fuzz_json(sk_sp<SkData>);
Kevin Lubickf04c50a2017-01-06 13:48:19 -050086static void fuzz_path_deserialize(sk_sp<SkData>);
Kevin Lubickedee1ae2017-02-20 17:47:18 -050087static void fuzz_region_deserialize(sk_sp<SkData>);
Kevin Lubick2541edf2018-01-11 10:27:14 -050088static void fuzz_region_set_path(sk_sp<SkData>);
Kevin Lubick2be14d32019-10-21 13:44:48 -040089static void fuzz_skdescriptor_deserialize(sk_sp<SkData>);
Kevin Lubick0168e042017-02-14 13:12:37 -050090static void fuzz_skp(sk_sp<SkData>);
Zepeng Hua5783f32020-07-10 13:36:20 +000091static void fuzz_skruntimeeffect(sk_sp<SkData>);
Kevin Lubicke9c1ce82019-03-11 11:09:40 -040092static void fuzz_sksl2glsl(sk_sp<SkData>);
93static void fuzz_sksl2metal(sk_sp<SkData>);
Kevin Lubick0f0a7102019-03-18 16:20:55 -040094static void fuzz_sksl2pipeline(sk_sp<SkData>);
Kevin Lubick2be14d32019-10-21 13:44:48 -040095static void fuzz_sksl2spirv(sk_sp<SkData>);
Zepeng Huedaf3022020-06-12 12:20:59 +000096static void fuzz_svg_dom(sk_sp<SkData>);
Kevin Lubickd46e85f2017-11-21 17:13:35 -050097static void fuzz_textblob_deserialize(sk_sp<SkData>);
Herb Derbya839fc02017-03-16 12:30:43 -040098
Kevin Lubickfffa6412018-04-23 16:44:55 -040099static void print_api_names();
100
Florin Malita0b0d93d2018-05-04 15:01:03 -0400101#if defined(SK_ENABLE_SKOTTIE)
102static void fuzz_skottie_json(sk_sp<SkData>);
103#endif
104
mtklein65e58242016-01-13 12:57:57 -0800105int main(int argc, char** argv) {
Mike Klein88544fb2019-03-20 10:50:33 -0500106 CommandLineFlags::SetUsage(
107 "Usage: fuzz -t <type> -b <path/to/file> [-n api-to-fuzz]\n"
108 " fuzz -b <path/to/file>\n"
109 "--help lists the valid types. If type is not specified,\n"
110 "fuzz will make a guess based on the name of the file.\n");
111 CommandLineFlags::Parse(argc, argv);
Kevin Lubickd7255a72019-06-03 11:59:23 -0400112 gSkFontMgr_DefaultFactory = &ToolUtils::MakePortableFontMgr;
mtkleinf5e97822016-01-15 06:19:53 -0800113
Kevin Lubickfffa6412018-04-23 16:44:55 -0400114 SkString path = SkString(FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0]);
115 SkString type = SkString(FLAGS_type.isEmpty() ? "" : FLAGS_type[0]);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500116
Brian Osmanf4e57372020-07-17 11:37:14 -0400117 int loopCount = std::max(FLAGS_loops, 1);
118
Kevin Lubickfffa6412018-04-23 16:44:55 -0400119 if (!sk_isdir(path.c_str())) {
Brian Osmanf4e57372020-07-17 11:37:14 -0400120 for (int i = 0; i < loopCount; ++i) {
121 int rv = fuzz_file(path, type);
122 if (rv != 0) {
123 return rv;
124 }
125 }
126 return 0;
Kevin Lubickf80f1152017-01-06 08:26:56 -0500127 }
128
Kevin Lubickfffa6412018-04-23 16:44:55 -0400129 SkOSFile::Iter it(path.c_str());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500130 for (SkString file; it.next(&file); ) {
Kevin Lubickfffa6412018-04-23 16:44:55 -0400131 SkString p = SkOSPath::Join(path.c_str(), file.c_str());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500132 SkDebugf("Fuzzing %s\n", p.c_str());
Brian Osmanf4e57372020-07-17 11:37:14 -0400133 for (int i = 0; i < loopCount; ++i) {
134 int rv = fuzz_file(p, type);
135 if (rv != 0) {
136 return rv;
137 }
Kevin Lubickf80f1152017-01-06 08:26:56 -0500138 }
139 }
140 return 0;
141}
142
Kevin Lubickfffa6412018-04-23 16:44:55 -0400143static int fuzz_file(SkString path, SkString type) {
144 sk_sp<SkData> bytes(SkData::MakeFromFileName(path.c_str()));
kjlubickdba57342016-01-21 05:03:28 -0800145 if (!bytes) {
Kevin Lubickfffa6412018-04-23 16:44:55 -0400146 SkDebugf("Could not read %s\n", path.c_str());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500147 return 1;
kjlubickdba57342016-01-21 05:03:28 -0800148 }
mtklein65e58242016-01-13 12:57:57 -0800149
Kevin Lubickfffa6412018-04-23 16:44:55 -0400150 SkString name = SkString(FLAGS_name.isEmpty() ? "" : FLAGS_name[0]);
151
152 if (type.isEmpty()) {
153 type = try_auto_detect(path, &name);
kjlubickdba57342016-01-21 05:03:28 -0800154 }
Kevin Lubickfffa6412018-04-23 16:44:55 -0400155
156 if (type.isEmpty()) {
157 SkDebugf("Could not autodetect type of %s\n", path.c_str());
158 return 1;
159 }
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400160 if (type.equals("android_codec")) {
161 fuzz_android_codec(bytes);
162 return 0;
163 }
Kevin Lubickfffa6412018-04-23 16:44:55 -0400164 if (type.equals("animated_image_decode")) {
165 fuzz_animated_img(bytes);
166 return 0;
167 }
168 if (type.equals("api")) {
169 fuzz_api(bytes, name);
170 return 0;
171 }
172 if (type.equals("color_deserialize")) {
173 fuzz_color_deserialize(bytes);
174 return 0;
175 }
Florin Malita80452be2018-06-19 11:27:20 -0400176 if (type.equals("filter_fuzz")) {
177 fuzz_filter_fuzz(bytes);
178 return 0;
179 }
Kevin Lubickfffa6412018-04-23 16:44:55 -0400180 if (type.equals("image_decode")) {
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400181 fuzz_image_decode(bytes);
182 return 0;
183 }
184 if (type.equals("image_decode_incremental")) {
185 fuzz_image_decode_incremental(bytes);
Kevin Lubickfffa6412018-04-23 16:44:55 -0400186 return 0;
187 }
188 if (type.equals("image_scale")) {
189 uint8_t option = calculate_option(bytes.get());
190 fuzz_img(bytes, option, 0);
191 return 0;
192 }
193 if (type.equals("image_mode")) {
194 uint8_t option = calculate_option(bytes.get());
195 fuzz_img(bytes, 0, option);
196 return 0;
197 }
Florin Malita80452be2018-06-19 11:27:20 -0400198 if (type.equals("json")) {
199 fuzz_json(bytes);
Kevin Lubick9eeede22018-05-03 16:26:10 -0400200 return 0;
201 }
Kevin Lubickfffa6412018-04-23 16:44:55 -0400202 if (type.equals("path_deserialize")) {
203 fuzz_path_deserialize(bytes);
204 return 0;
205 }
206 if (type.equals("region_deserialize")) {
207 fuzz_region_deserialize(bytes);
208 return 0;
209 }
210 if (type.equals("region_set_path")) {
211 fuzz_region_set_path(bytes);
212 return 0;
213 }
214 if (type.equals("pipe")) {
Mike Klein60900b52018-09-21 11:19:45 -0400215 SkDebugf("I would prefer not to.\n");
Kevin Lubickfffa6412018-04-23 16:44:55 -0400216 return 0;
217 }
Kevin Lubick2be14d32019-10-21 13:44:48 -0400218 if (type.equals("skdescriptor_deserialize")) {
219 fuzz_skdescriptor_deserialize(bytes);
220 return 0;
221 }
Florin Malita0b0d93d2018-05-04 15:01:03 -0400222#if defined(SK_ENABLE_SKOTTIE)
Kevin Lubick9eeede22018-05-03 16:26:10 -0400223 if (type.equals("skottie_json")) {
224 fuzz_skottie_json(bytes);
Kevin Lubickfffa6412018-04-23 16:44:55 -0400225 return 0;
226 }
Florin Malita0b0d93d2018-05-04 15:01:03 -0400227#endif
Kevin Lubick9eeede22018-05-03 16:26:10 -0400228 if (type.equals("skp")) {
229 fuzz_skp(bytes);
Kevin Lubickfffa6412018-04-23 16:44:55 -0400230 return 0;
231 }
Zepeng Hua5783f32020-07-10 13:36:20 +0000232 if (type.equals("skruntimeeffect")) {
233 fuzz_skruntimeeffect(bytes);
234 return 0;
235 }
Kevin Lubickfffa6412018-04-23 16:44:55 -0400236 if (type.equals("sksl2glsl")) {
237 fuzz_sksl2glsl(bytes);
238 return 0;
239 }
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400240 if (type.equals("sksl2metal")) {
241 fuzz_sksl2metal(bytes);
242 return 0;
243 }
244 if (type.equals("sksl2spirv")) {
245 fuzz_sksl2spirv(bytes);
246 return 0;
247 }
Kevin Lubick0f0a7102019-03-18 16:20:55 -0400248 if (type.equals("sksl2pipeline")) {
249 fuzz_sksl2pipeline(bytes);
250 return 0;
251 }
Zepeng Huedaf3022020-06-12 12:20:59 +0000252 if (type.equals("svg_dom")) {
253 fuzz_svg_dom(bytes);
254 return 0;
255 }
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400256 if (type.equals("textblob")) {
257 fuzz_textblob_deserialize(bytes);
258 return 0;
259 }
Kevin Lubick457fa972018-05-29 09:22:06 -0400260 SkDebugf("Unknown type %s\n", type.c_str());
Mike Klein88544fb2019-03-20 10:50:33 -0500261 CommandLineFlags::PrintUsage();
Kevin Lubick9ff5dc92018-01-09 12:47:33 -0500262 return 1;
kjlubickdba57342016-01-21 05:03:28 -0800263}
264
Kevin Lubickfffa6412018-04-23 16:44:55 -0400265static std::map<std::string, std::string> cf_api_map = {
Zepeng Hu94007012020-07-22 14:37:46 +0000266 {"api_create_ddl", "CreateDDL"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400267 {"api_draw_functions", "DrawFunctions"},
Adlai Hollerccb68662021-02-25 10:28:44 -0500268 {"api_ddl_threading", "DDLThreadingGL"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400269 {"api_gradients", "Gradients"},
270 {"api_image_filter", "ImageFilter"},
271 {"api_mock_gpu_canvas", "MockGPUCanvas"},
272 {"api_null_canvas", "NullCanvas"},
273 {"api_path_measure", "PathMeasure"},
Kevin Lubickde54d7c2018-09-24 08:48:19 -0400274 {"api_pathop", "Pathop"},
Kevin Lubick57507f12018-10-11 09:35:15 -0400275 {"api_polyutils", "PolyUtils"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400276 {"api_raster_n32_canvas", "RasterN32Canvas"},
Weston Tracey1a771fe2021-02-04 11:09:59 -0500277 {"api_skparagraph", "SkParagraph"},
Zepeng Huba7cbf72020-07-01 13:21:03 +0000278 {"api_svg_canvas", "SVGCanvas"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400279 {"jpeg_encoder", "JPEGEncoder"},
280 {"png_encoder", "PNGEncoder"},
Kevin Lubickf84ded22018-10-23 09:28:48 -0400281 {"skia_pathop_fuzzer", "LegacyChromiumPathop"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400282 {"webp_encoder", "WEBPEncoder"}
283};
284
Kevin Lubick6c560552018-06-20 09:29:23 -0400285// maps clusterfuzz/oss-fuzz -> Skia's name
Kevin Lubickfffa6412018-04-23 16:44:55 -0400286static std::map<std::string, std::string> cf_map = {
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400287 {"android_codec", "android_codec"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400288 {"animated_image_decode", "animated_image_decode"},
289 {"image_decode", "image_decode"},
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400290 {"image_decode_incremental", "image_decode_incremental"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400291 {"image_filter_deserialize", "filter_fuzz"},
292 {"image_filter_deserialize_width", "filter_fuzz"},
293 {"path_deserialize", "path_deserialize"},
294 {"region_deserialize", "region_deserialize"},
295 {"region_set_path", "region_set_path"},
Kevin Lubick2be14d32019-10-21 13:44:48 -0400296 {"skdescriptor_deserialize", "skdescriptor_deserialize"},
Kevin Lubick6c560552018-06-20 09:29:23 -0400297 {"skjson", "json"},
Zepeng Hufcb7ba02020-07-31 17:21:29 +0000298 {"skp", "skp"},
Zepeng Hua5783f32020-07-10 13:36:20 +0000299 {"skruntimeeffect", "skruntimeeffect"},
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400300 {"sksl2glsl", "sksl2glsl"},
301 {"sksl2metal", "sksl2metal"},
302 {"sksl2spirv", "sksl2spirv"},
Kevin Lubick0f0a7102019-03-18 16:20:55 -0400303 {"sksl2pipeline", "sksl2pipeline"},
Kevin Lubickde54d7c2018-09-24 08:48:19 -0400304#if defined(SK_ENABLE_SKOTTIE)
305 {"skottie_json", "skottie_json"},
306#endif
Zepeng Huedaf3022020-06-12 12:20:59 +0000307 {"svg_dom", "svg_dom"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400308 {"textblob_deserialize", "textblob"}
309};
310
311static SkString try_auto_detect(SkString path, SkString* name) {
312 std::cmatch m;
313 std::regex clusterfuzz("clusterfuzz-testcase(-minimized)?-([a-z0-9_]+)-[\\d]+");
314 std::regex skiafuzzer("(api-)?(\\w+)-[a-f0-9]+");
315
316 if (std::regex_search(path.c_str(), m, clusterfuzz)) {
317 std::string type = m.str(2);
Kevin Lubick6c560552018-06-20 09:29:23 -0400318
319 if (cf_api_map.find(type) != cf_api_map.end()) {
320 *name = SkString(cf_api_map[type].c_str());
321 return SkString("api");
Kevin Lubickfffa6412018-04-23 16:44:55 -0400322 } else {
323 if (cf_map.find(type) != cf_map.end()) {
324 return SkString(cf_map[type].c_str());
325 }
326 }
327 } else if (std::regex_search(path.c_str(), m, skiafuzzer)) {
328 std::string a1 = m.str(1);
329 std::string typeOrName = m.str(2);
330 if (a1.length() > 0) {
331 // it's an api fuzzer
332 *name = SkString(typeOrName.c_str());
333 return SkString("api");
334 } else {
335 return SkString(typeOrName.c_str());
336 }
337 }
338
339 return SkString("");
340}
341
Florin Malita80452be2018-06-19 11:27:20 -0400342void FuzzJSON(sk_sp<SkData> bytes);
Florin Malita7796f002018-06-08 12:25:38 -0400343
Florin Malita80452be2018-06-19 11:27:20 -0400344static void fuzz_json(sk_sp<SkData> bytes){
345 FuzzJSON(bytes);
Florin Malita7796f002018-06-08 12:25:38 -0400346 SkDebugf("[terminated] Done parsing!\n");
347}
Florin Malita7796f002018-06-08 12:25:38 -0400348
Florin Malita0b0d93d2018-05-04 15:01:03 -0400349#if defined(SK_ENABLE_SKOTTIE)
Kevin Lubick9eeede22018-05-03 16:26:10 -0400350void FuzzSkottieJSON(sk_sp<SkData> bytes);
351
352static void fuzz_skottie_json(sk_sp<SkData> bytes){
353 FuzzSkottieJSON(bytes);
354 SkDebugf("[terminated] Done animating!\n");
355}
Florin Malita0b0d93d2018-05-04 15:01:03 -0400356#endif
Kevin Lubick9eeede22018-05-03 16:26:10 -0400357
Zepeng Huedaf3022020-06-12 12:20:59 +0000358void FuzzSVG(sk_sp<SkData> bytes);
359static void fuzz_svg_dom(sk_sp<SkData> bytes){
360 FuzzSVG(bytes);
361 SkDebugf("[terminated] Done DOM!\n");
362}
363
kjlubick2a42f482016-02-16 16:14:23 -0800364// This adds up the first 1024 bytes and returns it as an 8 bit integer. This allows afl-fuzz to
365// deterministically excercise different paths, or *options* (such as different scaling sizes or
366// different image modes) without needing to introduce a parameter. This way we don't need a
367// image_scale1, image_scale2, image_scale4, etc fuzzer, we can just have a image_scale fuzzer.
368// Clients are expected to transform this number into a different range, e.g. with modulo (%).
369static uint8_t calculate_option(SkData* bytes) {
370 uint8_t total = 0;
371 const uint8_t* data = bytes->bytes();
372 for (size_t i = 0; i < 1024 && i < bytes->size(); i++) {
373 total += data[i];
374 }
375 return total;
376}
377
Kevin Lubickfffa6412018-04-23 16:44:55 -0400378static void print_api_names(){
379 SkDebugf("When using --type api, please choose an API to fuzz with --name/-n:\n");
Hal Canary972eba32018-07-30 17:07:07 -0400380 for (const Fuzzable& fuzzable : sk_tools::Registry<Fuzzable>::Range()) {
Kevin Lubickfffa6412018-04-23 16:44:55 -0400381 SkDebugf("\t%s\n", fuzzable.name);
382 }
383}
384
385static void fuzz_api(sk_sp<SkData> bytes, SkString name) {
Hal Canary972eba32018-07-30 17:07:07 -0400386 for (const Fuzzable& fuzzable : sk_tools::Registry<Fuzzable>::Range()) {
Kevin Lubickfffa6412018-04-23 16:44:55 -0400387 if (name.equals(fuzzable.name)) {
mtkleinf5e97822016-01-15 06:19:53 -0800388 SkDebugf("Fuzzing %s...\n", fuzzable.name);
Kevin Lubick1ac8fd22017-03-01 10:42:45 -0500389 Fuzz fuzz(std::move(bytes));
mtklein65e58242016-01-13 12:57:57 -0800390 fuzzable.fn(&fuzz);
kjlubick47d158e2016-02-01 08:23:50 -0800391 SkDebugf("[terminated] Success!\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500392 return;
mtklein65e58242016-01-13 12:57:57 -0800393 }
394 }
mtkleind4387ea2016-01-21 06:13:52 -0800395
Kevin Lubickfffa6412018-04-23 16:44:55 -0400396 print_api_names();
kjlubickdba57342016-01-21 05:03:28 -0800397}
398
399static void dump_png(SkBitmap bitmap) {
400 if (!FLAGS_dump.isEmpty()) {
Mike Kleinea3f0142019-03-20 11:12:10 -0500401 ToolUtils::EncodeImageToFile(FLAGS_dump[0], bitmap, SkEncodedImageFormat::kPNG, 100);
kjlubickdba57342016-01-21 05:03:28 -0800402 SkDebugf("Dumped to %s\n", FLAGS_dump[0]);
403 }
404}
405
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400406bool FuzzAnimatedImage(sk_sp<SkData> bytes);
Kevin Lubick2416f962018-02-12 08:26:39 -0500407
408static void fuzz_animated_img(sk_sp<SkData> bytes) {
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400409 if (FuzzAnimatedImage(bytes)) {
410 SkDebugf("[terminated] Success from decoding/drawing animated image!\n");
411 return;
412 }
413 SkDebugf("[terminated] Could not decode or draw animated image.\n");
Kevin Lubick2416f962018-02-12 08:26:39 -0500414}
415
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400416bool FuzzImageDecode(sk_sp<SkData> bytes);
Kevin Lubick2416f962018-02-12 08:26:39 -0500417
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400418static void fuzz_image_decode(sk_sp<SkData> bytes) {
419 if (FuzzImageDecode(bytes)) {
420 SkDebugf("[terminated] Success from decoding/drawing image!\n");
421 return;
422 }
423 SkDebugf("[terminated] Could not decode or draw image.\n");
Kevin Lubick2416f962018-02-12 08:26:39 -0500424}
425
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400426bool FuzzIncrementalImageDecode(sk_sp<SkData> bytes);
427
428static void fuzz_image_decode_incremental(sk_sp<SkData> bytes) {
429 if (FuzzIncrementalImageDecode(bytes)) {
430 SkDebugf("[terminated] Success using incremental decode!\n");
431 return;
432 }
433 SkDebugf("[terminated] Could not incrementally decode and image.\n");
434}
435
436bool FuzzAndroidCodec(sk_sp<SkData> bytes, uint8_t sampleSize);
437
438static void fuzz_android_codec(sk_sp<SkData> bytes) {
439 Fuzz fuzz(bytes);
440 uint8_t sampleSize;
441 fuzz.nextRange(&sampleSize, 1, 64);
442 bytes = SkData::MakeSubset(bytes.get(), 1, bytes->size() - 1);
443 if (FuzzAndroidCodec(bytes, sampleSize)) {
444 SkDebugf("[terminated] Success on Android Codec sampleSize=%u!\n", sampleSize);
445 return;
446 }
447 SkDebugf("[terminated] Could not use Android Codec sampleSize=%u!\n", sampleSize);
448}
449
450// This is a "legacy" fuzzer that likely does too much. It was based off of how
451// DM reads in images. image_decode, image_decode_incremental and android_codec
452// are more targeted fuzzers that do a subset of what this one does.
Kevin Lubickf80f1152017-01-06 08:26:56 -0500453static void fuzz_img(sk_sp<SkData> bytes, uint8_t scale, uint8_t mode) {
kjlubick2a42f482016-02-16 16:14:23 -0800454 // We can scale 1x, 2x, 4x, 8x, 16x
455 scale = scale % 5;
kjlubick5bd98a22016-02-18 06:27:38 -0800456 float fscale = (float)pow(2.0f, scale);
457 SkDebugf("Scaling factor: %f\n", fscale);
kjlubick2a42f482016-02-16 16:14:23 -0800458
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500459 // We have 5 different modes of decoding.
460 mode = mode % 5;
kjlubick2a42f482016-02-16 16:14:23 -0800461 SkDebugf("Mode: %d\n", mode);
462
463 // This is mostly copied from DMSrcSink's CodecSrc::draw method.
kjlubick47d158e2016-02-01 08:23:50 -0800464 SkDebugf("Decoding\n");
Mike Reedede7bac2017-07-23 15:30:02 -0400465 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(bytes));
John Stilesa008b0f2020-08-16 08:48:02 -0400466 if (nullptr == codec) {
kjlubick47d158e2016-02-01 08:23:50 -0800467 SkDebugf("[terminated] Couldn't create codec.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500468 return;
kjlubickdba57342016-01-21 05:03:28 -0800469 }
470
471 SkImageInfo decodeInfo = codec->getInfo();
kjlubick2a42f482016-02-16 16:14:23 -0800472 SkISize size = codec->getScaledDimensions(fscale);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400473 decodeInfo = decodeInfo.makeDimensions(size);
kjlubick2a42f482016-02-16 16:14:23 -0800474
kjlubickdba57342016-01-21 05:03:28 -0800475 SkBitmap bitmap;
kjlubickdba57342016-01-21 05:03:28 -0800476 SkCodec::Options options;
477 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
478
Mike Reed086a4272017-07-18 10:53:11 -0400479 if (!bitmap.tryAllocPixelsFlags(decodeInfo, SkBitmap::kZeroPixels_AllocFlag)) {
kjlubick47d158e2016-02-01 08:23:50 -0800480 SkDebugf("[terminated] Could not allocate memory. Image might be too large (%d x %d)",
kjlubick2a42f482016-02-16 16:14:23 -0800481 decodeInfo.width(), decodeInfo.height());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500482 return;
kjlubickdba57342016-01-21 05:03:28 -0800483 }
484
kjlubick2a42f482016-02-16 16:14:23 -0800485 switch (mode) {
486 case 0: {//kCodecZeroInit_Mode, kCodec_Mode
Leon Scroggins571b30f2017-07-11 17:35:31 +0000487 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options)) {
kjlubick2a42f482016-02-16 16:14:23 -0800488 case SkCodec::kSuccess:
489 SkDebugf("[terminated] Success!\n");
490 break;
491 case SkCodec::kIncompleteInput:
492 SkDebugf("[terminated] Partial Success\n");
493 break;
Leon Scroggins III674a1842017-07-06 12:26:09 -0400494 case SkCodec::kErrorInInput:
495 SkDebugf("[terminated] Partial Success with error\n");
496 break;
kjlubick2a42f482016-02-16 16:14:23 -0800497 case SkCodec::kInvalidConversion:
498 SkDebugf("Incompatible colortype conversion\n");
499 // Crash to allow afl-fuzz to know this was a bug.
500 raise(SIGSEGV);
John Stiles30212b72020-06-11 17:55:07 -0400501 break;
kjlubick2a42f482016-02-16 16:14:23 -0800502 default:
503 SkDebugf("[terminated] Couldn't getPixels.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500504 return;
kjlubick2a42f482016-02-16 16:14:23 -0800505 }
506 break;
507 }
508 case 1: {//kScanline_Mode
Leon Scroggins571b30f2017-07-11 17:35:31 +0000509 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)) {
510 SkDebugf("[terminated] Could not start scanline decoder\n");
511 return;
512 }
kjlubick2a42f482016-02-16 16:14:23 -0800513
514 void* dst = bitmap.getAddr(0, 0);
515 size_t rowBytes = bitmap.rowBytes();
516 uint32_t height = decodeInfo.height();
Brian Osmanea176c62018-04-06 15:28:23 -0400517 // We do not need to check the return value. On an incomplete
518 // image, memory will be filled with a default value.
519 codec->getScanlines(dst, height, rowBytes);
kjlubick47d158e2016-02-01 08:23:50 -0800520 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800521 break;
kjlubick2a42f482016-02-16 16:14:23 -0800522 }
523 case 2: { //kStripe_Mode
524 const int height = decodeInfo.height();
525 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
526 // does not align with image blocks.
527 const int stripeHeight = 37;
528 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
529
530 // Decode odd stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000531 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)
kjlubick2a42f482016-02-16 16:14:23 -0800532 || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
533 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
534 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
535 // to run this test for image types that do not have this scanline ordering.
536 SkDebugf("[terminated] Could not start top-down scanline decoder\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500537 return;
kjlubick2a42f482016-02-16 16:14:23 -0800538 }
539
540 for (int i = 0; i < numStripes; i += 2) {
541 // Skip a stripe
Brian Osman788b9162020-02-07 10:36:46 -0500542 const int linesToSkip = std::min(stripeHeight, height - i * stripeHeight);
kjlubick2a42f482016-02-16 16:14:23 -0800543 codec->skipScanlines(linesToSkip);
544
545 // Read a stripe
546 const int startY = (i + 1) * stripeHeight;
Brian Osman788b9162020-02-07 10:36:46 -0500547 const int linesToRead = std::min(stripeHeight, height - startY);
kjlubick2a42f482016-02-16 16:14:23 -0800548 if (linesToRead > 0) {
549 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
550 }
551 }
552
553 // Decode even stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000554 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo);
kjlubick2a42f482016-02-16 16:14:23 -0800555 if (SkCodec::kSuccess != startResult) {
556 SkDebugf("[terminated] Failed to restart scanline decoder with same parameters.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500557 return;
kjlubick2a42f482016-02-16 16:14:23 -0800558 }
559 for (int i = 0; i < numStripes; i += 2) {
560 // Read a stripe
561 const int startY = i * stripeHeight;
Brian Osman788b9162020-02-07 10:36:46 -0500562 const int linesToRead = std::min(stripeHeight, height - startY);
kjlubick2a42f482016-02-16 16:14:23 -0800563 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
564
565 // Skip a stripe
Brian Osman788b9162020-02-07 10:36:46 -0500566 const int linesToSkip = std::min(stripeHeight, height - (i + 1) * stripeHeight);
kjlubick2a42f482016-02-16 16:14:23 -0800567 if (linesToSkip > 0) {
568 codec->skipScanlines(linesToSkip);
569 }
570 }
571 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800572 break;
kjlubick2a42f482016-02-16 16:14:23 -0800573 }
574 case 3: { //kSubset_Mode
575 // Arbitrarily choose a divisor.
576 int divisor = 2;
577 // Total width/height of the image.
578 const int W = codec->getInfo().width();
579 const int H = codec->getInfo().height();
580 if (divisor > W || divisor > H) {
581 SkDebugf("[terminated] Cannot codec subset: divisor %d is too big "
582 "with dimensions (%d x %d)\n", divisor, W, H);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500583 return;
kjlubick2a42f482016-02-16 16:14:23 -0800584 }
585 // subset dimensions
586 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
587 const int w = SkAlign2(W / divisor);
588 const int h = SkAlign2(H / divisor);
589 SkIRect subset;
590 SkCodec::Options opts;
591 opts.fSubset = &subset;
592 SkBitmap subsetBm;
593 // We will reuse pixel memory from bitmap.
594 void* pixels = bitmap.getPixels();
595 // Keep track of left and top (for drawing subsetBm into canvas). We could use
596 // fscale * x and fscale * y, but we want integers such that the next subset will start
597 // where the last one ended. So we'll add decodeInfo.width() and height().
598 int left = 0;
599 for (int x = 0; x < W; x += w) {
600 int top = 0;
601 for (int y = 0; y < H; y+= h) {
602 // Do not make the subset go off the edge of the image.
Brian Osman788b9162020-02-07 10:36:46 -0500603 const int preScaleW = std::min(w, W - x);
604 const int preScaleH = std::min(h, H - y);
kjlubick2a42f482016-02-16 16:14:23 -0800605 subset.setXYWH(x, y, preScaleW, preScaleH);
606 // And fscale
607 // FIXME: Should we have a version of getScaledDimensions that takes a subset
608 // into account?
609 decodeInfo = decodeInfo.makeWH(
Brian Osman788b9162020-02-07 10:36:46 -0500610 std::max(1, SkScalarRoundToInt(preScaleW * fscale)),
611 std::max(1, SkScalarRoundToInt(preScaleH * fscale)));
kjlubick2a42f482016-02-16 16:14:23 -0800612 size_t rowBytes = decodeInfo.minRowBytes();
Leon Scroggins571b30f2017-07-11 17:35:31 +0000613 if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes)) {
kjlubick2a42f482016-02-16 16:14:23 -0800614 SkDebugf("[terminated] Could not install pixels.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500615 return;
kjlubick2a42f482016-02-16 16:14:23 -0800616 }
617 const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000618 &opts);
kjlubick2a42f482016-02-16 16:14:23 -0800619 switch (result) {
620 case SkCodec::kSuccess:
621 case SkCodec::kIncompleteInput:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400622 case SkCodec::kErrorInInput:
kjlubick2a42f482016-02-16 16:14:23 -0800623 SkDebugf("okay\n");
624 break;
625 case SkCodec::kInvalidConversion:
626 if (0 == (x|y)) {
627 // First subset is okay to return unimplemented.
628 SkDebugf("[terminated] Incompatible colortype conversion\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500629 return;
kjlubick2a42f482016-02-16 16:14:23 -0800630 }
631 // If the first subset succeeded, a later one should not fail.
John Stiles30212b72020-06-11 17:55:07 -0400632 [[fallthrough]];
kjlubick2a42f482016-02-16 16:14:23 -0800633 case SkCodec::kUnimplemented:
634 if (0 == (x|y)) {
635 // First subset is okay to return unimplemented.
636 SkDebugf("[terminated] subset codec not supported\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500637 return;
kjlubick2a42f482016-02-16 16:14:23 -0800638 }
639 // If the first subset succeeded, why would a later one fail?
John Stiles30212b72020-06-11 17:55:07 -0400640 [[fallthrough]];
kjlubick2a42f482016-02-16 16:14:23 -0800641 default:
642 SkDebugf("[terminated] subset codec failed to decode (%d, %d, %d, %d) "
643 "with dimensions (%d x %d)\t error %d\n",
644 x, y, decodeInfo.width(), decodeInfo.height(),
645 W, H, result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500646 return;
kjlubick2a42f482016-02-16 16:14:23 -0800647 }
648 // translate by the scaled height.
649 top += decodeInfo.height();
650 }
651 // translate by the scaled width.
652 left += decodeInfo.width();
653 }
654 SkDebugf("[terminated] Success!\n");
655 break;
656 }
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500657 case 4: { //kAnimated_Mode
658 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
659 if (frameInfos.size() == 0) {
660 SkDebugf("[terminated] Not an animated image\n");
661 break;
662 }
663
664 for (size_t i = 0; i < frameInfos.size(); i++) {
665 options.fFrameIndex = i;
666 auto result = codec->startIncrementalDecode(decodeInfo, bitmap.getPixels(),
667 bitmap.rowBytes(), &options);
668 if (SkCodec::kSuccess != result) {
669 SkDebugf("[terminated] failed to start incremental decode "
John Stiles7bf79992021-06-25 11:05:20 -0400670 "in frame %zu with error %d\n", i, result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500671 return;
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500672 }
673
674 result = codec->incrementalDecode();
Leon Scroggins III674a1842017-07-06 12:26:09 -0400675 if (result == SkCodec::kIncompleteInput || result == SkCodec::kErrorInInput) {
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500676 SkDebugf("okay\n");
677 // Frames beyond this one will not decode.
678 break;
679 }
680 if (result == SkCodec::kSuccess) {
John Stiles7bf79992021-06-25 11:05:20 -0400681 SkDebugf("okay - decoded frame %zu\n", i);
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500682 } else {
683 SkDebugf("[terminated] incremental decode failed with "
684 "error %d\n", result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500685 return;
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500686 }
687 }
688 SkDebugf("[terminated] Success!\n");
689 break;
690 }
kjlubickdba57342016-01-21 05:03:28 -0800691 default:
kjlubick2a42f482016-02-16 16:14:23 -0800692 SkDebugf("[terminated] Mode not implemented yet\n");
kjlubickdba57342016-01-21 05:03:28 -0800693 }
694
695 dump_png(bitmap);
mtklein65e58242016-01-13 12:57:57 -0800696}
697
Zepeng Hufcb7ba02020-07-31 17:21:29 +0000698void FuzzSKP(sk_sp<SkData> bytes);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500699static void fuzz_skp(sk_sp<SkData> bytes) {
Zepeng Hufcb7ba02020-07-31 17:21:29 +0000700 FuzzSKP(bytes);
701 SkDebugf("[terminated] Finished SKP\n");
kjlubickdba57342016-01-21 05:03:28 -0800702}
mtklein65e58242016-01-13 12:57:57 -0800703
Kevin Lubickf80f1152017-01-06 08:26:56 -0500704static void fuzz_color_deserialize(sk_sp<SkData> bytes) {
kjlubick3e3c1a52016-06-23 10:49:27 -0700705 sk_sp<SkColorSpace> space(SkColorSpace::Deserialize(bytes->data(), bytes->size()));
706 if (!space) {
707 SkDebugf("[terminated] Couldn't deserialize Colorspace.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500708 return;
kjlubick3e3c1a52016-06-23 10:49:27 -0700709 }
710 SkDebugf("[terminated] Success! deserialized Colorspace.\n");
kjlubick3e3c1a52016-06-23 10:49:27 -0700711}
712
Kevin Lubickf034d112018-02-08 14:31:24 -0500713void FuzzPathDeserialize(SkReadBuffer& buf);
Kevin Lubickc9d28292017-11-09 08:12:56 -0500714
Kevin Lubickf034d112018-02-08 14:31:24 -0500715static void fuzz_path_deserialize(sk_sp<SkData> bytes) {
716 SkReadBuffer buf(bytes->data(), bytes->size());
717 FuzzPathDeserialize(buf);
718 SkDebugf("[terminated] path_deserialize didn't crash!\n");
Kevin Lubickf04c50a2017-01-06 13:48:19 -0500719}
720
Kevin Lubick2541edf2018-01-11 10:27:14 -0500721bool FuzzRegionDeserialize(sk_sp<SkData> bytes);
722
Kevin Lubickedee1ae2017-02-20 17:47:18 -0500723static void fuzz_region_deserialize(sk_sp<SkData> bytes) {
Kevin Lubick2541edf2018-01-11 10:27:14 -0500724 if (!FuzzRegionDeserialize(bytes)) {
Kevin Lubickedee1ae2017-02-20 17:47:18 -0500725 SkDebugf("[terminated] Couldn't initialize SkRegion.\n");
726 return;
727 }
Kevin Lubickedee1ae2017-02-20 17:47:18 -0500728 SkDebugf("[terminated] Success! Initialized SkRegion.\n");
729}
730
Kevin Lubickf034d112018-02-08 14:31:24 -0500731void FuzzTextBlobDeserialize(SkReadBuffer& buf);
732
Kevin Lubickd46e85f2017-11-21 17:13:35 -0500733static void fuzz_textblob_deserialize(sk_sp<SkData> bytes) {
Mike Reedfadbfcd2017-12-06 16:09:20 -0500734 SkReadBuffer buf(bytes->data(), bytes->size());
Kevin Lubickf034d112018-02-08 14:31:24 -0500735 FuzzTextBlobDeserialize(buf);
736 SkDebugf("[terminated] textblob didn't crash!\n");
Kevin Lubickd46e85f2017-11-21 17:13:35 -0500737}
738
Kevin Lubick2541edf2018-01-11 10:27:14 -0500739void FuzzRegionSetPath(Fuzz* fuzz);
740
741static void fuzz_region_set_path(sk_sp<SkData> bytes) {
742 Fuzz fuzz(bytes);
743 FuzzRegionSetPath(&fuzz);
744 SkDebugf("[terminated] region_set_path didn't crash!\n");
745}
746
Kevin Lubickf034d112018-02-08 14:31:24 -0500747void FuzzImageFilterDeserialize(sk_sp<SkData> bytes);
748
Herb Derbya839fc02017-03-16 12:30:43 -0400749static void fuzz_filter_fuzz(sk_sp<SkData> bytes) {
Kevin Lubickf034d112018-02-08 14:31:24 -0500750 FuzzImageFilterDeserialize(bytes);
751 SkDebugf("[terminated] filter_fuzz didn't crash!\n");
Herb Derbya839fc02017-03-16 12:30:43 -0400752}
753
Zepeng Hua5783f32020-07-10 13:36:20 +0000754bool FuzzSkRuntimeEffect(sk_sp<SkData> bytes);
755
756static void fuzz_skruntimeeffect(sk_sp<SkData> bytes) {
757 if (FuzzSkRuntimeEffect(bytes)) {
758 SkDebugf("[terminated] Success! Compiled and Executed sksl code.\n");
759 } else {
760 SkDebugf("[terminated] Could not Compile or Execute sksl code.\n");
761 }
762}
763
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400764bool FuzzSKSL2GLSL(sk_sp<SkData> bytes);
765
Kevin Lubickf80f1152017-01-06 08:26:56 -0500766static void fuzz_sksl2glsl(sk_sp<SkData> bytes) {
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400767 if (FuzzSKSL2GLSL(bytes)) {
768 SkDebugf("[terminated] Success! Compiled input to GLSL.\n");
769 } else {
770 SkDebugf("[terminated] Could not compile input to GLSL.\n");
kjlubicke7195772016-10-18 10:06:24 -0700771 }
kjlubicke7195772016-10-18 10:06:24 -0700772}
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400773
774bool FuzzSKSL2SPIRV(sk_sp<SkData> bytes);
775
776static void fuzz_sksl2spirv(sk_sp<SkData> bytes) {
777 if (FuzzSKSL2SPIRV(bytes)) {
778 SkDebugf("[terminated] Success! Compiled input to SPIRV.\n");
779 } else {
780 SkDebugf("[terminated] Could not compile input to SPIRV.\n");
781 }
782}
783
784bool FuzzSKSL2Metal(sk_sp<SkData> bytes);
785
786static void fuzz_sksl2metal(sk_sp<SkData> bytes) {
787 if (FuzzSKSL2Metal(bytes)) {
Kevin Lubick39cbe462019-03-11 15:38:00 -0400788 SkDebugf("[terminated] Success! Compiled input to Metal.\n");
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400789 } else {
Kevin Lubick39cbe462019-03-11 15:38:00 -0400790 SkDebugf("[terminated] Could not compile input to Metal.\n");
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400791 }
792}
Kevin Lubick0f0a7102019-03-18 16:20:55 -0400793
794bool FuzzSKSL2Pipeline(sk_sp<SkData> bytes);
795
796static void fuzz_sksl2pipeline(sk_sp<SkData> bytes) {
797 if (FuzzSKSL2Pipeline(bytes)) {
798 SkDebugf("[terminated] Success! Compiled input to pipeline stage.\n");
799 } else {
800 SkDebugf("[terminated] Could not compile input to pipeline stage.\n");
801 }
802}
Kevin Lubick2be14d32019-10-21 13:44:48 -0400803
804void FuzzSkDescriptorDeserialize(sk_sp<SkData> bytes);
805
806static void fuzz_skdescriptor_deserialize(sk_sp<SkData> bytes) {
807 FuzzSkDescriptorDeserialize(bytes);
808 SkDebugf("[terminated] Did not crash while deserializing an SkDescriptor.\n");
809}
810