-
Notifications
You must be signed in to change notification settings - Fork 391
Implement rwlocks on Windows #1461
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dca00ab
introduce platform-specific module hierarchy for dlsym (similar to fo…
RalfJung 8e92969
implement Windows SRWLock shims
RalfJung e54619b
with this, we support panics on Windows
RalfJung a9dc279
Move get/set_at_offset helpers to global helpers file
RalfJung 3a5bcb9
move rwlock dequeuing to shared code, and use that code for Windows r…
RalfJung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,161 @@ | ||||||
use rustc_target::abi::Size; | ||||||
|
||||||
use crate::*; | ||||||
|
||||||
// Locks are pointer-sized pieces of data, initialized to 0. | ||||||
// We use them to count readers, with usize::MAX representing the write-locked state. | ||||||
|
||||||
fn deref_lock<'mir, 'tcx: 'mir>( | ||||||
ecx: &mut MiriEvalContext<'mir, 'tcx>, | ||||||
lock_op: OpTy<'tcx, Tag>, | ||||||
) -> InterpResult<'tcx, MPlaceTy<'tcx, Tag>> { | ||||||
// `lock` is a pointer to `void*`; cast it to a pointer to `usize`. | ||||||
let lock = ecx.deref_operand(lock_op)?; | ||||||
let usize = ecx.machine.layouts.usize; | ||||||
assert_eq!(lock.layout.size, usize.size); | ||||||
Ok(lock.offset(Size::ZERO, MemPlaceMeta::None, usize, ecx)?) | ||||||
} | ||||||
|
||||||
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {} | ||||||
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> { | ||||||
#[allow(non_snake_case)] | ||||||
fn AcquireSRWLockExclusive( | ||||||
&mut self, | ||||||
lock_op: OpTy<'tcx, Tag>, | ||||||
) -> InterpResult<'tcx> { | ||||||
let this = self.eval_context_mut(); | ||||||
assert_eq!(this.get_total_thread_count(), 1, "concurrency on Windows is not supported"); | ||||||
|
||||||
let lock = deref_lock(this, lock_op)?; | ||||||
let lock_val = this.read_scalar(lock.into())?.to_machine_usize(this)?; | ||||||
if lock_val == 0 { | ||||||
// Currently not locked. Lock it. | ||||||
let new_val = Scalar::from_machine_usize(this.machine_usize_max(), this); | ||||||
this.write_scalar(new_val, lock.into())?; | ||||||
} else { | ||||||
// Lock is already held. This is a deadlock. | ||||||
throw_machine_stop!(TerminationInfo::Deadlock); | ||||||
} | ||||||
|
||||||
Ok(()) | ||||||
} | ||||||
|
||||||
#[allow(non_snake_case)] | ||||||
fn TryAcquireSRWLockExclusive( | ||||||
&mut self, | ||||||
lock_op: OpTy<'tcx, Tag>, | ||||||
) -> InterpResult<'tcx, u8> { | ||||||
let this = self.eval_context_mut(); | ||||||
assert_eq!(this.get_total_thread_count(), 1, "concurrency on Windows is not supported"); | ||||||
|
||||||
let lock = deref_lock(this, lock_op)?; | ||||||
let lock_val = this.read_scalar(lock.into())?.to_machine_usize(this)?; | ||||||
if lock_val == 0 { | ||||||
// Currently not locked. Lock it. | ||||||
let new_val = this.machine_usize_max(); | ||||||
this.write_scalar(Scalar::from_machine_usize(new_val, this), lock.into())?; | ||||||
Ok(1) | ||||||
} else { | ||||||
// Lock is already held. | ||||||
Ok(0) | ||||||
} | ||||||
} | ||||||
|
||||||
#[allow(non_snake_case)] | ||||||
fn ReleaseSRWLockExclusive( | ||||||
&mut self, | ||||||
lock_op: OpTy<'tcx, Tag>, | ||||||
) -> InterpResult<'tcx> { | ||||||
let this = self.eval_context_mut(); | ||||||
assert_eq!(this.get_total_thread_count(), 1, "concurrency on Windows is not supported"); | ||||||
|
||||||
let lock = deref_lock(this, lock_op)?; | ||||||
let lock_val = this.read_scalar(lock.into())?.to_machine_usize(this)?; | ||||||
if lock_val == this.machine_usize_max() { | ||||||
// Currently locked. Unlock it. | ||||||
let new_val = 0; | ||||||
this.write_scalar(Scalar::from_machine_usize(new_val, this), lock.into())?; | ||||||
} else { | ||||||
// Lock is not locked. | ||||||
throw_ub_format!("calling ReleaseSRWLockExclusive on an SRWLock that is not exclusively locked"); | ||||||
} | ||||||
|
||||||
Ok(()) | ||||||
} | ||||||
|
||||||
#[allow(non_snake_case)] | ||||||
fn AcquireSRWLockShared( | ||||||
&mut self, | ||||||
lock_op: OpTy<'tcx, Tag>, | ||||||
) -> InterpResult<'tcx> { | ||||||
let this = self.eval_context_mut(); | ||||||
assert_eq!(this.get_total_thread_count(), 1, "concurrency on Windows is not supported"); | ||||||
|
||||||
let lock = deref_lock(this, lock_op)?; | ||||||
let lock_val = this.read_scalar(lock.into())?.to_machine_usize(this)?; | ||||||
if lock_val == this.machine_usize_max() { | ||||||
// Currently write locked. This is a deadlock. | ||||||
throw_machine_stop!(TerminationInfo::Deadlock); | ||||||
} else { | ||||||
// Bump up read counter (cannot overflow as we just checkd against usize::MAX); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
let new_val = lock_val+1; | ||||||
// Make sure this does not reach the "write locked" flag. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice. |
||||||
if new_val == this.machine_usize_max() { | ||||||
throw_unsup_format!("SRWLock read-acquired too many times"); | ||||||
} | ||||||
this.write_scalar(Scalar::from_machine_usize(new_val, this), lock.into())?; | ||||||
} | ||||||
|
||||||
Ok(()) | ||||||
} | ||||||
|
||||||
#[allow(non_snake_case)] | ||||||
fn TryAcquireSRWLockShared( | ||||||
&mut self, | ||||||
lock_op: OpTy<'tcx, Tag>, | ||||||
) -> InterpResult<'tcx, u8> { | ||||||
let this = self.eval_context_mut(); | ||||||
assert_eq!(this.get_total_thread_count(), 1, "concurrency on Windows is not supported"); | ||||||
|
||||||
let lock = deref_lock(this, lock_op)?; | ||||||
let lock_val = this.read_scalar(lock.into())?.to_machine_usize(this)?; | ||||||
if lock_val == this.machine_usize_max() { | ||||||
// Currently write locked. | ||||||
Ok(0) | ||||||
} else { | ||||||
// Bump up read counter (cannot overflow as we just checkd against usize::MAX); | ||||||
let new_val = lock_val+1; | ||||||
// Make sure this does not reach the "write locked" flag. | ||||||
if new_val == this.machine_usize_max() { | ||||||
throw_unsup_format!("SRWLock read-acquired too many times"); | ||||||
} | ||||||
this.write_scalar(Scalar::from_machine_usize(new_val, this), lock.into())?; | ||||||
Ok(1) | ||||||
} | ||||||
} | ||||||
|
||||||
#[allow(non_snake_case)] | ||||||
fn ReleaseSRWLockShared( | ||||||
&mut self, | ||||||
lock_op: OpTy<'tcx, Tag>, | ||||||
) -> InterpResult<'tcx> { | ||||||
let this = self.eval_context_mut(); | ||||||
assert_eq!(this.get_total_thread_count(), 1, "concurrency on Windows is not supported"); | ||||||
|
||||||
let lock = deref_lock(this, lock_op)?; | ||||||
let lock_val = this.read_scalar(lock.into())?.to_machine_usize(this)?; | ||||||
if lock_val == this.machine_usize_max() { | ||||||
// Currently write locked. This is a UB. | ||||||
throw_ub_format!("calling ReleaseSRWLockShared on write-locked SRWLock"); | ||||||
} else if lock_val == 0 { | ||||||
// Currently not locked at all. | ||||||
throw_ub_format!("calling ReleaseSRWLockShared on unlocked SRWLock"); | ||||||
} else { | ||||||
// Decrement read counter (cannot overflow as we just checkd against 0); | ||||||
let new_val = lock_val-1; | ||||||
this.write_scalar(Scalar::from_machine_usize(new_val, this), lock.into())?; | ||||||
} | ||||||
|
||||||
Ok(()) | ||||||
} | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function could just invoke its
Try*
equivalent and cause the deadlock error in case the result is0
. That should help deduplicating the logicThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you looked at an old intermediate commit -- this changed quite a bit for the final PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I entirely re-implemented rwlocks later and figured I would keep the history. Sorry for the confusion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😆 I didn't get that far yet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so the thread id assertions are gone because the logic would now support threading? (I know we don't support thread starting yet at all, but the lock logic works now correctly for threads?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. It's the same logic as what we use on POSIX targets.