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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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 renamed to src/tools/miri/tests/fail-dep/concurrency/windows_join_detached.rs

Lines changed: 3 additions & 5 deletions
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 renamed to src/tools/miri/tests/fail-dep/concurrency/windows_join_main.rs

Lines changed: 4 additions & 7 deletions
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 renamed to src/tools/miri/tests/fail-dep/concurrency/windows_join_main.stderr

Lines changed: 2 additions & 2 deletions
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 renamed to src/tools/miri/tests/fail-dep/concurrency/windows_join_self.rs

Lines changed: 3 additions & 7 deletions
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 renamed to src/tools/miri/tests/fail-dep/concurrency/windows_join_self.stderr

Lines changed: 1 addition & 1 deletion
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 renamed to src/tools/miri/tests/pass-dep/concurrency/windows_condvar_shared.rs

Lines changed: 16 additions & 25 deletions
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);

0 commit comments

Comments
 (0)