filter_fuzz_stub.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #include "base/files/file_util.h"
  5. #include "base/logging.h"
  6. #include "base/process/memory.h"
  7. #include "base/test/test_discardable_memory_allocator.h"
  8. #include "third_party/skia/include/core/SkBitmap.h"
  9. #include "third_party/skia/include/core/SkCanvas.h"
  10. #include "third_party/skia/include/core/SkImage.h"
  11. #include "third_party/skia/include/core/SkImageFilter.h"
  12. namespace {
  13. static const int BitmapSize = 24;
  14. bool ReadTestCase(const char* filename, std::string* ipc_filter_message) {
  15. base::FilePath filepath = base::FilePath::FromUTF8Unsafe(filename);
  16. if (!base::ReadFileToString(filepath, ipc_filter_message)) {
  17. LOG(ERROR) << filename << ": couldn't read file.";
  18. return false;
  19. }
  20. return true;
  21. }
  22. void RunTestCase(std::string& ipc_filter_message, SkBitmap& bitmap,
  23. SkCanvas* canvas) {
  24. // This call shouldn't crash or cause ASAN to flag any memory issues
  25. // If nothing bad happens within this call, everything is fine
  26. sk_sp<SkImageFilter> flattenable = SkImageFilter::Deserialize(
  27. ipc_filter_message.c_str(), ipc_filter_message.size());
  28. // Adding some info, but the test passed if we got here without any trouble
  29. if (flattenable != NULL) {
  30. LOG(INFO) << "Valid stream detected.";
  31. // Let's see if using the filters can cause any trouble...
  32. SkPaint paint;
  33. paint.setImageFilter(flattenable);
  34. canvas->save();
  35. canvas->clipRect(SkRect::MakeXYWH(
  36. 0, 0, SkIntToScalar(BitmapSize), SkIntToScalar(BitmapSize)));
  37. // This call shouldn't crash or cause ASAN to flag any memory issues
  38. // If nothing bad happens within this call, everything is fine
  39. canvas->drawImage(bitmap.asImage(), 0, 0, SkSamplingOptions(), &paint);
  40. LOG(INFO) << "Filter DAG rendered successfully";
  41. canvas->restore();
  42. } else {
  43. LOG(INFO) << "Invalid stream detected.";
  44. }
  45. }
  46. bool ReadAndRunTestCase(const char* filename, SkBitmap& bitmap,
  47. SkCanvas* canvas) {
  48. std::string ipc_filter_message;
  49. LOG(INFO) << "Test case: " << filename;
  50. // ReadTestCase will print a useful error message if it fails.
  51. if (!ReadTestCase(filename, &ipc_filter_message))
  52. return false;
  53. RunTestCase(ipc_filter_message, bitmap, canvas);
  54. return true;
  55. }
  56. }
  57. int main(int argc, char** argv) {
  58. int ret = 0;
  59. base::EnableTerminationOnOutOfMemory();
  60. base::TestDiscardableMemoryAllocator discardable_memory_allocator;
  61. base::DiscardableMemoryAllocator::SetInstance(&discardable_memory_allocator);
  62. SkBitmap bitmap;
  63. bitmap.allocN32Pixels(BitmapSize, BitmapSize);
  64. SkCanvas canvas(bitmap, SkSurfaceProps{});
  65. canvas.clear(0x00000000);
  66. for (int i = 1; i < argc; i++)
  67. if (!ReadAndRunTestCase(argv[i], bitmap, &canvas))
  68. ret = 2;
  69. // Cluster-Fuzz likes "#EOF" as the last line of output to help distinguish
  70. // successful runs from crashes.
  71. printf("#EOF\n");
  72. return ret;
  73. }