Chromium Code Reviews

Side by Side Diff: fuzz/fuzz.cpp

Issue 1591073002: Add ability to fuzz images and skps to fuzz binary (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: Fix imports Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "SkImage.h"
14 #include "SkImageEncoder.h"
15 #include "SkMallocPixelRef.h"
16 #include "SkPicture.h"
17 #include "SkStream.h"
18
10 #include <signal.h> 19 #include <signal.h>
11 #include <stdlib.h> 20 #include <stdlib.h>
21 #include <cmath>
12 22
13 DEFINE_string2(bytes, b, "", "A path to a file containing fuzzed bytes."); 23 DEFINE_string2(bytes, b, "", "A path to a file.");
mtklein 2016/01/20 14:49:06 ? Do they not contain fuzzed bytes any more?
kjlubick 2016/01/20 19:21:58 Done.
14 DEFINE_string2(match, m, "", "The usual --match, applied to DEF_FUZZ names."); 24 DEFINE_string2(match, m, "", "The usual --match, applied to DEF_FUZZ names.");
15 25
26 DEFINE_string(mode, "api", "How to interpret --bytes, either 'image', 'skp', or 'api'.");
mtklein 2016/01/20 14:49:06 We may want to make this type, not mode, so that w
kjlubick 2016/01/20 19:21:58 Done.
27 DEFINE_string(dump, "", "If not empty, dump 'image' or 'skp' modes as a PNG with this name.");
28
29 void printUsage(char*);
mtklein 2016/01/20 14:49:06 It is a good habit to get into make functions (rea
mtklein 2016/01/20 14:49:06 It's another good habit to take your arguments wit
kjlubick 2016/01/20 19:21:58 Done.
30 int runSingleTest(SkData*);
mtklein 2016/01/20 14:49:06 Let's make these more symmetric: static int fuzz_
kjlubick 2016/01/20 19:21:58 Done.
31 int decodeImage(SkData*);
32 int decodeSkp();
33
16 int main(int argc, char** argv) { 34 int main(int argc, char** argv) {
17 SkCommandLineFlags::Parse(argc, argv); 35 SkCommandLineFlags::Parse(argc, argv);
18 36
19 if (FLAGS_bytes.isEmpty()) { 37 if (FLAGS_bytes.isEmpty() || FLAGS_mode.isEmpty()) {
mtklein 2016/01/20 14:49:06 I think you need to rebase your patch. Are you in
kjlubick 2016/01/20 19:21:58 Whoops, just needed a rebase.
20 SkDebugf("Usage: %s -b <path/to/fuzzed.data> [-m pattern]\n", argv[0]); 38 printUsage(argv[0]);
mtklein 2016/01/20 14:49:06 You might consider writing this as: static int us
scroggo 2016/01/20 18:46:19 SkCommandLineFlags has a "usage" string, but it on
kjlubick 2016/01/20 19:21:57 That would be nice.
kjlubick 2016/01/20 19:21:58 Done.
21 return 1; 39 return 1;
22 } 40 }
41
42 if (0 == strcmp(FLAGS_mode[0], "skp")) {
43 return decodeSkp();
44 }
45
23 SkAutoTUnref<SkData> bytes(SkData::NewFromFileName(FLAGS_bytes[0])); 46 SkAutoTUnref<SkData> bytes(SkData::NewFromFileName(FLAGS_bytes[0]));
47 if (!bytes) {
48 SkDebugf("Could not read %s\n", FLAGS_bytes[0]);
49 return 2;
50 }
51 if (0 == strcmp(FLAGS_mode[0], "api")) {
mtklein 2016/01/20 14:49:06 This is fine, but we probably don't need to be thi
kjlubick 2016/01/20 19:21:58 Done, although I'm not sure what the c is for.
52 return runSingleTest(bytes);
53 } else if (0 == strcmp(FLAGS_mode[0], "image")) {
54 return decodeImage(bytes);
55 }
56 printUsage(argv[0]);
57 return 1;
58 }
24 59
60 void printUsage(char* name) {
61 SkDebugf("Usage: %s --mode <mode> -b <path/to/file> [-m pattern]\n", name);
mtklein 2016/01/20 14:49:07 It seems like this one would be more useful if def
kjlubick 2016/01/20 19:21:58 Done.
62 }
63
64 int runSingleTest(SkData* bytes) {
25 for (auto r = SkTRegistry<Fuzzable>::Head(); r; r = r->next()) { 65 for (auto r = SkTRegistry<Fuzzable>::Head(); r; r = r->next()) {
26 auto fuzzable = r->factory(); 66 auto fuzzable = r->factory();
27 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, fuzzable.name)) { 67 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, fuzzable.name)) {
28 SkDebugf("Fuzzing %s...\n", fuzzable.name); 68 SkDebugf("Fuzzing %s...\n", fuzzable.name);
29 Fuzz fuzz(bytes); 69 Fuzz fuzz(bytes);
30 fuzzable.fn(&fuzz); 70 fuzzable.fn(&fuzz);
71 return 0;
31 } 72 }
32 } 73 }
33 return 0; 74 return 0;
mtklein 2016/01/20 14:49:06 1?
kjlubick 2016/01/20 19:21:58 Done.
34 } 75 }
35 76
77 int decodeImage(SkData* bytes) {
78 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(bytes));
mtklein 2016/01/20 14:49:06 This can't really be the easiest way to use SkCode
scroggo 2016/01/20 18:46:19 This looks about right. It's complicated in part b
kjlubick 2016/01/20 19:21:58 I went over it with scroggo@ yesterday, and this i
79 if (nullptr == codec.get()) {
80 SkDebugf("Couldn't create codec.");
81 return 3;
82 }
83
84 SkImageInfo decodeInfo = codec->getInfo();
85 // Construct a color table for the decode if necessary
86 SkAutoTUnref<SkColorTable> colorTable(nullptr);
87 SkPMColor* colorPtr = nullptr;
88 int* colorCountPtr = nullptr;
89 int maxColors = 256;
90 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
91 SkPMColor colors[256];
92 colorTable.reset(new SkColorTable(colors, maxColors));
93 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
94 colorCountPtr = &maxColors;
95 }
96
97 SkBitmap bitmap;
98 SkMallocPixelRef::ZeroedPRFactory zeroFactory;
99 SkCodec::Options options;
100 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
101
102 if (!bitmap.tryAllocPixels(decodeInfo, &zeroFactory, nullptr)) {
103 SkDebugf("Could not allocate memory. Image might be too large (%d x %d) ",
104 decodeInfo.width(), decodeInfo.height());
105 return 4;
106 }
107
108 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options,
109 colorPtr, colorCountPtr)) {
110 case SkCodec::kSuccess:
111 SkDebugf("Success!\n");
112 break;
113 case SkCodec::kIncompleteInput:
114 SkDebugf("Partial Success\n");
115 break;
116 case SkCodec::kInvalidConversion:
117 SkDebugf("Incompatible colortype conversion");
118 return 5;
119 default:
120 // Everything else is considered a failure.
121 SkDebugf("Couldn't getPixels.");
122 return 6;
123 }
124
125
126 if (!FLAGS_dump.isEmpty()) {
127 SkImageEncoder::EncodeFile(FLAGS_dump[0], bitmap, SkImageEncoder::kPNG_T ype, 100);
128 SkDebugf("Dumped to %s\n", FLAGS_dump[0]);
129 }
130 return 0;
131 }
132
133 static const SkRect kSKPViewport = {0,0, 1000,1000};
134
135 int decodeSkp() {
136 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(FLAGS_bytes[0]));
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.clipRect(kSKPViewport);
mtklein 2016/01/20 14:49:06 Let's not do this? If we're allocating space for
kjlubick 2016/01/20 19:21:58 Done.
155 canvas.drawPicture(pic);
156 SkDebugf("Decoded and rendered an SkPicture!\n");
157 if (!FLAGS_dump.isEmpty()) {
158 SkImageEncoder::EncodeFile(FLAGS_dump[0], bitmap, SkImageEncoder::kPNG_T ype, 100);
mtklein 2016/01/20 14:49:06 Seems like we've got a bunch of common logic for .
kjlubick 2016/01/20 19:21:57 Deduped.
159 SkDebugf("Dumped to %s\n", FLAGS_dump[0]);
160 }
161 return 0;
162 }
36 163
37 Fuzz::Fuzz(SkData* bytes) : fBytes(SkSafeRef(bytes)), fNextByte(0) {} 164 Fuzz::Fuzz(SkData* bytes) : fBytes(SkSafeRef(bytes)), fNextByte(0) {}
38 165
39 void Fuzz::signalBug () { raise(SIGSEGV); } 166 void Fuzz::signalBug () { raise(SIGSEGV); }
40 void Fuzz::signalBoring() { exit(0); } 167 void Fuzz::signalBoring() { exit(0); }
41 168
42 template <typename T> 169 template <typename T>
43 T Fuzz::nextT() { 170 T Fuzz::nextT() {
44 if (fNextByte + sizeof(T) > fBytes->size()) { 171 if (fNextByte + sizeof(T) > fBytes->size()) {
45 this->signalBoring(); 172 this->signalBoring();
46 } 173 }
47 174
48 T val; 175 T val;
49 memcpy(&val, fBytes->bytes() + fNextByte, sizeof(T)); 176 memcpy(&val, fBytes->bytes() + fNextByte, sizeof(T));
50 fNextByte += sizeof(T); 177 fNextByte += sizeof(T);
51 return val; 178 return val;
52 } 179 }
53 180
54 uint8_t Fuzz::nextB() { return this->nextT<uint8_t >(); } 181 uint8_t Fuzz::nextB() { return this->nextT<uint8_t >(); }
55 uint32_t Fuzz::nextU() { return this->nextT<uint32_t>(); } 182 uint32_t Fuzz::nextU() { return this->nextT<uint32_t>(); }
56 float Fuzz::nextF() { return this->nextT<float >(); } 183 float Fuzz::nextF() { return this->nextT<float >(); }
57 184
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine