]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/src/shims/os_str.rs
Rollup merge of #106971 - oli-obk:tait_error, r=davidtwco
[rust.git] / src / tools / miri / src / shims / os_str.rs
1 use std::borrow::Cow;
2 use std::ffi::{OsStr, OsString};
3 use std::path::{Path, PathBuf};
4
5 #[cfg(unix)]
6 use std::os::unix::ffi::{OsStrExt, OsStringExt};
7 #[cfg(windows)]
8 use std::os::windows::ffi::{OsStrExt, OsStringExt};
9
10 use rustc_middle::ty::layout::LayoutOf;
11
12 use crate::*;
13
14 /// Represent how path separator conversion should be done.
15 pub enum PathConversion {
16     HostToTarget,
17     TargetToHost,
18 }
19
20 #[cfg(unix)]
21 pub fn os_str_to_bytes<'tcx>(os_str: &OsStr) -> InterpResult<'tcx, &[u8]> {
22     Ok(os_str.as_bytes())
23 }
24
25 #[cfg(not(unix))]
26 pub fn os_str_to_bytes<'tcx>(os_str: &OsStr) -> InterpResult<'tcx, &[u8]> {
27     // On non-unix platforms the best we can do to transform bytes from/to OS strings is to do the
28     // intermediate transformation into strings. Which invalidates non-utf8 paths that are actually
29     // valid.
30     os_str
31         .to_str()
32         .map(|s| s.as_bytes())
33         .ok_or_else(|| err_unsup_format!("{:?} is not a valid utf-8 string", os_str).into())
34 }
35
36 #[cfg(unix)]
37 pub fn bytes_to_os_str<'tcx>(bytes: &[u8]) -> InterpResult<'tcx, &OsStr> {
38     Ok(OsStr::from_bytes(bytes))
39 }
40 #[cfg(not(unix))]
41 pub fn bytes_to_os_str<'tcx>(bytes: &[u8]) -> InterpResult<'tcx, &OsStr> {
42     let s = std::str::from_utf8(bytes)
43         .map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", bytes))?;
44     Ok(OsStr::new(s))
45 }
46
47 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
48 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
49     /// Helper function to read an OsString from a null-terminated sequence of bytes, which is what
50     /// the Unix APIs usually handle.
51     fn read_os_str_from_c_str<'a>(
52         &'a self,
53         ptr: Pointer<Option<Provenance>>,
54     ) -> InterpResult<'tcx, &'a OsStr>
55     where
56         'tcx: 'a,
57         'mir: 'a,
58     {
59         let this = self.eval_context_ref();
60         let bytes = this.read_c_str(ptr)?;
61         bytes_to_os_str(bytes)
62     }
63
64     /// Helper function to read an OsString from a 0x0000-terminated sequence of u16,
65     /// which is what the Windows APIs usually handle.
66     fn read_os_str_from_wide_str<'a>(
67         &'a self,
68         ptr: Pointer<Option<Provenance>>,
69     ) -> InterpResult<'tcx, OsString>
70     where
71         'tcx: 'a,
72         'mir: 'a,
73     {
74         #[cfg(windows)]
75         pub fn u16vec_to_osstring<'tcx>(u16_vec: Vec<u16>) -> InterpResult<'tcx, OsString> {
76             Ok(OsString::from_wide(&u16_vec[..]))
77         }
78         #[cfg(not(windows))]
79         pub fn u16vec_to_osstring<'tcx>(u16_vec: Vec<u16>) -> InterpResult<'tcx, OsString> {
80             let s = String::from_utf16(&u16_vec[..])
81                 .map_err(|_| err_unsup_format!("{:?} is not a valid utf-16 string", u16_vec))?;
82             Ok(s.into())
83         }
84
85         let u16_vec = self.eval_context_ref().read_wide_str(ptr)?;
86         u16vec_to_osstring(u16_vec)
87     }
88
89     /// Helper function to write an OsStr as a null-terminated sequence of bytes, which is what
90     /// the Unix APIs usually handle. This function returns `Ok((false, length))` without trying
91     /// to write if `size` is not large enough to fit the contents of `os_string` plus a null
92     /// terminator. It returns `Ok((true, length))` if the writing process was successful. The
93     /// string length returned does include the null terminator.
94     fn write_os_str_to_c_str(
95         &mut self,
96         os_str: &OsStr,
97         ptr: Pointer<Option<Provenance>>,
98         size: u64,
99     ) -> InterpResult<'tcx, (bool, u64)> {
100         let bytes = os_str_to_bytes(os_str)?;
101         self.eval_context_mut().write_c_str(bytes, ptr, size)
102     }
103
104     /// Helper function to write an OsStr as a 0x0000-terminated u16-sequence, which is what the
105     /// Windows APIs usually handle.
106     ///
107     /// If `truncate == false` (the usual mode of operation), this function returns `Ok((false,
108     /// length))` without trying to write if `size` is not large enough to fit the contents of
109     /// `os_string` plus a null terminator. It returns `Ok((true, length))` if the writing process
110     /// was successful. The string length returned does include the null terminator. Length is
111     /// measured in units of `u16.`
112     ///
113     /// If `truncate == true`, then in case `size` is not large enough it *will* write the first
114     /// `size.saturating_sub(1)` many items, followed by a null terminator (if `size > 0`).
115     fn write_os_str_to_wide_str(
116         &mut self,
117         os_str: &OsStr,
118         ptr: Pointer<Option<Provenance>>,
119         size: u64,
120         truncate: bool,
121     ) -> InterpResult<'tcx, (bool, u64)> {
122         #[cfg(windows)]
123         fn os_str_to_u16vec<'tcx>(os_str: &OsStr) -> InterpResult<'tcx, Vec<u16>> {
124             Ok(os_str.encode_wide().collect())
125         }
126         #[cfg(not(windows))]
127         fn os_str_to_u16vec<'tcx>(os_str: &OsStr) -> InterpResult<'tcx, Vec<u16>> {
128             // On non-Windows platforms the best we can do to transform Vec<u16> from/to OS strings is to do the
129             // intermediate transformation into strings. Which invalidates non-utf8 paths that are actually
130             // valid.
131             os_str
132                 .to_str()
133                 .map(|s| s.encode_utf16().collect())
134                 .ok_or_else(|| err_unsup_format!("{:?} is not a valid utf-8 string", os_str).into())
135         }
136
137         let u16_vec = os_str_to_u16vec(os_str)?;
138         let (written, size_needed) = self.eval_context_mut().write_wide_str(&u16_vec, ptr, size)?;
139         if truncate && !written && size > 0 {
140             // Write the truncated part that fits.
141             let truncated_data = &u16_vec[..size.saturating_sub(1).try_into().unwrap()];
142             let (written, written_len) =
143                 self.eval_context_mut().write_wide_str(truncated_data, ptr, size)?;
144             assert!(written && written_len == size);
145         }
146         Ok((written, size_needed))
147     }
148
149     /// Allocate enough memory to store the given `OsStr` as a null-terminated sequence of bytes.
150     fn alloc_os_str_as_c_str(
151         &mut self,
152         os_str: &OsStr,
153         memkind: MemoryKind<MiriMemoryKind>,
154     ) -> InterpResult<'tcx, Pointer<Option<Provenance>>> {
155         let size = u64::try_from(os_str.len()).unwrap().checked_add(1).unwrap(); // Make space for `0` terminator.
156         let this = self.eval_context_mut();
157
158         let arg_type = this.tcx.mk_array(this.tcx.types.u8, size);
159         let arg_place = this.allocate(this.layout_of(arg_type).unwrap(), memkind)?;
160         let (written, _) = self.write_os_str_to_c_str(os_str, arg_place.ptr, size).unwrap();
161         assert!(written);
162         Ok(arg_place.ptr)
163     }
164
165     /// Allocate enough memory to store the given `OsStr` as a null-terminated sequence of `u16`.
166     fn alloc_os_str_as_wide_str(
167         &mut self,
168         os_str: &OsStr,
169         memkind: MemoryKind<MiriMemoryKind>,
170     ) -> InterpResult<'tcx, Pointer<Option<Provenance>>> {
171         let size = u64::try_from(os_str.len()).unwrap().checked_add(1).unwrap(); // Make space for `0x0000` terminator.
172         let this = self.eval_context_mut();
173
174         let arg_type = this.tcx.mk_array(this.tcx.types.u16, size);
175         let arg_place = this.allocate(this.layout_of(arg_type).unwrap(), memkind)?;
176         let (written, _) =
177             self.write_os_str_to_wide_str(os_str, arg_place.ptr, size, /*truncate*/ false).unwrap();
178         assert!(written);
179         Ok(arg_place.ptr)
180     }
181
182     /// Read a null-terminated sequence of bytes, and perform path separator conversion if needed.
183     fn read_path_from_c_str<'a>(
184         &'a self,
185         ptr: Pointer<Option<Provenance>>,
186     ) -> InterpResult<'tcx, Cow<'a, Path>>
187     where
188         'tcx: 'a,
189         'mir: 'a,
190     {
191         let this = self.eval_context_ref();
192         let os_str = this.read_os_str_from_c_str(ptr)?;
193
194         Ok(match this.convert_path(Cow::Borrowed(os_str), PathConversion::TargetToHost) {
195             Cow::Borrowed(x) => Cow::Borrowed(Path::new(x)),
196             Cow::Owned(y) => Cow::Owned(PathBuf::from(y)),
197         })
198     }
199
200     /// Read a null-terminated sequence of `u16`s, and perform path separator conversion if needed.
201     fn read_path_from_wide_str(
202         &self,
203         ptr: Pointer<Option<Provenance>>,
204     ) -> InterpResult<'tcx, PathBuf> {
205         let this = self.eval_context_ref();
206         let os_str = this.read_os_str_from_wide_str(ptr)?;
207
208         Ok(this.convert_path(Cow::Owned(os_str), PathConversion::TargetToHost).into_owned().into())
209     }
210
211     /// Write a Path to the machine memory (as a null-terminated sequence of bytes),
212     /// adjusting path separators if needed.
213     fn write_path_to_c_str(
214         &mut self,
215         path: &Path,
216         ptr: Pointer<Option<Provenance>>,
217         size: u64,
218     ) -> InterpResult<'tcx, (bool, u64)> {
219         let this = self.eval_context_mut();
220         let os_str =
221             this.convert_path(Cow::Borrowed(path.as_os_str()), PathConversion::HostToTarget);
222         this.write_os_str_to_c_str(&os_str, ptr, size)
223     }
224
225     /// Write a Path to the machine memory (as a null-terminated sequence of `u16`s),
226     /// adjusting path separators if needed.
227     fn write_path_to_wide_str(
228         &mut self,
229         path: &Path,
230         ptr: Pointer<Option<Provenance>>,
231         size: u64,
232         truncate: bool,
233     ) -> InterpResult<'tcx, (bool, u64)> {
234         let this = self.eval_context_mut();
235         let os_str =
236             this.convert_path(Cow::Borrowed(path.as_os_str()), PathConversion::HostToTarget);
237         this.write_os_str_to_wide_str(&os_str, ptr, size, truncate)
238     }
239
240     /// Allocate enough memory to store a Path as a null-terminated sequence of bytes,
241     /// adjusting path separators if needed.
242     fn alloc_path_as_c_str(
243         &mut self,
244         path: &Path,
245         memkind: MemoryKind<MiriMemoryKind>,
246     ) -> InterpResult<'tcx, Pointer<Option<Provenance>>> {
247         let this = self.eval_context_mut();
248         let os_str =
249             this.convert_path(Cow::Borrowed(path.as_os_str()), PathConversion::HostToTarget);
250         this.alloc_os_str_as_c_str(&os_str, memkind)
251     }
252
253     /// Allocate enough memory to store a Path as a null-terminated sequence of `u16`s,
254     /// adjusting path separators if needed.
255     fn alloc_path_as_wide_str(
256         &mut self,
257         path: &Path,
258         memkind: MemoryKind<MiriMemoryKind>,
259     ) -> InterpResult<'tcx, Pointer<Option<Provenance>>> {
260         let this = self.eval_context_mut();
261         let os_str =
262             this.convert_path(Cow::Borrowed(path.as_os_str()), PathConversion::HostToTarget);
263         this.alloc_os_str_as_wide_str(&os_str, memkind)
264     }
265
266     #[allow(clippy::get_first)]
267     fn convert_path<'a>(
268         &self,
269         os_str: Cow<'a, OsStr>,
270         direction: PathConversion,
271     ) -> Cow<'a, OsStr> {
272         let this = self.eval_context_ref();
273         let target_os = &this.tcx.sess.target.os;
274
275         #[cfg(windows)]
276         return if target_os == "windows" {
277             // Windows-on-Windows, all fine.
278             os_str
279         } else {
280             // Unix target, Windows host.
281             let (from, to) = match direction {
282                 PathConversion::HostToTarget => ('\\', '/'),
283                 PathConversion::TargetToHost => ('/', '\\'),
284             };
285             let mut converted = os_str
286                 .encode_wide()
287                 .map(|wchar| if wchar == from as u16 { to as u16 } else { wchar })
288                 .collect::<Vec<_>>();
289             // We also have to ensure that absolute paths remain absolute.
290             match direction {
291                 PathConversion::HostToTarget => {
292                     // If this is an absolute Windows path that starts with a drive letter (`C:/...`
293                     // after separator conversion), it would not be considered absolute by Unix
294                     // target code.
295                     if converted.get(1).copied() == Some(b':' as u16)
296                         && converted.get(2).copied() == Some(b'/' as u16)
297                     {
298                         // We add a `/` at the beginning, to store the absolute Windows
299                         // path in something that looks like an absolute Unix path.
300                         converted.insert(0, b'/' as u16);
301                     }
302                 }
303                 PathConversion::TargetToHost => {
304                     // If the path is `\C:\`, the leading backslash was probably added by the above code
305                     // and we should get rid of it again.
306                     if converted.get(0).copied() == Some(b'\\' as u16)
307                         && converted.get(2).copied() == Some(b':' as u16)
308                         && converted.get(3).copied() == Some(b'\\' as u16)
309                     {
310                         converted.remove(0);
311                     }
312                 }
313             }
314             Cow::Owned(OsString::from_wide(&converted))
315         };
316         #[cfg(unix)]
317         return if target_os == "windows" {
318             // Windows target, Unix host.
319             let (from, to) = match direction {
320                 PathConversion::HostToTarget => (b'/', b'\\'),
321                 PathConversion::TargetToHost => (b'\\', b'/'),
322             };
323             let mut converted = os_str
324                 .as_bytes()
325                 .iter()
326                 .map(|&wchar| if wchar == from { to } else { wchar })
327                 .collect::<Vec<_>>();
328             // We also have to ensure that absolute paths remain absolute.
329             match direction {
330                 PathConversion::HostToTarget => {
331                     // If this start withs a `\`, we add `\\?` so it starts with `\\?\` which is
332                     // some magic path on Windos that *is* considered absolute.
333                     if converted.get(0).copied() == Some(b'\\') {
334                         converted.splice(0..0, b"\\\\?".iter().copied());
335                     }
336                 }
337                 PathConversion::TargetToHost => {
338                     // If this starts with `//?/`, it was probably produced by the above code and we
339                     // remove the `//?` that got added to get the Unix path back out.
340                     if converted.get(0).copied() == Some(b'/')
341                         && converted.get(1).copied() == Some(b'/')
342                         && converted.get(2).copied() == Some(b'?')
343                         && converted.get(3).copied() == Some(b'/')
344                     {
345                         // Remove first 3 characters
346                         converted.splice(0..3, std::iter::empty());
347                     }
348                 }
349             }
350             Cow::Owned(OsString::from_vec(converted))
351         } else {
352             // Unix-on-Unix, all is fine.
353             os_str
354         };
355     }
356 }