Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
FuzzMain.cpp
Go to the documentation of this file.
1/*
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/Fuzz.h"
12#include "include/core/SkData.h"
17#include "include/core/SkPath.h"
22#include "src/core/SkOSFile.h"
24#include "src/utils/SkOSPath.h"
25#include "tools/ToolUtils.h"
28
29#include <iostream>
30#include <map>
31#include <regex>
32#include <signal.h>
33
34static 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.");
41static DEFINE_int(loops, 1, "Run the fuzzer on each input this many times.");
42DEFINE_bool2(verbose, v, false, "Print more information while fuzzing.");
43
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"
46 "android_codec\n"
47 "animated_image_decode\n"
48 "api\n"
49 "color_deserialize\n"
50 "colrv1\n"
51 "filter_fuzz (equivalent to Chrome's filter_fuzz_stub)\n"
52 "image_decode\n"
53 "image_decode_incremental\n"
54 "image_mode\n"
55 "image_scale\n"
56 "json\n"
57 "path_deserialize\n"
58 "region_deserialize\n"
59 "region_set_path\n"
60 "skdescriptor_deserialize\n"
61 "skmeshspecialization\n"
62#if defined(SK_ENABLE_SKOTTIE)
63 "skottie_json\n"
64#endif
65 "skp\n"
66 "skruntimeblender\n"
67 "skruntimecolorfilter\n"
68 "skruntimeeffect\n"
69 "sksl2glsl\n"
70 "sksl2metal\n"
71 "sksl2pipeline\n"
72 "sksl2spirv\n"
73 "sksl2wgsl\n"
74 "svg_dom\n"
75 "textblob";
76
78
79static int fuzz_file(const SkString& path, SkString type);
80static uint8_t calculate_option(SkData*);
81static SkString try_auto_detect(const SkString& path, SkString* name);
82
83static void fuzz_android_codec(const sk_sp<SkData>&);
84static void fuzz_animated_img(const sk_sp<SkData>&);
85static void fuzz_api(const sk_sp<SkData>&, const SkString& name);
86static 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>&);
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>&);
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>&);
108
109static void print_api_names();
110
111#if defined(SK_ENABLE_SVG)
112static void fuzz_svg_dom(const sk_sp<SkData>&);
113#endif
114
115#if defined(SK_ENABLE_SKOTTIE)
116static void fuzz_skottie_json(const sk_sp<SkData>&);
117#endif
118
119int main(int argc, char** argv) {
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");
127
128 SkString path = SkString(FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0]);
129 SkString type = SkString(FLAGS_type.isEmpty() ? "" : FLAGS_type[0]);
130
131 int loopCount = std::max(FLAGS_loops, 1);
132
133 if (!sk_isdir(path.c_str())) {
134 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;
141 }
142
143 SkOSFile::Iter it(path.c_str());
144 for (SkString file; it.next(&file); ) {
145 SkString p = SkOSPath::Join(path.c_str(), file.c_str());
146 SkDebugf("Fuzzing %s\n", p.c_str());
147 for (int i = 0; i < loopCount; ++i) {
148 int rv = fuzz_file(p, type);
149 if (rv != 0) {
150 return rv;
151 }
152 }
153 }
154 return 0;
155}
156
157static int fuzz_file(const SkString& path, SkString type) {
158 sk_sp<SkData> bytes(SkData::MakeFromFileName(path.c_str()));
159 if (!bytes) {
160 SkDebugf("Could not read %s\n", path.c_str());
161 return 1;
162 }
163
164 SkString name = SkString(FLAGS_name.isEmpty() ? "" : FLAGS_name[0]);
165
166 if (type.isEmpty()) {
167 type = try_auto_detect(path, &name);
168 }
169
170 if (type.isEmpty()) {
171 SkDebugf("Could not autodetect type of %s\n", path.c_str());
172 return 1;
173 }
174 if (type.equals("android_codec")) {
175 fuzz_android_codec(std::move(bytes));
176 return 0;
177 }
178 if (type.equals("animated_image_decode")) {
179 fuzz_animated_img(std::move(bytes));
180 return 0;
181 }
182 if (type.equals("api")) {
183 fuzz_api(bytes, name);
184 return 0;
185 }
186 if (type.equals("color_deserialize")) {
187 fuzz_color_deserialize(std::move(bytes));
188 return 0;
189 }
190 if (type.equals("colrv1")) {
191 fuzz_colrv1(std::move(bytes));
192 return 0;
193 }
194 if (type.equals("filter_fuzz")) {
195 fuzz_filter_fuzz(std::move(bytes));
196 return 0;
197 }
198 if (type.equals("image_decode")) {
199 fuzz_image_decode(std::move(bytes));
200 return 0;
201 }
202 if (type.equals("image_decode_incremental")) {
203 fuzz_image_decode_incremental(std::move(bytes));
204 return 0;
205 }
206 if (type.equals("image_scale")) {
207 uint8_t option = calculate_option(bytes.get());
208 fuzz_img(std::move(bytes), option, 0);
209 return 0;
210 }
211 if (type.equals("image_mode")) {
212 uint8_t option = calculate_option(bytes.get());
213 fuzz_img(std::move(bytes), 0, option);
214 return 0;
215 }
216 if (type.equals("json")) {
217 fuzz_json(std::move(bytes));
218 return 0;
219 }
220 if (type.equals("path_deserialize")) {
221 fuzz_path_deserialize(std::move(bytes));
222 return 0;
223 }
224 if (type.equals("region_deserialize")) {
225 fuzz_region_deserialize(std::move(bytes));
226 return 0;
227 }
228 if (type.equals("region_set_path")) {
229 fuzz_region_set_path(std::move(bytes));
230 return 0;
231 }
232 if (type.equals("pipe")) {
233 SkDebugf("I would prefer not to.\n");
234 return 0;
235 }
236 if (type.equals("skdescriptor_deserialize")) {
237 fuzz_skdescriptor_deserialize(std::move(bytes));
238 return 0;
239 }
240#if defined(SK_ENABLE_SKOTTIE)
241 if (type.equals("skottie_json")) {
242 fuzz_skottie_json(std::move(bytes));
243 return 0;
244 }
245#endif
246 if (type.equals("skmeshspecification")) {
247 fuzz_skmeshspecification(std::move(bytes));
248 return 0;
249 }
250 if (type.equals("skp")) {
251 fuzz_skp(std::move(bytes));
252 return 0;
253 }
254 if (type.equals("skruntimeblender")) {
255 fuzz_skruntimeblender(std::move(bytes));
256 return 0;
257 }
258 if (type.equals("skruntimecolorfilter")) {
259 fuzz_skruntimecolorfilter(std::move(bytes));
260 return 0;
261 }
262 if (type.equals("skruntimeeffect")) {
263 fuzz_skruntimeeffect(std::move(bytes));
264 return 0;
265 }
266 if (type.equals("sksl2glsl")) {
267 fuzz_sksl2glsl(std::move(bytes));
268 return 0;
269 }
270 if (type.equals("sksl2metal")) {
271 fuzz_sksl2metal(std::move(bytes));
272 return 0;
273 }
274 if (type.equals("sksl2pipeline")) {
275 fuzz_sksl2pipeline(std::move(bytes));
276 return 0;
277 }
278 if (type.equals("sksl2spirv")) {
279 fuzz_sksl2spirv(std::move(bytes));
280 return 0;
281 }
282 if (type.equals("sksl2wgsl")) {
283 fuzz_sksl2wgsl(std::move(bytes));
284 return 0;
285 }
286#if defined(SK_ENABLE_SVG)
287 if (type.equals("svg_dom")) {
288 fuzz_svg_dom(std::move(bytes));
289 return 0;
290 }
291#endif
292 if (type.equals("textblob")) {
293 fuzz_textblob_deserialize(std::move(bytes));
294 return 0;
295 }
296 SkDebugf("Unknown type %s\n", type.c_str());
298 return 1;
299}
300
301static std::map<std::string, std::string> cf_api_map = {
302 {"api_create_ddl", "CreateDDL"},
303 {"api_draw_functions", "DrawFunctions"},
304 {"api_ddl_threading", "DDLThreadingGL"},
305 {"api_gradients", "Gradients"},
306 {"api_image_filter", "ImageFilter"},
307 {"api_mock_gpu_canvas", "MockGPUCanvas"},
308 {"api_null_canvas", "NullCanvas"},
309 {"api_path_measure", "PathMeasure"},
310 {"api_pathop", "Pathop"},
311 {"api_polyutils", "PolyUtils"},
312#if defined(SK_GRAPHITE) && defined(SK_ENABLE_PRECOMPILE)
313 {"api_precompile", "Precompile"},
314#endif
315 {"api_raster_n32_canvas", "RasterN32Canvas"},
316 {"api_skparagraph", "SkParagraph"},
317 {"api_svg_canvas", "SVGCanvas"},
318 {"cubic_quad_roots", "CubicQuadRoots"},
319 {"jpeg_encoder", "JPEGEncoder"},
320 {"png_encoder", "PNGEncoder"},
321 {"skia_pathop_fuzzer", "LegacyChromiumPathop"},
322 {"webp_encoder", "WEBPEncoder"}
323};
324
325// maps clusterfuzz/oss-fuzz -> Skia's name
326static std::map<std::string, std::string> cf_map = {
327 {"android_codec", "android_codec"},
328 {"animated_image_decode", "animated_image_decode"},
329 {"colrv1", "colrv1"},
330 {"image_decode", "image_decode"},
331 {"image_decode_incremental", "image_decode_incremental"},
332 {"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"},
337 {"skdescriptor_deserialize", "skdescriptor_deserialize"},
338 {"skjson", "json"},
339 {"skmeshspecification", "skmeshspecification"},
340 {"skp", "skp"},
341 {"skruntimeeffect", "skruntimeeffect"},
342 {"sksl2glsl", "sksl2glsl"},
343 {"sksl2metal", "sksl2metal"},
344 {"sksl2spirv", "sksl2spirv"},
345 {"sksl2pipeline", "sksl2pipeline"},
346#if defined(SK_ENABLE_SKOTTIE)
347 {"skottie_json", "skottie_json"},
348#endif
349#if defined(SK_ENABLE_SVG)
350 {"svg_dom", "svg_dom"},
351#endif
352 {"textblob_deserialize", "textblob"}
353};
354
356 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);
362
363 if (cf_api_map.find(type) != cf_api_map.end()) {
364 *name = SkString(cf_api_map[type].c_str());
365 return SkString("api");
366 } 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
383 return SkString();
384}
385
386void FuzzJSON(const uint8_t *data, size_t size);
387
388static void fuzz_json(const sk_sp<SkData>& data){
389 FuzzJSON(data->bytes(), data->size());
390 SkDebugf("[terminated] Done parsing!\n");
391}
392
393#if defined(SK_ENABLE_SKOTTIE)
394void FuzzSkottieJSON(const uint8_t *data, size_t size);
395
396static void fuzz_skottie_json(const sk_sp<SkData>& data){
397 FuzzSkottieJSON(data->bytes(), data->size());
398 SkDebugf("[terminated] Done animating!\n");
399}
400#endif
401
402#if defined(SK_ENABLE_SVG)
403void FuzzSVG(const uint8_t *data, size_t size);
404
405static void fuzz_svg_dom(const sk_sp<SkData>& data){
406 FuzzSVG(data->bytes(), data->size());
407 SkDebugf("[terminated] Done DOM!\n");
408}
409#endif
410
411void FuzzCOLRv1(const uint8_t* data, size_t size);
412
413static void fuzz_colrv1(const sk_sp<SkData>& data) {
414 FuzzCOLRv1(data->bytes(), data->size());
415 SkDebugf("[terminated] Done COLRv1!\n");
416}
417
418// 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
432static void print_api_names(){
433 SkDebugf("When using --type api, please choose an API to fuzz with --name/-n:\n");
434 for (const Fuzzable& fuzzable : sk_tools::Registry<Fuzzable>::Range()) {
435 SkDebugf("\t%s\n", fuzzable.name);
436 }
437}
438
439static void fuzz_api(const sk_sp<SkData>& data, const SkString& name) {
440 for (const Fuzzable& fuzzable : sk_tools::Registry<Fuzzable>::Range()) {
441 if (name.equals(fuzzable.name)) {
442 SkDebugf("Fuzzing %s...\n", fuzzable.name);
443 Fuzz fuzz(data->bytes(), data->size());
444 fuzzable.fn(&fuzz);
445 SkDebugf("[terminated] Success!\n");
446 return;
447 }
448 }
449
451}
452
453static void dump_png(const SkBitmap& bitmap) {
454 if (!FLAGS_dump.isEmpty()) {
455 SkFILEWStream file(FLAGS_dump[0]);
456 SkPngEncoder::Encode(&file, bitmap.pixmap(), {});
457 SkDebugf("Dumped to %s\n", FLAGS_dump[0]);
458 }
459}
460
461bool FuzzAnimatedImage(const uint8_t *data, size_t size);
462
463static void fuzz_animated_img(const sk_sp<SkData>& data) {
464 if (FuzzAnimatedImage(data->bytes(), data->size())) {
465 SkDebugf("[terminated] Success from decoding/drawing animated image!\n");
466 return;
467 }
468 SkDebugf("[terminated] Could not decode or draw animated image.\n");
469}
470
471bool FuzzImageDecode(const uint8_t *data, size_t size);
472
473static void fuzz_image_decode(const sk_sp<SkData>& data) {
474 if (FuzzImageDecode(data->bytes(), data->size())) {
475 SkDebugf("[terminated] Success from decoding/drawing image!\n");
476 return;
477 }
478 SkDebugf("[terminated] Could not decode or draw image.\n");
479}
480
481bool FuzzIncrementalImageDecode(const uint8_t *data, size_t size);
482
484 if (FuzzIncrementalImageDecode(data->bytes(), data->size())) {
485 SkDebugf("[terminated] Success using incremental decode!\n");
486 return;
487 }
488 SkDebugf("[terminated] Could not incrementally decode and image.\n");
489}
490
491bool FuzzAndroidCodec(const uint8_t *fuzzData, size_t fuzzSize, uint8_t sampleSize);
492
493static void fuzz_android_codec(const sk_sp<SkData>& data) {
494 Fuzz fuzz(data->bytes(), data->size());
495 uint8_t sampleSize;
496 fuzz.nextRange(&sampleSize, 1, 64);
497 if (FuzzAndroidCodec(fuzz.remainingData(), fuzz.remainingSize(), sampleSize)) {
498 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.
507static void fuzz_img(const sk_sp<SkData>& bytes, uint8_t scale, uint8_t mode) {
508 // We can scale 1x, 2x, 4x, 8x, 16x
509 scale = scale % 5;
510 float fscale = (float)pow(2.0f, scale);
511 SkDebugf("Scaling factor: %f\n", fscale);
512
513 // We have 5 different modes of decoding.
514 mode = mode % 5;
515 SkDebugf("Mode: %d\n", mode);
516
517 // This is mostly copied from DMSrcSink's CodecSrc::draw method.
518 SkDebugf("Decoding\n");
519 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(bytes));
520 if (nullptr == codec) {
521 SkDebugf("[terminated] Couldn't create codec.\n");
522 return;
523 }
524
525 SkImageInfo decodeInfo = codec->getInfo();
526 SkISize size = codec->getScaledDimensions(fscale);
527 decodeInfo = decodeInfo.makeDimensions(size);
528
532
533 if (!bitmap.tryAllocPixelsFlags(decodeInfo, SkBitmap::kZeroPixels_AllocFlag)) {
534 SkDebugf("[terminated] Could not allocate memory. Image might be too large (%d x %d)",
535 decodeInfo.width(), decodeInfo.height());
536 return;
537 }
538
539 switch (mode) {
540 case 0: {//kCodecZeroInit_Mode, kCodec_Mode
541 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options)) {
543 SkDebugf("[terminated] Success!\n");
544 break;
546 SkDebugf("[terminated] Partial Success\n");
547 break;
549 SkDebugf("[terminated] Partial Success with error\n");
550 break;
552 SkDebugf("Incompatible colortype conversion\n");
553 // Crash to allow afl-fuzz to know this was a bug.
554 raise(SIGSEGV);
555 break;
556 default:
557 SkDebugf("[terminated] Couldn't getPixels.\n");
558 return;
559 }
560 break;
561 }
562 case 1: {//kScanline_Mode
563 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)) {
564 SkDebugf("[terminated] Could not start scanline decoder\n");
565 return;
566 }
567
568 void* dst = bitmap.getAddr(0, 0);
569 size_t rowBytes = bitmap.rowBytes();
570 uint32_t height = decodeInfo.height();
571 // 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);
574 SkDebugf("[terminated] Success!\n");
575 break;
576 }
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
585 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)
586 || 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");
591 return;
592 }
593
594 for (int i = 0; i < numStripes; i += 2) {
595 // Skip a stripe
596 const int linesToSkip = std::min(stripeHeight, height - i * stripeHeight);
597 codec->skipScanlines(linesToSkip);
598
599 // Read a stripe
600 const int startY = (i + 1) * stripeHeight;
601 const int linesToRead = std::min(stripeHeight, height - startY);
602 if (linesToRead > 0) {
603 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
604 }
605 }
606
607 // Decode even stripes
608 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo);
609 if (SkCodec::kSuccess != startResult) {
610 SkDebugf("[terminated] Failed to restart scanline decoder with same parameters.\n");
611 return;
612 }
613 for (int i = 0; i < numStripes; i += 2) {
614 // Read a stripe
615 const int startY = i * stripeHeight;
616 const int linesToRead = std::min(stripeHeight, height - startY);
617 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
618
619 // Skip a stripe
620 const int linesToSkip = std::min(stripeHeight, height - (i + 1) * stripeHeight);
621 if (linesToSkip > 0) {
622 codec->skipScanlines(linesToSkip);
623 }
624 }
625 SkDebugf("[terminated] Success!\n");
626 break;
627 }
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);
637 return;
638 }
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();
649 for (int x = 0; x < W; x += w) {
650 for (int y = 0; y < H; y+= h) {
651 // Do not make the subset go off the edge of the image.
652 const int preScaleW = std::min(w, W - x);
653 const int preScaleH = std::min(h, H - y);
654 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(
659 std::max(1, SkScalarRoundToInt(preScaleW * fscale)),
660 std::max(1, SkScalarRoundToInt(preScaleH * fscale)));
661 size_t rowBytes = decodeInfo.minRowBytes();
662 if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes)) {
663 SkDebugf("[terminated] Could not install pixels.\n");
664 return;
665 }
666 const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes,
667 &opts);
668 switch (result) {
672 SkDebugf("okay\n");
673 break;
675 if (0 == (x|y)) {
676 // First subset is okay to return unimplemented.
677 SkDebugf("[terminated] Incompatible colortype conversion\n");
678 return;
679 }
680 // If the first subset succeeded, a later one should not fail.
681 [[fallthrough]];
683 if (0 == (x|y)) {
684 // First subset is okay to return unimplemented.
685 SkDebugf("[terminated] subset codec not supported\n");
686 return;
687 }
688 // If the first subset succeeded, why would a later one fail?
689 [[fallthrough]];
690 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);
695 return;
696 }
697 }
698 }
699 SkDebugf("[terminated] Success!\n");
700 break;
701 }
702 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 "
715 "in frame %zu with error %d\n", i, result);
716 return;
717 }
718
719 result = codec->incrementalDecode();
721 SkDebugf("okay\n");
722 // Frames beyond this one will not decode.
723 break;
724 }
725 if (result == SkCodec::kSuccess) {
726 SkDebugf("okay - decoded frame %zu\n", i);
727 } else {
728 SkDebugf("[terminated] incremental decode failed with "
729 "error %d\n", result);
730 return;
731 }
732 }
733 SkDebugf("[terminated] Success!\n");
734 break;
735 }
736 default:
737 SkDebugf("[terminated] Mode not implemented yet\n");
738 }
739
741}
742
743void FuzzSKP(const uint8_t *data, size_t size);
744
745static void fuzz_skp(const sk_sp<SkData>& data) {
746 FuzzSKP(data->bytes(), data->size());
747 SkDebugf("[terminated] Finished SKP\n");
748}
749
750void 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());
754 SkDebugf("[terminated] Finished SkColorspace\n");
755}
756
757void FuzzPathDeserialize(const uint8_t *data, size_t size);
758
759static void fuzz_path_deserialize(const sk_sp<SkData>& data) {
760 FuzzPathDeserialize(data->bytes(), data->size());
761 SkDebugf("[terminated] path_deserialize didn't crash!\n");
762}
763
764bool FuzzRegionDeserialize(const uint8_t *data, size_t size);
765
766static void fuzz_region_deserialize(const sk_sp<SkData>& data) {
767 if (!FuzzRegionDeserialize(data->bytes(), data->size())) {
768 SkDebugf("[terminated] Couldn't initialize SkRegion.\n");
769 return;
770 }
771 SkDebugf("[terminated] Success! Initialized SkRegion.\n");
772}
773
774void FuzzTextBlobDeserialize(const uint8_t *data, size_t size);
775
776static void fuzz_textblob_deserialize(const sk_sp<SkData>& data) {
777 FuzzTextBlobDeserialize(data->bytes(), data->size());
778 SkDebugf("[terminated] textblob didn't crash!\n");
779}
780
781void FuzzRegionSetPath(Fuzz* fuzz);
782
783static void fuzz_region_set_path(const sk_sp<SkData>& data) {
784 Fuzz fuzz(data->bytes(), data->size());
785 FuzzRegionSetPath(&fuzz);
786 SkDebugf("[terminated] region_set_path didn't crash!\n");
787}
788
789void FuzzImageFilterDeserialize(const uint8_t *data, size_t size);
790
791static void fuzz_filter_fuzz(const sk_sp<SkData>& data) {
792 FuzzImageFilterDeserialize(data->bytes(), data->size());
793 SkDebugf("[terminated] filter_fuzz didn't crash!\n");
794}
795
796void FuzzSkMeshSpecification(const uint8_t *fuzzData, size_t fuzzSize);
797
798static void fuzz_skmeshspecification(const sk_sp<SkData>& data) {
799 FuzzSkMeshSpecification(data->bytes(), data->size());
800 SkDebugf("[terminated] SkMeshSpecification::Make didn't crash!\n");
801}
802
803bool FuzzSkRuntimeBlender(const uint8_t *data, size_t size);
804
805static void fuzz_skruntimeblender(const sk_sp<SkData>& data) {
806 if (FuzzSkRuntimeBlender(data->bytes(), data->size())) {
807 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
813bool FuzzSkRuntimeColorFilter(const uint8_t *data, size_t size);
814
815static void fuzz_skruntimecolorfilter(const sk_sp<SkData>& data) {
816 if (FuzzSkRuntimeColorFilter(data->bytes(), data->size())) {
817 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
823bool FuzzSkRuntimeEffect(const uint8_t *data, size_t size);
824
825static void fuzz_skruntimeeffect(const sk_sp<SkData>& data) {
826 if (FuzzSkRuntimeEffect(data->bytes(), data->size())) {
827 SkDebugf("[terminated] Success! Compiled and executed SkSL shader.\n");
828 } else {
829 SkDebugf("[terminated] Could not compile or execute SkSL shader.\n");
830 }
831}
832
833bool FuzzSKSL2GLSL(const uint8_t *data, size_t size);
834
835static void fuzz_sksl2glsl(const sk_sp<SkData>& data) {
836 if (FuzzSKSL2GLSL(data->bytes(), data->size())) {
837 SkDebugf("[terminated] Success! Compiled input to GLSL.\n");
838 } else {
839 SkDebugf("[terminated] Could not compile input to GLSL.\n");
840 }
841}
842
843bool FuzzSKSL2Metal(const uint8_t *data, size_t size);
844
845static void fuzz_sksl2metal(const sk_sp<SkData>& data) {
846 if (FuzzSKSL2Metal(data->bytes(), data->size())) {
847 SkDebugf("[terminated] Success! Compiled input to Metal.\n");
848 } else {
849 SkDebugf("[terminated] Could not compile input to Metal.\n");
850 }
851}
852
853bool FuzzSKSL2Pipeline(const uint8_t *data, size_t size);
854
855static void fuzz_sksl2pipeline(const sk_sp<SkData>& data) {
856 if (FuzzSKSL2Pipeline(data->bytes(), data->size())) {
857 SkDebugf("[terminated] Success! Compiled input to pipeline stage.\n");
858 } else {
859 SkDebugf("[terminated] Could not compile input to pipeline stage.\n");
860 }
861}
862
863bool FuzzSKSL2SPIRV(const uint8_t *data, size_t size);
864
865static void fuzz_sksl2spirv(const sk_sp<SkData>& data) {
866 if (FuzzSKSL2SPIRV(data->bytes(), data->size())) {
867 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
873bool FuzzSKSL2WGSL(const uint8_t *data, size_t size);
874
875static void fuzz_sksl2wgsl(const sk_sp<SkData>& data) {
876 if (FuzzSKSL2WGSL(data->bytes(), data->size())) {
877 SkDebugf("[terminated] Success! Compiled input to WGSL.\n");
878 } else {
879 SkDebugf("[terminated] Could not compile input to WGSL.\n");
880 }
881}
882
883void FuzzSkDescriptorDeserialize(const uint8_t *data, size_t size);
884
886 FuzzSkDescriptorDeserialize(data->bytes(), data->size());
887 SkDebugf("[terminated] Did not crash while deserializing an SkDescriptor.\n");
888}
#define DEFINE_int(name, defaultValue, helpString)
#define DEFINE_bool2(name, shortName, defaultValue, helpString)
#define DEFINE_string2(name, shortName, defaultValue, helpString)
const char * options
void FuzzRegionSetPath(Fuzz *fuzz)
static void fuzz_path_deserialize(const sk_sp< SkData > &)
Definition FuzzMain.cpp:759
bool FuzzSKSL2SPIRV(const uint8_t *data, size_t size)
void FuzzSKP(const uint8_t *data, size_t size)
Definition FuzzSKP.cpp:15
static void fuzz_sksl2pipeline(const sk_sp< SkData > &)
Definition FuzzMain.cpp:855
static void fuzz_colrv1(const sk_sp< SkData > &)
Definition FuzzMain.cpp:413
bool FuzzSKSL2Metal(const uint8_t *data, size_t size)
static void fuzz_textblob_deserialize(const sk_sp< SkData > &)
Definition FuzzMain.cpp:776
static void fuzz_skp(const sk_sp< SkData > &)
Definition FuzzMain.cpp:745
static void fuzz_sksl2wgsl(const sk_sp< SkData > &)
Definition FuzzMain.cpp:875
bool FuzzImageDecode(const uint8_t *data, size_t size)
Definition FuzzImage.cpp:14
static void fuzz_sksl2glsl(const sk_sp< SkData > &)
Definition FuzzMain.cpp:835
void FuzzImageFilterDeserialize(const uint8_t *data, size_t size)
void FuzzSkDescriptorDeserialize(const uint8_t *data, size_t size)
static SkString try_auto_detect(const SkString &path, SkString *name)
Definition FuzzMain.cpp:355
static void fuzz_color_deserialize(const sk_sp< SkData > &)
Definition FuzzMain.cpp:752
bool FuzzAndroidCodec(const uint8_t *fuzzData, size_t fuzzSize, uint8_t sampleSize)
void FuzzColorspace(const uint8_t *data, size_t size)
static void fuzz_img(const sk_sp< SkData > &, uint8_t, uint8_t)
Definition FuzzMain.cpp:507
static void fuzz_skruntimeblender(const sk_sp< SkData > &)
Definition FuzzMain.cpp:805
bool FuzzIncrementalImageDecode(const uint8_t *data, size_t size)
void FuzzJSON(const uint8_t *data, size_t size)
Definition FuzzJSON.cpp:11
static std::map< std::string, std::string > cf_map
Definition FuzzMain.cpp:326
static std::map< std::string, std::string > cf_api_map
Definition FuzzMain.cpp:301
static void fuzz_skruntimeeffect(const sk_sp< SkData > &)
Definition FuzzMain.cpp:825
static void dump_png(const SkBitmap &bitmap)
Definition FuzzMain.cpp:453
static void fuzz_filter_fuzz(const sk_sp< SkData > &)
Definition FuzzMain.cpp:791
bool FuzzRegionDeserialize(const uint8_t *data, size_t size)
bool FuzzSKSL2WGSL(const uint8_t *data, size_t size)
static void fuzz_json(const sk_sp< SkData > &)
Definition FuzzMain.cpp:388
bool FuzzSkRuntimeColorFilter(const uint8_t *data, size_t size)
static void fuzz_sksl2metal(const sk_sp< SkData > &)
Definition FuzzMain.cpp:845
static void fuzz_android_codec(const sk_sp< SkData > &)
Definition FuzzMain.cpp:493
static void print_api_names()
Definition FuzzMain.cpp:432
static void fuzz_skmeshspecification(const sk_sp< SkData > &)
Definition FuzzMain.cpp:798
void FuzzSkMeshSpecification(const uint8_t *fuzzData, size_t fuzzSize)
bool FuzzSKSL2Pipeline(const uint8_t *data, size_t size)
static void fuzz_api(const sk_sp< SkData > &, const SkString &name)
Definition FuzzMain.cpp:439
static void fuzz_skdescriptor_deserialize(const sk_sp< SkData > &)
Definition FuzzMain.cpp:885
bool FuzzSkRuntimeBlender(const uint8_t *data, size_t size)
static constexpr char g_type_message[]
Definition FuzzMain.cpp:45
static uint8_t calculate_option(SkData *)
Definition FuzzMain.cpp:423
static void fuzz_region_deserialize(const sk_sp< SkData > &)
Definition FuzzMain.cpp:766
static void fuzz_animated_img(const sk_sp< SkData > &)
Definition FuzzMain.cpp:463
bool FuzzSKSL2GLSL(const uint8_t *data, size_t size)
void FuzzTextBlobDeserialize(const uint8_t *data, size_t size)
static int fuzz_file(const SkString &path, SkString type)
Definition FuzzMain.cpp:157
static void fuzz_image_decode(const sk_sp< SkData > &)
Definition FuzzMain.cpp:473
void FuzzCOLRv1(const uint8_t *data, size_t size)
static void fuzz_sksl2spirv(const sk_sp< SkData > &)
Definition FuzzMain.cpp:865
bool FuzzSkRuntimeEffect(const uint8_t *data, size_t size)
void FuzzPathDeserialize(const uint8_t *data, size_t size)
bool FuzzAnimatedImage(const uint8_t *data, size_t size)
static void fuzz_skruntimecolorfilter(const sk_sp< SkData > &)
Definition FuzzMain.cpp:815
static void fuzz_image_decode_incremental(const sk_sp< SkData > &)
Definition FuzzMain.cpp:483
static void fuzz_region_set_path(const sk_sp< SkData > &)
Definition FuzzMain.cpp:783
void FuzzSkottieJSON(const uint8_t *data, size_t size)
static constexpr T SkAlign2(T x)
Definition SkAlign.h:15
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
bool sk_isdir(const char *path)
#define SkScalarRoundToInt(x)
Definition SkScalar.h:37
static void dump(const float m[20], SkYUVColorSpace cs, bool rgb2yuv)
#define W
Definition aaa.cpp:17
static void PrintUsage()
static void Parse(int argc, const char *const *argv)
static void SetUsage(const char *usage)
Definition Fuzz.h:24
size_t remainingSize() const
Definition Fuzz.h:47
const uint8_t * remainingData() const
Definition Fuzz.h:51
void nextRange(T *, Min, Max)
Definition Fuzz.h:119
bool installPixels(const SkImageInfo &info, void *pixels, size_t rowBytes, void(*releaseProc)(void *addr, void *context), void *context)
Definition SkBitmap.cpp:323
@ kZeroPixels_AllocFlag
zero pixel memory. No effect. This is the default.
Definition SkBitmap.h:435
static std::unique_ptr< SkCodec > MakeFromData(sk_sp< SkData >, SkSpan< const SkCodecs::Decoder > decoders, SkPngChunkReader *=nullptr)
Definition SkCodec.cpp:241
@ kYes_ZeroInitialized
Definition SkCodec.h:308
@ kTopDown_SkScanlineOrder
Definition SkCodec.h:581
@ kInvalidConversion
Definition SkCodec.h:96
@ kIncompleteInput
Definition SkCodec.h:84
@ kUnimplemented
Definition SkCodec.h:123
@ kSuccess
Definition SkCodec.h:80
@ kErrorInInput
Definition SkCodec.h:91
const uint8_t * bytes() const
Definition SkData.h:43
static sk_sp< SkData > MakeFromFileName(const char path[])
Definition SkData.cpp:148
size_t size() const
Definition SkData.h:30
SK_SPI bool next(SkString *name, bool getDir=false)
static SkString Join(const char *rootPath, const char *relativePath)
Definition SkOSPath.cpp:14
T * get() const
Definition SkRefCnt.h:303
#define H
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition main.cc:19
static bool b
GAsyncResult * result
const char * name
Definition fuchsia.cc:50
char ** argv
Definition library.h:9
double y
double x
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
void UsePortableFontMgr()
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switches.h:41
Definition main.py:1
SkScalar w
SkScalar h
int32_t height
const Scalar scale
Definition SkMD5.cpp:130
const SkIRect * fSubset
Definition SkCodec.h:347
ZeroInitialized fZeroInitialized
Definition SkCodec.h:329
void setXYWH(int32_t x, int32_t y, int32_t width, int32_t height)
Definition SkRect.h:268
SkImageInfo makeWH(int newWidth, int newHeight) const
size_t minRowBytes() const
SkImageInfo makeDimensions(SkISize newSize) const
int width() const
int height() const