1 //===-- llvm-dwarfdump-fuzzer.cpp - Fuzz the llvm-dwarfdump tool ----------===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file implements a function that runs llvm-dwarfdump
11 /// on a single input. This function is then linked into the Fuzzer library.
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"
20 using namespace object
;
22 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data
, size_t size
) {
23 std::unique_ptr
<MemoryBuffer
> Buff
= MemoryBuffer::getMemBuffer(
24 StringRef((const char *)data
, size
), "", false);
26 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
=
27 ObjectFile::createObjectFile(Buff
->getMemBufferRef());
28 if (auto E
= ObjOrErr
.takeError()) {
29 consumeError(std::move(E
));
32 ObjectFile
&Obj
= *ObjOrErr
.get();
33 std::unique_ptr
<DIContext
> DICtx
= DWARFContext::create(Obj
);
37 opts
.DumpType
= DIDT_All
;
38 DICtx
->dump(nulls(), opts
);