Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miri subtree update #119574

Merged
merged 14 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
have windows tests use windows-sys
  • Loading branch information
beepster4096 committed Dec 26, 2023
commit ac9368ab1eece4419e9bee4426c6389f8ae28450
179 changes: 123 additions & 56 deletions src/tools/miri/test_dependencies/Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/tools/miri/test_dependencies/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ rand = { version = "0.8", features = ["small_rng"] }
page_size = "0.6"
tokio = { version = "1.24", features = ["full"] }

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.52", features = [ "Win32_Foundation", "Win32_System_Threading" ] }

[workspace]
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@

// Joining a detached thread is undefined behavior.

use std::os::windows::io::{AsRawHandle, RawHandle};
use std::os::windows::io::AsRawHandle;
use std::thread;

extern "system" {
fn CloseHandle(handle: RawHandle) -> u32;
}
use windows_sys::Win32::Foundation::CloseHandle;

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

unsafe {
assert_ne!(CloseHandle(thread.as_raw_handle()), 0);
assert_ne!(CloseHandle(thread.as_raw_handle() as _), 0);
}

thread.join().unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@

use std::thread;

extern "system" {
fn WaitForSingleObject(handle: isize, timeout: u32) -> u32;
}

const INFINITE: u32 = u32::MAX;
use windows_sys::Win32::Foundation::{HANDLE, WAIT_OBJECT_0};
use windows_sys::Win32::System::Threading::{WaitForSingleObject, INFINITE};

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

