|
| 1 | +//! Contains macOS-specific synchronization functions. |
| 2 | +//! |
| 3 | +//! For `os_unfair_lock`, see the documentation |
| 4 | +//! <https://developer.apple.com/documentation/os/synchronization?language=objc> |
| 5 | +//! and in case of underspecification its implementation |
| 6 | +//! <https://github.com/apple-oss-distributions/libplatform/blob/a00a4cc36da2110578bcf3b8eeeeb93dcc7f4e11/src/os/lock.c#L645> |
| 7 | +
|
| 8 | +use crate::*; |
| 9 | + |
| 10 | +impl<'tcx> EvalContextExtPriv<'tcx> for crate::MiriInterpCx<'tcx> {} |
| 11 | +trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> { |
| 12 | + fn os_unfair_lock_getid(&mut self, lock_op: &OpTy<'tcx>) -> InterpResult<'tcx, MutexId> { |
| 13 | + let this = self.eval_context_mut(); |
| 14 | + // os_unfair_lock holds a 32-bit value, is initialized with zero and |
| 15 | + // must be assumed to be opaque. Therefore, we can just store our |
| 16 | + // internal mutex ID in the structure without anyone noticing. |
| 17 | + this.mutex_get_or_create_id(lock_op, this.libc_ty_layout("os_unfair_lock"), 0) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {} |
| 22 | +pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { |
| 23 | + fn os_unfair_lock_lock(&mut self, lock_op: &OpTy<'tcx>) -> InterpResult<'tcx> { |
| 24 | + let this = self.eval_context_mut(); |
| 25 | + |
| 26 | + let id = this.os_unfair_lock_getid(lock_op)?; |
| 27 | + if this.mutex_is_locked(id) { |
| 28 | + if this.mutex_get_owner(id) == this.active_thread() { |
| 29 | + // Matching the current macOS implementation: abort on reentrant locking. |
| 30 | + throw_machine_stop!(TerminationInfo::Abort( |
| 31 | + "attempted to lock an os_unfair_lock that is already locked by the current thread".to_owned() |
| 32 | + )); |
| 33 | + } |
| 34 | + |
| 35 | + this.mutex_enqueue_and_block(id, None); |
| 36 | + } else { |
| 37 | + this.mutex_lock(id); |
| 38 | + } |
| 39 | + |
| 40 | + Ok(()) |
| 41 | + } |
| 42 | + |
| 43 | + fn os_unfair_lock_trylock( |
| 44 | + &mut self, |
| 45 | + lock_op: &OpTy<'tcx>, |
| 46 | + dest: &MPlaceTy<'tcx>, |
| 47 | + ) -> InterpResult<'tcx> { |
| 48 | + let this = self.eval_context_mut(); |
| 49 | + |
| 50 | + let id = this.os_unfair_lock_getid(lock_op)?; |
| 51 | + if this.mutex_is_locked(id) { |
| 52 | + // Contrary to the blocking lock function, this does not check for |
| 53 | + // reentrancy. |
| 54 | + this.write_scalar(Scalar::from_bool(false), dest)?; |
| 55 | + } else { |
| 56 | + this.mutex_lock(id); |
| 57 | + this.write_scalar(Scalar::from_bool(true), dest)?; |
| 58 | + } |
| 59 | + |
| 60 | + Ok(()) |
| 61 | + } |
| 62 | + |
| 63 | + fn os_unfair_lock_unlock(&mut self, lock_op: &OpTy<'tcx>) -> InterpResult<'tcx> { |
| 64 | + let this = self.eval_context_mut(); |
| 65 | + |
| 66 | + let id = this.os_unfair_lock_getid(lock_op)?; |
| 67 | + if this.mutex_unlock(id)?.is_none() { |
| 68 | + // Matching the current macOS implementation: abort. |
| 69 | + throw_machine_stop!(TerminationInfo::Abort( |
| 70 | + "attempted to unlock an os_unfair_lock not owned by the current thread".to_owned() |
| 71 | + )); |
| 72 | + } |
| 73 | + |
| 74 | + Ok(()) |
| 75 | + } |
| 76 | + |
| 77 | + fn os_unfair_lock_assert_owner(&mut self, lock_op: &OpTy<'tcx>) -> InterpResult<'tcx> { |
| 78 | + let this = self.eval_context_mut(); |
| 79 | + |
| 80 | + let id = this.os_unfair_lock_getid(lock_op)?; |
| 81 | + if !this.mutex_is_locked(id) || this.mutex_get_owner(id) != this.active_thread() { |
| 82 | + throw_machine_stop!(TerminationInfo::Abort( |
| 83 | + "called os_unfair_lock_assert_owner on an os_unfair_lock not owned by the current thread".to_owned() |
| 84 | + )); |
| 85 | + } |
| 86 | + |
| 87 | + Ok(()) |
| 88 | + } |
| 89 | + |
| 90 | + fn os_unfair_lock_assert_not_owner(&mut self, lock_op: &OpTy<'tcx>) -> InterpResult<'tcx> { |
| 91 | + let this = self.eval_context_mut(); |
| 92 | + |
| 93 | + let id = this.os_unfair_lock_getid(lock_op)?; |
| 94 | + if this.mutex_is_locked(id) && this.mutex_get_owner(id) == this.active_thread() { |
| 95 | + throw_machine_stop!(TerminationInfo::Abort( |
| 96 | + "called os_unfair_lock_assert_not_owner on an os_unfair_lock owned by the current thread".to_owned() |
| 97 | + )); |
| 98 | + } |
| 99 | + |
| 100 | + Ok(()) |
| 101 | + } |
| 102 | +} |
0 commit comments