Skip to content

Commit 255beb3

Browse files
authored
Merge pull request #67334 from compnerd/66973
swift-inspect: clean up incorrect casts introduced in #66973
2 parents c6fbff4 + 9fb9561 commit 255beb3

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
@@ -392,41 +392,41 @@ internal final class WindowsRemoteProcess: RemoteProcess {
392392
}
393393

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

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

0 commit comments

Comments
 (0)