fn main() {
thread::spawn(|| {
unsafe {
assert_eq!(WaitForSingleObject(MAIN_THREAD, INFINITE), 0); //~ ERROR: deadlock: the evaluated program deadlocked
assert_eq!(WaitForSingleObject(MAIN_THREAD, INFINITE), WAIT_OBJECT_0); //~ ERROR: deadlock: the evaluated program deadlocked
}
})
.join()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: deadlock: the evaluated program deadlocked
--> $DIR/windows_join_main.rs:LL:CC
|
LL | assert_eq!(WaitForSingleObject(MAIN_THREAD, INFINITE), 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program deadlocked
LL | assert_eq!(WaitForSingleObject(MAIN_THREAD, INFINITE), WAIT_OBJECT_0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program deadlocked
|
= note: inside closure at RUSTLIB/core/src/macros/mod.rs:LL:CC
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@

use std::thread;

extern "system" {
fn GetCurrentThread() -> usize;
fn WaitForSingleObject(handle: usize, timeout: u32) -> u32;
}

const INFINITE: u32 = u32::MAX;
use windows_sys::Win32::Foundation::WAIT_OBJECT_0;
use windows_sys::Win32::System::Threading::{GetCurrentThread, WaitForSingleObject, INFINITE};

fn main() {
thread::spawn(|| {
unsafe {
let native = GetCurrentThread();
assert_eq!(WaitForSingleObject(native, INFINITE), 0); //~ ERROR: deadlock: the evaluated program deadlocked
assert_eq!(WaitForSingleObject(native, INFINITE), WAIT_OBJECT_0); //~ ERROR: deadlock: the evaluated program deadlocked
}
})
.join()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
error: deadlock: the evaluated program deadlocked
--> $DIR/windows_join_self.rs:LL:CC
|
LL | assert_eq!(WaitForSingleObject(native, INFINITE), 0);
LL | assert_eq!(WaitForSingleObject(native, INFINITE), WAIT_OBJECT_0);
| ^ the evaluated program deadlocked
|
= note: inside closure at $DIR/windows_join_self.rs:LL:CC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,29 @@
// We are making scheduler assumptions here.
//@compile-flags: -Zmiri-preemption-rate=0

use std::ffi::c_void;
use std::ptr::null_mut;
use std::mem::MaybeUninit;
use std::thread;

use windows_sys::Win32::System::Threading::{
AcquireSRWLockExclusive, AcquireSRWLockShared, ReleaseSRWLockExclusive, ReleaseSRWLockShared,
SleepConditionVariableSRW, WakeAllConditionVariable, CONDITION_VARIABLE_LOCKMODE_SHARED,
INFINITE,
};

#[derive(Copy, Clone)]
struct SendPtr<T>(*mut T);

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

extern "system" {
fn SleepConditionVariableSRW(
condvar: *mut *mut c_void,
lock: *mut *mut c_void,
timeout: u32,
flags: u32,
) -> i32;
fn WakeAllConditionVariable(condvar: *mut *mut c_void);

fn AcquireSRWLockExclusive(lock: *mut *mut c_void);
fn AcquireSRWLockShared(lock: *mut *mut c_void);
fn ReleaseSRWLockExclusive(lock: *mut *mut c_void);
fn ReleaseSRWLockShared(lock: *mut *mut c_void);
}

const CONDITION_VARIABLE_LOCKMODE_SHARED: u32 = 1;
const INFINITE: u32 = u32::MAX;

/// threads should be able to reacquire the lock while it is locked by multiple other threads in shared mode
fn all_shared() {
println!("all_shared");

let mut lock = null_mut();
let mut condvar = null_mut();
let mut lock = MaybeUninit::zeroed();
let mut condvar = MaybeUninit::zeroed();

let lock_ptr = SendPtr(&mut lock);
let condvar_ptr = SendPtr(&mut condvar);
let lock_ptr = SendPtr(lock.as_mut_ptr());
let condvar_ptr = SendPtr(condvar.as_mut_ptr());

let mut handles = Vec::with_capacity(10);

Expand Down Expand Up @@ -105,11 +92,11 @@ fn all_shared() {
fn shared_sleep_and_exclusive_lock() {
println!("shared_sleep_and_exclusive_lock");

let mut lock = null_mut();
let mut condvar = null_mut();
let mut lock = MaybeUninit::zeroed();
let mut condvar = MaybeUninit::zeroed();

let lock_ptr = SendPtr(&mut lock);
let condvar_ptr = SendPtr(&mut condvar);
let lock_ptr = SendPtr(lock.as_mut_ptr());
let condvar_ptr = SendPtr(condvar.as_mut_ptr());

let mut waiters = Vec::with_capacity(5);
for i in 0..5 {
Expand Down Expand Up @@ -166,11 +153,11 @@ fn shared_sleep_and_exclusive_lock() {
fn exclusive_sleep_and_shared_lock() {
println!("exclusive_sleep_and_shared_lock");

let mut lock = null_mut();
let mut condvar = null_mut();
let mut lock = MaybeUninit::zeroed();
let mut condvar = MaybeUninit::zeroed();

let lock_ptr = SendPtr(&mut lock);
let condvar_ptr = SendPtr(&mut condvar);
let lock_ptr = SendPtr(lock.as_mut_ptr());
let condvar_ptr = SendPtr(condvar.as_mut_ptr());

let mut handles = Vec::with_capacity(10);
for i in 0..5 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
use std::os::windows::io::IntoRawHandle;
use std::thread;

extern "system" {
fn CloseHandle(handle: usize) -> i32;
}
use windows_sys::Win32::Foundation::CloseHandle;

fn main() {
let thread = thread::spawn(|| {}).into_raw_handle() as usize;
let thread = thread::spawn(|| {}).into_raw_handle();

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

unsafe {
assert_ne!(CloseHandle(thread), 0);
assert_ne!(CloseHandle(thread as _), 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,75 @@
// We are making scheduler assumptions here.
//@compile-flags: -Zmiri-preemption-rate=0

use std::ffi::c_void;
use std::mem::MaybeUninit;
use std::ptr::null_mut;
use std::thread;

use windows_sys::Win32::Foundation::{FALSE, TRUE};
use windows_sys::Win32::System::Threading::{
InitOnceBeginInitialize, InitOnceComplete, INIT_ONCE_INIT_FAILED,
};

#[derive(Copy, Clone)]
struct SendPtr<T>(*mut T);

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

extern "system" {
fn InitOnceBeginInitialize(
init: *mut *mut c_void,
flags: u32,
pending: *mut i32,
context: *mut c_void,
) -> i32;

fn InitOnceComplete(init: *mut *mut c_void, flags: u32, context: *mut c_void) -> i32;
}

const TRUE: i32 = 1;
const FALSE: i32 = 0;

const INIT_ONCE_INIT_FAILED: u32 = 4;

fn single_thread() {
let mut init_once = null_mut();
let mut init_once = MaybeUninit::zeroed();
let mut pending = 0;

unsafe {
assert_eq!(InitOnceBeginInitialize(&mut init_once, 0, &mut pending, null_mut()), TRUE);
assert_eq!(
InitOnceBeginInitialize(init_once.as_mut_ptr(), 0, &mut pending, null_mut()),
TRUE
);
assert_eq!(pending, TRUE);

assert_eq!(InitOnceComplete(&mut init_once, 0, null_mut()), TRUE);
assert_eq!(InitOnceComplete(init_once.as_mut_ptr(), 0, null_mut()), TRUE);

assert_eq!(InitOnceBeginInitialize(&mut init_once, 0, &mut pending, null_mut()), TRUE);
assert_eq!(
InitOnceBeginInitialize(init_once.as_mut_ptr(), 0, &mut pending, null_mut()),
TRUE
);
assert_eq!(pending, FALSE);
}

let mut init_once = null_mut();
let mut init_once = MaybeUninit::zeroed();

unsafe {
assert_eq!(InitOnceBeginInitialize(&mut init_once, 0, &mut pending, null_mut()), TRUE);
assert_eq!(
InitOnceBeginInitialize(init_once.as_mut_ptr(), 0, &mut pending, null_mut()),
TRUE
);
assert_eq!(pending, TRUE);

assert_eq!(InitOnceComplete(&mut init_once, INIT_ONCE_INIT_FAILED, null_mut()), TRUE);
assert_eq!(
InitOnceComplete(init_once.as_mut_ptr(), INIT_ONCE_INIT_FAILED, null_mut()),
TRUE
);

assert_eq!(InitOnceBeginInitialize(&mut init_once, 0, &mut pending, null_mut()), TRUE);
assert_eq!(
InitOnceBeginInitialize(init_once.as_mut_ptr(), 0, &mut pending, null_mut()),
TRUE
);
assert_eq!(pending, TRUE);
}
}

fn block_until_complete() {
let mut init_once = null_mut();
let mut init_once = MaybeUninit::zeroed();
let mut pending = 0;

unsafe {
assert_eq!(InitOnceBeginInitialize(&mut init_once, 0, &mut pending, null_mut()), TRUE);
assert_eq!(
InitOnceBeginInitialize(init_once.as_mut_ptr(), 0, &mut pending, null_mut()),
TRUE
);
assert_eq!(pending, TRUE);
}

let init_once_ptr = SendPtr(&mut init_once);
let init_once_ptr = SendPtr(init_once.as_mut_ptr());

let waiter = move || unsafe {
let init_once_ptr = init_once_ptr; // avoid field capture
Expand Down Expand Up @@ -92,15 +99,18 @@ fn block_until_complete() {
}

fn retry_on_fail() {
let mut init_once = null_mut();
let mut init_once = MaybeUninit::zeroed();
let mut pending = 0;

unsafe {
assert_eq!(InitOnceBeginInitialize(&mut init_once, 0, &mut pending, null_mut()), TRUE);
assert_eq!(
InitOnceBeginInitialize(init_once.as_mut_ptr(), 0, &mut pending, null_mut()),
TRUE
);
assert_eq!(pending, TRUE);
}

let init_once_ptr = SendPtr(&mut init_once);
let init_once_ptr = SendPtr(init_once.as_mut_ptr());

let waiter = move || unsafe {
let init_once_ptr = init_once_ptr; // avoid field capture
Expand Down Expand Up @@ -134,15 +144,18 @@ fn retry_on_fail() {
}

fn no_data_race_after_complete() {
let mut init_once = null_mut();
let mut init_once = MaybeUninit::zeroed();
let mut pending = 0;

unsafe {
assert_eq!(InitOnceBeginInitialize(&mut init_once, 0, &mut pending, null_mut()), TRUE);
assert_eq!(
InitOnceBeginInitialize(init_once.as_mut_ptr(), 0, &mut pending, null_mut()),
TRUE
);
assert_eq!(pending, TRUE);
}

let init_once_ptr = SendPtr(&mut init_once);
let init_once_ptr = SendPtr(init_once.as_mut_ptr());

let mut place = 0;
let place_ptr = SendPtr(&mut place);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ use std::os::windows::io::IntoRawHandle;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;

extern "system" {
fn WaitForSingleObject(handle: usize, timeout: u32) -> u32;
}

const INFINITE: u32 = u32::MAX;
use windows_sys::Win32::Foundation::WAIT_OBJECT_0;
use windows_sys::Win32::System::Threading::{WaitForSingleObject, INFINITE};

fn main() {
static FLAG: AtomicBool = AtomicBool::new(false);
Expand All @@ -20,10 +17,10 @@ fn main() {
thread::yield_now();
}
})
.into_raw_handle() as usize;
.into_raw_handle() as _;

let waiter = move || unsafe {
assert_eq!(WaitForSingleObject(blocker, INFINITE), 0);
assert_eq!(WaitForSingleObject(blocker, INFINITE), WAIT_OBJECT_0);
};

let waiter1 = thread::spawn(waiter);
Expand Down