Author: shafik Date: Wed Jul 17 13:16:13 2019 New Revision: 366365 URL: http://llvm.org/viewvc/llvm-project?rev=366365&view=rev Log: Fix CreateFunctionTemplateSpecialization to prevent dangling poiner to stack memory
In ClangASTContext::CreateFunctionTemplateSpecializationInfo a TemplateArgumentList is allocated on the stack but is treated as if it is persistent in subsequent calls. When we exit the function func_decl will still point to the stack allocated memory. We will use TemplateArgumentList::CreateCopy instead which will allocate memory out of the DeclContext. Differential Revision: https://reviews.llvm.org/D64777 Added: lldb/trunk/packages/Python/lldbsuite/test/expression_command/function_template_specialization_temp_args/ lldb/trunk/packages/Python/lldbsuite/test/expression_command/function_template_specialization_temp_args/Makefile lldb/trunk/packages/Python/lldbsuite/test/expression_command/function_template_specialization_temp_args/TestFunctionTemplateSpecializationTempArgs.py lldb/trunk/packages/Python/lldbsuite/test/expression_command/function_template_specialization_temp_args/main.cpp Modified: lldb/trunk/source/Symbol/ClangASTContext.cpp Added: lldb/trunk/packages/Python/lldbsuite/test/expression_command/function_template_specialization_temp_args/Makefile URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/expression_command/function_template_specialization_temp_args/Makefile?rev=366365&view=auto ============================================================================== --- lldb/trunk/packages/Python/lldbsuite/test/expression_command/function_template_specialization_temp_args/Makefile (added) +++ lldb/trunk/packages/Python/lldbsuite/test/expression_command/function_template_specialization_temp_args/Makefile Wed Jul 17 13:16:13 2019 @@ -0,0 +1,5 @@ +LEVEL = ../../make + +CXX_SOURCES := main.cpp + +include $(LEVEL)/Makefile.rules Added: lldb/trunk/packages/Python/lldbsuite/test/expression_command/function_template_specialization_temp_args/TestFunctionTemplateSpecializationTempArgs.py URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/expression_command/function_template_specialization_temp_args/TestFunctionTemplateSpecializationTempArgs.py?rev=366365&view=auto ============================================================================== --- lldb/trunk/packages/Python/lldbsuite/test/expression_command/function_template_specialization_temp_args/TestFunctionTemplateSpecializationTempArgs.py (added) +++ lldb/trunk/packages/Python/lldbsuite/test/expression_command/function_template_specialization_temp_args/TestFunctionTemplateSpecializationTempArgs.py Wed Jul 17 13:16:13 2019 @@ -0,0 +1,17 @@ +import lldb +from lldbsuite.test.decorators import * +from lldbsuite.test.lldbtest import * +from lldbsuite.test import lldbutil + +class TestFunctionTemplateSpecializationTempArgs(TestBase): + + mydir = TestBase.compute_mydir(__file__) + + def test_function_template_specialization_temp_args(self): + self.build() + + (self.target, self.process, _, bkpt) = lldbutil.run_to_source_breakpoint(self, '// break here', + lldb.SBFileSpec("main.cpp", False)) + + self.expect("expr p0", + substrs=['(VType) $0 = {}']) Added: lldb/trunk/packages/Python/lldbsuite/test/expression_command/function_template_specialization_temp_args/main.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/expression_command/function_template_specialization_temp_args/main.cpp?rev=366365&view=auto ============================================================================== --- lldb/trunk/packages/Python/lldbsuite/test/expression_command/function_template_specialization_temp_args/main.cpp (added) +++ lldb/trunk/packages/Python/lldbsuite/test/expression_command/function_template_specialization_temp_args/main.cpp Wed Jul 17 13:16:13 2019 @@ -0,0 +1,17 @@ +template <typename T> struct M {}; + +template <typename T> void f(T &t); + +template <> void f<int>(int &t) { + typedef M<int> VType; + + VType p0; // break here +} + +int main() { + int x; + + f(x); + + return 0; +} Modified: lldb/trunk/source/Symbol/ClangASTContext.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Symbol/ClangASTContext.cpp?rev=366365&r1=366364&r2=366365&view=diff ============================================================================== --- lldb/trunk/source/Symbol/ClangASTContext.cpp (original) +++ lldb/trunk/source/Symbol/ClangASTContext.cpp Wed Jul 17 13:16:13 2019 @@ -1615,10 +1615,11 @@ clang::FunctionTemplateDecl *ClangASTCon void ClangASTContext::CreateFunctionTemplateSpecializationInfo( FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl, const TemplateParameterInfos &infos) { - TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args); + TemplateArgumentList *template_args_ptr = + TemplateArgumentList::CreateCopy(func_decl->getASTContext(), infos.args); - func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args, - nullptr); + func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, + template_args_ptr, nullptr); } ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl( _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits