1 //===-- llvm-dwarfdump-fuzzer.cpp - Fuzz the llvm-dwarfdump tool ----------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// This file implements a function that runs llvm-dwarfdump 11 /// on a single input. This function is then linked into the Fuzzer library. 12 /// 13 //===----------------------------------------------------------------------===// 14 #include "llvm/DebugInfo/DIContext.h" 15 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 16 #include "llvm/Object/ObjectFile.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 19 using namespace llvm; 20 using namespace object; 21 LLVMFuzzerTestOneInput(uint8_t * data,size_t size)22extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) { 23 std::unique_ptr<MemoryBuffer> Buff = MemoryBuffer::getMemBuffer( 24 StringRef((const char *)data, size), "", false); 25 26 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = 27 ObjectFile::createObjectFile(Buff->getMemBufferRef()); 28 if (auto E = ObjOrErr.takeError()) { 29 consumeError(std::move(E)); 30 return 0; 31 } 32 ObjectFile &Obj = *ObjOrErr.get(); 33 std::unique_ptr<DIContext> DICtx = DWARFContext::create(Obj); 34 35 36 DIDumpOptions opts; 37 opts.DumpType = DIDT_All; 38 DICtx->dump(nulls(), opts); 39 return 0; 40 } 41