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 | 248ff02 | 2016-11-22 09:03:03 -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 | 248ff02 | 2016-11-22 09:03:03 -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 | |
| 138 | // We have 4 different modes of decoding, just like DM. |
| 139 | mode = mode % 4; |
| 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(); |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 151 | |
| 152 | SkISize size = codec->getScaledDimensions(fscale); |
| 153 | decodeInfo = decodeInfo.makeWH(size.width(), size.height()); |
| 154 | |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 155 | // Construct a color table for the decode if necessary |
Hal Canary | 2db8361 | 2016-11-04 13:02:54 -0400 | [diff] [blame] | 156 | sk_sp<SkColorTable> colorTable(nullptr); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 157 | SkPMColor* colorPtr = nullptr; |
| 158 | int* colorCountPtr = nullptr; |
| 159 | int maxColors = 256; |
| 160 | if (kIndex_8_SkColorType == decodeInfo.colorType()) { |
| 161 | SkPMColor colors[256]; |
| 162 | colorTable.reset(new SkColorTable(colors, maxColors)); |
| 163 | colorPtr = const_cast<SkPMColor*>(colorTable->readColors()); |
| 164 | colorCountPtr = &maxColors; |
| 165 | } |
| 166 | |
| 167 | SkBitmap bitmap; |
| 168 | SkMallocPixelRef::ZeroedPRFactory zeroFactory; |
| 169 | SkCodec::Options options; |
| 170 | options.fZeroInitialized = SkCodec::kYes_ZeroInitialized; |
| 171 | |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 172 | if (!bitmap.tryAllocPixels(decodeInfo, &zeroFactory, colorTable.get())) { |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 173 | 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] | 174 | decodeInfo.width(), decodeInfo.height()); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 175 | return 4; |
| 176 | } |
| 177 | |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 178 | switch (mode) { |
| 179 | case 0: {//kCodecZeroInit_Mode, kCodec_Mode |
| 180 | switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options, |
| 181 | colorPtr, colorCountPtr)) { |
| 182 | case SkCodec::kSuccess: |
| 183 | SkDebugf("[terminated] Success!\n"); |
| 184 | break; |
| 185 | case SkCodec::kIncompleteInput: |
| 186 | SkDebugf("[terminated] Partial Success\n"); |
| 187 | break; |
| 188 | case SkCodec::kInvalidConversion: |
| 189 | SkDebugf("Incompatible colortype conversion\n"); |
| 190 | // Crash to allow afl-fuzz to know this was a bug. |
| 191 | raise(SIGSEGV); |
| 192 | default: |
| 193 | SkDebugf("[terminated] Couldn't getPixels.\n"); |
| 194 | return 6; |
| 195 | } |
| 196 | break; |
| 197 | } |
| 198 | case 1: {//kScanline_Mode |
| 199 | if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr, |
| 200 | colorCountPtr)) { |
| 201 | SkDebugf("[terminated] Could not start scanline decoder\n"); |
| 202 | return 7; |
| 203 | } |
| 204 | |
| 205 | void* dst = bitmap.getAddr(0, 0); |
| 206 | size_t rowBytes = bitmap.rowBytes(); |
| 207 | uint32_t height = decodeInfo.height(); |
| 208 | switch (codec->getScanlineOrder()) { |
| 209 | case SkCodec::kTopDown_SkScanlineOrder: |
| 210 | case SkCodec::kBottomUp_SkScanlineOrder: |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 211 | // We do not need to check the return value. On an incomplete |
| 212 | // image, memory will be filled with a default value. |
| 213 | codec->getScanlines(dst, height, rowBytes); |
| 214 | break; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 215 | } |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 216 | SkDebugf("[terminated] Success!\n"); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 217 | break; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 218 | } |
| 219 | case 2: { //kStripe_Mode |
| 220 | const int height = decodeInfo.height(); |
| 221 | // This value is chosen arbitrarily. We exercise more cases by choosing a value that |
| 222 | // does not align with image blocks. |
| 223 | const int stripeHeight = 37; |
| 224 | const int numStripes = (height + stripeHeight - 1) / stripeHeight; |
| 225 | |
| 226 | // Decode odd stripes |
| 227 | if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr, |
| 228 | colorCountPtr) |
| 229 | || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) { |
| 230 | // This mode was designed to test the new skip scanlines API in libjpeg-turbo. |
| 231 | // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting |
| 232 | // to run this test for image types that do not have this scanline ordering. |
| 233 | SkDebugf("[terminated] Could not start top-down scanline decoder\n"); |
| 234 | return 8; |
| 235 | } |
| 236 | |
| 237 | for (int i = 0; i < numStripes; i += 2) { |
| 238 | // Skip a stripe |
| 239 | const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight); |
| 240 | codec->skipScanlines(linesToSkip); |
| 241 | |
| 242 | // Read a stripe |
| 243 | const int startY = (i + 1) * stripeHeight; |
| 244 | const int linesToRead = SkTMin(stripeHeight, height - startY); |
| 245 | if (linesToRead > 0) { |
| 246 | codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes()); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | // Decode even stripes |
| 251 | const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo, nullptr, |
| 252 | colorPtr, colorCountPtr); |
| 253 | if (SkCodec::kSuccess != startResult) { |
| 254 | SkDebugf("[terminated] Failed to restart scanline decoder with same parameters.\n"); |
| 255 | return 9; |
| 256 | } |
| 257 | for (int i = 0; i < numStripes; i += 2) { |
| 258 | // Read a stripe |
| 259 | const int startY = i * stripeHeight; |
| 260 | const int linesToRead = SkTMin(stripeHeight, height - startY); |
| 261 | codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes()); |
| 262 | |
| 263 | // Skip a stripe |
| 264 | const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight); |
| 265 | if (linesToSkip > 0) { |
| 266 | codec->skipScanlines(linesToSkip); |
| 267 | } |
| 268 | } |
| 269 | SkDebugf("[terminated] Success!\n"); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 270 | break; |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 271 | } |
| 272 | case 3: { //kSubset_Mode |
| 273 | // Arbitrarily choose a divisor. |
| 274 | int divisor = 2; |
| 275 | // Total width/height of the image. |
| 276 | const int W = codec->getInfo().width(); |
| 277 | const int H = codec->getInfo().height(); |
| 278 | if (divisor > W || divisor > H) { |
| 279 | SkDebugf("[terminated] Cannot codec subset: divisor %d is too big " |
| 280 | "with dimensions (%d x %d)\n", divisor, W, H); |
| 281 | return 10; |
| 282 | } |
| 283 | // subset dimensions |
| 284 | // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries. |
| 285 | const int w = SkAlign2(W / divisor); |
| 286 | const int h = SkAlign2(H / divisor); |
| 287 | SkIRect subset; |
| 288 | SkCodec::Options opts; |
| 289 | opts.fSubset = ⊂ |
| 290 | SkBitmap subsetBm; |
| 291 | // We will reuse pixel memory from bitmap. |
| 292 | void* pixels = bitmap.getPixels(); |
| 293 | // Keep track of left and top (for drawing subsetBm into canvas). We could use |
| 294 | // fscale * x and fscale * y, but we want integers such that the next subset will start |
| 295 | // where the last one ended. So we'll add decodeInfo.width() and height(). |
| 296 | int left = 0; |
| 297 | for (int x = 0; x < W; x += w) { |
| 298 | int top = 0; |
| 299 | for (int y = 0; y < H; y+= h) { |
| 300 | // Do not make the subset go off the edge of the image. |
| 301 | const int preScaleW = SkTMin(w, W - x); |
| 302 | const int preScaleH = SkTMin(h, H - y); |
| 303 | subset.setXYWH(x, y, preScaleW, preScaleH); |
| 304 | // And fscale |
| 305 | // FIXME: Should we have a version of getScaledDimensions that takes a subset |
| 306 | // into account? |
| 307 | decodeInfo = decodeInfo.makeWH( |
| 308 | SkTMax(1, SkScalarRoundToInt(preScaleW * fscale)), |
| 309 | SkTMax(1, SkScalarRoundToInt(preScaleH * fscale))); |
| 310 | size_t rowBytes = decodeInfo.minRowBytes(); |
| 311 | if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes, colorTable.get(), |
| 312 | nullptr, nullptr)) { |
| 313 | SkDebugf("[terminated] Could not install pixels.\n"); |
| 314 | return 11; |
| 315 | } |
| 316 | const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes, |
| 317 | &opts, colorPtr, colorCountPtr); |
| 318 | switch (result) { |
| 319 | case SkCodec::kSuccess: |
| 320 | case SkCodec::kIncompleteInput: |
| 321 | SkDebugf("okay\n"); |
| 322 | break; |
| 323 | case SkCodec::kInvalidConversion: |
| 324 | if (0 == (x|y)) { |
| 325 | // First subset is okay to return unimplemented. |
| 326 | SkDebugf("[terminated] Incompatible colortype conversion\n"); |
| 327 | return 12; |
| 328 | } |
| 329 | // If the first subset succeeded, a later one should not fail. |
| 330 | // fall through to failure |
| 331 | case SkCodec::kUnimplemented: |
| 332 | if (0 == (x|y)) { |
| 333 | // First subset is okay to return unimplemented. |
| 334 | SkDebugf("[terminated] subset codec not supported\n"); |
| 335 | return 13; |
| 336 | } |
| 337 | // If the first subset succeeded, why would a later one fail? |
| 338 | // fall through to failure |
| 339 | default: |
| 340 | SkDebugf("[terminated] subset codec failed to decode (%d, %d, %d, %d) " |
| 341 | "with dimensions (%d x %d)\t error %d\n", |
| 342 | x, y, decodeInfo.width(), decodeInfo.height(), |
| 343 | W, H, result); |
| 344 | return 14; |
| 345 | } |
| 346 | // translate by the scaled height. |
| 347 | top += decodeInfo.height(); |
| 348 | } |
| 349 | // translate by the scaled width. |
| 350 | left += decodeInfo.width(); |
| 351 | } |
| 352 | SkDebugf("[terminated] Success!\n"); |
| 353 | break; |
| 354 | } |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 355 | default: |
kjlubick | 2a42f48 | 2016-02-16 16:14:23 -0800 | [diff] [blame] | 356 | SkDebugf("[terminated] Mode not implemented yet\n"); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | dump_png(bitmap); |
mtklein | f5e9782 | 2016-01-15 06:19:53 -0800 | [diff] [blame] | 360 | return 0; |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 361 | } |
| 362 | |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 363 | int fuzz_skp(sk_sp<SkData> bytes) { |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 364 | SkMemoryStream stream(bytes); |
| 365 | SkDebugf("Decoding\n"); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 366 | sk_sp<SkPicture> pic(SkPicture::MakeFromStream(&stream)); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 367 | if (!pic) { |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 368 | SkDebugf("[terminated] Couldn't decode as a picture.\n"); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 369 | return 3; |
| 370 | } |
| 371 | SkDebugf("Rendering\n"); |
| 372 | SkBitmap bitmap; |
| 373 | if (!FLAGS_dump.isEmpty()) { |
| 374 | SkIRect size = pic->cullRect().roundOut(); |
| 375 | bitmap.allocN32Pixels(size.width(), size.height()); |
| 376 | } |
| 377 | SkCanvas canvas(bitmap); |
| 378 | canvas.drawPicture(pic); |
kjlubick | 47d158e | 2016-02-01 08:23:50 -0800 | [diff] [blame] | 379 | SkDebugf("[terminated] Success! Decoded and rendered an SkPicture!\n"); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 380 | dump_png(bitmap); |
| 381 | return 0; |
| 382 | } |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 383 | |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 384 | int fuzz_icc(sk_sp<SkData> bytes) { |
Brian Osman | 526972e | 2016-10-24 09:24:02 -0400 | [diff] [blame] | 385 | sk_sp<SkColorSpace> space(SkColorSpace::MakeICC(bytes->data(), bytes->size())); |
kjlubick | 897a8e3 | 2016-06-09 07:15:12 -0700 | [diff] [blame] | 386 | if (!space) { |
| 387 | SkDebugf("[terminated] Couldn't decode ICC.\n"); |
| 388 | return 1; |
| 389 | } |
| 390 | SkDebugf("[terminated] Success! Decoded ICC.\n"); |
| 391 | return 0; |
| 392 | } |
| 393 | |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 394 | int fuzz_color_deserialize(sk_sp<SkData> bytes) { |
kjlubick | 3e3c1a5 | 2016-06-23 10:49:27 -0700 | [diff] [blame] | 395 | sk_sp<SkColorSpace> space(SkColorSpace::Deserialize(bytes->data(), bytes->size())); |
| 396 | if (!space) { |
| 397 | SkDebugf("[terminated] Couldn't deserialize Colorspace.\n"); |
| 398 | return 1; |
| 399 | } |
| 400 | SkDebugf("[terminated] Success! deserialized Colorspace.\n"); |
| 401 | return 0; |
| 402 | } |
| 403 | |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 404 | #if SK_SUPPORT_GPU |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 405 | int fuzz_sksl2glsl(sk_sp<SkData> bytes) { |
| 406 | SkSL::Compiler compiler; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 407 | SkString output; |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 408 | bool result = compiler.toGLSL(SkSL::Program::kFragment_Kind, |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 409 | SkString((const char*)bytes->data()), *SkSL::GLSLCapsFactory::Default(), &output); |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 410 | |
| 411 | if (!result) { |
| 412 | SkDebugf("[terminated] Couldn't compile input.\n"); |
| 413 | return 1; |
| 414 | } |
| 415 | SkDebugf("[terminated] Success! Compiled input.\n"); |
| 416 | return 0; |
| 417 | } |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 418 | #endif |
kjlubick | e719577 | 2016-10-18 10:06:24 -0700 | [diff] [blame] | 419 | |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 420 | Fuzz::Fuzz(sk_sp<SkData> bytes) : fBytes(bytes), fNextByte(0) {} |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 421 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 422 | void Fuzz::signalBug() { SkDebugf("Signal bug\n"); raise(SIGSEGV); } |
mtklein | a115942 | 2016-01-15 05:46:54 -0800 | [diff] [blame] | 423 | |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 424 | size_t Fuzz::size() { return fBytes->size(); } |
| 425 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 426 | bool Fuzz::exhausted() { |
| 427 | return fBytes->size() == fNextByte; |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 428 | } |