style: Print lock address on assert

Note that the crash reason is sanitized so we're not exposing anything
sensitive.

I think my patch just changed the signature of the stack, as it didn't
change anything related to guards or what not. But without knowing why
is failing or a repro it's hard to know what's going on.

Printing the address at list would give us some indication of what might
be going wrong (perhaps we're using a static lock when we don't expect
one or such?).

Differential Revision: https://phabricator.services.mozilla.com/D125948
This commit is contained in:
Emilio Cobos Álvarez 2023-05-27 14:57:02 +02:00 committed by Oriol Brufau
parent 75acb72256
commit 2a42be3cc8

View file

@ -94,6 +94,12 @@ impl SharedRwLock {
SharedRwLock { cell: None }
}
#[cfg(feature = "gecko")]
#[inline]
fn ptr(&self) -> *const SomethingZeroSizedButTyped {
self.cell.as_ref().map(|cell| cell.as_ptr() as *const _).unwrap_or(ptr::null())
}
/// Wrap the given data to make its access protected by this lock.
pub fn wrap<T>(&self, data: T) -> Locked<T> {
Locked {
@ -144,6 +150,14 @@ impl<'a> Drop for SharedRwLockReadGuard<'a> {
}
}
impl<'a> SharedRwLockReadGuard<'a> {
#[inline]
#[cfg(feature = "gecko")]
fn ptr(&self) -> *const SomethingZeroSizedButTyped {
self.0.as_ref().map(|r| &**r as *const _).unwrap_or(ptr::null())
}
}
/// Proof that a shared lock was obtained for writing (servo).
#[cfg(feature = "servo")]
pub struct SharedRwLockWriteGuard<'a>(&'a SharedRwLock);
@ -190,25 +204,18 @@ impl<T> Locked<T> {
}
#[cfg(feature = "gecko")]
fn same_lock_as(&self, derefed_guard: Option<&SomethingZeroSizedButTyped>) -> bool {
ptr::eq(
self.shared_lock
.cell
.as_ref()
.map(|cell| cell.as_ptr())
.unwrap_or(ptr::null_mut()),
derefed_guard
.map(|guard| guard as *const _ as *mut _)
.unwrap_or(ptr::null_mut()),
)
fn same_lock_as(&self, ptr: *const SomethingZeroSizedButTyped) -> bool {
ptr::eq(self.shared_lock.ptr(), ptr)
}
/// Access the data for reading.
pub fn read_with<'a>(&'a self, guard: &'a SharedRwLockReadGuard) -> &'a T {
#[cfg(feature = "gecko")]
assert!(
self.is_read_only_lock() || self.same_lock_as(guard.0.as_ref().map(|r| &**r)),
"Locked::read_with called with a guard from an unrelated SharedRwLock"
self.is_read_only_lock() || self.same_lock_as(guard.ptr()),
"Locked::read_with called with a guard from an unrelated SharedRwLock: {:?} vs. {:?}",
self.shared_lock.ptr(),
guard.ptr(),
);
#[cfg(not(feature = "gecko"))]
assert!(self.same_lock_as(&guard.0));
@ -235,7 +242,7 @@ impl<T> Locked<T> {
pub fn write_with<'a>(&'a self, guard: &'a mut SharedRwLockWriteGuard) -> &'a mut T {
#[cfg(feature = "gecko")]
assert!(
!self.is_read_only_lock() && self.same_lock_as(Some(&guard.0)),
!self.is_read_only_lock() && self.same_lock_as(&*guard.0),
"Locked::write_with called with a guard from a read only or unrelated SharedRwLock"
);
#[cfg(not(feature = "gecko"))]