| 1 |
/* |
| 2 |
* Copyright 2017 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/Fuzz.h" |
| 9 |
#include "fuzz/FuzzCanvasHelpers.h" |
| 10 |
#include "fuzz/FuzzCommon.h" |
| 11 |
#include "include/core/SkBitmap.h" |
| 12 |
#include "include/core/SkCanvas.h" |
| 13 |
#include "include/core/SkPicture.h" |
| 14 |
#include "include/core/SkPictureRecorder.h" |
| 15 |
#include "include/core/SkSerialProcs.h" |
| 16 |
#include "include/core/SkStream.h" |
| 17 |
#include "include/docs/SkPDFDocument.h" |
| 18 |
#include "include/docs/SkPDFJpegHelpers.h" |
| 19 |
#include "include/svg/SkSVGCanvas.h" |
| 20 |
#include "include/utils/SkNullCanvas.h" |
| 21 |
#include "src/core/SkReadBuffer.h" |
| 22 |
#include "src/utils/SkJSONWriter.h" |
| 23 |
#include "tools/UrlDataManager.h" |
| 24 |
#include "tools/debugger/DebugCanvas.h" |
| 25 |
#include "tools/flags/CommandLineFlags.h" |
| 26 |
#include "tools/fonts/FontToolUtils.h" |
| 27 |
|
| 28 |
#include <iostream> |
| 29 |
#include <utility> |
| 30 |
|
| 31 |
#if defined(SK_GANESH) |
| 32 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 33 |
#include "include/gpu/ganesh/SkSurfaceGanesh.h" |
| 34 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 35 |
#include "tools/ganesh/GrContextFactory.h" |
| 36 |
#endif |
| 37 |
|
| 38 |
#if defined(SK_GL) |
| 39 |
#include "include/gpu/ganesh/gl/GrGLFunctions.h" |
| 40 |
#include "src/gpu/ganesh/gl/GrGLGpu.h" |
| 41 |
#include "src/gpu/ganesh/gl/GrGLUtil.h" |
| 42 |
#endif |
| 43 |
|
| 44 |
|
| 45 |
static DEFINE_bool2(gpuInfo, g, false, "Display GPU information on relevant targets."); |
| 46 |
|
| 47 |
DEF_FUZZ(NullCanvas, fuzz) { |
| 48 |
FuzzCanvas(fuzz, SkMakeNullCanvas().get()); |
| 49 |
} |
| 50 |
|
| 51 |
constexpr SkISize kCanvasSize = {128, 160}; |
| 52 |
|
| 53 |
DEF_FUZZ(RasterN32Canvas, fuzz) { |
| 54 |
auto surface = SkSurfaces::Raster( |
| 55 |
SkImageInfo::MakeN32Premul(kCanvasSize.width(), kCanvasSize.height())); |
| 56 |
if (!surface || !surface->getCanvas()) { fuzz->signalBug(); } |
| 57 |
FuzzCanvas(fuzz, surface->getCanvas()); |
| 58 |
} |
| 59 |
|
| 60 |
#if defined(SK_CODEC_DECODES_PNG_WITH_LIBPNG) && defined(SK_CODEC_ENCODES_PNG_WITH_LIBPNG) |
| 61 |
#include "include/codec/SkPngDecoder.h" |
| 62 |
#include "include/encode/SkPngEncoder.h" |
| 63 |
|
| 64 |
DEF_FUZZ(RasterN32CanvasViaSerialization, fuzz) { |
| 65 |
SkPictureRecorder recorder; |
| 66 |
FuzzCanvas(fuzz, recorder.beginRecording(SkIntToScalar(kCanvasSize.width()), |
| 67 |
SkIntToScalar(kCanvasSize.height()))); |
| 68 |
sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture()); |
| 69 |
if (!pic) { fuzz->signalBug(); } |
| 70 |
SkSerialProcs sProcs; |
| 71 |
sProcs.fImageProc = [](SkImage* img, void*) -> sk_sp<const SkData> { |
| 72 |
return SkPngEncoder::Encode(nullptr, img, SkPngEncoder::Options{}); |
| 73 |
}; |
| 74 |
sk_sp<SkData> data = pic->serialize(&sProcs); |
| 75 |
if (!data) { fuzz->signalBug(); } |
| 76 |
SkReadBuffer rb(data->data(), data->size()); |
| 77 |
SkCodecs::Register(SkPngDecoder::Decoder()); |
| 78 |
auto deserialized = SkPicturePriv::MakeFromBuffer(rb); |
| 79 |
if (!deserialized) { fuzz->signalBug(); } |
| 80 |
auto surface = SkSurfaces::Raster( |
| 81 |
SkImageInfo::MakeN32Premul(kCanvasSize.width(), kCanvasSize.height())); |
| 82 |
SkASSERT(surface && surface->getCanvas()); |
| 83 |
surface->getCanvas()->drawPicture(deserialized); |
| 84 |
} |
| 85 |
|
| 86 |
#endif |
| 87 |
|
| 88 |
DEF_FUZZ(ImageFilter, fuzz) { |
| 89 |
SkBitmap bitmap; |
| 90 |
if (!bitmap.tryAllocN32Pixels(256, 256)) { |
| 91 |
SkDEBUGF("Could not allocate 256x256 bitmap in ImageFilter"); |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
auto fil = MakeFuzzImageFilter(fuzz, 20); |
| 96 |
|
| 97 |
SkPaint paint; |
| 98 |
paint.setImageFilter(fil); |
| 99 |
SkCanvas canvas(bitmap); |
| 100 |
canvas.saveLayer(SkRect::MakeWH(500, 500), &paint); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
//SkRandom _rand; |
| 105 |
#define SK_ADD_RANDOM_BIT_FLIPS |
| 106 |
|
| 107 |
DEF_FUZZ(SerializedImageFilter, fuzz) { |
| 108 |
SkBitmap bitmap; |
| 109 |
if (!bitmap.tryAllocN32Pixels(256, 256)) { |
| 110 |
SkDEBUGF("Could not allocate 256x256 bitmap in SerializedImageFilter"); |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
auto filter = MakeFuzzImageFilter(fuzz, 20); |
| 115 |
if (!filter) { |
| 116 |
return; |
| 117 |
} |
| 118 |
auto data = filter->serialize(); |
| 119 |
const unsigned char* ptr = static_cast<const unsigned char*>(data->data()); |
| 120 |
size_t len = data->size(); |
| 121 |
#ifdef SK_ADD_RANDOM_BIT_FLIPS |
| 122 |
unsigned char* p = const_cast<unsigned char*>(ptr); |
| 123 |
for (size_t i = 0; i < len; ++i, ++p) { |
| 124 |
uint8_t j; |
| 125 |
fuzz->nextRange(&j, 1, 250); |
| 126 |
if (j == 1) { // 0.4% of the time, flip a bit or byte |
| 127 |
uint8_t k; |
| 128 |
fuzz->nextRange(&k, 1, 10); |
| 129 |
if (k == 1) { // Then 10% of the time, change a whole byte |
| 130 |
uint8_t s; |
| 131 |
fuzz->nextRange(&s, 0, 2); |
| 132 |
switch(s) { |
| 133 |
case 0: |
| 134 |
*p ^= 0xFF; // Flip entire byte |
| 135 |
break; |
| 136 |
case 1: |
| 137 |
*p = 0xFF; // Set all bits to 1 |
| 138 |
break; |
| 139 |
case 2: |
| 140 |
*p = 0x00; // Set all bits to 0 |
| 141 |
break; |
| 142 |
} |
| 143 |
} else { |
| 144 |
uint8_t s; |
| 145 |
fuzz->nextRange(&s, 0, 7); |
| 146 |
*p ^= (1 << 7); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
#endif // SK_ADD_RANDOM_BIT_FLIPS |
| 151 |
auto deserializedFil = SkImageFilter::Deserialize(ptr, len); |
| 152 |
|
| 153 |
// uncomment below to write out a serialized image filter (to make corpus |
| 154 |
// for -t filter_fuzz) |
| 155 |
// SkString s("./serialized_filters/sf"); |
| 156 |
// s.appendU32(_rand.nextU()); |
| 157 |
// auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag); |
| 158 |
// sk_fwrite(data->bytes(), data->size(), file); |
| 159 |
// sk_fclose(file); |
| 160 |
|
| 161 |
SkPaint paint; |
| 162 |
paint.setImageFilter(deserializedFil); |
| 163 |
|
| 164 |
SkCanvas canvas(bitmap); |
| 165 |
canvas.saveLayer(SkRect::MakeWH(256, 256), &paint); |
| 166 |
canvas.restore(); |
| 167 |
} |
| 168 |
|
| 169 |
#if defined(SK_GANESH) |
| 170 |
static void fuzz_ganesh(Fuzz* fuzz, GrDirectContext* context) { |
| 171 |
SkASSERT(context); |
| 172 |
auto surface = SkSurfaces::RenderTarget( |
| 173 |
context, |
| 174 |
skgpu::Budgeted::kNo, |
| 175 |
SkImageInfo::Make(kCanvasSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType)); |
| 176 |
SkASSERT(surface && surface->getCanvas()); |
| 177 |
FuzzCanvas(fuzz, surface->getCanvas()); |
| 178 |
} |
| 179 |
|
| 180 |
DEF_FUZZ(MockGPUCanvas, fuzz) { |
| 181 |
sk_gpu_test::GrContextFactory f; |
| 182 |
fuzz_ganesh(fuzz, f.get(skgpu::ContextType::kMock)); |
| 183 |
} |
| 184 |
#endif |
| 185 |
|
| 186 |
#ifdef SK_GL |
| 187 |
static void dump_GPU_info(GrDirectContext* context) { |
| 188 |
const GrGLInterface* gl = static_cast<GrGLGpu*>(context->priv().getGpu()) |
| 189 |
->glInterface(); |
| 190 |
const GrGLubyte* output; |
| 191 |
GR_GL_CALL_RET(gl, output, GetString(GR_GL_RENDERER)); |
| 192 |
SkDebugf("GL_RENDERER %s\n", (const char*) output); |
| 193 |
|
| 194 |
GR_GL_CALL_RET(gl, output, GetString(GR_GL_VENDOR)); |
| 195 |
SkDebugf("GL_VENDOR %s\n", (const char*) output); |
| 196 |
|
| 197 |
GR_GL_CALL_RET(gl, output, GetString(GR_GL_VERSION)); |
| 198 |
SkDebugf("GL_VERSION %s\n", (const char*) output); |
| 199 |
} |
| 200 |
|
| 201 |
DEF_FUZZ(NativeGLCanvas, fuzz) { |
| 202 |
sk_gpu_test::GrContextFactory f; |
| 203 |
auto context = f.get(skgpu::ContextType::kGL); |
| 204 |
if (!context) { |
| 205 |
context = f.get(skgpu::ContextType::kGLES); |
| 206 |
} |
| 207 |
if (FLAGS_gpuInfo) { |
| 208 |
dump_GPU_info(context); |
| 209 |
} |
| 210 |
fuzz_ganesh(fuzz, context); |
| 211 |
} |
| 212 |
#endif |
| 213 |
|
| 214 |
DEF_FUZZ(PDFCanvas, fuzz) { |
| 215 |
SkNullWStream stream; |
| 216 |
auto doc = SkPDF::MakeDocument(&stream, SkPDF::JPEG::MetadataWithCallbacks()); |
| 217 |
FuzzCanvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()), |
| 218 |
SkIntToScalar(kCanvasSize.height()))); |
| 219 |
} |
| 220 |
|
| 221 |
// not a "real" thing to fuzz, used to debug errors found while fuzzing. |
| 222 |
DEF_FUZZ(_DumpCanvas, fuzz) { |
| 223 |
DebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height()); |
| 224 |
FuzzCanvas(fuzz, &debugCanvas); |
| 225 |
std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas(); |
| 226 |
UrlDataManager dataManager(SkString("data")); |
| 227 |
SkDynamicMemoryWStream stream; |
| 228 |
SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty); |
| 229 |
writer.beginObject(); // root |
| 230 |
debugCanvas.toJSON(writer, dataManager, nullCanvas.get()); |
| 231 |
writer.endObject(); // root |
| 232 |
writer.flush(); |
| 233 |
sk_sp<SkData> json = stream.detachAsData(); |
| 234 |
fwrite(json->data(), json->size(), 1, stdout); |
| 235 |
} |
| 236 |
|
| 237 |
DEF_FUZZ(SVGCanvas, fuzz) { |
| 238 |
SkNullWStream stream; |
| 239 |
SkRect bounds = SkRect::MakeIWH(150, 150); |
| 240 |
std::unique_ptr<SkCanvas> canvas = SkSVGCanvas::Make(bounds, &stream); |
| 241 |
FuzzCanvas(fuzz, canvas.get()); |
| 242 |
} |