Flutter Engine
The Flutter Engine
chrome_fuzz.cpp
Go to the documentation of this file.
1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6#include "SkFlattenableSerialization.h"
9#include "src/core/SkOSFile.h"
10
11#include <stdio.h>
12
13static const int kBitmapSize = 24;
14
15static bool read_test_case(const char* filename, SkString* testdata) {
16 FILE* file = sk_fopen(filename, kRead_SkFILE_Flag);
17 if (!file) {
18 SkDebugf("couldn't open file %s\n", filename);
19 return false;
20 }
21 size_t len = sk_fgetsize(file);
22 if (!len) {
23 SkDebugf("couldn't read file %s\n", filename);
24 return false;
25 }
26 testdata->resize(len);
27 (void) fread(testdata->data(), len, file);
28 return true;
29}
30
31static void run_test_case(const SkString& testdata, const SkBitmap& bitmap,
32 SkCanvas* canvas) {
33 // This call shouldn't crash or cause ASAN to flag any memory issues
34 // If nothing bad happens within this call, everything is fine
35 sk_sp<SkImageFilter> flattenable = SkValidatingDeserializeImageFilter(testdata.c_str(),
36 testdata.size());
37
38 // Adding some info, but the test passed if we got here without any trouble
39 if (flattenable != nullptr) {
40 SkDebugf("Valid stream detected.\n");
41 // Let's see if using the filters can cause any trouble...
43 paint.setImageFilter(flattenable);
44 canvas->save();
47
48 // This call shouldn't crash or cause ASAN to flag any memory issues
49 // If nothing bad happens within this call, everything is fine
50 canvas->drawBitmap(bitmap, 0, 0, &paint);
51
52 SkDebugf("Filter DAG rendered successfully.\n");
53 canvas->restore();
54 } else {
55 SkDebugf("Invalid stream detected.\n");
56 }
57}
58
59static bool read_and_run_test_case(const char* filename, const SkBitmap& bitmap,
60 SkCanvas* canvas) {
61 SkString testdata;
62 SkDebugf("Test case: %s\n", filename);
63 // read_test_case will print a useful error message if it fails.
64 if (!read_test_case(filename, &testdata))
65 return false;
66 run_test_case(testdata, bitmap, canvas);
67 return true;
68}
69
70int main(int argc, char** argv) {
71 int ret = 0;
73 bitmap.allocN32Pixels(kBitmapSize, kBitmapSize);
74 SkCanvas canvas(bitmap);
75 canvas.clear(0x00000000);
76 for (int i = 1; i < argc; i++)
77 if (!read_and_run_test_case(argv[i], bitmap, &canvas))
78 ret = 2;
79 // Cluster-Fuzz likes "#EOF" as the last line of output to help distinguish
80 // successful runs from crashes.
81 SkDebugf("#EOF\n");
82 return ret;
83}
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
FILE * sk_fopen(const char path[], SkFILE_Flags)
@ kRead_SkFILE_Flag
Definition: SkOSFile.h:20
size_t sk_fgetsize(FILE *)
#define SkIntToScalar(x)
Definition: SkScalar.h:57
static bool read_test_case(const char *filename, SkString *testdata)
Definition: chrome_fuzz.cpp:15
int main(int argc, char **argv)
Definition: chrome_fuzz.cpp:70
static bool read_and_run_test_case(const char *filename, const SkBitmap &bitmap, SkCanvas *canvas)
Definition: chrome_fuzz.cpp:59
static void run_test_case(const SkString &testdata, const SkBitmap &bitmap, SkCanvas *canvas)
Definition: chrome_fuzz.cpp:31
static const int kBitmapSize
Definition: chrome_fuzz.cpp:13
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
void restore()
Definition: SkCanvas.cpp:461
void clear(SkColor color)
Definition: SkCanvas.h:1199
int save()
Definition: SkCanvas.cpp:447
size_t size() const
Definition: SkString.h:131
const char * data() const
Definition: SkString.h:132
void resize(size_t len)
Definition: SkString.cpp:374
const char * c_str() const
Definition: SkString.h:133
const Paint & paint
Definition: color_source.cc:38
char ** argv
Definition: library.h:9
Definition: bitmap.py:1
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659