-
Notifications
You must be signed in to change notification settings - Fork 207
[windows] add a check for a matching python architecture #1942
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
Open
charles-zablit
wants to merge
1
commit into
swiftlang:main
Choose a base branch
from
charles-zablit:charles-zablit/windows/warn-architecture-mismatch-python
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import Foundation | ||
|
||
import struct TSCBasic.AbsolutePath | ||
import struct TSCBasic.ByteString | ||
import struct TSCBasic.Diagnostic | ||
import protocol TSCBasic.DiagnosticData | ||
import class TSCBasic.DiagnosticsEngine | ||
import protocol TSCBasic.FileSystem | ||
import protocol TSCBasic.OutputByteStream | ||
import typealias TSCBasic.ProcessEnvironmentBlock | ||
import func TSCBasic.getEnvSearchPaths | ||
import func TSCBasic.lookupExecutablePath | ||
|
||
/// Check that the architecture of the toolchain matches the architecture | ||
/// of the Python installation. | ||
/// | ||
/// When installing the x86 toolchain on ARM64 Windows, if the user does not | ||
/// install an x86 version of Python, they will get acryptic error message | ||
/// when running lldb (`0xC000007B`). Calling this function before invoking | ||
/// lldb gives them a warning to help troublshoot the issue. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: "troublshoot" mis-spelled There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed, thanks! |
||
/// | ||
/// - Parameters: | ||
/// - cwd: The current working directory. | ||
/// - env: A dictionary of the environment variables and their values. Usually of the parent shell. | ||
/// - diagnosticsEngine: DiagnosticsEngine instance to use for printing the warning. | ||
public func checkIfMatchingPythonArch( | ||
cwd: AbsolutePath?, envBlock: ProcessEnvironmentBlock, diagnosticsEngine: DiagnosticsEngine | ||
) throws { | ||
#if arch(arm64) | ||
let toolchainArchitecture = COFFBinaryExecutableArchitecture.arm64 | ||
#elseif arch(x86_64) | ||
let toolchainArchitecture = COFFBinaryExecutableArchitecture.x64 | ||
#elseif arch(x86) | ||
let toolchainArchitecture = COFFBinaryExecutableArchitecture.x86 | ||
#else | ||
return | ||
#endif | ||
let pythonArchitecture = COFFBinaryExecutableArchitecture.readWindowsExecutableArchitecture( | ||
cwd: cwd, envBlock: envBlock, filename: "python.exe") | ||
|
||
if toolchainArchitecture != pythonArchitecture { | ||
diagnosticsEngine.emit( | ||
.warning( | ||
""" | ||
There is an architecture mismatch between the installed toolchain and the resolved Python's architecture: | ||
Toolchain: \(toolchainArchitecture) | ||
Python: \(pythonArchitecture) | ||
""")) | ||
} | ||
} | ||
|
||
/// Some of the architectures that can be stored in a COFF header. | ||
enum COFFBinaryExecutableArchitecture: String { | ||
case x86 = "X86" | ||
case x64 = "X64" | ||
case arm64 = "ARM64" | ||
case unknown = "Unknown" | ||
|
||
static func fromPEMachineByte(machine: UInt16) -> Self { | ||
// https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#machine-types | ||
switch machine { | ||
case 0x014c: return .x86 | ||
case 0x8664: return .x64 | ||
case 0xAA64: return .arm64 | ||
default: return .unknown | ||
} | ||
} | ||
|
||
/// Resolves the filename from the `Path` environment variable and read its COFF header to determine the architecture | ||
/// of the binary. | ||
/// | ||
/// - Parameters: | ||
/// - cwd: The current working directory. | ||
/// - env: A dictionary of the environment variables and their values. Usually of the parent shell. | ||
/// - filename: The name of the file we are resolving the architecture of. | ||
/// - Returns: The architecture of the file which was found in the `Path`. | ||
static func readWindowsExecutableArchitecture( | ||
cwd: AbsolutePath?, envBlock: ProcessEnvironmentBlock, filename: String | ||
) -> Self { | ||
let searchPaths = getEnvSearchPaths(pathString: envBlock["Path"], currentWorkingDirectory: cwd) | ||
guard | ||
let filePath = lookupExecutablePath( | ||
filename: filename, currentWorkingDirectory: cwd, searchPaths: searchPaths) | ||
else { | ||
return .unknown | ||
} | ||
guard let fileHandle = FileHandle(forReadingAtPath: filePath.pathString) else { | ||
return .unknown | ||
} | ||
|
||
defer { fileHandle.closeFile() } | ||
|
||
// Infering the architecture of a Windows executable from its COFF header involves the following: | ||
// 1. Get the COFF header offset from the pointer located at the 0x3C offset (4 bytes long). | ||
// 2. Jump to that offset and read the next 6 bytes. | ||
// 3. The first 4 are the signature which should be equal to 0x50450000. | ||
// 4. The last 2 are the machine architecture which can be infered from the value we get. | ||
// | ||
// The link below provides a visualization of the COFF header and the process to get to it. | ||
// https://upload.wikimedia.org/wikipedia/commons/1/1b/Portable_Executable_32_bit_Structure_in_SVG_fixed.svg | ||
fileHandle.seek(toFileOffset: 0x3C) | ||
guard let offsetPointer = try? fileHandle.read(upToCount: 4), | ||
offsetPointer.count == 4 | ||
else { | ||
return .unknown | ||
} | ||
|
||
let peHeaderOffset = offsetPointer.withUnsafeBytes { $0.load(as: UInt32.self) } | ||
|
||
fileHandle.seek(toFileOffset: UInt64(peHeaderOffset)) | ||
guard let coffHeader = try? fileHandle.read(upToCount: 6), coffHeader.count == 6 else { | ||
return .unknown | ||
} | ||
|
||
let signature = coffHeader.prefix(4) | ||
let machineBytes = coffHeader.suffix(2) | ||
|
||
guard signature == Data([0x50, 0x45, 0x00, 0x00]) else { | ||
return .unknown | ||
} | ||
|
||
return .fromPEMachineByte(machine: machineBytes.withUnsafeBytes { $0.load(as: UInt16.self) }) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: "acryptic" => "a cryptic"
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, thanks!