chrome_fuzz.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 "include/core/SkCanvas.h"
  5. #include "SkFlattenableSerialization.h"
  6. #include "include/core/SkImageFilter.h"
  7. #include "include/core/SkString.h"
  8. #include "src/core/SkOSFile.h"
  9. #include <stdio.h>
  10. static const int kBitmapSize = 24;
  11. static bool read_test_case(const char* filename, SkString* testdata) {
  12. FILE* file = sk_fopen(filename, kRead_SkFILE_Flag);
  13. if (!file) {
  14. SkDebugf("couldn't open file %s\n", filename);
  15. return false;
  16. }
  17. size_t len = sk_fgetsize(file);
  18. if (!len) {
  19. SkDebugf("couldn't read file %s\n", filename);
  20. return false;
  21. }
  22. testdata->resize(len);
  23. (void) fread(testdata->writable_str(), len, file);
  24. return true;
  25. }
  26. static void run_test_case(const SkString& testdata, const SkBitmap& bitmap,
  27. SkCanvas* canvas) {
  28. // This call shouldn't crash or cause ASAN to flag any memory issues
  29. // If nothing bad happens within this call, everything is fine
  30. sk_sp<SkImageFilter> flattenable = SkValidatingDeserializeImageFilter(testdata.c_str(),
  31. testdata.size());
  32. // Adding some info, but the test passed if we got here without any trouble
  33. if (flattenable != nullptr) {
  34. SkDebugf("Valid stream detected.\n");
  35. // Let's see if using the filters can cause any trouble...
  36. SkPaint paint;
  37. paint.setImageFilter(flattenable);
  38. canvas->save();
  39. canvas->clipRect(SkRect::MakeXYWH(
  40. 0, 0, SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize)));
  41. // This call shouldn't crash or cause ASAN to flag any memory issues
  42. // If nothing bad happens within this call, everything is fine
  43. canvas->drawBitmap(bitmap, 0, 0, &paint);
  44. SkDebugf("Filter DAG rendered successfully.\n");
  45. canvas->restore();
  46. } else {
  47. SkDebugf("Invalid stream detected.\n");
  48. }
  49. }
  50. static bool read_and_run_test_case(const char* filename, const SkBitmap& bitmap,
  51. SkCanvas* canvas) {
  52. SkString testdata;
  53. SkDebugf("Test case: %s\n", filename);
  54. // read_test_case will print a useful error message if it fails.
  55. if (!read_test_case(filename, &testdata))
  56. return false;
  57. run_test_case(testdata, bitmap, canvas);
  58. return true;
  59. }
  60. int main(int argc, char** argv) {
  61. int ret = 0;
  62. SkBitmap bitmap;
  63. bitmap.allocN32Pixels(kBitmapSize, kBitmapSize);
  64. SkCanvas canvas(bitmap);
  65. canvas.clear(0x00000000);
  66. for (int i = 1; i < argc; i++)
  67. if (!read_and_run_test_case(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. SkDebugf("#EOF\n");
  72. return ret;
  73. }