fuzz: dump_canvas
authorHal Canary <halcanary@google.com>
Mon, 27 Feb 2017 21:42:03 +0000 (16:42 -0500)
committerSkia Commit-Bot <skia-commit-bot@chromium.org>
Wed, 1 Mar 2017 15:16:06 +0000 (15:16 +0000)
For example: `fuzz --type _dump_canvas -b fuzz_file`

Note well: this command-line usage is subject to change.

Change-Id: Ida7368a699bafe2395604d8428a2d50cb0eff6aa
Reviewed-on: https://skia-review.googlesource.com/9025
Reviewed-by: Kevin Lubick <kjlubick@google.com>
Reviewed-by: Herb Derby <herb@google.com>
Commit-Queue: Hal Canary <halcanary@google.com>

fuzz/fuzz.cpp

index 9c59a1b..12f11e5 100644 (file)
 #include "SkCodec.h"
 #include "SkCommandLineFlags.h"
 #include "SkData.h"
+#include "SkDebugCanvas.h"
 #include "SkDocument.h"
 #include "SkImage.h"
 #include "SkImageEncoder.h"
 #include "SkMallocPixelRef.h"
 #include "SkNullCanvas.h"
-#include "SkPath.h"
-#include "SkRegion.h"
-#include "SkSurface.h"
 #include "SkOSFile.h"
 #include "SkOSPath.h"
+#include "SkPath.h"
 #include "SkPicture.h"
+#include "SkRegion.h"
+#include "SkStream.h"
+#include "SkSurface.h"
+
 #if SK_SUPPORT_GPU
 #include "SkSLCompiler.h"
 #endif
-#include "SkStream.h"
 
+#include <iostream>
 #include <signal.h>
 
 #include "sk_tool_utils.h"
 
 #include "FuzzCanvas.h"
 
-DEFINE_string2(bytes, b, "", "A path to a file or a directory. If a file, the contents will be used as the fuzz bytes. If a directory, all files in the directory will be used as fuzz bytes for the fuzzer, one at a time.");
+DEFINE_string2(bytes, b, "", "A path to a file or a directory. If a file, the "
+        "contents will be used as the fuzz bytes. If a directory, all files "
+        "in the directory will be used as fuzz bytes for the fuzzer, one at a "
+        "time.");
 DEFINE_string2(name, n, "", "If --type is 'api', fuzz the API with this name.");
 
-DEFINE_string2(type, t, "api", "How to interpret --bytes, either 'image_scale', 'image_mode', 'skp', 'icc', or 'api'.");
-DEFINE_string2(dump, d, "", "If not empty, dump 'image*' or 'skp' types as a PNG with this name.");
+DEFINE_string2(type, t, "api", "How to interpret --bytes, either 'image_scale'"
+        ", 'image_mode', 'skp', 'icc', or 'api'.");
+DEFINE_string2(dump, d, "", "If not empty, dump 'image*' or 'skp' types as a "
+        "PNG with this name.");
 
 static int printUsage() {
     SkDebugf("Usage: fuzz -t <type> -b <path/to/file> [-n api-to-fuzz]\n");
@@ -53,6 +61,7 @@ static void fuzz_path_deserialize(sk_sp<SkData>);
 static void fuzz_region_deserialize(sk_sp<SkData>);
 static void fuzz_pdf_canvas(sk_sp<SkData>);
 static void fuzz_null_canvas(sk_sp<SkData>);
+static void fuzz_dump_canvas(sk_sp<SkData>);
 static void fuzz_raster_n32_canvas(sk_sp<SkData>);
 static void fuzz_skp(sk_sp<SkData>);
 #if SK_SUPPORT_GPU
@@ -130,6 +139,11 @@ static int fuzz_file(const char* path) {
             fuzz_raster_n32_canvas(bytes);
             return 0;
         }
+        // not a "real" thing to fuzz, used to debug errors found while fuzzing.
+        if (0 == strcmp("_dump_canvas", FLAGS_type[0])) {
+            fuzz_dump_canvas(bytes);
+            return 0;
+        }
         if (0 == strcmp("null_canvas", FLAGS_type[0])) {
             fuzz_null_canvas(bytes);
             return 0;
@@ -478,6 +492,16 @@ static void fuzz_pdf_canvas(sk_sp<SkData> bytes) {
     FuzzCanvas(&fuzz, doc->beginPage(612.0f, 792.0f));
 }
 
+static void fuzz_dump_canvas(sk_sp<SkData> bytes) {
+    Fuzz fuzz(std::move(bytes));
+    SkDebugCanvas debugCanvas(612, 792);
+    FuzzCanvas(&fuzz, &debugCanvas);
+    std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
+    UrlDataManager dataManager(SkString("data"));
+    Json::Value json = debugCanvas.toJSON(dataManager, debugCanvas.getSize(), nullCanvas.get());
+    Json::StyledStreamWriter("  ").write(std::cout, json);
+}
+
 static void fuzz_skp(sk_sp<SkData> bytes) {
     SkMemoryStream stream(bytes);
     SkDebugf("Decoding\n");