1 // ignore-windows: No libc on Windows
2 // compile-flags: -Zmiri-disable-isolation
4 #![feature(rustc_private)]
5 #![allow(unused)] // necessary on macos due to conditional compilation
7 use std::path::PathBuf;
12 std::env::var("MIRI_TEMP").map(PathBuf::from).unwrap_or_else(|_| std::env::temp_dir())
15 #[cfg(not(target_os = "macos"))]
16 fn test_posix_fadvise() {
17 use std::convert::TryInto;
18 use std::fs::{File, remove_file};
20 use std::os::unix::io::AsRawFd;
22 let path = tmp().join("miri_test_libc.txt");
23 // Cleanup before test
24 remove_file(&path).ok();
26 // Set up an open file
27 let mut file = File::create(&path).unwrap();
28 let bytes = b"Hello, World!\n";
29 file.write(bytes).unwrap();
31 // Test calling posix_fadvise on a file.
36 bytes.len().try_into().unwrap(),
37 libc::POSIX_FADV_DONTNEED,
41 remove_file(&path).unwrap();
42 assert_eq!(result, 0);
45 fn test_mutex_libc_init_recursive() {
47 let mut attr: libc::pthread_mutexattr_t = std::mem::zeroed();
48 assert_eq!(libc::pthread_mutexattr_init(&mut attr as *mut _), 0);
49 assert_eq!(libc::pthread_mutexattr_settype(&mut attr as *mut _, libc::PTHREAD_MUTEX_RECURSIVE), 0);
50 let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
51 assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mut attr as *mut _), 0);
52 assert_eq!(libc::pthread_mutex_lock(&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_unlock(&mut mutex as *mut _), 0);
56 assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
57 assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
58 assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
59 assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
60 assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), libc::EPERM);
61 assert_eq!(libc::pthread_mutex_destroy(&mut mutex as *mut _), 0);
62 assert_eq!(libc::pthread_mutexattr_destroy(&mut attr as *mut _), 0);
66 fn test_mutex_libc_init_normal() {
68 let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed();
69 assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_NORMAL), 0);
70 let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
71 assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0);
72 assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
73 assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), libc::EBUSY);
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_destroy(&mut mutex as *mut _), 0);
81 // Only linux provides PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
82 // libc for macOS just has the default PTHREAD_MUTEX_INITIALIZER.
83 #[cfg(target_os = "linux")]
84 fn test_mutex_libc_static_initializer_recursive() {
85 let mutex = std::cell::UnsafeCell::new(libc::PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
87 assert_eq!(libc::pthread_mutex_lock(mutex.get()), 0);
88 assert_eq!(libc::pthread_mutex_trylock(mutex.get()), 0);
89 assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
90 assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
91 assert_eq!(libc::pthread_mutex_trylock(mutex.get()), 0);
92 assert_eq!(libc::pthread_mutex_lock(mutex.get()), 0);
93 assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
94 assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
95 assert_eq!(libc::pthread_mutex_unlock(mutex.get()), libc::EPERM);
96 assert_eq!(libc::pthread_mutex_destroy(mutex.get()), 0);
100 // Testing the behavior of std::sync::RwLock does not fully exercise the pthread rwlock shims, we
101 // need to go a layer deeper and test the behavior of the libc functions, because
102 // std::sys::unix::rwlock::RWLock itself keeps track of write_locked and num_readers.
103 fn test_rwlock_libc_static_initializer() {
104 let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER);
106 assert_eq!(libc::pthread_rwlock_rdlock(rw.get()), 0);
107 assert_eq!(libc::pthread_rwlock_rdlock(rw.get()), 0);
108 assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
109 assert_eq!(libc::pthread_rwlock_tryrdlock(rw.get()), 0);
110 assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
111 assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), libc::EBUSY);
112 assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
114 assert_eq!(libc::pthread_rwlock_wrlock(rw.get()), 0);
115 assert_eq!(libc::pthread_rwlock_tryrdlock(rw.get()), libc::EBUSY);
116 assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), libc::EBUSY);
117 assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
119 assert_eq!(libc::pthread_rwlock_destroy(rw.get()), 0);
124 #[cfg(not(target_os = "macos"))]
125 test_posix_fadvise();
127 test_mutex_libc_init_recursive();
128 test_mutex_libc_init_normal();
129 test_rwlock_libc_static_initializer();
131 #[cfg(target_os = "linux")]
132 test_mutex_libc_static_initializer_recursive();