| 1 |
/* |
| 2 |
* Copyright 2016 Mozilla Foundation |
| 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 "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkFont.h" |
| 12 |
#include "include/core/SkImage.h" |
| 13 |
#include "include/core/SkPath.h" |
| 14 |
#include "include/core/SkPathBuilder.h" |
| 15 |
#include "include/core/SkSurface.h" |
| 16 |
#include "include/core/SkTextBlob.h" |
| 17 |
#include "include/core/SkTypeface.h" |
| 18 |
#include "src/core/SkPaintPriv.h" |
| 19 |
#include "tools/fonts/FontToolUtils.h" |
| 20 |
|
| 21 |
static const int kBmpSize = 24; |
| 22 |
static const int kMaxX = 250; |
| 23 |
static const int kMaxY = 250; |
| 24 |
static const int kPtsLen = 10; |
| 25 |
static const int kTxtLen = 5; |
| 26 |
|
| 27 |
static void init_string(Fuzz* fuzz, char* str, size_t bufSize) { |
| 28 |
for (size_t i = 0; i < bufSize-1; ++i) { |
| 29 |
fuzz->nextRange(&str[i], 0x20, 0x7E); // printable ASCII |
| 30 |
} |
| 31 |
str[bufSize-1] = '\0'; |
| 32 |
} |
| 33 |
|
| 34 |
// make_paint mostly borrowed from FilterFuzz.cpp |
| 35 |
static void init_paint(Fuzz* fuzz, SkPaint* p) { |
| 36 |
bool b; |
| 37 |
fuzz->next(&b); |
| 38 |
p->setAntiAlias(b); |
| 39 |
|
| 40 |
uint8_t tmp_u8; |
| 41 |
fuzz->nextRange(&tmp_u8, 0, (int)SkBlendMode::kLastMode); |
| 42 |
p->setBlendMode(static_cast<SkBlendMode>(tmp_u8)); |
| 43 |
|
| 44 |
SkColor co; |
| 45 |
fuzz->next(&co); |
| 46 |
p->setColor(co); |
| 47 |
|
| 48 |
fuzz->next(&b); |
| 49 |
p->setDither(b); |
| 50 |
|
| 51 |
fuzz->nextRange(&tmp_u8, 0, (int)SkPaint::kLast_Cap); |
| 52 |
p->setStrokeCap(static_cast<SkPaint::Cap>(tmp_u8)); |
| 53 |
|
| 54 |
fuzz->nextRange(&tmp_u8, 0, (int)SkPaint::kLast_Join); |
| 55 |
p->setStrokeJoin(static_cast<SkPaint::Join>(tmp_u8)); |
| 56 |
|
| 57 |
SkScalar sc; |
| 58 |
fuzz->next(&sc); |
| 59 |
p->setStrokeMiter(sc); |
| 60 |
|
| 61 |
fuzz->next(&sc); |
| 62 |
p->setStrokeWidth(sc); |
| 63 |
|
| 64 |
fuzz->nextRange(&tmp_u8, 0, (int)SkPaint::kStrokeAndFill_Style); |
| 65 |
p->setStyle(static_cast<SkPaint::Style>(tmp_u8)); |
| 66 |
} |
| 67 |
|
| 68 |
static void init_bitmap(Fuzz* fuzz, SkBitmap* bmp) { |
| 69 |
uint8_t colorType; |
| 70 |
fuzz->nextRange(&colorType, 0, (int)kLastEnum_SkColorType); |
| 71 |
// ColorType needs to match what the system configuration is. |
| 72 |
if (colorType == kRGBA_8888_SkColorType || colorType == kBGRA_8888_SkColorType) { |
| 73 |
colorType = kN32_SkColorType; |
| 74 |
} |
| 75 |
bool b; |
| 76 |
fuzz->next(&b); |
| 77 |
SkImageInfo info = SkImageInfo::Make(kBmpSize, |
| 78 |
kBmpSize, |
| 79 |
(SkColorType)colorType, |
| 80 |
b ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
| 81 |
if (!bmp->tryAllocPixels(info)) { |
| 82 |
SkDEBUGF("Bitmap not allocated\n"); |
| 83 |
} |
| 84 |
SkColor c; |
| 85 |
fuzz->next(&c); |
| 86 |
bmp->eraseColor(c); |
| 87 |
|
| 88 |
fuzz->next(&b); |
| 89 |
SkPaint p; |
| 90 |
if (b) { |
| 91 |
init_paint(fuzz, &p); |
| 92 |
} |
| 93 |
else { |
| 94 |
fuzz->next(&c); |
| 95 |
p.setColor(c); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
static void init_surface(Fuzz* fuzz, sk_sp<SkSurface>* s) { |
| 100 |
uint8_t x, y; |
| 101 |
fuzz->nextRange(&x, 1, kMaxX); |
| 102 |
fuzz->nextRange(&y, 1, kMaxY); |
| 103 |
*s = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(x, y)); |
| 104 |
|
| 105 |
if (!*s) { |
| 106 |
// Was possibly too big for the memory constrained fuzzing environments |
| 107 |
*s = SkSurfaces::Null(x, y); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
static void fuzz_drawText(Fuzz* fuzz, sk_sp<SkTypeface> typeface) { |
| 113 |
SkFont font(std::move(typeface)); |
| 114 |
SkPaint p; |
| 115 |
init_paint(fuzz, &p); |
| 116 |
sk_sp<SkSurface> surface; |
| 117 |
init_surface(fuzz, &surface); |
| 118 |
|
| 119 |
char text[kTxtLen]; |
| 120 |
init_string(fuzz, text, kTxtLen); |
| 121 |
|
| 122 |
SkScalar x, y; |
| 123 |
fuzz->next(&x, &y); |
| 124 |
// populate pts array |
| 125 |
SkPoint pts[kPtsLen]; |
| 126 |
for (uint8_t i = 0; i < kPtsLen; ++i) { |
| 127 |
pts[i].set(x, y); |
| 128 |
x += font.getSize(); |
| 129 |
} |
| 130 |
|
| 131 |
bool b; |
| 132 |
fuzz->next(&b); |
| 133 |
font.setForceAutoHinting(b); |
| 134 |
fuzz->next(&b); |
| 135 |
font.setEmbeddedBitmaps(b); |
| 136 |
fuzz->next(&b); |
| 137 |
font.setEmbolden(b); |
| 138 |
fuzz->next(&b); |
| 139 |
font.setEdging(b ? SkFont::Edging::kAntiAlias : SkFont::Edging::kSubpixelAntiAlias); |
| 140 |
fuzz->next(&b); |
| 141 |
font.setLinearMetrics(b); |
| 142 |
fuzz->next(&b); |
| 143 |
font.setSubpixel(b); |
| 144 |
fuzz->next(&x); |
| 145 |
font.setScaleX(x); |
| 146 |
fuzz->next(&x); |
| 147 |
font.setSkewX(x); |
| 148 |
fuzz->next(&x); |
| 149 |
font.setSize(x); |
| 150 |
|
| 151 |
SkCanvas* cnv = surface->getCanvas(); |
| 152 |
fuzz->next(&x); |
| 153 |
fuzz->next(&y); |
| 154 |
cnv->drawTextBlob(SkTextBlob::MakeFromPosText(text, kTxtLen-1, pts, font), x, y, p); |
| 155 |
} |
| 156 |
|
| 157 |
static void fuzz_drawCircle(Fuzz* fuzz) { |
| 158 |
SkPaint p; |
| 159 |
init_paint(fuzz, &p); |
| 160 |
sk_sp<SkSurface> surface; |
| 161 |
init_surface(fuzz, &surface); |
| 162 |
|
| 163 |
SkScalar a, b, c; |
| 164 |
fuzz->next(&a, &b, &c); |
| 165 |
surface->getCanvas()->drawCircle(a, b, c, p); |
| 166 |
} |
| 167 |
|
| 168 |
static void fuzz_drawLine(Fuzz* fuzz) { |
| 169 |
SkPaint p; |
| 170 |
init_paint(fuzz, &p); |
| 171 |
sk_sp<SkSurface> surface; |
| 172 |
init_surface(fuzz, &surface); |
| 173 |
|
| 174 |
SkScalar a, b, c, d; |
| 175 |
fuzz->next(&a, &b, &c, &d); |
| 176 |
surface->getCanvas()->drawLine(a, b, c, d, p); |
| 177 |
} |
| 178 |
|
| 179 |
static void fuzz_drawRect(Fuzz* fuzz) { |
| 180 |
SkPaint p; |
| 181 |
init_paint(fuzz, &p); |
| 182 |
sk_sp<SkSurface> surface; |
| 183 |
init_surface(fuzz, &surface); |
| 184 |
|
| 185 |
SkScalar a, b, c, d; |
| 186 |
fuzz->next(&a, &b, &c, &d); |
| 187 |
SkRect r; |
| 188 |
r = SkRect::MakeXYWH(a, b, c, d); |
| 189 |
|
| 190 |
SkCanvas* cnv = surface->getCanvas(); |
| 191 |
cnv->drawRect(r, p); |
| 192 |
|
| 193 |
bool bl; |
| 194 |
fuzz->next(&bl); |
| 195 |
fuzz->next(&a, &b, &c, &d); |
| 196 |
r = SkRect::MakeXYWH(a, b, c, d); |
| 197 |
cnv->clipRect(r, SkClipOp::kIntersect, bl); |
| 198 |
} |
| 199 |
|
| 200 |
static void fuzz_drawPath(Fuzz* fuzz) { |
| 201 |
SkPaint p; |
| 202 |
init_paint(fuzz, &p); |
| 203 |
sk_sp<SkSurface> surface; |
| 204 |
init_surface(fuzz, &surface); |
| 205 |
|
| 206 |
// TODO(kjlubick): put the ability to fuzz a path in shared file, with |
| 207 |
// other common things (e.g. rects, lines) |
| 208 |
uint8_t i, j; |
| 209 |
fuzz->nextRange(&i, 0, 10); // set i to number of operations to perform |
| 210 |
SkPathBuilder builder; |
| 211 |
SkScalar a, b, c, d, e, f; |
| 212 |
for (int k = 0; k < i; ++k) { |
| 213 |
fuzz->nextRange(&j, 0, 5); // set j to choose operation to perform |
| 214 |
switch (j) { |
| 215 |
case 0: |
| 216 |
fuzz->next(&a, &b); |
| 217 |
builder.moveTo(a, b); |
| 218 |
break; |
| 219 |
case 1: |
| 220 |
fuzz->next(&a, &b); |
| 221 |
builder.lineTo(a, b); |
| 222 |
break; |
| 223 |
case 2: |
| 224 |
fuzz->next(&a, &b, &c, &d); |
| 225 |
builder.quadTo(a, b, c, d); |
| 226 |
break; |
| 227 |
case 3: |
| 228 |
fuzz->next(&a, &b, &c, &d, &e); |
| 229 |
if (!(e > 0)) { |
| 230 |
e = 1.0f; |
| 231 |
} |
| 232 |
builder.conicTo(a, b, c, d, e); |
| 233 |
break; |
| 234 |
case 4: |
| 235 |
fuzz->next(&a, &b, &c, &d, &e, &f); |
| 236 |
builder.cubicTo(a, b, c, d, e, f); |
| 237 |
break; |
| 238 |
case 5: |
| 239 |
fuzz->next(&a, &b, &c, &d, &e); |
| 240 |
builder.arcTo({a, b}, {c, d}, e); |
| 241 |
break; |
| 242 |
} |
| 243 |
} |
| 244 |
SkPath path = builder.close().detach(); |
| 245 |
|
| 246 |
SkCanvas* cnv = surface->getCanvas(); |
| 247 |
cnv->drawPath(path, p); |
| 248 |
|
| 249 |
bool bl; |
| 250 |
fuzz->next(&bl); |
| 251 |
cnv->clipPath(path, SkClipOp::kIntersect, bl); |
| 252 |
} |
| 253 |
|
| 254 |
static void fuzz_drawImage(Fuzz* fuzz) { |
| 255 |
SkPaint p; |
| 256 |
init_paint(fuzz, &p); |
| 257 |
sk_sp<SkSurface> surface; |
| 258 |
init_surface(fuzz, &surface); |
| 259 |
SkBitmap bmp; |
| 260 |
init_bitmap(fuzz, &bmp); |
| 261 |
|
| 262 |
sk_sp<SkImage> image(bmp.asImage()); |
| 263 |
|
| 264 |
bool bl; |
| 265 |
fuzz->next(&bl); |
| 266 |
SkScalar a, b; |
| 267 |
fuzz->next(&a, &b); |
| 268 |
if (bl) { |
| 269 |
surface->getCanvas()->drawImage(image, a, b, SkSamplingOptions(), &p); |
| 270 |
} |
| 271 |
else { |
| 272 |
SkRect dst = SkRect::MakeWH(a, b); |
| 273 |
fuzz->next(&a, &b); |
| 274 |
SkRect src = SkRect::MakeWH(a, b); |
| 275 |
uint8_t x; |
| 276 |
fuzz->nextRange(&x, 0, 1); |
| 277 |
SkCanvas::SrcRectConstraint cst = (SkCanvas::SrcRectConstraint)x; |
| 278 |
surface->getCanvas()->drawImageRect(image.get(), src, dst, SkSamplingOptions(), &p, cst); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
static void fuzz_drawPaint(Fuzz* fuzz) { |
| 283 |
SkPaint l, p; |
| 284 |
init_paint(fuzz, &p); |
| 285 |
sk_sp<SkSurface> surface; |
| 286 |
init_surface(fuzz, &surface); |
| 287 |
|
| 288 |
surface->getCanvas()->drawPaint(p); |
| 289 |
} |
| 290 |
|
| 291 |
DEF_FUZZ(DrawFunctions, fuzz) { |
| 292 |
uint8_t i; |
| 293 |
fuzz->next(&i); |
| 294 |
|
| 295 |
switch(i) { |
| 296 |
case 0: { |
| 297 |
sk_sp<SkTypeface> f = ToolUtils::DefaultPortableTypeface(); |
| 298 |
if (f == nullptr) { |
| 299 |
SkDebugf("Could not initialize font.\n"); |
| 300 |
fuzz->signalBug(); |
| 301 |
} |
| 302 |
SkDEBUGF("Fuzz DrawText\n"); |
| 303 |
fuzz_drawText(fuzz, f); |
| 304 |
return; |
| 305 |
} |
| 306 |
case 1: |
| 307 |
SkDEBUGF("Fuzz DrawRect\n"); |
| 308 |
fuzz_drawRect(fuzz); |
| 309 |
return; |
| 310 |
case 2: |
| 311 |
SkDEBUGF("Fuzz DrawCircle\n"); |
| 312 |
fuzz_drawCircle(fuzz); |
| 313 |
return; |
| 314 |
case 3: |
| 315 |
SkDEBUGF("Fuzz DrawLine\n"); |
| 316 |
fuzz_drawLine(fuzz); |
| 317 |
return; |
| 318 |
case 4: |
| 319 |
SkDEBUGF("Fuzz DrawPath\n"); |
| 320 |
fuzz_drawPath(fuzz); |
| 321 |
return; |
| 322 |
case 5: |
| 323 |
SkDEBUGF("Fuzz DrawImage/DrawImageRect\n"); |
| 324 |
fuzz_drawImage(fuzz); |
| 325 |
return; |
| 326 |
case 6: |
| 327 |
SkDEBUGF("Fuzz DrawPaint\n"); |
| 328 |
fuzz_drawPaint(fuzz); |
| 329 |
return; |
| 330 |
} |
| 331 |
} |