1 //@ignore-target-windows: no libc on Windows
2 //@compile-flags: -Zmiri-disable-isolation
3
4 #![feature(io_error_more)]
5 #![feature(io_error_uncategorized)]
6
7 use std::convert::TryInto;
8 use std::ffi::{c_char, CStr, CString};
9 use std::fs::{canonicalize, remove_dir_all, remove_file, File};
10 use std::io::{Error, ErrorKind, Write};
11 use std::os::unix::ffi::OsStrExt;
12 use std::path::PathBuf;
13
main()14 fn main() {
15 test_dup_stdout_stderr();
16 test_canonicalize_too_long();
17 test_readlink();
18 test_file_open_unix_allow_two_args();
19 test_file_open_unix_needs_three_args();
20 test_file_open_unix_extra_third_arg();
21 #[cfg(target_os = "linux")]
22 test_o_tmpfile_flag();
23 }
24
tmp() -> PathBuf25 fn tmp() -> PathBuf {
26 let path = std::env::var("MIRI_TEMP")
27 .unwrap_or_else(|_| std::env::temp_dir().into_os_string().into_string().unwrap());
28 // These are host paths. We need to convert them to the target.
29 let path = CString::new(path).unwrap();
30 let mut out = Vec::with_capacity(1024);
31
32 unsafe {
33 extern "Rust" {
34 fn miri_host_to_target_path(
35 path: *const c_char,
36 out: *mut c_char,
37 out_size: usize,
38 ) -> usize;
39 }
40 let ret = miri_host_to_target_path(path.as_ptr(), out.as_mut_ptr(), out.capacity());
41 assert_eq!(ret, 0);
42 let out = CStr::from_ptr(out.as_ptr()).to_str().unwrap();
43 PathBuf::from(out)
44 }
45 }
46
47 /// Prepare: compute filename and make sure the file does not exist.
prepare(filename: &str) -> PathBuf48 fn prepare(filename: &str) -> PathBuf {
49 let path = tmp().join(filename);
50 // Clean the paths for robustness.
51 remove_file(&path).ok();
52 path
53 }
54
55 /// Prepare directory: compute directory name and make sure it does not exist.
56 #[allow(unused)]
prepare_dir(dirname: &str) -> PathBuf57 fn prepare_dir(dirname: &str) -> PathBuf {
58 let path = tmp().join(&dirname);
59 // Clean the directory for robustness.
60 remove_dir_all(&path).ok();
61 path
62 }
63
64 /// Prepare like above, and also write some initial content to the file.
prepare_with_content(filename: &str, content: &[u8]) -> PathBuf65 fn prepare_with_content(filename: &str, content: &[u8]) -> PathBuf {
66 let path = prepare(filename);
67 let mut file = File::create(&path).unwrap();
68 file.write(content).unwrap();
69 path
70 }
71
test_file_open_unix_allow_two_args()72 fn test_file_open_unix_allow_two_args() {
73 let path = prepare_with_content("test_file_open_unix_allow_two_args.txt", &[]);
74
75 let mut name = path.into_os_string();
76 name.push("\0");
77 let name_ptr = name.as_bytes().as_ptr().cast::<libc::c_char>();
78 let _fd = unsafe { libc::open(name_ptr, libc::O_RDONLY) };
79 }
80
test_file_open_unix_needs_three_args()81 fn test_file_open_unix_needs_three_args() {
82 let path = prepare_with_content("test_file_open_unix_needs_three_args.txt", &[]);
83
84 let mut name = path.into_os_string();
85 name.push("\0");
86 let name_ptr = name.as_bytes().as_ptr().cast::<libc::c_char>();
87 let _fd = unsafe { libc::open(name_ptr, libc::O_CREAT, 0o666) };
88 }
89
test_file_open_unix_extra_third_arg()90 fn test_file_open_unix_extra_third_arg() {
91 let path = prepare_with_content("test_file_open_unix_extra_third_arg.txt", &[]);
92
93 let mut name = path.into_os_string();
94 name.push("\0");
95 let name_ptr = name.as_bytes().as_ptr().cast::<libc::c_char>();
96 let _fd = unsafe { libc::open(name_ptr, libc::O_RDONLY, 42) };
97 }
98
test_dup_stdout_stderr()99 fn test_dup_stdout_stderr() {
100 let bytes = b"hello dup fd\n";
101 unsafe {
102 let new_stdout = libc::fcntl(1, libc::F_DUPFD, 0);
103 let new_stderr = libc::fcntl(2, libc::F_DUPFD, 0);
104 libc::write(new_stdout, bytes.as_ptr() as *const libc::c_void, bytes.len());
105 libc::write(new_stderr, bytes.as_ptr() as *const libc::c_void, bytes.len());
106 }
107 }
108
test_canonicalize_too_long()109 fn test_canonicalize_too_long() {
110 // Make sure we get an error for long paths.
111 let too_long = "x/".repeat(libc::PATH_MAX.try_into().unwrap());
112 assert!(canonicalize(too_long).is_err());
113 }
114
test_readlink()115 fn test_readlink() {
116 let bytes = b"Hello, World!\n";
117 let path = prepare_with_content("miri_test_fs_link_target.txt", bytes);
118 let expected_path = path.as_os_str().as_bytes();
119
120 let symlink_path = prepare("miri_test_fs_symlink.txt");
121 std::os::unix::fs::symlink(&path, &symlink_path).unwrap();
122
123 // Test that the expected string gets written to a buffer of proper
124 // length, and that a trailing null byte is not written.
125 let symlink_c_str = CString::new(symlink_path.as_os_str().as_bytes()).unwrap();
126 let symlink_c_ptr = symlink_c_str.as_ptr();
127
128 // Make the buf one byte larger than it needs to be,
129 // and check that the last byte is not overwritten.
130 let mut large_buf = vec![0xFF; expected_path.len() + 1];
131 let res =
132 unsafe { libc::readlink(symlink_c_ptr, large_buf.as_mut_ptr().cast(), large_buf.len()) };
133 // Check that the resolved path was properly written into the buf.
134 assert_eq!(&large_buf[..(large_buf.len() - 1)], expected_path);
135 assert_eq!(large_buf.last(), Some(&0xFF));
136 assert_eq!(res, large_buf.len() as isize - 1);
137
138 // Test that the resolved path is truncated if the provided buffer
139 // is too small.
140 let mut small_buf = [0u8; 2];
141 let res =
142 unsafe { libc::readlink(symlink_c_ptr, small_buf.as_mut_ptr().cast(), small_buf.len()) };
143 assert_eq!(small_buf, &expected_path[..small_buf.len()]);
144 assert_eq!(res, small_buf.len() as isize);
145
146 // Test that we report a proper error for a missing path.
147 let bad_path = CString::new("MIRI_MISSING_FILE_NAME").unwrap();
148 let res = unsafe {
149 libc::readlink(bad_path.as_ptr(), small_buf.as_mut_ptr().cast(), small_buf.len())
150 };
151 assert_eq!(res, -1);
152 assert_eq!(Error::last_os_error().kind(), ErrorKind::NotFound);
153 }
154
155 #[cfg(target_os = "linux")]
test_o_tmpfile_flag()156 fn test_o_tmpfile_flag() {
157 use std::fs::{create_dir, OpenOptions};
158 use std::os::unix::fs::OpenOptionsExt;
159 let dir_path = prepare_dir("miri_test_fs_dir");
160 create_dir(&dir_path).unwrap();
161 // test that the `O_TMPFILE` custom flag gracefully errors instead of stopping execution
162 assert_eq!(
163 Some(libc::EOPNOTSUPP),
164 OpenOptions::new()
165 .read(true)
166 .write(true)
167 .custom_flags(libc::O_TMPFILE)
168 .open(dir_path)
169 .unwrap_err()
170 .raw_os_error(),
171 );
172 }
173