color_transform_fuzzer.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) 2016 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 <stddef.h>
  5. #include <stdint.h>
  6. #include <random>
  7. #include "base/at_exit.h"
  8. #include "base/logging.h"
  9. #include "ui/gfx/color_space.h"
  10. #include "ui/gfx/color_transform.h"
  11. #include "ui/gfx/icc_profile.h"
  12. static constexpr size_t kPixels = 256;
  13. static gfx::ColorTransform::TriStim pixels[kPixels];
  14. static void GeneratePixels(size_t hash) {
  15. static std::uniform_real_distribution<float> uniform(-0.1f, 1.1f);
  16. std::mt19937_64 random(hash);
  17. for (size_t i = 0; i < kPixels; ++i)
  18. pixels[i].SetPoint(uniform(random), uniform(random), uniform(random));
  19. }
  20. static gfx::ColorSpace test;
  21. static gfx::ColorSpace srgb;
  22. static void ColorTransform(size_t hash) {
  23. const gfx::ColorTransform::Options options;
  24. std::unique_ptr<gfx::ColorTransform> transform;
  25. if (hash & 2) {
  26. transform = gfx::ColorTransform::NewColorTransform(test, srgb, options);
  27. } else {
  28. transform = gfx::ColorTransform::NewColorTransform(srgb, test, options);
  29. }
  30. transform->Transform(pixels, kPixels);
  31. }
  32. static gfx::ColorSpace CreateRGBColorSpace(size_t hash) {
  33. auto primaries = static_cast<gfx::ColorSpace::PrimaryID>(
  34. 1 + ((hash >> 0) % (size_t)gfx::ColorSpace::PrimaryID::kMaxValue));
  35. auto transfer = static_cast<gfx::ColorSpace::TransferID>(
  36. 1 + ((hash >> 8) % (size_t)gfx::ColorSpace::TransferID::kMaxValue));
  37. auto matrix = static_cast<gfx::ColorSpace::MatrixID>(
  38. 1 + ((hash >> 16) % (size_t)gfx::ColorSpace::MatrixID::kMaxValue));
  39. auto range = static_cast<gfx::ColorSpace::RangeID>(
  40. 1 + ((hash >> 24) % (size_t)gfx::ColorSpace::RangeID::kMaxValue));
  41. return gfx::ColorSpace(primaries, transfer, matrix, range);
  42. }
  43. inline size_t Hash(const char* data, size_t size, size_t hash = ~0) {
  44. for (size_t i = 0; i < size; ++i)
  45. hash = hash * 131 + *data++;
  46. return hash;
  47. }
  48. struct Environment {
  49. Environment() { logging::SetMinLogLevel(logging::LOG_FATAL); }
  50. };
  51. Environment* environment = new Environment();
  52. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  53. base::AtExitManager at_exit;
  54. constexpr size_t kSizeLimit = 4 * 1024 * 1024;
  55. if (size < 128 || size > kSizeLimit)
  56. return 0;
  57. gfx::ICCProfile profile =
  58. gfx::ICCProfile::FromData(reinterpret_cast<const char*>(data), size);
  59. if (!profile.GetColorSpace().IsValid())
  60. return 0;
  61. test = profile.GetColorSpace();
  62. const size_t hash = Hash(reinterpret_cast<const char*>(data), size);
  63. srgb = CreateRGBColorSpace(hash);
  64. GeneratePixels(hash);
  65. ColorTransform(hash);
  66. return 0;
  67. }