about summary refs log tree commit diff
path: root/src/tools/miri/tests/pass-dep/libc/pthread-sync.rs
blob: 255944662940ddc1bb198890c4061a865cd707fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//@ignore-target: windows # No pthreads on Windows
// We use `yield` to test specific interleavings, so disable automatic preemption.
//@compile-flags: -Zmiri-deterministic-concurrency
#![feature(sync_unsafe_cell)]

use std::cell::SyncUnsafeCell;
use std::mem::MaybeUninit;
use std::{mem, ptr, thread};

fn main() {
    test_mutex_libc_init_recursive();
    test_mutex_libc_init_normal();
    test_mutex_libc_init_errorcheck();
    test_rwlock_libc_static_initializer();
    #[cfg(target_os = "linux")]
    test_mutex_libc_static_initializer_recursive();

    check_mutex();
    check_rwlock_write();
    check_rwlock_read_no_deadlock();
    check_cond();
    check_condattr();
}

// We want to only use pthread APIs here for easier testing.
// So we can't use `thread::scope`. That means panics can lead
// to a failure to join threads which can lead to further issues,
// so let's turn such unwinding into aborts.
struct AbortOnDrop;
impl AbortOnDrop {
    fn defuse(self) {
        mem::forget(self);
    }
}
impl Drop for AbortOnDrop {
    fn drop(&mut self) {
        std::process::abort();
    }
}

fn test_mutex_libc_init_recursive() {
    unsafe {
        let mut attr: libc::pthread_mutexattr_t = mem::zeroed();
        assert_eq!(libc::pthread_mutexattr_init(&mut attr as *mut _), 0);
        assert_eq!(
            libc::pthread_mutexattr_settype(&mut attr as *mut _, libc::PTHREAD_MUTEX_RECURSIVE),
            0,
        );
        let mut mutex: libc::pthread_mutex_t = mem::zeroed();
        assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mut attr as *mut _), 0);
        assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
        assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
        assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
        assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
        assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
        assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
        assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
        assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
        assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), libc::EPERM);
        assert_eq!(libc::pthread_mutex_destroy(&mut mutex as *mut _), 0);
        assert_eq!(libc::pthread_mutexattr_destroy(&mut attr as *mut _), 0);
    }
}

fn test_mutex_libc_init_normal() {
    unsafe {
        let mut mutexattr: libc::pthread_mutexattr_t = mem::zeroed();
        assert_eq!(
            libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, 0x12345678),
            libc::EINVAL,
        );
        assert_eq!(
            libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_NORMAL),
            0,
        );
        let mut mutex: libc::pthread_mutex_t = mem::zeroed();
        assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0);
        assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
        assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), libc::EBUSY);
        assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
        assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
        assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
        assert_eq!(libc::pthread_mutex_destroy(&mut mutex as *mut _), 0);
    }
}

fn test_mutex_libc_init_errorcheck() {
    unsafe {
        let mut mutexattr: libc::pthread_mutexattr_t = mem::zeroed();
        assert_eq!(
            libc::pthread_mutexattr_settype(
                &mut mutexattr as *mut _,
                libc::PTHREAD_MUTEX_ERRORCHECK,
            ),
            0,
        );
        let mut mutex: libc::pthread_mutex_t = mem::zeroed();
        assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0);
        assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
        assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), libc::EBUSY);
        assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), libc::EDEADLK);
        assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
        assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
        assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
        assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), libc::EPERM);
        assert_eq!(libc::pthread_mutex_destroy(&mut mutex as *mut _), 0);
    }
}

