blob: 1d74856c0fb8a619d3b51204432bc7a833ee928a [file] [log] [blame]
Alexey Samsonovd804a1e2015-05-28 18:35:18 +00001//===-- llvm-dwarfdump-fuzzer.cpp - Fuzz the llvm-dwarfdump tool ----------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Samsonovd804a1e2015-05-28 18:35:18 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file implements a function that runs llvm-dwarfdump
Alexey Samsonovd804a1e2015-05-28 18:35:18 +000011/// 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
19using namespace llvm;
20using namespace object;
21
Kostya Serebryanydd4c61f2017-08-29 01:14:05 +000022extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
Alexey Samsonovd804a1e2015-05-28 18:35:18 +000023 std::unique_ptr<MemoryBuffer> Buff = MemoryBuffer::getMemBuffer(
24 StringRef((const char *)data, size), "", false);
25
David Majnemer2da76642016-05-31 01:24:33 +000026 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
Alexey Samsonovd804a1e2015-05-28 18:35:18 +000027 ObjectFile::createObjectFile(Buff->getMemBufferRef());
David Majnemer2da76642016-05-31 01:24:33 +000028 if (auto E = ObjOrErr.takeError()) {
29 consumeError(std::move(E));
Kostya Serebryanydd4c61f2017-08-29 01:14:05 +000030 return 0;
David Majnemer2da76642016-05-31 01:24:33 +000031 }
Alexey Samsonovd804a1e2015-05-28 18:35:18 +000032 ObjectFile &Obj = *ObjOrErr.get();
Rafael Espindola3ee9e112017-07-19 23:34:59 +000033 std::unique_ptr<DIContext> DICtx = DWARFContext::create(Obj);
George Karpenkov0ac90d32017-08-23 00:40:58 +000034
35
36 DIDumpOptions opts;
37 opts.DumpType = DIDT_All;
38 DICtx->dump(nulls(), opts);
Kostya Serebryanydd4c61f2017-08-29 01:14:05 +000039 return 0;
Alexey Samsonovd804a1e2015-05-28 18:35:18 +000040}