diff options
author | Benjamin Kramer <benny.kra@googlemail.com> | 2017-10-26 10:03:11 +0000 |
---|---|---|
committer | Benjamin Kramer <benny.kra@googlemail.com> | 2017-10-26 10:03:11 +0000 |
commit | 5b93411de259c265227b08268572a6436f122041 (patch) | |
tree | b7be3ab8fb8a3d840e7f1a9158b72c66489c4ee1 | |
parent | a94bb25b1400abef481f2a4411e8be005536b892 (diff) |
[clangd] Add a simple fuzzer. It crashes a lot :)
git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@316649 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | clangd/CMakeLists.txt | 3 | ||||
-rw-r--r-- | clangd/fuzzer/CMakeLists.txt | 23 | ||||
-rw-r--r-- | clangd/fuzzer/ClangdFuzzer.cpp | 34 |
3 files changed, 60 insertions, 0 deletions
diff --git a/clangd/CMakeLists.txt b/clangd/CMakeLists.txt index 9d037fea..99fbf289 100644 --- a/clangd/CMakeLists.txt +++ b/clangd/CMakeLists.txt @@ -28,4 +28,7 @@ add_clang_library(clangDaemon ${LLVM_PTHREAD_LIB} ) +if( LLVM_LIB_FUZZING_ENGINE OR LLVM_USE_SANITIZE_COVERAGE ) + add_subdirectory(fuzzer) +endif() add_subdirectory(tool) diff --git a/clangd/fuzzer/CMakeLists.txt b/clangd/fuzzer/CMakeLists.txt new file mode 100644 index 00000000..0451deab --- /dev/null +++ b/clangd/fuzzer/CMakeLists.txt @@ -0,0 +1,23 @@ +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/..) + +set(LLVM_LINK_COMPONENTS support) + +if(LLVM_USE_SANITIZE_COVERAGE) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer") +endif() + +add_clang_executable(clangd-fuzzer + EXCLUDE_FROM_ALL + ClangdFuzzer.cpp + ) + +target_link_libraries(clangd-fuzzer + clangBasic + clangDaemon + clangFormat + clangFrontend + clangSema + clangTooling + clangToolingCore + ${LLVM_LIB_FUZZING_ENGINE} + ) diff --git a/clangd/fuzzer/ClangdFuzzer.cpp b/clangd/fuzzer/ClangdFuzzer.cpp new file mode 100644 index 00000000..606da15d --- /dev/null +++ b/clangd/fuzzer/ClangdFuzzer.cpp @@ -0,0 +1,34 @@ +//===-- ClangdFuzzer.cpp - Fuzz clangd ------------------------------------===// +// +// 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 clangd on a single input. +/// This function is then linked into the Fuzzer library. +/// +//===----------------------------------------------------------------------===// + +#include "ClangdLSPServer.h" +#include "llvm/Support/Program.h" +#include <sstream> + +extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) { + /// Change stdin to binary to not lose \r\n on windows. + llvm::sys::ChangeStdinToBinary(); + + clang::clangd::JSONOutput Out(llvm::nulls(), llvm::nulls(), nullptr); + + /// Initialize and run ClangdLSPServer. + clang::clangd::ClangdLSPServer LSPServer( + Out, clang::clangd::getDefaultAsyncThreadsCount(), + /*EnableSnippets=*/false, llvm::None, llvm::None); + + std::istringstream In(std::string(reinterpret_cast<char *>(data), size)); + LSPServer.run(In); + return 0; +} |