]> git.lizzy.rs Git - rust.git/blob - src/shims/os_str.rs
move OsStr helpers to a separate file
[rust.git] / src / shims / os_str.rs
1 use std::borrow::Cow;
2 use std::convert::TryFrom;
3 use std::ffi::{OsStr, OsString};
4 use std::iter;
5 #[cfg(unix)]
6 use std::os::unix::ffi::{OsStrExt, OsStringExt};
7 #[cfg(windows)]
8 use std::os::windows::ffi::{OsStrExt, OsStringExt};
9 use std::path::{Path, PathBuf};
10
11 use rustc::ty::layout::LayoutOf;
12
13 use crate::*;
14
15 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
16 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
17     /// Helper function to read an OsString from a null-terminated sequence of bytes, which is what
18     /// the Unix APIs usually handle.
19     fn read_os_str_from_c_str<'a>(&'a self, scalar: Scalar<Tag>) -> InterpResult<'tcx, &'a OsStr>
20     where
21         'tcx: 'a,
22         'mir: 'a,
23     {
24         #[cfg(unix)]
25         fn bytes_to_os_str<'tcx, 'a>(bytes: &'a [u8]) -> InterpResult<'tcx, &'a OsStr> {
26             Ok(OsStr::from_bytes(bytes))
27         }
28         #[cfg(not(unix))]
29         fn bytes_to_os_str<'tcx, 'a>(bytes: &'a [u8]) -> InterpResult<'tcx, &'a OsStr> {
30             let s = std::str::from_utf8(bytes)
31                 .map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", bytes))?;
32             Ok(OsStr::new(s))
33         }
34
35         let this = self.eval_context_ref();
36         let bytes = this.memory.read_c_str(scalar)?;
37         bytes_to_os_str(bytes)
38     }
39
40     /// Helper function to read an OsString from a 0x0000-terminated sequence of u16,
41     /// which is what the Windows APIs usually handle.
42     fn read_os_str_from_wide_str<'a>(&'a self, scalar: Scalar<Tag>) -> InterpResult<'tcx, OsString>
43     where
44         'tcx: 'a,
45         'mir: 'a,
46     {
47         #[cfg(windows)]
48         pub fn u16vec_to_osstring<'tcx, 'a>(u16_vec: Vec<u16>) -> InterpResult<'tcx, OsString> {
49             Ok(OsString::from_wide(&u16_vec[..]))
50         }
51         #[cfg(not(windows))]
52         pub fn u16vec_to_osstring<'tcx, 'a>(u16_vec: Vec<u16>) -> InterpResult<'tcx, OsString> {
53             let s = String::from_utf16(&u16_vec[..])
54                 .map_err(|_| err_unsup_format!("{:?} is not a valid utf-16 string", u16_vec))?;
55             Ok(s.into())
56         }
57
58         let u16_vec = self.eval_context_ref().memory.read_wide_str(scalar)?;
59         u16vec_to_osstring(u16_vec)
60     }
61
62     /// Helper function to write an OsStr as a null-terminated sequence of bytes, which is what
63     /// the Unix APIs usually handle. This function returns `Ok((false, length))` without trying
64     /// to write if `size` is not large enough to fit the contents of `os_string` plus a null
65     /// terminator. It returns `Ok((true, length))` if the writing process was successful. The
66     /// string length returned does not include the null terminator.
67     fn write_os_str_to_c_str(
68         &mut self,
69         os_str: &OsStr,
70         scalar: Scalar<Tag>,
71         size: u64,
72     ) -> InterpResult<'tcx, (bool, u64)> {
73         #[cfg(unix)]
74         fn os_str_to_bytes<'tcx, 'a>(os_str: &'a OsStr) -> InterpResult<'tcx, &'a [u8]> {
75             Ok(os_str.as_bytes())
76         }
77         #[cfg(not(unix))]
78         fn os_str_to_bytes<'tcx, 'a>(os_str: &'a OsStr) -> InterpResult<'tcx, &'a [u8]> {
79             // On non-unix platforms the best we can do to transform bytes from/to OS strings is to do the
80             // intermediate transformation into strings. Which invalidates non-utf8 paths that are actually
81             // valid.
82             os_str
83                 .to_str()
84                 .map(|s| s.as_bytes())
85                 .ok_or_else(|| err_unsup_format!("{:?} is not a valid utf-8 string", os_str).into())
86         }
87
88         let bytes = os_str_to_bytes(os_str)?;
89         // If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required null
90         // terminator to memory using the `ptr` pointer would cause an out-of-bounds access.
91         let string_length = u64::try_from(bytes.len()).unwrap();
92         if size <= string_length {
93             return Ok((false, string_length));
94         }
95         self.eval_context_mut()
96             .memory
97             .write_bytes(scalar, bytes.iter().copied().chain(iter::once(0u8)))?;
98         Ok((true, string_length))
99     }
100
101     /// Helper function to write an OsStr as a 0x0000-terminated u16-sequence, which is what
102     /// the Windows APIs usually handle. This function returns `Ok((false, length))` without trying
103     /// to write if `size` is not large enough to fit the contents of `os_string` plus a null
104     /// terminator. It returns `Ok((true, length))` if the writing process was successful. The
105     /// string length returned does not include the null terminator.
106     fn write_os_str_to_wide_str(
107         &mut self,
108         os_str: &OsStr,
109         scalar: Scalar<Tag>,
110         size: u64,
111     ) -> InterpResult<'tcx, (bool, u64)> {
112         #[cfg(windows)]
113         fn os_str_to_u16vec<'tcx>(os_str: &OsStr) -> InterpResult<'tcx, Vec<u16>> {
114             Ok(os_str.encode_wide().collect())
115         }
116         #[cfg(not(windows))]
117         fn os_str_to_u16vec<'tcx>(os_str: &OsStr) -> InterpResult<'tcx, Vec<u16>> {
118             // On non-Windows platforms the best we can do to transform Vec<u16> from/to OS strings is to do the
119             // intermediate transformation into strings. Which invalidates non-utf8 paths that are actually
120             // valid.
121             os_str
122                 .to_str()
123                 .map(|s| s.encode_utf16().collect())
124                 .ok_or_else(|| err_unsup_format!("{:?} is not a valid utf-8 string", os_str).into())
125         }
126
127         let u16_vec = os_str_to_u16vec(os_str)?;
128         // If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required
129         // 0x0000 terminator to memory would cause an out-of-bounds access.
130         let string_length = u64::try_from(u16_vec.len()).unwrap();
131         if size <= string_length {
132             return Ok((false, string_length));
133         }
134
135         // Store the UTF-16 string.
136         self.eval_context_mut()
137             .memory
138             .write_u16s(scalar, u16_vec.into_iter().chain(iter::once(0x0000)))?;
139         Ok((true, string_length))
140     }
141
142     /// Allocate enough memory to store the given `OsStr` as a null-terminated sequence of bytes.
143     fn alloc_os_str_as_c_str(
144         &mut self,
145         os_str: &OsStr,
146         memkind: MemoryKind<MiriMemoryKind>,
147     ) -> Pointer<Tag> {
148         let size = u64::try_from(os_str.len()).unwrap().checked_add(1).unwrap(); // Make space for `0` terminator.
149         let this = self.eval_context_mut();
150
151         let arg_type = this.tcx.mk_array(this.tcx.types.u8, size);
152         let arg_place = this.allocate(this.layout_of(arg_type).unwrap(), memkind);
153         assert!(self.write_os_str_to_c_str(os_str, arg_place.ptr, size).unwrap().0);
154         arg_place.ptr.assert_ptr()
155     }
156
157     /// Allocate enough memory to store the given `OsStr` as a null-terminated sequence of `u16`.
158     fn alloc_os_str_as_wide_str(
159         &mut self,
160         os_str: &OsStr,
161         memkind: MemoryKind<MiriMemoryKind>,
162     ) -> Pointer<Tag> {
163         let size = u64::try_from(os_str.len()).unwrap().checked_add(1).unwrap(); // Make space for `0x0000` terminator.
164         let this = self.eval_context_mut();
165
166         let arg_type = this.tcx.mk_array(this.tcx.types.u16, size);
167         let arg_place = this.allocate(this.layout_of(arg_type).unwrap(), memkind);
168         assert!(self.write_os_str_to_wide_str(os_str, arg_place.ptr, size).unwrap().0);
169         arg_place.ptr.assert_ptr()
170     }
171
172     /// Read a null-terminated sequence of bytes, and perform path separator conversion if needed.
173     fn read_path_from_c_str<'a>(&'a self, scalar: Scalar<Tag>) -> InterpResult<'tcx, Cow<'a, Path>>
174     where
175         'tcx: 'a,
176         'mir: 'a,
177     {
178         let this = self.eval_context_ref();
179         let os_str = this.read_os_str_from_c_str(scalar)?;
180
181         #[cfg(windows)]
182         return Ok(if this.tcx.sess.target.target.target_os == "windows" {
183             // Windows-on-Windows, all fine.
184             Cow::Borrowed(Path::new(os_str))
185         } else {
186             // Unix target, Windows host. Need to convert target '/' to host '\'.
187             let converted = os_str
188                 .encode_wide()
189                 .map(|wchar| if wchar == '/' as u16 { '\\' as u16 } else { wchar })
190                 .collect::<Vec<_>>();
191             Cow::Owned(PathBuf::from(OsString::from_wide(&converted)))
192         });
193         #[cfg(unix)]
194         return Ok(if this.tcx.sess.target.target.target_os == "windows" {
195             // Windows target, Unix host. Need to convert target '\' to host '/'.
196             let converted = os_str
197                 .as_bytes()
198                 .iter()
199                 .map(|&wchar| if wchar == '/' as u8 { '\\' as u8 } else { wchar })
200                 .collect::<Vec<_>>();
201             Cow::Owned(PathBuf::from(OsString::from_vec(converted)))
202         } else {
203             // Unix-on-Unix, all is fine.
204             Cow::Borrowed(Path::new(os_str))
205         });
206     }
207
208     /// Write a Path to the machine memory, adjusting path separators if needed.
209     fn write_path_to_c_str(
210         &mut self,
211         path: &Path,
212         scalar: Scalar<Tag>,
213         size: u64,
214     ) -> InterpResult<'tcx, (bool, u64)> {
215         let this = self.eval_context_mut();
216
217         #[cfg(windows)]
218         let os_str = if this.tcx.sess.target.target.target_os == "windows" {
219             // Windows-on-Windows, all fine.
220             Cow::Borrowed(path.as_os_str())
221         } else {
222             // Unix target, Windows host. Need to convert host '\\' to target '/'.
223             let converted = path
224                 .as_os_str()
225                 .encode_wide()
226                 .map(|wchar| if wchar == '\\' as u16 { '/' as u16 } else { wchar })
227                 .collect::<Vec<_>>();
228             Cow::Owned(OsString::from_wide(&converted))
229         };
230         #[cfg(unix)]
231         let os_str = if this.tcx.sess.target.target.target_os == "windows" {
232             // Windows target, Unix host. Need to convert host '/' to target '\'.
233             let converted = path
234                 .as_os_str()
235                 .as_bytes()
236                 .iter()
237                 .map(|&wchar| if wchar == '/' as u8 { '\\' as u8 } else { wchar })
238                 .collect::<Vec<_>>();
239             Cow::Owned(OsString::from_vec(converted))
240         } else {
241             // Unix-on-Unix, all is fine.
242             Cow::Borrowed(path.as_os_str())
243         };
244
245         this.write_os_str_to_c_str(&os_str, scalar, size)
246     }
247 }