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