Manuel Klimek | 667c152 | 2015-03-28 00:07:39 +0000 | [diff] [blame] | 1 | //===-- ClangFuzzer.cpp - Fuzz Clang --------------------------------------===// |
| 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 Clang on a single |
| 12 | /// input. This function is then linked into the Fuzzer library. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "clang/Tooling/Tooling.h" |
| 17 | #include "clang/Frontend/FrontendActions.h" |
| 18 | #include "clang/Frontend/CompilerInstance.h" |
Manuel Klimek | bea7dfb | 2015-03-28 00:42:36 +0000 | [diff] [blame] | 19 | #include "llvm/Option/Option.h" |
Manuel Klimek | 667c152 | 2015-03-28 00:07:39 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | |
Kostya Serebryany | 9ba68b3 | 2015-05-07 04:01:39 +0000 | [diff] [blame] | 23 | extern "C" void LLVMFuzzerTestOneInput(uint8_t *data, size_t size) { |
Manuel Klimek | 667c152 | 2015-03-28 00:07:39 +0000 | [diff] [blame] | 24 | std::string s((const char *)data, size); |
Manuel Klimek | bea7dfb | 2015-03-28 00:42:36 +0000 | [diff] [blame] | 25 | llvm::opt::ArgStringList CC1Args; |
| 26 | CC1Args.push_back("-cc1"); |
Kostya Serebryany | 7862b01 | 2015-05-04 21:14:45 +0000 | [diff] [blame] | 27 | CC1Args.push_back("./test.cc"); |
Manuel Klimek | 667c152 | 2015-03-28 00:07:39 +0000 | [diff] [blame] | 28 | llvm::IntrusiveRefCntPtr<FileManager> Files( |
| 29 | new FileManager(FileSystemOptions())); |
Manuel Klimek | 667c152 | 2015-03-28 00:07:39 +0000 | [diff] [blame] | 30 | IgnoringDiagConsumer Diags; |
Manuel Klimek | bea7dfb | 2015-03-28 00:42:36 +0000 | [diff] [blame] | 31 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
| 32 | DiagnosticsEngine Diagnostics( |
| 33 | IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts, |
| 34 | &Diags, false); |
| 35 | std::unique_ptr<clang::CompilerInvocation> Invocation( |
| 36 | tooling::newInvocation(&Diagnostics, CC1Args)); |
| 37 | std::unique_ptr<llvm::MemoryBuffer> Input = |
| 38 | llvm::MemoryBuffer::getMemBuffer(s); |
Kostya Serebryany | 7862b01 | 2015-05-04 21:14:45 +0000 | [diff] [blame] | 39 | Invocation->getPreprocessorOpts().addRemappedFile("./test.cc", Input.release()); |
Manuel Klimek | bea7dfb | 2015-03-28 00:42:36 +0000 | [diff] [blame] | 40 | std::unique_ptr<tooling::ToolAction> action( |
| 41 | tooling::newFrontendActionFactory<clang::SyntaxOnlyAction>()); |
Alexey Samsonov | 2f640d9 | 2015-06-25 20:21:59 +0000 | [diff] [blame] | 42 | std::shared_ptr<PCHContainerOperations> PCHContainerOps = |
Adrian Prantl | d5aae93 | 2015-07-17 04:07:47 +0000 | [diff] [blame^] | 43 | std::make_shared<PCHContainerOperations>(); |
Alexey Samsonov | 2f640d9 | 2015-06-25 20:21:59 +0000 | [diff] [blame] | 44 | action->runInvocation(Invocation.release(), Files.get(), PCHContainerOps, |
| 45 | &Diags); |
Manuel Klimek | 667c152 | 2015-03-28 00:07:39 +0000 | [diff] [blame] | 46 | } |