Skip to content

Commit fad52d8

Browse files
authored
Merge pull request #1944 from TheBlueMatt/2022-01-lockorder-windows-robust
Make `debug_sync` regex more robust
2 parents cd40078 + ab46f6b commit fad52d8

File tree

5 files changed

+112
-111
lines changed

5 files changed

+112
-111
lines changed

lightning/src/lib.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,7 @@ mod prelude {
175175
pub use alloc::string::ToString;
176176
}
177177

178-
#[cfg(all(not(feature = "_bench_unstable"), feature = "std", test))]
179-
mod debug_sync;
180178
#[cfg(all(not(feature = "_bench_unstable"), feature = "backtrace", feature = "std", test))]
181179
extern crate backtrace;
182180

183-
#[cfg(feature = "std")]
184-
mod sync {
185-
#[cfg(all(not(feature = "_bench_unstable"), test))]
186-
pub use crate::debug_sync::*;
187-
#[cfg(any(feature = "_bench_unstable", not(test)))]
188-
pub use ::std::sync::{Arc, Mutex, Condvar, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
189-
#[cfg(any(feature = "_bench_unstable", not(test)))]
190-
pub use crate::util::fairrwlock::FairRwLock;
191-
}
192-
193-
#[cfg(not(feature = "std"))]
194181
mod sync;

lightning/src/debug_sync.rs renamed to lightning/src/sync/debug_sync.rs

Lines changed: 1 addition & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ fn get_construction_location(backtrace: &Backtrace) -> String {
7777
// Find the first frame that is after `debug_sync` (or that is in our tests) and use
7878
// that as the mutex construction site. Note that the first few frames may be in
7979
// the `backtrace` crate, so we have to ignore those.
80-
let sync_mutex_constr_regex = regex::Regex::new(r"lightning.*debug_sync.*new").unwrap();
80+
let sync_mutex_constr_regex = regex::Regex::new(r"lightning.*debug_sync").unwrap();
8181
let mut found_debug_sync = false;
8282
for frame in backtrace.frames() {
8383
for symbol in frame.symbols() {
@@ -333,100 +333,3 @@ impl<T> RwLock<T> {
333333
}
334334

335335
pub type FairRwLock<T> = RwLock<T>;
336-
337-
mod tests {
338-
use super::{RwLock, Mutex};
339-
340-
#[test]
341-
#[should_panic]
342-
#[cfg(not(feature = "backtrace"))]
343-
fn recursive_lock_fail() {
344-
let mutex = Mutex::new(());
345-
let _a = mutex.lock().unwrap();
346-
let _b = mutex.lock().unwrap();
347-
}
348-
349-
#[test]
350-
fn recursive_read() {
351-
let lock = RwLock::new(());
352-
let _a = lock.read().unwrap();
353-
let _b = lock.read().unwrap();
354-
}
355-
356-
#[test]
357-
#[should_panic]
358-
fn lockorder_fail() {
359-
let a = Mutex::new(());
360-
let b = Mutex::new(());
361-
{
362-
let _a = a.lock().unwrap();
363-
let _b = b.lock().unwrap();
364-
}
365-
{
366-
let _b = b.lock().unwrap();
367-
let _a = a.lock().unwrap();
368-
}
369-
}
370-
371-
#[test]
372-
#[should_panic]
373-
fn write_lockorder_fail() {
374-
let a = RwLock::new(());
375-
let b = RwLock::new(());
376-
{
377-
let _a = a.write().unwrap();
378-
let _b = b.write().unwrap();
379-
}
380-
{
381-
let _b = b.write().unwrap();
382-
let _a = a.write().unwrap();
383-
}
384-
}
385-
386-
#[test]
387-
#[should_panic]
388-
fn read_lockorder_fail() {
389-
let a = RwLock::new(());
390-
let b = RwLock::new(());
391-
{
392-
let _a = a.read().unwrap();
393-
let _b = b.read().unwrap();
394-
}
395-
{
396-
let _b = b.read().unwrap();
397-
let _a = a.read().unwrap();
398-
}
399-
}
400-
401-
#[test]
402-
fn read_recursive_no_lockorder() {
403-
// Like the above, but note that no lockorder is implied when we recursively read-lock a
404-
// RwLock, causing this to pass just fine.
405-
let a = RwLock::new(());
406-
let b = RwLock::new(());
407-
let _outer = a.read().unwrap();
408-
{
409-
let _a = a.read().unwrap();
410-
let _b = b.read().unwrap();
411-
}
412-
{
413-
let _b = b.read().unwrap();
414-
let _a = a.read().unwrap();
415-
}
416-
}
417-
418-
#[test]
419-
#[should_panic]
420-
fn read_write_lockorder_fail() {
421-
let a = RwLock::new(());
422-
let b = RwLock::new(());
423-
{
424-
let _a = a.write().unwrap();
425-
let _b = b.read().unwrap();
426-
}
427-
{
428-
let _b = b.read().unwrap();
429-
let _a = a.write().unwrap();
430-
}
431-
}
432-
}

lightning/src/sync/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#[cfg(all(feature = "std", not(feature = "_bench_unstable"), test))]
2+
mod debug_sync;
3+
#[cfg(all(feature = "std", not(feature = "_bench_unstable"), test))]
4+
pub use debug_sync::*;
5+
#[cfg(all(feature = "std", not(feature = "_bench_unstable"), test))]
6+
// Note that to make debug_sync's regex work this must not contain `debug_string` in the module name
7+
mod test_lockorder_checks;
8+
9+
#[cfg(all(feature = "std", any(feature = "_bench_unstable", not(test))))]
10+
pub use ::std::sync::{Arc, Mutex, Condvar, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
11+
#[cfg(all(feature = "std", any(feature = "_bench_unstable", not(test))))]
12+
pub use crate::util::fairrwlock::FairRwLock;
13+
14+
#[cfg(not(feature = "std"))]
15+
mod nostd_sync;
16+
#[cfg(not(feature = "std"))]
17+
pub use nostd_sync::*;
File renamed without changes.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
use crate::sync::debug_sync::{RwLock, Mutex};
2+
3+
#[test]
4+
#[should_panic]
5+
#[cfg(not(feature = "backtrace"))]
6+
fn recursive_lock_fail() {
7+
let mutex = Mutex::new(());
8+
let _a = mutex.lock().unwrap();
9+
let _b = mutex.lock().unwrap();
10+
}
11+
12+
#[test]
13+
fn recursive_read() {
14+
let lock = RwLock::new(());
15+
let _a = lock.read().unwrap();
16+
let _b = lock.read().unwrap();
17+
}
18+
19+
#[test]
20+
#[should_panic]
21+
fn lockorder_fail() {
22+
let a = Mutex::new(());
23+
let b = Mutex::new(());
24+
{
25+
let _a = a.lock().unwrap();
26+
let _b = b.lock().unwrap();
27+
}
28+
{
29+
let _b = b.lock().unwrap();
30+
let _a = a.lock().unwrap();
31+
}
32+
}
33+
34+
#[test]
35+
#[should_panic]
36+
fn write_lockorder_fail() {
37+
let a = RwLock::new(());
38+
let b = RwLock::new(());
39+
{
40+
let _a = a.write().unwrap();
41+
let _b = b.write().unwrap();
42+
}
43+
{
44+
let _b = b.write().unwrap();
45+
let _a = a.write().unwrap();
46+
}
47+
}
48+
49+
#[test]
50+
#[should_panic]
51+
fn read_lockorder_fail() {
52+
let a = RwLock::new(());
53+
let b = RwLock::new(());
54+
{
55+
let _a = a.read().unwrap();
56+
let _b = b.read().unwrap();
57+
}
58+
{
59+
let _b = b.read().unwrap();
60+
let _a = a.read().unwrap();
61+
}
62+
}
63+
64+
#[test]
65+
fn read_recursive_no_lockorder() {
66+
// Like the above, but note that no lockorder is implied when we recursively read-lock a
67+
// RwLock, causing this to pass just fine.
68+
let a = RwLock::new(());
69+
let b = RwLock::new(());
70+
let _outer = a.read().unwrap();
71+
{
72+
let _a = a.read().unwrap();
73+
let _b = b.read().unwrap();
74+
}
75+
{
76+
let _b = b.read().unwrap();
77+
let _a = a.read().unwrap();
78+
}
79+
}
80+
81+
#[test]
82+
#[should_panic]
83+
fn read_write_lockorder_fail() {
84+
let a = RwLock::new(());
85+
let b = RwLock::new(());
86+
{
87+
let _a = a.write().unwrap();
88+
let _b = b.read().unwrap();
89+
}
90+
{
91+
let _b = b.read().unwrap();
92+
let _a = a.write().unwrap();
93+
}
94+
}

0 commit comments

Comments
 (0)