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