mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "fuzz/Fuzz.h" |
| 9 | #include "include/codec/SkCodec.h" |
Kevin Lubick | 87a2e79 | 2023-03-23 15:06:36 -0400 | [diff] [blame] | 10 | #include "include/core/SkBitmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkCanvas.h" |
| 12 | #include "include/core/SkData.h" |
Kevin Lubick | 1e97119 | 2023-11-10 16:14:44 -0500 | [diff] [blame] | 13 | #include "include/core/SkFontMgr.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/core/SkImage.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #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 Lubick | f48d2b0 | 2023-04-11 18:37:34 -0400 | [diff] [blame] | 21 | #include "include/encode/SkPngEncoder.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/core/SkOSFile.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/core/SkReadBuffer.h" |
| 24 | #include "src/utils/SkOSPath.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "tools/ToolUtils.h" |
Kevin Lubick | d7255a7 | 2019-06-03 11:59:23 -0400 | [diff] [blame] | 26 | #include "tools/flags/CommandLineFlags.h" |
Kevin Lubick | 1e97119 | 2023-11-10 16:14:44 -0500 | [diff] [blame] | 27 | #include "tools/fonts/FontToolUtils.h" |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 28 | |
Hal Canary | 62176db | 2017-02-27 16:42:03 -0500 | [diff] [blame] | 29 | #include <iostream> |
Kevin Lubick | acd456a | 2018-04-24 13:58:16 -0400 | [diff] [blame] | 30 | #include <map> |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 31 | #include <regex> |
mtklein | a115942 | 2016-01-15 05:46:54 -0800 | [diff] [blame] | 32 | #include <signal.h> |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 33 | |
Mike Klein | 84836b7 | 2019-03-21 11:31:36 -0500 | [diff] [blame] | 34 | static 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."); |
| 38 | static DEFINE_string2(name, n, "", "If --type is 'api', fuzz the API with this name."); |
| 39 | static DEFINE_string2(dump, d, "", "If not empty, dump 'image*' or 'skp' types as a " |
| 40 | "PNG with this name."); |
Brian Osman | f4e5737 | 2020-07-17 11:37:14 -0400 | [diff] [blame] | 41 | static DEFINE_int(loops, 1, "Run the fuzzer on each input this many times."); |
Kevin Lubick | 9ff5dc9 | 2018-01-09 12:47:33 -0500 | [diff] [blame] | 42 | DEFINE_bool2(verbose, v, false, "Print more information while fuzzing."); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 43 | |
| 44 | // This cannot be inlined in DEFINE_string2 due to interleaved ifdefs |
| 45 | static constexpr char g_type_message[] = "How to interpret --bytes, one of:\n" |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 46 | "android_codec\n" |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 47 | "animated_image_decode\n" |
| 48 | "api\n" |
| 49 | "color_deserialize\n" |
Dominik Röttsches | fa37f3c | 2022-08-22 16:48:42 +0300 | [diff] [blame] | 50 | "colrv1\n" |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 51 | "filter_fuzz (equivalent to Chrome's filter_fuzz_stub)\n" |
| 52 | "image_decode\n" |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 53 | "image_decode_incremental\n" |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 54 | "image_mode\n" |
| 55 | "image_scale\n" |
Florin Malita | 80452be | 2018-06-19 11:27:20 -0400 | [diff] [blame] | 56 | "json\n" |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 57 | "path_deserialize\n" |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 58 | "region_deserialize\n" |
| 59 | "region_set_path\n" |
Kevin Lubick | 2be14d3 | 2019-10-21 13:44:48 -0400 | [diff] [blame] | 60 | "skdescriptor_deserialize\n" |
John Stiles | f560952 | 2022-10-07 13:14:33 -0400 | [diff] [blame] | 61 | "skmeshspecialization\n" |
John Stiles | b27d63a | 2023-10-06 14:42:54 -0400 | [diff] [blame] | 62 | #if defined(SK_ENABLE_SKOTTIE) |
| 63 | "skottie_json\n" |
| 64 | #endif |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 65 | "skp\n" |
John Stiles | 54fd78d | 2023-10-06 14:23:07 -0400 | [diff] [blame] | 66 | "skruntimeblender\n" |
John Stiles | 3dfb0fb | 2023-10-06 14:23:04 -0400 | [diff] [blame] | 67 | "skruntimecolorfilter\n" |
Zepeng Hu | a5783f3 | 2020-07-10 13:36:20 +0000 | [diff] [blame] | 68 | "skruntimeeffect\n" |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 69 | "sksl2glsl\n" |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 70 | "sksl2metal\n" |
Kevin Lubick | 0f0a710 | 2019-03-18 16:20:55 -0400 | [diff] [blame] | 71 | "sksl2pipeline\n" |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 72 | "sksl2spirv\n" |
John Stiles | b27d63a | 2023-10-06 14:42:54 -0400 | [diff] [blame] | 73 | "sksl2wgsl\n" |
| 74 | "svg_dom\n" |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 75 | "textblob"; |
| 76 | |
Mike Klein | 84836b7 | 2019-03-21 11:31:36 -0500 | [diff] [blame] | 77 | static DEFINE_string2(type, t, "", g_type_message); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 78 | |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 79 | static int fuzz_file(const SkString& path, SkString type); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 80 | static uint8_t calculate_option(SkData*); |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 81 | static SkString try_auto_detect(const SkString& path, SkString* name); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 82 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 83 | static void fuzz_android_codec(const sk_sp<SkData>&); |
| 84 | static void fuzz_animated_img(const sk_sp<SkData>&); |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 85 | static void fuzz_api(const sk_sp<SkData>&, const SkString& name); |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 86 | static void fuzz_color_deserialize(const sk_sp<SkData>&); |
| 87 | static void fuzz_colrv1(const sk_sp<SkData>&); |
| 88 | static void fuzz_filter_fuzz(const sk_sp<SkData>&); |
| 89 | static void fuzz_image_decode(const sk_sp<SkData>&); |
| 90 | static void fuzz_image_decode_incremental(const sk_sp<SkData>&); |
| 91 | static void fuzz_img(const sk_sp<SkData>&, uint8_t, uint8_t); |
| 92 | static void fuzz_json(const sk_sp<SkData>&); |
| 93 | static void fuzz_path_deserialize(const sk_sp<SkData>&); |
| 94 | static void fuzz_region_deserialize(const sk_sp<SkData>&); |
| 95 | static void fuzz_region_set_path(const sk_sp<SkData>&); |
| 96 | static void fuzz_skdescriptor_deserialize(const sk_sp<SkData>&); |
| 97 | static void fuzz_skmeshspecification(const sk_sp<SkData>&); |
| 98 | static void fuzz_skp(const sk_sp<SkData>&); |
| 99 | static void fuzz_skruntimeblender(const sk_sp<SkData>&); |
| 100 | static void fuzz_skruntimecolorfilter(const sk_sp<SkData>&); |
| 101 | static void fuzz_skruntimeeffect(const sk_sp<SkData>&); |
| 102 | static void fuzz_sksl2glsl(const sk_sp<SkData>&); |
| 103 | static void fuzz_sksl2metal(const sk_sp<SkData>&); |
| 104 | static void fuzz_sksl2pipeline(const sk_sp<SkData>&); |
| 105 | static void fuzz_sksl2spirv(const sk_sp<SkData>&); |
| 106 | static void fuzz_sksl2wgsl(const sk_sp<SkData>&); |
| 107 | static void fuzz_textblob_deserialize(const sk_sp<SkData>&); |
Herb Derby | a839fc0 | 2017-03-16 12:30:43 -0400 | [diff] [blame] | 108 | |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 109 | static void print_api_names(); |
| 110 | |
Robert Phillips | 2af13c1 | 2021-09-01 16:47:01 +0000 | [diff] [blame] | 111 | #if defined(SK_ENABLE_SVG) |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 112 | static void fuzz_svg_dom(const sk_sp<SkData>&); |
Robert Phillips | 2af13c1 | 2021-09-01 16:47:01 +0000 | [diff] [blame] | 113 | #endif |
| 114 | |
Florin Malita | 0b0d93d | 2018-05-04 15:01:03 -0400 | [diff] [blame] | 115 | #if defined(SK_ENABLE_SKOTTIE) |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 116 | static void fuzz_skottie_json(const sk_sp<SkData>&); |
Florin Malita | 0b0d93d | 2018-05-04 15:01:03 -0400 | [diff] [blame] | 117 | #endif |
| 118 | |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 119 | int main(int argc, char** argv) { |
Mike Klein | 88544fb | 2019-03-20 10:50:33 -0500 | [diff] [blame] | 120 | 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 Lubick | 1e97119 | 2023-11-10 16:14:44 -0500 | [diff] [blame] | 126 | ToolUtils::UsePortableFontMgr(); |
mtklein | f5e9782 | 2016-01-15 06:19:53 -0800 | [diff] [blame] | 127 | |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 128 | SkString path = SkString(FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0]); |
| 129 | SkString type = SkString(FLAGS_type.isEmpty() ? "" : FLAGS_type[0]); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 130 | |
Brian Osman | f4e5737 | 2020-07-17 11:37:14 -0400 | [diff] [blame] | 131 | int loopCount = std::max(FLAGS_loops, 1); |
| 132 | |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 133 | if (!sk_isdir(path.c_str())) { |
Brian Osman | f4e5737 | 2020-07-17 11:37:14 -0400 | [diff] [blame] | 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; |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 141 | } |
| 142 | |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 143 | SkOSFile::Iter it(path.c_str()); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 144 | for (SkString file; it.next(&file); ) { |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 145 | SkString p = SkOSPath::Join(path.c_str(), file.c_str()); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 146 | SkDebugf("Fuzzing %s\n", p.c_str()); |
Brian Osman | f4e5737 | 2020-07-17 11:37:14 -0400 | [diff] [blame] | 147 | for (int i = 0; i < loopCount; ++i) { |
| 148 | int rv = fuzz_file(p, type); |
| 149 | if (rv != 0) { |
| 150 | return rv; |
| 151 | } |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | return 0; |
| 155 | } |
| 156 | |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 157 | static int fuzz_file(const SkString& path, SkString type) { |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 158 | sk_sp<SkData> bytes(SkData::MakeFromFileName(path.c_str())); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 159 | if (!bytes) { |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 160 | SkDebugf("Could not read %s\n", path.c_str()); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 161 | return 1; |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 162 | } |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 163 | |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 164 | SkString name = SkString(FLAGS_name.isEmpty() ? "" : FLAGS_name[0]); |
| 165 | |
| 166 | if (type.isEmpty()) { |
| 167 | type = try_auto_detect(path, &name); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 168 | } |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 169 | |
| 170 | if (type.isEmpty()) { |
| 171 | SkDebugf("Could not autodetect type of %s\n", path.c_str()); |
| 172 | return 1; |
| 173 | } |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 174 | if (type.equals("android_codec")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 175 | fuzz_android_codec(std::move(bytes)); |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 176 | return 0; |
| 177 | } |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 178 | if (type.equals("animated_image_decode")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 179 | fuzz_animated_img(std::move(bytes)); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | if (type.equals("api")) { |
| 183 | fuzz_api(bytes, name); |
| 184 | return 0; |
| 185 | } |
| 186 | if (type.equals("color_deserialize")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 187 | fuzz_color_deserialize(std::move(bytes)); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 188 | return 0; |
| 189 | } |
Dominik Röttsches | fa37f3c | 2022-08-22 16:48:42 +0300 | [diff] [blame] | 190 | if (type.equals("colrv1")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 191 | fuzz_colrv1(std::move(bytes)); |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 192 | return 0; |
Dominik Röttsches | fa37f3c | 2022-08-22 16:48:42 +0300 | [diff] [blame] | 193 | } |
Florin Malita | 80452be | 2018-06-19 11:27:20 -0400 | [diff] [blame] | 194 | if (type.equals("filter_fuzz")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 195 | fuzz_filter_fuzz(std::move(bytes)); |
Florin Malita | 80452be | 2018-06-19 11:27:20 -0400 | [diff] [blame] | 196 | return 0; |
| 197 | } |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 198 | if (type.equals("image_decode")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 199 | fuzz_image_decode(std::move(bytes)); |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | if (type.equals("image_decode_incremental")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 203 | fuzz_image_decode_incremental(std::move(bytes)); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | if (type.equals("image_scale")) { |
| 207 | uint8_t option = calculate_option(bytes.get()); |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 208 | fuzz_img(std::move(bytes), option, 0); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 209 | return 0; |
| 210 | } |
| 211 | if (type.equals("image_mode")) { |
| 212 | uint8_t option = calculate_option(bytes.get()); |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 213 | fuzz_img(std::move(bytes), 0, option); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 214 | return 0; |
| 215 | } |
Florin Malita | 80452be | 2018-06-19 11:27:20 -0400 | [diff] [blame] | 216 | if (type.equals("json")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 217 | fuzz_json(std::move(bytes)); |
Kevin Lubick | 9eeede2 | 2018-05-03 16:26:10 -0400 | [diff] [blame] | 218 | return 0; |
| 219 | } |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 220 | if (type.equals("path_deserialize")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 221 | fuzz_path_deserialize(std::move(bytes)); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 222 | return 0; |
| 223 | } |
| 224 | if (type.equals("region_deserialize")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 225 | fuzz_region_deserialize(std::move(bytes)); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | if (type.equals("region_set_path")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 229 | fuzz_region_set_path(std::move(bytes)); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 230 | return 0; |
| 231 | } |
| 232 | if (type.equals("pipe")) { |
Mike Klein | 60900b5 | 2018-09-21 11:19:45 -0400 | [diff] [blame] | 233 | SkDebugf("I would prefer not to.\n"); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 234 | return 0; |
| 235 | } |
Kevin Lubick | 2be14d3 | 2019-10-21 13:44:48 -0400 | [diff] [blame] | 236 | if (type.equals("skdescriptor_deserialize")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 237 | fuzz_skdescriptor_deserialize(std::move(bytes)); |
Kevin Lubick | 2be14d3 | 2019-10-21 13:44:48 -0400 | [diff] [blame] | 238 | return 0; |
| 239 | } |
Florin Malita | 0b0d93d | 2018-05-04 15:01:03 -0400 | [diff] [blame] | 240 | #if defined(SK_ENABLE_SKOTTIE) |
Kevin Lubick | 9eeede2 | 2018-05-03 16:26:10 -0400 | [diff] [blame] | 241 | if (type.equals("skottie_json")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 242 | fuzz_skottie_json(std::move(bytes)); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 243 | return 0; |
| 244 | } |
Florin Malita | 0b0d93d | 2018-05-04 15:01:03 -0400 | [diff] [blame] | 245 | #endif |
John Stiles | f560952 | 2022-10-07 13:14:33 -0400 | [diff] [blame] | 246 | if (type.equals("skmeshspecification")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 247 | fuzz_skmeshspecification(std::move(bytes)); |
John Stiles | f560952 | 2022-10-07 13:14:33 -0400 | [diff] [blame] | 248 | return 0; |
| 249 | } |
Kevin Lubick | 9eeede2 | 2018-05-03 16:26:10 -0400 | [diff] [blame] | 250 | if (type.equals("skp")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 251 | fuzz_skp(std::move(bytes)); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 252 | return 0; |
| 253 | } |
John Stiles | 54fd78d | 2023-10-06 14:23:07 -0400 | [diff] [blame] | 254 | if (type.equals("skruntimeblender")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 255 | fuzz_skruntimeblender(std::move(bytes)); |
John Stiles | 54fd78d | 2023-10-06 14:23:07 -0400 | [diff] [blame] | 256 | return 0; |
| 257 | } |
John Stiles | 3dfb0fb | 2023-10-06 14:23:04 -0400 | [diff] [blame] | 258 | if (type.equals("skruntimecolorfilter")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 259 | fuzz_skruntimecolorfilter(std::move(bytes)); |
John Stiles | 3dfb0fb | 2023-10-06 14:23:04 -0400 | [diff] [blame] | 260 | return 0; |
| 261 | } |
Zepeng Hu | a5783f3 | 2020-07-10 13:36:20 +0000 | [diff] [blame] | 262 | if (type.equals("skruntimeeffect")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 263 | fuzz_skruntimeeffect(std::move(bytes)); |
Zepeng Hu | a5783f3 | 2020-07-10 13:36:20 +0000 | [diff] [blame] | 264 | return 0; |
| 265 | } |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 266 | if (type.equals("sksl2glsl")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 267 | fuzz_sksl2glsl(std::move(bytes)); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 268 | return 0; |
| 269 | } |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 270 | if (type.equals("sksl2metal")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 271 | fuzz_sksl2metal(std::move(bytes)); |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 272 | return 0; |
| 273 | } |
John Stiles | b27d63a | 2023-10-06 14:42:54 -0400 | [diff] [blame] | 274 | if (type.equals("sksl2pipeline")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 275 | fuzz_sksl2pipeline(std::move(bytes)); |
John Stiles | b27d63a | 2023-10-06 14:42:54 -0400 | [diff] [blame] | 276 | return 0; |
| 277 | } |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 278 | if (type.equals("sksl2spirv")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 279 | fuzz_sksl2spirv(std::move(bytes)); |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 280 | return 0; |
| 281 | } |
John Stiles | b27d63a | 2023-10-06 14:42:54 -0400 | [diff] [blame] | 282 | if (type.equals("sksl2wgsl")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 283 | fuzz_sksl2wgsl(std::move(bytes)); |
Kevin Lubick | 0f0a710 | 2019-03-18 16:20:55 -0400 | [diff] [blame] | 284 | return 0; |
| 285 | } |
Robert Phillips | 2af13c1 | 2021-09-01 16:47:01 +0000 | [diff] [blame] | 286 | #if defined(SK_ENABLE_SVG) |
Zepeng Hu | edaf302 | 2020-06-12 12:20:59 +0000 | [diff] [blame] | 287 | if (type.equals("svg_dom")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 288 | fuzz_svg_dom(std::move(bytes)); |
Zepeng Hu | edaf302 | 2020-06-12 12:20:59 +0000 | [diff] [blame] | 289 | return 0; |
| 290 | } |
Robert Phillips | 2af13c1 | 2021-09-01 16:47:01 +0000 | [diff] [blame] | 291 | #endif |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 292 | if (type.equals("textblob")) { |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 293 | fuzz_textblob_deserialize(std::move(bytes)); |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 294 | return 0; |
| 295 | } |
Kevin Lubick | 457fa97 | 2018-05-29 09:22:06 -0400 | [diff] [blame] | 296 | SkDebugf("Unknown type %s\n", type.c_str()); |
Mike Klein | 88544fb | 2019-03-20 10:50:33 -0500 | [diff] [blame] | 297 | CommandLineFlags::PrintUsage(); |
Kevin Lubick | 9ff5dc9 | 2018-01-09 12:47:33 -0500 | [diff] [blame] | 298 | return 1; |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 301 | static std::map<std::string, std::string> cf_api_map = { |
Zepeng Hu | 9400701 | 2020-07-22 14:37:46 +0000 | [diff] [blame] | 302 | {"api_create_ddl", "CreateDDL"}, |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 303 | {"api_draw_functions", "DrawFunctions"}, |
Adlai Holler | ccb6866 | 2021-02-25 10:28:44 -0500 | [diff] [blame] | 304 | {"api_ddl_threading", "DDLThreadingGL"}, |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 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"}, |
Kevin Lubick | de54d7c | 2018-09-24 08:48:19 -0400 | [diff] [blame] | 310 | {"api_pathop", "Pathop"}, |
Kevin Lubick | 57507f1 | 2018-10-11 09:35:15 -0400 | [diff] [blame] | 311 | {"api_polyutils", "PolyUtils"}, |
Robert Phillips | 1f175b7 | 2023-07-18 16:29:13 -0400 | [diff] [blame] | 312 | #if defined(SK_GRAPHITE) && defined(SK_ENABLE_PRECOMPILE) |
| 313 | {"api_precompile", "Precompile"}, |
| 314 | #endif |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 315 | {"api_raster_n32_canvas", "RasterN32Canvas"}, |
Weston Tracey | 1a771fe | 2021-02-04 11:09:59 -0500 | [diff] [blame] | 316 | {"api_skparagraph", "SkParagraph"}, |
Zepeng Hu | ba7cbf7 | 2020-07-01 13:21:03 +0000 | [diff] [blame] | 317 | {"api_svg_canvas", "SVGCanvas"}, |
Kevin Lubick | 876b045 | 2023-01-24 11:04:37 -0500 | [diff] [blame] | 318 | {"cubic_quad_roots", "CubicQuadRoots"}, |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 319 | {"jpeg_encoder", "JPEGEncoder"}, |
| 320 | {"png_encoder", "PNGEncoder"}, |
Kevin Lubick | f84ded2 | 2018-10-23 09:28:48 -0400 | [diff] [blame] | 321 | {"skia_pathop_fuzzer", "LegacyChromiumPathop"}, |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 322 | {"webp_encoder", "WEBPEncoder"} |
| 323 | }; |
| 324 | |
Kevin Lubick | 6c56055 | 2018-06-20 09:29:23 -0400 | [diff] [blame] | 325 | // maps clusterfuzz/oss-fuzz -> Skia's name |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 326 | static std::map<std::string, std::string> cf_map = { |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 327 | {"android_codec", "android_codec"}, |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 328 | {"animated_image_decode", "animated_image_decode"}, |
Kevin Lubick | e3ec13b | 2022-10-28 10:05:22 -0400 | [diff] [blame] | 329 | {"colrv1", "colrv1"}, |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 330 | {"image_decode", "image_decode"}, |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 331 | {"image_decode_incremental", "image_decode_incremental"}, |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 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"}, |
Kevin Lubick | 2be14d3 | 2019-10-21 13:44:48 -0400 | [diff] [blame] | 337 | {"skdescriptor_deserialize", "skdescriptor_deserialize"}, |
Kevin Lubick | 6c56055 | 2018-06-20 09:29:23 -0400 | [diff] [blame] | 338 | {"skjson", "json"}, |
John Stiles | f560952 | 2022-10-07 13:14:33 -0400 | [diff] [blame] | 339 | {"skmeshspecification", "skmeshspecification"}, |
Zepeng Hu | fcb7ba0 | 2020-07-31 17:21:29 +0000 | [diff] [blame] | 340 | {"skp", "skp"}, |
Zepeng Hu | a5783f3 | 2020-07-10 13:36:20 +0000 | [diff] [blame] | 341 | {"skruntimeeffect", "skruntimeeffect"}, |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 342 | {"sksl2glsl", "sksl2glsl"}, |
| 343 | {"sksl2metal", "sksl2metal"}, |
| 344 | {"sksl2spirv", "sksl2spirv"}, |
Kevin Lubick | 0f0a710 | 2019-03-18 16:20:55 -0400 | [diff] [blame] | 345 | {"sksl2pipeline", "sksl2pipeline"}, |
Kevin Lubick | de54d7c | 2018-09-24 08:48:19 -0400 | [diff] [blame] | 346 | #if defined(SK_ENABLE_SKOTTIE) |
| 347 | {"skottie_json", "skottie_json"}, |
| 348 | #endif |
Robert Phillips | 2af13c1 | 2021-09-01 16:47:01 +0000 | [diff] [blame] | 349 | #if defined(SK_ENABLE_SVG) |
Zepeng Hu | edaf302 | 2020-06-12 12:20:59 +0000 | [diff] [blame] | 350 | {"svg_dom", "svg_dom"}, |
Robert Phillips | 2af13c1 | 2021-09-01 16:47:01 +0000 | [diff] [blame] | 351 | #endif |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 352 | {"textblob_deserialize", "textblob"} |
| 353 | }; |
| 354 | |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 355 | static SkString try_auto_detect(const SkString& path, SkString* name) { |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 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); |
Kevin Lubick | 6c56055 | 2018-06-20 09:29:23 -0400 | [diff] [blame] | 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"); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 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 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 383 | return SkString(); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 384 | } |
| 385 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 386 | void FuzzJSON(const uint8_t *data, size_t size); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 387 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 388 | static void fuzz_json(const sk_sp<SkData>& data){ |
| 389 | FuzzJSON(data->bytes(), data->size()); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 390 | SkDebugf("[terminated] Done parsing!\n"); |
| 391 | } |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 392 | |
Florin Malita | 0b0d93d | 2018-05-04 15:01:03 -0400 | [diff] [blame] | 393 | #if defined(SK_ENABLE_SKOTTIE) |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 394 | void FuzzSkottieJSON(const uint8_t *data, size_t size); |
Kevin Lubick | 9eeede2 | 2018-05-03 16:26:10 -0400 | [diff] [blame] | 395 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 396 | static void fuzz_skottie_json(const sk_sp<SkData>& data){ |
| 397 | FuzzSkottieJSON(data->bytes(), data->size()); |
Kevin Lubick | 9eeede2 | 2018-05-03 16:26:10 -0400 | [diff] [blame] | 398 | SkDebugf("[terminated] Done animating!\n"); |
| 399 | } |
Florin Malita | 0b0d93d | 2018-05-04 15:01:03 -0400 | [diff] [blame] | 400 | #endif |
Kevin Lubick | 9eeede2 | 2018-05-03 16:26:10 -0400 | [diff] [blame] | 401 | |
Robert Phillips | 2af13c1 | 2021-09-01 16:47:01 +0000 | [diff] [blame] | 402 | #if defined(SK_ENABLE_SVG) |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 403 | void FuzzSVG(const uint8_t *data, size_t size); |
Robert Phillips | 2af13c1 | 2021-09-01 16:47:01 +0000 | [diff] [blame] | 404 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 405 | static void fuzz_svg_dom(const sk_sp<SkData>& data){ |
| 406 | FuzzSVG(data->bytes(), data->size()); |
Zepeng Hu | edaf302 | 2020-06-12 12:20:59 +0000 | [diff] [blame] | 407 | SkDebugf("[terminated] Done DOM!\n"); |
| 408 | } |
Robert Phillips | 2af13c1 | 2021-09-01 16:47:01 +0000 | [diff] [blame] | 409 | #endif |
Zepeng Hu | edaf302 | 2020-06-12 12:20:59 +0000 | [diff] [blame] | 410 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 411 | void FuzzCOLRv1(const uint8_t* data, size_t size); |
Dominik Röttsches | fa37f3c | 2022-08-22 16:48:42 +0300 | [diff] [blame] | 412 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 413 | static void fuzz_colrv1(const sk_sp<SkData>& data) { |
| 414 | FuzzCOLRv1(data->bytes(), data->size()); |
Dominik Röttsches | fa37f3c | 2022-08-22 16:48:42 +0300 | [diff] [blame] | 415 | SkDebugf("[terminated] Done COLRv1!\n"); |
| 416 | } |
| 417 | |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 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 (%). |
| 423 | static 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 Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 432 | static void print_api_names(){ |
| 433 | SkDebugf("When using --type api, please choose an API to fuzz with --name/-n:\n"); |
Hal Canary | 972eba3 | 2018-07-30 17:07:07 -0400 | [diff] [blame] | 434 | for (const Fuzzable& fuzzable : sk_tools::Registry<Fuzzable>::Range()) { |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 435 | SkDebugf("\t%s\n", fuzzable.name); |
| 436 | } |
| 437 | } |
| 438 | |
John Stiles | 04a9f72 | 2023-10-19 11:46:34 -0400 | [diff] [blame] | 439 | static void fuzz_api(const sk_sp<SkData>& data, const SkString& name) { |
Hal Canary | 972eba3 | 2018-07-30 17:07:07 -0400 | [diff] [blame] | 440 | for (const Fuzzable& fuzzable : sk_tools::Registry<Fuzzable>::Range()) { |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 441 | if (name.equals(fuzzable.name)) { |
mtklein | f5e9782 | 2016-01-15 06:19:53 -0800 | [diff] [blame] | 442 | SkDebugf("Fuzzing %s...\n", fuzzable.name); |
John Stiles | cf747b0 | 2023-10-18 10:45:37 -0400 | [diff] [blame] | 443 | Fuzz fuzz(data->bytes(), data->size()); |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 444 | fuzzable.fn(&fuzz); |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 445 | SkDebugf("[terminated] Success!\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 446 | return; |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 447 | } |
| 448 | } |
mtklein | d4387ea | 2016-01-21 06:13:52 -0800 | [diff] [blame] | 449 | |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 450 | print_api_names(); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 451 | } |
| 452 | |
John Stiles | b0af618 | 2023-10-19 11:43:46 -0400 | [diff] [blame] | 453 | static void dump_png(const SkBitmap& bitmap) { |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 454 | if (!FLAGS_dump.isEmpty()) { |
Kevin Lubick | f48d2b0 | 2023-04-11 18:37:34 -0400 | [diff] [blame] | 455 | SkFILEWStream file(FLAGS_dump[0]); |
| 456 | SkPngEncoder::Encode(&file, bitmap.pixmap(), {}); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 457 | SkDebugf("Dumped to %s\n", FLAGS_dump[0]); |
| 458 | } |
| 459 | } |
| 460 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 461 | bool FuzzAnimatedImage(const uint8_t *data, size_t size); |
Kevin Lubick | 2416f96 | 2018-02-12 08:26:39 -0500 | [diff] [blame] | 462 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 463 | static void fuzz_animated_img(const sk_sp<SkData>& data) { |
| 464 | if (FuzzAnimatedImage(data->bytes(), data->size())) { |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 465 | SkDebugf("[terminated] Success from decoding/drawing animated image!\n"); |
| 466 | return; |
| 467 | } |
| 468 | SkDebugf("[terminated] Could not decode or draw animated image.\n"); |
Kevin Lubick | 2416f96 | 2018-02-12 08:26:39 -0500 | [diff] [blame] | 469 | } |
| 470 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 471 | bool FuzzImageDecode(const uint8_t *data, size_t size); |
Kevin Lubick | 2416f96 | 2018-02-12 08:26:39 -0500 | [diff] [blame] | 472 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 473 | static void fuzz_image_decode(const sk_sp<SkData>& data) { |
| 474 | if (FuzzImageDecode(data->bytes(), data->size())) { |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 475 | SkDebugf("[terminated] Success from decoding/drawing image!\n"); |
| 476 | return; |
| 477 | } |
| 478 | SkDebugf("[terminated] Could not decode or draw image.\n"); |
Kevin Lubick | 2416f96 | 2018-02-12 08:26:39 -0500 | [diff] [blame] | 479 | } |
| 480 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 481 | bool FuzzIncrementalImageDecode(const uint8_t *data, size_t size); |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 482 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 483 | static void fuzz_image_decode_incremental(const sk_sp<SkData>& data) { |
| 484 | if (FuzzIncrementalImageDecode(data->bytes(), data->size())) { |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 485 | SkDebugf("[terminated] Success using incremental decode!\n"); |
| 486 | return; |
| 487 | } |
| 488 | SkDebugf("[terminated] Could not incrementally decode and image.\n"); |
| 489 | } |
| 490 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 491 | bool FuzzAndroidCodec(const uint8_t *fuzzData, size_t fuzzSize, uint8_t sampleSize); |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 492 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 493 | static void fuzz_android_codec(const sk_sp<SkData>& data) { |
John Stiles | cf747b0 | 2023-10-18 10:45:37 -0400 | [diff] [blame] | 494 | Fuzz fuzz(data->bytes(), data->size()); |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 495 | uint8_t sampleSize; |
| 496 | fuzz.nextRange(&sampleSize, 1, 64); |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 497 | if (FuzzAndroidCodec(fuzz.remainingData(), fuzz.remainingSize(), sampleSize)) { |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 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. |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 507 | static void fuzz_img(const sk_sp<SkData>& bytes, uint8_t scale, uint8_t mode) { |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 508 | // We can scale 1x, 2x, 4x, 8x, 16x |
| 509 | scale = scale % 5; |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 510 | float fscale = (float)pow(2.0f, scale); |
| 511 | SkDebugf("Scaling factor: %f\n", fscale); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 512 | |
Leon Scroggins III | c5a8366 | 2016-12-08 09:07:56 -0500 | [diff] [blame] | 513 | // We have 5 different modes of decoding. |
| 514 | mode = mode % 5; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 515 | SkDebugf("Mode: %d\n", mode); |
| 516 | |
| 517 | // This is mostly copied from DMSrcSink's CodecSrc::draw method. |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 518 | SkDebugf("Decoding\n"); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 519 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(bytes)); |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 520 | if (nullptr == codec) { |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 521 | SkDebugf("[terminated] Couldn't create codec.\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 522 | return; |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | SkImageInfo decodeInfo = codec->getInfo(); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 526 | SkISize size = codec->getScaledDimensions(fscale); |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 527 | decodeInfo = decodeInfo.makeDimensions(size); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 528 | |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 529 | SkBitmap bitmap; |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 530 | SkCodec::Options options; |
| 531 | options.fZeroInitialized = SkCodec::kYes_ZeroInitialized; |
| 532 | |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 533 | if (!bitmap.tryAllocPixelsFlags(decodeInfo, SkBitmap::kZeroPixels_AllocFlag)) { |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 534 | SkDebugf("[terminated] Could not allocate memory. Image might be too large (%d x %d)", |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 535 | decodeInfo.width(), decodeInfo.height()); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 536 | return; |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 537 | } |
| 538 | |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 539 | switch (mode) { |
| 540 | case 0: {//kCodecZeroInit_Mode, kCodec_Mode |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 541 | switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options)) { |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 542 | case SkCodec::kSuccess: |
| 543 | SkDebugf("[terminated] Success!\n"); |
| 544 | break; |
| 545 | case SkCodec::kIncompleteInput: |
| 546 | SkDebugf("[terminated] Partial Success\n"); |
| 547 | break; |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 548 | case SkCodec::kErrorInInput: |
| 549 | SkDebugf("[terminated] Partial Success with error\n"); |
| 550 | break; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 551 | 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 Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 555 | break; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 556 | default: |
| 557 | SkDebugf("[terminated] Couldn't getPixels.\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 558 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 559 | } |
| 560 | break; |
| 561 | } |
| 562 | case 1: {//kScanline_Mode |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 563 | if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)) { |
| 564 | SkDebugf("[terminated] Could not start scanline decoder\n"); |
| 565 | return; |
| 566 | } |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 567 | |
| 568 | void* dst = bitmap.getAddr(0, 0); |
| 569 | size_t rowBytes = bitmap.rowBytes(); |
| 570 | uint32_t height = decodeInfo.height(); |
Brian Osman | ea176c6 | 2018-04-06 15:28:23 -0400 | [diff] [blame] | 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); |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 574 | SkDebugf("[terminated] Success!\n"); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 575 | break; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 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 |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 585 | if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo) |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 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"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 591 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | for (int i = 0; i < numStripes; i += 2) { |
| 595 | // Skip a stripe |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 596 | const int linesToSkip = std::min(stripeHeight, height - i * stripeHeight); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 597 | codec->skipScanlines(linesToSkip); |
| 598 | |
| 599 | // Read a stripe |
| 600 | const int startY = (i + 1) * stripeHeight; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 601 | const int linesToRead = std::min(stripeHeight, height - startY); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 602 | if (linesToRead > 0) { |
| 603 | codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes()); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | // Decode even stripes |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 608 | const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 609 | if (SkCodec::kSuccess != startResult) { |
| 610 | SkDebugf("[terminated] Failed to restart scanline decoder with same parameters.\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 611 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 612 | } |
| 613 | for (int i = 0; i < numStripes; i += 2) { |
| 614 | // Read a stripe |
| 615 | const int startY = i * stripeHeight; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 616 | const int linesToRead = std::min(stripeHeight, height - startY); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 617 | codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes()); |
| 618 | |
| 619 | // Skip a stripe |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 620 | const int linesToSkip = std::min(stripeHeight, height - (i + 1) * stripeHeight); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 621 | if (linesToSkip > 0) { |
| 622 | codec->skipScanlines(linesToSkip); |
| 623 | } |
| 624 | } |
| 625 | SkDebugf("[terminated] Success!\n"); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 626 | break; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 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); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 637 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 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 = ⊂ |
| 646 | SkBitmap subsetBm; |
| 647 | // We will reuse pixel memory from bitmap. |
| 648 | void* pixels = bitmap.getPixels(); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 649 | for (int x = 0; x < W; x += w) { |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 650 | for (int y = 0; y < H; y+= h) { |
| 651 | // Do not make the subset go off the edge of the image. |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 652 | const int preScaleW = std::min(w, W - x); |
| 653 | const int preScaleH = std::min(h, H - y); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 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( |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 659 | std::max(1, SkScalarRoundToInt(preScaleW * fscale)), |
| 660 | std::max(1, SkScalarRoundToInt(preScaleH * fscale))); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 661 | size_t rowBytes = decodeInfo.minRowBytes(); |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 662 | if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes)) { |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 663 | SkDebugf("[terminated] Could not install pixels.\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 664 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 665 | } |
| 666 | const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 667 | &opts); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 668 | switch (result) { |
| 669 | case SkCodec::kSuccess: |
| 670 | case SkCodec::kIncompleteInput: |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 671 | case SkCodec::kErrorInInput: |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 672 | 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 Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 678 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 679 | } |
| 680 | // If the first subset succeeded, a later one should not fail. |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 681 | [[fallthrough]]; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 682 | 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 Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 686 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 687 | } |
| 688 | // If the first subset succeeded, why would a later one fail? |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 689 | [[fallthrough]]; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 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); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 695 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 696 | } |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 697 | } |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 698 | } |
| 699 | SkDebugf("[terminated] Success!\n"); |
| 700 | break; |
| 701 | } |
Leon Scroggins III | c5a8366 | 2016-12-08 09:07:56 -0500 | [diff] [blame] | 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 " |
John Stiles | 7bf7999 | 2021-06-25 11:05:20 -0400 | [diff] [blame] | 715 | "in frame %zu with error %d\n", i, result); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 716 | return; |
Leon Scroggins III | c5a8366 | 2016-12-08 09:07:56 -0500 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | result = codec->incrementalDecode(); |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 720 | if (result == SkCodec::kIncompleteInput || result == SkCodec::kErrorInInput) { |
Leon Scroggins III | c5a8366 | 2016-12-08 09:07:56 -0500 | [diff] [blame] | 721 | SkDebugf("okay\n"); |
| 722 | // Frames beyond this one will not decode. |
| 723 | break; |
| 724 | } |
| 725 | if (result == SkCodec::kSuccess) { |
John Stiles | 7bf7999 | 2021-06-25 11:05:20 -0400 | [diff] [blame] | 726 | SkDebugf("okay - decoded frame %zu\n", i); |
Leon Scroggins III | c5a8366 | 2016-12-08 09:07:56 -0500 | [diff] [blame] | 727 | } else { |
| 728 | SkDebugf("[terminated] incremental decode failed with " |
| 729 | "error %d\n", result); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 730 | return; |
Leon Scroggins III | c5a8366 | 2016-12-08 09:07:56 -0500 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | SkDebugf("[terminated] Success!\n"); |
| 734 | break; |
| 735 | } |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 736 | default: |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 737 | SkDebugf("[terminated] Mode not implemented yet\n"); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | dump_png(bitmap); |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 741 | } |
| 742 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 743 | void FuzzSKP(const uint8_t *data, size_t size); |
| 744 | |
| 745 | static void fuzz_skp(const sk_sp<SkData>& data) { |
| 746 | FuzzSKP(data->bytes(), data->size()); |
Zepeng Hu | fcb7ba0 | 2020-07-31 17:21:29 +0000 | [diff] [blame] | 747 | SkDebugf("[terminated] Finished SKP\n"); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 748 | } |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 749 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 750 | void FuzzColorspace(const uint8_t *data, size_t size); |
| 751 | |
| 752 | static void fuzz_color_deserialize(const sk_sp<SkData>& data) { |
| 753 | FuzzColorspace(data->bytes(), data->size()); |
Kevin Lubick | 5df5880 | 2023-10-18 08:31:15 -0400 | [diff] [blame] | 754 | SkDebugf("[terminated] Finished SkColorspace\n"); |
kjlubick | 3e3c1a5 | 2016-06-23 10:49:27 -0700 | [diff] [blame] | 755 | } |
| 756 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 757 | void FuzzPathDeserialize(const uint8_t *data, size_t size); |
Kevin Lubick | c9d2829 | 2017-11-09 08:12:56 -0500 | [diff] [blame] | 758 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 759 | static void fuzz_path_deserialize(const sk_sp<SkData>& data) { |
| 760 | FuzzPathDeserialize(data->bytes(), data->size()); |
Kevin Lubick | f034d11 | 2018-02-08 14:31:24 -0500 | [diff] [blame] | 761 | SkDebugf("[terminated] path_deserialize didn't crash!\n"); |
Kevin Lubick | f04c50a | 2017-01-06 13:48:19 -0500 | [diff] [blame] | 762 | } |
| 763 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 764 | bool FuzzRegionDeserialize(const uint8_t *data, size_t size); |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 765 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 766 | static void fuzz_region_deserialize(const sk_sp<SkData>& data) { |
| 767 | if (!FuzzRegionDeserialize(data->bytes(), data->size())) { |
Kevin Lubick | edee1ae | 2017-02-20 17:47:18 -0500 | [diff] [blame] | 768 | SkDebugf("[terminated] Couldn't initialize SkRegion.\n"); |
| 769 | return; |
| 770 | } |
Kevin Lubick | edee1ae | 2017-02-20 17:47:18 -0500 | [diff] [blame] | 771 | SkDebugf("[terminated] Success! Initialized SkRegion.\n"); |
| 772 | } |
| 773 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 774 | void FuzzTextBlobDeserialize(const uint8_t *data, size_t size); |
Kevin Lubick | f034d11 | 2018-02-08 14:31:24 -0500 | [diff] [blame] | 775 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 776 | static void fuzz_textblob_deserialize(const sk_sp<SkData>& data) { |
| 777 | FuzzTextBlobDeserialize(data->bytes(), data->size()); |
Kevin Lubick | f034d11 | 2018-02-08 14:31:24 -0500 | [diff] [blame] | 778 | SkDebugf("[terminated] textblob didn't crash!\n"); |
Kevin Lubick | d46e85f | 2017-11-21 17:13:35 -0500 | [diff] [blame] | 779 | } |
| 780 | |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 781 | void FuzzRegionSetPath(Fuzz* fuzz); |
| 782 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 783 | static void fuzz_region_set_path(const sk_sp<SkData>& data) { |
John Stiles | cf747b0 | 2023-10-18 10:45:37 -0400 | [diff] [blame] | 784 | Fuzz fuzz(data->bytes(), data->size()); |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 785 | FuzzRegionSetPath(&fuzz); |
| 786 | SkDebugf("[terminated] region_set_path didn't crash!\n"); |
| 787 | } |
| 788 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 789 | void FuzzImageFilterDeserialize(const uint8_t *data, size_t size); |
Kevin Lubick | f034d11 | 2018-02-08 14:31:24 -0500 | [diff] [blame] | 790 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 791 | static void fuzz_filter_fuzz(const sk_sp<SkData>& data) { |
| 792 | FuzzImageFilterDeserialize(data->bytes(), data->size()); |
Kevin Lubick | f034d11 | 2018-02-08 14:31:24 -0500 | [diff] [blame] | 793 | SkDebugf("[terminated] filter_fuzz didn't crash!\n"); |
Herb Derby | a839fc0 | 2017-03-16 12:30:43 -0400 | [diff] [blame] | 794 | } |
| 795 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 796 | void FuzzSkMeshSpecification(const uint8_t *fuzzData, size_t fuzzSize); |
John Stiles | f560952 | 2022-10-07 13:14:33 -0400 | [diff] [blame] | 797 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 798 | static void fuzz_skmeshspecification(const sk_sp<SkData>& data) { |
| 799 | FuzzSkMeshSpecification(data->bytes(), data->size()); |
John Stiles | f560952 | 2022-10-07 13:14:33 -0400 | [diff] [blame] | 800 | SkDebugf("[terminated] SkMeshSpecification::Make didn't crash!\n"); |
| 801 | } |
| 802 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 803 | bool FuzzSkRuntimeBlender(const uint8_t *data, size_t size); |
John Stiles | 54fd78d | 2023-10-06 14:23:07 -0400 | [diff] [blame] | 804 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 805 | static void fuzz_skruntimeblender(const sk_sp<SkData>& data) { |
| 806 | if (FuzzSkRuntimeBlender(data->bytes(), data->size())) { |
John Stiles | 54fd78d | 2023-10-06 14:23:07 -0400 | [diff] [blame] | 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 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 813 | bool FuzzSkRuntimeColorFilter(const uint8_t *data, size_t size); |
John Stiles | 3dfb0fb | 2023-10-06 14:23:04 -0400 | [diff] [blame] | 814 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 815 | static void fuzz_skruntimecolorfilter(const sk_sp<SkData>& data) { |
| 816 | if (FuzzSkRuntimeColorFilter(data->bytes(), data->size())) { |
John Stiles | 3dfb0fb | 2023-10-06 14:23:04 -0400 | [diff] [blame] | 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 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 823 | bool FuzzSkRuntimeEffect(const uint8_t *data, size_t size); |
Zepeng Hu | a5783f3 | 2020-07-10 13:36:20 +0000 | [diff] [blame] | 824 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 825 | static void fuzz_skruntimeeffect(const sk_sp<SkData>& data) { |
| 826 | if (FuzzSkRuntimeEffect(data->bytes(), data->size())) { |
John Stiles | 3dfb0fb | 2023-10-06 14:23:04 -0400 | [diff] [blame] | 827 | SkDebugf("[terminated] Success! Compiled and executed SkSL shader.\n"); |
Zepeng Hu | a5783f3 | 2020-07-10 13:36:20 +0000 | [diff] [blame] | 828 | } else { |
John Stiles | 3dfb0fb | 2023-10-06 14:23:04 -0400 | [diff] [blame] | 829 | SkDebugf("[terminated] Could not compile or execute SkSL shader.\n"); |
Zepeng Hu | a5783f3 | 2020-07-10 13:36:20 +0000 | [diff] [blame] | 830 | } |
| 831 | } |
| 832 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 833 | bool FuzzSKSL2GLSL(const uint8_t *data, size_t size); |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 834 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 835 | static void fuzz_sksl2glsl(const sk_sp<SkData>& data) { |
| 836 | if (FuzzSKSL2GLSL(data->bytes(), data->size())) { |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 837 | SkDebugf("[terminated] Success! Compiled input to GLSL.\n"); |
| 838 | } else { |
| 839 | SkDebugf("[terminated] Could not compile input to GLSL.\n"); |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 840 | } |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 841 | } |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 842 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 843 | bool FuzzSKSL2Metal(const uint8_t *data, size_t size); |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 844 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 845 | static void fuzz_sksl2metal(const sk_sp<SkData>& data) { |
| 846 | if (FuzzSKSL2Metal(data->bytes(), data->size())) { |
Kevin Lubick | 39cbe46 | 2019-03-11 15:38:00 -0400 | [diff] [blame] | 847 | SkDebugf("[terminated] Success! Compiled input to Metal.\n"); |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 848 | } else { |
Kevin Lubick | 39cbe46 | 2019-03-11 15:38:00 -0400 | [diff] [blame] | 849 | SkDebugf("[terminated] Could not compile input to Metal.\n"); |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 850 | } |
| 851 | } |
Kevin Lubick | 0f0a710 | 2019-03-18 16:20:55 -0400 | [diff] [blame] | 852 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 853 | bool FuzzSKSL2Pipeline(const uint8_t *data, size_t size); |
Kevin Lubick | 0f0a710 | 2019-03-18 16:20:55 -0400 | [diff] [blame] | 854 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 855 | static void fuzz_sksl2pipeline(const sk_sp<SkData>& data) { |
| 856 | if (FuzzSKSL2Pipeline(data->bytes(), data->size())) { |
Kevin Lubick | 0f0a710 | 2019-03-18 16:20:55 -0400 | [diff] [blame] | 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 | } |
Kevin Lubick | 2be14d3 | 2019-10-21 13:44:48 -0400 | [diff] [blame] | 862 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 863 | bool FuzzSKSL2SPIRV(const uint8_t *data, size_t size); |
John Stiles | b27d63a | 2023-10-06 14:42:54 -0400 | [diff] [blame] | 864 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 865 | static void fuzz_sksl2spirv(const sk_sp<SkData>& data) { |
| 866 | if (FuzzSKSL2SPIRV(data->bytes(), data->size())) { |
John Stiles | b27d63a | 2023-10-06 14:42:54 -0400 | [diff] [blame] | 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 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 873 | bool FuzzSKSL2WGSL(const uint8_t *data, size_t size); |
John Stiles | b27d63a | 2023-10-06 14:42:54 -0400 | [diff] [blame] | 874 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 875 | static void fuzz_sksl2wgsl(const sk_sp<SkData>& data) { |
| 876 | if (FuzzSKSL2WGSL(data->bytes(), data->size())) { |
John Stiles | b27d63a | 2023-10-06 14:42:54 -0400 | [diff] [blame] | 877 | SkDebugf("[terminated] Success! Compiled input to WGSL.\n"); |
| 878 | } else { |
| 879 | SkDebugf("[terminated] Could not compile input to WGSL.\n"); |
| 880 | } |
| 881 | } |
| 882 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 883 | void FuzzSkDescriptorDeserialize(const uint8_t *data, size_t size); |
Kevin Lubick | 2be14d3 | 2019-10-21 13:44:48 -0400 | [diff] [blame] | 884 | |
John Stiles | 89f49b1 | 2023-10-18 10:45:39 -0400 | [diff] [blame] | 885 | static void fuzz_skdescriptor_deserialize(const sk_sp<SkData>& data) { |
| 886 | FuzzSkDescriptorDeserialize(data->bytes(), data->size()); |
Kevin Lubick | 2be14d3 | 2019-10-21 13:44:48 -0400 | [diff] [blame] | 887 | SkDebugf("[terminated] Did not crash while deserializing an SkDescriptor.\n"); |
| 888 | } |