Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "Fuzz.h" | 8 #include "Fuzz.h" |
| 9 #include "SkCanvas.h" | |
| 10 #include "SkCodec.h" | |
| 9 #include "SkCommandLineFlags.h" | 11 #include "SkCommandLineFlags.h" |
| 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 | |
| 10 #include <signal.h> | 20 #include <signal.h> |
| 11 #include <stdlib.h> | 21 #include <stdlib.h> |
| 22 #include <cmath> | |
|
mtklein
2016/01/20 20:39:29
What's this needed for?
kjlubick
2016/01/20 20:45:59
Deleted.
| |
| 12 | 23 |
| 13 DEFINE_string2(bytes, b, "", "A path to a file containing fuzzed bytes."); | 24 __SK_FORCE_IMAGE_DECODER_LINKING; |
|
mtklein
2016/01/20 20:39:29
I don't think this is needed for Codec. Try remov
kjlubick
2016/01/20 20:45:59
When dump is specified, if I don't include this li
| |
| 14 DEFINE_string2(match, m, "", "The usual --match, applied to DEF_FUZZ names."); | 25 |
| 26 DEFINE_string2(bytes, b, "", "A path to a file. This can be the fuzz bytes or a binary to parse."); | |
| 27 DEFINE_string2(name, n, "", "If --type is 'api', run the DEF_FUZZ with this name ."); | |
|
mtklein
2016/01/20 20:39:29
let's write API fuzz here too.
kjlubick
2016/01/20 20:46:00
Done.
| |
| 28 | |
| 29 DEFINE_string2(type, t, "api", "How to interpret --bytes, either 'image', 'skp', or 'api'."); | |
| 30 DEFINE_string2(dump, d, "", "If not empty, dump 'image' or 'skp' types as a PNG with this name."); | |
| 31 | |
| 32 static int printUsage(const char* name) { | |
| 33 SkDebugf("Usage: %s -t <type> -b <path/to/file> [-m pattern]\n", name); | |
|
mtklein
2016/01/20 20:39:29
update?
kjlubick
2016/01/20 20:46:00
Done.
| |
| 34 return 1; | |
| 35 } | |
| 36 | |
| 37 static int fuzz_api(SkData*); | |
| 38 static int fuzz_img(SkData*); | |
| 39 static int fuzz_skp(SkData*); | |
| 15 | 40 |
| 16 int main(int argc, char** argv) { | 41 int main(int argc, char** argv) { |
| 17 SkCommandLineFlags::Parse(argc, argv); | 42 SkCommandLineFlags::Parse(argc, argv); |
| 18 | 43 |
| 44 if (FLAGS_type.isEmpty()) { | |
|
mtklein
2016/01/20 20:39:29
No longer needed? If it's empty or doesn't match
kjlubick
2016/01/20 20:45:59
Good catch.
| |
| 45 return printUsage(argv[0]); | |
| 46 } | |
| 19 const char* path = FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0]; | 47 const char* path = FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0]; |
| 20 SkAutoTUnref<SkData> bytes(SkData::NewFromFileName(path)); | 48 SkAutoTUnref<SkData> bytes(SkData::NewFromFileName(path)); |
| 49 if (!bytes) { | |
| 50 SkDebugf("Could not read %s\n", path); | |
| 51 return 2; | |
| 52 } | |
| 21 | 53 |
| 54 switch (FLAGS_type[0][0]) { | |
| 55 case 'a': return fuzz_api(bytes); | |
| 56 case 'i': return fuzz_img(bytes); | |
| 57 case 's': return fuzz_skp(bytes); | |
| 58 } | |
| 59 return printUsage(argv[0]); | |
| 60 } | |
| 61 | |
| 62 int fuzz_api(SkData* bytes) { | |
| 22 for (auto r = SkTRegistry<Fuzzable>::Head(); r; r = r->next()) { | 63 for (auto r = SkTRegistry<Fuzzable>::Head(); r; r = r->next()) { |
| 23 auto fuzzable = r->factory(); | 64 auto fuzzable = r->factory(); |
| 24 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, fuzzable.name)) { | 65 if (0 == strcmp(FLAGS_name[0], fuzzable.name)) { |
| 25 SkDebugf("Fuzzing %s...\n", fuzzable.name); | 66 SkDebugf("Fuzzing %s...\n", fuzzable.name); |
| 26 Fuzz fuzz(bytes); | 67 Fuzz fuzz(bytes); |
| 27 fuzzable.fn(&fuzz); | 68 fuzzable.fn(&fuzz); |
| 69 return 0; | |
| 28 } | 70 } |
| 29 } | 71 } |
| 72 SkDebugf("API fuzz %s not found\n", FLAGS_name[0]); | |
| 73 return 1; | |
| 74 } | |
| 75 | |
| 76 static void dumpPng(SkBitmap bitmap) { | |
|
mtklein
2016/01/20 20:39:29
static void dump_png(SkBitmap bitmap)
kjlubick
2016/01/20 20:46:00
Done.
| |
| 77 if (!FLAGS_dump.isEmpty()) { | |
| 78 SkImageEncoder::EncodeFile(FLAGS_dump[0], bitmap, SkImageEncoder::kPNG_T ype, 100); | |
| 79 SkDebugf("Dumped to %s\n", FLAGS_dump[0]); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 int fuzz_img(SkData* bytes) { | |
| 84 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(bytes)); | |
| 85 if (nullptr == codec.get()) { | |
| 86 SkDebugf("Couldn't create codec."); | |
| 87 return 3; | |
| 88 } | |
| 89 | |
| 90 SkImageInfo decodeInfo = codec->getInfo(); | |
| 91 // Construct a color table for the decode if necessary | |
| 92 SkAutoTUnref<SkColorTable> colorTable(nullptr); | |
| 93 SkPMColor* colorPtr = nullptr; | |
| 94 int* colorCountPtr = nullptr; | |
| 95 int maxColors = 256; | |
| 96 if (kIndex_8_SkColorType == decodeInfo.colorType()) { | |
| 97 SkPMColor colors[256]; | |
| 98 colorTable.reset(new SkColorTable(colors, maxColors)); | |
| 99 colorPtr = const_cast<SkPMColor*>(colorTable->readColors()); | |
| 100 colorCountPtr = &maxColors; | |
| 101 } | |
| 102 | |
| 103 SkBitmap bitmap; | |
| 104 SkMallocPixelRef::ZeroedPRFactory zeroFactory; | |
| 105 SkCodec::Options options; | |
| 106 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized; | |
| 107 | |
| 108 if (!bitmap.tryAllocPixels(decodeInfo, &zeroFactory, nullptr)) { | |
| 109 SkDebugf("Could not allocate memory. Image might be too large (%d x %d) ", | |
| 110 decodeInfo.width(), decodeInfo.height()); | |
| 111 return 4; | |
| 112 } | |
| 113 | |
| 114 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options, | |
| 115 colorPtr, colorCountPtr)) { | |
| 116 case SkCodec::kSuccess: | |
| 117 SkDebugf("Success!\n"); | |
| 118 break; | |
| 119 case SkCodec::kIncompleteInput: | |
| 120 SkDebugf("Partial Success\n"); | |
| 121 break; | |
| 122 case SkCodec::kInvalidConversion: | |
| 123 SkDebugf("Incompatible colortype conversion"); | |
| 124 return 5; | |
| 125 default: | |
| 126 // Everything else is considered a failure. | |
| 127 SkDebugf("Couldn't getPixels."); | |
| 128 return 6; | |
| 129 } | |
| 130 | |
| 131 dumpPng(bitmap); | |
| 30 return 0; | 132 return 0; |
| 31 } | 133 } |
| 32 | 134 |
| 135 int fuzz_skp(SkData* bytes) { | |
| 136 SkMemoryStream stream(bytes); | |
| 137 SkDebugf("Decoding\n"); | |
| 138 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(&stream)); | |
| 139 if (!pic) { | |
| 140 SkDebugf("Couldn't decode as a picture.\n"); | |
| 141 return 3; | |
| 142 } | |
| 143 SkDebugf("Rendering\n"); | |
| 144 SkBitmap bitmap; | |
| 145 if (!FLAGS_dump.isEmpty()) { | |
| 146 SkIRect size = pic->cullRect().roundOut(); | |
| 147 bitmap.allocN32Pixels(size.width(), size.height()); | |
| 148 } | |
| 149 SkCanvas canvas(bitmap); | |
| 150 canvas.drawPicture(pic); | |
| 151 SkDebugf("Decoded and rendered an SkPicture!\n"); | |
| 152 dumpPng(bitmap); | |
| 153 return 0; | |
| 154 } | |
| 33 | 155 |
| 34 Fuzz::Fuzz(SkData* bytes) : fBytes(SkSafeRef(bytes)), fNextByte(0) {} | 156 Fuzz::Fuzz(SkData* bytes) : fBytes(SkSafeRef(bytes)), fNextByte(0) {} |
| 35 | 157 |
| 36 void Fuzz::signalBug () { raise(SIGSEGV); } | 158 void Fuzz::signalBug () { raise(SIGSEGV); } |
| 37 void Fuzz::signalBoring() { exit(0); } | 159 void Fuzz::signalBoring() { exit(0); } |
| 38 | 160 |
| 39 template <typename T> | 161 template <typename T> |
| 40 T Fuzz::nextT() { | 162 T Fuzz::nextT() { |
| 41 if (fNextByte + sizeof(T) > fBytes->size()) { | 163 if (fNextByte + sizeof(T) > fBytes->size()) { |
| 42 this->signalBoring(); | 164 this->signalBoring(); |
| 43 } | 165 } |
| 44 | 166 |
| 45 T val; | 167 T val; |
| 46 memcpy(&val, fBytes->bytes() + fNextByte, sizeof(T)); | 168 memcpy(&val, fBytes->bytes() + fNextByte, sizeof(T)); |
| 47 fNextByte += sizeof(T); | 169 fNextByte += sizeof(T); |
| 48 return val; | 170 return val; |
| 49 } | 171 } |
| 50 | 172 |
| 51 uint8_t Fuzz::nextB() { return this->nextT<uint8_t >(); } | 173 uint8_t Fuzz::nextB() { return this->nextT<uint8_t >(); } |
| 52 uint32_t Fuzz::nextU() { return this->nextT<uint32_t>(); } | 174 uint32_t Fuzz::nextU() { return this->nextT<uint32_t>(); } |
| 53 float Fuzz::nextF() { return this->nextT<float >(); } | 175 float Fuzz::nextF() { return this->nextT<float >(); } |
| 54 | 176 |
| OLD | NEW |