Alexey Samsonov | d804a1e | 2015-05-28 18:35:18 +0000 | [diff] [blame] | 1 | //===-- llvm-dwarfdump-fuzzer.cpp - Fuzz the llvm-dwarfdump tool ----------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Alexey Samsonov | d804a1e | 2015-05-28 18:35:18 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// This file implements a function that runs llvm-dwarfdump |
Alexey Samsonov | d804a1e | 2015-05-28 18:35:18 +0000 | [diff] [blame] | 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 | |
Kostya Serebryany | dd4c61f | 2017-08-29 01:14:05 +0000 | [diff] [blame] | 22 | extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) { |
Alexey Samsonov | d804a1e | 2015-05-28 18:35:18 +0000 | [diff] [blame] | 23 | std::unique_ptr<MemoryBuffer> Buff = MemoryBuffer::getMemBuffer( |
| 24 | StringRef((const char *)data, size), "", false); |
| 25 | |
David Majnemer | 2da7664 | 2016-05-31 01:24:33 +0000 | [diff] [blame] | 26 | Expected<std::unique_ptr<ObjectFile>> ObjOrErr = |
Alexey Samsonov | d804a1e | 2015-05-28 18:35:18 +0000 | [diff] [blame] | 27 | ObjectFile::createObjectFile(Buff->getMemBufferRef()); |
David Majnemer | 2da7664 | 2016-05-31 01:24:33 +0000 | [diff] [blame] | 28 | if (auto E = ObjOrErr.takeError()) { |
| 29 | consumeError(std::move(E)); |
Kostya Serebryany | dd4c61f | 2017-08-29 01:14:05 +0000 | [diff] [blame] | 30 | return 0; |
David Majnemer | 2da7664 | 2016-05-31 01:24:33 +0000 | [diff] [blame] | 31 | } |
Alexey Samsonov | d804a1e | 2015-05-28 18:35:18 +0000 | [diff] [blame] | 32 | ObjectFile &Obj = *ObjOrErr.get(); |
Rafael Espindola | 3ee9e11 | 2017-07-19 23:34:59 +0000 | [diff] [blame] | 33 | std::unique_ptr<DIContext> DICtx = DWARFContext::create(Obj); |
George Karpenkov | 0ac90d3 | 2017-08-23 00:40:58 +0000 | [diff] [blame] | 34 | |
| 35 | |
| 36 | DIDumpOptions opts; |
| 37 | opts.DumpType = DIDT_All; |
| 38 | DICtx->dump(nulls(), opts); |
Kostya Serebryany | dd4c61f | 2017-08-29 01:14:05 +0000 | [diff] [blame] | 39 | return 0; |
Alexey Samsonov | d804a1e | 2015-05-28 18:35:18 +0000 | [diff] [blame] | 40 | } |