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