Skip to content
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

FileManager.isExecutableFile fails for wow64 processes #860

Open
jakepetroules opened this issue Aug 16, 2024 · 2 comments
Open

FileManager.isExecutableFile fails for wow64 processes #860

jakepetroules opened this issue Aug 16, 2024 · 2 comments
Assignees

Comments

@jakepetroules
Copy link

The following code, if compiled as an x86_64 process and run on an arm64 system, will claim that the command shell binary is not executable. This is because GetBinaryTypeW returns ERROR_BAD_EXE_FORMAT for wow64 processes.

This is not necessarily a bug in Foundation, but a quirk in the Win32 APIs. It seems worth documenting this caveat, at least.

import Foundation
import WinSDK

let fm = FileManager()
let path = "C:\\Windows\\system32\\cmd.exe"
print("\(path) isExecutable = \(fm.isExecutableFile(atPath: path))") // false

var dwBinaryType: DWORD = .max
let ret = path.withCString(encodedAs: UTF16.self) { ptr in
    GetBinaryTypeW(ptr, &dwBinaryType)
}
let err = GetLastError()
print("\(ret) \(dwBinaryType) \(err)")

// err == ERROR_BAD_EXE_FORMAT
@jmschonfeld jmschonfeld self-assigned this Aug 19, 2024
@jmschonfeld
Copy link
Contributor

Thanks for catching this, I wonder if there's an alternative API that we can be using here that would allow us to determine if the file is executable even if the architectures don't match (or perhaps if we call GetBinaryTypeW and the error is ERROR_BAD_EXE_FORMAT, does that indicate that it is executable and we should add a special case to return true in that scenario?

@jakepetroules
Copy link
Author

Apparently you'd have to parse the PE file manually: https://stackoverflow.com/a/44338280

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants