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