caryclark | 7da2e55 | 2015-05-12 08:36:48 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 5 | #include "include/core/SkCanvas.h" |
caryclark | 7da2e55 | 2015-05-12 08:36:48 -0700 | [diff] [blame] | 6 | #include "SkFlattenableSerialization.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 7 | #include "include/core/SkImageFilter.h" |
| 8 | #include "include/core/SkString.h" |
| 9 | #include "src/core/SkOSFile.h" |
caryclark | 7da2e55 | 2015-05-12 08:36:48 -0700 | [diff] [blame] | 10 | |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | |
caryclark | 7da2e55 | 2015-05-12 08:36:48 -0700 | [diff] [blame] | 13 | static const int kBitmapSize = 24; |
| 14 | |
| 15 | static bool read_test_case(const char* filename, SkString* testdata) { |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 16 | FILE* file = sk_fopen(filename, kRead_SkFILE_Flag); |
caryclark | 7da2e55 | 2015-05-12 08:36:48 -0700 | [diff] [blame] | 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); |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 27 | (void) fread(testdata->writable_str(), len, file); |
caryclark | 7da2e55 | 2015-05-12 08:36:48 -0700 | [diff] [blame] | 28 | return true; |
| 29 | } |
| 30 | |
| 31 | static void run_test_case(const SkString& testdata, const SkBitmap& bitmap, |
Mike Reed | 5e25717 | 2016-11-01 11:22:05 -0400 | [diff] [blame] | 32 | SkCanvas* canvas) { |
caryclark | 7da2e55 | 2015-05-12 08:36:48 -0700 | [diff] [blame] | 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 |
Mike Reed | 5e25717 | 2016-11-01 11:22:05 -0400 | [diff] [blame] | 35 | sk_sp<SkImageFilter> flattenable = SkValidatingDeserializeImageFilter(testdata.c_str(), |
| 36 | testdata.size()); |
caryclark | 7da2e55 | 2015-05-12 08:36:48 -0700 | [diff] [blame] | 37 | |
| 38 | // Adding some info, but the test passed if we got here without any trouble |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 39 | if (flattenable != nullptr) { |
caryclark | 7da2e55 | 2015-05-12 08:36:48 -0700 | [diff] [blame] | 40 | SkDebugf("Valid stream detected.\n"); |
| 41 | // Let's see if using the filters can cause any trouble... |
| 42 | SkPaint paint; |
Mike Reed | 5e25717 | 2016-11-01 11:22:05 -0400 | [diff] [blame] | 43 | paint.setImageFilter(flattenable); |
caryclark | 7da2e55 | 2015-05-12 08:36:48 -0700 | [diff] [blame] | 44 | canvas->save(); |
| 45 | canvas->clipRect(SkRect::MakeXYWH( |
| 46 | 0, 0, SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize))); |
| 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 | |
| 59 | static 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 | |
| 70 | int main(int argc, char** argv) { |
| 71 | int ret = 0; |
| 72 | SkBitmap bitmap; |
| 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 | } |