//===-- 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" int 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 Files( new FileManager(FileSystemOptions())); IgnoringDiagConsumer Diags; IntrusiveRefCntPtr DiagOpts = new DiagnosticOptions(); DiagnosticsEngine Diagnostics( IntrusiveRefCntPtr(new DiagnosticIDs()), &*DiagOpts, &Diags, false); std::unique_ptr Invocation( tooling::newInvocation(&Diagnostics, CC1Args)); std::unique_ptr Input = llvm::MemoryBuffer::getMemBuffer(s); Invocation->getPreprocessorOpts().addRemappedFile("./test.cc", Input.release()); std::unique_ptr action( tooling::newFrontendActionFactory()); std::shared_ptr PCHContainerOps = std::make_shared(); action->runInvocation(Invocation.release(), Files.get(), PCHContainerOps, &Diags); return 0; }