Skip to content

Commit 9fb9561

Browse files
committed
swift-inspect: clean up incorrect casts introduced in swiftlang#66973
Clean up some casts that would cause warnings on Windows introduced in PR swiftlang#66973. Rewrite some of the path computation for the DLL path to avoid some unnecessary conversions. Split out some of the merged guards to allow for split diagnostic emissions.
1 parent e89de6e commit 9fb9561

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

tools/swift-inspect/Sources/swift-inspect/WindowsRemoteProcess.swift

+33-33
Original file line numberDiff line numberDiff line change
@@ -393,41 +393,41 @@ internal final class WindowsRemoteProcess: RemoteProcess {
393393
}
394394

395395
private func allocateDllPathRemote() -> UnsafeMutableRawPointer? {
396-
// The path to the dll assuming it's in the same directory as swift-inspect.
397-
let swiftInspectPath = ProcessInfo.processInfo.arguments[0]
398-
return URL(fileURLWithPath: swiftInspectPath)
396+
URL(fileURLWithPath: ProcessInfo.processInfo.arguments[0])
399397
.deletingLastPathComponent()
400398
.appendingPathComponent("SwiftInspectClient.dll")
401-
.withUnsafeFileSystemRepresentation {
402-
#"\\?\\#(String(decodingCString: unsafeBitCast($0!, to: UnsafePointer<UInt8>.self), as: UTF8.self))"#
403-
.withCString(encodedAs: UTF16.self) {
404-
// Check that the dll file exists
405-
var faAttributes: WIN32_FILE_ATTRIBUTE_DATA = .init()
406-
guard GetFileAttributesExW($0, GetFileExInfoStandard, &faAttributes),
407-
faAttributes.dwFileAttributes & DWORD(FILE_ATTRIBUTE_REPARSE_POINT) == 0
408-
else {
409-
print("\($0) doesn't exist")
410-
return nil
411-
}
412-
// Allocate memory in the remote process
413-
let szLength = SIZE_T(wcslen($0) * MemoryLayout<WCHAR>.size + 1)
414-
guard
415-
let allocation = VirtualAllocEx(
416-
self.process, nil, szLength,
417-
DWORD(MEM_COMMIT), DWORD(PAGE_READWRITE))
418-
else {
419-
print("VirtualAllocEx failed \(GetLastError())")
420-
return nil
421-
}
422-
// Write the path in the allocated memory
423-
if !WriteProcessMemory(self.process, allocation, $0, szLength, nil) {
424-
print("WriteProcessMemory failed \(GetLastError())")
425-
VirtualFreeEx(self.process, allocation, 0, DWORD(MEM_RELEASE))
426-
return nil
427-
}
428-
429-
return allocation
399+
.path
400+
.withCString(encodedAs: UTF16.self) { pwszPath in
401+
let dwLength = GetFullPathNameW(pwszPath, 0, nil, nil)
402+
return withUnsafeTemporaryAllocation(of: WCHAR.self, capacity: Int(dwLength)) {
403+
guard GetFullPathNameW(pwszPath, dwLength, $0.baseAddress, nil) == dwLength - 1 else { return nil }
404+
405+
var faAttributes: WIN32_FILE_ATTRIBUTE_DATA = .init()
406+
guard GetFileAttributesExW($0.baseAddress, GetFileExInfoStandard, &faAttributes) else {
407+
print("\(String(decodingCString: $0.baseAddress!, as: UTF16.self)) doesn't exist")
408+
return nil
430409
}
410+
guard faAttributes.dwFileAttributes & DWORD(FILE_ATTRIBUTE_REPARSE_POINT) == 0 else {
411+
print("\(String(decodingCString: $0.baseAddress!, as: UTF16.self)) doesn't exist")
412+
return nil
413+
}
414+
415+
let szLength = SIZE_T(Int(dwLength) * MemoryLayout<WCHAR>.size)
416+
guard let pAllocation =
417+
VirtualAllocEx(self.process, nil, szLength,
418+
DWORD(MEM_COMMIT), DWORD(PAGE_READWRITE)) else {
419+
print("VirtualAllocEx failed \(GetLastError())")
420+
return nil
421+
}
422+
423+
if !WriteProcessMemory(self.process, pAllocation, $0.baseAddress, szLength, nil) {
424+
print("WriteProcessMemory failed \(GetLastError())")
425+
_ = VirtualFreeEx(self.process, pAllocation, 0, DWORD(MEM_RELEASE))
426+
return nil
427+
}
428+
429+
return pAllocation
430+
}
431431
}
432432
}
433433

@@ -450,7 +450,7 @@ internal final class WindowsRemoteProcess: RemoteProcess {
450450
// Unload the dll from the remote process
451451
let hUnloadThread = CreateRemoteThread(
452452
self.process, nil, 0, freeLibraryAddr,
453-
unsafeBitCast(hDllModule, to: LPVOID.self), 0, nil)
453+
UnsafeMutableRawPointer(hDllModule), 0, nil)
454454
if hUnloadThread == HANDLE(bitPattern: 0) {
455455
print("CreateRemoteThread for unload failed \(GetLastError())")
456456
return false

0 commit comments

Comments
 (0)