// Only linux provides PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
// libc for macOS just has the default PTHREAD_MUTEX_INITIALIZER.
#[cfg(target_os = "linux")]
fn test_mutex_libc_static_initializer_recursive() {
    let mutex = std::cell::UnsafeCell::new(libc::PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
    unsafe {
        assert_eq!(libc::pthread_mutex_lock(mutex.get()), 0);
        assert_eq!(libc::pthread_mutex_trylock(mutex.get()), 0);
        assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
        assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
        assert_eq!(libc::pthread_mutex_trylock(mutex.get()), 0);
        assert_eq!(libc::pthread_mutex_lock(mutex.get()), 0);
        assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
        assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
        assert_eq!(libc::pthread_mutex_unlock(mutex.get()), libc::EPERM);
        assert_eq!(libc::pthread_mutex_destroy(mutex.get()), 0);
    }
}

struct SendPtr<T> {
    ptr: *mut T,
}
unsafe impl<T> Send for SendPtr<T> {}
impl<T> Copy for SendPtr<T> {}
impl<T> Clone for SendPtr<T> {
    fn clone(&self) -> Self {
        *self
    }
}

fn check_mutex() {
    let bomb = AbortOnDrop;
    // Specifically *not* using `Arc` to make sure there is no synchronization apart from the mutex.
    unsafe {
        let data = SyncUnsafeCell::new((libc::PTHREAD_MUTEX_INITIALIZER, 0));
        let ptr = SendPtr { ptr: data.get() };
        let mut threads = Vec::new();

        for _ in 0..3 {
            let thread = thread::spawn(move || {
                let ptr = ptr; // circumvent per-field closure capture
                let mutexptr = ptr::addr_of_mut!((*ptr.ptr).0);
                assert_eq!(libc::pthread_mutex_lock(mutexptr), 0);
                thread::yield_now();
                (*ptr.ptr).1 += 1;
                assert_eq!(libc::pthread_mutex_unlock(mutexptr), 0);
            });
            threads.push(thread);
        }

        for thread in threads {
            thread.join().unwrap();
        }

        let mutexptr = ptr::addr_of_mut!((*ptr.ptr).0);
        assert_eq!(libc::pthread_mutex_trylock(mutexptr), 0);
        assert_eq!((*ptr.ptr).1, 3);
    }
    bomb.defuse();
}

fn check_rwlock_write() {
    let bomb = AbortOnDrop;
    unsafe {
        let data = SyncUnsafeCell::new((libc::PTHREAD_RWLOCK_INITIALIZER, 0));
        let ptr = SendPtr { ptr: data.get() };
        let mut threads = Vec::new();

        for _ in 0..3 {
            let thread = thread::spawn(move || {
                let ptr = ptr; // circumvent per-field closure capture
                let rwlockptr = ptr::addr_of_mut!((*ptr.ptr).0);
                assert_eq!(libc::pthread_rwlock_wrlock(rwlockptr), 0);
                thread::yield_now();
                (*ptr.ptr).1 += 1;
                assert_eq!(libc::pthread_rwlock_unlock(rwlockptr), 0);
            });
            threads.push(thread);

            let readthread = thread::spawn(move || {
                let ptr = ptr; // circumvent per-field closure capture
                let rwlockptr = ptr::addr_of_mut!((*ptr.ptr).0);
                assert_eq!(libc::pthread_rwlock_rdlock(rwlockptr), 0);
                thread::yield_now();
                let val = (*ptr.ptr).1;
                assert!(val >= 0 && val <= 3);
                assert_eq!(libc::pthread_rwlock_unlock(rwlockptr), 0);
            });
            threads.push(readthread);
        }

        for thread in threads {
            thread.join().unwrap();
        }

        let rwlockptr = ptr::addr_of_mut!((*ptr.ptr).0);
        assert_eq!(libc::pthread_rwlock_tryrdlock(rwlockptr), 0);
        assert_eq!((*ptr.ptr).1, 3);
    }
    bomb.defuse();
}

fn check_rwlock_read_no_deadlock() {
    let bomb = AbortOnDrop;
    unsafe {
        let l1 = SyncUnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER);
        let l1 = SendPtr { ptr: l1.get() };
        let l2 = SyncUnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER);
        let l2 = SendPtr { ptr: l2.get() };

        // acquire l1 and hold it until after the other thread is done
        assert_eq!(libc::pthread_rwlock_rdlock(l1.ptr), 0);
        let handle = thread::spawn(move || {
            let l1 = l1; // circumvent per-field closure capture
            let l2 = l2; // circumvent per-field closure capture
            // acquire l2 before the other thread
            assert_eq!(libc::pthread_rwlock_rdlock(l2.ptr), 0);
            thread::yield_now();
            assert_eq!(libc::pthread_rwlock_rdlock(l1.ptr), 0);
            thread::yield_now();
            assert_eq!(libc::pthread_rwlock_unlock(l1.ptr), 0);
            assert_eq!(libc::pthread_rwlock_unlock(l2.ptr), 0);
        });
        thread::yield_now();
        assert_eq!(libc::pthread_rwlock_rdlock(l2.ptr), 0);
        handle.join().unwrap();
    }
    bomb.defuse();
}

