From 1f0614963c2cec3541d656362f4eae0965e3f0e1 Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Mon, 27 Apr 2020 17:04:20 -0700 Subject: [PATCH 1/3] Fix use-after-free --- lldb/include/lldb/Symbol/SwiftASTContext.h | 2 +- lldb/source/Symbol/SwiftASTContext.cpp | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lldb/include/lldb/Symbol/SwiftASTContext.h b/lldb/include/lldb/Symbol/SwiftASTContext.h index bcdd62a59a5bd..48796aca9b5ce 100644 --- a/lldb/include/lldb/Symbol/SwiftASTContext.h +++ b/lldb/include/lldb/Symbol/SwiftASTContext.h @@ -1228,7 +1228,7 @@ class SwiftASTContext : public TypeSystemSwift { void RemapClangImporterOptions(const PathMappingList &path_map); /// Infer the appropriate Swift resource directory for a target triple. - llvm::StringRef GetResourceDir(const llvm::Triple &target); + std::string GetResourceDir(const llvm::Triple &target); /// Implementation of \c GetResourceDir. static std::string GetResourceDir(llvm::StringRef platform_sdk_path, diff --git a/lldb/source/Symbol/SwiftASTContext.cpp b/lldb/source/Symbol/SwiftASTContext.cpp index e0f507acc05bf..2e4a7c3717cb1 100644 --- a/lldb/source/Symbol/SwiftASTContext.cpp +++ b/lldb/source/Symbol/SwiftASTContext.cpp @@ -1006,7 +1006,7 @@ StringRef SwiftASTContext::GetSwiftStdlibOSDir(const llvm::Triple &target, return target.getOSName(); } -StringRef SwiftASTContext::GetResourceDir(const llvm::Triple &triple) { +std::string SwiftASTContext::GetResourceDir(const llvm::Triple &triple) { static std::mutex g_mutex; std::lock_guard locker(g_mutex); StringRef platform_sdk_path = GetPlatformSDKPath(); @@ -1706,7 +1706,7 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language, } triple = swift_ast_sp->GetTriple(); - StringRef resource_dir = swift_ast_sp->GetResourceDir(triple); + std::string resource_dir = swift_ast_sp->GetResourceDir(triple); ConfigureResourceDirs(swift_ast_sp->GetCompilerInvocation(), FileSpec(resource_dir), triple); @@ -2014,7 +2014,7 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language, } llvm::Triple triple = swift_ast_sp->GetTriple(); - StringRef resource_dir = swift_ast_sp->GetResourceDir(triple); + std::string resource_dir = swift_ast_sp->GetResourceDir(triple); ConfigureResourceDirs(swift_ast_sp->GetCompilerInvocation(), FileSpec(resource_dir), triple); @@ -2679,7 +2679,7 @@ void SwiftASTContext::InitializeSearchPathOptions( } llvm::Triple triple(GetTriple()); - StringRef resource_dir = GetResourceDir(triple); + std::string resource_dir = GetResourceDir(triple); ConfigureResourceDirs(GetCompilerInvocation(), FileSpec(resource_dir), triple); @@ -3437,7 +3437,7 @@ swift::ASTContext *SwiftASTContext::GetASTContext() { // Compute the prebuilt module cache path to use: // //prebuilt-modules llvm::Triple triple(GetTriple()); - llvm::SmallString<128> prebuiltModuleCachePath = GetResourceDir(triple); + llvm::SmallString<128> prebuiltModuleCachePath(GetResourceDir(triple)); StringRef platform; if (swift::tripleIsMacCatalystEnvironment(triple)) { // The prebuilt cache for macCatalyst is the same as the one for macOS, From cfc721091d75ece18adfcbea5b724a88aa1a92f6 Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Mon, 27 Apr 2020 17:18:11 -0700 Subject: [PATCH 2/3] Revert "Fix use-after-free" This reverts commit 1f0614963c2cec3541d656362f4eae0965e3f0e1. --- lldb/include/lldb/Symbol/SwiftASTContext.h | 2 +- lldb/source/Symbol/SwiftASTContext.cpp | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lldb/include/lldb/Symbol/SwiftASTContext.h b/lldb/include/lldb/Symbol/SwiftASTContext.h index 48796aca9b5ce..bcdd62a59a5bd 100644 --- a/lldb/include/lldb/Symbol/SwiftASTContext.h +++ b/lldb/include/lldb/Symbol/SwiftASTContext.h @@ -1228,7 +1228,7 @@ class SwiftASTContext : public TypeSystemSwift { void RemapClangImporterOptions(const PathMappingList &path_map); /// Infer the appropriate Swift resource directory for a target triple. - std::string GetResourceDir(const llvm::Triple &target); + llvm::StringRef GetResourceDir(const llvm::Triple &target); /// Implementation of \c GetResourceDir. static std::string GetResourceDir(llvm::StringRef platform_sdk_path, diff --git a/lldb/source/Symbol/SwiftASTContext.cpp b/lldb/source/Symbol/SwiftASTContext.cpp index 2e4a7c3717cb1..e0f507acc05bf 100644 --- a/lldb/source/Symbol/SwiftASTContext.cpp +++ b/lldb/source/Symbol/SwiftASTContext.cpp @@ -1006,7 +1006,7 @@ StringRef SwiftASTContext::GetSwiftStdlibOSDir(const llvm::Triple &target, return target.getOSName(); } -std::string SwiftASTContext::GetResourceDir(const llvm::Triple &triple) { +StringRef SwiftASTContext::GetResourceDir(const llvm::Triple &triple) { static std::mutex g_mutex; std::lock_guard locker(g_mutex); StringRef platform_sdk_path = GetPlatformSDKPath(); @@ -1706,7 +1706,7 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language, } triple = swift_ast_sp->GetTriple(); - std::string resource_dir = swift_ast_sp->GetResourceDir(triple); + StringRef resource_dir = swift_ast_sp->GetResourceDir(triple); ConfigureResourceDirs(swift_ast_sp->GetCompilerInvocation(), FileSpec(resource_dir), triple); @@ -2014,7 +2014,7 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language, } llvm::Triple triple = swift_ast_sp->GetTriple(); - std::string resource_dir = swift_ast_sp->GetResourceDir(triple); + StringRef resource_dir = swift_ast_sp->GetResourceDir(triple); ConfigureResourceDirs(swift_ast_sp->GetCompilerInvocation(), FileSpec(resource_dir), triple); @@ -2679,7 +2679,7 @@ void SwiftASTContext::InitializeSearchPathOptions( } llvm::Triple triple(GetTriple()); - std::string resource_dir = GetResourceDir(triple); + StringRef resource_dir = GetResourceDir(triple); ConfigureResourceDirs(GetCompilerInvocation(), FileSpec(resource_dir), triple); @@ -3437,7 +3437,7 @@ swift::ASTContext *SwiftASTContext::GetASTContext() { // Compute the prebuilt module cache path to use: // //prebuilt-modules llvm::Triple triple(GetTriple()); - llvm::SmallString<128> prebuiltModuleCachePath(GetResourceDir(triple)); + llvm::SmallString<128> prebuiltModuleCachePath = GetResourceDir(triple); StringRef platform; if (swift::tripleIsMacCatalystEnvironment(triple)) { // The prebuilt cache for macCatalyst is the same as the one for macOS, From 26cda253323814298ebabc6a91650d15a2b9fd3d Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Mon, 27 Apr 2020 17:21:16 -0700 Subject: [PATCH 3/3] Fix a use-after-free in GetSwiftStdlibOSDir. XcodeSDK::GetCanonicalName() no longer returns a constant string. --- lldb/include/lldb/Symbol/SwiftASTContext.h | 4 ++-- lldb/source/Symbol/SwiftASTContext.cpp | 4 ++-- lldb/unittests/Symbol/TestSwiftASTContext.cpp | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lldb/include/lldb/Symbol/SwiftASTContext.h b/lldb/include/lldb/Symbol/SwiftASTContext.h index bcdd62a59a5bd..144380e95be4d 100644 --- a/lldb/include/lldb/Symbol/SwiftASTContext.h +++ b/lldb/include/lldb/Symbol/SwiftASTContext.h @@ -1240,8 +1240,8 @@ class SwiftASTContext : public TypeSystemSwift { /// Return the name of the OS-specific subdirectory containing the /// Swift stdlib needed for \p target. - static llvm::StringRef GetSwiftStdlibOSDir(const llvm::Triple &target, - const llvm::Triple &host); + static std::string GetSwiftStdlibOSDir(const llvm::Triple &target, + const llvm::Triple &host); }; class SwiftASTContextForExpressions : public SwiftASTContext { diff --git a/lldb/source/Symbol/SwiftASTContext.cpp b/lldb/source/Symbol/SwiftASTContext.cpp index e0f507acc05bf..e2240cefe267d 100644 --- a/lldb/source/Symbol/SwiftASTContext.cpp +++ b/lldb/source/Symbol/SwiftASTContext.cpp @@ -995,8 +995,8 @@ static SDKTypeMinVersion GetSDKType(const llvm::Triple &target, /// Return the name of the OS-specific subdirectory containing the /// Swift stdlib needed for \p target. -StringRef SwiftASTContext::GetSwiftStdlibOSDir(const llvm::Triple &target, - const llvm::Triple &host) { +std::string SwiftASTContext::GetSwiftStdlibOSDir(const llvm::Triple &target, + const llvm::Triple &host) { auto sdk = GetSDKType(target, host); XcodeSDK::Info sdk_info; sdk_info.type = sdk.sdk_type; diff --git a/lldb/unittests/Symbol/TestSwiftASTContext.cpp b/lldb/unittests/Symbol/TestSwiftASTContext.cpp index 49f301400300b..57da58220ad59 100644 --- a/lldb/unittests/Symbol/TestSwiftASTContext.cpp +++ b/lldb/unittests/Symbol/TestSwiftASTContext.cpp @@ -71,8 +71,8 @@ struct SwiftASTContextTester : public SwiftASTContext { platform_sdk_path, swift_dir, swift_stdlib_os_dir, xcode_contents_path, toolchain_path, cl_tools_path); } - static llvm::StringRef GetSwiftStdlibOSDir(const llvm::Triple &target, - const llvm::Triple &host) { + static std::string GetSwiftStdlibOSDir(const llvm::Triple &target, + const llvm::Triple &host) { return SwiftASTContext::GetSwiftStdlibOSDir(target, host); } };