Alexey Samsonov | d804a1e | 2015-05-28 18:35:18 +0000 | [diff] [blame] | 1 | //===-- llvm-dwarfdump-fuzzer.cpp - Fuzz the llvm-dwarfdump tool ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file implements a function that runs llvm-dwarfdump |
| 12 | /// on a single input. This function is then linked into the Fuzzer library. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | #include "llvm/DebugInfo/DIContext.h" |
| 16 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 17 | #include "llvm/Object/ObjectFile.h" |
| 18 | #include "llvm/Support/MemoryBuffer.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | using namespace object; |
| 22 | |
Kostya Serebryany | dd4c61f | 2017-08-29 01:14:05 +0000 | [diff] [blame^] | 23 | extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) { |
Alexey Samsonov | d804a1e | 2015-05-28 18:35:18 +0000 | [diff] [blame] | 24 | std::unique_ptr<MemoryBuffer> Buff = MemoryBuffer::getMemBuffer( |
| 25 | StringRef((const char *)data, size), "", false); |
| 26 | |
David Majnemer | 2da7664 | 2016-05-31 01:24:33 +0000 | [diff] [blame] | 27 | Expected<std::unique_ptr<ObjectFile>> ObjOrErr = |
Alexey Samsonov | d804a1e | 2015-05-28 18:35:18 +0000 | [diff] [blame] | 28 | ObjectFile::createObjectFile(Buff->getMemBufferRef()); |
David Majnemer | 2da7664 | 2016-05-31 01:24:33 +0000 | [diff] [blame] | 29 | if (auto E = ObjOrErr.takeError()) { |
| 30 | consumeError(std::move(E)); |
Kostya Serebryany | dd4c61f | 2017-08-29 01:14:05 +0000 | [diff] [blame^] | 31 | return 0; |
David Majnemer | 2da7664 | 2016-05-31 01:24:33 +0000 | [diff] [blame] | 32 | } |
Alexey Samsonov | d804a1e | 2015-05-28 18:35:18 +0000 | [diff] [blame] | 33 | ObjectFile &Obj = *ObjOrErr.get(); |
Rafael Espindola | 3ee9e11 | 2017-07-19 23:34:59 +0000 | [diff] [blame] | 34 | std::unique_ptr<DIContext> DICtx = DWARFContext::create(Obj); |
George Karpenkov | 0ac90d3 | 2017-08-23 00:40:58 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | DIDumpOptions opts; |
| 38 | opts.DumpType = DIDT_All; |
| 39 | DICtx->dump(nulls(), opts); |
Kostya Serebryany | dd4c61f | 2017-08-29 01:14:05 +0000 | [diff] [blame^] | 40 | return 0; |
Alexey Samsonov | d804a1e | 2015-05-28 18:35:18 +0000 | [diff] [blame] | 41 | } |