Skip to content

Deserialize Swift macro implementation paths from Swift modules #6753

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 1 commit into from
May 4, 2023
Merged
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
174 changes: 141 additions & 33 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,34 @@ void SwiftASTContext::DiagnoseWarnings(Process &process, Module &module) const {
process.PrintWarningCantLoadSwiftModule(module, message);
}

/// Locate the swift-plugin-server for a plugin library,
/// by converting ${toolchain}/usr/(local)?/lib/swift/host/plugins
/// into ${toolchain}/usr/bin/swift-plugin-server
/// FIXME: move this to Host, it may be platform-specific.
static std::string GetPluginServer(llvm::StringRef plugin_library_path) {
llvm::StringRef path = llvm::sys::path::parent_path(plugin_library_path);
if (llvm::sys::path::filename(path) != "plugins")
return {};
path = llvm::sys::path::parent_path(path);
if (llvm::sys::path::filename(path) != "host")
return {};
path = llvm::sys::path::parent_path(path);
if (llvm::sys::path::filename(path) != "swift")
return {};
path = llvm::sys::path::parent_path(path);
if (llvm::sys::path::filename(path) != "lib")
return {};
path = llvm::sys::path::parent_path(path);
if (llvm::sys::path::filename(path) == "local")
path = llvm::sys::path::parent_path(path);
llvm::SmallString<256> server(path);
llvm::sys::path::append(server, "bin", "swift-plugin-server");
std::string result(server);
if (FileSystem::Instance().Exists(result))
return result;
return {};
}

/// Retrieve the serialized AST data blobs and initialize the compiler
/// invocation with the concatenated search paths from the blobs.
/// \returns true if an error was encountered.
Expand All @@ -1076,21 +1104,31 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation,
return false;

auto &search_path_options = invocation.getSearchPathOptions();
std::vector<std::string> import_search_paths;
llvm::StringSet<> known_import_search_paths;
for (auto &path : search_path_options.getImportSearchPaths()) {
import_search_paths.push_back(path);
known_import_search_paths.insert(path);
}

std::vector<swift::SearchPathOptions::FrameworkSearchPath>
framework_search_paths;
llvm::StringSet<> known_framework_search_paths;
for (auto &path : search_path_options.getFrameworkSearchPaths()) {
framework_search_paths.push_back(path);
known_framework_search_paths.insert(path.Path);
}

#define INIT_SEARCH_PATH_SET(TYPE, ACCESSOR, NAME, KEY) \
std::vector<TYPE> NAME; \
llvm::StringSet<> known_##NAME; \
for (auto &path : search_path_options.ACCESSOR) { \
NAME.push_back(path); \
known_##NAME.insert(path KEY); \
}

INIT_SEARCH_PATH_SET(std::string, getImportSearchPaths(),
import_search_paths, );
INIT_SEARCH_PATH_SET(swift::SearchPathOptions::FrameworkSearchPath,
getFrameworkSearchPaths(), framework_search_paths,
.Path);
INIT_SEARCH_PATH_SET(std::string, PluginSearchPaths, plugin_search_paths, );
INIT_SEARCH_PATH_SET(swift::ExternalPluginSearchPathAndServerPath,
ExternalPluginSearchPaths, external_plugin_search_paths,
.SearchPath);
INIT_SEARCH_PATH_SET(std::string, getCompilerPluginLibraryPaths(),
compiler_plugin_library_paths, );
INIT_SEARCH_PATH_SET(swift::PluginExecutablePathAndModuleNames,
getCompilerPluginExecutablePaths(),
compiler_plugin_executable_paths, .ExecutablePath);


