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