Ignore lockorder violation on same callsite with different construction
[rust-lightning] / lightning / src / sync / debug_sync.rs
1 pub use ::alloc::sync::Arc;
2 use core::ops::{Deref, DerefMut};
3 use core::time::Duration;
4
5 use std::cell::RefCell;
6
7 use std::sync::atomic::{AtomicUsize, Ordering};
8 use std::sync::Mutex as StdMutex;
9 use std::sync::MutexGuard as StdMutexGuard;
10 use std::sync::RwLock as StdRwLock;
11 use std::sync::RwLockReadGuard as StdRwLockReadGuard;
12 use std::sync::RwLockWriteGuard as StdRwLockWriteGuard;
13 use std::sync::Condvar as StdCondvar;
14
15 use crate::prelude::HashMap;
16
17 use super::{LockTestExt, LockHeldState};
18
19 #[cfg(feature = "backtrace")]
20 use {crate::prelude::hash_map, backtrace::Backtrace, std::sync::Once};
21
22 #[cfg(not(feature = "backtrace"))]
23 struct Backtrace{}
24 #[cfg(not(feature = "backtrace"))]
25 impl Backtrace { fn new() -> Backtrace { Backtrace {} } }
26
27 pub type LockResult<Guard> = Result<Guard, ()>;
28
29 pub struct Condvar {
30         inner: StdCondvar,
31 }
32
33 impl Condvar {
34         pub fn new() -> Condvar {
35                 Condvar { inner: StdCondvar::new() }
36         }
37
38         pub fn wait<'a, T>(&'a self, guard: MutexGuard<'a, T>) -> LockResult<MutexGuard<'a, T>> {
39                 let mutex: &'a Mutex<T> = guard.mutex;
40                 self.inner.wait(guard.into_inner()).map(|lock| MutexGuard { mutex, lock }).map_err(|_| ())
41         }
42
43         #[allow(unused)]
44         pub fn wait_timeout<'a, T>(&'a self, guard: MutexGuard<'a, T>, dur: Duration) -> LockResult<(MutexGuard<'a, T>, ())> {
45                 let mutex = guard.mutex;
46                 self.inner.wait_timeout(guard.into_inner(), dur).map(|(lock, _)| (MutexGuard { mutex, lock }, ())).map_err(|_| ())
47         }
48
49         pub fn notify_all(&self) { self.inner.notify_all(); }
50 }
51
52 thread_local! {
53         /// We track the set of locks currently held by a reference to their `LockMetadata`
54         static LOCKS_HELD: RefCell<HashMap<u64, Arc<LockMetadata>>> = RefCell::new(HashMap::new());
55 }
56 static LOCK_IDX: AtomicUsize = AtomicUsize::new(0);
57
58 #[cfg(feature = "backtrace")]
59 static mut LOCKS: Option<StdMutex<HashMap<String, Arc<LockMetadata>>>> = None;
60 #[cfg(feature = "backtrace")]
61 static LOCKS_INIT: Once = Once::new();
62
63 /// Metadata about a single lock, by id, the set of things locked-before it, and the backtrace of
64 /// when the Mutex itself was constructed.
65 struct LockMetadata {
66         lock_idx: u64,
67         locked_before: StdMutex<HashMap<u64, LockDep>>,
68         _lock_construction_bt: Backtrace,
69 }
70
71 struct LockDep {
72         lock: Arc<LockMetadata>,
73         /// lockdep_trace is unused unless we're building with `backtrace`, so we mark it _
74         _lockdep_trace: Backtrace,
75 }
76
77 // Locates the frame preceding the earliest `debug_sync` frame in the call stack. This ensures we
78 // can properly detect a lock's construction and acquiral callsites, since the latter may contain
79 // multiple `debug_sync` frames.
80 #[cfg(feature = "backtrace")]
81 fn locate_call_symbol(backtrace: &Backtrace) -> (String, Option<u32>) {
82         // Find the earliest `debug_sync` frame (or that is in our tests) and use the frame preceding it
83         // as the callsite. Note that the first few frames may be in the `backtrace` crate, so we have
84         // to ignore those.
85         let sync_mutex_constr_regex = regex::Regex::new(r"lightning.*debug_sync").unwrap();
86         let mut found_debug_sync = false;
87         let mut symbol_after_latest_debug_sync = None;
88         for frame in backtrace.frames().iter() {
89                 for symbol in frame.symbols().iter() {
90                         if let Some(symbol_name) = symbol.name().map(|name| name.as_str()).flatten() {
91                                 if !sync_mutex_constr_regex.is_match(symbol_name) {
92                                         if found_debug_sync {
93                                                 symbol_after_latest_debug_sync = Some(symbol);
94                                                 found_debug_sync = false;
95                                         }
96                                 } else { found_debug_sync = true; }
97                         }
98                 }
99         }
100         let symbol = symbol_after_latest_debug_sync.expect("Couldn't find lock call symbol");
101         (format!("{}:{}", symbol.filename().unwrap().display(), symbol.lineno().unwrap()), symbol.colno())
102 }
103
104 impl LockMetadata {
105         fn new() -> Arc<LockMetadata> {
106                 let backtrace = Backtrace::new();
107                 let lock_idx = LOCK_IDX.fetch_add(1, Ordering::Relaxed) as u64;
108
109                 let res = Arc::new(LockMetadata {
110                         locked_before: StdMutex::new(HashMap::new()),
111                         lock_idx,
112                         _lock_construction_bt: backtrace,
113                 });
114
115                 #[cfg(feature = "backtrace")]
116                 {
117                         let (lock_constr_location, lock_constr_colno) =
118                                 locate_call_symbol(&res._lock_construction_bt);
119                         LOCKS_INIT.call_once(|| { unsafe { LOCKS = Some(StdMutex::new(HashMap::new())); } });
120                         let mut locks = unsafe { LOCKS.as_ref() }.unwrap().lock().unwrap();
121                         match locks.entry(lock_constr_location) {
122                                 hash_map::Entry::Occupied(e) => {
123                                         assert_eq!(lock_constr_colno,
124                                                 locate_call_symbol(&e.get()._lock_construction_bt).1,
125                                                 "Because Windows doesn't support column number results in backtraces, we cannot construct two mutexes on the same line or we risk lockorder detection false positives.");
126                                         return Arc::clone(e.get())
127                                 },
128                                 hash_map::Entry::Vacant(e) => { e.insert(Arc::clone(&res)); },
129                         }
130                 }
131                 res
132         }
133
134         fn pre_lock(this: &Arc<LockMetadata>, _double_lock_self_allowed: bool) {
135                 LOCKS_HELD.with(|held| {
136                         // For each lock that is currently held, check that no lock's `locked_before` set
137                         // includes the lock we're about to hold, which would imply a lockorder inversion.
138                         for (locked_idx, _locked) in held.borrow().iter() {
139                                 if *locked_idx == this.lock_idx {
140                                         // Note that with `feature = "backtrace"` set, we may be looking at different
141                                         // instances of the same lock. Still, doing so is quite risky, a total order
142                                         // must be maintained, and doing so across a set of otherwise-identical mutexes
143                                         // is fraught with issues.
144                                         #[cfg(feature = "backtrace")]
145                                         debug_assert!(_double_lock_self_allowed,
146                                                 "Tried to acquire a lock while it was held!\nLock constructed at {}",
147                                                 locate_call_symbol(&this._lock_construction_bt).0);
148                                         #[cfg(not(feature = "backtrace"))]
149                                         panic!("Tried to acquire a lock while it was held!");
150                                 }
151                         }
152                         for (_locked_idx, locked) in held.borrow().iter() {
153                                 for (locked_dep_idx, _locked_dep) in locked.locked_before.lock().unwrap().iter() {
154                                         let is_dep_this_lock = *locked_dep_idx == this.lock_idx;
155                                         let has_same_construction = *locked_dep_idx == locked.lock_idx;
156                                         if is_dep_this_lock && !has_same_construction {
157                                                 #[allow(unused_mut, unused_assignments)]
158                                                 let mut has_same_callsite = false;
159                                                 #[cfg(feature = "backtrace")] {
160                                                         has_same_callsite = _double_lock_self_allowed &&
161                                                                 locate_call_symbol(&_locked_dep._lockdep_trace) ==
162                                                                         locate_call_symbol(&Backtrace::new());
163                                                 }
164                                                 if !has_same_callsite {
165                                                         #[cfg(feature = "backtrace")]
166                                                         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 being taken constructed at: {} ({}):\n{:?}\nLock constructed at: {} ({})\n{:?}\n\nLock dep created at:\n{:?}\n\n",
167                                                                 locate_call_symbol(&this._lock_construction_bt).0,
168                                                                 this.lock_idx, this._lock_construction_bt,
169                                                                 locate_call_symbol(&locked._lock_construction_bt).0,
170                                                                 locked.lock_idx, locked._lock_construction_bt,
171                                                                 _locked_dep._lockdep_trace);
172                                                         #[cfg(not(feature = "backtrace"))]
173                                                         panic!("Tried to violate existing lockorder. Build with the backtrace feature for more info.");
174                                                 }
175                                         }
176                                 }
177                                 // Insert any already-held locks in our locked-before set.
178                                 let mut locked_before = this.locked_before.lock().unwrap();
179                                 if !locked_before.contains_key(&locked.lock_idx) {
180                                         let lockdep = LockDep { lock: Arc::clone(locked), _lockdep_trace: Backtrace::new() };
181                                         locked_before.insert(lockdep.lock.lock_idx, lockdep);
182                                 }
183                         }
184                         held.borrow_mut().insert(this.lock_idx, Arc::clone(this));
185                 });
186         }
187
188         fn held_by_thread(this: &Arc<LockMetadata>) -> LockHeldState {
189                 let mut res = LockHeldState::NotHeldByThread;
190                 LOCKS_HELD.with(|held| {
191                         for (locked_idx, _locked) in held.borrow().iter() {
192                                 if *locked_idx == this.lock_idx {
193                                         res = LockHeldState::HeldByThread;
194                                 }
195                         }
196                 });
197                 res
198         }
199
200         fn try_locked(this: &Arc<LockMetadata>) {
201                 LOCKS_HELD.with(|held| {
202                         // Since a try-lock will simply fail if the lock is held already, we do not
203                         // consider try-locks to ever generate lockorder inversions. However, if a try-lock
204                         // succeeds, we do consider it to have created lockorder dependencies.
205                         let mut locked_before = this.locked_before.lock().unwrap();
206                         for (locked_idx, locked) in held.borrow().iter() {
207                                 if !locked_before.contains_key(locked_idx) {
208                                         let lockdep = LockDep { lock: Arc::clone(locked), _lockdep_trace: Backtrace::new() };
209                                         locked_before.insert(*locked_idx, lockdep);
210                                 }
211                         }
212                         held.borrow_mut().insert(this.lock_idx, Arc::clone(this));
213                 });
214         }
215 }
216
217 pub struct Mutex<T: Sized> {
218         inner: StdMutex<T>,
219         deps: Arc<LockMetadata>,
220 }
221 impl<T: Sized> Mutex<T> {
222         pub(crate) fn into_inner(self) -> LockResult<T> {
223                 self.inner.into_inner().map_err(|_| ())
224         }
225 }
226
227 #[must_use = "if unused the Mutex will immediately unlock"]
228 pub struct MutexGuard<'a, T: Sized + 'a> {
229         mutex: &'a Mutex<T>,
230         lock: StdMutexGuard<'a, T>,
231 }
232
233 impl<'a, T: Sized> MutexGuard<'a, T> {
234         fn into_inner(self) -> StdMutexGuard<'a, T> {
235                 // Somewhat unclear why we cannot move out of self.lock, but doing so gets E0509.
236                 unsafe {
237                         let v: StdMutexGuard<'a, T> = std::ptr::read(&self.lock);
238                         std::mem::forget(self);
239                         v
240                 }
241         }
242 }
243
244 impl<T: Sized> Drop for MutexGuard<'_, T> {
245         fn drop(&mut self) {
246                 LOCKS_HELD.with(|held| {
247                         held.borrow_mut().remove(&self.mutex.deps.lock_idx);
248                 });
249         }
250 }
251
252 impl<T: Sized> Deref for MutexGuard<'_, T> {
253         type Target = T;
254
255         fn deref(&self) -> &T {
256                 &self.lock.deref()
257         }
258 }
259
260 impl<T: Sized> DerefMut for MutexGuard<'_, T> {
261         fn deref_mut(&mut self) -> &mut T {
262                 self.lock.deref_mut()
263         }
264 }
265
266 impl<T> Mutex<T> {
267         pub fn new(inner: T) -> Mutex<T> {
268                 Mutex { inner: StdMutex::new(inner), deps: LockMetadata::new() }
269         }
270
271         pub fn lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
272                 LockMetadata::pre_lock(&self.deps, false);
273                 self.inner.lock().map(|lock| MutexGuard { mutex: self, lock }).map_err(|_| ())
274         }
275
276         pub fn try_lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
277                 let res = self.inner.try_lock().map(|lock| MutexGuard { mutex: self, lock }).map_err(|_| ());
278                 if res.is_ok() {
279                         LockMetadata::try_locked(&self.deps);
280                 }
281                 res
282         }
283 }
284
285 impl<'a, T: 'a> LockTestExt<'a> for Mutex<T> {
286         #[inline]
287         fn held_by_thread(&self) -> LockHeldState {
288                 LockMetadata::held_by_thread(&self.deps)
289         }
290         type ExclLock = MutexGuard<'a, T>;
291         #[inline]
292         fn unsafe_well_ordered_double_lock_self(&'a self) -> MutexGuard<T> {
293                 LockMetadata::pre_lock(&self.deps, true);
294                 self.inner.lock().map(|lock| MutexGuard { mutex: self, lock }).unwrap()
295         }
296 }
297
298 pub struct RwLock<T: Sized> {
299         inner: StdRwLock<T>,
300         deps: Arc<LockMetadata>,
301 }
302
303 pub struct RwLockReadGuard<'a, T: Sized + 'a> {
304         lock: &'a RwLock<T>,
305         guard: StdRwLockReadGuard<'a, T>,
306 }
307
308 pub struct RwLockWriteGuard<'a, T: Sized + 'a> {
309         lock: &'a RwLock<T>,
310         guard: StdRwLockWriteGuard<'a, T>,
311 }
312
313 impl<T: Sized> Deref for RwLockReadGuard<'_, T> {
314         type Target = T;
315
316         fn deref(&self) -> &T {
317                 &self.guard.deref()
318         }
319 }
320
321 impl<T: Sized> Drop for RwLockReadGuard<'_, T> {
322         fn drop(&mut self) {
323                 LOCKS_HELD.with(|held| {
324                         held.borrow_mut().remove(&self.lock.deps.lock_idx);
325                 });
326         }
327 }
328
329 impl<T: Sized> Deref for RwLockWriteGuard<'_, T> {
330         type Target = T;
331
332         fn deref(&self) -> &T {
333                 &self.guard.deref()
334         }
335 }
336
337 impl<T: Sized> Drop for RwLockWriteGuard<'_, T> {
338         fn drop(&mut self) {
339                 LOCKS_HELD.with(|held| {
340                         held.borrow_mut().remove(&self.lock.deps.lock_idx);
341                 });
342         }
343 }
344
345 impl<T: Sized> DerefMut for RwLockWriteGuard<'_, T> {
346         fn deref_mut(&mut self) -> &mut T {
347                 self.guard.deref_mut()
348         }
349 }
350
351 impl<T> RwLock<T> {
352         pub fn new(inner: T) -> RwLock<T> {
353                 RwLock { inner: StdRwLock::new(inner), deps: LockMetadata::new() }
354         }
355
356         pub fn read<'a>(&'a self) -> LockResult<RwLockReadGuard<'a, T>> {
357                 // Note that while we could be taking a recursive read lock here, Rust's `RwLock` may
358                 // deadlock trying to take a second read lock if another thread is waiting on the write
359                 // lock. This behavior is platform dependent, but our in-tree `FairRwLock` guarantees
360                 // such a deadlock.
361                 LockMetadata::pre_lock(&self.deps, false);
362                 self.inner.read().map(|guard| RwLockReadGuard { lock: self, guard }).map_err(|_| ())
363         }
364
365         pub fn write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
366                 LockMetadata::pre_lock(&self.deps, false);
367                 self.inner.write().map(|guard| RwLockWriteGuard { lock: self, guard }).map_err(|_| ())
368         }
369
370         pub fn try_write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
371                 let res = self.inner.try_write().map(|guard| RwLockWriteGuard { lock: self, guard }).map_err(|_| ());
372                 if res.is_ok() {
373                         LockMetadata::try_locked(&self.deps);
374                 }
375                 res
376         }
377 }
378
379 impl<'a, T: 'a> LockTestExt<'a> for RwLock<T> {
380         #[inline]
381         fn held_by_thread(&self) -> LockHeldState {
382                 LockMetadata::held_by_thread(&self.deps)
383         }
384         type ExclLock = RwLockWriteGuard<'a, T>;
385         #[inline]
386         fn unsafe_well_ordered_double_lock_self(&'a self) -> RwLockWriteGuard<'a, T> {
387                 LockMetadata::pre_lock(&self.deps, true);
388                 self.inner.write().map(|guard| RwLockWriteGuard { lock: self, guard }).unwrap()
389         }
390 }
391
392 pub type FairRwLock<T> = RwLock<T>;