blob: 02bf76e6653fe6cb1a09ee1730e316b5a38d387e [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"
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
mtkleina1159422016-01-15 05:46:54 -080020#include <signal.h>
mtkleinf5e97822016-01-15 06:19:53 -080021#include <stdlib.h>
22
kjlubickdba57342016-01-21 05:03:28 -080023__SK_FORCE_IMAGE_DECODER_LINKING;
24
25DEFINE_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 -080026DEFINE_string2(name, n, "", "If --type is 'api', fuzz the API with this name.");
kjlubickdba57342016-01-21 05:03:28 -080027
28DEFINE_string2(type, t, "api", "How to interpret --bytes, either 'image', 'skp', or 'api'.");
29DEFINE_string2(dump, d, "", "If not empty, dump 'image' or 'skp' types as a PNG with this name.");
30
31static int printUsage(const char* name) {
mtkleind4387ea2016-01-21 06:13:52 -080032 SkDebugf("Usage: %s -t <type> -b <path/to/file> [-n api-to-fuzz]\n", name);
kjlubickdba57342016-01-21 05:03:28 -080033 return 1;
34}
35
36static int fuzz_api(SkData*);
37static int fuzz_img(SkData*);
38static int fuzz_skp(SkData*);
mtklein65e58242016-01-13 12:57:57 -080039
40int main(int argc, char** argv) {
mtkleinf5e97822016-01-15 06:19:53 -080041 SkCommandLineFlags::Parse(argc, argv);
42
mtkleind0b82342016-01-15 07:56:20 -080043 const char* path = FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0];
44 SkAutoTUnref<SkData> bytes(SkData::NewFromFileName(path));
kjlubickdba57342016-01-21 05:03:28 -080045 if (!bytes) {
46 SkDebugf("Could not read %s\n", path);
47 return 2;
48 }
mtklein65e58242016-01-13 12:57:57 -080049
mtkleind4387ea2016-01-21 06:13:52 -080050 if (!FLAGS_type.isEmpty()) {
51 switch (FLAGS_type[0][0]) {
52 case 'a': return fuzz_api(bytes);
53 case 'i': return fuzz_img(bytes);
54 case 's': return fuzz_skp(bytes);
55 }
kjlubickdba57342016-01-21 05:03:28 -080056 }
57 return printUsage(argv[0]);
58}
59
60int fuzz_api(SkData* bytes) {
mtkleind4387ea2016-01-21 06:13:52 -080061 const char* name = FLAGS_name.isEmpty() ? "" : FLAGS_name[0];
62
mtklein65e58242016-01-13 12:57:57 -080063 for (auto r = SkTRegistry<Fuzzable>::Head(); r; r = r->next()) {
64 auto fuzzable = r->factory();
mtkleind4387ea2016-01-21 06:13:52 -080065 if (0 == strcmp(name, fuzzable.name)) {
mtkleinf5e97822016-01-15 06:19:53 -080066 SkDebugf("Fuzzing %s...\n", fuzzable.name);
67 Fuzz fuzz(bytes);
mtklein65e58242016-01-13 12:57:57 -080068 fuzzable.fn(&fuzz);
kjlubick47d158e2016-02-01 08:23:50 -080069 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -080070 return 0;
mtklein65e58242016-01-13 12:57:57 -080071 }
72 }
mtkleind4387ea2016-01-21 06:13:52 -080073
74 SkDebugf("When using --type api, please choose an API to fuzz with --name/-n:\n");
75 for (auto r = SkTRegistry<Fuzzable>::Head(); r; r = r->next()) {
76 auto fuzzable = r->factory();
77 SkDebugf("\t%s\n", fuzzable.name);
78 }
kjlubickdba57342016-01-21 05:03:28 -080079 return 1;
80}
81
82static void dump_png(SkBitmap bitmap) {
83 if (!FLAGS_dump.isEmpty()) {
84 SkImageEncoder::EncodeFile(FLAGS_dump[0], bitmap, SkImageEncoder::kPNG_Type, 100);
85 SkDebugf("Dumped to %s\n", FLAGS_dump[0]);
86 }
87}
88
89int fuzz_img(SkData* bytes) {
kjlubick47d158e2016-02-01 08:23:50 -080090 SkDebugf("Decoding\n");
kjlubickdba57342016-01-21 05:03:28 -080091 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(bytes));
92 if (nullptr == codec.get()) {
kjlubick47d158e2016-02-01 08:23:50 -080093 SkDebugf("[terminated] Couldn't create codec.\n");
kjlubickdba57342016-01-21 05:03:28 -080094 return 3;
95 }
96
97 SkImageInfo decodeInfo = codec->getInfo();
98 // Construct a color table for the decode if necessary
99 SkAutoTUnref<SkColorTable> colorTable(nullptr);
100 SkPMColor* colorPtr = nullptr;
101 int* colorCountPtr = nullptr;
102 int maxColors = 256;
103 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
104 SkPMColor colors[256];
105 colorTable.reset(new SkColorTable(colors, maxColors));
106 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
107 colorCountPtr = &maxColors;
108 }
109
110 SkBitmap bitmap;
111 SkMallocPixelRef::ZeroedPRFactory zeroFactory;
112 SkCodec::Options options;
113 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
114
115 if (!bitmap.tryAllocPixels(decodeInfo, &zeroFactory, nullptr)) {
kjlubick47d158e2016-02-01 08:23:50 -0800116 SkDebugf("[terminated] Could not allocate memory. Image might be too large (%d x %d)",
kjlubickdba57342016-01-21 05:03:28 -0800117 decodeInfo.width(), decodeInfo.height());
118 return 4;
119 }
120
121 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options,
122 colorPtr, colorCountPtr)) {
123 case SkCodec::kSuccess:
kjlubick47d158e2016-02-01 08:23:50 -0800124 SkDebugf("[terminated] Success!\n");
kjlubickdba57342016-01-21 05:03:28 -0800125 break;
126 case SkCodec::kIncompleteInput:
kjlubick47d158e2016-02-01 08:23:50 -0800127 SkDebugf("[terminated] Partial Success\n");
kjlubickdba57342016-01-21 05:03:28 -0800128 break;
129 case SkCodec::kInvalidConversion:
kjlubick47d158e2016-02-01 08:23:50 -0800130 SkDebugf("[terminated] Incompatible colortype conversion\n");
kjlubickdba57342016-01-21 05:03:28 -0800131 return 5;
132 default:
133 // Everything else is considered a failure.
kjlubick47d158e2016-02-01 08:23:50 -0800134 SkDebugf("[terminated] Couldn't getPixels.\n");
kjlubickdba57342016-01-21 05:03:28 -0800135 return 6;
136 }
137
138 dump_png(bitmap);
mtkleinf5e97822016-01-15 06:19:53 -0800139 return 0;
mtklein65e58242016-01-13 12:57:57 -0800140}
141
kjlubickdba57342016-01-21 05:03:28 -0800142int fuzz_skp(SkData* bytes) {
143 SkMemoryStream stream(bytes);
144 SkDebugf("Decoding\n");
145 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(&stream));
146 if (!pic) {
kjlubick47d158e2016-02-01 08:23:50 -0800147 SkDebugf("[terminated] Couldn't decode as a picture.\n");
kjlubickdba57342016-01-21 05:03:28 -0800148 return 3;
149 }
150 SkDebugf("Rendering\n");
151 SkBitmap bitmap;
152 if (!FLAGS_dump.isEmpty()) {
153 SkIRect size = pic->cullRect().roundOut();
154 bitmap.allocN32Pixels(size.width(), size.height());
155 }
156 SkCanvas canvas(bitmap);
157 canvas.drawPicture(pic);
kjlubick47d158e2016-02-01 08:23:50 -0800158 SkDebugf("[terminated] Success! Decoded and rendered an SkPicture!\n");
kjlubickdba57342016-01-21 05:03:28 -0800159 dump_png(bitmap);
160 return 0;
161}
mtklein65e58242016-01-13 12:57:57 -0800162
mtklein24a22c72016-01-14 04:59:42 -0800163Fuzz::Fuzz(SkData* bytes) : fBytes(SkSafeRef(bytes)), fNextByte(0) {}
mtklein65e58242016-01-13 12:57:57 -0800164
mtkleina1159422016-01-15 05:46:54 -0800165void Fuzz::signalBug () { raise(SIGSEGV); }
166void Fuzz::signalBoring() { exit(0); }
167
mtklein24a22c72016-01-14 04:59:42 -0800168template <typename T>
mtkleina1159422016-01-15 05:46:54 -0800169T Fuzz::nextT() {
170 if (fNextByte + sizeof(T) > fBytes->size()) {
171 this->signalBoring();
mtklein24a22c72016-01-14 04:59:42 -0800172 }
mtkleina1159422016-01-15 05:46:54 -0800173
mtklein24a22c72016-01-14 04:59:42 -0800174 T val;
mtkleina1159422016-01-15 05:46:54 -0800175 memcpy(&val, fBytes->bytes() + fNextByte, sizeof(T));
176 fNextByte += sizeof(T);
mtklein24a22c72016-01-14 04:59:42 -0800177 return val;
178}
179
mtkleina1159422016-01-15 05:46:54 -0800180uint8_t Fuzz::nextB() { return this->nextT<uint8_t >(); }
181uint32_t Fuzz::nextU() { return this->nextT<uint32_t>(); }
182float Fuzz::nextF() { return this->nextT<float >(); }
mtklein65e58242016-01-13 12:57:57 -0800183