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" |
| 13 | #include "SkForceLinking.h" |
| 14 | #include "SkImage.h" |
| 15 | #include "SkImageEncoder.h" |
| 16 | #include "SkMallocPixelRef.h" |
| 17 | #include "SkPicture.h" |
| 18 | #include "SkStream.h" |
| 19 | |
mtklein | a115942 | 2016-01-15 05:46:54 -0800 | [diff] [blame] | 20 | #include <signal.h> |
mtklein | f5e9782 | 2016-01-15 06:19:53 -0800 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 23 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 24 | |
| 25 | 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^] | 26 | 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] | 27 | |
| 28 | DEFINE_string2(type, t, "api", "How to interpret --bytes, either 'image', 'skp', or 'api'."); |
| 29 | DEFINE_string2(dump, d, "", "If not empty, dump 'image' or 'skp' types as a PNG with this name."); |
| 30 | |
| 31 | static int printUsage(const char* name) { |
mtklein | d4387ea | 2016-01-21 06:13:52 -0800 | [diff] [blame^] | 32 | 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] | 33 | return 1; |
| 34 | } |
| 35 | |
| 36 | static int fuzz_api(SkData*); |
| 37 | static int fuzz_img(SkData*); |
| 38 | static int fuzz_skp(SkData*); |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 39 | |
| 40 | int main(int argc, char** argv) { |
mtklein | f5e9782 | 2016-01-15 06:19:53 -0800 | [diff] [blame] | 41 | SkCommandLineFlags::Parse(argc, argv); |
| 42 | |
mtklein | d0b8234 | 2016-01-15 07:56:20 -0800 | [diff] [blame] | 43 | const char* path = FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0]; |
| 44 | SkAutoTUnref<SkData> bytes(SkData::NewFromFileName(path)); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 45 | if (!bytes) { |
| 46 | SkDebugf("Could not read %s\n", path); |
| 47 | return 2; |
| 48 | } |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 49 | |
mtklein | d4387ea | 2016-01-21 06:13:52 -0800 | [diff] [blame^] | 50 | if (!FLAGS_type.isEmpty()) { |
| 51 | switch (FLAGS_type[0][0]) { |
| 52 | case 'a': return fuzz_api(bytes); |
| 53 | case 'i': return fuzz_img(bytes); |
| 54 | case 's': return fuzz_skp(bytes); |
| 55 | } |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 56 | } |
| 57 | return printUsage(argv[0]); |
| 58 | } |
| 59 | |
| 60 | int fuzz_api(SkData* bytes) { |
mtklein | d4387ea | 2016-01-21 06:13:52 -0800 | [diff] [blame^] | 61 | const char* name = FLAGS_name.isEmpty() ? "" : FLAGS_name[0]; |
| 62 | |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 63 | for (auto r = SkTRegistry<Fuzzable>::Head(); r; r = r->next()) { |
| 64 | auto fuzzable = r->factory(); |
mtklein | d4387ea | 2016-01-21 06:13:52 -0800 | [diff] [blame^] | 65 | if (0 == strcmp(name, fuzzable.name)) { |
mtklein | f5e9782 | 2016-01-15 06:19:53 -0800 | [diff] [blame] | 66 | SkDebugf("Fuzzing %s...\n", fuzzable.name); |
| 67 | Fuzz fuzz(bytes); |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 68 | fuzzable.fn(&fuzz); |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 69 | return 0; |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 70 | } |
| 71 | } |
mtklein | d4387ea | 2016-01-21 06:13:52 -0800 | [diff] [blame^] | 72 | |
| 73 | SkDebugf("When using --type api, please choose an API to fuzz with --name/-n:\n"); |
| 74 | for (auto r = SkTRegistry<Fuzzable>::Head(); r; r = r->next()) { |
| 75 | auto fuzzable = r->factory(); |
| 76 | SkDebugf("\t%s\n", fuzzable.name); |
| 77 | } |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | static void dump_png(SkBitmap bitmap) { |
| 82 | if (!FLAGS_dump.isEmpty()) { |
| 83 | SkImageEncoder::EncodeFile(FLAGS_dump[0], bitmap, SkImageEncoder::kPNG_Type, 100); |
| 84 | SkDebugf("Dumped to %s\n", FLAGS_dump[0]); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | int fuzz_img(SkData* bytes) { |
| 89 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(bytes)); |
| 90 | if (nullptr == codec.get()) { |
| 91 | SkDebugf("Couldn't create codec."); |
| 92 | return 3; |
| 93 | } |
| 94 | |
| 95 | SkImageInfo decodeInfo = codec->getInfo(); |
| 96 | // Construct a color table for the decode if necessary |
| 97 | SkAutoTUnref<SkColorTable> colorTable(nullptr); |
| 98 | SkPMColor* colorPtr = nullptr; |
| 99 | int* colorCountPtr = nullptr; |
| 100 | int maxColors = 256; |
| 101 | if (kIndex_8_SkColorType == decodeInfo.colorType()) { |
| 102 | SkPMColor colors[256]; |
| 103 | colorTable.reset(new SkColorTable(colors, maxColors)); |
| 104 | colorPtr = const_cast<SkPMColor*>(colorTable->readColors()); |
| 105 | colorCountPtr = &maxColors; |
| 106 | } |
| 107 | |
| 108 | SkBitmap bitmap; |
| 109 | SkMallocPixelRef::ZeroedPRFactory zeroFactory; |
| 110 | SkCodec::Options options; |
| 111 | options.fZeroInitialized = SkCodec::kYes_ZeroInitialized; |
| 112 | |
| 113 | if (!bitmap.tryAllocPixels(decodeInfo, &zeroFactory, nullptr)) { |
| 114 | SkDebugf("Could not allocate memory. Image might be too large (%d x %d)", |
| 115 | decodeInfo.width(), decodeInfo.height()); |
| 116 | return 4; |
| 117 | } |
| 118 | |
| 119 | switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options, |
| 120 | colorPtr, colorCountPtr)) { |
| 121 | case SkCodec::kSuccess: |
| 122 | SkDebugf("Success!\n"); |
| 123 | break; |
| 124 | case SkCodec::kIncompleteInput: |
| 125 | SkDebugf("Partial Success\n"); |
| 126 | break; |
| 127 | case SkCodec::kInvalidConversion: |
| 128 | SkDebugf("Incompatible colortype conversion"); |
| 129 | return 5; |
| 130 | default: |
| 131 | // Everything else is considered a failure. |
| 132 | SkDebugf("Couldn't getPixels."); |
| 133 | return 6; |
| 134 | } |
| 135 | |
| 136 | dump_png(bitmap); |
mtklein | f5e9782 | 2016-01-15 06:19:53 -0800 | [diff] [blame] | 137 | return 0; |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 138 | } |
| 139 | |
kjlubick | dba5734 | 2016-01-21 05:03:28 -0800 | [diff] [blame] | 140 | int fuzz_skp(SkData* bytes) { |
| 141 | SkMemoryStream stream(bytes); |
| 142 | SkDebugf("Decoding\n"); |
| 143 | SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(&stream)); |
| 144 | if (!pic) { |
| 145 | SkDebugf("Couldn't decode as a picture.\n"); |
| 146 | return 3; |
| 147 | } |
| 148 | SkDebugf("Rendering\n"); |
| 149 | SkBitmap bitmap; |
| 150 | if (!FLAGS_dump.isEmpty()) { |
| 151 | SkIRect size = pic->cullRect().roundOut(); |
| 152 | bitmap.allocN32Pixels(size.width(), size.height()); |
| 153 | } |
| 154 | SkCanvas canvas(bitmap); |
| 155 | canvas.drawPicture(pic); |
| 156 | SkDebugf("Decoded and rendered an SkPicture!\n"); |
| 157 | dump_png(bitmap); |
| 158 | return 0; |
| 159 | } |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 160 | |
mtklein | 24a22c7 | 2016-01-14 04:59:42 -0800 | [diff] [blame] | 161 | Fuzz::Fuzz(SkData* bytes) : fBytes(SkSafeRef(bytes)), fNextByte(0) {} |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 162 | |
mtklein | a115942 | 2016-01-15 05:46:54 -0800 | [diff] [blame] | 163 | void Fuzz::signalBug () { raise(SIGSEGV); } |
| 164 | void Fuzz::signalBoring() { exit(0); } |
| 165 | |
mtklein | 24a22c7 | 2016-01-14 04:59:42 -0800 | [diff] [blame] | 166 | template <typename T> |
mtklein | a115942 | 2016-01-15 05:46:54 -0800 | [diff] [blame] | 167 | T Fuzz::nextT() { |
| 168 | if (fNextByte + sizeof(T) > fBytes->size()) { |
| 169 | this->signalBoring(); |
mtklein | 24a22c7 | 2016-01-14 04:59:42 -0800 | [diff] [blame] | 170 | } |
mtklein | a115942 | 2016-01-15 05:46:54 -0800 | [diff] [blame] | 171 | |
mtklein | 24a22c7 | 2016-01-14 04:59:42 -0800 | [diff] [blame] | 172 | T val; |
mtklein | a115942 | 2016-01-15 05:46:54 -0800 | [diff] [blame] | 173 | memcpy(&val, fBytes->bytes() + fNextByte, sizeof(T)); |
| 174 | fNextByte += sizeof(T); |
mtklein | 24a22c7 | 2016-01-14 04:59:42 -0800 | [diff] [blame] | 175 | return val; |
| 176 | } |
| 177 | |
mtklein | a115942 | 2016-01-15 05:46:54 -0800 | [diff] [blame] | 178 | uint8_t Fuzz::nextB() { return this->nextT<uint8_t >(); } |
| 179 | uint32_t Fuzz::nextU() { return this->nextT<uint32_t>(); } |
| 180 | float Fuzz::nextF() { return this->nextT<float >(); } |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 181 | |