2 use std::convert::TryFrom;
3 use std::ffi::{OsStr, OsString};
6 use std::os::unix::ffi::{OsStrExt, OsStringExt};
8 use std::os::windows::ffi::{OsStrExt, OsStringExt};
9 use std::path::{Path, PathBuf};
11 use rustc::ty::layout::LayoutOf;
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>
25 fn bytes_to_os_str<'tcx, 'a>(bytes: &'a [u8]) -> InterpResult<'tcx, &'a OsStr> {
26 Ok(OsStr::from_bytes(bytes))
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))?;
35 let this = self.eval_context_ref();
36 let bytes = this.memory.read_c_str(scalar)?;
37 bytes_to_os_str(bytes)
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>
48 pub fn u16vec_to_osstring<'tcx, 'a>(u16_vec: Vec<u16>) -> InterpResult<'tcx, OsString> {
49 Ok(OsString::from_wide(&u16_vec[..]))
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))?;
58 let u16_vec = self.eval_context_ref().memory.read_wide_str(scalar)?;
59 u16vec_to_osstring(u16_vec)
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(
72 ) -> InterpResult<'tcx, (bool, u64)> {
74 fn os_str_to_bytes<'tcx, 'a>(os_str: &'a OsStr) -> InterpResult<'tcx, &'a [u8]> {
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
84 .map(|s| s.as_bytes())
85 .ok_or_else(|| err_unsup_format!("{:?} is not a valid utf-8 string", os_str).into())
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));
95 self.eval_context_mut()
97 .write_bytes(scalar, bytes.iter().copied().chain(iter::once(0u8)))?;
98 Ok((true, string_length))
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(
111 ) -> InterpResult<'tcx, (bool, u64)> {
113 fn os_str_to_u16vec<'tcx>(os_str: &OsStr) -> InterpResult<'tcx, Vec<u16>> {
114 Ok(os_str.encode_wide().collect())
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
123 .map(|s| s.encode_utf16().collect())
124 .ok_or_else(|| err_unsup_format!("{:?} is not a valid utf-8 string", os_str).into())
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));
135 // Store the UTF-16 string.
136 self.eval_context_mut()
138 .write_u16s(scalar, u16_vec.into_iter().chain(iter::once(0x0000)))?;
139 Ok((true, string_length))
142 /// Allocate enough memory to store the given `OsStr` as a null-terminated sequence of bytes.
143 fn alloc_os_str_as_c_str(
146 memkind: MemoryKind<MiriMemoryKind>,
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();
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()
157 /// Allocate enough memory to store the given `OsStr` as a null-terminated sequence of `u16`.
158 fn alloc_os_str_as_wide_str(
161 memkind: MemoryKind<MiriMemoryKind>,
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();
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()
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>>
178 let this = self.eval_context_ref();
179 let os_str = this.read_os_str_from_c_str(scalar)?;
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))
186 // Unix target, Windows host. Need to convert target '/' to host '\'.
187 let converted = os_str
189 .map(|wchar| if wchar == '/' as u16 { '\\' as u16 } else { wchar })
190 .collect::<Vec<_>>();
191 Cow::Owned(PathBuf::from(OsString::from_wide(&converted)))
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
199 .map(|&wchar| if wchar == '/' as u8 { '\\' as u8 } else { wchar })
200 .collect::<Vec<_>>();
201 Cow::Owned(PathBuf::from(OsString::from_vec(converted)))
203 // Unix-on-Unix, all is fine.
204 Cow::Borrowed(Path::new(os_str))
208 /// Write a Path to the machine memory, adjusting path separators if needed.
209 fn write_path_to_c_str(
214 ) -> InterpResult<'tcx, (bool, u64)> {
215 let this = self.eval_context_mut();
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())
222 // Unix target, Windows host. Need to convert host '\\' to target '/'.
226 .map(|wchar| if wchar == '\\' as u16 { '/' as u16 } else { wchar })
227 .collect::<Vec<_>>();
228 Cow::Owned(OsString::from_wide(&converted))
231 let os_str = if this.tcx.sess.target.target.target_os == "windows" {
232 // Windows target, Unix host. Need to convert host '/' to target '\'.
237 .map(|&wchar| if wchar == '/' as u8 { '\\' as u8 } else { wchar })
238 .collect::<Vec<_>>();
239 Cow::Owned(OsString::from_vec(converted))
241 // Unix-on-Unix, all is fine.
242 Cow::Borrowed(path.as_os_str())
245 this.write_os_str_to_c_str(&os_str, scalar, size)