// An AST section consists of one or more AST modules, optionally
// with headers. Iterate over all AST modules.
for (auto ast_file_data_sp : ast_file_datas) {
Expand Down Expand Up @@ -1133,27 +1171,89 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation,
/// serialized AST.
auto deserializeCompilerFlags = [&]() -> bool {
auto result = invocation.loadFromSerializedAST(moduleData);
if (result == swift::serialization::Status::Valid) {
if (discover_implicit_search_paths) {
for (auto &searchPath : searchPaths) {
std::string path = remap(searchPath.Path);
if (!searchPath.IsFramework) {
if (known_import_search_paths.insert(path).second)
import_search_paths.push_back(path);
} else {
swift::SearchPathOptions::FrameworkSearchPath
framework_search_path(path, searchPath.IsSystem);
if (known_framework_search_paths.insert(path).second)
framework_search_paths.push_back(framework_search_path);
}
if (result != swift::serialization::Status::Valid) {
error << "Could not deserialize " << info.name << ":\n"
<< getImportFailureString(result) << "\n";
return false;
}
if (discover_implicit_search_paths) {
for (auto &searchPath : searchPaths) {
std::string path = remap(searchPath.Path);
if (!searchPath.IsFramework) {
if (known_import_search_paths.insert(path).second)
import_search_paths.push_back(path);
} else {
swift::SearchPathOptions::FrameworkSearchPath
framework_search_path(path, searchPath.IsSystem);
if (known_framework_search_paths.insert(path).second)
framework_search_paths.push_back(framework_search_path);
}
}
return true;
}
auto exists = [&](llvm::StringRef path) {
if (FileSystem::Instance().Exists(path))
return true;
HEALTH_LOG_PRINTF("Ignoring missing Swift plugin at path: %s",
path.str().c_str());
return false;
};

// Discover, rewrite, and unique compiler plugin paths.
for (auto path : extended_validation_info.getPluginSearchPaths()) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we worried about reading invalid local paths in distributed swiftmodules? LLDB could read paths to local macros from a different machine/configuration, it could make LLDB load undesired macro plugins on different machines. Similar to the issue we have with the search paths pointing to /Applications/Xcode.app/.

If so, LLDB could apply those paths only for local non-distributed module using the isNonUserModule check, if a module is non-user it's likely from the SDK, so we could ignore those paths. Alternatively, if we expect macros to be in the SDK we could filter out search paths for distributed modules that point outside the SDK.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is isNonUserModule encoded in the validation info somewhere? I couldn't find it on first glance.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is available on ModuleDecl and ModuleEntity only at this time, but it only checks the path of the module file iirc so it should be possible to bring it down in the module loading logic as needed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We no longer have the original path to the .swiftmodule file when debugging from a .dSYM where all .swiftmodule files have been concatenated into the __swift_ast section of the symbol-rich binary.

// System plugins shipping with the compiler.
// Rewrite them to go through an ABI-compatible swift-plugin-server.
if (known_plugin_search_paths.insert(path).second) {
if (known_external_plugin_search_paths.insert(path).second) {
std::string server = GetPluginServer(path);
if (server.empty()) {
HEALTH_LOG_PRINTF("Could not find swift-plugin-server for %s",
path.str().c_str());
continue;
}
if (exists(path))
external_plugin_search_paths.push_back({path.str(), server});
}
}
for (auto path :
extended_validation_info.getExternalPluginSearchPaths()) {
// Sandboxed system plugins shipping with some compiler.
// Keep the original plugin server path, it needs to be ABI
// compatible with the version of SwiftSyntax used by the plugin.
auto plugin_server = path.split('#');
llvm::StringRef plugin = plugin_server.first;
llvm::StringRef server = plugin_server.second;
if (known_external_plugin_search_paths.insert(plugin).second)
if (exists(plugin) && exists(server))
external_plugin_search_paths.push_back(
{plugin.str(), server.str()});
}

error << "Could not deserialize " << info.name << ":\n"
<< getImportFailureString(result) << "\n";
return false;
for (auto path :
extended_validation_info.getCompilerPluginLibraryPaths()) {
// Compiler plugin libraries.
if (known_compiler_plugin_library_paths.insert(path).second)
if (exists(path))
compiler_plugin_library_paths.push_back(path.str());
}

for (auto path :
extended_validation_info.getCompilerPluginExecutablePaths()) {
// Compiler plugin executables.
auto plugin_modules = path.split('#');
llvm::StringRef plugin = plugin_modules.first;
llvm::StringRef modules_list = plugin_modules.second;
llvm::SmallVector<llvm::StringRef, 0> modules;
modules_list.split(modules, ",");
std::vector<std::string> modules_vec;
for (auto m : modules)
modules_vec.push_back(m.str());
if (known_compiler_plugin_executable_paths.insert(path).second)
if (exists(plugin))
compiler_plugin_executable_paths.push_back(
{plugin.str(), modules_vec});
}
}
return true;
};

got_serialized_options |= deserializeCompilerFlags();
Expand All @@ -1166,8 +1266,16 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation,
}
}

search_path_options.setImportSearchPaths(import_search_paths);
search_path_options.setFrameworkSearchPaths(framework_search_paths);
search_path_options.setImportSearchPaths(std::move(import_search_paths));
search_path_options.setFrameworkSearchPaths(
std::move(framework_search_paths));
// (All PluginSearchPaths were rewritten to be external.)
search_path_options.ExternalPluginSearchPaths =
std::move(external_plugin_search_paths);
search_path_options.setCompilerPluginLibraryPaths(
std::move(compiler_plugin_library_paths));
search_path_options.setCompilerPluginExecutablePaths(
std::move(compiler_plugin_executable_paths));
return found_validation_errors;
}

Expand Down