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