summaryrefslogtreecommitdiff
path: root/tools/clang-fuzzer
diff options
context:
space:
mode:
Diffstat (limited to 'tools/clang-fuzzer')
-rw-r--r--tools/clang-fuzzer/CMakeLists.txt20
-rw-r--r--tools/clang-fuzzer/ClangFuzzer.cpp43
2 files changed, 63 insertions, 0 deletions
diff --git a/tools/clang-fuzzer/CMakeLists.txt b/tools/clang-fuzzer/CMakeLists.txt
new file mode 100644
index 0000000000000..87d21c6bf116b
--- /dev/null
+++ b/tools/clang-fuzzer/CMakeLists.txt
@@ -0,0 +1,20 @@
+if( LLVM_USE_SANITIZE_COVERAGE )
+ set(LLVM_LINK_COMPONENTS support)
+
+ add_clang_executable(clang-fuzzer
+ EXCLUDE_FROM_ALL
+ ClangFuzzer.cpp
+ )
+
+ target_link_libraries(clang-fuzzer
+ ${CLANG_FORMAT_LIB_DEPS}
+ clangAST
+ clangBasic
+ clangDriver
+ clangFrontend
+ clangRewriteFrontend
+ clangStaticAnalyzerFrontend
+ clangTooling
+ LLVMFuzzer
+ )
+endif()
diff --git a/tools/clang-fuzzer/ClangFuzzer.cpp b/tools/clang-fuzzer/ClangFuzzer.cpp
new file mode 100644
index 0000000000000..17ef0521e3c38
--- /dev/null
+++ b/tools/clang-fuzzer/ClangFuzzer.cpp
@@ -0,0 +1,43 @@
+//===-- ClangFuzzer.cpp - Fuzz Clang --------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// \brief This file implements a function that runs Clang on a single
+/// input. This function is then linked into the Fuzzer library.
+///
+//===----------------------------------------------------------------------===//
+
+#include "clang/Tooling/Tooling.h"
+#include "clang/Frontend/FrontendActions.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "llvm/Option/Option.h"
+
+using namespace clang;
+
+extern "C" void LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
+ std::string s((const char *)data, size);
+ llvm::opt::ArgStringList CC1Args;
+ CC1Args.push_back("-cc1");
+ 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::SyntaxOnlyAction>());
+ action->runInvocation(Invocation.release(), Files.get(), &Diags);
+}