Skip to content

Commit 38847ae

Browse files
committed
Auto merge of #3239 - beepster4096:windows_sys_tests, r=RalfJung
Use `windows-sys` in windows tests This PR adds `windows-sys` to `test_dependencies` so that we don't have to write out windows api bindings for each test.
2 parents 8bec0a5 + 109ada8 commit 38847ae

14 files changed

+52
-80
lines changed

src/tools/miri/test_dependencies/Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ dependencies = [
204204
"rand",
205205
"tempfile",
206206
"tokio",
207+
"windows-sys 0.52.0",
207208
]
208209

209210
[[package]]

src/tools/miri/test_dependencies/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ rand = { version = "0.8", features = ["small_rng"] }
2121
page_size = "0.6"
2222
tokio = { version = "1.24", features = ["full"] }
2323

24+
[target.'cfg(windows)'.dependencies]
25+
windows-sys = { version = "0.52", features = [ "Win32_Foundation", "Win32_System_Threading" ] }
26+
2427
[workspace]

src/tools/miri/tests/fail/concurrency/windows_join_detached.rs src/tools/miri/tests/fail-dep/concurrency/windows_join_detached.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@
33

44
// Joining a detached thread is undefined behavior.
55

6-
use std::os::windows::io::{AsRawHandle, RawHandle};
6+
use std::os::windows::io::AsRawHandle;
77
use std::thread;
88

9-
extern "system" {
10-
fn CloseHandle(handle: RawHandle) -> u32;
11-
}
9+
use windows_sys::Win32::Foundation::{CloseHandle, HANDLE};
1210

