From 973fcc25fb19b9bcd845a8f260673319b12954a5 Mon Sep 17 00:00:00 2001 From: Alexander Kornienko Date: Tue, 8 Jan 2019 16:55:13 +0000 Subject: [PATCH] Fix use-after-free bug in Tooling. Summary: `buildASTFromCodeWithArgs()` was creating a memory buffer referencing a stack-allocated string. This diff changes the implementation to copy the code string into the memory buffer so that said buffer owns the memory. Patch by Yitzhak Mandelbaum. Reviewers: alexfh Reviewed By: alexfh Subscribers: cfe-commits, EricWF Differential Revision: https://reviews.llvm.org/D55765 llvm-svn: 350638 --- clang/include/clang/Tooling/Tooling.h | 8 ++++---- clang/lib/Tooling/Tooling.cpp | 18 ++++++------------ clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp | 5 ++++- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/clang/include/clang/Tooling/Tooling.h b/clang/include/clang/Tooling/Tooling.h index 358acf3..662a980 100644 --- a/clang/include/clang/Tooling/Tooling.h +++ b/clang/include/clang/Tooling/Tooling.h @@ -205,7 +205,7 @@ bool runToolOnCodeWithArgs( /// /// \return The resulting AST or null if an error occurred. std::unique_ptr -buildASTFromCode(const Twine &Code, const Twine &FileName = "input.cc", +buildASTFromCode(StringRef Code, StringRef FileName = "input.cc", std::shared_ptr PCHContainerOps = std::make_shared()); @@ -223,10 +223,10 @@ buildASTFromCode(const Twine &Code, const Twine &FileName = "input.cc", /// /// \return The resulting AST or null if an error occurred. std::unique_ptr buildASTFromCodeWithArgs( - const Twine &Code, const std::vector &Args, - const Twine &FileName = "input.cc", const Twine &ToolName = "clang-tool", + StringRef Code, const std::vector &Args, + StringRef FileName = "input.cc", StringRef ToolName = "clang-tool", std::shared_ptr PCHContainerOps = - std::make_shared(), + std::make_shared(), ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster()); /// Utility to run a FrontendAction in a single clang invocation. diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp index 84a4ac64..63aa64a 100644 --- a/clang/lib/Tooling/Tooling.cpp +++ b/clang/lib/Tooling/Tooling.cpp @@ -574,20 +574,16 @@ namespace clang { namespace tooling { std::unique_ptr -buildASTFromCode(const Twine &Code, const Twine &FileName, +buildASTFromCode(StringRef Code, StringRef FileName, std::shared_ptr PCHContainerOps) { return buildASTFromCodeWithArgs(Code, std::vector(), FileName, "clang-tool", std::move(PCHContainerOps)); } std::unique_ptr buildASTFromCodeWithArgs( - const Twine &Code, const std::vector &Args, - const Twine &FileName, const Twine &ToolName, - std::shared_ptr PCHContainerOps, + StringRef Code, const std::vector &Args, StringRef FileName, + StringRef ToolName, std::shared_ptr PCHContainerOps, ArgumentsAdjuster Adjuster) { - SmallString<16> FileNameStorage; - StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); - std::vector> ASTs; ASTBuilderAction Action(ASTs); llvm::IntrusiveRefCntPtr OverlayFileSystem( @@ -599,13 +595,11 @@ std::unique_ptr buildASTFromCodeWithArgs( new FileManager(FileSystemOptions(), OverlayFileSystem)); ToolInvocation Invocation( - getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileNameRef), FileNameRef), + getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileName), FileName), &Action, Files.get(), std::move(PCHContainerOps)); - SmallString<1024> CodeStorage; - InMemoryFileSystem->addFile(FileNameRef, 0, - llvm::MemoryBuffer::getMemBuffer( - Code.toNullTerminatedStringRef(CodeStorage))); + InMemoryFileSystem->addFile(FileName, 0, + llvm::MemoryBuffer::getMemBufferCopy(Code)); if (!Invocation.run()) return nullptr; diff --git a/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp b/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp index 9c6bc78..68c921e 100644 --- a/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp +++ b/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp @@ -11,6 +11,7 @@ #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" #include "clang/Tooling/Tooling.h" +#include "llvm/ADT/SmallString.h" #include "gmock/gmock.h" #include "gtest/gtest.h" #include @@ -32,7 +33,9 @@ using StmtMatcher = internal::Matcher; std::unique_ptr buildASTFromCodeWithArgs(const Twine &Code, const std::vector &Args) { - auto AST = tooling::buildASTFromCodeWithArgs(Code, Args); + SmallString<1024> CodeStorage; + auto AST = + tooling::buildASTFromCodeWithArgs(Code.toStringRef(CodeStorage), Args); EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred()); return AST; } -- 2.7.4