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" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkData.h" |
| 12 | #include "include/core/SkImage.h" |
| 13 | #include "include/core/SkImageEncoder.h" |
| 14 | #include "include/core/SkMallocPixelRef.h" |
| 15 | #include "include/core/SkPaint.h" |
| 16 | #include "include/core/SkPath.h" |
| 17 | #include "include/core/SkStream.h" |
| 18 | #include "include/core/SkSurface.h" |
| 19 | #include "include/core/SkTextBlob.h" |
Kevin Lubick | d7255a7 | 2019-06-03 11:59:23 -0400 | [diff] [blame] | 20 | #include "src/core/SkFontMgrPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/core/SkOSFile.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/core/SkReadBuffer.h" |
| 23 | #include "src/utils/SkOSPath.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "tools/ToolUtils.h" |
Kevin Lubick | d7255a7 | 2019-06-03 11:59:23 -0400 | [diff] [blame] | 25 | #include "tools/flags/CommandLineFlags.h" |
| 26 | #include "tools/fonts/TestFontMgr.h" |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 27 | |
Hal Canary | 62176db | 2017-02-27 16:42:03 -0500 | [diff] [blame] | 28 | #include <iostream> |
Kevin Lubick | acd456a | 2018-04-24 13:58:16 -0400 | [diff] [blame] | 29 | #include <map> |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 30 | #include <regex> |
mtklein | a115942 | 2016-01-15 05:46:54 -0800 | [diff] [blame] | 31 | #include <signal.h> |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 32 | |
Mike Klein | 84836b7 | 2019-03-21 11:31:36 -0500 | [diff] [blame] | 33 | static DEFINE_string2(bytes, b, "", "A path to a file or a directory. If a file, the " |
| 34 | "contents will be used as the fuzz bytes. If a directory, all files " |
| 35 | "in the directory will be used as fuzz bytes for the fuzzer, one at a " |
| 36 | "time."); |
| 37 | static DEFINE_string2(name, n, "", "If --type is 'api', fuzz the API with this name."); |
| 38 | static DEFINE_string2(dump, d, "", "If not empty, dump 'image*' or 'skp' types as a " |
| 39 | "PNG with this name."); |
Brian Osman | f4e5737 | 2020-07-17 11:37:14 -0400 | [diff] [blame] | 40 | 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] | 41 | DEFINE_bool2(verbose, v, false, "Print more information while fuzzing."); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 42 | |
| 43 | // This cannot be inlined in DEFINE_string2 due to interleaved ifdefs |
| 44 | 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] | 45 | "android_codec\n" |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 46 | "animated_image_decode\n" |
| 47 | "api\n" |
| 48 | "color_deserialize\n" |
| 49 | "filter_fuzz (equivalent to Chrome's filter_fuzz_stub)\n" |
| 50 | "image_decode\n" |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 51 | "image_decode_incremental\n" |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 52 | "image_mode\n" |
| 53 | "image_scale\n" |
Florin Malita | 80452be | 2018-06-19 11:27:20 -0400 | [diff] [blame] | 54 | "json\n" |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 55 | "path_deserialize\n" |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 56 | "region_deserialize\n" |
| 57 | "region_set_path\n" |
Kevin Lubick | 2be14d3 | 2019-10-21 13:44:48 -0400 | [diff] [blame] | 58 | "skdescriptor_deserialize\n" |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 59 | "skp\n" |
Zepeng Hu | a5783f3 | 2020-07-10 13:36:20 +0000 | [diff] [blame] | 60 | "skruntimeeffect\n" |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 61 | "sksl2glsl\n" |
Zepeng Hu | edaf302 | 2020-06-12 12:20:59 +0000 | [diff] [blame] | 62 | "svg_dom\n" |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 63 | "sksl2metal\n" |
Kevin Lubick | 0f0a710 | 2019-03-18 16:20:55 -0400 | [diff] [blame] | 64 | "sksl2pipeline\n" |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 65 | "sksl2spirv\n" |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 66 | #if defined(SK_ENABLE_SKOTTIE) |
| 67 | "skottie_json\n" |
| 68 | #endif |
| 69 | "textblob"; |
| 70 | |
Mike Klein | 84836b7 | 2019-03-21 11:31:36 -0500 | [diff] [blame] | 71 | static DEFINE_string2(type, t, "", g_type_message); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 72 | |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 73 | static int fuzz_file(SkString path, SkString type); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 74 | static uint8_t calculate_option(SkData*); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 75 | static SkString try_auto_detect(SkString path, SkString* name); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 76 | |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 77 | static void fuzz_android_codec(sk_sp<SkData>); |
| 78 | static void fuzz_animated_img(sk_sp<SkData>); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 79 | static void fuzz_api(sk_sp<SkData> bytes, SkString name); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 80 | static void fuzz_color_deserialize(sk_sp<SkData>); |
Kevin Lubick | d46e85f | 2017-11-21 17:13:35 -0500 | [diff] [blame] | 81 | static void fuzz_filter_fuzz(sk_sp<SkData>); |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 82 | static void fuzz_image_decode(sk_sp<SkData>); |
| 83 | static void fuzz_image_decode_incremental(sk_sp<SkData>); |
Kevin Lubick | 0168e04 | 2017-02-14 13:12:37 -0500 | [diff] [blame] | 84 | static void fuzz_img(sk_sp<SkData>, uint8_t, uint8_t); |
Florin Malita | 80452be | 2018-06-19 11:27:20 -0400 | [diff] [blame] | 85 | static void fuzz_json(sk_sp<SkData>); |
Kevin Lubick | f04c50a | 2017-01-06 13:48:19 -0500 | [diff] [blame] | 86 | static void fuzz_path_deserialize(sk_sp<SkData>); |
Kevin Lubick | edee1ae | 2017-02-20 17:47:18 -0500 | [diff] [blame] | 87 | static void fuzz_region_deserialize(sk_sp<SkData>); |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 88 | static void fuzz_region_set_path(sk_sp<SkData>); |
Kevin Lubick | 2be14d3 | 2019-10-21 13:44:48 -0400 | [diff] [blame] | 89 | static void fuzz_skdescriptor_deserialize(sk_sp<SkData>); |
Kevin Lubick | 0168e04 | 2017-02-14 13:12:37 -0500 | [diff] [blame] | 90 | static void fuzz_skp(sk_sp<SkData>); |
Zepeng Hu | a5783f3 | 2020-07-10 13:36:20 +0000 | [diff] [blame] | 91 | static void fuzz_skruntimeeffect(sk_sp<SkData>); |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 92 | static void fuzz_sksl2glsl(sk_sp<SkData>); |
| 93 | static void fuzz_sksl2metal(sk_sp<SkData>); |
Kevin Lubick | 0f0a710 | 2019-03-18 16:20:55 -0400 | [diff] [blame] | 94 | static void fuzz_sksl2pipeline(sk_sp<SkData>); |
Kevin Lubick | 2be14d3 | 2019-10-21 13:44:48 -0400 | [diff] [blame] | 95 | static void fuzz_sksl2spirv(sk_sp<SkData>); |
Robert Phillips | 44b7568 | 2021-08-31 22:30:44 +0000 | [diff] [blame^] | 96 | static void fuzz_svg_dom(sk_sp<SkData>); |
Kevin Lubick | d46e85f | 2017-11-21 17:13:35 -0500 | [diff] [blame] | 97 | static void fuzz_textblob_deserialize(sk_sp<SkData>); |
Herb Derby | a839fc0 | 2017-03-16 12:30:43 -0400 | [diff] [blame] | 98 | |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 99 | static void print_api_names(); |
| 100 | |
Florin Malita | 0b0d93d | 2018-05-04 15:01:03 -0400 | [diff] [blame] | 101 | #if defined(SK_ENABLE_SKOTTIE) |
| 102 | static void fuzz_skottie_json(sk_sp<SkData>); |
| 103 | #endif |
| 104 | |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 105 | int main(int argc, char** argv) { |
Mike Klein | 88544fb | 2019-03-20 10:50:33 -0500 | [diff] [blame] | 106 | CommandLineFlags::SetUsage( |
| 107 | "Usage: fuzz -t <type> -b <path/to/file> [-n api-to-fuzz]\n" |
| 108 | " fuzz -b <path/to/file>\n" |
| 109 | "--help lists the valid types. If type is not specified,\n" |
| 110 | "fuzz will make a guess based on the name of the file.\n"); |
| 111 | CommandLineFlags::Parse(argc, argv); |
Kevin Lubick | d7255a7 | 2019-06-03 11:59:23 -0400 | [diff] [blame] | 112 | gSkFontMgr_DefaultFactory = &ToolUtils::MakePortableFontMgr; |
mtklein | f5e9782 | 2016-01-15 06:19:53 -0800 | [diff] [blame] | 113 | |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 114 | SkString path = SkString(FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0]); |
| 115 | SkString type = SkString(FLAGS_type.isEmpty() ? "" : FLAGS_type[0]); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 116 | |
Brian Osman | f4e5737 | 2020-07-17 11:37:14 -0400 | [diff] [blame] | 117 | int loopCount = std::max(FLAGS_loops, 1); |
| 118 | |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 119 | if (!sk_isdir(path.c_str())) { |
Brian Osman | f4e5737 | 2020-07-17 11:37:14 -0400 | [diff] [blame] | 120 | for (int i = 0; i < loopCount; ++i) { |
| 121 | int rv = fuzz_file(path, type); |
| 122 | if (rv != 0) { |
| 123 | return rv; |
| 124 | } |
| 125 | } |
| 126 | return 0; |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 127 | } |
| 128 | |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 129 | SkOSFile::Iter it(path.c_str()); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 130 | for (SkString file; it.next(&file); ) { |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 131 | SkString p = SkOSPath::Join(path.c_str(), file.c_str()); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 132 | SkDebugf("Fuzzing %s\n", p.c_str()); |
Brian Osman | f4e5737 | 2020-07-17 11:37:14 -0400 | [diff] [blame] | 133 | for (int i = 0; i < loopCount; ++i) { |
| 134 | int rv = fuzz_file(p, type); |
| 135 | if (rv != 0) { |
| 136 | return rv; |
| 137 | } |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | return 0; |
| 141 | } |
| 142 | |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 143 | static int fuzz_file(SkString path, SkString type) { |
| 144 | sk_sp<SkData> bytes(SkData::MakeFromFileName(path.c_str())); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 145 | if (!bytes) { |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 146 | SkDebugf("Could not read %s\n", path.c_str()); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 147 | return 1; |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 148 | } |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 149 | |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 150 | SkString name = SkString(FLAGS_name.isEmpty() ? "" : FLAGS_name[0]); |
| 151 | |
| 152 | if (type.isEmpty()) { |
| 153 | type = try_auto_detect(path, &name); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 154 | } |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 155 | |
| 156 | if (type.isEmpty()) { |
| 157 | SkDebugf("Could not autodetect type of %s\n", path.c_str()); |
| 158 | return 1; |
| 159 | } |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 160 | if (type.equals("android_codec")) { |
| 161 | fuzz_android_codec(bytes); |
| 162 | return 0; |
| 163 | } |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 164 | if (type.equals("animated_image_decode")) { |
| 165 | fuzz_animated_img(bytes); |
| 166 | return 0; |
| 167 | } |
| 168 | if (type.equals("api")) { |
| 169 | fuzz_api(bytes, name); |
| 170 | return 0; |
| 171 | } |
| 172 | if (type.equals("color_deserialize")) { |
| 173 | fuzz_color_deserialize(bytes); |
| 174 | return 0; |
| 175 | } |
Florin Malita | 80452be | 2018-06-19 11:27:20 -0400 | [diff] [blame] | 176 | if (type.equals("filter_fuzz")) { |
| 177 | fuzz_filter_fuzz(bytes); |
| 178 | return 0; |
| 179 | } |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 180 | if (type.equals("image_decode")) { |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 181 | fuzz_image_decode(bytes); |
| 182 | return 0; |
| 183 | } |
| 184 | if (type.equals("image_decode_incremental")) { |
| 185 | fuzz_image_decode_incremental(bytes); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | if (type.equals("image_scale")) { |
| 189 | uint8_t option = calculate_option(bytes.get()); |
| 190 | fuzz_img(bytes, option, 0); |
| 191 | return 0; |
| 192 | } |
| 193 | if (type.equals("image_mode")) { |
| 194 | uint8_t option = calculate_option(bytes.get()); |
| 195 | fuzz_img(bytes, 0, option); |
| 196 | return 0; |
| 197 | } |
Florin Malita | 80452be | 2018-06-19 11:27:20 -0400 | [diff] [blame] | 198 | if (type.equals("json")) { |
| 199 | fuzz_json(bytes); |
Kevin Lubick | 9eeede2 | 2018-05-03 16:26:10 -0400 | [diff] [blame] | 200 | return 0; |
| 201 | } |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 202 | if (type.equals("path_deserialize")) { |
| 203 | fuzz_path_deserialize(bytes); |
| 204 | return 0; |
| 205 | } |
| 206 | if (type.equals("region_deserialize")) { |
| 207 | fuzz_region_deserialize(bytes); |
| 208 | return 0; |
| 209 | } |
| 210 | if (type.equals("region_set_path")) { |
| 211 | fuzz_region_set_path(bytes); |
| 212 | return 0; |
| 213 | } |
| 214 | if (type.equals("pipe")) { |
Mike Klein | 60900b5 | 2018-09-21 11:19:45 -0400 | [diff] [blame] | 215 | SkDebugf("I would prefer not to.\n"); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 216 | return 0; |
| 217 | } |
Kevin Lubick | 2be14d3 | 2019-10-21 13:44:48 -0400 | [diff] [blame] | 218 | if (type.equals("skdescriptor_deserialize")) { |
| 219 | fuzz_skdescriptor_deserialize(bytes); |
| 220 | return 0; |
| 221 | } |
Florin Malita | 0b0d93d | 2018-05-04 15:01:03 -0400 | [diff] [blame] | 222 | #if defined(SK_ENABLE_SKOTTIE) |
Kevin Lubick | 9eeede2 | 2018-05-03 16:26:10 -0400 | [diff] [blame] | 223 | if (type.equals("skottie_json")) { |
| 224 | fuzz_skottie_json(bytes); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 225 | return 0; |
| 226 | } |
Florin Malita | 0b0d93d | 2018-05-04 15:01:03 -0400 | [diff] [blame] | 227 | #endif |
Kevin Lubick | 9eeede2 | 2018-05-03 16:26:10 -0400 | [diff] [blame] | 228 | if (type.equals("skp")) { |
| 229 | fuzz_skp(bytes); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 230 | return 0; |
| 231 | } |
Zepeng Hu | a5783f3 | 2020-07-10 13:36:20 +0000 | [diff] [blame] | 232 | if (type.equals("skruntimeeffect")) { |
| 233 | fuzz_skruntimeeffect(bytes); |
| 234 | return 0; |
| 235 | } |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 236 | if (type.equals("sksl2glsl")) { |
| 237 | fuzz_sksl2glsl(bytes); |
| 238 | return 0; |
| 239 | } |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 240 | if (type.equals("sksl2metal")) { |
| 241 | fuzz_sksl2metal(bytes); |
| 242 | return 0; |
| 243 | } |
| 244 | if (type.equals("sksl2spirv")) { |
| 245 | fuzz_sksl2spirv(bytes); |
| 246 | return 0; |
| 247 | } |
Kevin Lubick | 0f0a710 | 2019-03-18 16:20:55 -0400 | [diff] [blame] | 248 | if (type.equals("sksl2pipeline")) { |
| 249 | fuzz_sksl2pipeline(bytes); |
| 250 | return 0; |
| 251 | } |
Zepeng Hu | edaf302 | 2020-06-12 12:20:59 +0000 | [diff] [blame] | 252 | if (type.equals("svg_dom")) { |
| 253 | fuzz_svg_dom(bytes); |
| 254 | return 0; |
| 255 | } |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 256 | if (type.equals("textblob")) { |
| 257 | fuzz_textblob_deserialize(bytes); |
| 258 | return 0; |
| 259 | } |
Kevin Lubick | 457fa97 | 2018-05-29 09:22:06 -0400 | [diff] [blame] | 260 | SkDebugf("Unknown type %s\n", type.c_str()); |
Mike Klein | 88544fb | 2019-03-20 10:50:33 -0500 | [diff] [blame] | 261 | CommandLineFlags::PrintUsage(); |
Kevin Lubick | 9ff5dc9 | 2018-01-09 12:47:33 -0500 | [diff] [blame] | 262 | return 1; |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 265 | static std::map<std::string, std::string> cf_api_map = { |
Zepeng Hu | 9400701 | 2020-07-22 14:37:46 +0000 | [diff] [blame] | 266 | {"api_create_ddl", "CreateDDL"}, |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 267 | {"api_draw_functions", "DrawFunctions"}, |
Adlai Holler | ccb6866 | 2021-02-25 10:28:44 -0500 | [diff] [blame] | 268 | {"api_ddl_threading", "DDLThreadingGL"}, |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 269 | {"api_gradients", "Gradients"}, |
| 270 | {"api_image_filter", "ImageFilter"}, |
| 271 | {"api_mock_gpu_canvas", "MockGPUCanvas"}, |
| 272 | {"api_null_canvas", "NullCanvas"}, |
| 273 | {"api_path_measure", "PathMeasure"}, |
Kevin Lubick | de54d7c | 2018-09-24 08:48:19 -0400 | [diff] [blame] | 274 | {"api_pathop", "Pathop"}, |
Kevin Lubick | 57507f1 | 2018-10-11 09:35:15 -0400 | [diff] [blame] | 275 | {"api_polyutils", "PolyUtils"}, |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 276 | {"api_raster_n32_canvas", "RasterN32Canvas"}, |
Weston Tracey | 1a771fe | 2021-02-04 11:09:59 -0500 | [diff] [blame] | 277 | {"api_skparagraph", "SkParagraph"}, |
Zepeng Hu | ba7cbf7 | 2020-07-01 13:21:03 +0000 | [diff] [blame] | 278 | {"api_svg_canvas", "SVGCanvas"}, |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 279 | {"jpeg_encoder", "JPEGEncoder"}, |
| 280 | {"png_encoder", "PNGEncoder"}, |
Kevin Lubick | f84ded2 | 2018-10-23 09:28:48 -0400 | [diff] [blame] | 281 | {"skia_pathop_fuzzer", "LegacyChromiumPathop"}, |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 282 | {"webp_encoder", "WEBPEncoder"} |
| 283 | }; |
| 284 | |
Kevin Lubick | 6c56055 | 2018-06-20 09:29:23 -0400 | [diff] [blame] | 285 | // maps clusterfuzz/oss-fuzz -> Skia's name |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 286 | static std::map<std::string, std::string> cf_map = { |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 287 | {"android_codec", "android_codec"}, |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 288 | {"animated_image_decode", "animated_image_decode"}, |
| 289 | {"image_decode", "image_decode"}, |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 290 | {"image_decode_incremental", "image_decode_incremental"}, |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 291 | {"image_filter_deserialize", "filter_fuzz"}, |
| 292 | {"image_filter_deserialize_width", "filter_fuzz"}, |
| 293 | {"path_deserialize", "path_deserialize"}, |
| 294 | {"region_deserialize", "region_deserialize"}, |
| 295 | {"region_set_path", "region_set_path"}, |
Kevin Lubick | 2be14d3 | 2019-10-21 13:44:48 -0400 | [diff] [blame] | 296 | {"skdescriptor_deserialize", "skdescriptor_deserialize"}, |
Kevin Lubick | 6c56055 | 2018-06-20 09:29:23 -0400 | [diff] [blame] | 297 | {"skjson", "json"}, |
Zepeng Hu | fcb7ba0 | 2020-07-31 17:21:29 +0000 | [diff] [blame] | 298 | {"skp", "skp"}, |
Zepeng Hu | a5783f3 | 2020-07-10 13:36:20 +0000 | [diff] [blame] | 299 | {"skruntimeeffect", "skruntimeeffect"}, |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 300 | {"sksl2glsl", "sksl2glsl"}, |
| 301 | {"sksl2metal", "sksl2metal"}, |
| 302 | {"sksl2spirv", "sksl2spirv"}, |
Kevin Lubick | 0f0a710 | 2019-03-18 16:20:55 -0400 | [diff] [blame] | 303 | {"sksl2pipeline", "sksl2pipeline"}, |
Kevin Lubick | de54d7c | 2018-09-24 08:48:19 -0400 | [diff] [blame] | 304 | #if defined(SK_ENABLE_SKOTTIE) |
| 305 | {"skottie_json", "skottie_json"}, |
| 306 | #endif |
Zepeng Hu | edaf302 | 2020-06-12 12:20:59 +0000 | [diff] [blame] | 307 | {"svg_dom", "svg_dom"}, |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 308 | {"textblob_deserialize", "textblob"} |
| 309 | }; |
| 310 | |
| 311 | static SkString try_auto_detect(SkString path, SkString* name) { |
| 312 | std::cmatch m; |
| 313 | std::regex clusterfuzz("clusterfuzz-testcase(-minimized)?-([a-z0-9_]+)-[\\d]+"); |
| 314 | std::regex skiafuzzer("(api-)?(\\w+)-[a-f0-9]+"); |
| 315 | |
| 316 | if (std::regex_search(path.c_str(), m, clusterfuzz)) { |
| 317 | std::string type = m.str(2); |
Kevin Lubick | 6c56055 | 2018-06-20 09:29:23 -0400 | [diff] [blame] | 318 | |
| 319 | if (cf_api_map.find(type) != cf_api_map.end()) { |
| 320 | *name = SkString(cf_api_map[type].c_str()); |
| 321 | return SkString("api"); |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 322 | } else { |
| 323 | if (cf_map.find(type) != cf_map.end()) { |
| 324 | return SkString(cf_map[type].c_str()); |
| 325 | } |
| 326 | } |
| 327 | } else if (std::regex_search(path.c_str(), m, skiafuzzer)) { |
| 328 | std::string a1 = m.str(1); |
| 329 | std::string typeOrName = m.str(2); |
| 330 | if (a1.length() > 0) { |
| 331 | // it's an api fuzzer |
| 332 | *name = SkString(typeOrName.c_str()); |
| 333 | return SkString("api"); |
| 334 | } else { |
| 335 | return SkString(typeOrName.c_str()); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | return SkString(""); |
| 340 | } |
| 341 | |
Florin Malita | 80452be | 2018-06-19 11:27:20 -0400 | [diff] [blame] | 342 | void FuzzJSON(sk_sp<SkData> bytes); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 343 | |
Florin Malita | 80452be | 2018-06-19 11:27:20 -0400 | [diff] [blame] | 344 | static void fuzz_json(sk_sp<SkData> bytes){ |
| 345 | FuzzJSON(bytes); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 346 | SkDebugf("[terminated] Done parsing!\n"); |
| 347 | } |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 348 | |
Florin Malita | 0b0d93d | 2018-05-04 15:01:03 -0400 | [diff] [blame] | 349 | #if defined(SK_ENABLE_SKOTTIE) |
Kevin Lubick | 9eeede2 | 2018-05-03 16:26:10 -0400 | [diff] [blame] | 350 | void FuzzSkottieJSON(sk_sp<SkData> bytes); |
| 351 | |
| 352 | static void fuzz_skottie_json(sk_sp<SkData> bytes){ |
| 353 | FuzzSkottieJSON(bytes); |
| 354 | SkDebugf("[terminated] Done animating!\n"); |
| 355 | } |
Florin Malita | 0b0d93d | 2018-05-04 15:01:03 -0400 | [diff] [blame] | 356 | #endif |
Kevin Lubick | 9eeede2 | 2018-05-03 16:26:10 -0400 | [diff] [blame] | 357 | |
Zepeng Hu | edaf302 | 2020-06-12 12:20:59 +0000 | [diff] [blame] | 358 | void FuzzSVG(sk_sp<SkData> bytes); |
| 359 | static void fuzz_svg_dom(sk_sp<SkData> bytes){ |
| 360 | FuzzSVG(bytes); |
| 361 | SkDebugf("[terminated] Done DOM!\n"); |
| 362 | } |
| 363 | |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 364 | // This adds up the first 1024 bytes and returns it as an 8 bit integer. This allows afl-fuzz to |
| 365 | // deterministically excercise different paths, or *options* (such as different scaling sizes or |
| 366 | // different image modes) without needing to introduce a parameter. This way we don't need a |
| 367 | // image_scale1, image_scale2, image_scale4, etc fuzzer, we can just have a image_scale fuzzer. |
| 368 | // Clients are expected to transform this number into a different range, e.g. with modulo (%). |
| 369 | static uint8_t calculate_option(SkData* bytes) { |
| 370 | uint8_t total = 0; |
| 371 | const uint8_t* data = bytes->bytes(); |
| 372 | for (size_t i = 0; i < 1024 && i < bytes->size(); i++) { |
| 373 | total += data[i]; |
| 374 | } |
| 375 | return total; |
| 376 | } |
| 377 | |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 378 | static void print_api_names(){ |
| 379 | 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] | 380 | for (const Fuzzable& fuzzable : sk_tools::Registry<Fuzzable>::Range()) { |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 381 | SkDebugf("\t%s\n", fuzzable.name); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | static void fuzz_api(sk_sp<SkData> bytes, SkString name) { |
Hal Canary | 972eba3 | 2018-07-30 17:07:07 -0400 | [diff] [blame] | 386 | for (const Fuzzable& fuzzable : sk_tools::Registry<Fuzzable>::Range()) { |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 387 | if (name.equals(fuzzable.name)) { |
mtklein | f5e9782 | 2016-01-15 06:19:53 -0800 | [diff] [blame] | 388 | SkDebugf("Fuzzing %s...\n", fuzzable.name); |
Kevin Lubick | 1ac8fd2 | 2017-03-01 10:42:45 -0500 | [diff] [blame] | 389 | Fuzz fuzz(std::move(bytes)); |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 390 | fuzzable.fn(&fuzz); |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 391 | SkDebugf("[terminated] Success!\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 392 | return; |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 393 | } |
| 394 | } |
mtklein | d4387ea | 2016-01-21 06:13:52 -0800 | [diff] [blame] | 395 | |
Kevin Lubick | fffa641 | 2018-04-23 16:44:55 -0400 | [diff] [blame] | 396 | print_api_names(); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | static void dump_png(SkBitmap bitmap) { |
| 400 | if (!FLAGS_dump.isEmpty()) { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 401 | ToolUtils::EncodeImageToFile(FLAGS_dump[0], bitmap, SkEncodedImageFormat::kPNG, 100); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 402 | SkDebugf("Dumped to %s\n", FLAGS_dump[0]); |
| 403 | } |
| 404 | } |
| 405 | |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 406 | bool FuzzAnimatedImage(sk_sp<SkData> bytes); |
Kevin Lubick | 2416f96 | 2018-02-12 08:26:39 -0500 | [diff] [blame] | 407 | |
| 408 | static void fuzz_animated_img(sk_sp<SkData> bytes) { |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 409 | if (FuzzAnimatedImage(bytes)) { |
| 410 | SkDebugf("[terminated] Success from decoding/drawing animated image!\n"); |
| 411 | return; |
| 412 | } |
| 413 | SkDebugf("[terminated] Could not decode or draw animated image.\n"); |
Kevin Lubick | 2416f96 | 2018-02-12 08:26:39 -0500 | [diff] [blame] | 414 | } |
| 415 | |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 416 | bool FuzzImageDecode(sk_sp<SkData> bytes); |
Kevin Lubick | 2416f96 | 2018-02-12 08:26:39 -0500 | [diff] [blame] | 417 | |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 418 | static void fuzz_image_decode(sk_sp<SkData> bytes) { |
| 419 | if (FuzzImageDecode(bytes)) { |
| 420 | SkDebugf("[terminated] Success from decoding/drawing image!\n"); |
| 421 | return; |
| 422 | } |
| 423 | SkDebugf("[terminated] Could not decode or draw image.\n"); |
Kevin Lubick | 2416f96 | 2018-02-12 08:26:39 -0500 | [diff] [blame] | 424 | } |
| 425 | |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 426 | bool FuzzIncrementalImageDecode(sk_sp<SkData> bytes); |
| 427 | |
| 428 | static void fuzz_image_decode_incremental(sk_sp<SkData> bytes) { |
| 429 | if (FuzzIncrementalImageDecode(bytes)) { |
| 430 | SkDebugf("[terminated] Success using incremental decode!\n"); |
| 431 | return; |
| 432 | } |
| 433 | SkDebugf("[terminated] Could not incrementally decode and image.\n"); |
| 434 | } |
| 435 | |
| 436 | bool FuzzAndroidCodec(sk_sp<SkData> bytes, uint8_t sampleSize); |
| 437 | |
| 438 | static void fuzz_android_codec(sk_sp<SkData> bytes) { |
| 439 | Fuzz fuzz(bytes); |
| 440 | uint8_t sampleSize; |
| 441 | fuzz.nextRange(&sampleSize, 1, 64); |
| 442 | bytes = SkData::MakeSubset(bytes.get(), 1, bytes->size() - 1); |
| 443 | if (FuzzAndroidCodec(bytes, sampleSize)) { |
| 444 | SkDebugf("[terminated] Success on Android Codec sampleSize=%u!\n", sampleSize); |
| 445 | return; |
| 446 | } |
| 447 | SkDebugf("[terminated] Could not use Android Codec sampleSize=%u!\n", sampleSize); |
| 448 | } |
| 449 | |
| 450 | // This is a "legacy" fuzzer that likely does too much. It was based off of how |
| 451 | // DM reads in images. image_decode, image_decode_incremental and android_codec |
| 452 | // are more targeted fuzzers that do a subset of what this one does. |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 453 | static void fuzz_img(sk_sp<SkData> bytes, uint8_t scale, uint8_t mode) { |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 454 | // We can scale 1x, 2x, 4x, 8x, 16x |
| 455 | scale = scale % 5; |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 456 | float fscale = (float)pow(2.0f, scale); |
| 457 | SkDebugf("Scaling factor: %f\n", fscale); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 458 | |
Leon Scroggins III | c5a8366 | 2016-12-08 09:07:56 -0500 | [diff] [blame] | 459 | // We have 5 different modes of decoding. |
| 460 | mode = mode % 5; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 461 | SkDebugf("Mode: %d\n", mode); |
| 462 | |
| 463 | // This is mostly copied from DMSrcSink's CodecSrc::draw method. |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 464 | SkDebugf("Decoding\n"); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 465 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(bytes)); |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 466 | if (nullptr == codec) { |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 467 | SkDebugf("[terminated] Couldn't create codec.\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 468 | return; |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | SkImageInfo decodeInfo = codec->getInfo(); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 472 | SkISize size = codec->getScaledDimensions(fscale); |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 473 | decodeInfo = decodeInfo.makeDimensions(size); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 474 | |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 475 | SkBitmap bitmap; |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 476 | SkCodec::Options options; |
| 477 | options.fZeroInitialized = SkCodec::kYes_ZeroInitialized; |
| 478 | |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 479 | if (!bitmap.tryAllocPixelsFlags(decodeInfo, SkBitmap::kZeroPixels_AllocFlag)) { |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 480 | 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] | 481 | decodeInfo.width(), decodeInfo.height()); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 482 | return; |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 483 | } |
| 484 | |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 485 | switch (mode) { |
| 486 | case 0: {//kCodecZeroInit_Mode, kCodec_Mode |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 487 | switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options)) { |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 488 | case SkCodec::kSuccess: |
| 489 | SkDebugf("[terminated] Success!\n"); |
| 490 | break; |
| 491 | case SkCodec::kIncompleteInput: |
| 492 | SkDebugf("[terminated] Partial Success\n"); |
| 493 | break; |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 494 | case SkCodec::kErrorInInput: |
| 495 | SkDebugf("[terminated] Partial Success with error\n"); |
| 496 | break; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 497 | case SkCodec::kInvalidConversion: |
| 498 | SkDebugf("Incompatible colortype conversion\n"); |
| 499 | // Crash to allow afl-fuzz to know this was a bug. |
| 500 | raise(SIGSEGV); |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 501 | break; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 502 | default: |
| 503 | SkDebugf("[terminated] Couldn't getPixels.\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 504 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 505 | } |
| 506 | break; |
| 507 | } |
| 508 | case 1: {//kScanline_Mode |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 509 | if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)) { |
| 510 | SkDebugf("[terminated] Could not start scanline decoder\n"); |
| 511 | return; |
| 512 | } |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 513 | |
| 514 | void* dst = bitmap.getAddr(0, 0); |
| 515 | size_t rowBytes = bitmap.rowBytes(); |
| 516 | uint32_t height = decodeInfo.height(); |
Brian Osman | ea176c6 | 2018-04-06 15:28:23 -0400 | [diff] [blame] | 517 | // We do not need to check the return value. On an incomplete |
| 518 | // image, memory will be filled with a default value. |
| 519 | codec->getScanlines(dst, height, rowBytes); |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 520 | SkDebugf("[terminated] Success!\n"); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 521 | break; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 522 | } |
| 523 | case 2: { //kStripe_Mode |
| 524 | const int height = decodeInfo.height(); |
| 525 | // This value is chosen arbitrarily. We exercise more cases by choosing a value that |
| 526 | // does not align with image blocks. |
| 527 | const int stripeHeight = 37; |
| 528 | const int numStripes = (height + stripeHeight - 1) / stripeHeight; |
| 529 | |
| 530 | // Decode odd stripes |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 531 | if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo) |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 532 | || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) { |
| 533 | // This mode was designed to test the new skip scanlines API in libjpeg-turbo. |
| 534 | // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting |
| 535 | // to run this test for image types that do not have this scanline ordering. |
| 536 | SkDebugf("[terminated] Could not start top-down scanline decoder\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 537 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | for (int i = 0; i < numStripes; i += 2) { |
| 541 | // Skip a stripe |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 542 | const int linesToSkip = std::min(stripeHeight, height - i * stripeHeight); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 543 | codec->skipScanlines(linesToSkip); |
| 544 | |
| 545 | // Read a stripe |
| 546 | const int startY = (i + 1) * stripeHeight; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 547 | const int linesToRead = std::min(stripeHeight, height - startY); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 548 | if (linesToRead > 0) { |
| 549 | codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes()); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | // Decode even stripes |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 554 | const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 555 | if (SkCodec::kSuccess != startResult) { |
| 556 | SkDebugf("[terminated] Failed to restart scanline decoder with same parameters.\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 557 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 558 | } |
| 559 | for (int i = 0; i < numStripes; i += 2) { |
| 560 | // Read a stripe |
| 561 | const int startY = i * stripeHeight; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 562 | const int linesToRead = std::min(stripeHeight, height - startY); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 563 | codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes()); |
| 564 | |
| 565 | // Skip a stripe |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 566 | const int linesToSkip = std::min(stripeHeight, height - (i + 1) * stripeHeight); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 567 | if (linesToSkip > 0) { |
| 568 | codec->skipScanlines(linesToSkip); |
| 569 | } |
| 570 | } |
| 571 | SkDebugf("[terminated] Success!\n"); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 572 | break; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 573 | } |
| 574 | case 3: { //kSubset_Mode |
| 575 | // Arbitrarily choose a divisor. |
| 576 | int divisor = 2; |
| 577 | // Total width/height of the image. |
| 578 | const int W = codec->getInfo().width(); |
| 579 | const int H = codec->getInfo().height(); |
| 580 | if (divisor > W || divisor > H) { |
| 581 | SkDebugf("[terminated] Cannot codec subset: divisor %d is too big " |
| 582 | "with dimensions (%d x %d)\n", divisor, W, H); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 583 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 584 | } |
| 585 | // subset dimensions |
| 586 | // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries. |
| 587 | const int w = SkAlign2(W / divisor); |
| 588 | const int h = SkAlign2(H / divisor); |
| 589 | SkIRect subset; |
| 590 | SkCodec::Options opts; |
| 591 | opts.fSubset = ⊂ |
| 592 | SkBitmap subsetBm; |
| 593 | // We will reuse pixel memory from bitmap. |
| 594 | void* pixels = bitmap.getPixels(); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 595 | for (int x = 0; x < W; x += w) { |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 596 | for (int y = 0; y < H; y+= h) { |
| 597 | // Do not make the subset go off the edge of the image. |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 598 | const int preScaleW = std::min(w, W - x); |
| 599 | const int preScaleH = std::min(h, H - y); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 600 | subset.setXYWH(x, y, preScaleW, preScaleH); |
| 601 | // And fscale |
| 602 | // FIXME: Should we have a version of getScaledDimensions that takes a subset |
| 603 | // into account? |
| 604 | decodeInfo = decodeInfo.makeWH( |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 605 | std::max(1, SkScalarRoundToInt(preScaleW * fscale)), |
| 606 | std::max(1, SkScalarRoundToInt(preScaleH * fscale))); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 607 | size_t rowBytes = decodeInfo.minRowBytes(); |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 608 | if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes)) { |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 609 | SkDebugf("[terminated] Could not install pixels.\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 610 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 611 | } |
| 612 | const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 613 | &opts); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 614 | switch (result) { |
| 615 | case SkCodec::kSuccess: |
| 616 | case SkCodec::kIncompleteInput: |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 617 | case SkCodec::kErrorInInput: |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 618 | SkDebugf("okay\n"); |
| 619 | break; |
| 620 | case SkCodec::kInvalidConversion: |
| 621 | if (0 == (x|y)) { |
| 622 | // First subset is okay to return unimplemented. |
| 623 | SkDebugf("[terminated] Incompatible colortype conversion\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 624 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 625 | } |
| 626 | // If the first subset succeeded, a later one should not fail. |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 627 | [[fallthrough]]; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 628 | case SkCodec::kUnimplemented: |
| 629 | if (0 == (x|y)) { |
| 630 | // First subset is okay to return unimplemented. |
| 631 | SkDebugf("[terminated] subset codec not supported\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 632 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 633 | } |
| 634 | // If the first subset succeeded, why would a later one fail? |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 635 | [[fallthrough]]; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 636 | default: |
| 637 | SkDebugf("[terminated] subset codec failed to decode (%d, %d, %d, %d) " |
| 638 | "with dimensions (%d x %d)\t error %d\n", |
| 639 | x, y, decodeInfo.width(), decodeInfo.height(), |
| 640 | W, H, result); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 641 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 642 | } |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 643 | } |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 644 | } |
| 645 | SkDebugf("[terminated] Success!\n"); |
| 646 | break; |
| 647 | } |
Leon Scroggins III | c5a8366 | 2016-12-08 09:07:56 -0500 | [diff] [blame] | 648 | case 4: { //kAnimated_Mode |
| 649 | std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo(); |
| 650 | if (frameInfos.size() == 0) { |
| 651 | SkDebugf("[terminated] Not an animated image\n"); |
| 652 | break; |
| 653 | } |
| 654 | |
| 655 | for (size_t i = 0; i < frameInfos.size(); i++) { |
| 656 | options.fFrameIndex = i; |
| 657 | auto result = codec->startIncrementalDecode(decodeInfo, bitmap.getPixels(), |
| 658 | bitmap.rowBytes(), &options); |
| 659 | if (SkCodec::kSuccess != result) { |
| 660 | SkDebugf("[terminated] failed to start incremental decode " |
John Stiles | 7bf7999 | 2021-06-25 11:05:20 -0400 | [diff] [blame] | 661 | "in frame %zu with error %d\n", i, result); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 662 | return; |
Leon Scroggins III | c5a8366 | 2016-12-08 09:07:56 -0500 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | result = codec->incrementalDecode(); |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 666 | if (result == SkCodec::kIncompleteInput || result == SkCodec::kErrorInInput) { |
Leon Scroggins III | c5a8366 | 2016-12-08 09:07:56 -0500 | [diff] [blame] | 667 | SkDebugf("okay\n"); |
| 668 | // Frames beyond this one will not decode. |
| 669 | break; |
| 670 | } |
| 671 | if (result == SkCodec::kSuccess) { |
John Stiles | 7bf7999 | 2021-06-25 11:05:20 -0400 | [diff] [blame] | 672 | SkDebugf("okay - decoded frame %zu\n", i); |
Leon Scroggins III | c5a8366 | 2016-12-08 09:07:56 -0500 | [diff] [blame] | 673 | } else { |
| 674 | SkDebugf("[terminated] incremental decode failed with " |
| 675 | "error %d\n", result); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 676 | return; |
Leon Scroggins III | c5a8366 | 2016-12-08 09:07:56 -0500 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | SkDebugf("[terminated] Success!\n"); |
| 680 | break; |
| 681 | } |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 682 | default: |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 683 | SkDebugf("[terminated] Mode not implemented yet\n"); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | dump_png(bitmap); |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 687 | } |
| 688 | |
Zepeng Hu | fcb7ba0 | 2020-07-31 17:21:29 +0000 | [diff] [blame] | 689 | void FuzzSKP(sk_sp<SkData> bytes); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 690 | static void fuzz_skp(sk_sp<SkData> bytes) { |
Zepeng Hu | fcb7ba0 | 2020-07-31 17:21:29 +0000 | [diff] [blame] | 691 | FuzzSKP(bytes); |
| 692 | SkDebugf("[terminated] Finished SKP\n"); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 693 | } |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 694 | |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 695 | static void fuzz_color_deserialize(sk_sp<SkData> bytes) { |
kjlubick | 3e3c1a5 | 2016-06-23 10:49:27 -0700 | [diff] [blame] | 696 | sk_sp<SkColorSpace> space(SkColorSpace::Deserialize(bytes->data(), bytes->size())); |
| 697 | if (!space) { |
| 698 | SkDebugf("[terminated] Couldn't deserialize Colorspace.\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 699 | return; |
kjlubick | 3e3c1a5 | 2016-06-23 10:49:27 -0700 | [diff] [blame] | 700 | } |
| 701 | SkDebugf("[terminated] Success! deserialized Colorspace.\n"); |
kjlubick | 3e3c1a5 | 2016-06-23 10:49:27 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Kevin Lubick | f034d11 | 2018-02-08 14:31:24 -0500 | [diff] [blame] | 704 | void FuzzPathDeserialize(SkReadBuffer& buf); |
Kevin Lubick | c9d2829 | 2017-11-09 08:12:56 -0500 | [diff] [blame] | 705 | |
Kevin Lubick | f034d11 | 2018-02-08 14:31:24 -0500 | [diff] [blame] | 706 | static void fuzz_path_deserialize(sk_sp<SkData> bytes) { |
| 707 | SkReadBuffer buf(bytes->data(), bytes->size()); |
| 708 | FuzzPathDeserialize(buf); |
| 709 | SkDebugf("[terminated] path_deserialize didn't crash!\n"); |
Kevin Lubick | f04c50a | 2017-01-06 13:48:19 -0500 | [diff] [blame] | 710 | } |
| 711 | |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 712 | bool FuzzRegionDeserialize(sk_sp<SkData> bytes); |
| 713 | |
Kevin Lubick | edee1ae | 2017-02-20 17:47:18 -0500 | [diff] [blame] | 714 | static void fuzz_region_deserialize(sk_sp<SkData> bytes) { |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 715 | if (!FuzzRegionDeserialize(bytes)) { |
Kevin Lubick | edee1ae | 2017-02-20 17:47:18 -0500 | [diff] [blame] | 716 | SkDebugf("[terminated] Couldn't initialize SkRegion.\n"); |
| 717 | return; |
| 718 | } |
Kevin Lubick | edee1ae | 2017-02-20 17:47:18 -0500 | [diff] [blame] | 719 | SkDebugf("[terminated] Success! Initialized SkRegion.\n"); |
| 720 | } |
| 721 | |
Kevin Lubick | f034d11 | 2018-02-08 14:31:24 -0500 | [diff] [blame] | 722 | void FuzzTextBlobDeserialize(SkReadBuffer& buf); |
| 723 | |
Kevin Lubick | d46e85f | 2017-11-21 17:13:35 -0500 | [diff] [blame] | 724 | static void fuzz_textblob_deserialize(sk_sp<SkData> bytes) { |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 725 | SkReadBuffer buf(bytes->data(), bytes->size()); |
Kevin Lubick | f034d11 | 2018-02-08 14:31:24 -0500 | [diff] [blame] | 726 | FuzzTextBlobDeserialize(buf); |
| 727 | SkDebugf("[terminated] textblob didn't crash!\n"); |
Kevin Lubick | d46e85f | 2017-11-21 17:13:35 -0500 | [diff] [blame] | 728 | } |
| 729 | |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 730 | void FuzzRegionSetPath(Fuzz* fuzz); |
| 731 | |
| 732 | static void fuzz_region_set_path(sk_sp<SkData> bytes) { |
| 733 | Fuzz fuzz(bytes); |
| 734 | FuzzRegionSetPath(&fuzz); |
| 735 | SkDebugf("[terminated] region_set_path didn't crash!\n"); |
| 736 | } |
| 737 | |
Kevin Lubick | f034d11 | 2018-02-08 14:31:24 -0500 | [diff] [blame] | 738 | void FuzzImageFilterDeserialize(sk_sp<SkData> bytes); |
| 739 | |
Herb Derby | a839fc0 | 2017-03-16 12:30:43 -0400 | [diff] [blame] | 740 | static void fuzz_filter_fuzz(sk_sp<SkData> bytes) { |
Kevin Lubick | f034d11 | 2018-02-08 14:31:24 -0500 | [diff] [blame] | 741 | FuzzImageFilterDeserialize(bytes); |
| 742 | SkDebugf("[terminated] filter_fuzz didn't crash!\n"); |
Herb Derby | a839fc0 | 2017-03-16 12:30:43 -0400 | [diff] [blame] | 743 | } |
| 744 | |
Zepeng Hu | a5783f3 | 2020-07-10 13:36:20 +0000 | [diff] [blame] | 745 | bool FuzzSkRuntimeEffect(sk_sp<SkData> bytes); |
| 746 | |
| 747 | static void fuzz_skruntimeeffect(sk_sp<SkData> bytes) { |
| 748 | if (FuzzSkRuntimeEffect(bytes)) { |
| 749 | SkDebugf("[terminated] Success! Compiled and Executed sksl code.\n"); |
| 750 | } else { |
| 751 | SkDebugf("[terminated] Could not Compile or Execute sksl code.\n"); |
| 752 | } |
| 753 | } |
| 754 | |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 755 | bool FuzzSKSL2GLSL(sk_sp<SkData> bytes); |
| 756 | |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 757 | static void fuzz_sksl2glsl(sk_sp<SkData> bytes) { |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 758 | if (FuzzSKSL2GLSL(bytes)) { |
| 759 | SkDebugf("[terminated] Success! Compiled input to GLSL.\n"); |
| 760 | } else { |
| 761 | SkDebugf("[terminated] Could not compile input to GLSL.\n"); |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 762 | } |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 763 | } |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 764 | |
| 765 | bool FuzzSKSL2SPIRV(sk_sp<SkData> bytes); |
| 766 | |
| 767 | static void fuzz_sksl2spirv(sk_sp<SkData> bytes) { |
| 768 | if (FuzzSKSL2SPIRV(bytes)) { |
| 769 | SkDebugf("[terminated] Success! Compiled input to SPIRV.\n"); |
| 770 | } else { |
| 771 | SkDebugf("[terminated] Could not compile input to SPIRV.\n"); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | bool FuzzSKSL2Metal(sk_sp<SkData> bytes); |
| 776 | |
| 777 | static void fuzz_sksl2metal(sk_sp<SkData> bytes) { |
| 778 | if (FuzzSKSL2Metal(bytes)) { |
Kevin Lubick | 39cbe46 | 2019-03-11 15:38:00 -0400 | [diff] [blame] | 779 | SkDebugf("[terminated] Success! Compiled input to Metal.\n"); |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 780 | } else { |
Kevin Lubick | 39cbe46 | 2019-03-11 15:38:00 -0400 | [diff] [blame] | 781 | SkDebugf("[terminated] Could not compile input to Metal.\n"); |
Kevin Lubick | e9c1ce8 | 2019-03-11 11:09:40 -0400 | [diff] [blame] | 782 | } |
| 783 | } |
Kevin Lubick | 0f0a710 | 2019-03-18 16:20:55 -0400 | [diff] [blame] | 784 | |
| 785 | bool FuzzSKSL2Pipeline(sk_sp<SkData> bytes); |
| 786 | |
| 787 | static void fuzz_sksl2pipeline(sk_sp<SkData> bytes) { |
| 788 | if (FuzzSKSL2Pipeline(bytes)) { |
| 789 | SkDebugf("[terminated] Success! Compiled input to pipeline stage.\n"); |
| 790 | } else { |
| 791 | SkDebugf("[terminated] Could not compile input to pipeline stage.\n"); |
| 792 | } |
| 793 | } |
Kevin Lubick | 2be14d3 | 2019-10-21 13:44:48 -0400 | [diff] [blame] | 794 | |
| 795 | void FuzzSkDescriptorDeserialize(sk_sp<SkData> bytes); |
| 796 | |
| 797 | static void fuzz_skdescriptor_deserialize(sk_sp<SkData> bytes) { |
| 798 | FuzzSkDescriptorDeserialize(bytes); |
| 799 | SkDebugf("[terminated] Did not crash while deserializing an SkDescriptor.\n"); |
| 800 | } |
| 801 | |