From 2a42be3cc83fb45bbbaaa5dbfe60635526ba1675 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 27 May 2023 14:57:02 +0200 Subject: [PATCH] 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 --- components/style/shared_lock.rs | 35 ++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/components/style/shared_lock.rs b/components/style/shared_lock.rs index 5bfa7d19737..d524f0c6cdf 100644 --- a/components/style/shared_lock.rs +++ b/components/style/shared_lock.rs @@ -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(&self, data: T) -> Locked { 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 Locked { } #[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 Locked { 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"))]