blob: 9eceb843e581ca99b09ebd3cdf4e473d0a2b3513 [file] [log] [blame]
Manuel Klimek667c1522015-03-28 00:07:39 +00001//===-- 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 Serebryany83281332017-07-14 18:42:07 +000017#include "clang/CodeGen/CodeGenAction.h"
Manuel Klimek667c1522015-03-28 00:07:39 +000018#include "clang/Frontend/CompilerInstance.h"
Mehdi Amini320a5a62016-07-18 20:33:09 +000019#include "clang/Lex/PreprocessorOptions.h"
Manuel Klimekbea7dfb2015-03-28 00:42:36 +000020#include "llvm/Option/Option.h"
Kostya Serebryany83281332017-07-14 18:42:07 +000021#include "llvm/Support/TargetSelect.h"
Manuel Klimek667c1522015-03-28 00:07:39 +000022
23using namespace clang;
24
Kostya Serebryanye39fec52015-10-02 23:34:37 +000025extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
Manuel Klimek667c1522015-03-28 00:07:39 +000026 std::string s((const char *)data, size);
Kostya Serebryany83281332017-07-14 18:42:07 +000027 llvm::InitializeAllTargets();
28 llvm::InitializeAllTargetMCs();
29 llvm::InitializeAllAsmPrinters();
30 llvm::InitializeAllAsmParsers();
31
Manuel Klimekbea7dfb2015-03-28 00:42:36 +000032 llvm::opt::ArgStringList CC1Args;
33 CC1Args.push_back("-cc1");
Kostya Serebryany7862b012015-05-04 21:14:45 +000034 CC1Args.push_back("./test.cc");
Kostya Serebryany83281332017-07-14 18:42:07 +000035 CC1Args.push_back("-O2");
Manuel Klimek667c1522015-03-28 00:07:39 +000036 llvm::IntrusiveRefCntPtr<FileManager> Files(
37 new FileManager(FileSystemOptions()));
Manuel Klimek667c1522015-03-28 00:07:39 +000038 IgnoringDiagConsumer Diags;
Manuel Klimekbea7dfb2015-03-28 00:42:36 +000039 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 Serebryany7862b012015-05-04 21:14:45 +000047 Invocation->getPreprocessorOpts().addRemappedFile("./test.cc", Input.release());
Manuel Klimekbea7dfb2015-03-28 00:42:36 +000048 std::unique_ptr<tooling::ToolAction> action(
Kostya Serebryany83281332017-07-14 18:42:07 +000049 tooling::newFrontendActionFactory<clang::EmitObjAction>());
Alexey Samsonov2f640d92015-06-25 20:21:59 +000050 std::shared_ptr<PCHContainerOperations> PCHContainerOps =
Adrian Prantld5aae932015-07-17 04:07:47 +000051 std::make_shared<PCHContainerOperations>();
Benjamin Kramer2e8927a2017-01-11 16:42:26 +000052 action->runInvocation(std::move(Invocation), Files.get(), PCHContainerOps,
Alexey Samsonov2f640d92015-06-25 20:21:59 +000053 &Diags);
Kostya Serebryanye39fec52015-10-02 23:34:37 +000054 return 0;
Manuel Klimek667c1522015-03-28 00:07:39 +000055}