1*a03ca8b9SKrzysztof Kosiński // Copyright 2018 The Chromium Authors. All rights reserved. 2*a03ca8b9SKrzysztof Kosiński // Use of this source code is governed by a BSD-style license that can be 3*a03ca8b9SKrzysztof Kosiński // found in the LICENSE file. 4*a03ca8b9SKrzysztof Kosiński 5*a03ca8b9SKrzysztof Kosiński #include <stdint.h> 6*a03ca8b9SKrzysztof Kosiński 7*a03ca8b9SKrzysztof Kosiński #include <iostream> 8*a03ca8b9SKrzysztof Kosiński #include <memory> 9*a03ca8b9SKrzysztof Kosiński 10*a03ca8b9SKrzysztof Kosiński #include "base/environment.h" 11*a03ca8b9SKrzysztof Kosiński #include "base/logging.h" 12*a03ca8b9SKrzysztof Kosiński #include "components/zucchini/buffer_sink.h" 13*a03ca8b9SKrzysztof Kosiński #include "components/zucchini/buffer_view.h" 14*a03ca8b9SKrzysztof Kosiński #include "components/zucchini/fuzzers/file_pair.pb.h" 15*a03ca8b9SKrzysztof Kosiński #include "components/zucchini/patch_writer.h" 16*a03ca8b9SKrzysztof Kosiński #include "components/zucchini/zucchini.h" 17*a03ca8b9SKrzysztof Kosiński #include "testing/libfuzzer/proto/lpm_interface.h" 18*a03ca8b9SKrzysztof Kosiński 19*a03ca8b9SKrzysztof Kosiński namespace { 20*a03ca8b9SKrzysztof Kosiński 21*a03ca8b9SKrzysztof Kosiński constexpr size_t kMinImageSize = 16; 22*a03ca8b9SKrzysztof Kosiński constexpr size_t kMaxImageSize = 1024; 23*a03ca8b9SKrzysztof Kosiński 24*a03ca8b9SKrzysztof Kosiński } // namespace 25*a03ca8b9SKrzysztof Kosiński 26*a03ca8b9SKrzysztof Kosiński struct Environment { EnvironmentEnvironment27*a03ca8b9SKrzysztof Kosiński Environment() { 28*a03ca8b9SKrzysztof Kosiński logging::SetMinLogLevel(logging::LOG_FATAL); // Disable console spamming. 29*a03ca8b9SKrzysztof Kosiński } 30*a03ca8b9SKrzysztof Kosiński }; 31*a03ca8b9SKrzysztof Kosiński DEFINE_BINARY_PROTO_FUZZER(const zucchini::fuzzers::FilePair & file_pair)32*a03ca8b9SKrzysztof KosińskiDEFINE_BINARY_PROTO_FUZZER(const zucchini::fuzzers::FilePair& file_pair) { 33*a03ca8b9SKrzysztof Kosiński static Environment env; 34*a03ca8b9SKrzysztof Kosiński // Dump code for debugging. 35*a03ca8b9SKrzysztof Kosiński if (base::Environment::Create()->HasVar("LPM_DUMP_NATIVE_INPUT")) { 36*a03ca8b9SKrzysztof Kosiński std::cout << "Imposed Matches: " << file_pair.imposed_matches() << std::endl 37*a03ca8b9SKrzysztof Kosiński << "Old File: " << file_pair.old_file() << std::endl 38*a03ca8b9SKrzysztof Kosiński << "New File: " << file_pair.new_or_patch_file() << std::endl; 39*a03ca8b9SKrzysztof Kosiński } 40*a03ca8b9SKrzysztof Kosiński 41*a03ca8b9SKrzysztof Kosiński // Prepare data. 42*a03ca8b9SKrzysztof Kosiński zucchini::ConstBufferView old_image( 43*a03ca8b9SKrzysztof Kosiński reinterpret_cast<const uint8_t*>(file_pair.old_file().data()), 44*a03ca8b9SKrzysztof Kosiński file_pair.old_file().size()); 45*a03ca8b9SKrzysztof Kosiński zucchini::ConstBufferView new_image( 46*a03ca8b9SKrzysztof Kosiński reinterpret_cast<const uint8_t*>(file_pair.new_or_patch_file().data()), 47*a03ca8b9SKrzysztof Kosiński file_pair.new_or_patch_file().size()); 48*a03ca8b9SKrzysztof Kosiński 49*a03ca8b9SKrzysztof Kosiński // Restrict image sizes to speed up fuzzing. 50*a03ca8b9SKrzysztof Kosiński if (old_image.size() < kMinImageSize || old_image.size() > kMaxImageSize || 51*a03ca8b9SKrzysztof Kosiński new_image.size() < kMinImageSize || new_image.size() > kMaxImageSize) { 52*a03ca8b9SKrzysztof Kosiński return; 53*a03ca8b9SKrzysztof Kosiński } 54*a03ca8b9SKrzysztof Kosiński 55*a03ca8b9SKrzysztof Kosiński // Generate a patch writer. 56*a03ca8b9SKrzysztof Kosiński zucchini::EnsemblePatchWriter patch_writer(old_image, new_image); 57*a03ca8b9SKrzysztof Kosiński 58*a03ca8b9SKrzysztof Kosiński // Fuzz Target. 59*a03ca8b9SKrzysztof Kosiński zucchini::GenerateBufferImposed(old_image, new_image, 60*a03ca8b9SKrzysztof Kosiński file_pair.imposed_matches(), &patch_writer); 61*a03ca8b9SKrzysztof Kosiński 62*a03ca8b9SKrzysztof Kosiński // Write to buffer to avoid IO. 63*a03ca8b9SKrzysztof Kosiński size_t patch_size = patch_writer.SerializedSize(); 64*a03ca8b9SKrzysztof Kosiński std::unique_ptr<uint8_t[]> patch_data(new uint8_t[patch_size]); 65*a03ca8b9SKrzysztof Kosiński zucchini::BufferSink patch(patch_data.get(), patch_size); 66*a03ca8b9SKrzysztof Kosiński patch_writer.SerializeInto(patch); 67*a03ca8b9SKrzysztof Kosiński } 68