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