blob: 9187cb510d4c26881fb6324349006ee0acf19f92 [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"
Kevin Lubick87a2e792023-03-23 15:06:36 -040010#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkCanvas.h"
12#include "include/core/SkData.h"
Kevin Lubick1e971192023-11-10 16:14:44 -050013#include "include/core/SkFontMgr.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkImage.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkMallocPixelRef.h"
16#include "include/core/SkPaint.h"
17#include "include/core/SkPath.h"
18#include "include/core/SkStream.h"
19#include "include/core/SkSurface.h"
20#include "include/core/SkTextBlob.h"
Kevin Lubickf48d2b02023-04-11 18:37:34 -040021#include "include/encode/SkPngEncoder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/core/SkOSFile.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/core/SkReadBuffer.h"
24#include "src/utils/SkOSPath.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "tools/ToolUtils.h"
Kevin Lubickd7255a72019-06-03 11:59:23 -040026#include "tools/flags/CommandLineFlags.h"
Kevin Lubick1e971192023-11-10 16:14:44 -050027#include "tools/fonts/FontToolUtils.h"
Hal Canary8a001442018-09-19 11:31:27 -040028
Hal Canary62176db2017-02-27 16:42:03 -050029#include <iostream>
Kevin Lubickacd456a2018-04-24 13:58:16 -040030#include <map>
Kevin Lubickfffa6412018-04-23 16:44:55 -040031#include <regex>
mtkleina1159422016-01-15 05:46:54 -080032#include <signal.h>
Kevin Lubick2541edf2018-01-11 10:27:14 -050033
Mike Klein84836b72019-03-21 11:31:36 -050034static DEFINE_string2(bytes, b, "", "A path to a file or a directory. If a file, the "
35 "contents will be used as the fuzz bytes. If a directory, all files "
36 "in the directory will be used as fuzz bytes for the fuzzer, one at a "
37 "time.");
38static DEFINE_string2(name, n, "", "If --type is 'api', fuzz the API with this name.");
39static DEFINE_string2(dump, d, "", "If not empty, dump 'image*' or 'skp' types as a "
40 "PNG with this name.");
Brian Osmanf4e57372020-07-17 11:37:14 -040041static DEFINE_int(loops, 1, "Run the fuzzer on each input this many times.");
Kevin Lubick9ff5dc92018-01-09 12:47:33 -050042DEFINE_bool2(verbose, v, false, "Print more information while fuzzing.");
Florin Malita7796f002018-06-08 12:25:38 -040043
44// This cannot be inlined in DEFINE_string2 due to interleaved ifdefs
45static constexpr char g_type_message[] = "How to interpret --bytes, one of:\n"
Kevin Lubick0f3d2a62018-10-17 10:24:44 -040046 "android_codec\n"
Florin Malita7796f002018-06-08 12:25:38 -040047 "animated_image_decode\n"
48 "api\n"
49 "color_deserialize\n"
Dominik Röttschesfa37f3c2022-08-22 16:48:42 +030050 "colrv1\n"
Florin Malita7796f002018-06-08 12:25:38 -040051 "filter_fuzz (equivalent to Chrome's filter_fuzz_stub)\n"
52 "image_decode\n"
Kevin Lubick0f3d2a62018-10-17 10:24:44 -040053 "image_decode_incremental\n"
Florin Malita7796f002018-06-08 12:25:38 -040054 "image_mode\n"
55 "image_scale\n"
Florin Malita80452be2018-06-19 11:27:20 -040056 "json\n"
Florin Malita7796f002018-06-08 12:25:38 -040057 "path_deserialize\n"
Florin Malita7796f002018-06-08 12:25:38 -040058 "region_deserialize\n"
59 "region_set_path\n"
Kevin Lubick2be14d32019-10-21 13:44:48 -040060 "skdescriptor_deserialize\n"
John Stilesf5609522022-10-07 13:14:33 -040061 "skmeshspecialization\n"
John Stilesb27d63a2023-10-06 14:42:54 -040062#if defined(SK_ENABLE_SKOTTIE)
63 "skottie_json\n"
64#endif
Florin Malita7796f002018-06-08 12:25:38 -040065 "skp\n"
John Stiles54fd78d2023-10-06 14:23:07 -040066 "skruntimeblender\n"
John Stiles3dfb0fb2023-10-06 14:23:04 -040067 "skruntimecolorfilter\n"
Zepeng Hua5783f32020-07-10 13:36:20 +000068 "skruntimeeffect\n"
Florin Malita7796f002018-06-08 12:25:38 -040069 "sksl2glsl\n"
Kevin Lubicke9c1ce82019-03-11 11:09:40 -040070 "sksl2metal\n"
Kevin Lubick0f0a7102019-03-18 16:20:55 -040071 "sksl2pipeline\n"
Kevin Lubicke9c1ce82019-03-11 11:09:40 -040072 "sksl2spirv\n"
John Stilesb27d63a2023-10-06 14:42:54 -040073 "sksl2wgsl\n"
74 "svg_dom\n"
Florin Malita7796f002018-06-08 12:25:38 -040075 "textblob";
76
Mike Klein84836b72019-03-21 11:31:36 -050077static DEFINE_string2(type, t, "", g_type_message);
kjlubickdba57342016-01-21 05:03:28 -080078
John Stiles04a9f722023-10-19 11:46:34 -040079static int fuzz_file(const SkString& path, SkString type);
kjlubick2a42f482016-02-16 16:14:23 -080080static uint8_t calculate_option(SkData*);
John Stiles04a9f722023-10-19 11:46:34 -040081static SkString try_auto_detect(const SkString& path, SkString* name);
kjlubickdba57342016-01-21 05:03:28 -080082
John Stiles89f49b12023-10-18 10:45:39 -040083static void fuzz_android_codec(const sk_sp<SkData>&);
84static void fuzz_animated_img(const sk_sp<SkData>&);
John Stiles04a9f722023-10-19 11:46:34 -040085static void fuzz_api(const sk_sp<SkData>&, const SkString& name);
John Stiles89f49b12023-10-18 10:45:39 -040086static void fuzz_color_deserialize(const sk_sp<SkData>&);
87static void fuzz_colrv1(const sk_sp<SkData>&);
88static void fuzz_filter_fuzz(const sk_sp<SkData>&);
89static void fuzz_image_decode(const sk_sp<SkData>&);
90static void fuzz_image_decode_incremental(const sk_sp<SkData>&);
91static void fuzz_img(const sk_sp<SkData>&, uint8_t, uint8_t);
92static void fuzz_json(const sk_sp<SkData>&);
93static void fuzz_path_deserialize(const sk_sp<SkData>&);
94static void fuzz_region_deserialize(const sk_sp<SkData>&);
95static void fuzz_region_set_path(const sk_sp<SkData>&);
96static void fuzz_skdescriptor_deserialize(const sk_sp<SkData>&);
97static void fuzz_skmeshspecification(const sk_sp<SkData>&);
98static void fuzz_skp(const sk_sp<SkData>&);
99static void fuzz_skruntimeblender(const sk_sp<SkData>&);
100static void fuzz_skruntimecolorfilter(const sk_sp<SkData>&);
101static void fuzz_skruntimeeffect(const sk_sp<SkData>&);
102static void fuzz_sksl2glsl(const sk_sp<SkData>&);
103static void fuzz_sksl2metal(const sk_sp<SkData>&);
104static void fuzz_sksl2pipeline(const sk_sp<SkData>&);
105static void fuzz_sksl2spirv(const sk_sp<SkData>&);
106static void fuzz_sksl2wgsl(const sk_sp<SkData>&);
107static void fuzz_textblob_deserialize(const sk_sp<SkData>&);
Herb Derbya839fc02017-03-16 12:30:43 -0400108
Kevin Lubickfffa6412018-04-23 16:44:55 -0400109static void print_api_names();
110
Robert Phillips2af13c12021-09-01 16:47:01 +0000111#if defined(SK_ENABLE_SVG)
John Stiles89f49b12023-10-18 10:45:39 -0400112static void fuzz_svg_dom(const sk_sp<SkData>&);
Robert Phillips2af13c12021-09-01 16:47:01 +0000113#endif
114
Florin Malita0b0d93d2018-05-04 15:01:03 -0400115#if defined(SK_ENABLE_SKOTTIE)
John Stiles89f49b12023-10-18 10:45:39 -0400116static void fuzz_skottie_json(const sk_sp<SkData>&);
Florin Malita0b0d93d2018-05-04 15:01:03 -0400117#endif
118
mtklein65e58242016-01-13 12:57:57 -0800119int main(int argc, char** argv) {
Mike Klein88544fb2019-03-20 10:50:33 -0500120 CommandLineFlags::SetUsage(
121 "Usage: fuzz -t <type> -b <path/to/file> [-n api-to-fuzz]\n"
122 " fuzz -b <path/to/file>\n"
123 "--help lists the valid types. If type is not specified,\n"
124 "fuzz will make a guess based on the name of the file.\n");
125 CommandLineFlags::Parse(argc, argv);
Kevin Lubick1e971192023-11-10 16:14:44 -0500126 ToolUtils::UsePortableFontMgr();
mtkleinf5e97822016-01-15 06:19:53 -0800127
Kevin Lubickfffa6412018-04-23 16:44:55 -0400128 SkString path = SkString(FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0]);
129 SkString type = SkString(FLAGS_type.isEmpty() ? "" : FLAGS_type[0]);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500130
Brian Osmanf4e57372020-07-17 11:37:14 -0400131 int loopCount = std::max(FLAGS_loops, 1);
132
Kevin Lubickfffa6412018-04-23 16:44:55 -0400133 if (!sk_isdir(path.c_str())) {
Brian Osmanf4e57372020-07-17 11:37:14 -0400134 for (int i = 0; i < loopCount; ++i) {
135 int rv = fuzz_file(path, type);
136 if (rv != 0) {
137 return rv;
138 }
139 }
140 return 0;
Kevin Lubickf80f1152017-01-06 08:26:56 -0500141 }
142
Kevin Lubickfffa6412018-04-23 16:44:55 -0400143 SkOSFile::Iter it(path.c_str());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500144 for (SkString file; it.next(&file); ) {
Kevin Lubickfffa6412018-04-23 16:44:55 -0400145 SkString p = SkOSPath::Join(path.c_str(), file.c_str());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500146 SkDebugf("Fuzzing %s\n", p.c_str());
Brian Osmanf4e57372020-07-17 11:37:14 -0400147 for (int i = 0; i < loopCount; ++i) {
148 int rv = fuzz_file(p, type);
149 if (rv != 0) {
150 return rv;
151 }
Kevin Lubickf80f1152017-01-06 08:26:56 -0500152 }
153 }
154 return 0;
155}
156
John Stiles04a9f722023-10-19 11:46:34 -0400157static int fuzz_file(const SkString& path, SkString type) {
Kevin Lubickfffa6412018-04-23 16:44:55 -0400158 sk_sp<SkData> bytes(SkData::MakeFromFileName(path.c_str()));
kjlubickdba57342016-01-21 05:03:28 -0800159 if (!bytes) {
Kevin Lubickfffa6412018-04-23 16:44:55 -0400160 SkDebugf("Could not read %s\n", path.c_str());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500161 return 1;
kjlubickdba57342016-01-21 05:03:28 -0800162 }
mtklein65e58242016-01-13 12:57:57 -0800163
Kevin Lubickfffa6412018-04-23 16:44:55 -0400164 SkString name = SkString(FLAGS_name.isEmpty() ? "" : FLAGS_name[0]);
165
166 if (type.isEmpty()) {
167 type = try_auto_detect(path, &name);
kjlubickdba57342016-01-21 05:03:28 -0800168 }
Kevin Lubickfffa6412018-04-23 16:44:55 -0400169
170 if (type.isEmpty()) {
171 SkDebugf("Could not autodetect type of %s\n", path.c_str());
172 return 1;
173 }
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400174 if (type.equals("android_codec")) {
John Stiles04a9f722023-10-19 11:46:34 -0400175 fuzz_android_codec(std::move(bytes));
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400176 return 0;
177 }
Kevin Lubickfffa6412018-04-23 16:44:55 -0400178 if (type.equals("animated_image_decode")) {
John Stiles04a9f722023-10-19 11:46:34 -0400179 fuzz_animated_img(std::move(bytes));
Kevin Lubickfffa6412018-04-23 16:44:55 -0400180 return 0;
181 }
182 if (type.equals("api")) {
183 fuzz_api(bytes, name);
184 return 0;
185 }
186 if (type.equals("color_deserialize")) {
John Stiles04a9f722023-10-19 11:46:34 -0400187 fuzz_color_deserialize(std::move(bytes));
Kevin Lubickfffa6412018-04-23 16:44:55 -0400188 return 0;
189 }
Dominik Röttschesfa37f3c2022-08-22 16:48:42 +0300190 if (type.equals("colrv1")) {
John Stiles04a9f722023-10-19 11:46:34 -0400191 fuzz_colrv1(std::move(bytes));
John Stiles89f49b12023-10-18 10:45:39 -0400192 return 0;
Dominik Röttschesfa37f3c2022-08-22 16:48:42 +0300193 }
Florin Malita80452be2018-06-19 11:27:20 -0400194 if (type.equals("filter_fuzz")) {
John Stiles04a9f722023-10-19 11:46:34 -0400195 fuzz_filter_fuzz(std::move(bytes));
Florin Malita80452be2018-06-19 11:27:20 -0400196 return 0;
197 }
Kevin Lubickfffa6412018-04-23 16:44:55 -0400198 if (type.equals("image_decode")) {
John Stiles04a9f722023-10-19 11:46:34 -0400199 fuzz_image_decode(std::move(bytes));
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400200 return 0;
201 }
202 if (type.equals("image_decode_incremental")) {
John Stiles04a9f722023-10-19 11:46:34 -0400203 fuzz_image_decode_incremental(std::move(bytes));
Kevin Lubickfffa6412018-04-23 16:44:55 -0400204 return 0;
205 }
206 if (type.equals("image_scale")) {
207 uint8_t option = calculate_option(bytes.get());
John Stiles04a9f722023-10-19 11:46:34 -0400208 fuzz_img(std::move(bytes), option, 0);
Kevin Lubickfffa6412018-04-23 16:44:55 -0400209 return 0;
210 }
211 if (type.equals("image_mode")) {
212 uint8_t option = calculate_option(bytes.get());
John Stiles04a9f722023-10-19 11:46:34 -0400213 fuzz_img(std::move(bytes), 0, option);
Kevin Lubickfffa6412018-04-23 16:44:55 -0400214 return 0;
215 }
Florin Malita80452be2018-06-19 11:27:20 -0400216 if (type.equals("json")) {
John Stiles04a9f722023-10-19 11:46:34 -0400217 fuzz_json(std::move(bytes));
Kevin Lubick9eeede22018-05-03 16:26:10 -0400218 return 0;
219 }
Kevin Lubickfffa6412018-04-23 16:44:55 -0400220 if (type.equals("path_deserialize")) {
John Stiles04a9f722023-10-19 11:46:34 -0400221 fuzz_path_deserialize(std::move(bytes));
Kevin Lubickfffa6412018-04-23 16:44:55 -0400222 return 0;
223 }
224 if (type.equals("region_deserialize")) {
John Stiles04a9f722023-10-19 11:46:34 -0400225 fuzz_region_deserialize(std::move(bytes));
Kevin Lubickfffa6412018-04-23 16:44:55 -0400226 return 0;
227 }
228 if (type.equals("region_set_path")) {
John Stiles04a9f722023-10-19 11:46:34 -0400229 fuzz_region_set_path(std::move(bytes));
Kevin Lubickfffa6412018-04-23 16:44:55 -0400230 return 0;
231 }
232 if (type.equals("pipe")) {
Mike Klein60900b52018-09-21 11:19:45 -0400233 SkDebugf("I would prefer not to.\n");
Kevin Lubickfffa6412018-04-23 16:44:55 -0400234 return 0;
235 }
Kevin Lubick2be14d32019-10-21 13:44:48 -0400236 if (type.equals("skdescriptor_deserialize")) {
John Stiles04a9f722023-10-19 11:46:34 -0400237 fuzz_skdescriptor_deserialize(std::move(bytes));
Kevin Lubick2be14d32019-10-21 13:44:48 -0400238 return 0;
239 }
Florin Malita0b0d93d2018-05-04 15:01:03 -0400240#if defined(SK_ENABLE_SKOTTIE)
Kevin Lubick9eeede22018-05-03 16:26:10 -0400241 if (type.equals("skottie_json")) {
John Stiles04a9f722023-10-19 11:46:34 -0400242 fuzz_skottie_json(std::move(bytes));
Kevin Lubickfffa6412018-04-23 16:44:55 -0400243 return 0;
244 }
Florin Malita0b0d93d2018-05-04 15:01:03 -0400245#endif
John Stilesf5609522022-10-07 13:14:33 -0400246 if (type.equals("skmeshspecification")) {
John Stiles04a9f722023-10-19 11:46:34 -0400247 fuzz_skmeshspecification(std::move(bytes));
John Stilesf5609522022-10-07 13:14:33 -0400248 return 0;
249 }
Kevin Lubick9eeede22018-05-03 16:26:10 -0400250 if (type.equals("skp")) {
John Stiles04a9f722023-10-19 11:46:34 -0400251 fuzz_skp(std::move(bytes));
Kevin Lubickfffa6412018-04-23 16:44:55 -0400252 return 0;
253 }
John Stiles54fd78d2023-10-06 14:23:07 -0400254 if (type.equals("skruntimeblender")) {
John Stiles04a9f722023-10-19 11:46:34 -0400255 fuzz_skruntimeblender(std::move(bytes));
John Stiles54fd78d2023-10-06 14:23:07 -0400256 return 0;
257 }
John Stiles3dfb0fb2023-10-06 14:23:04 -0400258 if (type.equals("skruntimecolorfilter")) {
John Stiles04a9f722023-10-19 11:46:34 -0400259 fuzz_skruntimecolorfilter(std::move(bytes));
John Stiles3dfb0fb2023-10-06 14:23:04 -0400260 return 0;
261 }
Zepeng Hua5783f32020-07-10 13:36:20 +0000262 if (type.equals("skruntimeeffect")) {
John Stiles04a9f722023-10-19 11:46:34 -0400263 fuzz_skruntimeeffect(std::move(bytes));
Zepeng Hua5783f32020-07-10 13:36:20 +0000264 return 0;
265 }
Kevin Lubickfffa6412018-04-23 16:44:55 -0400266 if (type.equals("sksl2glsl")) {
John Stiles04a9f722023-10-19 11:46:34 -0400267 fuzz_sksl2glsl(std::move(bytes));
Kevin Lubickfffa6412018-04-23 16:44:55 -0400268 return 0;
269 }
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400270 if (type.equals("sksl2metal")) {
John Stiles04a9f722023-10-19 11:46:34 -0400271 fuzz_sksl2metal(std::move(bytes));
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400272 return 0;
273 }
John Stilesb27d63a2023-10-06 14:42:54 -0400274 if (type.equals("sksl2pipeline")) {
John Stiles04a9f722023-10-19 11:46:34 -0400275 fuzz_sksl2pipeline(std::move(bytes));
John Stilesb27d63a2023-10-06 14:42:54 -0400276 return 0;
277 }
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400278 if (type.equals("sksl2spirv")) {
John Stiles04a9f722023-10-19 11:46:34 -0400279 fuzz_sksl2spirv(std::move(bytes));
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400280 return 0;
281 }
John Stilesb27d63a2023-10-06 14:42:54 -0400282 if (type.equals("sksl2wgsl")) {
John Stiles04a9f722023-10-19 11:46:34 -0400283 fuzz_sksl2wgsl(std::move(bytes));
Kevin Lubick0f0a7102019-03-18 16:20:55 -0400284 return 0;
285 }
Robert Phillips2af13c12021-09-01 16:47:01 +0000286#if defined(SK_ENABLE_SVG)
Zepeng Huedaf3022020-06-12 12:20:59 +0000287 if (type.equals("svg_dom")) {
John Stiles04a9f722023-10-19 11:46:34 -0400288 fuzz_svg_dom(std::move(bytes));
Zepeng Huedaf3022020-06-12 12:20:59 +0000289 return 0;
290 }
Robert Phillips2af13c12021-09-01 16:47:01 +0000291#endif
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400292 if (type.equals("textblob")) {
John Stiles04a9f722023-10-19 11:46:34 -0400293 fuzz_textblob_deserialize(std::move(bytes));
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400294 return 0;
295 }
Kevin Lubick457fa972018-05-29 09:22:06 -0400296 SkDebugf("Unknown type %s\n", type.c_str());
Mike Klein88544fb2019-03-20 10:50:33 -0500297 CommandLineFlags::PrintUsage();
Kevin Lubick9ff5dc92018-01-09 12:47:33 -0500298 return 1;
kjlubickdba57342016-01-21 05:03:28 -0800299}
300
Kevin Lubickfffa6412018-04-23 16:44:55 -0400301static std::map<std::string, std::string> cf_api_map = {
Zepeng Hu94007012020-07-22 14:37:46 +0000302 {"api_create_ddl", "CreateDDL"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400303 {"api_draw_functions", "DrawFunctions"},
Adlai Hollerccb68662021-02-25 10:28:44 -0500304 {"api_ddl_threading", "DDLThreadingGL"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400305 {"api_gradients", "Gradients"},
306 {"api_image_filter", "ImageFilter"},
307 {"api_mock_gpu_canvas", "MockGPUCanvas"},
308 {"api_null_canvas", "NullCanvas"},
309 {"api_path_measure", "PathMeasure"},
Kevin Lubickde54d7c2018-09-24 08:48:19 -0400310 {"api_pathop", "Pathop"},
Kevin Lubick57507f12018-10-11 09:35:15 -0400311 {"api_polyutils", "PolyUtils"},
Robert Phillips1f175b72023-07-18 16:29:13 -0400312#if defined(SK_GRAPHITE) && defined(SK_ENABLE_PRECOMPILE)
313 {"api_precompile", "Precompile"},
314#endif
Kevin Lubickfffa6412018-04-23 16:44:55 -0400315 {"api_raster_n32_canvas", "RasterN32Canvas"},
Weston Tracey1a771fe2021-02-04 11:09:59 -0500316 {"api_skparagraph", "SkParagraph"},
Zepeng Huba7cbf72020-07-01 13:21:03 +0000317 {"api_svg_canvas", "SVGCanvas"},
Kevin Lubick876b0452023-01-24 11:04:37 -0500318 {"cubic_quad_roots", "CubicQuadRoots"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400319 {"jpeg_encoder", "JPEGEncoder"},
320 {"png_encoder", "PNGEncoder"},
Kevin Lubickf84ded22018-10-23 09:28:48 -0400321 {"skia_pathop_fuzzer", "LegacyChromiumPathop"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400322 {"webp_encoder", "WEBPEncoder"}
323};
324
Kevin Lubick6c560552018-06-20 09:29:23 -0400325// maps clusterfuzz/oss-fuzz -> Skia's name
Kevin Lubickfffa6412018-04-23 16:44:55 -0400326static std::map<std::string, std::string> cf_map = {
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400327 {"android_codec", "android_codec"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400328 {"animated_image_decode", "animated_image_decode"},
Kevin Lubicke3ec13b2022-10-28 10:05:22 -0400329 {"colrv1", "colrv1"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400330 {"image_decode", "image_decode"},
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400331 {"image_decode_incremental", "image_decode_incremental"},
Kevin Lubickfffa6412018-04-23 16:44:55 -0400332 {"image_filter_deserialize", "filter_fuzz"},
333 {"image_filter_deserialize_width", "filter_fuzz"},
334 {"path_deserialize", "path_deserialize"},
335 {"region_deserialize", "region_deserialize"},
336 {"region_set_path", "region_set_path"},
Kevin Lubick2be14d32019-10-21 13:44:48 -0400337 {"skdescriptor_deserialize", "skdescriptor_deserialize"},
Kevin Lubick6c560552018-06-20 09:29:23 -0400338 {"skjson", "json"},
John Stilesf5609522022-10-07 13:14:33 -0400339 {"skmeshspecification", "skmeshspecification"},
Zepeng Hufcb7ba02020-07-31 17:21:29 +0000340 {"skp", "skp"},
Zepeng Hua5783f32020-07-10 13:36:20 +0000341 {"skruntimeeffect", "skruntimeeffect"},
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400342 {"sksl2glsl", "sksl2glsl"},
343 {"sksl2metal", "sksl2metal"},
344 {"sksl2spirv", "sksl2spirv"},
Kevin Lubick0f0a7102019-03-18 16:20:55 -0400345 {"sksl2pipeline", "sksl2pipeline"},
Kevin Lubickde54d7c2018-09-24 08:48:19 -0400346#if defined(SK_ENABLE_SKOTTIE)
347 {"skottie_json", "skottie_json"},
348#endif
Robert Phillips2af13c12021-09-01 16:47:01 +0000349#if defined(SK_ENABLE_SVG)
Zepeng Huedaf3022020-06-12 12:20:59 +0000350 {"svg_dom", "svg_dom"},
Robert Phillips2af13c12021-09-01 16:47:01 +0000351#endif
Kevin Lubickfffa6412018-04-23 16:44:55 -0400352 {"textblob_deserialize", "textblob"}
353};
354
John Stiles04a9f722023-10-19 11:46:34 -0400355static SkString try_auto_detect(const SkString& path, SkString* name) {
Kevin Lubickfffa6412018-04-23 16:44:55 -0400356 std::cmatch m;
357 std::regex clusterfuzz("clusterfuzz-testcase(-minimized)?-([a-z0-9_]+)-[\\d]+");
358 std::regex skiafuzzer("(api-)?(\\w+)-[a-f0-9]+");
359
360 if (std::regex_search(path.c_str(), m, clusterfuzz)) {
361 std::string type = m.str(2);
Kevin Lubick6c560552018-06-20 09:29:23 -0400362
363 if (cf_api_map.find(type) != cf_api_map.end()) {
364 *name = SkString(cf_api_map[type].c_str());
365 return SkString("api");
Kevin Lubickfffa6412018-04-23 16:44:55 -0400366 } else {
367 if (cf_map.find(type) != cf_map.end()) {
368 return SkString(cf_map[type].c_str());
369 }
370 }
371 } else if (std::regex_search(path.c_str(), m, skiafuzzer)) {
372 std::string a1 = m.str(1);
373 std::string typeOrName = m.str(2);
374 if (a1.length() > 0) {
375 // it's an api fuzzer
376 *name = SkString(typeOrName.c_str());
377 return SkString("api");
378 } else {
379 return SkString(typeOrName.c_str());
380 }
381 }
382
John Stiles89f49b12023-10-18 10:45:39 -0400383 return SkString();
Kevin Lubickfffa6412018-04-23 16:44:55 -0400384}
385
John Stiles89f49b12023-10-18 10:45:39 -0400386void FuzzJSON(const uint8_t *data, size_t size);
Florin Malita7796f002018-06-08 12:25:38 -0400387
John Stiles89f49b12023-10-18 10:45:39 -0400388static void fuzz_json(const sk_sp<SkData>& data){
389 FuzzJSON(data->bytes(), data->size());
Florin Malita7796f002018-06-08 12:25:38 -0400390 SkDebugf("[terminated] Done parsing!\n");
391}
Florin Malita7796f002018-06-08 12:25:38 -0400392
Florin Malita0b0d93d2018-05-04 15:01:03 -0400393#if defined(SK_ENABLE_SKOTTIE)
John Stiles89f49b12023-10-18 10:45:39 -0400394void FuzzSkottieJSON(const uint8_t *data, size_t size);
Kevin Lubick9eeede22018-05-03 16:26:10 -0400395
John Stiles89f49b12023-10-18 10:45:39 -0400396static void fuzz_skottie_json(const sk_sp<SkData>& data){
397 FuzzSkottieJSON(data->bytes(), data->size());
Kevin Lubick9eeede22018-05-03 16:26:10 -0400398 SkDebugf("[terminated] Done animating!\n");
399}
Florin Malita0b0d93d2018-05-04 15:01:03 -0400400#endif
Kevin Lubick9eeede22018-05-03 16:26:10 -0400401
Robert Phillips2af13c12021-09-01 16:47:01 +0000402#if defined(SK_ENABLE_SVG)
John Stiles89f49b12023-10-18 10:45:39 -0400403void FuzzSVG(const uint8_t *data, size_t size);
Robert Phillips2af13c12021-09-01 16:47:01 +0000404
John Stiles89f49b12023-10-18 10:45:39 -0400405static void fuzz_svg_dom(const sk_sp<SkData>& data){
406 FuzzSVG(data->bytes(), data->size());
Zepeng Huedaf3022020-06-12 12:20:59 +0000407 SkDebugf("[terminated] Done DOM!\n");
408}
Robert Phillips2af13c12021-09-01 16:47:01 +0000409#endif
Zepeng Huedaf3022020-06-12 12:20:59 +0000410
John Stiles89f49b12023-10-18 10:45:39 -0400411void FuzzCOLRv1(const uint8_t* data, size_t size);
Dominik Röttschesfa37f3c2022-08-22 16:48:42 +0300412
John Stiles89f49b12023-10-18 10:45:39 -0400413static void fuzz_colrv1(const sk_sp<SkData>& data) {
414 FuzzCOLRv1(data->bytes(), data->size());
Dominik Röttschesfa37f3c2022-08-22 16:48:42 +0300415 SkDebugf("[terminated] Done COLRv1!\n");
416}
417
kjlubick2a42f482016-02-16 16:14:23 -0800418// This adds up the first 1024 bytes and returns it as an 8 bit integer. This allows afl-fuzz to
419// deterministically excercise different paths, or *options* (such as different scaling sizes or
420// different image modes) without needing to introduce a parameter. This way we don't need a
421// image_scale1, image_scale2, image_scale4, etc fuzzer, we can just have a image_scale fuzzer.
422// Clients are expected to transform this number into a different range, e.g. with modulo (%).
423static uint8_t calculate_option(SkData* bytes) {
424 uint8_t total = 0;
425 const uint8_t* data = bytes->bytes();
426 for (size_t i = 0; i < 1024 && i < bytes->size(); i++) {
427 total += data[i];
428 }
429 return total;
430}
431
Kevin Lubickfffa6412018-04-23 16:44:55 -0400432static void print_api_names(){
433 SkDebugf("When using --type api, please choose an API to fuzz with --name/-n:\n");
Hal Canary972eba32018-07-30 17:07:07 -0400434 for (const Fuzzable& fuzzable : sk_tools::Registry<Fuzzable>::Range()) {
Kevin Lubickfffa6412018-04-23 16:44:55 -0400435 SkDebugf("\t%s\n", fuzzable.name);
436 }
437}
438
John Stiles04a9f722023-10-19 11:46:34 -0400439static void fuzz_api(const sk_sp<SkData>& data, const SkString& name) {
Hal Canary972eba32018-07-30 17:07:07 -0400440 for (const Fuzzable& fuzzable : sk_tools::Registry<Fuzzable>::Range()) {
Kevin Lubickfffa6412018-04-23 16:44:55 -0400441 if (name.equals(fuzzable.name)) {
mtkleinf5e97822016-01-15 06:19:53 -0800442 SkDebugf("Fuzzing %s...\n", fuzzable.name);
John Stilescf747b02023-10-18 10:45:37 -0400443 Fuzz fuzz(data->bytes(), data->size());
mtklein65e58242016-01-13 12:57:57 -0800444 fuzzable.fn(&fuzz);
kjlubick47d158e2016-02-01 08:23:50 -0800445 SkDebugf("[terminated] Success!\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500446 return;
mtklein65e58242016-01-13 12:57:57 -0800447 }
448 }
mtkleind4387ea2016-01-21 06:13:52 -0800449
Kevin Lubickfffa6412018-04-23 16:44:55 -0400450 print_api_names();
kjlubickdba57342016-01-21 05:03:28 -0800451}
452
John Stilesb0af6182023-10-19 11:43:46 -0400453static void dump_png(const SkBitmap& bitmap) {
kjlubickdba57342016-01-21 05:03:28 -0800454 if (!FLAGS_dump.isEmpty()) {
Kevin Lubickf48d2b02023-04-11 18:37:34 -0400455 SkFILEWStream file(FLAGS_dump[0]);
456 SkPngEncoder::Encode(&file, bitmap.pixmap(), {});
kjlubickdba57342016-01-21 05:03:28 -0800457 SkDebugf("Dumped to %s\n", FLAGS_dump[0]);
458 }
459}
460
John Stiles89f49b12023-10-18 10:45:39 -0400461bool FuzzAnimatedImage(const uint8_t *data, size_t size);
Kevin Lubick2416f962018-02-12 08:26:39 -0500462
John Stiles89f49b12023-10-18 10:45:39 -0400463static void fuzz_animated_img(const sk_sp<SkData>& data) {
464 if (FuzzAnimatedImage(data->bytes(), data->size())) {
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400465 SkDebugf("[terminated] Success from decoding/drawing animated image!\n");
466 return;
467 }
468 SkDebugf("[terminated] Could not decode or draw animated image.\n");
Kevin Lubick2416f962018-02-12 08:26:39 -0500469}
470
John Stiles89f49b12023-10-18 10:45:39 -0400471bool FuzzImageDecode(const uint8_t *data, size_t size);
Kevin Lubick2416f962018-02-12 08:26:39 -0500472
John Stiles89f49b12023-10-18 10:45:39 -0400473static void fuzz_image_decode(const sk_sp<SkData>& data) {
474 if (FuzzImageDecode(data->bytes(), data->size())) {
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400475 SkDebugf("[terminated] Success from decoding/drawing image!\n");
476 return;
477 }
478 SkDebugf("[terminated] Could not decode or draw image.\n");
Kevin Lubick2416f962018-02-12 08:26:39 -0500479}
480
John Stiles89f49b12023-10-18 10:45:39 -0400481bool FuzzIncrementalImageDecode(const uint8_t *data, size_t size);
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400482
John Stiles89f49b12023-10-18 10:45:39 -0400483static void fuzz_image_decode_incremental(const sk_sp<SkData>& data) {
484 if (FuzzIncrementalImageDecode(data->bytes(), data->size())) {
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400485 SkDebugf("[terminated] Success using incremental decode!\n");
486 return;
487 }
488 SkDebugf("[terminated] Could not incrementally decode and image.\n");
489}
490
John Stiles89f49b12023-10-18 10:45:39 -0400491bool FuzzAndroidCodec(const uint8_t *fuzzData, size_t fuzzSize, uint8_t sampleSize);
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400492
John Stiles89f49b12023-10-18 10:45:39 -0400493static void fuzz_android_codec(const sk_sp<SkData>& data) {
John Stilescf747b02023-10-18 10:45:37 -0400494 Fuzz fuzz(data->bytes(), data->size());
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400495 uint8_t sampleSize;
496 fuzz.nextRange(&sampleSize, 1, 64);
John Stiles89f49b12023-10-18 10:45:39 -0400497 if (FuzzAndroidCodec(fuzz.remainingData(), fuzz.remainingSize(), sampleSize)) {
Kevin Lubick0f3d2a62018-10-17 10:24:44 -0400498 SkDebugf("[terminated] Success on Android Codec sampleSize=%u!\n", sampleSize);
499 return;
500 }
501 SkDebugf("[terminated] Could not use Android Codec sampleSize=%u!\n", sampleSize);
502}
503
504// This is a "legacy" fuzzer that likely does too much. It was based off of how
505// DM reads in images. image_decode, image_decode_incremental and android_codec
506// are more targeted fuzzers that do a subset of what this one does.
John Stiles89f49b12023-10-18 10:45:39 -0400507static void fuzz_img(const sk_sp<SkData>& bytes, uint8_t scale, uint8_t mode) {
kjlubick2a42f482016-02-16 16:14:23 -0800508 // We can scale 1x, 2x, 4x, 8x, 16x
509 scale = scale % 5;
kjlubick5bd98a22016-02-18 06:27:38 -0800510 float fscale = (float)pow(2.0f, scale);
511 SkDebugf("Scaling factor: %f\n", fscale);
kjlubick2a42f482016-02-16 16:14:23 -0800512
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500513 // We have 5 different modes of decoding.
514 mode = mode % 5;
kjlubick2a42f482016-02-16 16:14:23 -0800515 SkDebugf("Mode: %d\n", mode);
516
517 // This is mostly copied from DMSrcSink's CodecSrc::draw method.
kjlubick47d158e2016-02-01 08:23:50 -0800518 SkDebugf("Decoding\n");
Mike Reedede7bac2017-07-23 15:30:02 -0400519 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(bytes));
John Stilesa008b0f2020-08-16 08:48:02 -0400520 if (nullptr == codec) {
kjlubick47d158e2016-02-01 08:23:50 -0800521 SkDebugf("[terminated] Couldn't create codec.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500522 return;
kjlubickdba57342016-01-21 05:03:28 -0800523 }
524
525 SkImageInfo decodeInfo = codec->getInfo();
kjlubick2a42f482016-02-16 16:14:23 -0800526 SkISize size = codec->getScaledDimensions(fscale);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400527 decodeInfo = decodeInfo.makeDimensions(size);
kjlubick2a42f482016-02-16 16:14:23 -0800528
kjlubickdba57342016-01-21 05:03:28 -0800529 SkBitmap bitmap;
kjlubickdba57342016-01-21 05:03:28 -0800530 SkCodec::Options options;
531 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
532
Mike Reed086a4272017-07-18 10:53:11 -0400533 if (!bitmap.tryAllocPixelsFlags(decodeInfo, SkBitmap::kZeroPixels_AllocFlag)) {
kjlubick47d158e2016-02-01 08:23:50 -0800534 SkDebugf("[terminated] Could not allocate memory. Image might be too large (%d x %d)",
kjlubick2a42f482016-02-16 16:14:23 -0800535 decodeInfo.width(), decodeInfo.height());
Kevin Lubickf80f1152017-01-06 08:26:56 -0500536 return;
kjlubickdba57342016-01-21 05:03:28 -0800537 }
538
kjlubick2a42f482016-02-16 16:14:23 -0800539 switch (mode) {
540 case 0: {//kCodecZeroInit_Mode, kCodec_Mode
Leon Scroggins571b30f2017-07-11 17:35:31 +0000541 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options)) {
kjlubick2a42f482016-02-16 16:14:23 -0800542 case SkCodec::kSuccess:
543 SkDebugf("[terminated] Success!\n");
544 break;
545 case SkCodec::kIncompleteInput:
546 SkDebugf("[terminated] Partial Success\n");
547 break;
Leon Scroggins III674a1842017-07-06 12:26:09 -0400548 case SkCodec::kErrorInInput:
549 SkDebugf("[terminated] Partial Success with error\n");
550 break;
kjlubick2a42f482016-02-16 16:14:23 -0800551 case SkCodec::kInvalidConversion:
552 SkDebugf("Incompatible colortype conversion\n");
553 // Crash to allow afl-fuzz to know this was a bug.
554 raise(SIGSEGV);
John Stiles30212b72020-06-11 17:55:07 -0400555 break;
kjlubick2a42f482016-02-16 16:14:23 -0800556 default:
557 SkDebugf("[terminated] Couldn't getPixels.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500558 return;
kjlubick2a42f482016-02-16 16:14:23 -0800559 }
560 break;
561 }
562 case 1: {//kScanline_Mode
Leon Scroggins571b30f2017-07-11 17:35:31 +0000563 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)) {
564 SkDebugf("[terminated] Could not start scanline decoder\n");
565 return;
566 }
kjlubick2a42f482016-02-16 16:14:23 -0800567
568 void* dst = bitmap.getAddr(0, 0);
569 size_t rowBytes = bitmap.rowBytes();
570 uint32_t height = decodeInfo.height();
Brian Osmanea176c62018-04-06 15:28:23 -0400571 // We do not need to check the return value. On an incomplete
572 // image, memory will be filled with a default value.
573 codec->getScanlines(dst, height, rowBytes);
kjlubick47d158e2016-02-01 08:23:50 -0800574 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800575 break;
kjlubick2a42f482016-02-16 16:14:23 -0800576 }
577 case 2: { //kStripe_Mode
578 const int height = decodeInfo.height();
579 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
580 // does not align with image blocks.
581 const int stripeHeight = 37;
582 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
583
584 // Decode odd stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000585 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)
kjlubick2a42f482016-02-16 16:14:23 -0800586 || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
587 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
588 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
589 // to run this test for image types that do not have this scanline ordering.
590 SkDebugf("[terminated] Could not start top-down scanline decoder\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500591 return;
kjlubick2a42f482016-02-16 16:14:23 -0800592 }
593
594 for (int i = 0; i < numStripes; i += 2) {
595 // Skip a stripe
Brian Osman788b9162020-02-07 10:36:46 -0500596 const int linesToSkip = std::min(stripeHeight, height - i * stripeHeight);
kjlubick2a42f482016-02-16 16:14:23 -0800597 codec->skipScanlines(linesToSkip);
598
599 // Read a stripe
600 const int startY = (i + 1) * stripeHeight;
Brian Osman788b9162020-02-07 10:36:46 -0500601 const int linesToRead = std::min(stripeHeight, height - startY);
kjlubick2a42f482016-02-16 16:14:23 -0800602 if (linesToRead > 0) {
603 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
604 }
605 }
606
607 // Decode even stripes
Leon Scroggins571b30f2017-07-11 17:35:31 +0000608 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo);
kjlubick2a42f482016-02-16 16:14:23 -0800609 if (SkCodec::kSuccess != startResult) {
610 SkDebugf("[terminated] Failed to restart scanline decoder with same parameters.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500611 return;
kjlubick2a42f482016-02-16 16:14:23 -0800612 }
613 for (int i = 0; i < numStripes; i += 2) {
614 // Read a stripe
615 const int startY = i * stripeHeight;
Brian Osman788b9162020-02-07 10:36:46 -0500616 const int linesToRead = std::min(stripeHeight, height - startY);
kjlubick2a42f482016-02-16 16:14:23 -0800617 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
618
619 // Skip a stripe
Brian Osman788b9162020-02-07 10:36:46 -0500620 const int linesToSkip = std::min(stripeHeight, height - (i + 1) * stripeHeight);
kjlubick2a42f482016-02-16 16:14:23 -0800621 if (linesToSkip > 0) {
622 codec->skipScanlines(linesToSkip);
623 }
624 }
625 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800626 break;
kjlubick2a42f482016-02-16 16:14:23 -0800627 }
628 case 3: { //kSubset_Mode
629 // Arbitrarily choose a divisor.
630 int divisor = 2;
631 // Total width/height of the image.
632 const int W = codec->getInfo().width();
633 const int H = codec->getInfo().height();
634 if (divisor > W || divisor > H) {
635 SkDebugf("[terminated] Cannot codec subset: divisor %d is too big "
636 "with dimensions (%d x %d)\n", divisor, W, H);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500637 return;
kjlubick2a42f482016-02-16 16:14:23 -0800638 }
639 // subset dimensions
640 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
641 const int w = SkAlign2(W / divisor);
642 const int h = SkAlign2(H / divisor);
643 SkIRect subset;
644 SkCodec::Options opts;
645 opts.fSubset = &subset;
646 SkBitmap subsetBm;
647 // We will reuse pixel memory from bitmap.
648 void* pixels = bitmap.getPixels();
kjlubick2a42f482016-02-16 16:14:23 -0800649 for (int x = 0; x < W; x += w) {
kjlubick2a42f482016-02-16 16:14:23 -0800650 for (int y = 0; y < H; y+= h) {
651 // Do not make the subset go off the edge of the image.
Brian Osman788b9162020-02-07 10:36:46 -0500652 const int preScaleW = std::min(w, W - x);
653 const int preScaleH = std::min(h, H - y);
kjlubick2a42f482016-02-16 16:14:23 -0800654 subset.setXYWH(x, y, preScaleW, preScaleH);
655 // And fscale
656 // FIXME: Should we have a version of getScaledDimensions that takes a subset
657 // into account?
658 decodeInfo = decodeInfo.makeWH(
Brian Osman788b9162020-02-07 10:36:46 -0500659 std::max(1, SkScalarRoundToInt(preScaleW * fscale)),
660 std::max(1, SkScalarRoundToInt(preScaleH * fscale)));
kjlubick2a42f482016-02-16 16:14:23 -0800661 size_t rowBytes = decodeInfo.minRowBytes();
Leon Scroggins571b30f2017-07-11 17:35:31 +0000662 if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes)) {
kjlubick2a42f482016-02-16 16:14:23 -0800663 SkDebugf("[terminated] Could not install pixels.\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500664 return;
kjlubick2a42f482016-02-16 16:14:23 -0800665 }
666 const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000667 &opts);
kjlubick2a42f482016-02-16 16:14:23 -0800668 switch (result) {
669 case SkCodec::kSuccess:
670 case SkCodec::kIncompleteInput:
Leon Scroggins III674a1842017-07-06 12:26:09 -0400671 case SkCodec::kErrorInInput:
kjlubick2a42f482016-02-16 16:14:23 -0800672 SkDebugf("okay\n");
673 break;
674 case SkCodec::kInvalidConversion:
675 if (0 == (x|y)) {
676 // First subset is okay to return unimplemented.
677 SkDebugf("[terminated] Incompatible colortype conversion\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500678 return;
kjlubick2a42f482016-02-16 16:14:23 -0800679 }
680 // If the first subset succeeded, a later one should not fail.
John Stiles30212b72020-06-11 17:55:07 -0400681 [[fallthrough]];
kjlubick2a42f482016-02-16 16:14:23 -0800682 case SkCodec::kUnimplemented:
683 if (0 == (x|y)) {
684 // First subset is okay to return unimplemented.
685 SkDebugf("[terminated] subset codec not supported\n");
Kevin Lubickf80f1152017-01-06 08:26:56 -0500686 return;
kjlubick2a42f482016-02-16 16:14:23 -0800687 }
688 // If the first subset succeeded, why would a later one fail?
John Stiles30212b72020-06-11 17:55:07 -0400689 [[fallthrough]];
kjlubick2a42f482016-02-16 16:14:23 -0800690 default:
691 SkDebugf("[terminated] subset codec failed to decode (%d, %d, %d, %d) "
692 "with dimensions (%d x %d)\t error %d\n",
693 x, y, decodeInfo.width(), decodeInfo.height(),
694 W, H, result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500695 return;
kjlubick2a42f482016-02-16 16:14:23 -0800696 }
kjlubick2a42f482016-02-16 16:14:23 -0800697 }
kjlubick2a42f482016-02-16 16:14:23 -0800698 }
699 SkDebugf("[terminated] Success!\n");
700 break;
701 }
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500702 case 4: { //kAnimated_Mode
703 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
704 if (frameInfos.size() == 0) {
705 SkDebugf("[terminated] Not an animated image\n");
706 break;
707 }
708
709 for (size_t i = 0; i < frameInfos.size(); i++) {
710 options.fFrameIndex = i;
711 auto result = codec->startIncrementalDecode(decodeInfo, bitmap.getPixels(),
712 bitmap.rowBytes(), &options);
713 if (SkCodec::kSuccess != result) {
714 SkDebugf("[terminated] failed to start incremental decode "
John Stiles7bf79992021-06-25 11:05:20 -0400715 "in frame %zu with error %d\n", i, result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500716 return;
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500717 }
718
719 result = codec->incrementalDecode();
Leon Scroggins III674a1842017-07-06 12:26:09 -0400720 if (result == SkCodec::kIncompleteInput || result == SkCodec::kErrorInInput) {
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500721 SkDebugf("okay\n");
722 // Frames beyond this one will not decode.
723 break;
724 }
725 if (result == SkCodec::kSuccess) {
John Stiles7bf79992021-06-25 11:05:20 -0400726 SkDebugf("okay - decoded frame %zu\n", i);
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500727 } else {
728 SkDebugf("[terminated] incremental decode failed with "
729 "error %d\n", result);
Kevin Lubickf80f1152017-01-06 08:26:56 -0500730 return;
Leon Scroggins IIIc5a83662016-12-08 09:07:56 -0500731 }
732 }
733 SkDebugf("[terminated] Success!\n");
734 break;
735 }
kjlubickdba57342016-01-21 05:03:28 -0800736 default:
kjlubick2a42f482016-02-16 16:14:23 -0800737 SkDebugf("[terminated] Mode not implemented yet\n");
kjlubickdba57342016-01-21 05:03:28 -0800738 }
739
740 dump_png(bitmap);
mtklein65e58242016-01-13 12:57:57 -0800741}
742
John Stiles89f49b12023-10-18 10:45:39 -0400743void FuzzSKP(const uint8_t *data, size_t size);
744
745static void fuzz_skp(const sk_sp<SkData>& data) {
746 FuzzSKP(data->bytes(), data->size());
Zepeng Hufcb7ba02020-07-31 17:21:29 +0000747 SkDebugf("[terminated] Finished SKP\n");
kjlubickdba57342016-01-21 05:03:28 -0800748}
mtklein65e58242016-01-13 12:57:57 -0800749
John Stiles89f49b12023-10-18 10:45:39 -0400750void FuzzColorspace(const uint8_t *data, size_t size);
751
752static void fuzz_color_deserialize(const sk_sp<SkData>& data) {
753 FuzzColorspace(data->bytes(), data->size());
Kevin Lubick5df58802023-10-18 08:31:15 -0400754 SkDebugf("[terminated] Finished SkColorspace\n");
kjlubick3e3c1a52016-06-23 10:49:27 -0700755}
756
John Stiles89f49b12023-10-18 10:45:39 -0400757void FuzzPathDeserialize(const uint8_t *data, size_t size);
Kevin Lubickc9d28292017-11-09 08:12:56 -0500758
John Stiles89f49b12023-10-18 10:45:39 -0400759static void fuzz_path_deserialize(const sk_sp<SkData>& data) {
760 FuzzPathDeserialize(data->bytes(), data->size());
Kevin Lubickf034d112018-02-08 14:31:24 -0500761 SkDebugf("[terminated] path_deserialize didn't crash!\n");
Kevin Lubickf04c50a2017-01-06 13:48:19 -0500762}
763
John Stiles89f49b12023-10-18 10:45:39 -0400764bool FuzzRegionDeserialize(const uint8_t *data, size_t size);
Kevin Lubick2541edf2018-01-11 10:27:14 -0500765
John Stiles89f49b12023-10-18 10:45:39 -0400766static void fuzz_region_deserialize(const sk_sp<SkData>& data) {
767 if (!FuzzRegionDeserialize(data->bytes(), data->size())) {
Kevin Lubickedee1ae2017-02-20 17:47:18 -0500768 SkDebugf("[terminated] Couldn't initialize SkRegion.\n");
769 return;
770 }
Kevin Lubickedee1ae2017-02-20 17:47:18 -0500771 SkDebugf("[terminated] Success! Initialized SkRegion.\n");
772}
773
John Stiles89f49b12023-10-18 10:45:39 -0400774void FuzzTextBlobDeserialize(const uint8_t *data, size_t size);
Kevin Lubickf034d112018-02-08 14:31:24 -0500775
John Stiles89f49b12023-10-18 10:45:39 -0400776static void fuzz_textblob_deserialize(const sk_sp<SkData>& data) {
777 FuzzTextBlobDeserialize(data->bytes(), data->size());
Kevin Lubickf034d112018-02-08 14:31:24 -0500778 SkDebugf("[terminated] textblob didn't crash!\n");
Kevin Lubickd46e85f2017-11-21 17:13:35 -0500779}
780
Kevin Lubick2541edf2018-01-11 10:27:14 -0500781void FuzzRegionSetPath(Fuzz* fuzz);
782
John Stiles89f49b12023-10-18 10:45:39 -0400783static void fuzz_region_set_path(const sk_sp<SkData>& data) {
John Stilescf747b02023-10-18 10:45:37 -0400784 Fuzz fuzz(data->bytes(), data->size());
Kevin Lubick2541edf2018-01-11 10:27:14 -0500785 FuzzRegionSetPath(&fuzz);
786 SkDebugf("[terminated] region_set_path didn't crash!\n");
787}
788
John Stiles89f49b12023-10-18 10:45:39 -0400789void FuzzImageFilterDeserialize(const uint8_t *data, size_t size);
Kevin Lubickf034d112018-02-08 14:31:24 -0500790
John Stiles89f49b12023-10-18 10:45:39 -0400791static void fuzz_filter_fuzz(const sk_sp<SkData>& data) {
792 FuzzImageFilterDeserialize(data->bytes(), data->size());
Kevin Lubickf034d112018-02-08 14:31:24 -0500793 SkDebugf("[terminated] filter_fuzz didn't crash!\n");
Herb Derbya839fc02017-03-16 12:30:43 -0400794}
795
John Stiles89f49b12023-10-18 10:45:39 -0400796void FuzzSkMeshSpecification(const uint8_t *fuzzData, size_t fuzzSize);
John Stilesf5609522022-10-07 13:14:33 -0400797
John Stiles89f49b12023-10-18 10:45:39 -0400798static void fuzz_skmeshspecification(const sk_sp<SkData>& data) {
799 FuzzSkMeshSpecification(data->bytes(), data->size());
John Stilesf5609522022-10-07 13:14:33 -0400800 SkDebugf("[terminated] SkMeshSpecification::Make didn't crash!\n");
801}
802
John Stiles89f49b12023-10-18 10:45:39 -0400803bool FuzzSkRuntimeBlender(const uint8_t *data, size_t size);
John Stiles54fd78d2023-10-06 14:23:07 -0400804
John Stiles89f49b12023-10-18 10:45:39 -0400805static void fuzz_skruntimeblender(const sk_sp<SkData>& data) {
806 if (FuzzSkRuntimeBlender(data->bytes(), data->size())) {
John Stiles54fd78d2023-10-06 14:23:07 -0400807 SkDebugf("[terminated] Success! Compiled and executed SkSL blender.\n");
808 } else {
809 SkDebugf("[terminated] Could not compile or execute SkSL blender.\n");
810 }
811}
812
John Stiles89f49b12023-10-18 10:45:39 -0400813bool FuzzSkRuntimeColorFilter(const uint8_t *data, size_t size);
John Stiles3dfb0fb2023-10-06 14:23:04 -0400814
John Stiles89f49b12023-10-18 10:45:39 -0400815static void fuzz_skruntimecolorfilter(const sk_sp<SkData>& data) {
816 if (FuzzSkRuntimeColorFilter(data->bytes(), data->size())) {
John Stiles3dfb0fb2023-10-06 14:23:04 -0400817 SkDebugf("[terminated] Success! Compiled and executed SkSL color filter.\n");
818 } else {
819 SkDebugf("[terminated] Could not compile or execute SkSL color filter.\n");
820 }
821}
822
John Stiles89f49b12023-10-18 10:45:39 -0400823bool FuzzSkRuntimeEffect(const uint8_t *data, size_t size);
Zepeng Hua5783f32020-07-10 13:36:20 +0000824
John Stiles89f49b12023-10-18 10:45:39 -0400825static void fuzz_skruntimeeffect(const sk_sp<SkData>& data) {
826 if (FuzzSkRuntimeEffect(data->bytes(), data->size())) {
John Stiles3dfb0fb2023-10-06 14:23:04 -0400827 SkDebugf("[terminated] Success! Compiled and executed SkSL shader.\n");
Zepeng Hua5783f32020-07-10 13:36:20 +0000828 } else {
John Stiles3dfb0fb2023-10-06 14:23:04 -0400829 SkDebugf("[terminated] Could not compile or execute SkSL shader.\n");
Zepeng Hua5783f32020-07-10 13:36:20 +0000830 }
831}
832
John Stiles89f49b12023-10-18 10:45:39 -0400833bool FuzzSKSL2GLSL(const uint8_t *data, size_t size);
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400834
John Stiles89f49b12023-10-18 10:45:39 -0400835static void fuzz_sksl2glsl(const sk_sp<SkData>& data) {
836 if (FuzzSKSL2GLSL(data->bytes(), data->size())) {
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400837 SkDebugf("[terminated] Success! Compiled input to GLSL.\n");
838 } else {
839 SkDebugf("[terminated] Could not compile input to GLSL.\n");
kjlubicke7195772016-10-18 10:06:24 -0700840 }
kjlubicke7195772016-10-18 10:06:24 -0700841}
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400842
John Stiles89f49b12023-10-18 10:45:39 -0400843bool FuzzSKSL2Metal(const uint8_t *data, size_t size);
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400844
John Stiles89f49b12023-10-18 10:45:39 -0400845static void fuzz_sksl2metal(const sk_sp<SkData>& data) {
846 if (FuzzSKSL2Metal(data->bytes(), data->size())) {
Kevin Lubick39cbe462019-03-11 15:38:00 -0400847 SkDebugf("[terminated] Success! Compiled input to Metal.\n");
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400848 } else {
Kevin Lubick39cbe462019-03-11 15:38:00 -0400849 SkDebugf("[terminated] Could not compile input to Metal.\n");
Kevin Lubicke9c1ce82019-03-11 11:09:40 -0400850 }
851}
Kevin Lubick0f0a7102019-03-18 16:20:55 -0400852
John Stiles89f49b12023-10-18 10:45:39 -0400853bool FuzzSKSL2Pipeline(const uint8_t *data, size_t size);
Kevin Lubick0f0a7102019-03-18 16:20:55 -0400854
John Stiles89f49b12023-10-18 10:45:39 -0400855static void fuzz_sksl2pipeline(const sk_sp<SkData>& data) {
856 if (FuzzSKSL2Pipeline(data->bytes(), data->size())) {
Kevin Lubick0f0a7102019-03-18 16:20:55 -0400857 SkDebugf("[terminated] Success! Compiled input to pipeline stage.\n");
858 } else {
859 SkDebugf("[terminated] Could not compile input to pipeline stage.\n");
860 }
861}
Kevin Lubick2be14d32019-10-21 13:44:48 -0400862
John Stiles89f49b12023-10-18 10:45:39 -0400863bool FuzzSKSL2SPIRV(const uint8_t *data, size_t size);
John Stilesb27d63a2023-10-06 14:42:54 -0400864
John Stiles89f49b12023-10-18 10:45:39 -0400865static void fuzz_sksl2spirv(const sk_sp<SkData>& data) {
866 if (FuzzSKSL2SPIRV(data->bytes(), data->size())) {
John Stilesb27d63a2023-10-06 14:42:54 -0400867 SkDebugf("[terminated] Success! Compiled input to SPIR-V.\n");
868 } else {
869 SkDebugf("[terminated] Could not compile input to SPIR-V.\n");
870 }
871}
872
John Stiles89f49b12023-10-18 10:45:39 -0400873bool FuzzSKSL2WGSL(const uint8_t *data, size_t size);
John Stilesb27d63a2023-10-06 14:42:54 -0400874
John Stiles89f49b12023-10-18 10:45:39 -0400875static void fuzz_sksl2wgsl(const sk_sp<SkData>& data) {
876 if (FuzzSKSL2WGSL(data->bytes(), data->size())) {
John Stilesb27d63a2023-10-06 14:42:54 -0400877 SkDebugf("[terminated] Success! Compiled input to WGSL.\n");
878 } else {
879 SkDebugf("[terminated] Could not compile input to WGSL.\n");
880 }
881}
882
John Stiles89f49b12023-10-18 10:45:39 -0400883void FuzzSkDescriptorDeserialize(const uint8_t *data, size_t size);
Kevin Lubick2be14d32019-10-21 13:44:48 -0400884
John Stiles89f49b12023-10-18 10:45:39 -0400885static void fuzz_skdescriptor_deserialize(const sk_sp<SkData>& data) {
886 FuzzSkDescriptorDeserialize(data->bytes(), data->size());
Kevin Lubick2be14d32019-10-21 13:44:48 -0400887 SkDebugf("[terminated] Did not crash while deserializing an SkDescriptor.\n");
888}