blob: 988f47dcce97ef908d1c77378da5e52fb21f10ce [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"
Zachary Turner36eb0472016-06-09 00:21:37 +000018#include "llvm/DebugInfo/PDB/Raw/IPDBStreamData.h"
19#include "llvm/DebugInfo/PDB/Raw/MappedBlockStream.h"
David Majnemer8df24c32016-05-31 01:24:40 +000020#include "llvm/DebugInfo/PDB/Raw/ModStream.h"
21#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
22#include "llvm/DebugInfo/PDB/Raw/RawSession.h"
23#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/ScopedPrinter.h"
25
26using namespace llvm;
27
Kostya Serebryanyf7a8ef52016-05-31 23:39:31 +000028extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
David Majnemer8df24c32016-05-31 01:24:40 +000029 std::unique_ptr<MemoryBuffer> Buff = MemoryBuffer::getMemBuffer(
30 StringRef((const char *)data, size), "", false);
31
32 ScopedPrinter P(nulls());
Reid Klecknercbb1d062016-05-31 22:32:54 +000033 codeview::CVTypeDumper TD(&P, false);
David Majnemer8df24c32016-05-31 01:24:40 +000034
35 std::unique_ptr<pdb::PDBFile> File(new pdb::PDBFile(std::move(Buff)));
36 if (auto E = File->parseFileHeaders()) {
37 consumeError(std::move(E));
Kostya Serebryanyf7a8ef52016-05-31 23:39:31 +000038 return 0;
David Majnemer8df24c32016-05-31 01:24:40 +000039 }
40 if (auto E = File->parseStreamData()) {
41 consumeError(std::move(E));
Kostya Serebryanyf7a8ef52016-05-31 23:39:31 +000042 return 0;
David Majnemer8df24c32016-05-31 01:24:40 +000043 }
44
45 auto DbiS = File->getPDBDbiStream();
46 if (auto E = DbiS.takeError()) {
47 consumeError(std::move(E));
Kostya Serebryanyf7a8ef52016-05-31 23:39:31 +000048 return 0;
David Majnemer8df24c32016-05-31 01:24:40 +000049 }
50 auto TpiS = File->getPDBTpiStream();
51 if (auto E = TpiS.takeError()) {
52 consumeError(std::move(E));
Kostya Serebryanyf7a8ef52016-05-31 23:39:31 +000053 return 0;
David Majnemer8df24c32016-05-31 01:24:40 +000054 }
55 auto IpiS = File->getPDBIpiStream();
56 if (auto E = IpiS.takeError()) {
57 consumeError(std::move(E));
Kostya Serebryanyf7a8ef52016-05-31 23:39:31 +000058 return 0;
David Majnemer8df24c32016-05-31 01:24:40 +000059 }
60 auto InfoS = File->getPDBInfoStream();
61 if (auto E = InfoS.takeError()) {
62 consumeError(std::move(E));
Kostya Serebryanyf7a8ef52016-05-31 23:39:31 +000063 return 0;
David Majnemer8df24c32016-05-31 01:24:40 +000064 }
65 pdb::DbiStream &DS = DbiS.get();
66
67 for (auto &Modi : DS.modules()) {
Zachary Turner36eb0472016-06-09 00:21:37 +000068 auto ModStreamData = pdb::MappedBlockStream::createIndexedStream(
69 Modi.Info.getModuleStreamIndex(), *File);
70 if (!ModStreamData) {
71 consumeError(ModStreamData.takeError());
72 return 0;
73 }
74 pdb::ModStream ModS(Modi.Info, std::move(*ModStreamData));
David Majnemer8df24c32016-05-31 01:24:40 +000075 if (auto E = ModS.reload()) {
76 consumeError(std::move(E));
Kostya Serebryanyf7a8ef52016-05-31 23:39:31 +000077 return 0;
David Majnemer8df24c32016-05-31 01:24:40 +000078 }
79 codeview::CVSymbolDumper SD(P, TD, nullptr, false);
80 bool HadError = false;
81 for (auto &S : ModS.symbols(&HadError)) {
82 SD.dump(S);
83 }
84 }
Kostya Serebryanyf7a8ef52016-05-31 23:39:31 +000085 return 0;
David Majnemer8df24c32016-05-31 01:24:40 +000086}