blob: d879f298b26c28dc5f167d50054b3903cd7228f8 [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);
kjlubickdba57342016-01-21 05:03:28 -080069 return 0;
mtklein65e58242016-01-13 12:57:57 -080070 }
71 }
mtkleind4387ea2016-01-21 06:13:52 -080072
73 SkDebugf("When using --type api, please choose an API to fuzz with --name/-n:\n");
74 for (auto r = SkTRegistry<Fuzzable>::Head(); r; r = r->next()) {
75 auto fuzzable = r->factory();
76 SkDebugf("\t%s\n", fuzzable.name);
77 }
kjlubickdba57342016-01-21 05:03:28 -080078 return 1;
79}
80
81static void dump_png(SkBitmap bitmap) {
82 if (!FLAGS_dump.isEmpty()) {
83 SkImageEncoder::EncodeFile(FLAGS_dump[0], bitmap, SkImageEncoder::kPNG_Type, 100);
84 SkDebugf("Dumped to %s\n", FLAGS_dump[0]);
85 }
86}
87
88int fuzz_img(SkData* bytes) {
89 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(bytes));
90 if (nullptr == codec.get()) {
91 SkDebugf("Couldn't create codec.");
92 return 3;
93 }
94
95 SkImageInfo decodeInfo = codec->getInfo();
96 // Construct a color table for the decode if necessary
97 SkAutoTUnref<SkColorTable> colorTable(nullptr);
98 SkPMColor* colorPtr = nullptr;
99 int* colorCountPtr = nullptr;
100 int maxColors = 256;
101 if (kIndex_8_SkColorType == decodeInfo.colorType()) {
102 SkPMColor colors[256];
103 colorTable.reset(new SkColorTable(colors, maxColors));
104 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
105 colorCountPtr = &maxColors;
106 }
107
108 SkBitmap bitmap;
109 SkMallocPixelRef::ZeroedPRFactory zeroFactory;
110 SkCodec::Options options;
111 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
112
113 if (!bitmap.tryAllocPixels(decodeInfo, &zeroFactory, nullptr)) {
114 SkDebugf("Could not allocate memory. Image might be too large (%d x %d)",
115 decodeInfo.width(), decodeInfo.height());
116 return 4;
117 }
118
119 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options,
120 colorPtr, colorCountPtr)) {
121 case SkCodec::kSuccess:
122 SkDebugf("Success!\n");
123 break;
124 case SkCodec::kIncompleteInput:
125 SkDebugf("Partial Success\n");
126 break;
127 case SkCodec::kInvalidConversion:
128 SkDebugf("Incompatible colortype conversion");
129 return 5;
130 default:
131 // Everything else is considered a failure.
132 SkDebugf("Couldn't getPixels.");
133 return 6;
134 }
135
136 dump_png(bitmap);
mtkleinf5e97822016-01-15 06:19:53 -0800137 return 0;
mtklein65e58242016-01-13 12:57:57 -0800138}
139
kjlubickdba57342016-01-21 05:03:28 -0800140int fuzz_skp(SkData* bytes) {
141 SkMemoryStream stream(bytes);
142 SkDebugf("Decoding\n");
143 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(&stream));
144 if (!pic) {
145 SkDebugf("Couldn't decode as a picture.\n");
146 return 3;
147 }
148 SkDebugf("Rendering\n");
149 SkBitmap bitmap;
150 if (!FLAGS_dump.isEmpty()) {
151 SkIRect size = pic->cullRect().roundOut();
152 bitmap.allocN32Pixels(size.width(), size.height());
153 }
154 SkCanvas canvas(bitmap);
155 canvas.drawPicture(pic);
156 SkDebugf("Decoded and rendered an SkPicture!\n");
157 dump_png(bitmap);
158 return 0;
159}
mtklein65e58242016-01-13 12:57:57 -0800160
mtklein24a22c72016-01-14 04:59:42 -0800161Fuzz::Fuzz(SkData* bytes) : fBytes(SkSafeRef(bytes)), fNextByte(0) {}
mtklein65e58242016-01-13 12:57:57 -0800162
mtkleina1159422016-01-15 05:46:54 -0800163void Fuzz::signalBug () { raise(SIGSEGV); }
164void Fuzz::signalBoring() { exit(0); }
165
mtklein24a22c72016-01-14 04:59:42 -0800166template <typename T>
mtkleina1159422016-01-15 05:46:54 -0800167T Fuzz::nextT() {
168 if (fNextByte + sizeof(T) > fBytes->size()) {
169 this->signalBoring();
mtklein24a22c72016-01-14 04:59:42 -0800170 }
mtkleina1159422016-01-15 05:46:54 -0800171
mtklein24a22c72016-01-14 04:59:42 -0800172 T val;
mtkleina1159422016-01-15 05:46:54 -0800173 memcpy(&val, fBytes->bytes() + fNextByte, sizeof(T));
174 fNextByte += sizeof(T);
mtklein24a22c72016-01-14 04:59:42 -0800175 return val;
176}
177
mtkleina1159422016-01-15 05:46:54 -0800178uint8_t Fuzz::nextB() { return this->nextT<uint8_t >(); }
179uint32_t Fuzz::nextU() { return this->nextT<uint32_t>(); }
180float Fuzz::nextF() { return this->nextT<float >(); }
mtklein65e58242016-01-13 12:57:57 -0800181