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