Tweak API fuzzers to run better in libfuzzer

Prevents logging from cluttering the stats.
Better handles limited memory.

Bug: skia:
Change-Id: I12c1a46875fd9120938cab520ef70de69c451ad8
Reviewed-on: https://skia-review.googlesource.com/110642
Reviewed-by: Mike Klein <mtklein@chromium.org>
Commit-Queue: Kevin Lubick <kjlubick@google.com>
This commit is contained in:
Kevin Lubick 2018-02-27 10:59:10 -05:00 committed by Skia Commit-Bot
parent f895a420c9
commit 1991f5502e
4 changed files with 35 additions and 17 deletions

View File

@ -109,6 +109,7 @@ inline void Fuzz::nextRange(T* n, Min min, Max max) {
}
if (min > max) {
// Avoid misuse of nextRange
SkDebugf("min > max (%d > %d) \n", min, max);
this->signalBug();
}
if (*n < 0) { // Handle negatives

View File

@ -901,7 +901,10 @@ static SkBitmap make_fuzz_bitmap(Fuzz* fuzz) {
int w, h;
fuzz->nextRange(&w, 1, 1024);
fuzz->nextRange(&h, 1, 1024);
bitmap.allocN32Pixels(w, h);
if (!bitmap.tryAllocN32Pixels(w, h)) {
SkDEBUGF(("Could not allocate pixels %d x %d", w, h));
return bitmap;
}
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
SkColor c;
@ -973,6 +976,11 @@ static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkPaint& paint) {
if (SkPaint::kGlyphID_TextEncoding == paint.getTextEncoding()) {
int glyphRange = paint.getTypeface() ? paint.getTypeface()->countGlyphs()
: SkTypeface::MakeDefault()->countGlyphs();
if (glyphRange == 0) {
// Some fuzzing environments have no fonts, so empty array is the best
// we can do.
return array;
}
int glyphCount;
fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
@ -1464,6 +1472,10 @@ static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
if (make_fuzz_t<bool>(fuzz)) {
fuzz->next(&center);
} else { // Make valid center, see SkLatticeIter::Valid().
if (img.width() == 0 || img.height() == 0) {
// bitmap may not have had its pixels initialized.
break;
}
fuzz->nextRange(&center.fLeft, 0, img.width() - 1);
fuzz->nextRange(&center.fTop, 0, img.height() - 1);
fuzz->nextRange(&center.fRight, center.fLeft + 1, img.width());

View File

@ -81,7 +81,7 @@ static void init_bitmap(Fuzz* fuzz, SkBitmap* bmp) {
(SkColorType)colorType,
b ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
if (!bmp->tryAllocPixels(info)) {
SkDebugf("Bitmap not allocated\n");
SkDEBUGF(("Bitmap not allocated\n"));
}
SkColor c;
fuzz->next(&c);
@ -103,6 +103,11 @@ static void init_surface(Fuzz* fuzz, sk_sp<SkSurface>* s) {
fuzz->nextRange(&x, 1, kMaxX);
fuzz->nextRange(&y, 1, kMaxY);
*s = SkSurface::MakeRasterN32Premul(x, y);
if (!*s) {
// Was possibly too big for the memory constrained fuzzing environments
*s = SkSurface::MakeNull(x, y);
}
}
@ -313,36 +318,36 @@ DEF_FUZZ(DrawFunctions, fuzz) {
SkDebugf("Could not initialize font.\n");
fuzz->signalBug();
}
SkDebugf("Fuzz DrawText\n");
SkDEBUGF(("Fuzz DrawText\n"));
fuzz_drawText(fuzz, f);
return;
}
case 1:
SkDebugf("Fuzz DrawRect\n");
SkDEBUGF(("Fuzz DrawRect\n"));
fuzz_drawRect(fuzz);
return;
case 2:
SkDebugf("Fuzz DrawCircle\n");
SkDEBUGF(("Fuzz DrawCircle\n"));
fuzz_drawCircle(fuzz);
return;
case 3:
SkDebugf("Fuzz DrawLine\n");
SkDEBUGF(("Fuzz DrawLine\n"));
fuzz_drawLine(fuzz);
return;
case 4:
SkDebugf("Fuzz DrawPath\n");
SkDEBUGF(("Fuzz DrawPath\n"));
fuzz_drawPath(fuzz);
return;
case 5:
SkDebugf("Fuzz DrawImage/DrawImageRect\n");
SkDEBUGF(("Fuzz DrawImage/DrawImageRect\n"));
fuzz_drawImage(fuzz);
return;
case 6:
SkDebugf("Fuzz DrawBitmap\n");
SkDEBUGF(("Fuzz DrawBitmap\n"));
fuzz_drawBitmap(fuzz);
return;
case 7:
SkDebugf("Fuzz DrawPaint\n");
SkDEBUGF(("Fuzz DrawPaint\n"));
fuzz_drawPaint(fuzz);
return;
}

View File

@ -56,11 +56,11 @@ static void logOptionalMatrix(const char* label, const SkMatrix* m) {
return;
}
SkDebugf(" %s: [ ", label);
SkDEBUGF((" %s: [ ", label));
for (int i = 0; i < 9; ++i) {
SkDebugf("%.9g ", m->get(i));
SkDEBUGF(("%.9g ", m->get(i)));
}
SkDebugf("]\n");
SkDEBUGF(("]\n"));
}
static void logLinearGradient(const SkPoint pts[2],
@ -256,19 +256,19 @@ DEF_FUZZ(Gradients, fuzz) {
switch(i) {
case 0:
SkDebugf("LinearGradient\n");
SkDEBUGF(("LinearGradient\n"));
fuzzLinearGradient(fuzz);
return;
case 1:
SkDebugf("RadialGradient\n");
SkDEBUGF(("RadialGradient\n"));
fuzzRadialGradient(fuzz);
return;
case 2:
SkDebugf("TwoPointConicalGradient\n");
SkDEBUGF(("TwoPointConicalGradient\n"));
fuzzTwoPointConicalGradient(fuzz);
return;
}
SkDebugf("SweepGradient\n");
SkDEBUGF(("SweepGradient\n"));
fuzzSweepGradient(fuzz);
return;
}