]> git.lizzy.rs Git - rust.git/blobdiff - tests/run-pass/libc.rs
Move prctl test to the same file as other libc tests.
[rust.git] / tests / run-pass / libc.rs
index 5b7b37db327be78db0208d0ccf9d3df34464f27e..5873d429695002789469d8bbd031db673226e290 100644 (file)
@@ -3,18 +3,21 @@
 
 #![feature(rustc_private)]
 
-#[allow(unused)] // necessary on macos due to conditional compilation
 extern crate libc;
 
-#[cfg(not(target_os = "macos"))]
+#[cfg(target_os = "linux")]
+fn tmp() -> std::path::PathBuf {
+    std::env::var("MIRI_TEMP").map(std::path::PathBuf::from).unwrap_or_else(|_| std::env::temp_dir())
+}
+
+#[cfg(target_os = "linux")]
 fn test_posix_fadvise() {
     use std::convert::TryInto;
-    use std::env::temp_dir;
-    use std::fs::{File, remove_file};
+    use std::fs::{remove_file, File};
     use std::io::Write;
     use std::os::unix::io::AsRawFd;
 
-    let path = temp_dir().join("miri_test_libc.txt");
+    let path = tmp().join("miri_test_libc.txt");
     // Cleanup before test
     remove_file(&path).ok();
 
@@ -37,7 +40,133 @@ fn test_posix_fadvise() {
     assert_eq!(result, 0);
 }
 
+fn test_mutex_libc_init_recursive() {
+    unsafe {
+        let mut attr: libc::pthread_mutexattr_t = std::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 = std::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 = std::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 = std::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 = std::mem::zeroed();
+        assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_ERRORCHECK), 0);
+        let mut mutex: libc::pthread_mutex_t = std::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);
+    }
+}
+
+// Testing the behavior of std::sync::RwLock does not fully exercise the pthread rwlock shims, we
+// need to go a layer deeper and test the behavior of the libc functions, because
+// std::sys::unix::rwlock::RWLock itself keeps track of write_locked and num_readers.
+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);
+    }
+}
+
+/// Test whether the `prctl` shim correctly sets the thread name.
+///
+/// Note: `prctl` exists only on Linux.
+fn test_prctl_thread_name() {
+    use std::ffi::CString;
+    unsafe {
+        let thread_name = CString::new("hello").expect("CString::new failed");
+        assert_eq!(libc::prctl(libc::PR_SET_NAME, thread_name.as_ptr() as libc::c_long, 0 as libc::c_long, 0 as libc::c_long, 0 as libc::c_long), 0);
+        let mut buf = [0; 6];
+        assert_eq!(libc::prctl(libc::PR_GET_NAME, buf.as_mut_ptr() as libc::c_long, 0 as libc::c_long, 0 as libc::c_long, 0 as libc::c_long), 0);
+        assert_eq!(thread_name.as_bytes_with_nul(), buf);
+    }
+}
+
 fn main() {
-    #[cfg(not(target_os = "macos"))]
+    #[cfg(target_os = "linux")]
     test_posix_fadvise();
+
+    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();
+
+    #[cfg(target_os = "linux")]
+    test_prctl_thread_name();
 }