Skip to content

Commit 457f4d6

Browse files
committed
Expand lockorder testing to look at mutexes, not specific instances
Our existing lockorder inversion checks look at specific instances of mutexes rather than the general mutex itself. This changes that behavior to look at the instruction pointer at which a mutex was created and treat all mutexes which were created at the same location as equivalent. This allows us to detect lockorder inversions which occur across tests, though it does substantially reduce parallelism during test runs.
1 parent 0627c0c commit 457f4d6

File tree

1 file changed

+74
-17
lines changed

1 file changed

+74
-17
lines changed

lightning/src/debug_sync.rs

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use core::time::Duration;
55
use std::collections::HashSet;
66
use std::cell::RefCell;
77

8+
#[cfg(not(feature = "backtrace"))]
89
use std::sync::atomic::{AtomicUsize, Ordering};
910

1011
use std::sync::Mutex as StdMutex;
@@ -15,7 +16,12 @@ use std::sync::RwLockWriteGuard as StdRwLockWriteGuard;
1516
use std::sync::Condvar as StdCondvar;
1617

1718
#[cfg(feature = "backtrace")]
18-
use backtrace::Backtrace;
19+
use {prelude::HashMap, backtrace::Backtrace, std::sync::Once};
20+
21+
#[cfg(not(feature = "backtrace"))]
22+
struct Backtrace{}
23+
#[cfg(not(feature = "backtrace"))]
24+
impl Backtrace { fn new() -> Backtrace { Backtrace {} } }
1925

2026
pub type LockResult<Guard> = Result<Guard, ()>;
2127

@@ -46,14 +52,19 @@ thread_local! {
4652
/// We track the set of locks currently held by a reference to their `LockMetadata`
4753
static LOCKS_HELD: RefCell<HashSet<Arc<LockMetadata>>> = RefCell::new(HashSet::new());
4854
}
55+
#[cfg(not(feature = "backtrace"))]
4956
static LOCK_IDX: AtomicUsize = AtomicUsize::new(0);
5057

58+
#[cfg(feature = "backtrace")]
59+
static mut LOCKS: Option<StdMutex<HashMap<u64, Arc<LockMetadata>>>> = None;
60+
#[cfg(feature = "backtrace")]
61+
static LOCKS_INIT: Once = Once::new();
62+
5163
/// Metadata about a single lock, by id, the set of things locked-before it, and the backtrace of
5264
/// when the Mutex itself was constructed.
5365
struct LockMetadata {
5466
lock_idx: u64,
55-
locked_before: StdMutex<HashSet<Arc<LockMetadata>>>,
56-
#[cfg(feature = "backtrace")]
67+
locked_before: StdMutex<HashSet<LockDep>>,
5768
lock_construction_bt: Backtrace,
5869
}
5970
impl PartialEq for LockMetadata {
@@ -64,14 +75,47 @@ impl std::hash::Hash for LockMetadata {
6475
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) { hasher.write_u64(self.lock_idx); }
6576
}
6677

