blob: a0e14360d7af24b7dbfa97019c175dfd72971d15 [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
Kostya Serebryanyf7a8ef52016-05-31 23:39:31 +000026extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
David Majnemer8df24c32016-05-31 01:24:40 +000027 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));
Kostya Serebryanyf7a8ef52016-05-31 23:39:31 +000036 return 0;
David Majnemer8df24c32016-05-31 01:24:40 +000037 }
38 if (auto E = File->parseStreamData()) {
39 consumeError(std::move(E));
Kostya Serebryanyf7a8ef52016-05-31 23:39:31 +000040 return 0;
David Majnemer8df24c32016-05-31 01:24:40 +000041 }
42
43 auto DbiS = File->getPDBDbiStream();
44 if (auto E = DbiS.takeError()) {
45 consumeError(std::move(E));
Kostya Serebryanyf7a8ef52016-05-31 23:39:31 +000046 return 0;
David Majnemer8df24c32016-05-31 01:24:40 +000047 }
48 auto TpiS = File->getPDBTpiStream();
49 if (auto E = TpiS.takeError()) {
50 consumeError(std::move(E));
Kostya Serebryanyf7a8ef52016-05-31 23:39:31 +000051 return 0;
David Majnemer8df24c32016-05-31 01:24:40 +000052 }
53 auto IpiS = File->getPDBIpiStream();
54 if (auto E = IpiS.takeError()) {
55 consumeError(std::move(E));
Kostya Serebryanyf7a8ef52016-05-31 23:39:31 +000056 return 0;
David Majnemer8df24c32016-05-31 01:24:40 +000057 }
58 auto InfoS = File->getPDBInfoStream();
59 if (auto E = InfoS.takeError()) {
60 consumeError(std::move(E));
Kostya Serebryanyf7a8ef52016-05-31 23:39:31 +000061 return 0;
David Majnemer8df24c32016-05-31 01:24:40 +000062 }
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));
Kostya Serebryanyf7a8ef52016-05-31 23:39:31 +000069 return 0;
David Majnemer8df24c32016-05-31 01:24:40 +000070 }
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 }
Kostya Serebryanyf7a8ef52016-05-31 23:39:31 +000077 return 0;
David Majnemer8df24c32016-05-31 01:24:40 +000078}