blob: 0c191e2eb0a50f1b62891be6d52fc288dfff131e [file] [log] [blame]
David Majnemer8df24c32016-05-31 01:24:40 +00001//===-- llvm-pdbdump-fuzzer.cpp - Fuzz the llvm-pdbdump 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-pdbdump
12/// on a single input. This function is then linked into the Fuzzer library.
13///
14//===----------------------------------------------------------------------===//
15#include "llvm/DebugInfo/CodeView/SymbolDumper.h"
16#include "llvm/DebugInfo/CodeView/TypeDumper.h"
17#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
18#include "llvm/DebugInfo/PDB/Raw/ModStream.h"
19#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
20#include "llvm/DebugInfo/PDB/Raw/RawSession.h"
21#include "llvm/Support/MemoryBuffer.h"
22#include "llvm/Support/ScopedPrinter.h"
23
24using namespace llvm;
25
26extern "C" void LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
27 std::unique_ptr<MemoryBuffer> Buff = MemoryBuffer::getMemBuffer(
28 StringRef((const char *)data, size), "", false);
29
30 ScopedPrinter P(nulls());
Reid Klecknercbb1d062016-05-31 22:32:54 +000031 codeview::CVTypeDumper TD(&P, false);
David Majnemer8df24c32016-05-31 01:24:40 +000032
33 std::unique_ptr<pdb::PDBFile> File(new pdb::PDBFile(std::move(Buff)));
34 if (auto E = File->parseFileHeaders()) {
35 consumeError(std::move(E));
36 return;
37 }
38 if (auto E = File->parseStreamData()) {
39 consumeError(std::move(E));
40 return;
41 }
42
43 auto DbiS = File->getPDBDbiStream();
44 if (auto E = DbiS.takeError()) {
45 consumeError(std::move(E));
46 return;
47 }
48 auto TpiS = File->getPDBTpiStream();
49 if (auto E = TpiS.takeError()) {
50 consumeError(std::move(E));
51 return;
52 }
53 auto IpiS = File->getPDBIpiStream();
54 if (auto E = IpiS.takeError()) {
55 consumeError(std::move(E));
56 return;
57 }
58 auto InfoS = File->getPDBInfoStream();
59 if (auto E = InfoS.takeError()) {
60 consumeError(std::move(E));
61 return;
62 }
63 pdb::DbiStream &DS = DbiS.get();
64
65 for (auto &Modi : DS.modules()) {
66 pdb::ModStream ModS(*File, Modi.Info);
67 if (auto E = ModS.reload()) {
68 consumeError(std::move(E));
69 return;
70 }
71 codeview::CVSymbolDumper SD(P, TD, nullptr, false);
72 bool HadError = false;
73 for (auto &S : ModS.symbols(&HadError)) {
74 SD.dump(S);
75 }
76 }
77}