raw_gen_fuzzer.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_gen.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. Environment* env = new Environment();
  25. DEFINE_BINARY_PROTO_FUZZER(const zucchini::fuzzers::FilePair& file_pair) {
  26. // Dump code for debugging.
  27. if (base::Environment::Create()->HasVar("LPM_DUMP_NATIVE_INPUT")) {
  28. std::cout << "Old File: " << file_pair.old_file() << std::endl
  29. << "New File: " << file_pair.new_or_patch_file() << std::endl;
  30. }
  31. // Prepare data.
  32. zucchini::ConstBufferView old_image(
  33. reinterpret_cast<const uint8_t*>(file_pair.old_file().data()),
  34. file_pair.old_file().size());
  35. zucchini::ConstBufferView new_image(
  36. reinterpret_cast<const uint8_t*>(file_pair.new_or_patch_file().data()),
  37. file_pair.new_or_patch_file().size());
  38. // Restrict image sizes to speed up fuzzing.
  39. if (old_image.size() < kMinImageSize || old_image.size() > kMaxImageSize ||
  40. new_image.size() < kMinImageSize || new_image.size() > kMaxImageSize) {
  41. return;
  42. }
  43. // Generate a patch writer.
  44. zucchini::EnsemblePatchWriter patch_writer(old_image, new_image);
  45. // Fuzz Target.
  46. zucchini::GenerateBufferRaw(old_image, new_image, &patch_writer);
  47. // Check that the patch size is sane. Crash the fuzzer if this isn't the case
  48. // as it is a failure in Zucchini's patch performance that is worth
  49. // investigating.
  50. size_t patch_size = patch_writer.SerializedSize();
  51. CHECK_LE(patch_size, kMaxImageSize * 2);
  52. // Write to buffer to avoid IO.
  53. std::unique_ptr<uint8_t[]> patch_data(new uint8_t[patch_size]);
  54. zucchini::BufferSink patch(patch_data.get(), patch_size);
  55. patch_writer.SerializeInto(patch);
  56. }