78+
struct LockDep {
79+
lock: Arc<LockMetadata>,
80+
lockdep_trace: Option<Backtrace>,
81+
}
82+
impl LockDep {
83+
/// Note that `Backtrace::new()` is rather expensive so we rely on the caller to fill in the
84+
/// `lockdep_backtrace` field after ensuring we need it.
85+
fn new_without_bt(lock: &Arc<LockMetadata>) -> Self {
86+
Self { lock: Arc::clone(lock), lockdep_trace: None }
87+
}
88+
}
89+
impl PartialEq for LockDep {
90+
fn eq(&self, o: &LockDep) -> bool { self.lock.lock_idx == o.lock.lock_idx }
91+
}
92+
impl Eq for LockDep {}
93+
impl std::hash::Hash for LockDep {
94+
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) { hasher.write_u64(self.lock.lock_idx); }
95+
}
96+
6797
impl LockMetadata {
68-
fn new() -> LockMetadata {
69-
LockMetadata {
70-
locked_before: StdMutex::new(HashSet::new()),
71-
lock_idx: LOCK_IDX.fetch_add(1, Ordering::Relaxed) as u64,
72-
#[cfg(feature = "backtrace")]
73-
lock_construction_bt: Backtrace::new(),
98+
fn new() -> Arc<LockMetadata> {
99+
#[cfg(not(feature = "backtrace"))]
100+
let lock_idx = LOCK_IDX.fetch_add(1, Ordering::Relaxed) as u64;
101+
let backtrace = Backtrace::new();
102+
103+
#[cfg(feature = "backtrace")]
104+
let lock_idx = backtrace.frames()[2].ip() as usize as u64;
105+
106+
#[cfg(feature = "backtrace")]
107+
{
108+
LOCKS_INIT.call_once(|| { unsafe { LOCKS = Some(StdMutex::new(HashMap::new())); } });
109+
if let Some(metadata) = unsafe { LOCKS.as_ref() }.unwrap().lock().unwrap().get(&lock_idx) {
110+
return Arc::clone(&metadata);
111+
}
74112
}
113+
114+
Arc::new(LockMetadata {
115+
locked_before: StdMutex::new(HashSet::new()),
116+
lock_idx,
117+
lock_construction_bt: backtrace,
118+
})
75119
}
76120

77121
// Returns whether we were a recursive lock (only relevant for read)
@@ -89,18 +133,25 @@ impl LockMetadata {
89133
}
90134
for locked in held.borrow().iter() {
91135
if !read && *locked == *this {
92-
panic!("Tried to lock a lock while it was held!");
136+
// With `feature = "backtrace"` set, we may be looking at different instances
137+
// of the same lock.
138+
debug_assert!(cfg!(feature = "backtrace"), "Tried to lock a lock while it was held!");
93139
}
94140
for locked_dep in locked.locked_before.lock().unwrap().iter() {
95-
if *locked_dep == *this {
141+
if locked_dep.lock == *this && locked_dep.lock != *locked {
96142
#[cfg(feature = "backtrace")]
97-
panic!("Tried to violate existing lockorder.\nMutex that should be locked after the current lock was created at the following backtrace.\nNote that to get a backtrace for the lockorder violation, you should set RUST_BACKTRACE=1\n{:?}", locked.lock_construction_bt);
143+
panic!("Tried to violate existing lockorder.\nMutex that should be locked after the current lock was created at the following backtrace.\nNote that to get a backtrace for the lockorder violation, you should set RUST_BACKTRACE=1\nLock constructed at:\n{:?}\n\nLock dep created at:\n{:?}\n\n", locked.lock_construction_bt, locked_dep.lockdep_trace);
98144
#[cfg(not(feature = "backtrace"))]
99145
panic!("Tried to violate existing lockorder. Build with the backtrace feature for more info.");
100146
}
101147
}
102148
// Insert any already-held locks in our locked-before set.
103-
this.locked_before.lock().unwrap().insert(Arc::clone(locked));
149+
let mut locked_before = this.locked_before.lock().unwrap();
150+
let mut lockdep = LockDep::new_without_bt(locked);
151+
if !locked_before.contains(&lockdep) {
152+
lockdep.lockdep_trace = Some(Backtrace::new());
153+
locked_before.insert(lockdep);
154+
}
104155
}
105156
held.borrow_mut().insert(Arc::clone(this));
106157
inserted = true;
@@ -116,10 +167,15 @@ impl LockMetadata {
116167
// Since a try-lock will simply fail if the lock is held already, we do not
117168
// consider try-locks to ever generate lockorder inversions. However, if a try-lock
118169
// succeeds, we do consider it to have created lockorder dependencies.
170+
held.borrow_mut().insert(Arc::clone(this));
171+
let mut locked_before = this.locked_before.lock().unwrap();
119172
for locked in held.borrow().iter() {
120-
this.locked_before.lock().unwrap().insert(Arc::clone(locked));
173+
let mut lockdep = LockDep::new_without_bt(locked);
174+
if !locked_before.contains(&lockdep) {
175+
lockdep.lockdep_trace = Some(Backtrace::new());
176+
locked_before.insert(lockdep);
177+
}
121178
}
122-
held.borrow_mut().insert(Arc::clone(this));
123179
});
124180
}
125181
}
@@ -170,7 +226,7 @@ impl<T: Sized> DerefMut for MutexGuard<'_, T> {
170226

171227
impl<T> Mutex<T> {
172228
pub fn new(inner: T) -> Mutex<T> {
173-
Mutex { inner: StdMutex::new(inner), deps: Arc::new(LockMetadata::new()) }
229+
Mutex { inner: StdMutex::new(inner), deps: LockMetadata::new() }
174230
}
175231

176232
pub fn lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
@@ -249,7 +305,7 @@ impl<T: Sized> DerefMut for RwLockWriteGuard<'_, T> {
249305

250306
impl<T> RwLock<T> {
251307
pub fn new(inner: T) -> RwLock<T> {
252-
RwLock { inner: StdRwLock::new(inner), deps: Arc::new(LockMetadata::new()) }
308+
RwLock { inner: StdRwLock::new(inner), deps: LockMetadata::new() }
253309
}
254310

255311
pub fn read<'a>(&'a self) -> LockResult<RwLockReadGuard<'a, T>> {
@@ -273,6 +329,7 @@ impl<T> RwLock<T> {
273329

274330
#[test]
275331
#[should_panic]
332+
#[cfg(not(feature = "backtrace"))]
276333
fn recursive_lock_fail() {
277334
let mutex = Mutex::new(());
278335
let _a = mutex.lock().unwrap();

0 commit comments

Comments
 (0)