blob: 377e53f6565f7da3ac32ab0b789c5dbe64130ec1 [file] [log] [blame]
mtklein65e58242016-01-13 12:57:57 -08001/*
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"
kjlubickdba57342016-01-21 05:03:28 -08009#include "SkCanvas.h"
10#include "SkCodec.h"
mtkleinf5e97822016-01-15 06:19:53 -080011#include "SkCommandLineFlags.h"
kjlubickdba57342016-01-21 05:03:28 -080012#include "SkData.h"
kjlubickdba57342016-01-21 05:03:28 -080013#include "SkImage.h"
14#include "SkImageEncoder.h"
15#include "SkMallocPixelRef.h"
16#include "SkPicture.h"
kjlubicke5654502016-07-19 16:50:03 -070017#include "SkPicture.h"
18#include "SkPicture.h"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050019#if SK_SUPPORT_GPU
kjlubicke7195772016-10-18 10:06:24 -070020#include "SkSLCompiler.h"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050021#endif
kjlubickdba57342016-01-21 05:03:28 -080022#include "SkStream.h"
23
mtkleina1159422016-01-15 05:46:54 -080024#include <signal.h>
mtkleinf5e97822016-01-15 06:19:53 -080025
kjlubickdba57342016-01-21 05:03:28 -080026DEFINE_string2(bytes, b, "", "A path to a file. This can be the fuzz bytes or a binary to parse.");
mtkleind4387ea2016-01-21 06:13:52 -080027DEFINE_string2(name, n, "", "If --type is 'api', fuzz the API with this name.");
kjlubickdba57342016-01-21 05:03:28 -080028
kjlubick897a8e32016-06-09 07:15:12 -070029DEFINE_string2(type, t, "api", "How to interpret --bytes, either 'image_scale', 'image_mode', 'skp', 'icc', or 'api'.");
kjlubick2a42f482016-02-16 16:14:23 -080030DEFINE_string2(dump, d, "", "If not empty, dump 'image*' or 'skp' types as a PNG with this name.");
kjlubickdba57342016-01-21 05:03:28 -080031
32static int printUsage(const char* name) {
mtkleind4387ea2016-01-21 06:13:52 -080033 SkDebugf("Usage: %s -t <type> -b <path/to/file> [-n api-to-fuzz]\n", name);
kjlubickdba57342016-01-21 05:03:28 -080034 return 1;
35}
kjlubick2a42f482016-02-16 16:14:23 -080036static uint8_t calculate_option(SkData*);
kjlubickdba57342016-01-21 05:03:28 -080037
reed42943c82016-09-12 12:01:44 -070038static int fuzz_api(sk_sp<SkData>);
39static int fuzz_img(sk_sp<SkData>, uint8_t, uint8_t);
40static int fuzz_skp(sk_sp<SkData>);
41static int fuzz_icc(sk_sp<SkData>);
42static int fuzz_color_deserialize(sk_sp<SkData>);
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050043#if SK_SUPPORT_GPU
kjlubicke7195772016-10-18 10:06:24 -070044static int fuzz_sksl2glsl(sk_sp<SkData>);
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050045#endif
mtklein65e58242016-01-13 12:57:57 -080046
47int main(int argc, char** argv) {
mtkleinf5e97822016-01-15 06:19:53 -080048 SkCommandLineFlags::Parse(argc, argv);
49
mtkleind0b82342016-01-15 07:56:20 -080050 const char* path = FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0];
bungeman38d909e2016-08-02 14:40:46 -070051 sk_sp<SkData> bytes(SkData::MakeFromFileName(path));
kjlubickdba57342016-01-21 05:03:28 -080052 if (!bytes) {
53 SkDebugf("Could not read %s\n", path);
54 return 2;
55 }
mtklein65e58242016-01-13 12:57:57 -080056
bungeman38d909e2016-08-02 14:40:46 -070057 uint8_t option = calculate_option(bytes.get());
kjlubick2a42f482016-02-16 16:14:23 -080058
mtkleind4387ea2016-01-21 06:13:52 -080059 if (!FLAGS_type.isEmpty()) {
kjlubicke7195772016-10-18 10:06:24 -070060 if (0 == strcmp("api", FLAGS_type[0])) {
61 return fuzz_api(bytes);
62 }
63 if (0 == strcmp("color_deserialize", FLAGS_type[0])) {
64 return fuzz_color_deserialize(bytes);
65 }
66 if (0 == strcmp("icc", FLAGS_type[0])) {
67 return fuzz_icc(bytes);
68 }
69 if (0 == strcmp("image_scale", FLAGS_type[0])) {
70 return fuzz_img(bytes, option, 0);
71 }
72 if (0 == strcmp("image_mode", FLAGS_type[0])) {
73 return fuzz_img(bytes, 0, option);
74 }
75 if (0 == strcmp("skp", FLAGS_type[0])) {
76 return fuzz_skp(bytes);
77 }
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050078#if SK_SUPPORT_GPU
kjlubicke7195772016-10-18 10:06:24 -070079 if (0 == strcmp("sksl2glsl", FLAGS_type[0])) {
80 return fuzz_sksl2glsl(bytes);
mtkleind4387ea2016-01-21 06:13:52 -080081 }
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050082#endif
kjlubickdba57342016-01-21 05:03:28 -080083 }
84 return printUsage(argv[0]);
85}
86
kjlubick2a42f482016-02-16 16:14:23 -080087// This adds up the first 1024 bytes and returns it as an 8 bit integer. This allows afl-fuzz to
88// deterministically excercise different paths, or *options* (such as different scaling sizes or
89// different image modes) without needing to introduce a parameter. This way we don't need a
90// image_scale1, image_scale2, image_scale4, etc fuzzer, we can just have a image_scale fuzzer.
91// Clients are expected to transform this number into a different range, e.g. with modulo (%).
92static uint8_t calculate_option(SkData* bytes) {
93 uint8_t total = 0;
94 const uint8_t* data = bytes->bytes();
95 for (size_t i = 0; i < 1024 && i < bytes->size(); i++) {
96 total += data[i];
97 }
98 return total;
99}
100
reed42943c82016-09-12 12:01:44 -0700101int fuzz_api(sk_sp<SkData> bytes) {
mtkleind4387ea2016-01-21 06:13:52 -0800102 const char* name = FLAGS_name.isEmpty() ? "" : FLAGS_name[0];
103
mtklein65e58242016-01-13 12:57:57 -0800104 for (auto r = SkTRegistry<Fuzzable>::Head(); r; r = r->next()) {
105 auto fuzzable = r->factory();
mtkleind4387ea2016-01-21 06:13:52 -0800106 if (0 == strcmp(name, fuzzable.name)) {
mtkleinf5e97822016-01-15 06:19:53 -0800107 SkDebugf("Fuzzing %s...\n", fuzzable.name);
108 Fuzz fuzz(bytes);
mtklein65e58242016-01-13 12:57:57 -0800109 fuzzable.fn(&fuzz);
kjlubick47d158e2016-02-01 08:23:50 -0800110 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800111 return 0;
mtklein65e58242016-01-13 12:57:57 -0800112 }
113 }
mtkleind4387ea2016-01-21 06:13:52 -0800114
115 SkDebugf("When using --type api, please choose an API to fuzz with --name/-n:\n");
116 for (auto r = SkTRegistry<Fuzzable>::Head(); r; r = r->next()) {
117 auto fuzzable = r->factory();
118 SkDebugf("\t%s\n", fuzzable.name);
119 }
kjlubickdba57342016-01-21 05:03:28 -0800120 return 1;
121}
122
123static void dump_png(SkBitmap bitmap) {
124 if (!FLAGS_dump.isEmpty()) {
125 SkImageEncoder::EncodeFile(FLAGS_dump[0], bitmap, SkImageEncoder::kPNG_Type, 100);
126 SkDebugf("Dumped to %s\n", FLAGS_dump[0]);
127 }
128}
129
reed42943c82016-09-12 12:01:44 -0700130int fuzz_img(sk_sp<SkData> bytes, uint8_t scale, uint8_t mode) {
kjlubick2a42f482016-02-16 16:14:23 -0800131 // We can scale 1x, 2x, 4x, 8x, 16x
132 scale = scale % 5;
kjlubick5bd98a22016-02-18 06:27:38 -0800133 float fscale = (float)pow(2.0f, scale);
134 SkDebugf("Scaling factor: %f\n", fscale);
kjlubick2a42f482016-02-16 16:14:23 -0800135
136 // We have 4 different modes of decoding, just like DM.
137 mode = mode % 4;
138 SkDebugf("Mode: %d\n", mode);
139
140 // This is mostly copied from DMSrcSink's CodecSrc::draw method.
kjlubick47d158e2016-02-01 08:23:50 -0800141 SkDebugf("Decoding\n");
Ben Wagner145dbcd2016-11-03 14:40:50 -0400142 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(bytes));
kjlubickdba57342016-01-21 05:03:28 -0800143 if (nullptr == codec.get()) {
kjlubick47d158e2016-02-01 08:23:50 -0800144 SkDebugf("[terminated] Couldn't create codec.\n");
kjlubickdba57342016-01-21 05:03:28 -0800145 return 3;
146 }
147
148 SkImageInfo decodeInfo = codec->getInfo();
kjlubick2a42f482016-02-16 16:14:23 -0800149
150 SkISize size = codec->getScaledDimensions(fscale);
151 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
152
kjlubickdba57342016-01-21 05:03:28 -0800153 // Construct a color table for the decode if necessary
Hal Canary2db83612016-11-04 13:02:54 -0400154 sk_sp<SkColorTable> colorTable(nullptr);
kjlubickdba57342016-01-21 05:03:28 -0800155 SkPMColor* colorPtr = nullptr;
156 int* colorCountPtr = nullptr;
157 int maxColors = 256;
158 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
159 SkPMColor colors[256];
160 colorTable.reset(new SkColorTable(colors, maxColors));
161 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
162 colorCountPtr = &maxColors;
163 }
164
165 SkBitmap bitmap;
166 SkMallocPixelRef::ZeroedPRFactory zeroFactory;
167 SkCodec::Options options;
168 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
169
kjlubick2a42f482016-02-16 16:14:23 -0800170 if (!bitmap.tryAllocPixels(decodeInfo, &zeroFactory, colorTable.get())) {
kjlubick47d158e2016-02-01 08:23:50 -0800171 SkDebugf("[terminated] Could not allocate memory. Image might be too large (%d x %d)",
kjlubick2a42f482016-02-16 16:14:23 -0800172 decodeInfo.width(), decodeInfo.height());
kjlubickdba57342016-01-21 05:03:28 -0800173 return 4;
174 }
175
kjlubick2a42f482016-02-16 16:14:23 -0800176 switch (mode) {
177 case 0: {//kCodecZeroInit_Mode, kCodec_Mode
178 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options,
179 colorPtr, colorCountPtr)) {
180 case SkCodec::kSuccess:
181 SkDebugf("[terminated] Success!\n");
182 break;
183 case SkCodec::kIncompleteInput:
184 SkDebugf("[terminated] Partial Success\n");
185 break;
186 case SkCodec::kInvalidConversion:
187 SkDebugf("Incompatible colortype conversion\n");
188 // Crash to allow afl-fuzz to know this was a bug.
189 raise(SIGSEGV);
190 default:
191 SkDebugf("[terminated] Couldn't getPixels.\n");
192 return 6;
193 }
194 break;
195 }
196 case 1: {//kScanline_Mode
197 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
198 colorCountPtr)) {
199 SkDebugf("[terminated] Could not start scanline decoder\n");
200 return 7;
201 }
202
203 void* dst = bitmap.getAddr(0, 0);
204 size_t rowBytes = bitmap.rowBytes();
205 uint32_t height = decodeInfo.height();
206 switch (codec->getScanlineOrder()) {
207 case SkCodec::kTopDown_SkScanlineOrder:
208 case SkCodec::kBottomUp_SkScanlineOrder:
kjlubick2a42f482016-02-16 16:14:23 -0800209 // We do not need to check the return value. On an incomplete
210 // image, memory will be filled with a default value.
211 codec->getScanlines(dst, height, rowBytes);
212 break;
kjlubick2a42f482016-02-16 16:14:23 -0800213 }
kjlubick47d158e2016-02-01 08:23:50 -0800214 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800215 break;
kjlubick2a42f482016-02-16 16:14:23 -0800216 }
217 case 2: { //kStripe_Mode
218 const int height = decodeInfo.height();
219 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
220 // does not align with image blocks.
221 const int stripeHeight = 37;
222 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
223
224 // Decode odd stripes
225 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
226 colorCountPtr)
227 || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
228 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
229 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
230 // to run this test for image types that do not have this scanline ordering.
231 SkDebugf("[terminated] Could not start top-down scanline decoder\n");
232 return 8;
233 }
234
235 for (int i = 0; i < numStripes; i += 2) {
236 // Skip a stripe
237 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
238 codec->skipScanlines(linesToSkip);
239
240 // Read a stripe
241 const int startY = (i + 1) * stripeHeight;
242 const int linesToRead = SkTMin(stripeHeight, height - startY);
243 if (linesToRead > 0) {
244 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
245 }
246 }
247
248 // Decode even stripes
249 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo, nullptr,
250 colorPtr, colorCountPtr);
251 if (SkCodec::kSuccess != startResult) {
252 SkDebugf("[terminated] Failed to restart scanline decoder with same parameters.\n");
253 return 9;
254 }
255 for (int i = 0; i < numStripes; i += 2) {
256 // Read a stripe
257 const int startY = i * stripeHeight;
258 const int linesToRead = SkTMin(stripeHeight, height - startY);
259 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
260
261 // Skip a stripe
262 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
263 if (linesToSkip > 0) {
264 codec->skipScanlines(linesToSkip);
265 }
266 }
267 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800268 break;
kjlubick2a42f482016-02-16 16:14:23 -0800269 }
270 case 3: { //kSubset_Mode
271 // Arbitrarily choose a divisor.
272 int divisor = 2;
273 // Total width/height of the image.
274 const int W = codec->getInfo().width();
275 const int H = codec->getInfo().height();
276 if (divisor > W || divisor > H) {
277 SkDebugf("[terminated] Cannot codec subset: divisor %d is too big "
278 "with dimensions (%d x %d)\n", divisor, W, H);
279 return 10;
280 }
281 // subset dimensions
282 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
283 const int w = SkAlign2(W / divisor);
284 const int h = SkAlign2(H / divisor);
285 SkIRect subset;
286 SkCodec::Options opts;
287 opts.fSubset = &subset;
288 SkBitmap subsetBm;
289 // We will reuse pixel memory from bitmap.
290 void* pixels = bitmap.getPixels();
291 // Keep track of left and top (for drawing subsetBm into canvas). We could use
292 // fscale * x and fscale * y, but we want integers such that the next subset will start
293 // where the last one ended. So we'll add decodeInfo.width() and height().
294 int left = 0;
295 for (int x = 0; x < W; x += w) {
296 int top = 0;
297 for (int y = 0; y < H; y+= h) {
298 // Do not make the subset go off the edge of the image.
299 const int preScaleW = SkTMin(w, W - x);
300 const int preScaleH = SkTMin(h, H - y);
301 subset.setXYWH(x, y, preScaleW, preScaleH);
302 // And fscale
303 // FIXME: Should we have a version of getScaledDimensions that takes a subset
304 // into account?
305 decodeInfo = decodeInfo.makeWH(
306 SkTMax(1, SkScalarRoundToInt(preScaleW * fscale)),
307 SkTMax(1, SkScalarRoundToInt(preScaleH * fscale)));
308 size_t rowBytes = decodeInfo.minRowBytes();
309 if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes, colorTable.get(),
310 nullptr, nullptr)) {
311 SkDebugf("[terminated] Could not install pixels.\n");
312 return 11;
313 }
314 const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes,
315 &opts, colorPtr, colorCountPtr);
316 switch (result) {
317 case SkCodec::kSuccess:
318 case SkCodec::kIncompleteInput:
319 SkDebugf("okay\n");
320 break;
321 case SkCodec::kInvalidConversion:
322 if (0 == (x|y)) {
323 // First subset is okay to return unimplemented.
324 SkDebugf("[terminated] Incompatible colortype conversion\n");
325 return 12;
326 }
327 // If the first subset succeeded, a later one should not fail.
328 // fall through to failure
329 case SkCodec::kUnimplemented:
330 if (0 == (x|y)) {
331 // First subset is okay to return unimplemented.
332 SkDebugf("[terminated] subset codec not supported\n");
333 return 13;
334 }
335 // If the first subset succeeded, why would a later one fail?
336 // fall through to failure
337 default:
338 SkDebugf("[terminated] subset codec failed to decode (%d, %d, %d, %d) "
339 "with dimensions (%d x %d)\t error %d\n",
340 x, y, decodeInfo.width(), decodeInfo.height(),
341 W, H, result);
342 return 14;
343 }
344 // translate by the scaled height.
345 top += decodeInfo.height();
346 }
347 // translate by the scaled width.
348 left += decodeInfo.width();
349 }
350 SkDebugf("[terminated] Success!\n");
351 break;
352 }
kjlubickdba57342016-01-21 05:03:28 -0800353 default:
kjlubick2a42f482016-02-16 16:14:23 -0800354 SkDebugf("[terminated] Mode not implemented yet\n");
kjlubickdba57342016-01-21 05:03:28 -0800355 }
356
357 dump_png(bitmap);
mtkleinf5e97822016-01-15 06:19:53 -0800358 return 0;
mtklein65e58242016-01-13 12:57:57 -0800359}
360
reed42943c82016-09-12 12:01:44 -0700361int fuzz_skp(sk_sp<SkData> bytes) {
kjlubickdba57342016-01-21 05:03:28 -0800362 SkMemoryStream stream(bytes);
363 SkDebugf("Decoding\n");
reedca2622b2016-03-18 07:25:55 -0700364 sk_sp<SkPicture> pic(SkPicture::MakeFromStream(&stream));
kjlubickdba57342016-01-21 05:03:28 -0800365 if (!pic) {
kjlubick47d158e2016-02-01 08:23:50 -0800366 SkDebugf("[terminated] Couldn't decode as a picture.\n");
kjlubickdba57342016-01-21 05:03:28 -0800367 return 3;
368 }
369 SkDebugf("Rendering\n");
370 SkBitmap bitmap;
371 if (!FLAGS_dump.isEmpty()) {
372 SkIRect size = pic->cullRect().roundOut();
373 bitmap.allocN32Pixels(size.width(), size.height());
374 }
375 SkCanvas canvas(bitmap);
376 canvas.drawPicture(pic);
kjlubick47d158e2016-02-01 08:23:50 -0800377 SkDebugf("[terminated] Success! Decoded and rendered an SkPicture!\n");
kjlubickdba57342016-01-21 05:03:28 -0800378 dump_png(bitmap);
379 return 0;
380}
mtklein65e58242016-01-13 12:57:57 -0800381
reed42943c82016-09-12 12:01:44 -0700382int fuzz_icc(sk_sp<SkData> bytes) {
Brian Osman526972e2016-10-24 09:24:02 -0400383 sk_sp<SkColorSpace> space(SkColorSpace::MakeICC(bytes->data(), bytes->size()));
kjlubick897a8e32016-06-09 07:15:12 -0700384 if (!space) {
385 SkDebugf("[terminated] Couldn't decode ICC.\n");
386 return 1;
387 }
388 SkDebugf("[terminated] Success! Decoded ICC.\n");
389 return 0;
390}
391
reed42943c82016-09-12 12:01:44 -0700392int fuzz_color_deserialize(sk_sp<SkData> bytes) {
kjlubick3e3c1a52016-06-23 10:49:27 -0700393 sk_sp<SkColorSpace> space(SkColorSpace::Deserialize(bytes->data(), bytes->size()));
394 if (!space) {
395 SkDebugf("[terminated] Couldn't deserialize Colorspace.\n");
396 return 1;
397 }
398 SkDebugf("[terminated] Success! deserialized Colorspace.\n");
399 return 0;
400}
401
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500402#if SK_SUPPORT_GPU
kjlubicke7195772016-10-18 10:06:24 -0700403int fuzz_sksl2glsl(sk_sp<SkData> bytes) {
404 SkSL::Compiler compiler;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500405 SkString output;
kjlubicke7195772016-10-18 10:06:24 -0700406 bool result = compiler.toGLSL(SkSL::Program::kFragment_Kind,
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500407 SkString((const char*)bytes->data()), *SkSL::GLSLCapsFactory::Default(), &output);
kjlubicke7195772016-10-18 10:06:24 -0700408
409 if (!result) {
410 SkDebugf("[terminated] Couldn't compile input.\n");
411 return 1;
412 }
413 SkDebugf("[terminated] Success! Compiled input.\n");
414 return 0;
415}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500416#endif
kjlubicke7195772016-10-18 10:06:24 -0700417
reed42943c82016-09-12 12:01:44 -0700418Fuzz::Fuzz(sk_sp<SkData> bytes) : fBytes(bytes), fNextByte(0) {}
mtklein65e58242016-01-13 12:57:57 -0800419
Kevin Lubick2f535ce2016-11-01 15:01:12 -0400420void Fuzz::signalBug() { SkDebugf("Signal bug\n"); raise(SIGSEGV); }
mtkleina1159422016-01-15 05:46:54 -0800421
kjlubicke5654502016-07-19 16:50:03 -0700422size_t Fuzz::size() { return fBytes->size(); }
423
Kevin Lubick2f535ce2016-11-01 15:01:12 -0400424bool Fuzz::exhausted() {
425 return fBytes->size() == fNextByte;
kjlubick5bd98a22016-02-18 06:27:38 -0800426}