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