Skip to content

Commit fe39888

Browse files
committed
pthread-sync: avoid confusing error when running with preemption
1 parent b17cf41 commit fe39888

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/tools/miri/tests/pass-dep/libc/pthread-sync.rs

+24
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ fn main() {
2222
check_condattr();
2323
}
2424

25+
// We want to only use pthread APIs here for easier testing.
26+
// So we can't use `thread::scope`. That means panics can lead
27+
// to a failure to join threads which can lead to further issues,
28+
// so let's turn such unwinding into aborts.
29+
struct AbortOnDrop;
30+
impl AbortOnDrop {
31+
fn defuse(self) {
32+
mem::forget(self);
33+
}
34+
}
35+
impl Drop for AbortOnDrop {
36+
fn drop(&mut self) {
37+
std::process::abort();
38+
}
39+
}
40+
2541
fn test_mutex_libc_init_recursive() {
2642
unsafe {
2743
let mut attr: libc::pthread_mutexattr_t = mem::zeroed();
@@ -122,6 +138,7 @@ impl<T> Clone for SendPtr<T> {
122138
}
123139

124140
fn check_mutex() {
141+
let bomb = AbortOnDrop;
125142
// Specifically *not* using `Arc` to make sure there is no synchronization apart from the mutex.
126143
unsafe {
127144
let data = SyncUnsafeCell::new((libc::PTHREAD_MUTEX_INITIALIZER, 0));
@@ -148,9 +165,11 @@ fn check_mutex() {
148165
assert_eq!(libc::pthread_mutex_trylock(mutexptr), 0);
149166
assert_eq!((*ptr.ptr).1, 3);
150167
}
168+
bomb.defuse();
151169
}
152170

153171
fn check_rwlock_write() {
172+
let bomb = AbortOnDrop;
154173
unsafe {
155174
let data = SyncUnsafeCell::new((libc::PTHREAD_RWLOCK_INITIALIZER, 0));
156175
let ptr = SendPtr { ptr: data.get() };
@@ -187,9 +206,11 @@ fn check_rwlock_write() {
187206
assert_eq!(libc::pthread_rwlock_tryrdlock(rwlockptr), 0);
188207
assert_eq!((*ptr.ptr).1, 3);
189208
}
209+
bomb.defuse();
190210
}
191211

192212
fn check_rwlock_read_no_deadlock() {
213+
let bomb = AbortOnDrop;
193214
unsafe {
194215
let l1 = SyncUnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER);
195216
let l1 = SendPtr { ptr: l1.get() };
@@ -213,9 +234,11 @@ fn check_rwlock_read_no_deadlock() {
213234
assert_eq!(libc::pthread_rwlock_rdlock(l2.ptr), 0);
214235
handle.join().unwrap();
215236
}
237+
bomb.defuse();
216238
}
217239

218240
fn check_cond() {
241+
let bomb = AbortOnDrop;
219242
unsafe {
220243
let mut cond: MaybeUninit<libc::pthread_cond_t> = MaybeUninit::uninit();
221244
assert_eq!(libc::pthread_cond_init(cond.as_mut_ptr(), ptr::null()), 0);
@@ -260,6 +283,7 @@ fn check_cond() {
260283

261284
t.join().unwrap();
262285
}
286+
bomb.defuse();
263287
}
264288

265289
fn check_condattr() {

0 commit comments

Comments
 (0)