blob: b27eec988a6102f05c255e195107f06e0ac9d86f [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
Hal Canarydb683012016-11-23 08:55:18 -070026#include "sk_tool_utils.h"
27
kjlubickdba57342016-01-21 05:03:28 -080028DEFINE_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 -080029DEFINE_string2(name, n, "", "If --type is 'api', fuzz the API with this name.");
kjlubickdba57342016-01-21 05:03:28 -080030
kjlubick897a8e32016-06-09 07:15:12 -070031DEFINE_string2(type, t, "api", "How to interpret --bytes, either 'image_scale', 'image_mode', 'skp', 'icc', or 'api'.");
kjlubick2a42f482016-02-16 16:14:23 -080032DEFINE_string2(dump, d, "", "If not empty, dump 'image*' or 'skp' types as a PNG with this name.");
kjlubickdba57342016-01-21 05:03:28 -080033
34static int printUsage(const char* name) {
mtkleind4387ea2016-01-21 06:13:52 -080035 SkDebugf("Usage: %s -t <type> -b <path/to/file> [-n api-to-fuzz]\n", name);
kjlubickdba57342016-01-21 05:03:28 -080036 return 1;
37}
kjlubick2a42f482016-02-16 16:14:23 -080038static uint8_t calculate_option(SkData*);
kjlubickdba57342016-01-21 05:03:28 -080039
reed42943c82016-09-12 12:01:44 -070040static int fuzz_api(sk_sp<SkData>);
41static int fuzz_img(sk_sp<SkData>, uint8_t, uint8_t);
42static int fuzz_skp(sk_sp<SkData>);
43static int fuzz_icc(sk_sp<SkData>);
44static int fuzz_color_deserialize(sk_sp<SkData>);
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050045#if SK_SUPPORT_GPU
kjlubicke7195772016-10-18 10:06:24 -070046static int fuzz_sksl2glsl(sk_sp<SkData>);
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050047#endif
mtklein65e58242016-01-13 12:57:57 -080048
49int main(int argc, char** argv) {
mtkleinf5e97822016-01-15 06:19:53 -080050 SkCommandLineFlags::Parse(argc, argv);
51
mtkleind0b82342016-01-15 07:56:20 -080052 const char* path = FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0];
bungeman38d909e2016-08-02 14:40:46 -070053 sk_sp<SkData> bytes(SkData::MakeFromFileName(path));
kjlubickdba57342016-01-21 05:03:28 -080054 if (!bytes) {
55 SkDebugf("Could not read %s\n", path);
56 return 2;
57 }
mtklein65e58242016-01-13 12:57:57 -080058
bungeman38d909e2016-08-02 14:40:46 -070059 uint8_t option = calculate_option(bytes.get());
kjlubick2a42f482016-02-16 16:14:23 -080060
mtkleind4387ea2016-01-21 06:13:52 -080061 if (!FLAGS_type.isEmpty()) {
kjlubicke7195772016-10-18 10:06:24 -070062 if (0 == strcmp("api", FLAGS_type[0])) {
63 return fuzz_api(bytes);
64 }
65 if (0 == strcmp("color_deserialize", FLAGS_type[0])) {
66 return fuzz_color_deserialize(bytes);
67 }
68 if (0 == strcmp("icc", FLAGS_type[0])) {
69 return fuzz_icc(bytes);
70 }
71 if (0 == strcmp("image_scale", FLAGS_type[0])) {
72 return fuzz_img(bytes, option, 0);
73 }
74 if (0 == strcmp("image_mode", FLAGS_type[0])) {
75 return fuzz_img(bytes, 0, option);
76 }
77 if (0 == strcmp("skp", FLAGS_type[0])) {
78 return fuzz_skp(bytes);
79 }
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050080#if SK_SUPPORT_GPU
kjlubicke7195772016-10-18 10:06:24 -070081 if (0 == strcmp("sksl2glsl", FLAGS_type[0])) {
82 return fuzz_sksl2glsl(bytes);
mtkleind4387ea2016-01-21 06:13:52 -080083 }
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050084#endif
kjlubickdba57342016-01-21 05:03:28 -080085 }
86 return printUsage(argv[0]);
87}
88
kjlubick2a42f482016-02-16 16:14:23 -080089// This adds up the first 1024 bytes and returns it as an 8 bit integer. This allows afl-fuzz to
90// deterministically excercise different paths, or *options* (such as different scaling sizes or
91// different image modes) without needing to introduce a parameter. This way we don't need a
92// image_scale1, image_scale2, image_scale4, etc fuzzer, we can just have a image_scale fuzzer.
93// Clients are expected to transform this number into a different range, e.g. with modulo (%).
94static uint8_t calculate_option(SkData* bytes) {
95 uint8_t total = 0;
96 const uint8_t* data = bytes->bytes();
97 for (size_t i = 0; i < 1024 && i < bytes->size(); i++) {
98 total += data[i];
99 }
100 return total;
101}
102
reed42943c82016-09-12 12:01:44 -0700103int fuzz_api(sk_sp<SkData> bytes) {
mtkleind4387ea2016-01-21 06:13:52 -0800104 const char* name = FLAGS_name.isEmpty() ? "" : FLAGS_name[0];
105
mtklein65e58242016-01-13 12:57:57 -0800106 for (auto r = SkTRegistry<Fuzzable>::Head(); r; r = r->next()) {
107 auto fuzzable = r->factory();
mtkleind4387ea2016-01-21 06:13:52 -0800108 if (0 == strcmp(name, fuzzable.name)) {
mtkleinf5e97822016-01-15 06:19:53 -0800109 SkDebugf("Fuzzing %s...\n", fuzzable.name);
110 Fuzz fuzz(bytes);
mtklein65e58242016-01-13 12:57:57 -0800111 fuzzable.fn(&fuzz);
kjlubick47d158e2016-02-01 08:23:50 -0800112 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800113 return 0;
mtklein65e58242016-01-13 12:57:57 -0800114 }
115 }
mtkleind4387ea2016-01-21 06:13:52 -0800116
117 SkDebugf("When using --type api, please choose an API to fuzz with --name/-n:\n");
118 for (auto r = SkTRegistry<Fuzzable>::Head(); r; r = r->next()) {
119 auto fuzzable = r->factory();
120 SkDebugf("\t%s\n", fuzzable.name);
121 }
kjlubickdba57342016-01-21 05:03:28 -0800122 return 1;
123}
124
125static void dump_png(SkBitmap bitmap) {
126 if (!FLAGS_dump.isEmpty()) {
Hal Canarydb683012016-11-23 08:55:18 -0700127 sk_tool_utils::EncodeImageToFile(FLAGS_dump[0], bitmap, SkEncodedImageFormat::kPNG, 100);
kjlubickdba57342016-01-21 05:03:28 -0800128 SkDebugf("Dumped to %s\n", FLAGS_dump[0]);
129 }
130}
131
reed42943c82016-09-12 12:01:44 -0700132int fuzz_img(sk_sp<SkData> bytes, uint8_t scale, uint8_t mode) {
kjlubick2a42f482016-02-16 16:14:23 -0800133 // We can scale 1x, 2x, 4x, 8x, 16x
134 scale = scale % 5;
kjlubick5bd98a22016-02-18 06:27:38 -0800135 float fscale = (float)pow(2.0f, scale);
136 SkDebugf("Scaling factor: %f\n", fscale);
kjlubick2a42f482016-02-16 16:14:23 -0800137
138 // We have 4 different modes of decoding, just like DM.
139 mode = mode % 4;
140 SkDebugf("Mode: %d\n", mode);
141
142 // This is mostly copied from DMSrcSink's CodecSrc::draw method.
kjlubick47d158e2016-02-01 08:23:50 -0800143 SkDebugf("Decoding\n");
Ben Wagner145dbcd2016-11-03 14:40:50 -0400144 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(bytes));
kjlubickdba57342016-01-21 05:03:28 -0800145 if (nullptr == codec.get()) {
kjlubick47d158e2016-02-01 08:23:50 -0800146 SkDebugf("[terminated] Couldn't create codec.\n");
kjlubickdba57342016-01-21 05:03:28 -0800147 return 3;
148 }
149
150 SkImageInfo decodeInfo = codec->getInfo();
kjlubick2a42f482016-02-16 16:14:23 -0800151
152 SkISize size = codec->getScaledDimensions(fscale);
153 decodeInfo = decodeInfo.makeWH(size.width(), size.height());
154
kjlubickdba57342016-01-21 05:03:28 -0800155 // Construct a color table for the decode if necessary
Hal Canary2db83612016-11-04 13:02:54 -0400156 sk_sp<SkColorTable> colorTable(nullptr);
kjlubickdba57342016-01-21 05:03:28 -0800157 SkPMColor* colorPtr = nullptr;
158 int* colorCountPtr = nullptr;
159 int maxColors = 256;
160 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
161 SkPMColor colors[256];
162 colorTable.reset(new SkColorTable(colors, maxColors));
163 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
164 colorCountPtr = &maxColors;
165 }
166
167 SkBitmap bitmap;
168 SkMallocPixelRef::ZeroedPRFactory zeroFactory;
169 SkCodec::Options options;
170 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
171
kjlubick2a42f482016-02-16 16:14:23 -0800172 if (!bitmap.tryAllocPixels(decodeInfo, &zeroFactory, colorTable.get())) {
kjlubick47d158e2016-02-01 08:23:50 -0800173 SkDebugf("[terminated] Could not allocate memory. Image might be too large (%d x %d)",
kjlubick2a42f482016-02-16 16:14:23 -0800174 decodeInfo.width(), decodeInfo.height());
kjlubickdba57342016-01-21 05:03:28 -0800175 return 4;
176 }
177
kjlubick2a42f482016-02-16 16:14:23 -0800178 switch (mode) {
179 case 0: {//kCodecZeroInit_Mode, kCodec_Mode
180 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options,
181 colorPtr, colorCountPtr)) {
182 case SkCodec::kSuccess:
183 SkDebugf("[terminated] Success!\n");
184 break;
185 case SkCodec::kIncompleteInput:
186 SkDebugf("[terminated] Partial Success\n");
187 break;
188 case SkCodec::kInvalidConversion:
189 SkDebugf("Incompatible colortype conversion\n");
190 // Crash to allow afl-fuzz to know this was a bug.
191 raise(SIGSEGV);
192 default:
193 SkDebugf("[terminated] Couldn't getPixels.\n");
194 return 6;
195 }
196 break;
197 }
198 case 1: {//kScanline_Mode
199 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
200 colorCountPtr)) {
201 SkDebugf("[terminated] Could not start scanline decoder\n");
202 return 7;
203 }
204
205 void* dst = bitmap.getAddr(0, 0);
206 size_t rowBytes = bitmap.rowBytes();
207 uint32_t height = decodeInfo.height();
208 switch (codec->getScanlineOrder()) {
209 case SkCodec::kTopDown_SkScanlineOrder:
210 case SkCodec::kBottomUp_SkScanlineOrder:
kjlubick2a42f482016-02-16 16:14:23 -0800211 // We do not need to check the return value. On an incomplete
212 // image, memory will be filled with a default value.
213 codec->getScanlines(dst, height, rowBytes);
214 break;
kjlubick2a42f482016-02-16 16:14:23 -0800215 }
kjlubick47d158e2016-02-01 08:23:50 -0800216 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800217 break;
kjlubick2a42f482016-02-16 16:14:23 -0800218 }
219 case 2: { //kStripe_Mode
220 const int height = decodeInfo.height();
221 // This value is chosen arbitrarily. We exercise more cases by choosing a value that
222 // does not align with image blocks.
223 const int stripeHeight = 37;
224 const int numStripes = (height + stripeHeight - 1) / stripeHeight;
225
226 // Decode odd stripes
227 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
228 colorCountPtr)
229 || SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) {
230 // This mode was designed to test the new skip scanlines API in libjpeg-turbo.
231 // Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting
232 // to run this test for image types that do not have this scanline ordering.
233 SkDebugf("[terminated] Could not start top-down scanline decoder\n");
234 return 8;
235 }
236
237 for (int i = 0; i < numStripes; i += 2) {
238 // Skip a stripe
239 const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
240 codec->skipScanlines(linesToSkip);
241
242 // Read a stripe
243 const int startY = (i + 1) * stripeHeight;
244 const int linesToRead = SkTMin(stripeHeight, height - startY);
245 if (linesToRead > 0) {
246 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
247 }
248 }
249
250 // Decode even stripes
251 const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo, nullptr,
252 colorPtr, colorCountPtr);
253 if (SkCodec::kSuccess != startResult) {
254 SkDebugf("[terminated] Failed to restart scanline decoder with same parameters.\n");
255 return 9;
256 }
257 for (int i = 0; i < numStripes; i += 2) {
258 // Read a stripe
259 const int startY = i * stripeHeight;
260 const int linesToRead = SkTMin(stripeHeight, height - startY);
261 codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes());
262
263 // Skip a stripe
264 const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
265 if (linesToSkip > 0) {
266 codec->skipScanlines(linesToSkip);
267 }
268 }
269 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800270 break;
kjlubick2a42f482016-02-16 16:14:23 -0800271 }
272 case 3: { //kSubset_Mode
273 // Arbitrarily choose a divisor.
274 int divisor = 2;
275 // Total width/height of the image.
276 const int W = codec->getInfo().width();
277 const int H = codec->getInfo().height();
278 if (divisor > W || divisor > H) {
279 SkDebugf("[terminated] Cannot codec subset: divisor %d is too big "
280 "with dimensions (%d x %d)\n", divisor, W, H);
281 return 10;
282 }
283 // subset dimensions
284 // SkWebpCodec, the only one that supports subsets, requires even top/left boundaries.
285 const int w = SkAlign2(W / divisor);
286 const int h = SkAlign2(H / divisor);
287 SkIRect subset;
288 SkCodec::Options opts;
289 opts.fSubset = &subset;
290 SkBitmap subsetBm;
291 // We will reuse pixel memory from bitmap.
292 void* pixels = bitmap.getPixels();
293 // Keep track of left and top (for drawing subsetBm into canvas). We could use
294 // fscale * x and fscale * y, but we want integers such that the next subset will start
295 // where the last one ended. So we'll add decodeInfo.width() and height().
296 int left = 0;
297 for (int x = 0; x < W; x += w) {
298 int top = 0;
299 for (int y = 0; y < H; y+= h) {
300 // Do not make the subset go off the edge of the image.
301 const int preScaleW = SkTMin(w, W - x);
302 const int preScaleH = SkTMin(h, H - y);
303 subset.setXYWH(x, y, preScaleW, preScaleH);
304 // And fscale
305 // FIXME: Should we have a version of getScaledDimensions that takes a subset
306 // into account?
307 decodeInfo = decodeInfo.makeWH(
308 SkTMax(1, SkScalarRoundToInt(preScaleW * fscale)),
309 SkTMax(1, SkScalarRoundToInt(preScaleH * fscale)));
310 size_t rowBytes = decodeInfo.minRowBytes();
311 if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes, colorTable.get(),
312 nullptr, nullptr)) {
313 SkDebugf("[terminated] Could not install pixels.\n");
314 return 11;
315 }
316 const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes,
317 &opts, colorPtr, colorCountPtr);
318 switch (result) {
319 case SkCodec::kSuccess:
320 case SkCodec::kIncompleteInput:
321 SkDebugf("okay\n");
322 break;
323 case SkCodec::kInvalidConversion:
324 if (0 == (x|y)) {
325 // First subset is okay to return unimplemented.
326 SkDebugf("[terminated] Incompatible colortype conversion\n");
327 return 12;
328 }
329 // If the first subset succeeded, a later one should not fail.
330 // fall through to failure
331 case SkCodec::kUnimplemented:
332 if (0 == (x|y)) {
333 // First subset is okay to return unimplemented.
334 SkDebugf("[terminated] subset codec not supported\n");
335 return 13;
336 }
337 // If the first subset succeeded, why would a later one fail?
338 // fall through to failure
339 default:
340 SkDebugf("[terminated] subset codec failed to decode (%d, %d, %d, %d) "
341 "with dimensions (%d x %d)\t error %d\n",
342 x, y, decodeInfo.width(), decodeInfo.height(),
343 W, H, result);
344 return 14;
345 }
346 // translate by the scaled height.
347 top += decodeInfo.height();
348 }
349 // translate by the scaled width.
350 left += decodeInfo.width();
351 }
352 SkDebugf("[terminated] Success!\n");
353 break;
354 }
kjlubickdba57342016-01-21 05:03:28 -0800355 default:
kjlubick2a42f482016-02-16 16:14:23 -0800356 SkDebugf("[terminated] Mode not implemented yet\n");
kjlubickdba57342016-01-21 05:03:28 -0800357 }
358
359 dump_png(bitmap);
mtkleinf5e97822016-01-15 06:19:53 -0800360 return 0;
mtklein65e58242016-01-13 12:57:57 -0800361}
362
reed42943c82016-09-12 12:01:44 -0700363int fuzz_skp(sk_sp<SkData> bytes) {
kjlubickdba57342016-01-21 05:03:28 -0800364 SkMemoryStream stream(bytes);
365 SkDebugf("Decoding\n");
reedca2622b2016-03-18 07:25:55 -0700366 sk_sp<SkPicture> pic(SkPicture::MakeFromStream(&stream));
kjlubickdba57342016-01-21 05:03:28 -0800367 if (!pic) {
kjlubick47d158e2016-02-01 08:23:50 -0800368 SkDebugf("[terminated] Couldn't decode as a picture.\n");
kjlubickdba57342016-01-21 05:03:28 -0800369 return 3;
370 }
371 SkDebugf("Rendering\n");
372 SkBitmap bitmap;
373 if (!FLAGS_dump.isEmpty()) {
374 SkIRect size = pic->cullRect().roundOut();
375 bitmap.allocN32Pixels(size.width(), size.height());
376 }
377 SkCanvas canvas(bitmap);
378 canvas.drawPicture(pic);
kjlubick47d158e2016-02-01 08:23:50 -0800379 SkDebugf("[terminated] Success! Decoded and rendered an SkPicture!\n");
kjlubickdba57342016-01-21 05:03:28 -0800380 dump_png(bitmap);
381 return 0;
382}
mtklein65e58242016-01-13 12:57:57 -0800383
reed42943c82016-09-12 12:01:44 -0700384int fuzz_icc(sk_sp<SkData> bytes) {
Brian Osman526972e2016-10-24 09:24:02 -0400385 sk_sp<SkColorSpace> space(SkColorSpace::MakeICC(bytes->data(), bytes->size()));
kjlubick897a8e32016-06-09 07:15:12 -0700386 if (!space) {
387 SkDebugf("[terminated] Couldn't decode ICC.\n");
388 return 1;
389 }
390 SkDebugf("[terminated] Success! Decoded ICC.\n");
391 return 0;
392}
393
reed42943c82016-09-12 12:01:44 -0700394int fuzz_color_deserialize(sk_sp<SkData> bytes) {
kjlubick3e3c1a52016-06-23 10:49:27 -0700395 sk_sp<SkColorSpace> space(SkColorSpace::Deserialize(bytes->data(), bytes->size()));
396 if (!space) {
397 SkDebugf("[terminated] Couldn't deserialize Colorspace.\n");
398 return 1;
399 }
400 SkDebugf("[terminated] Success! deserialized Colorspace.\n");
401 return 0;
402}
403
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500404#if SK_SUPPORT_GPU
kjlubicke7195772016-10-18 10:06:24 -0700405int fuzz_sksl2glsl(sk_sp<SkData> bytes) {
406 SkSL::Compiler compiler;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500407 SkString output;
kjlubicke7195772016-10-18 10:06:24 -0700408 bool result = compiler.toGLSL(SkSL::Program::kFragment_Kind,
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500409 SkString((const char*)bytes->data()), *SkSL::GLSLCapsFactory::Default(), &output);
kjlubicke7195772016-10-18 10:06:24 -0700410
411 if (!result) {
412 SkDebugf("[terminated] Couldn't compile input.\n");
413 return 1;
414 }
415 SkDebugf("[terminated] Success! Compiled input.\n");
416 return 0;
417}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500418#endif
kjlubicke7195772016-10-18 10:06:24 -0700419
reed42943c82016-09-12 12:01:44 -0700420Fuzz::Fuzz(sk_sp<SkData> bytes) : fBytes(bytes), fNextByte(0) {}
mtklein65e58242016-01-13 12:57:57 -0800421
Kevin Lubick2f535ce2016-11-01 15:01:12 -0400422void Fuzz::signalBug() { SkDebugf("Signal bug\n"); raise(SIGSEGV); }
mtkleina1159422016-01-15 05:46:54 -0800423
kjlubicke5654502016-07-19 16:50:03 -0700424size_t Fuzz::size() { return fBytes->size(); }
425
Kevin Lubick2f535ce2016-11-01 15:01:12 -0400426bool Fuzz::exhausted() {
427 return fBytes->size() == fNextByte;
kjlubick5bd98a22016-02-18 06:27:38 -0800428}