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