Index: clangd/CMakeLists.txt =================================================================== --- clangd/CMakeLists.txt +++ clangd/CMakeLists.txt @@ -116,7 +116,8 @@ ) add_subdirectory(refactor/tweaks) -if( LLVM_LIB_FUZZING_ENGINE OR LLVM_USE_SANITIZE_COVERAGE ) +if (LINUX) + # FIXME: Make fuzzer not use linux-specific APIs, build it everywhere. add_subdirectory(fuzzer) endif() add_subdirectory(tool) Index: clangd/fuzzer/CMakeLists.txt =================================================================== --- clangd/fuzzer/CMakeLists.txt +++ clangd/fuzzer/CMakeLists.txt @@ -2,13 +2,10 @@ 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 +# This fuzzer runs on oss-fuzz, so keep it around even if it looks unreferenced. +add_llvm_fuzzer(clangd-fuzzer + clangd-fuzzer.cpp + DUMMY_MAIN DummyClangdMain.cpp ) target_link_libraries(clangd-fuzzer @@ -20,5 +17,4 @@ clangSema clangTooling clangToolingCore - ${LLVM_LIB_FUZZING_ENGINE} ) Index: clangd/fuzzer/ClangdFuzzer.cpp =================================================================== --- clangd/fuzzer/ClangdFuzzer.cpp +++ clangd/fuzzer/ClangdFuzzer.cpp @@ -1,42 +0,0 @@ -//===-- ClangdFuzzer.cpp - Fuzz clangd ------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -/// -/// \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 "ClangdServer.h" -#include "CodeComplete.h" -#include "FSProvider.h" -#include -#include - -using namespace clang::clangd; - -extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) { - if (size == 0) - return 0; - - // fmemopen isn't portable, but I think we only run the fuzzer on Linux. - std::FILE *In = fmemopen(data, size, "r"); - auto Transport = newJSONTransport(In, llvm::nulls(), - /*InMirror=*/nullptr, /*Pretty=*/false, - /*Style=*/JSONStreamStyle::Delimited); - RealFileSystemProvider FS; - CodeCompleteOptions CCOpts; - CCOpts.EnableSnippets = false; - ClangdServer::Options Opts; - - // Initialize and run ClangdLSPServer. - ClangdLSPServer LSPServer(*Transport, FS, CCOpts, llvm::None, false, Opts); - LSPServer.run(); - return 0; -} Index: clangd/fuzzer/DummyClangdMain.cpp =================================================================== --- clangd/fuzzer/DummyClangdMain.cpp +++ clangd/fuzzer/DummyClangdMain.cpp @@ -0,0 +1,18 @@ +//===---- DummyClangdMain.cpp - Entry point to sanity check the fuzzer ----===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Implementation of main so we can build and test without linking libFuzzer. +// +//===----------------------------------------------------------------------===// + +#include "llvm/FuzzMutate/FuzzerCLI.h" + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size); +int main(int argc, char *argv[]) { + return llvm::runFuzzerOnInputs(argc, argv, LLVMFuzzerTestOneInput); +} Index: clangd/fuzzer/clangd-fuzzer.cpp =================================================================== --- clangd/fuzzer/clangd-fuzzer.cpp +++ clangd/fuzzer/clangd-fuzzer.cpp @@ -0,0 +1,42 @@ +//===-- ClangdFuzzer.cpp - Fuzz clangd ------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +/// +/// \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 "ClangdServer.h" +#include "CodeComplete.h" +#include "FSProvider.h" +#include +#include + +using namespace clang::clangd; + +extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) { + if (size == 0) + return 0; + + // fmemopen isn't portable, but I think we only run the fuzzer on Linux. + std::FILE *In = fmemopen(data, size, "r"); + auto Transport = newJSONTransport(In, llvm::nulls(), + /*InMirror=*/nullptr, /*Pretty=*/false, + /*Style=*/JSONStreamStyle::Delimited); + RealFileSystemProvider FS; + CodeCompleteOptions CCOpts; + CCOpts.EnableSnippets = false; + ClangdServer::Options Opts; + + // Initialize and run ClangdLSPServer. + ClangdLSPServer LSPServer(*Transport, FS, CCOpts, llvm::None, false, Opts); + LSPServer.run(); + return 0; +}