diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2017-12-18 20:11:37 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2017-12-18 20:11:37 +0000 |
commit | 461a67fa15370a9ec88f8f8a240bf7c123bb2029 (patch) | |
tree | 6942083d7d56bba40ec790a453ca58ad3baf6832 /tools/clang-fuzzer/handle-cxx | |
parent | 75c3240472ba6ac2669ee72ca67eb72d4e2851fc (diff) |
Notes
Diffstat (limited to 'tools/clang-fuzzer/handle-cxx')
-rw-r--r-- | tools/clang-fuzzer/handle-cxx/CMakeLists.txt | 12 | ||||
-rw-r--r-- | tools/clang-fuzzer/handle-cxx/handle_cxx.cpp | 58 | ||||
-rw-r--r-- | tools/clang-fuzzer/handle-cxx/handle_cxx.h | 25 |
3 files changed, 95 insertions, 0 deletions
diff --git a/tools/clang-fuzzer/handle-cxx/CMakeLists.txt b/tools/clang-fuzzer/handle-cxx/CMakeLists.txt new file mode 100644 index 0000000000000..caf1dba7af676 --- /dev/null +++ b/tools/clang-fuzzer/handle-cxx/CMakeLists.txt @@ -0,0 +1,12 @@ +set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD} Support) + +add_clang_library(clangHandleCXX + handle_cxx.cpp + + LINK_LIBS + clangBasic + clangCodeGen + clangFrontend + clangLex + clangTooling + ) diff --git a/tools/clang-fuzzer/handle-cxx/handle_cxx.cpp b/tools/clang-fuzzer/handle-cxx/handle_cxx.cpp new file mode 100644 index 0000000000000..312ab91e5fe21 --- /dev/null +++ b/tools/clang-fuzzer/handle-cxx/handle_cxx.cpp @@ -0,0 +1,58 @@ +//==-- handle_cxx.cpp - Helper function for Clang fuzzers ------------------==// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Implements HandleCXX for use by the Clang fuzzers. +// +//===----------------------------------------------------------------------===// + +#include "handle_cxx.h" + +#include "clang/CodeGen/CodeGenAction.h" +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Lex/PreprocessorOptions.h" +#include "clang/Tooling/Tooling.h" +#include "llvm/Option/Option.h" +#include "llvm/Support/TargetSelect.h" + +using namespace clang; + +void clang_fuzzer::HandleCXX(const std::string &S, + const std::vector<const char *> &ExtraArgs) { + llvm::InitializeAllTargets(); + llvm::InitializeAllTargetMCs(); + llvm::InitializeAllAsmPrinters(); + llvm::InitializeAllAsmParsers(); + + llvm::opt::ArgStringList CC1Args; + CC1Args.push_back("-cc1"); + for (auto &A : ExtraArgs) + CC1Args.push_back(A); + CC1Args.push_back("./test.cc"); + + llvm::IntrusiveRefCntPtr<FileManager> Files( + new FileManager(FileSystemOptions())); + IgnoringDiagConsumer Diags; + IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); + DiagnosticsEngine Diagnostics( + IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts, + &Diags, false); + std::unique_ptr<clang::CompilerInvocation> Invocation( + tooling::newInvocation(&Diagnostics, CC1Args)); + std::unique_ptr<llvm::MemoryBuffer> Input = + llvm::MemoryBuffer::getMemBuffer(S); + Invocation->getPreprocessorOpts().addRemappedFile("./test.cc", + Input.release()); + std::unique_ptr<tooling::ToolAction> action( + tooling::newFrontendActionFactory<clang::EmitObjAction>()); + std::shared_ptr<PCHContainerOperations> PCHContainerOps = + std::make_shared<PCHContainerOperations>(); + action->runInvocation(std::move(Invocation), Files.get(), PCHContainerOps, + &Diags); +} + diff --git a/tools/clang-fuzzer/handle-cxx/handle_cxx.h b/tools/clang-fuzzer/handle-cxx/handle_cxx.h new file mode 100644 index 0000000000000..e76311ff39c41 --- /dev/null +++ b/tools/clang-fuzzer/handle-cxx/handle_cxx.h @@ -0,0 +1,25 @@ +//==-- handle_cxx.h - Helper function for Clang fuzzers --------------------==// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Defines HandleCXX for use by the Clang fuzzers. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_CLANG_FUZZER_HANDLE_CXX_HANDLECXX_H +#define LLVM_CLANG_TOOLS_CLANG_FUZZER_HANDLE_CXX_HANDLECXX_H + +#include <string> +#include <vector> + +namespace clang_fuzzer { +void HandleCXX(const std::string &S, + const std::vector<const char *> &ExtraArgs); +} // namespace clang_fuzzer + +#endif |