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