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 <stddef.h> 6 #include <stdint.h> 7 8 #include "base/logging.h" 9 #include "components/zucchini/buffer_view.h" 10 #include "components/zucchini/disassembler.h" 11 #include "components/zucchini/disassembler_dex.h" 12 13 namespace { 14 15 struct Environment { Environment__anon0ebb0f010111::Environment16 Environment() { logging::SetMinLogLevel(logging::LOG_FATAL); } 17 }; 18 19 } // namespace 20 LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)21extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { 22 static Environment env; 23 if (!size) 24 return 0; 25 // Prepare data. 26 std::vector<uint8_t> mutable_data(data, data + size); 27 zucchini::ConstBufferView image(mutable_data.data(), mutable_data.size()); 28 29 // Create disassembler. Early exit on failure. 30 auto disassembler_dex = 31 zucchini::Disassembler::Make<zucchini::DisassemblerDex>(image); 32 if (!disassembler_dex) 33 return 0; 34 CHECK_LE(disassembler_dex->size(), image.size()); 35 zucchini::MutableBufferView mutable_image(mutable_data.data(), 36 disassembler_dex->size()); 37 38 std::vector<zucchini::Reference> references; 39 // Read all references in the file. 40 auto groups = disassembler_dex->MakeReferenceGroups(); 41 for (const auto& group : groups) { 42 auto reader = group.GetReader(disassembler_dex.get()); 43 for (auto ref = reader->GetNext(); ref.has_value(); 44 ref = reader->GetNext()) { 45 references.push_back(ref.value()); 46 } 47 reader.reset(); 48 auto writer = group.GetWriter(mutable_image, disassembler_dex.get()); 49 for (const auto& ref : references) 50 writer->PutNext(ref); 51 references.clear(); 52 } 53 return 0; 54 } 55