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" |
Herb Derby | a839fc0 | 2017-03-16 12:30:43 -0400 | [diff] [blame] | 13 | #include "SkFlattenableSerialization.h" |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 14 | #include "SkImage.h" |
| 15 | #include "SkImageEncoder.h" |
Herb Derby | a839fc0 | 2017-03-16 12:30:43 -0400 | [diff] [blame] | 16 | #include "SkImageFilter.h" |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 17 | #include "SkMallocPixelRef.h" |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 18 | #include "SkOSFile.h" |
| 19 | #include "SkOSPath.h" |
Herb Derby | a839fc0 | 2017-03-16 12:30:43 -0400 | [diff] [blame] | 20 | #include "SkPaint.h" |
Hal Canary | 62176db | 2017-02-27 16:42:03 -0500 | [diff] [blame] | 21 | #include "SkPath.h" |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 22 | #include "SkPicture.h" |
Kevin Lubick | 0d82566 | 2018-01-03 14:38:48 -0500 | [diff] [blame] | 23 | #include "SkPipe.h" |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 24 | #include "SkReadBuffer.h" |
Hal Canary | 62176db | 2017-02-27 16:42:03 -0500 | [diff] [blame] | 25 | #include "SkRegion.h" |
| 26 | #include "SkStream.h" |
| 27 | #include "SkSurface.h" |
Kevin Lubick | d46e85f | 2017-11-21 17:13:35 -0500 | [diff] [blame] | 28 | #include "SkTextBlob.h" |
Hal Canary | 62176db | 2017-02-27 16:42:03 -0500 | [diff] [blame] | 29 | |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 30 | #if SK_SUPPORT_GPU |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 31 | #include "SkSLCompiler.h" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 32 | #endif |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 33 | |
Hal Canary | 62176db | 2017-02-27 16:42:03 -0500 | [diff] [blame] | 34 | #include <iostream> |
mtklein | a115942 | 2016-01-15 05:46:54 -0800 | [diff] [blame] | 35 | #include <signal.h> |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 36 | #include "sk_tool_utils.h" |
| 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."); |
| 46 | DEFINE_string2(type, t, "", "How to interpret --bytes, one of:\n" |
| 47 | "api\n" |
| 48 | "color_deserialize\n" |
| 49 | "filter_fuzz (equivalent to Chrome's filter_fuzz_stub)\n" |
| 50 | "icc\n" |
| 51 | "image_mode\n" |
| 52 | "image_scale\n" |
| 53 | "path_deserialize\n" |
| 54 | "pipe\n" |
| 55 | "region_deserialize\n" |
| 56 | "skp\n" |
| 57 | "sksl2glsl\n" |
| 58 | "textblob"); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 59 | |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 60 | static int fuzz_file(const char* path); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 61 | static uint8_t calculate_option(SkData*); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 62 | |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 63 | static void fuzz_api(sk_sp<SkData>); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 64 | static void fuzz_color_deserialize(sk_sp<SkData>); |
Kevin Lubick | d46e85f | 2017-11-21 17:13:35 -0500 | [diff] [blame] | 65 | static void fuzz_filter_fuzz(sk_sp<SkData>); |
Kevin Lubick | 0168e04 | 2017-02-14 13:12:37 -0500 | [diff] [blame] | 66 | static void fuzz_icc(sk_sp<SkData>); |
| 67 | static void fuzz_img(sk_sp<SkData>, uint8_t, uint8_t); |
Kevin Lubick | f04c50a | 2017-01-06 13:48:19 -0500 | [diff] [blame] | 68 | static void fuzz_path_deserialize(sk_sp<SkData>); |
Kevin Lubick | edee1ae | 2017-02-20 17:47:18 -0500 | [diff] [blame] | 69 | static void fuzz_region_deserialize(sk_sp<SkData>); |
Kevin Lubick | 0168e04 | 2017-02-14 13:12:37 -0500 | [diff] [blame] | 70 | static void fuzz_skp(sk_sp<SkData>); |
Kevin Lubick | 0d82566 | 2018-01-03 14:38:48 -0500 | [diff] [blame] | 71 | static void fuzz_skpipe(sk_sp<SkData>); |
Kevin Lubick | d46e85f | 2017-11-21 17:13:35 -0500 | [diff] [blame] | 72 | static void fuzz_textblob_deserialize(sk_sp<SkData>); |
Herb Derby | a839fc0 | 2017-03-16 12:30:43 -0400 | [diff] [blame] | 73 | |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 74 | #if SK_SUPPORT_GPU |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 75 | static void fuzz_sksl2glsl(sk_sp<SkData>); |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 76 | #endif |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 77 | |
| 78 | int main(int argc, char** argv) { |
Kevin Lubick | 9ff5dc9 | 2018-01-09 12:47:33 -0500 | [diff] [blame^] | 79 | SkCommandLineFlags::SetUsage("Usage: fuzz -t <type> -b <path/to/file> [-n api-to-fuzz]\n" |
| 80 | "--help lists the valid types\n"); |
mtklein | f5e9782 | 2016-01-15 06:19:53 -0800 | [diff] [blame] | 81 | SkCommandLineFlags::Parse(argc, argv); |
| 82 | |
mtklein | d0b8234 | 2016-01-15 07:56:20 -0800 | [diff] [blame] | 83 | const char* path = FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0]; |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 84 | |
| 85 | if (!sk_isdir(path)) { |
| 86 | return fuzz_file(path); |
| 87 | } |
| 88 | |
| 89 | SkOSFile::Iter it(path); |
| 90 | for (SkString file; it.next(&file); ) { |
| 91 | SkString p = SkOSPath::Join(path, file.c_str()); |
| 92 | SkDebugf("Fuzzing %s\n", p.c_str()); |
| 93 | int rv = fuzz_file(p.c_str()); |
| 94 | if (rv != 0) { |
| 95 | return rv; |
| 96 | } |
| 97 | } |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int fuzz_file(const char* path) { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 102 | sk_sp<SkData> bytes(SkData::MakeFromFileName(path)); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 103 | if (!bytes) { |
| 104 | SkDebugf("Could not read %s\n", path); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 105 | return 1; |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 106 | } |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 107 | |
mtklein | d4387ea | 2016-01-21 06:13:52 -0800 | [diff] [blame] | 108 | if (!FLAGS_type.isEmpty()) { |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 109 | if (0 == strcmp("api", FLAGS_type[0])) { |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 110 | fuzz_api(bytes); |
| 111 | return 0; |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 112 | } |
| 113 | if (0 == strcmp("color_deserialize", FLAGS_type[0])) { |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 114 | fuzz_color_deserialize(bytes); |
| 115 | return 0; |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 116 | } |
| 117 | if (0 == strcmp("icc", FLAGS_type[0])) { |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 118 | fuzz_icc(bytes); |
| 119 | return 0; |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 120 | } |
| 121 | if (0 == strcmp("image_scale", FLAGS_type[0])) { |
Kevin Lubick | c9d2829 | 2017-11-09 08:12:56 -0500 | [diff] [blame] | 122 | uint8_t option = calculate_option(bytes.get()); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 123 | fuzz_img(bytes, option, 0); |
| 124 | return 0; |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 125 | } |
| 126 | if (0 == strcmp("image_mode", FLAGS_type[0])) { |
Kevin Lubick | c9d2829 | 2017-11-09 08:12:56 -0500 | [diff] [blame] | 127 | uint8_t option = calculate_option(bytes.get()); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 128 | fuzz_img(bytes, 0, option); |
| 129 | return 0; |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 130 | } |
Kevin Lubick | f04c50a | 2017-01-06 13:48:19 -0500 | [diff] [blame] | 131 | if (0 == strcmp("path_deserialize", FLAGS_type[0])) { |
| 132 | fuzz_path_deserialize(bytes); |
| 133 | return 0; |
| 134 | } |
Kevin Lubick | edee1ae | 2017-02-20 17:47:18 -0500 | [diff] [blame] | 135 | if (0 == strcmp("region_deserialize", FLAGS_type[0])) { |
| 136 | fuzz_region_deserialize(bytes); |
| 137 | return 0; |
| 138 | } |
Kevin Lubick | 0d82566 | 2018-01-03 14:38:48 -0500 | [diff] [blame] | 139 | if (0 == strcmp("pipe", FLAGS_type[0])) { |
| 140 | fuzz_skpipe(bytes); |
| 141 | return 0; |
| 142 | } |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 143 | if (0 == strcmp("skp", FLAGS_type[0])) { |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 144 | fuzz_skp(bytes); |
| 145 | return 0; |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 146 | } |
Herb Derby | a839fc0 | 2017-03-16 12:30:43 -0400 | [diff] [blame] | 147 | if (0 == strcmp("filter_fuzz", FLAGS_type[0])) { |
| 148 | fuzz_filter_fuzz(bytes); |
| 149 | return 0; |
| 150 | } |
Kevin Lubick | d46e85f | 2017-11-21 17:13:35 -0500 | [diff] [blame] | 151 | if (0 == strcmp("textblob", FLAGS_type[0])) { |
| 152 | fuzz_textblob_deserialize(bytes); |
| 153 | return 0; |
| 154 | } |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 155 | #if SK_SUPPORT_GPU |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 156 | if (0 == strcmp("sksl2glsl", FLAGS_type[0])) { |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 157 | fuzz_sksl2glsl(bytes); |
| 158 | return 0; |
mtklein | d4387ea | 2016-01-21 06:13:52 -0800 | [diff] [blame] | 159 | } |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 160 | #endif |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 161 | } |
Kevin Lubick | 9ff5dc9 | 2018-01-09 12:47:33 -0500 | [diff] [blame^] | 162 | SkCommandLineFlags::PrintUsage(); |
| 163 | return 1; |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 164 | } |
| 165 | |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 166 | // This adds up the first 1024 bytes and returns it as an 8 bit integer. This allows afl-fuzz to |
| 167 | // deterministically excercise different paths, or *options* (such as different scaling sizes or |
| 168 | // different image modes) without needing to introduce a parameter. This way we don't need a |
| 169 | // image_scale1, image_scale2, image_scale4, etc fuzzer, we can just have a image_scale fuzzer. |
| 170 | // Clients are expected to transform this number into a different range, e.g. with modulo (%). |
| 171 | static uint8_t calculate_option(SkData* bytes) { |
| 172 | uint8_t total = 0; |
| 173 | const uint8_t* data = bytes->bytes(); |
| 174 | for (size_t i = 0; i < 1024 && i < bytes->size(); i++) { |
| 175 | total += data[i]; |
| 176 | } |
| 177 | return total; |
| 178 | } |
| 179 | |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 180 | static void fuzz_api(sk_sp<SkData> bytes) { |
mtklein | d4387ea | 2016-01-21 06:13:52 -0800 | [diff] [blame] | 181 | const char* name = FLAGS_name.isEmpty() ? "" : FLAGS_name[0]; |
| 182 | |
Mike Reed | ab273fa | 2017-01-11 13:58:55 -0500 | [diff] [blame] | 183 | for (auto r = sk_tools::Registry<Fuzzable>::Head(); r; r = r->next()) { |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 184 | auto fuzzable = r->factory(); |
mtklein | d4387ea | 2016-01-21 06:13:52 -0800 | [diff] [blame] | 185 | if (0 == strcmp(name, fuzzable.name)) { |
mtklein | f5e9782 | 2016-01-15 06:19:53 -0800 | [diff] [blame] | 186 | SkDebugf("Fuzzing %s...\n", fuzzable.name); |
Kevin Lubick | 1ac8fd2 | 2017-03-01 10:42:45 -0500 | [diff] [blame] | 187 | Fuzz fuzz(std::move(bytes)); |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 188 | fuzzable.fn(&fuzz); |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 189 | SkDebugf("[terminated] Success!\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 190 | return; |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
mtklein | d4387ea | 2016-01-21 06:13:52 -0800 | [diff] [blame] | 193 | |
| 194 | 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] | 195 | for (auto r = sk_tools::Registry<Fuzzable>::Head(); r; r = r->next()) { |
mtklein | d4387ea | 2016-01-21 06:13:52 -0800 | [diff] [blame] | 196 | auto fuzzable = r->factory(); |
| 197 | SkDebugf("\t%s\n", fuzzable.name); |
| 198 | } |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static void dump_png(SkBitmap bitmap) { |
| 202 | if (!FLAGS_dump.isEmpty()) { |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 203 | sk_tool_utils::EncodeImageToFile(FLAGS_dump[0], bitmap, SkEncodedImageFormat::kPNG, 100); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 204 | SkDebugf("Dumped to %s\n", FLAGS_dump[0]); |
| 205 | } |
| 206 | } |
| 207 | |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 208 | 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] | 209 | // We can scale 1x, 2x, 4x, 8x, 16x |
| 210 | scale = scale % 5; |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 211 | float fscale = (float)pow(2.0f, scale); |
| 212 | SkDebugf("Scaling factor: %f\n", fscale); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 213 | |
Leon Scroggins III | c5a8366 | 2016-12-08 09:07:56 -0500 | [diff] [blame] | 214 | // We have 5 different modes of decoding. |
| 215 | mode = mode % 5; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 216 | SkDebugf("Mode: %d\n", mode); |
| 217 | |
| 218 | // This is mostly copied from DMSrcSink's CodecSrc::draw method. |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 219 | SkDebugf("Decoding\n"); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 220 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(bytes)); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 221 | if (nullptr == codec.get()) { |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 222 | SkDebugf("[terminated] Couldn't create codec.\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 223 | return; |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | SkImageInfo decodeInfo = codec->getInfo(); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 227 | SkISize size = codec->getScaledDimensions(fscale); |
| 228 | decodeInfo = decodeInfo.makeWH(size.width(), size.height()); |
| 229 | |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 230 | SkBitmap bitmap; |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 231 | SkCodec::Options options; |
| 232 | options.fZeroInitialized = SkCodec::kYes_ZeroInitialized; |
| 233 | |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 234 | if (!bitmap.tryAllocPixelsFlags(decodeInfo, SkBitmap::kZeroPixels_AllocFlag)) { |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 235 | 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] | 236 | decodeInfo.width(), decodeInfo.height()); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 237 | return; |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 238 | } |
| 239 | |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 240 | switch (mode) { |
| 241 | case 0: {//kCodecZeroInit_Mode, kCodec_Mode |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 242 | switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options)) { |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 243 | case SkCodec::kSuccess: |
| 244 | SkDebugf("[terminated] Success!\n"); |
| 245 | break; |
| 246 | case SkCodec::kIncompleteInput: |
| 247 | SkDebugf("[terminated] Partial Success\n"); |
| 248 | break; |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 249 | case SkCodec::kErrorInInput: |
| 250 | SkDebugf("[terminated] Partial Success with error\n"); |
| 251 | break; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 252 | case SkCodec::kInvalidConversion: |
| 253 | SkDebugf("Incompatible colortype conversion\n"); |
| 254 | // Crash to allow afl-fuzz to know this was a bug. |
| 255 | raise(SIGSEGV); |
| 256 | default: |
| 257 | SkDebugf("[terminated] Couldn't getPixels.\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 258 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 259 | } |
| 260 | break; |
| 261 | } |
| 262 | case 1: {//kScanline_Mode |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 263 | if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)) { |
| 264 | SkDebugf("[terminated] Could not start scanline decoder\n"); |
| 265 | return; |
| 266 | } |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 267 | |
| 268 | void* dst = bitmap.getAddr(0, 0); |
| 269 | size_t rowBytes = bitmap.rowBytes(); |
| 270 | uint32_t height = decodeInfo.height(); |
| 271 | switch (codec->getScanlineOrder()) { |
| 272 | case SkCodec::kTopDown_SkScanlineOrder: |
| 273 | case SkCodec::kBottomUp_SkScanlineOrder: |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 274 | // We do not need to check the return value. On an incomplete |
| 275 | // image, memory will be filled with a default value. |
| 276 | codec->getScanlines(dst, height, rowBytes); |
| 277 | break; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 278 | } |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 279 | SkDebugf("[terminated] Success!\n"); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 280 | break; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 281 | } |
| 282 | case 2: { //kStripe_Mode |
| 283 | const int height = decodeInfo.height(); |
| 284 | // This value is chosen arbitrarily. We exercise more cases by choosing a value that |
| 285 | // does not align with image blocks. |
| 286 | const int stripeHeight = 37; |
| 287 | const int numStripes = (height + stripeHeight - 1) / stripeHeight; |
| 288 | |
| 289 | // Decode odd stripes |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 290 | if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo) |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 291 | || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) { |
| 292 | // This mode was designed to test the new skip scanlines API in libjpeg-turbo. |
| 293 | // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting |
| 294 | // to run this test for image types that do not have this scanline ordering. |
| 295 | SkDebugf("[terminated] Could not start top-down scanline decoder\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 296 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | for (int i = 0; i < numStripes; i += 2) { |
| 300 | // Skip a stripe |
| 301 | const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight); |
| 302 | codec->skipScanlines(linesToSkip); |
| 303 | |
| 304 | // Read a stripe |
| 305 | const int startY = (i + 1) * stripeHeight; |
| 306 | const int linesToRead = SkTMin(stripeHeight, height - startY); |
| 307 | if (linesToRead > 0) { |
| 308 | codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes()); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | // Decode even stripes |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 313 | const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 314 | if (SkCodec::kSuccess != startResult) { |
| 315 | SkDebugf("[terminated] Failed to restart scanline decoder with same parameters.\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 316 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 317 | } |
| 318 | for (int i = 0; i < numStripes; i += 2) { |
| 319 | // Read a stripe |
| 320 | const int startY = i * stripeHeight; |
| 321 | const int linesToRead = SkTMin(stripeHeight, height - startY); |
| 322 | codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes()); |
| 323 | |
| 324 | // Skip a stripe |
| 325 | const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight); |
| 326 | if (linesToSkip > 0) { |
| 327 | codec->skipScanlines(linesToSkip); |
| 328 | } |
| 329 | } |
| 330 | SkDebugf("[terminated] Success!\n"); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 331 | break; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 332 | } |
| 333 | case 3: { //kSubset_Mode |
| 334 | // Arbitrarily choose a divisor. |
| 335 | int divisor = 2; |
| 336 | // Total width/height of the image. |
| 337 | const int W = codec->getInfo().width(); |
| 338 | const int H = codec->getInfo().height(); |
| 339 | if (divisor > W || divisor > H) { |
| 340 | SkDebugf("[terminated] Cannot codec subset: divisor %d is too big " |
| 341 | "with dimensions (%d x %d)\n", divisor, W, H); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 342 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 343 | } |
| 344 | // subset dimensions |
| 345 | // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries. |
| 346 | const int w = SkAlign2(W / divisor); |
| 347 | const int h = SkAlign2(H / divisor); |
| 348 | SkIRect subset; |
| 349 | SkCodec::Options opts; |
| 350 | opts.fSubset = ⊂ |
| 351 | SkBitmap subsetBm; |
| 352 | // We will reuse pixel memory from bitmap. |
| 353 | void* pixels = bitmap.getPixels(); |
| 354 | // Keep track of left and top (for drawing subsetBm into canvas). We could use |
| 355 | // fscale * x and fscale * y, but we want integers such that the next subset will start |
| 356 | // where the last one ended. So we'll add decodeInfo.width() and height(). |
| 357 | int left = 0; |
| 358 | for (int x = 0; x < W; x += w) { |
| 359 | int top = 0; |
| 360 | for (int y = 0; y < H; y+= h) { |
| 361 | // Do not make the subset go off the edge of the image. |
| 362 | const int preScaleW = SkTMin(w, W - x); |
| 363 | const int preScaleH = SkTMin(h, H - y); |
| 364 | subset.setXYWH(x, y, preScaleW, preScaleH); |
| 365 | // And fscale |
| 366 | // FIXME: Should we have a version of getScaledDimensions that takes a subset |
| 367 | // into account? |
| 368 | decodeInfo = decodeInfo.makeWH( |
| 369 | SkTMax(1, SkScalarRoundToInt(preScaleW * fscale)), |
| 370 | SkTMax(1, SkScalarRoundToInt(preScaleH * fscale))); |
| 371 | size_t rowBytes = decodeInfo.minRowBytes(); |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 372 | if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes)) { |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 373 | SkDebugf("[terminated] Could not install pixels.\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 374 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 375 | } |
| 376 | const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 377 | &opts); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 378 | switch (result) { |
| 379 | case SkCodec::kSuccess: |
| 380 | case SkCodec::kIncompleteInput: |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 381 | case SkCodec::kErrorInInput: |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 382 | SkDebugf("okay\n"); |
| 383 | break; |
| 384 | case SkCodec::kInvalidConversion: |
| 385 | if (0 == (x|y)) { |
| 386 | // First subset is okay to return unimplemented. |
| 387 | SkDebugf("[terminated] Incompatible colortype conversion\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 388 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 389 | } |
| 390 | // If the first subset succeeded, a later one should not fail. |
| 391 | // fall through to failure |
| 392 | case SkCodec::kUnimplemented: |
| 393 | if (0 == (x|y)) { |
| 394 | // First subset is okay to return unimplemented. |
| 395 | SkDebugf("[terminated] subset codec not supported\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 396 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 397 | } |
| 398 | // If the first subset succeeded, why would a later one fail? |
| 399 | // fall through to failure |
| 400 | default: |
| 401 | SkDebugf("[terminated] subset codec failed to decode (%d, %d, %d, %d) " |
| 402 | "with dimensions (%d x %d)\t error %d\n", |
| 403 | x, y, decodeInfo.width(), decodeInfo.height(), |
| 404 | W, H, result); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 405 | return; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 406 | } |
| 407 | // translate by the scaled height. |
| 408 | top += decodeInfo.height(); |
| 409 | } |
| 410 | // translate by the scaled width. |
| 411 | left += decodeInfo.width(); |
| 412 | } |
| 413 | SkDebugf("[terminated] Success!\n"); |
| 414 | break; |
| 415 | } |
Leon Scroggins III | c5a8366 | 2016-12-08 09:07:56 -0500 | [diff] [blame] | 416 | case 4: { //kAnimated_Mode |
| 417 | std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo(); |
| 418 | if (frameInfos.size() == 0) { |
| 419 | SkDebugf("[terminated] Not an animated image\n"); |
| 420 | break; |
| 421 | } |
| 422 | |
| 423 | for (size_t i = 0; i < frameInfos.size(); i++) { |
| 424 | options.fFrameIndex = i; |
| 425 | auto result = codec->startIncrementalDecode(decodeInfo, bitmap.getPixels(), |
| 426 | bitmap.rowBytes(), &options); |
| 427 | if (SkCodec::kSuccess != result) { |
| 428 | SkDebugf("[terminated] failed to start incremental decode " |
| 429 | "in frame %d with error %d\n", i, result); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 430 | return; |
Leon Scroggins III | c5a8366 | 2016-12-08 09:07:56 -0500 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | result = codec->incrementalDecode(); |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 434 | if (result == SkCodec::kIncompleteInput || result == SkCodec::kErrorInInput) { |
Leon Scroggins III | c5a8366 | 2016-12-08 09:07:56 -0500 | [diff] [blame] | 435 | SkDebugf("okay\n"); |
| 436 | // Frames beyond this one will not decode. |
| 437 | break; |
| 438 | } |
| 439 | if (result == SkCodec::kSuccess) { |
| 440 | SkDebugf("okay - decoded frame %d\n", i); |
| 441 | } else { |
| 442 | SkDebugf("[terminated] incremental decode failed with " |
| 443 | "error %d\n", result); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 444 | return; |
Leon Scroggins III | c5a8366 | 2016-12-08 09:07:56 -0500 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | SkDebugf("[terminated] Success!\n"); |
| 448 | break; |
| 449 | } |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 450 | default: |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 451 | SkDebugf("[terminated] Mode not implemented yet\n"); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | dump_png(bitmap); |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 455 | } |
| 456 | |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 457 | static void fuzz_skp(sk_sp<SkData> bytes) { |
Kevin Lubick | 09757b2 | 2017-12-12 12:52:39 -0500 | [diff] [blame] | 458 | SkReadBuffer buf(bytes->data(), bytes->size()); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 459 | SkDebugf("Decoding\n"); |
Kevin Lubick | 09757b2 | 2017-12-12 12:52:39 -0500 | [diff] [blame] | 460 | sk_sp<SkPicture> pic(SkPicture::MakeFromBuffer(buf)); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 461 | if (!pic) { |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 462 | SkDebugf("[terminated] Couldn't decode as a picture.\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 463 | return; |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 464 | } |
| 465 | SkDebugf("Rendering\n"); |
| 466 | SkBitmap bitmap; |
| 467 | if (!FLAGS_dump.isEmpty()) { |
| 468 | SkIRect size = pic->cullRect().roundOut(); |
| 469 | bitmap.allocN32Pixels(size.width(), size.height()); |
| 470 | } |
| 471 | SkCanvas canvas(bitmap); |
| 472 | canvas.drawPicture(pic); |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 473 | SkDebugf("[terminated] Success! Decoded and rendered an SkPicture!\n"); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 474 | dump_png(bitmap); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 475 | } |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 476 | |
Kevin Lubick | 0d82566 | 2018-01-03 14:38:48 -0500 | [diff] [blame] | 477 | static void fuzz_skpipe(sk_sp<SkData> bytes) { |
| 478 | SkPipeDeserializer d; |
| 479 | SkDebugf("Decoding\n"); |
| 480 | sk_sp<SkPicture> pic(d.readPicture(bytes.get())); |
| 481 | if (!pic) { |
| 482 | SkDebugf("[terminated] Couldn't decode picture via SkPipe.\n"); |
| 483 | return; |
| 484 | } |
| 485 | SkDebugf("Rendering\n"); |
| 486 | SkBitmap bitmap; |
| 487 | SkCanvas canvas(bitmap); |
| 488 | canvas.drawPicture(pic); |
| 489 | SkDebugf("[terminated] Success! Decoded and rendered an SkPicture from SkPipe!\n"); |
| 490 | } |
| 491 | |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 492 | static void fuzz_icc(sk_sp<SkData> bytes) { |
Brian Osman | 526972e | 2016-10-24 09:24:02 -0400 | [diff] [blame] | 493 | sk_sp<SkColorSpace> space(SkColorSpace::MakeICC(bytes->data(), bytes->size())); |
kjlubick | 897a8e3 | 2016-06-09 07:15:12 -0700 | [diff] [blame] | 494 | if (!space) { |
| 495 | SkDebugf("[terminated] Couldn't decode ICC.\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 496 | return; |
kjlubick | 897a8e3 | 2016-06-09 07:15:12 -0700 | [diff] [blame] | 497 | } |
| 498 | SkDebugf("[terminated] Success! Decoded ICC.\n"); |
kjlubick | 897a8e3 | 2016-06-09 07:15:12 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 501 | static void fuzz_color_deserialize(sk_sp<SkData> bytes) { |
kjlubick | 3e3c1a5 | 2016-06-23 10:49:27 -0700 | [diff] [blame] | 502 | sk_sp<SkColorSpace> space(SkColorSpace::Deserialize(bytes->data(), bytes->size())); |
| 503 | if (!space) { |
| 504 | SkDebugf("[terminated] Couldn't deserialize Colorspace.\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 505 | return; |
kjlubick | 3e3c1a5 | 2016-06-23 10:49:27 -0700 | [diff] [blame] | 506 | } |
| 507 | SkDebugf("[terminated] Success! deserialized Colorspace.\n"); |
kjlubick | 3e3c1a5 | 2016-06-23 10:49:27 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Kevin Lubick | f04c50a | 2017-01-06 13:48:19 -0500 | [diff] [blame] | 510 | static void fuzz_path_deserialize(sk_sp<SkData> bytes) { |
| 511 | SkPath path; |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 512 | SkReadBuffer buf(bytes->data(), bytes->size()); |
Kevin Lubick | c9d2829 | 2017-11-09 08:12:56 -0500 | [diff] [blame] | 513 | buf.readPath(&path); |
| 514 | if (!buf.isValid()) { |
| 515 | SkDebugf("[terminated] Couldn't deserialize SkPath.\n"); |
Kevin Lubick | f04c50a | 2017-01-06 13:48:19 -0500 | [diff] [blame] | 516 | return; |
| 517 | } |
Kevin Lubick | c9d2829 | 2017-11-09 08:12:56 -0500 | [diff] [blame] | 518 | |
Kevin Lubick | 0d4bc0d | 2017-02-21 09:37:48 -0500 | [diff] [blame] | 519 | auto s = SkSurface::MakeRasterN32Premul(1024, 1024); |
| 520 | s->getCanvas()->drawPath(path, SkPaint()); |
Kevin Lubick | f04c50a | 2017-01-06 13:48:19 -0500 | [diff] [blame] | 521 | SkDebugf("[terminated] Success! Initialized SkPath.\n"); |
| 522 | } |
| 523 | |
Kevin Lubick | edee1ae | 2017-02-20 17:47:18 -0500 | [diff] [blame] | 524 | static void fuzz_region_deserialize(sk_sp<SkData> bytes) { |
| 525 | SkRegion region; |
| 526 | if (!region.readFromMemory(bytes->data(), bytes->size())) { |
| 527 | SkDebugf("[terminated] Couldn't initialize SkRegion.\n"); |
| 528 | return; |
| 529 | } |
| 530 | region.computeRegionComplexity(); |
| 531 | region.isComplex(); |
| 532 | SkRegion r2; |
| 533 | if (region == r2) { |
| 534 | region.contains(0,0); |
| 535 | } else { |
| 536 | region.contains(1,1); |
| 537 | } |
| 538 | auto s = SkSurface::MakeRasterN32Premul(1024, 1024); |
| 539 | s->getCanvas()->drawRegion(region, SkPaint()); |
| 540 | SkDEBUGCODE(region.validate()); |
| 541 | SkDebugf("[terminated] Success! Initialized SkRegion.\n"); |
| 542 | } |
| 543 | |
Kevin Lubick | d46e85f | 2017-11-21 17:13:35 -0500 | [diff] [blame] | 544 | static void fuzz_textblob_deserialize(sk_sp<SkData> bytes) { |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 545 | SkReadBuffer buf(bytes->data(), bytes->size()); |
Kevin Lubick | d46e85f | 2017-11-21 17:13:35 -0500 | [diff] [blame] | 546 | auto tb = SkTextBlob::MakeFromBuffer(buf); |
| 547 | if (!buf.isValid()) { |
| 548 | SkDebugf("[terminated] Couldn't deserialize SkTextBlob.\n"); |
| 549 | return; |
| 550 | } |
| 551 | |
| 552 | auto s = SkSurface::MakeRasterN32Premul(512, 512); |
| 553 | s->getCanvas()->drawTextBlob(tb, 200, 200, SkPaint()); |
| 554 | SkDebugf("[terminated] Success! Initialized SkTextBlob.\n"); |
| 555 | } |
| 556 | |
Herb Derby | a839fc0 | 2017-03-16 12:30:43 -0400 | [diff] [blame] | 557 | static void fuzz_filter_fuzz(sk_sp<SkData> bytes) { |
| 558 | |
| 559 | const int BitmapSize = 24; |
| 560 | SkBitmap bitmap; |
| 561 | bitmap.allocN32Pixels(BitmapSize, BitmapSize); |
| 562 | SkCanvas canvas(bitmap); |
| 563 | canvas.clear(0x00000000); |
| 564 | |
| 565 | sk_sp<SkImageFilter> flattenable = SkValidatingDeserializeImageFilter( |
| 566 | bytes->data(), bytes->size()); |
| 567 | |
| 568 | // Adding some info, but the test passed if we got here without any trouble |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 569 | if (flattenable != nullptr) { |
Herb Derby | a839fc0 | 2017-03-16 12:30:43 -0400 | [diff] [blame] | 570 | SkDebugf("Valid stream detected.\n"); |
| 571 | // Let's see if using the filters can cause any trouble... |
| 572 | SkPaint paint; |
| 573 | paint.setImageFilter(flattenable); |
| 574 | canvas.save(); |
| 575 | canvas.clipRect(SkRect::MakeXYWH( |
| 576 | 0, 0, SkIntToScalar(BitmapSize), SkIntToScalar(BitmapSize))); |
| 577 | |
| 578 | // This call shouldn't crash or cause ASAN to flag any memory issues |
| 579 | // If nothing bad happens within this call, everything is fine |
| 580 | canvas.drawBitmap(bitmap, 0, 0, &paint); |
| 581 | |
| 582 | SkDebugf("Filter DAG rendered successfully\n"); |
| 583 | canvas.restore(); |
| 584 | } else { |
| 585 | SkDebugf("Invalid stream detected.\n"); |
| 586 | } |
| 587 | |
| 588 | SkDebugf("[terminated] Done\n"); |
| 589 | } |
| 590 | |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 591 | #if SK_SUPPORT_GPU |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 592 | static void fuzz_sksl2glsl(sk_sp<SkData> bytes) { |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 593 | SkSL::Compiler compiler; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 594 | SkSL::String output; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 595 | SkSL::Program::Settings settings; |
| 596 | sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default(); |
| 597 | settings.fCaps = caps.get(); |
| 598 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind, |
Brian Osman | 93ba0a4 | 2017-08-14 14:48:10 -0400 | [diff] [blame] | 599 | SkSL::String((const char*) bytes->data()), |
| 600 | settings); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 601 | if (!program || !compiler.toGLSL(*program, &output)) { |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 602 | SkDebugf("[terminated] Couldn't compile input.\n"); |
Kevin Lubick | f80f115 | 2017-01-06 08:26:56 -0500 | [diff] [blame] | 603 | return; |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 604 | } |
| 605 | SkDebugf("[terminated] Success! Compiled input.\n"); |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 606 | } |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 607 | #endif |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 608 | |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 609 | Fuzz::Fuzz(sk_sp<SkData> bytes) : fBytes(bytes), fNextByte(0) {} |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 610 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 611 | void Fuzz::signalBug() { SkDebugf("Signal bug\n"); raise(SIGSEGV); } |
mtklein | a115942 | 2016-01-15 05:46:54 -0800 | [diff] [blame] | 612 | |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 613 | size_t Fuzz::size() { return fBytes->size(); } |
| 614 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 615 | bool Fuzz::exhausted() { |
| 616 | return fBytes->size() == fNextByte; |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 617 | } |