filter_fuzz_stub.cc 2.95 KB
Newer Older
1 2 3 4
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "base/files/file_util.h"
6
#include "base/logging.h"
7
#include "base/process/memory.h"
8
#include "base/test/test_discardable_memory_allocator.h"
fmalita's avatar
fmalita committed
9
#include "third_party/skia/include/core/SkBitmap.h"
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkFlattenableSerialization.h"
#include "third_party/skia/include/core/SkImageFilter.h"

namespace {

static const int BitmapSize = 24;

bool ReadTestCase(const char* filename, std::string* ipc_filter_message) {
  base::FilePath filepath = base::FilePath::FromUTF8Unsafe(filename);

  if (!base::ReadFileToString(filepath, ipc_filter_message)) {
    LOG(ERROR) << filename << ": couldn't read file.";
    return false;
  }

  return true;
}

void RunTestCase(std::string& ipc_filter_message, SkBitmap& bitmap,
                 SkCanvas* canvas) {
  // This call shouldn't crash or cause ASAN to flag any memory issues
  // If nothing bad happens within this call, everything is fine
reed's avatar
reed committed
33 34
  sk_sp<SkImageFilter> flattenable = SkValidatingDeserializeImageFilter(
      ipc_filter_message.c_str(), ipc_filter_message.size());
35 36 37 38 39 40

  // Adding some info, but the test passed if we got here without any trouble
  if (flattenable != NULL) {
    LOG(INFO) << "Valid stream detected.";
    // Let's see if using the filters can cause any trouble...
    SkPaint paint;
reed's avatar
reed committed
41
    paint.setImageFilter(flattenable);
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    canvas->save();
    canvas->clipRect(SkRect::MakeXYWH(
        0, 0, SkIntToScalar(BitmapSize), SkIntToScalar(BitmapSize)));

    // This call shouldn't crash or cause ASAN to flag any memory issues
    // If nothing bad happens within this call, everything is fine
    canvas->drawBitmap(bitmap, 0, 0, &paint);

    LOG(INFO) << "Filter DAG rendered successfully";
    canvas->restore();
  } else {
    LOG(INFO) << "Invalid stream detected.";
  }
}

bool ReadAndRunTestCase(const char* filename, SkBitmap& bitmap,
                        SkCanvas* canvas) {
  std::string ipc_filter_message;

  LOG(INFO) << "Test case: " << filename;

  // ReadTestCase will print a useful error message if it fails.
  if (!ReadTestCase(filename, &ipc_filter_message))
    return false;

  RunTestCase(ipc_filter_message, bitmap, canvas);

  return true;
}

}

int main(int argc, char** argv) {
  int ret = 0;

77
  base::EnableTerminationOnOutOfMemory();
78 79 80
  base::TestDiscardableMemoryAllocator discardable_memory_allocator;
  base::DiscardableMemoryAllocator::SetInstance(&discardable_memory_allocator);

81
  SkBitmap bitmap;
82 83
  bitmap.allocN32Pixels(BitmapSize, BitmapSize);
  SkCanvas canvas(bitmap);
84 85 86 87 88 89 90 91 92 93 94 95 96
  canvas.clear(0x00000000);

  for (int i = 1; i < argc; i++)
    if (!ReadAndRunTestCase(argv[i], bitmap, &canvas))
      ret = 2;

  // Cluster-Fuzz likes "#EOF" as the last line of output to help distinguish
  // successful runs from crashes.
  printf("#EOF\n");

  return ret;
}