Skip to content

Commit

Permalink
Auto merge of #106610 - euclio:windows-rs, r=ChrisDenton
Browse files Browse the repository at this point in the history
migrate compiler, bootstrap and compiletest to windows-rs

This PR migrates the compiler, bootstrap, and compiletest to use [windows-rs](https://github.com/microsoft/windows-rs) instead of winapi-rs. windows-rs is the bindings crate provided by Microsoft, and is actively maintained compared to winapi-rs. Not all ecosystem crates have migrated over yet, so there will be a period of time where both crates are used.

windows-rs also provides some nice ergonomics over winapi-rs to convert return values to `Result`s (which found a case where we forgot to check the return value of `CreateFileW`).
  • Loading branch information
bors committed Mar 20, 2023
2 parents febd59e + bb7c373 commit 44f5180
Show file tree
Hide file tree
Showing 22 changed files with 379 additions and 280 deletions.
53 changes: 31 additions & 22 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ dependencies = [
"tracing-subscriber",
"unified-diff",
"walkdir",
"winapi",
"windows 0.46.0",
]

[[package]]
Expand Down Expand Up @@ -2273,7 +2273,7 @@ dependencies = [
"dirs",
"gix-path",
"libc",
"windows",
"windows 0.43.0",
]

[[package]]
Expand Down Expand Up @@ -4529,7 +4529,7 @@ dependencies = [
"tempfile",
"thin-vec",
"tracing",
"winapi",
"windows 0.46.0",
]

[[package]]
Expand Down Expand Up @@ -4588,7 +4588,7 @@ dependencies = [
"rustc_ty_utils",
"serde_json",
"tracing",
"winapi",
"windows 0.46.0",
]

[[package]]
Expand Down Expand Up @@ -4636,7 +4636,7 @@ dependencies = [
"termize",
"tracing",
"unicode-width",
"winapi",
"windows 0.46.0",
]

[[package]]
Expand Down Expand Up @@ -5277,7 +5277,7 @@ dependencies = [
"smallvec",
"termize",
"tracing",
"winapi",
"windows 0.46.0",
]

[[package]]
Expand Down Expand Up @@ -6908,6 +6908,15 @@ dependencies = [
"windows_x86_64_msvc",
]

[[package]]
name = "windows"
version = "0.46.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdacb41e6a96a052c6cb63a144f24900236121c6f63f4f8219fef5977ecb0c25"
dependencies = [
"windows-targets",
]

[[package]]
name = "windows-sys"
version = "0.42.0"
Expand All @@ -6934,9 +6943,9 @@ dependencies = [

[[package]]
name = "windows-targets"
version = "0.42.1"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7"
checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
Expand All @@ -6949,45 +6958,45 @@ dependencies = [

[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.1"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608"
checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"

[[package]]
name = "windows_aarch64_msvc"
version = "0.42.1"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7"
checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"

[[package]]
name = "windows_i686_gnu"
version = "0.42.1"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640"
checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"

[[package]]
name = "windows_i686_msvc"
version = "0.42.1"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605"
checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"

[[package]]
name = "windows_x86_64_gnu"
version = "0.42.1"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45"
checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"

[[package]]
name = "windows_x86_64_gnullvm"
version = "0.42.1"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463"
checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"

[[package]]
name = "windows_x86_64_msvc"
version = "0.42.1"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd"
checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"

[[package]]
name = "writeable"
Expand Down
11 changes: 9 additions & 2 deletions compiler/rustc_data_structures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,15 @@ elsa = "1.8"
[dependencies.parking_lot]
version = "0.11"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["fileapi", "psapi", "winerror"] }
[target.'cfg(windows)'.dependencies.windows]
version = "0.46.0"
features = [
"Win32_Foundation",
"Win32_Storage_FileSystem",
"Win32_System_IO",
"Win32_System_ProcessStatus",
"Win32_System_Threading",
]

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
memmap2 = "0.2.1"
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_data_structures/src/flock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
//! green/native threading. This is just a bare-bones enough solution for
//! librustdoc, it is not production quality at all.

#![allow(non_camel_case_types)]
#![allow(nonstandard_style)]

cfg_if! {
if #[cfg(target_os = "linux")] {
mod linux;
Expand All @@ -16,7 +13,7 @@ cfg_if! {
use unix as imp;
} else if #[cfg(windows)] {
mod windows;
use windows as imp;
use self::windows as imp;
} else {
mod unsupported;
use unsupported as imp;
Expand Down
64 changes: 38 additions & 26 deletions compiler/rustc_data_structures/src/flock/windows.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use std::fs::{File, OpenOptions};
use std::io;
use std::mem;
use std::os::windows::prelude::*;
use std::path::Path;

use winapi::shared::winerror::ERROR_INVALID_FUNCTION;
use winapi::um::fileapi::LockFileEx;
use winapi::um::minwinbase::{LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY, OVERLAPPED};
use winapi::um::winnt::{FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE};
use windows::{
Win32::Foundation::{ERROR_INVALID_FUNCTION, HANDLE},
Win32::Storage::FileSystem::{
LockFileEx, FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE, LOCKFILE_EXCLUSIVE_LOCK,
LOCKFILE_FAIL_IMMEDIATELY, LOCK_FILE_FLAGS,
},
Win32::System::IO::OVERLAPPED,
};

#[derive(Debug)]
pub struct Lock {
Expand All @@ -25,7 +28,7 @@ impl Lock {
let share_mode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;

let mut open_options = OpenOptions::new();
open_options.read(true).share_mode(share_mode);
open_options.read(true).share_mode(share_mode.0);

if create {
open_options.create(true).write(true);
Expand All @@ -43,33 +46,42 @@ impl Lock {
}
};

let ret = unsafe {
let mut overlapped: OVERLAPPED = mem::zeroed();
let mut flags = LOCK_FILE_FLAGS::default();
if !wait {
flags |= LOCKFILE_FAIL_IMMEDIATELY;
}

let mut dwFlags = 0;
if !wait {
dwFlags |= LOCKFILE_FAIL_IMMEDIATELY;
}
if exclusive {
flags |= LOCKFILE_EXCLUSIVE_LOCK;
}

if exclusive {
dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
}
let mut overlapped = OVERLAPPED::default();

debug!("attempting to acquire lock on lock file `{}`", p.display());
LockFileEx(file.as_raw_handle(), dwFlags, 0, 0xFFFF_FFFF, 0xFFFF_FFFF, &mut overlapped)
};
if ret == 0 {
let err = io::Error::last_os_error();
debug!("failed acquiring file lock: {}", err);
Err(err)
} else {
debug!("successfully acquired lock");
Ok(Lock { _file: file })
debug!("attempting to acquire lock on lock file `{}`", p.display());

unsafe {
LockFileEx(
HANDLE(file.as_raw_handle() as isize),
flags,
0,
u32::MAX,
u32::MAX,
&mut overlapped,
)
}
.ok()
.map_err(|e| {
let err = io::Error::from_raw_os_error(e.code().0);
debug!("failed acquiring file lock: {}", err);
err
})?;

debug!("successfully acquired lock");
Ok(Lock { _file: file })
}

pub fn error_unsupported(err: &io::Error) -> bool {
err.raw_os_error() == Some(ERROR_INVALID_FUNCTION as i32)
err.raw_os_error() == Some(ERROR_INVALID_FUNCTION.0 as i32)
}
}

Expand Down
33 changes: 19 additions & 14 deletions compiler/rustc_data_structures/src/profiling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,21 +796,26 @@ fn get_thread_id() -> u32 {
cfg_if! {
if #[cfg(windows)] {
pub fn get_resident_set_size() -> Option<usize> {
use std::mem::{self, MaybeUninit};
use winapi::shared::minwindef::DWORD;
use winapi::um::processthreadsapi::GetCurrentProcess;
use winapi::um::psapi::{GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS};

let mut pmc = MaybeUninit::<PROCESS_MEMORY_COUNTERS>::uninit();
match unsafe {
GetProcessMemoryInfo(GetCurrentProcess(), pmc.as_mut_ptr(), mem::size_of_val(&pmc) as DWORD)
} {
0 => None,
_ => {
let pmc = unsafe { pmc.assume_init() };
Some(pmc.WorkingSetSize as usize)
}
use std::mem;

use windows::{
Win32::System::ProcessStatus::{K32GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS},
Win32::System::Threading::GetCurrentProcess,
};

let mut pmc = PROCESS_MEMORY_COUNTERS::default();
let pmc_size = mem::size_of_val(&pmc);
unsafe {
K32GetProcessMemoryInfo(
GetCurrentProcess(),
&mut pmc,
pmc_size as u32,
)
}
.ok()
.ok()?;

Some(pmc.WorkingSetSize)
}
} else if #[cfg(target_os = "macos")] {
pub fn get_resident_set_size() -> Option<usize> {
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_driver_impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ rustc_hir_analysis = { path = "../rustc_hir_analysis" }
[target.'cfg(unix)'.dependencies]
libc = "0.2"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["consoleapi", "debugapi", "processenv"] }
[target.'cfg(windows)'.dependencies.windows]
version = "0.46.0"
features = [
"Win32_System_Diagnostics_Debug",
]

[features]
llvm = ['rustc_interface/llvm']
Expand Down
8 changes: 3 additions & 5 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1246,11 +1246,9 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
interface::try_print_query_stack(&handler, num_frames);

#[cfg(windows)]
unsafe {
if env::var("RUSTC_BREAK_ON_ICE").is_ok() {
// Trigger a debugger if we crashed during bootstrap
winapi::um::debugapi::DebugBreak();
}
if env::var("RUSTC_BREAK_ON_ICE").is_ok() {
// Trigger a debugger if we crashed during bootstrap
unsafe { windows::Win32::System::Diagnostics::Debug::DebugBreak() };
}
}

Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_errors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ termize = "0.1.1"
serde = { version = "1.0.125", features = [ "derive" ] }
serde_json = "1.0.59"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = [ "handleapi", "synchapi", "winbase" ] }
[target.'cfg(windows)'.dependencies.windows]
version = "0.46.0"
features = [
"Win32_Foundation",
"Win32_Security",
"Win32_System_Threading",
"Win32_System_WindowsProgramming",
]

[features]
rustc_use_parallel_compiler = ['rustc_error_messages/rustc_use_parallel_compiler']
Loading

0 comments on commit 44f5180

Please sign in to comment.