2 use std::ffi::{OsStr, OsString};
3 use std::path::{Path, PathBuf};
6 use std::os::unix::ffi::{OsStrExt, OsStringExt};
8 use std::os::windows::ffi::{OsStrExt, OsStringExt};
10 use rustc_middle::ty::layout::LayoutOf;
14 /// Represent how path separator conversion should be done.
15 pub enum PathConversion {
21 pub fn os_str_to_bytes<'tcx>(os_str: &OsStr) -> InterpResult<'tcx, &[u8]> {
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
32 .map(|s| s.as_bytes())
33 .ok_or_else(|| err_unsup_format!("{:?} is not a valid utf-8 string", os_str).into())
37 pub fn bytes_to_os_str<'tcx>(bytes: &[u8]) -> InterpResult<'tcx, &OsStr> {
38 Ok(OsStr::from_bytes(bytes))
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))?;
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>(
53 ptr: Pointer<Option<Provenance>>,
54 ) -> InterpResult<'tcx, &'a OsStr>
59 let this = self.eval_context_ref();
60 let bytes = this.read_c_str(ptr)?;
61 bytes_to_os_str(bytes)
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>(
68 ptr: Pointer<Option<Provenance>>,
69 ) -> InterpResult<'tcx, OsString>
75 pub fn u16vec_to_osstring<'tcx>(u16_vec: Vec<u16>) -> InterpResult<'tcx, OsString> {
76 Ok(OsString::from_wide(&u16_vec[..]))
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))?;
85 let u16_vec = self.eval_context_ref().read_wide_str(ptr)?;
86 u16vec_to_osstring(u16_vec)
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(
97 ptr: Pointer<Option<Provenance>>,
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)
104 /// Helper function to write an OsStr as a 0x0000-terminated u16-sequence, which is what the
105 /// Windows APIs usually handle.
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.`
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(
118 ptr: Pointer<Option<Provenance>>,
121 ) -> InterpResult<'tcx, (bool, u64)> {
123 fn os_str_to_u16vec<'tcx>(os_str: &OsStr) -> InterpResult<'tcx, Vec<u16>> {
124 Ok(os_str.encode_wide().collect())
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
133 .map(|s| s.encode_utf16().collect())
134 .ok_or_else(|| err_unsup_format!("{:?} is not a valid utf-8 string", os_str).into())
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);
146 Ok((written, size_needed))
149 /// Allocate enough memory to store the given `OsStr` as a null-terminated sequence of bytes.
150 fn alloc_os_str_as_c_str(
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();
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();
165 /// Allocate enough memory to store the given `OsStr` as a null-terminated sequence of `u16`.
166 fn alloc_os_str_as_wide_str(
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();
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)?;
177 self.write_os_str_to_wide_str(os_str, arg_place.ptr, size, /*truncate*/ false).unwrap();
182 /// Read a null-terminated sequence of bytes, and perform path separator conversion if needed.
183 fn read_path_from_c_str<'a>(
185 ptr: Pointer<Option<Provenance>>,
186 ) -> InterpResult<'tcx, Cow<'a, Path>>
191 let this = self.eval_context_ref();
192 let os_str = this.read_os_str_from_c_str(ptr)?;
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)),
200 /// Read a null-terminated sequence of `u16`s, and perform path separator conversion if needed.
201 fn read_path_from_wide_str(
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)?;
208 Ok(this.convert_path(Cow::Owned(os_str), PathConversion::TargetToHost).into_owned().into())
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(
216 ptr: Pointer<Option<Provenance>>,
218 ) -> InterpResult<'tcx, (bool, u64)> {
219 let this = self.eval_context_mut();
221 this.convert_path(Cow::Borrowed(path.as_os_str()), PathConversion::HostToTarget);
222 this.write_os_str_to_c_str(&os_str, ptr, size)
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(
230 ptr: Pointer<Option<Provenance>>,
233 ) -> InterpResult<'tcx, (bool, u64)> {
234 let this = self.eval_context_mut();
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)
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(
245 memkind: MemoryKind<MiriMemoryKind>,
246 ) -> InterpResult<'tcx, Pointer<Option<Provenance>>> {
247 let this = self.eval_context_mut();
249 this.convert_path(Cow::Borrowed(path.as_os_str()), PathConversion::HostToTarget);
250 this.alloc_os_str_as_c_str(&os_str, memkind)
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(
258 memkind: MemoryKind<MiriMemoryKind>,
259 ) -> InterpResult<'tcx, Pointer<Option<Provenance>>> {
260 let this = self.eval_context_mut();
262 this.convert_path(Cow::Borrowed(path.as_os_str()), PathConversion::HostToTarget);
263 this.alloc_os_str_as_wide_str(&os_str, memkind)
266 #[allow(clippy::get_first)]
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;
276 return if target_os == "windows" {
277 // Windows-on-Windows, all fine.
280 // Unix target, Windows host.
281 let (from, to) = match direction {
282 PathConversion::HostToTarget => ('\\', '/'),
283 PathConversion::TargetToHost => ('/', '\\'),
285 let mut converted = os_str
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.
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
295 if converted.get(1).copied() == Some(b':' as u16)
296 && converted.get(2).copied() == Some(b'/' as u16)
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);
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)
314 Cow::Owned(OsString::from_wide(&converted))
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'/'),
323 let mut converted = os_str
326 .map(|&wchar| if wchar == from { to } else { wchar })
327 .collect::<Vec<_>>();
328 // We also have to ensure that absolute paths remain absolute.
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());
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'/')
345 // Remove first 3 characters
346 converted.splice(0..3, std::iter::empty());
350 Cow::Owned(OsString::from_vec(converted))
352 // Unix-on-Unix, all is fine.