Skip to content

Compile manifests instead of interpreting them #2518

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
Jan 23, 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
2 changes: 2 additions & 0 deletions Sources/PackageDescription/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ foreach(PACKAGE_DESCRIPTION_VERSION 4 4_2)
$<$<COMPILE_LANGUAGE:Swift>:-enable-library-evolution>)
target_compile_options(PD${PACKAGE_DESCRIPTION_VERSION} PUBLIC
$<$<COMPILE_LANGUAGE:Swift>:-emit-module-interface-path$<SEMICOLON>${SWIFT_INTERFACE_PATH}>)
target_link_options(PD${PACKAGE_DESCRIPTION_VERSION} PRIVATE
"SHELL:-Xlinker -install_name -Xlinker @rpath/libPackageDescription.dylib")
endif()

set_target_properties(PD${PACKAGE_DESCRIPTION_VERSION} PROPERTIES
Expand Down
120 changes: 67 additions & 53 deletions Sources/PackageLoading/ManifestLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -531,39 +531,34 @@ public final class ManifestLoader: ManifestLoaderProtocol {

// Compute the path to runtime we need to load.
let runtimePath = self.runtimePath(for: toolsVersion)
let interpreterFlags = self.interpreterFlags(for: toolsVersion)
let compilerFlags = self.interpreterFlags(for: toolsVersion)

// FIXME: Workaround for the module cache bug that's been haunting Swift CI
// <rdar://problem/48443680>
let moduleCachePath = ProcessEnv.vars["SWIFTPM_MODULECACHE_OVERRIDE"] ?? ProcessEnv.vars["SWIFTPM_TESTS_MODULECACHE"]

var cmd = [String]()
#if os(macOS)
// If enabled, use sandbox-exec on macOS. This provides some safety against
// arbitrary code execution when parsing manifest files. We only allow
// the permissions which are absolutely necessary for manifest parsing.
if isManifestSandboxEnabled {
let cacheDirs = [
cacheDir,
moduleCachePath.map{ AbsolutePath($0) }
].compactMap{$0}
cmd += ["sandbox-exec", "-p", sandboxProfile(cacheDirs)]
}
#endif
var cmd: [String] = []
cmd += [resources.swiftCompiler.pathString]
cmd += ["--driver-mode=swift"]
cmd += verbosity.ccArgs

// If we got the binDir that means we could be developing SwiftPM in Xcode
// which produces a framework for dynamic package products.
let runtimeFrameworkPath = runtimePath.appending(component: "PackageFrameworks")
if resources.binDir != nil, localFileSystem.exists(runtimeFrameworkPath) {
cmd += ["-F", runtimeFrameworkPath.pathString, "-framework", "PackageDescription"]
let packageFrameworkPath = runtimePath.appending(component: "PackageFrameworks")
if resources.binDir != nil, localFileSystem.exists(packageFrameworkPath) {
cmd += [
"-F", packageFrameworkPath.pathString,
"-framework", "PackageDescription",
"-Xlinker", "-rpath", "-Xlinker", packageFrameworkPath.pathString,
]
} else {
cmd += ["-L", runtimePath.pathString, "-lPackageDescription"]
cmd += [
"-L", runtimePath.pathString,
"-lPackageDescription",
"-Xlinker", "-rpath", "-Xlinker", runtimePath.pathString
]
}

cmd += interpreterFlags
cmd += compilerFlags
if let moduleCachePath = moduleCachePath {
cmd += ["-module-cache-path", moduleCachePath]
}
Expand All @@ -579,36 +574,50 @@ public final class ManifestLoader: ManifestLoaderProtocol {

cmd += [manifestPath.pathString]

// Create and open a temporary file to write json to.
try withTemporaryFile { file in
// Set path to compiled manifest executable.
cmd += ["-o", file.path.pathString]

try Process.popen(arguments: cmd)

// Compile the manifest.
let compilerResult = try Process.popen(arguments: cmd)
let compilerOutput = try (compilerResult.utf8Output() + compilerResult.utf8stderrOutput()).spm_chuzzle()
manifestParseResult.compilerOutput = compilerOutput

// Return now if there was an error.
if compilerResult.exitStatus != .terminated(code: 0) {
return
}

// Pass the fd in arguments.
cmd += ["-fileno", "\(file.fileHandle.fileDescriptor)"]

// Prefer swiftinterface if both swiftmodule and swiftinterface files are present.
//
// This will avoid failures during incremental builds when the
// slate swiftmodule file is still present from the previous
// install. We should be able to remove this after some
// transition period.
var env = ProcessEnv.vars
cmd = [file.path.pathString, "-fileno", "1"]

#if os(macOS)
env["SWIFT_FORCE_MODULE_LOADING"] = "prefer-parseable"
// If enabled, use sandbox-exec on macOS. This provides some safety against
// arbitrary code execution when parsing manifest files. We only allow
// the permissions which are absolutely necessary for manifest parsing.
if isManifestSandboxEnabled {
let cacheDirectories = [
cacheDir,
moduleCachePath.map({ AbsolutePath($0) })
].compactMap({ $0 })
let profile = sandboxProfile(toolsVersion: toolsVersion, cacheDirectories: cacheDirectories)
cmd += ["sandbox-exec", "-p", profile]
}
#endif

// Run the command.
let result = try Process.popen(arguments: cmd, environment: env)
let output = try (result.utf8Output() + result.utf8stderrOutput()).spm_chuzzle()
manifestParseResult.compilerOutput = output
let runResult = try Process.popen(arguments: cmd)
let runOutput = try (runResult.utf8Output() + runResult.utf8stderrOutput()).spm_chuzzle()

// Return now if there was an error.
if result.exitStatus != .terminated(code: 0) {
if runResult.exitStatus != .terminated(code: 0) {
manifestParseResult.errorOutput = runOutput
return
}

guard let json = try localFileSystem.readFileContents(file.path).validDescription else {
throw StringError("the manifest has invalid encoding")
}
manifestParseResult.parsedManifest = json
manifestParseResult.parsedManifest = runOutput
}
}

Expand Down Expand Up @@ -670,10 +679,10 @@ public final class ManifestLoader: ManifestLoaderProtocol {
cmd += ["-I", runtimePath.pathString]
#if os(macOS)
cmd += ["-target", "x86_64-apple-macosx10.10"]
#endif
if let sdkRoot = resources.sdkRoot ?? self.sdkRoot() {
cmd += ["-sdk", sdkRoot.pathString]
}
#endif
cmd += ["-package-description-version", toolsVersion.description]
return cmd
}
Expand Down Expand Up @@ -705,26 +714,31 @@ public final class ManifestLoader: ManifestLoaderProtocol {
}

/// Returns the sandbox profile to be used when parsing manifest on macOS.
private func sandboxProfile(_ cacheDirs: [AbsolutePath] = []) -> String {
private func sandboxProfile(toolsVersion: ToolsVersion, cacheDirectories: [AbsolutePath] = []) -> String {
let stream = BufferedOutputByteStream()
stream <<< "(version 1)" <<< "\n"
// Deny everything by default.
stream <<< "(deny default)" <<< "\n"
// Import the system sandbox profile.
stream <<< "(import \"system.sb\")" <<< "\n"
// Allow reading all files.
stream <<< "(allow file-read*)" <<< "\n"
// These are required by the Swift compiler.
stream <<< "(allow process*)" <<< "\n"
stream <<< "(allow sysctl*)" <<< "\n"
// Allow writing in temporary locations.
stream <<< "(allow file-write*" <<< "\n"
for directory in Platform.darwinCacheDirectories() {
stream <<< " (regex #\"^\(directory.pathString)/org\\.llvm\\.clang.*\")" <<< "\n"
}
for cacheDir in cacheDirs {
stream <<< " (subpath \"\(cacheDir.pathString)\")" <<< "\n"

// The following accesses are only needed when interpreting the manifest (versus running a compiled version).
if toolsVersion < .vNext {
// Allow reading all files.
stream <<< "(allow file-read*)" <<< "\n"
// These are required by the Swift compiler.
stream <<< "(allow process*)" <<< "\n"
stream <<< "(allow sysctl*)" <<< "\n"
// Allow writing in temporary locations.
stream <<< "(allow file-write*" <<< "\n"
for directory in Platform.darwinCacheDirectories() {
stream <<< " (regex #\"^\(directory.pathString)/org\\.llvm\\.clang.*\")" <<< "\n"
}
for directory in cacheDirectories {
stream <<< " (subpath \"\(directory.pathString)\")" <<< "\n"
}
}

stream <<< ")" <<< "\n"
return stream.bytes.description
}
Expand Down
1 change: 1 addition & 0 deletions Sources/PackageModel/ToolsVersion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public struct ToolsVersion: CustomStringConvertible, Comparable, Hashable, Codab
public static let v4_2 = ToolsVersion(version: "4.2.0")
public static let v5 = ToolsVersion(version: "5.0.0")
public static let v5_2 = ToolsVersion(version: "5.2.0")
public static let vNext = ToolsVersion(version: "999.0.0")

/// The current tools version in use.
public static let currentToolsVersion = ToolsVersion(string:
Expand Down