]> git.lizzy.rs Git - rust.git/blob - tests/run-pass/libc.rs
Auto merge of #1308 - RalfJung:miri, r=RalfJung
[rust.git] / tests / run-pass / libc.rs
1 // ignore-windows: No libc on Windows
2 // compile-flags: -Zmiri-disable-isolation
3
4 #![feature(rustc_private)]
5 #![allow(unused)] // necessary on macos due to conditional compilation
6
7 use std::path::PathBuf;
8
9 extern crate libc;
10
11 fn tmp() -> PathBuf {
12     std::env::var("MIRI_TEMP").map(PathBuf::from).unwrap_or_else(|_| std::env::temp_dir())
13 }
14
15 #[cfg(target_os = "linux")]
16 fn test_posix_fadvise() {
17     use std::convert::TryInto;
18     use std::fs::{remove_file, File};
19     use std::io::Write;
20     use std::os::unix::io::AsRawFd;
21
22     let path = tmp().join("miri_test_libc.txt");
23     // Cleanup before test
24     remove_file(&path).ok();
25
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();
30
31     // Test calling posix_fadvise on a file.
32     let result = unsafe {
33         libc::posix_fadvise(
34             file.as_raw_fd(),
35             0,
36             bytes.len().try_into().unwrap(),
37             libc::POSIX_FADV_DONTNEED,
38         )
39     };
40     drop(file);
41     remove_file(&path).unwrap();
42     assert_eq!(result, 0);
43 }
44
45 fn test_mutex_libc_init_recursive() {
46     unsafe {
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);
63     }
64 }
65
66 fn test_mutex_libc_init_normal() {
67     unsafe {
68         let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed();
69         assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, 0x12345678), libc::EINVAL);
70         assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_NORMAL), 0);
71         let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
72         assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0);
73         assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
74         assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), libc::EBUSY);
75         assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
76         assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
77         assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
78         assert_eq!(libc::pthread_mutex_destroy(&mut mutex as *mut _), 0);
79     }
80 }
81
82 fn test_mutex_libc_init_errorcheck() {
83     unsafe {
84         let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed();
85         assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_ERRORCHECK), 0);
86         let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
87         assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0);
88         assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
89         assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), libc::EBUSY);
90         assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), libc::EDEADLK);
91         assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
92         assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
93         assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
94         assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), libc::EPERM);
95         assert_eq!(libc::pthread_mutex_destroy(&mut mutex as *mut _), 0);
96     }
97 }
98
99 // Only linux provides PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
100 // libc for macOS just has the default PTHREAD_MUTEX_INITIALIZER.
101 #[cfg(target_os = "linux")]
102 fn test_mutex_libc_static_initializer_recursive() {
103     let mutex = std::cell::UnsafeCell::new(libc::PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
104     unsafe {
105         assert_eq!(libc::pthread_mutex_lock(mutex.get()), 0);
106         assert_eq!(libc::pthread_mutex_trylock(mutex.get()), 0);
107         assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
108         assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
109         assert_eq!(libc::pthread_mutex_trylock(mutex.get()), 0);
110         assert_eq!(libc::pthread_mutex_lock(mutex.get()), 0);
111         assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
112         assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
113         assert_eq!(libc::pthread_mutex_unlock(mutex.get()), libc::EPERM);
114         assert_eq!(libc::pthread_mutex_destroy(mutex.get()), 0);
115     }
116 }
117
118 // Testing the behavior of std::sync::RwLock does not fully exercise the pthread rwlock shims, we
119 // need to go a layer deeper and test the behavior of the libc functions, because
120 // std::sys::unix::rwlock::RWLock itself keeps track of write_locked and num_readers.
121 fn test_rwlock_libc_static_initializer() {
122     let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER);
123     unsafe {
124         assert_eq!(libc::pthread_rwlock_rdlock(rw.get()), 0);
125         assert_eq!(libc::pthread_rwlock_rdlock(rw.get()), 0);
126         assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
127         assert_eq!(libc::pthread_rwlock_tryrdlock(rw.get()), 0);
128         assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
129         assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), libc::EBUSY);
130         assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
131
132         assert_eq!(libc::pthread_rwlock_wrlock(rw.get()), 0);
133         assert_eq!(libc::pthread_rwlock_tryrdlock(rw.get()), libc::EBUSY);
134         assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), libc::EBUSY);
135         assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
136
137         assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), 0);
138         assert_eq!(libc::pthread_rwlock_tryrdlock(rw.get()), libc::EBUSY);
139         assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), libc::EBUSY);
140         assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
141
142         assert_eq!(libc::pthread_rwlock_destroy(rw.get()), 0);
143     }
144 }
145
146 fn main() {
147     #[cfg(target_os = "linux")]
148     test_posix_fadvise();
149
150     test_mutex_libc_init_recursive();
151     test_mutex_libc_init_normal();
152     test_mutex_libc_init_errorcheck();
153     test_rwlock_libc_static_initializer();
154
155     #[cfg(target_os = "linux")]
156     test_mutex_libc_static_initializer_recursive();
157 }