apply_fuzzer.cc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 <stdlib.h>
  6. #include <iostream>
  7. #include <vector>
  8. #include "base/environment.h"
  9. #include "base/logging.h"
  10. #include "components/zucchini/buffer_view.h"
  11. #include "components/zucchini/fuzzers/file_pair.pb.h"
  12. #include "components/zucchini/patch_reader.h"
  13. #include "components/zucchini/zucchini.h"
  14. #include "testing/libfuzzer/proto/lpm_interface.h"
  15. struct Environment {
  16. Environment() {
  17. logging::SetMinLogLevel(logging::LOG_FATAL); // Disable console spamming.
  18. }
  19. };
  20. Environment* env = new Environment();
  21. DEFINE_BINARY_PROTO_FUZZER(const zucchini::fuzzers::FilePair& file_pair) {
  22. // Dump code for debugging.
  23. if (base::Environment::Create()->HasVar("LPM_DUMP_NATIVE_INPUT")) {
  24. std::cout << "Old File: " << file_pair.old_file() << std::endl
  25. << "Patch File: " << file_pair.new_or_patch_file() << std::endl;
  26. }
  27. // Prepare data.
  28. zucchini::ConstBufferView old_image(
  29. reinterpret_cast<const uint8_t*>(file_pair.old_file().data()),
  30. file_pair.old_file().size());
  31. zucchini::ConstBufferView patch_file(
  32. reinterpret_cast<const uint8_t*>(file_pair.new_or_patch_file().data()),
  33. file_pair.new_or_patch_file().size());
  34. // Generate a patch reader.
  35. auto patch_reader = zucchini::EnsemblePatchReader::Create(patch_file);
  36. // Abort if the patch can't be read.
  37. if (!patch_reader.has_value())
  38. return;
  39. // Create the underlying new file.
  40. size_t new_size = patch_reader->header().new_size;
  41. // Reject unreasonably large "new" files that fuzzed patch may specify.
  42. if (new_size > 64 * 1024)
  43. return;
  44. std::vector<uint8_t> new_data(new_size);
  45. zucchini::MutableBufferView new_image(new_data.data(), new_size);
  46. // Fuzz target.
  47. zucchini::ApplyBuffer(old_image, *patch_reader, new_image);
  48. // No need to check whether output exist, or if so, whether it's valid.
  49. }