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> | |
| 12 | 23 |
| 13 DEFINE_string2(bytes, b, "", "A path to a file containing fuzzed bytes."); | 24 __SK_FORCE_IMAGE_DECODER_LINKING; |
| 25 | |
| 26 DEFINE_string2(bytes, b, "", "A path to a file. This can be the fuzz bytes or a binary to parse."); | |
| 14 DEFINE_string2(match, m, "", "The usual --match, applied to DEF_FUZZ names."); | 27 DEFINE_string2(match, m, "", "The usual --match, applied to DEF_FUZZ names."); |
| 15 | 28 |
| 29 DEFINE_string2(type, t, "api", "How to interpret --bytes, either 'image', 'skp', or 'api'."); | |
| 30 DEFINE_string(dump, "", "If not empty, dump 'image' or 'skp' types as a PNG with this name."); | |
|
mtklein
2016/01/20 20:15:21
This can probably be string2(dump, d, ...)
kjlubick
2016/01/20 20:32:59
Done.
| |
| 31 | |
| 32 int printUsage(const char* name) { | |
|
mtklein
2016/01/20 20:15:21
static int print_usage(const char* name)
kjlubick
2016/01/20 20:32:59
Done.
| |
| 33 SkDebugf("Usage: %s -t <type> -b <path/to/file> [-m pattern]\n", name); | |
| 34 return 1; | |
| 35 } | |
| 36 | |
| 37 static int fuzz_api(SkData*); | |
| 38 static int fuzz_img(SkData*); | |
| 39 static int fuzz_skp(SkData*); | |
| 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_bytes.isEmpty() || FLAGS_type.isEmpty()) { | |
|
mtklein
2016/01/20 20:15:21
if (FLAGS_bytes.isEmpty() || FLAGS_type.isEmpty())
kjlubick
2016/01/20 20:32:59
https://goto.google.com/zvalt
| |
| 45 return printUsage(argv[0]); | |
| 46 } | |
| 47 | |
| 19 const char* path = FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0]; | 48 const char* path = FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0]; |
| 20 SkAutoTUnref<SkData> bytes(SkData::NewFromFileName(path)); | 49 SkAutoTUnref<SkData> bytes(SkData::NewFromFileName(path)); |
| 50 if (!bytes) { | |
| 51 SkDebugf("Could not read %s\n", path); | |
| 52 return 2; | |
| 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 } | |
| 21 | 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 (!SkCommandLineFlags::ShouldSkip(FLAGS_match, fuzzable.name)) { |
|
mtklein
2016/01/20 20:15:21
If we're going to run one, we'd better switch this
kjlubick
2016/01/20 20:32:59
How about just API Fuzz?
| |
| 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("Test %s not found", FLAGS_match[0]); | |
| 73 return 1; | |
| 74 } | |
| 75 | |
| 76 void dumpPng(SkBitmap bitmap) { | |
|
mtklein
2016/01/20 20:15:21
static void dump_png(...)
kjlubick
2016/01/20 20:32:59
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 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(bytes)); | |
|
mtklein
2016/01/20 20:15:21
SkMemoryStream stream(bytes);
if (!stream.isValid(
kjlubick
2016/01/20 20:32:59
There is no isValid on SkMemoryStream. I guess if
| |
| 137 if (!stream) { | |
| 138 SkDebugf("Couldn't read %s.", FLAGS_bytes[0]); | |
| 139 return 2; | |
| 140 } | |
| 141 SkDebugf("Decoding\n"); | |
| 142 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream)); | |
| 143 if (!pic) { | |
| 144 SkDebugf("Couldn't decode as a picture.\n"); | |
| 145 return 3; | |
| 146 } | |
| 147 SkDebugf("Rendering\n"); | |
| 148 SkBitmap bitmap; | |
| 149 if (!FLAGS_dump.isEmpty()) { | |
| 150 SkIRect size = pic->cullRect().roundOut(); | |
| 151 bitmap.allocN32Pixels(size.width(), size.height()); | |
| 152 } | |
| 153 SkCanvas canvas(bitmap); | |
| 154 canvas.drawPicture(pic); | |
| 155 SkDebugf("Decoded and rendered an SkPicture!\n"); | |
| 156 dumpPng(bitmap); | |
| 157 return 0; | |
| 158 } | |
| 33 | 159 |
| 34 Fuzz::Fuzz(SkData* bytes) : fBytes(SkSafeRef(bytes)), fNextByte(0) {} | 160 Fuzz::Fuzz(SkData* bytes) : fBytes(SkSafeRef(bytes)), fNextByte(0) {} |
| 35 | 161 |
| 36 void Fuzz::signalBug () { raise(SIGSEGV); } | 162 void Fuzz::signalBug () { raise(SIGSEGV); } |
| 37 void Fuzz::signalBoring() { exit(0); } | 163 void Fuzz::signalBoring() { exit(0); } |
| 38 | 164 |
| 39 template <typename T> | 165 template <typename T> |
| 40 T Fuzz::nextT() { | 166 T Fuzz::nextT() { |
| 41 if (fNextByte + sizeof(T) > fBytes->size()) { | 167 if (fNextByte + sizeof(T) > fBytes->size()) { |
| 42 this->signalBoring(); | 168 this->signalBoring(); |
| 43 } | 169 } |
| 44 | 170 |
| 45 T val; | 171 T val; |
| 46 memcpy(&val, fBytes->bytes() + fNextByte, sizeof(T)); | 172 memcpy(&val, fBytes->bytes() + fNextByte, sizeof(T)); |
| 47 fNextByte += sizeof(T); | 173 fNextByte += sizeof(T); |
| 48 return val; | 174 return val; |
| 49 } | 175 } |
| 50 | 176 |
| 51 uint8_t Fuzz::nextB() { return this->nextT<uint8_t >(); } | 177 uint8_t Fuzz::nextB() { return this->nextT<uint8_t >(); } |
| 52 uint32_t Fuzz::nextU() { return this->nextT<uint32_t>(); } | 178 uint32_t Fuzz::nextU() { return this->nextT<uint32_t>(); } |
| 53 float Fuzz::nextF() { return this->nextT<float >(); } | 179 float Fuzz::nextF() { return this->nextT<float >(); } |
| 54 | 180 |
| OLD | NEW |