From 09113ae2c5671afea23c3f9d69ae5228055c3d1a Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Thu, 26 Oct 2017 10:03:11 +0000 Subject: [PATCH] [clangd] Add a simple fuzzer. It crashes a lot :) llvm-svn: 316649 --- clang-tools-extra/clangd/CMakeLists.txt | 3 +++ clang-tools-extra/clangd/fuzzer/CMakeLists.txt | 23 ++++++++++++++++ clang-tools-extra/clangd/fuzzer/ClangdFuzzer.cpp | 34 ++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 clang-tools-extra/clangd/fuzzer/CMakeLists.txt create mode 100644 clang-tools-extra/clangd/fuzzer/ClangdFuzzer.cpp diff --git a/clang-tools-extra/clangd/CMakeLists.txt b/clang-tools-extra/clangd/CMakeLists.txt index 9d037fe..99fbf28 100644 --- a/clang-tools-extra/clangd/CMakeLists.txt +++ b/clang-tools-extra/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/clang-tools-extra/clangd/fuzzer/CMakeLists.txt b/clang-tools-extra/clangd/fuzzer/CMakeLists.txt new file mode 100644 index 0000000..0451dea --- /dev/null +++ b/clang-tools-extra/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/clang-tools-extra/clangd/fuzzer/ClangdFuzzer.cpp b/clang-tools-extra/clangd/fuzzer/ClangdFuzzer.cpp new file mode 100644 index 0000000..606da15 --- /dev/null +++ b/clang-tools-extra/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 + +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(data), size)); + LSPServer.run(In); + return 0; +} -- 2.7.4