Skip to content

Commit 7c856f8

Browse files
committed
move libc pthread tests into separate file
1 parent 38002b6 commit 7c856f8

File tree

2 files changed

+127
-125
lines changed

2 files changed

+127
-125
lines changed

tests/pass/shims/libc.rs renamed to tests/pass/shims/libc-misc.rs

Lines changed: 0 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//@ignore-target-windows: No libc on Windows
22
//@compile-flags: -Zmiri-disable-isolation
33
#![feature(io_error_more)]
4-
#![feature(rustc_private)]
54

65
use std::fs::{remove_file, File};
76
use std::os::unix::io::AsRawFd;
@@ -161,122 +160,6 @@ fn test_sync_file_range() {
161160
assert_eq!(result_2, 0);
162161
}
163162

164-
fn test_mutex_libc_init_recursive() {
165-
unsafe {
166-
let mut attr: libc::pthread_mutexattr_t = std::mem::zeroed();
167-
assert_eq!(libc::pthread_mutexattr_init(&mut attr as *mut _), 0);
168-
assert_eq!(
169-
libc::pthread_mutexattr_settype(&mut attr as *mut _, libc::PTHREAD_MUTEX_RECURSIVE),
170-
0,
171-
);
172-
let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
173-
assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mut attr as *mut _), 0);
174-
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
175-
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
176-
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
177-
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
178-
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
179-
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
180-
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
181-
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
182-
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), libc::EPERM);
183-
assert_eq!(libc::pthread_mutex_destroy(&mut mutex as *mut _), 0);
184-
assert_eq!(libc::pthread_mutexattr_destroy(&mut attr as *mut _), 0);
185-
}
186-
}
187-
188-
fn test_mutex_libc_init_normal() {
189-
unsafe {
190-
let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed();
191-
assert_eq!(
192-
libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, 0x12345678),
193-
libc::EINVAL,
194-
);
195-
assert_eq!(
196-
libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_NORMAL),
197-
0,
198-
);
199-
let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
200-
assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0);
201-
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
202-
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), libc::EBUSY);
203-
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
204-
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
205-
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
206-
assert_eq!(libc::pthread_mutex_destroy(&mut mutex as *mut _), 0);
207-
}
208-
}
209-
210-
fn test_mutex_libc_init_errorcheck() {
211-
unsafe {
212-
let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed();
213-
assert_eq!(
214-
libc::pthread_mutexattr_settype(
215-
&mut mutexattr as *mut _,
216-
libc::PTHREAD_MUTEX_ERRORCHECK,
217-
),
218-
0,
219-
);
220-
let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
221-
assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0);
222-
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
223-
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), libc::EBUSY);
224-
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), libc::EDEADLK);
225-
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
226-
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
227-
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
228-
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), libc::EPERM);
229-
assert_eq!(libc::pthread_mutex_destroy(&mut mutex as *mut _), 0);
230-
}
231-
}
232-
233-
// Only linux provides PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
234-
// libc for macOS just has the default PTHREAD_MUTEX_INITIALIZER.
235-
#[cfg(target_os = "linux")]
236-
fn test_mutex_libc_static_initializer_recursive() {
237-
let mutex = std::cell::UnsafeCell::new(libc::PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
238-
unsafe {
239-
assert_eq!(libc::pthread_mutex_lock(mutex.get()), 0);
240-
assert_eq!(libc::pthread_mutex_trylock(mutex.get()), 0);
241-
assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
242-
assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
243-
assert_eq!(libc::pthread_mutex_trylock(mutex.get()), 0);
244-
assert_eq!(libc::pthread_mutex_lock(mutex.get()), 0);
245-
assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
246-
assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
247-
assert_eq!(libc::pthread_mutex_unlock(mutex.get()), libc::EPERM);
248-
assert_eq!(libc::pthread_mutex_destroy(mutex.get()), 0);
249-
}
250-
}
251-
252-
// Testing the behavior of std::sync::RwLock does not fully exercise the pthread rwlock shims, we
253-
// need to go a layer deeper and test the behavior of the libc functions, because
254-
// std::sys::unix::rwlock::RWLock itself keeps track of write_locked and num_readers.
255-
fn test_rwlock_libc_static_initializer() {
256-
let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER);
257-
unsafe {
258-
assert_eq!(libc::pthread_rwlock_rdlock(rw.get()), 0);
259-
assert_eq!(libc::pthread_rwlock_rdlock(rw.get()), 0);
260-
assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
261-
assert_eq!(libc::pthread_rwlock_tryrdlock(rw.get()), 0);
262-
assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
263-
assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), libc::EBUSY);
264-
assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
265-
266-
assert_eq!(libc::pthread_rwlock_wrlock(rw.get()), 0);
267-
assert_eq!(libc::pthread_rwlock_tryrdlock(rw.get()), libc::EBUSY);
268-
assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), libc::EBUSY);
269-
assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
270-
271-
assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), 0);
272-
assert_eq!(libc::pthread_rwlock_tryrdlock(rw.get()), libc::EBUSY);
273-
assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), libc::EBUSY);
274-
assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
275-
276-
assert_eq!(libc::pthread_rwlock_destroy(rw.get()), 0);
277-
}
278-
}
279-
280163
/// Tests whether each thread has its own `__errno_location`.
281164
fn test_thread_local_errno() {
282165
#[cfg(target_os = "linux")]
@@ -413,14 +296,6 @@ fn main() {
413296
#[cfg(any(target_os = "linux"))]
414297
test_sync_file_range();
415298

416-
test_mutex_libc_init_recursive();
417-
test_mutex_libc_init_normal();
418-
test_mutex_libc_init_errorcheck();
419-
test_rwlock_libc_static_initializer();
420-
421-
#[cfg(any(target_os = "linux"))]
422-
test_mutex_libc_static_initializer_recursive();
423-
424299
test_thread_local_errno();
425300

426301
#[cfg(any(target_os = "linux"))]

tests/pass/shims/pthreads.rs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//@ignore-target-windows: No libc on Windows
2+
3+
fn main() {
4+
test_mutex_libc_init_recursive();
5+
test_mutex_libc_init_normal();
6+
test_mutex_libc_init_errorcheck();
7+
test_rwlock_libc_static_initializer();
8+
9+
#[cfg(any(target_os = "linux"))]
10+
test_mutex_libc_static_initializer_recursive();
11+
}
12+
13+
fn test_mutex_libc_init_recursive() {
14+
unsafe {
15+
let mut attr: libc::pthread_mutexattr_t = std::mem::zeroed();
16+
assert_eq!(libc::pthread_mutexattr_init(&mut attr as *mut _), 0);
17+
assert_eq!(
18+
libc::pthread_mutexattr_settype(&mut attr as *mut _, libc::PTHREAD_MUTEX_RECURSIVE),
19+
0,
20+
);
21+
let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
22+
assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mut attr as *mut _), 0);
23+
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
24+
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
25+
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
26+
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
27+
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
28+
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
29+
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
30+
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
31+
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), libc::EPERM);
32+
assert_eq!(libc::pthread_mutex_destroy(&mut mutex as *mut _), 0);
33+
assert_eq!(libc::pthread_mutexattr_destroy(&mut attr as *mut _), 0);
34+
}
35+
}
36+
37+
fn test_mutex_libc_init_normal() {
38+
unsafe {
39+
let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed();
40+
assert_eq!(
41+
libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, 0x12345678),
42+
libc::EINVAL,
43+
);
44+
assert_eq!(
45+
libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_NORMAL),
46+
0,
47+
);
48+
let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
49+
assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0);
50+
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
51+
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), libc::EBUSY);
52+
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
53+
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
54+
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
55+
assert_eq!(libc::pthread_mutex_destroy(&mut mutex as *mut _), 0);
56+
}
57+
}
58+
59+
fn test_mutex_libc_init_errorcheck() {
60+
unsafe {
61+
let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed();
62+
assert_eq!(
63+
libc::pthread_mutexattr_settype(
64+
&mut mutexattr as *mut _,
65+
libc::PTHREAD_MUTEX_ERRORCHECK,
66+
),
67+
0,
68+
);
69+
let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
70+
assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0);
71+
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
72+
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), libc::EBUSY);
73+
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), libc::EDEADLK);
74+
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
75+
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
76+
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
77+
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), libc::EPERM);
78+
assert_eq!(libc::pthread_mutex_destroy(&mut mutex as *mut _), 0);
79+
}
80+
}
81+
82+
// Only linux provides PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
83+
// libc for macOS just has the default PTHREAD_MUTEX_INITIALIZER.
84+
#[cfg(target_os = "linux")]
85+
fn test_mutex_libc_static_initializer_recursive() {
86+
let mutex = std::cell::UnsafeCell::new(libc::PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
87+
unsafe {
88+
assert_eq!(libc::pthread_mutex_lock(mutex.get()), 0);
89+
assert_eq!(libc::pthread_mutex_trylock(mutex.get()), 0);
90+
assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
91+
assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
92+
assert_eq!(libc::pthread_mutex_trylock(mutex.get()), 0);
93+
assert_eq!(libc::pthread_mutex_lock(mutex.get()), 0);
94+
assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
95+
assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
96+
assert_eq!(libc::pthread_mutex_unlock(mutex.get()), libc::EPERM);
97+
assert_eq!(libc::pthread_mutex_destroy(mutex.get()), 0);
98+
}
99+
}
100+
101+
// Testing the behavior of std::sync::RwLock does not fully exercise the pthread rwlock shims, we
102+
// need to go a layer deeper and test the behavior of the libc functions, because
103+
// std::sys::unix::rwlock::RWLock itself keeps track of write_locked and num_readers.
104+
fn test_rwlock_libc_static_initializer() {
105+
let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER);
106+
unsafe {
107+
assert_eq!(libc::pthread_rwlock_rdlock(rw.get()), 0);
108+
assert_eq!(libc::pthread_rwlock_rdlock(rw.get()), 0);
109+
assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
110+
assert_eq!(libc::pthread_rwlock_tryrdlock(rw.get()), 0);
111+
assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
112+
assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), libc::EBUSY);
113+
assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
114+
115+
assert_eq!(libc::pthread_rwlock_wrlock(rw.get()), 0);
116+
assert_eq!(libc::pthread_rwlock_tryrdlock(rw.get()), libc::EBUSY);
117+
assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), libc::EBUSY);
118+
assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
119+
120+
assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), 0);
121+
assert_eq!(libc::pthread_rwlock_tryrdlock(rw.get()), libc::EBUSY);
122+
assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), libc::EBUSY);
123+
assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
124+
125+
assert_eq!(libc::pthread_rwlock_destroy(rw.get()), 0);
126+
}
127+
}

0 commit comments

Comments
 (0)