Skip to content

Fix a use-after-free in GetSwiftStdlibOSDir. #1132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lldb/include/lldb/Symbol/SwiftASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions lldb/source/Symbol/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<std::mutex> locker(g_mutex);
StringRef platform_sdk_path = GetPlatformSDKPath();
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -3437,7 +3437,7 @@ swift::ASTContext *SwiftASTContext::GetASTContext() {
// Compute the prebuilt module cache path to use:
// <resource-dir>/<platform>/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,
Expand Down
4 changes: 2 additions & 2 deletions lldb/unittests/Symbol/TestSwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
Expand Down