imposed_ensemble_matcher_fuzzer.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2018 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 <stdint.h>
  5. #include <iostream>
  6. #include <memory>
  7. #include "base/environment.h"
  8. #include "base/logging.h"
  9. #include "components/zucchini/buffer_sink.h"
  10. #include "components/zucchini/buffer_view.h"
  11. #include "components/zucchini/fuzzers/file_pair.pb.h"
  12. #include "components/zucchini/patch_writer.h"
  13. #include "components/zucchini/zucchini.h"
  14. #include "testing/libfuzzer/proto/lpm_interface.h"
  15. namespace {
  16. constexpr size_t kMinImageSize = 16;
  17. constexpr size_t kMaxImageSize = 1024;
  18. } // namespace
  19. struct Environment {
  20. Environment() {
  21. logging::SetMinLogLevel(logging::LOG_FATAL); // Disable console spamming.
  22. }
  23. };
  24. DEFINE_BINARY_PROTO_FUZZER(const zucchini::fuzzers::FilePair& file_pair) {
  25. static Environment env;
  26. // Dump code for debugging.
  27. if (base::Environment::Create()->HasVar("LPM_DUMP_NATIVE_INPUT")) {
  28. std::cout << "Imposed Matches: " << file_pair.imposed_matches() << std::endl
  29. << "Old File: " << file_pair.old_file() << std::endl
  30. << "New File: " << file_pair.new_or_patch_file() << std::endl;
  31. }
  32. // Prepare data.
  33. zucchini::ConstBufferView old_image(
  34. reinterpret_cast<const uint8_t*>(file_pair.old_file().data()),
  35. file_pair.old_file().size());
  36. zucchini::ConstBufferView new_image(
  37. reinterpret_cast<const uint8_t*>(file_pair.new_or_patch_file().data()),
  38. file_pair.new_or_patch_file().size());
  39. // Restrict image sizes to speed up fuzzing.
  40. if (old_image.size() < kMinImageSize || old_image.size() > kMaxImageSize ||
  41. new_image.size() < kMinImageSize || new_image.size() > kMaxImageSize) {
  42. return;
  43. }
  44. // Generate a patch writer.
  45. zucchini::EnsemblePatchWriter patch_writer(old_image, new_image);
  46. // Fuzz Target.
  47. zucchini::GenerateBufferImposed(old_image, new_image,
  48. file_pair.imposed_matches(), &patch_writer);
  49. // Write to buffer to avoid IO.
  50. size_t patch_size = patch_writer.SerializedSize();
  51. std::unique_ptr<uint8_t[]> patch_data(new uint8_t[patch_size]);
  52. zucchini::BufferSink patch(patch_data.get(), patch_size);
  53. patch_writer.SerializeInto(patch);
  54. }