1311
fn main() {
1412
let thread = thread::spawn(|| ());
1513

1614
unsafe {
17-
assert_ne!(CloseHandle(thread.as_raw_handle()), 0);
15+
assert_ne!(CloseHandle(thread.as_raw_handle() as HANDLE), 0);
1816
}
1917

2018
thread.join().unwrap();

src/tools/miri/tests/fail/concurrency/windows_join_main.rs src/tools/miri/tests/fail-dep/concurrency/windows_join_main.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,18 @@
66

77
use std::thread;
88

9-
extern "system" {
10-
fn WaitForSingleObject(handle: isize, timeout: u32) -> u32;
11-
}
12-
13-
const INFINITE: u32 = u32::MAX;
9+
use windows_sys::Win32::Foundation::{HANDLE, WAIT_OBJECT_0};
10+
use windows_sys::Win32::System::Threading::{WaitForSingleObject, INFINITE};
1411

1512
// XXX HACK: This is how miri represents the handle for thread 0.
1613
// This value can be "legitimately" obtained by using `GetCurrentThread` with `DuplicateHandle`
1714
// but miri does not implement `DuplicateHandle` yet.
18-
const MAIN_THREAD: isize = (2i32 << 30) as isize;
15+
const MAIN_THREAD: HANDLE = (2i32 << 30) as HANDLE;
1916

2017
fn main() {
2118
thread::spawn(|| {
2219
unsafe {
23-
assert_eq!(WaitForSingleObject(MAIN_THREAD, INFINITE), 0); //~ ERROR: deadlock: the evaluated program deadlocked
20+
assert_eq!(WaitForSingleObject(MAIN_THREAD, INFINITE), WAIT_OBJECT_0); //~ ERROR: deadlock: the evaluated program deadlocked
2421
}
2522
})
2623
.join()

src/tools/miri/tests/fail/concurrency/windows_join_main.stderr src/tools/miri/tests/fail-dep/concurrency/windows_join_main.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: deadlock: the evaluated program deadlocked
22
--> $DIR/windows_join_main.rs:LL:CC
33
|
4-
LL | assert_eq!(WaitForSingleObject(MAIN_THREAD, INFINITE), 0);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program deadlocked
4+
LL | assert_eq!(WaitForSingleObject(MAIN_THREAD, INFINITE), WAIT_OBJECT_0);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program deadlocked
66
|
77
= note: inside closure at RUSTLIB/core/src/macros/mod.rs:LL:CC
88
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)

src/tools/miri/tests/fail/concurrency/windows_join_self.rs src/tools/miri/tests/fail-dep/concurrency/windows_join_self.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@
66

77
use std::thread;
88

9-
extern "system" {
10-
fn GetCurrentThread() -> usize;
11-
fn WaitForSingleObject(handle: usize, timeout: u32) -> u32;
12-
}
13-
14-
const INFINITE: u32 = u32::MAX;
9+
use windows_sys::Win32::Foundation::WAIT_OBJECT_0;
10+
use windows_sys::Win32::System::Threading::{GetCurrentThread, WaitForSingleObject, INFINITE};
1511

1612
fn main() {
1713
thread::spawn(|| {
1814
unsafe {
1915
let native = GetCurrentThread();
20-
assert_eq!(WaitForSingleObject(native, INFINITE), 0); //~ ERROR: deadlock: the evaluated program deadlocked
16+
assert_eq!(WaitForSingleObject(native, INFINITE), WAIT_OBJECT_0); //~ ERROR: deadlock: the evaluated program deadlocked
2117
}
2218
})
2319
.join()

src/tools/miri/tests/fail/concurrency/windows_join_self.stderr src/tools/miri/tests/fail-dep/concurrency/windows_join_self.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
error: deadlock: the evaluated program deadlocked
22
--> $DIR/windows_join_self.rs:LL:CC
33
|
4-
LL | assert_eq!(WaitForSingleObject(native, INFINITE), 0);
4+
LL | assert_eq!(WaitForSingleObject(native, INFINITE), WAIT_OBJECT_0);
55
| ^ the evaluated program deadlocked
66
|
77
= note: inside closure at $DIR/windows_join_self.rs:LL:CC

src/tools/miri/tests/pass/concurrency/windows_condvar_shared.rs src/tools/miri/tests/pass-dep/concurrency/windows_condvar_shared.rs

+16-25
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,30 @@
22
// We are making scheduler assumptions here.
33
//@compile-flags: -Zmiri-preemption-rate=0
44

5-
use std::ffi::c_void;
65
use std::ptr::null_mut;
76
use std::thread;
87

8+
use windows_sys::Win32::System::Threading::{
9+
AcquireSRWLockExclusive, AcquireSRWLockShared, ReleaseSRWLockExclusive, ReleaseSRWLockShared,
10+
SleepConditionVariableSRW, WakeAllConditionVariable, CONDITION_VARIABLE,
11+
CONDITION_VARIABLE_LOCKMODE_SHARED, INFINITE, SRWLOCK,
12+
};
13+
14+
// not in windows-sys
15+
const SRWLOCK_INIT: SRWLOCK = SRWLOCK { Ptr: null_mut() };
16+
const CONDITION_VARIABLE_INIT: CONDITION_VARIABLE = CONDITION_VARIABLE { Ptr: null_mut() };
17+
918
#[derive(Copy, Clone)]
1019
struct SendPtr<T>(*mut T);
1120

1221
unsafe impl<T> Send for SendPtr<T> {}
1322

14-
extern "system" {
15-
fn SleepConditionVariableSRW(
16-
condvar: *mut *mut c_void,
17-
lock: *mut *mut c_void,
18-
timeout: u32,
19-
flags: u32,
20-
) -> i32;
21-
fn WakeAllConditionVariable(condvar: *mut *mut c_void);
22-
23-
fn AcquireSRWLockExclusive(lock: *mut *mut c_void);
24-
fn AcquireSRWLockShared(lock: *mut *mut c_void);
25-
fn ReleaseSRWLockExclusive(lock: *mut *mut c_void);
26-
fn ReleaseSRWLockShared(lock: *mut *mut c_void);
27-
}
28-
29-
const CONDITION_VARIABLE_LOCKMODE_SHARED: u32 = 1;
30-
const INFINITE: u32 = u32::MAX;
31-
3223
/// threads should be able to reacquire the lock while it is locked by multiple other threads in shared mode
3324
fn all_shared() {
3425
println!("all_shared");
3526

36-
let mut lock = null_mut();
37-
let mut condvar = null_mut();
27+
let mut lock = SRWLOCK_INIT;
28+
let mut condvar = CONDITION_VARIABLE_INIT;
3829

3930
let lock_ptr = SendPtr(&mut lock);
4031
let condvar_ptr = SendPtr(&mut condvar);
@@ -105,8 +96,8 @@ fn all_shared() {
10596
fn shared_sleep_and_exclusive_lock() {
10697
println!("shared_sleep_and_exclusive_lock");
10798

108-
let mut lock = null_mut();
109-
let mut condvar = null_mut();
99+
let mut lock = SRWLOCK_INIT;
100+
let mut condvar = CONDITION_VARIABLE_INIT;
110101

111102
let lock_ptr = SendPtr(&mut lock);
112103
let condvar_ptr = SendPtr(&mut condvar);
@@ -166,8 +157,8 @@ fn shared_sleep_and_exclusive_lock() {
166157
fn exclusive_sleep_and_shared_lock() {
167158
println!("exclusive_sleep_and_shared_lock");
168159

169-
let mut lock = null_mut();
170-
let mut condvar = null_mut();
160+
let mut lock = SRWLOCK_INIT;
161+
let mut condvar = CONDITION_VARIABLE_INIT;
171162

172163
let lock_ptr = SendPtr(&mut lock);
173164
let condvar_ptr = SendPtr(&mut condvar);

src/tools/miri/tests/pass/concurrency/windows_detach_terminated.rs src/tools/miri/tests/pass-dep/concurrency/windows_detach_terminated.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
use std::os::windows::io::IntoRawHandle;
66
use std::thread;
77

8-
extern "system" {
9-
fn CloseHandle(handle: usize) -> i32;
10-
}
8+
use windows_sys::Win32::Foundation::{CloseHandle, HANDLE};
119

1210
fn main() {
13-
let thread = thread::spawn(|| {}).into_raw_handle() as usize;
11+
let thread = thread::spawn(|| {}).into_raw_handle() as HANDLE;
1412

1513
// this yield ensures that `thread` is terminated by this point
1614
thread::yield_now();

src/tools/miri/tests/pass/concurrency/windows_init_once.rs src/tools/miri/tests/pass-dep/concurrency/windows_init_once.rs

+13-22
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,24 @@
22
// We are making scheduler assumptions here.
33
//@compile-flags: -Zmiri-preemption-rate=0
44

5-
use std::ffi::c_void;
65
use std::ptr::null_mut;
76
use std::thread;
87

8+
use windows_sys::Win32::Foundation::{FALSE, TRUE};
9+
use windows_sys::Win32::System::Threading::{
10+
InitOnceBeginInitialize, InitOnceComplete, INIT_ONCE, INIT_ONCE_INIT_FAILED,
11+
};
12+
13+
// not in windows-sys
14+
const INIT_ONCE_STATIC_INIT: INIT_ONCE = INIT_ONCE { Ptr: null_mut() };
15+
916
#[derive(Copy, Clone)]
1017
struct SendPtr<T>(*mut T);
1118

1219
unsafe impl<T> Send for SendPtr<T> {}
1320

14-
extern "system" {
15-
fn InitOnceBeginInitialize(
16-
init: *mut *mut c_void,
17-
flags: u32,
18-
pending: *mut i32,
19-
context: *mut c_void,
20-
) -> i32;
21-
22-
fn InitOnceComplete(init: *mut *mut c_void, flags: u32, context: *mut c_void) -> i32;
23-
}
24-
25-
const TRUE: i32 = 1;
26-
const FALSE: i32 = 0;
27-
28-
const INIT_ONCE_INIT_FAILED: u32 = 4;
29-
3021
fn single_thread() {
31-
let mut init_once = null_mut();
22+
let mut init_once = INIT_ONCE_STATIC_INIT;
3223
let mut pending = 0;
3324

3425
unsafe {
@@ -41,7 +32,7 @@ fn single_thread() {
4132
assert_eq!(pending, FALSE);
4233
}
4334

44-
let mut init_once = null_mut();
35+
let mut init_once = INIT_ONCE_STATIC_INIT;
4536

4637
unsafe {
4738
assert_eq!(InitOnceBeginInitialize(&mut init_once, 0, &mut pending, null_mut()), TRUE);
@@ -55,7 +46,7 @@ fn single_thread() {
5546
}
5647

5748
fn block_until_complete() {
58-
let mut init_once = null_mut();
49+
let mut init_once = INIT_ONCE_STATIC_INIT;
5950
let mut pending = 0;
6051

6152
unsafe {
@@ -92,7 +83,7 @@ fn block_until_complete() {
9283
}
9384

9485
fn retry_on_fail() {
95-
let mut init_once = null_mut();
86+
let mut init_once = INIT_ONCE_STATIC_INIT;
9687
let mut pending = 0;
9788

9889
unsafe {
@@ -134,7 +125,7 @@ fn retry_on_fail() {
134125
}
135126

136127
fn no_data_race_after_complete() {
137-
let mut init_once = null_mut();
128+
let mut init_once = INIT_ONCE_STATIC_INIT;
138129
let mut pending = 0;
139130

140131
unsafe {

src/tools/miri/tests/pass/concurrency/windows_join_multiple.rs src/tools/miri/tests/pass-dep/concurrency/windows_join_multiple.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ use std::os::windows::io::IntoRawHandle;
66
use std::sync::atomic::{AtomicBool, Ordering};
77
use std::thread;
88

9-
extern "system" {
10-
fn WaitForSingleObject(handle: usize, timeout: u32) -> u32;
11-
}
12-
13-
const INFINITE: u32 = u32::MAX;
9+
use windows_sys::Win32::Foundation::{HANDLE, WAIT_OBJECT_0};
10+
use windows_sys::Win32::System::Threading::{WaitForSingleObject, INFINITE};
1411

1512
fn main() {
1613
static FLAG: AtomicBool = AtomicBool::new(false);
@@ -20,10 +17,10 @@ fn main() {
2017
thread::yield_now();
2118
}
2219
})
23-
.into_raw_handle() as usize;
20+
.into_raw_handle() as HANDLE;
2421

2522
let waiter = move || unsafe {
26-
assert_eq!(WaitForSingleObject(blocker, INFINITE), 0);
23+
assert_eq!(WaitForSingleObject(blocker, INFINITE), WAIT_OBJECT_0);
2724
};
2825

2926
let waiter1 = thread::spawn(waiter);

0 commit comments

Comments
 (0)