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" |
Kostya Serebryany | 8328133 | 2017-07-14 18:42:07 +0000 | [diff] [blame] | 17 | #include "clang/CodeGen/CodeGenAction.h" |
Manuel Klimek | 667c152 | 2015-03-28 00:07:39 +0000 | [diff] [blame] | 18 | #include "clang/Frontend/CompilerInstance.h" |
Mehdi Amini | 320a5a6 | 2016-07-18 20:33:09 +0000 | [diff] [blame] | 19 | #include "clang/Lex/PreprocessorOptions.h" |
Manuel Klimek | bea7dfb | 2015-03-28 00:42:36 +0000 | [diff] [blame] | 20 | #include "llvm/Option/Option.h" |
Kostya Serebryany | 8328133 | 2017-07-14 18:42:07 +0000 | [diff] [blame] | 21 | #include "llvm/Support/TargetSelect.h" |
Manuel Klimek | 667c152 | 2015-03-28 00:07:39 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | |
Kostya Serebryany | e39fec5 | 2015-10-02 23:34:37 +0000 | [diff] [blame] | 25 | extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) { |
Manuel Klimek | 667c152 | 2015-03-28 00:07:39 +0000 | [diff] [blame] | 26 | std::string s((const char *)data, size); |
Kostya Serebryany | 8328133 | 2017-07-14 18:42:07 +0000 | [diff] [blame] | 27 | llvm::InitializeAllTargets(); |
| 28 | llvm::InitializeAllTargetMCs(); |
| 29 | llvm::InitializeAllAsmPrinters(); |
| 30 | llvm::InitializeAllAsmParsers(); |
| 31 | |
Manuel Klimek | bea7dfb | 2015-03-28 00:42:36 +0000 | [diff] [blame] | 32 | llvm::opt::ArgStringList CC1Args; |
| 33 | CC1Args.push_back("-cc1"); |
Kostya Serebryany | 7862b01 | 2015-05-04 21:14:45 +0000 | [diff] [blame] | 34 | CC1Args.push_back("./test.cc"); |
Kostya Serebryany | 8328133 | 2017-07-14 18:42:07 +0000 | [diff] [blame] | 35 | CC1Args.push_back("-O2"); |
Manuel Klimek | 667c152 | 2015-03-28 00:07:39 +0000 | [diff] [blame] | 36 | llvm::IntrusiveRefCntPtr<FileManager> Files( |
| 37 | new FileManager(FileSystemOptions())); |
Manuel Klimek | 667c152 | 2015-03-28 00:07:39 +0000 | [diff] [blame] | 38 | IgnoringDiagConsumer Diags; |
Manuel Klimek | bea7dfb | 2015-03-28 00:42:36 +0000 | [diff] [blame] | 39 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
| 40 | DiagnosticsEngine Diagnostics( |
| 41 | IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts, |
| 42 | &Diags, false); |
| 43 | std::unique_ptr<clang::CompilerInvocation> Invocation( |
| 44 | tooling::newInvocation(&Diagnostics, CC1Args)); |
| 45 | std::unique_ptr<llvm::MemoryBuffer> Input = |
| 46 | llvm::MemoryBuffer::getMemBuffer(s); |
Kostya Serebryany | 7862b01 | 2015-05-04 21:14:45 +0000 | [diff] [blame] | 47 | Invocation->getPreprocessorOpts().addRemappedFile("./test.cc", Input.release()); |
Manuel Klimek | bea7dfb | 2015-03-28 00:42:36 +0000 | [diff] [blame] | 48 | std::unique_ptr<tooling::ToolAction> action( |
Kostya Serebryany | 8328133 | 2017-07-14 18:42:07 +0000 | [diff] [blame] | 49 | tooling::newFrontendActionFactory<clang::EmitObjAction>()); |
Alexey Samsonov | 2f640d9 | 2015-06-25 20:21:59 +0000 | [diff] [blame] | 50 | std::shared_ptr<PCHContainerOperations> PCHContainerOps = |
Adrian Prantl | d5aae93 | 2015-07-17 04:07:47 +0000 | [diff] [blame] | 51 | std::make_shared<PCHContainerOperations>(); |
Benjamin Kramer | 2e8927a | 2017-01-11 16:42:26 +0000 | [diff] [blame] | 52 | action->runInvocation(std::move(Invocation), Files.get(), PCHContainerOps, |
Alexey Samsonov | 2f640d9 | 2015-06-25 20:21:59 +0000 | [diff] [blame] | 53 | &Diags); |
Kostya Serebryany | e39fec5 | 2015-10-02 23:34:37 +0000 | [diff] [blame] | 54 | return 0; |
Manuel Klimek | 667c152 | 2015-03-28 00:07:39 +0000 | [diff] [blame] | 55 | } |