fn check_cond() {
    let bomb = AbortOnDrop;
    unsafe {
        let mut cond: MaybeUninit<libc::pthread_cond_t> = MaybeUninit::uninit();
        assert_eq!(libc::pthread_cond_init(cond.as_mut_ptr(), ptr::null()), 0);
        let cond = SendPtr { ptr: cond.as_mut_ptr() };

        let mut mutex: libc::pthread_mutex_t = libc::PTHREAD_MUTEX_INITIALIZER;
        let mutex = SendPtr { ptr: &mut mutex };

        let mut data = 0;
        let data = SendPtr { ptr: &mut data };

        let t = thread::spawn(move || {
            let mutex = mutex; // circumvent per-field closure capture
            let cond = cond;
            let data = data;
            assert_eq!(libc::pthread_mutex_lock(mutex.ptr), 0);
            assert!(data.ptr.read() == 0);
            data.ptr.write(1);
            libc::pthread_cond_wait(cond.ptr, mutex.ptr);
            assert!(data.ptr.read() == 3);
            data.ptr.write(4);
            assert_eq!(libc::pthread_mutex_unlock(mutex.ptr), 0);
        });

        thread::yield_now();

        assert_eq!(libc::pthread_mutex_lock(mutex.ptr), 0);
        assert!(data.ptr.read() == 1);
        data.ptr.write(2);
        assert_eq!(libc::pthread_cond_signal(cond.ptr), 0);
        thread::yield_now(); // the other thread wakes up but can't get the lock yet
        assert!(data.ptr.read() == 2);
        data.ptr.write(3);
        assert_eq!(libc::pthread_mutex_unlock(mutex.ptr), 0);

        thread::yield_now(); // now the other thread gets the lock back

        assert_eq!(libc::pthread_mutex_lock(mutex.ptr), 0);
        assert!(data.ptr.read() == 4);
        assert_eq!(libc::pthread_cond_broadcast(cond.ptr), 0); // just a smoke test
        assert_eq!(libc::pthread_mutex_unlock(mutex.ptr), 0);

        t.join().unwrap();
    }
    bomb.defuse();
}

fn check_condattr() {
    unsafe {
        // Just smoke-testing that these functions can be called.
        let mut attr: MaybeUninit<libc::pthread_condattr_t> = MaybeUninit::uninit();
        assert_eq!(libc::pthread_condattr_init(attr.as_mut_ptr()), 0);

        #[cfg(not(target_os = "macos"))] // setclock-getclock do not exist on macOS
        {
            let clock_id = libc::CLOCK_MONOTONIC;
            assert_eq!(libc::pthread_condattr_setclock(attr.as_mut_ptr(), clock_id), 0);
            let mut check_clock_id = MaybeUninit::<libc::clockid_t>::uninit();
            assert_eq!(
                libc::pthread_condattr_getclock(attr.as_mut_ptr(), check_clock_id.as_mut_ptr()),
                0
            );
            assert_eq!(check_clock_id.assume_init(), clock_id);
        }

        let mut cond: MaybeUninit<libc::pthread_cond_t> = MaybeUninit::uninit();
        assert_eq!(libc::pthread_cond_init(cond.as_mut_ptr(), attr.as_ptr()), 0);
        assert_eq!(libc::pthread_condattr_destroy(attr.as_mut_ptr()), 0);
        assert_eq!(libc::pthread_cond_destroy(cond.as_mut_ptr()), 0);
    }
}

// std::sync::RwLock does not even used pthread_rwlock any more.
// Do some smoke testing of the API surface.
fn test_rwlock_libc_static_initializer() {
    let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER);
    unsafe {
        assert_eq!(libc::pthread_rwlock_rdlock(rw.get()), 0);
        assert_eq!(libc::pthread_rwlock_rdlock(rw.get()), 0);
        assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
        assert_eq!(libc::pthread_rwlock_tryrdlock(rw.get()), 0);
        assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
        assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), libc::EBUSY);
        assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);

        assert_eq!(libc::pthread_rwlock_wrlock(rw.get()), 0);
        assert_eq!(libc::pthread_rwlock_tryrdlock(rw.get()), libc::EBUSY);
        assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), libc::EBUSY);
        assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);

        assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), 0);
        assert_eq!(libc::pthread_rwlock_tryrdlock(rw.get()), libc::EBUSY);
        assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), libc::EBUSY);
        assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);

        assert_eq!(libc::pthread_rwlock_destroy(rw.get()), 0);
    }
}