Skip to content

Commit 61be3ba

Browse files
committed
support current_exe on macOS, and fix write_os_str length logic
1 parent f633537 commit 61be3ba

File tree

5 files changed

+45
-7
lines changed

5 files changed

+45
-7
lines changed

src/shims/env.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ fn windows_check_buffer_size((success, len): (bool, u64)) -> u32 {
1818
if success {
1919
// If the function succeeds, the return value is the number of characters stored in the target buffer,
2020
// not including the terminating null character.
21-
u32::try_from(len).unwrap()
21+
u32::try_from(len.checked_sub(1).unwrap()).unwrap()
2222
} else {
2323
// If the target buffer was not large enough to hold the data, the return value is the buffer size, in characters,
2424
// required to hold the string and its terminating null character.
25-
u32::try_from(len.checked_add(1).unwrap()).unwrap()
25+
u32::try_from(len).unwrap()
2626
}
2727
}
2828

src/shims/os_str.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
9292
/// the Unix APIs usually handle. This function returns `Ok((false, length))` without trying
9393
/// to write if `size` is not large enough to fit the contents of `os_string` plus a null
9494
/// terminator. It returns `Ok((true, length))` if the writing process was successful. The
95-
/// string length returned does not include the null terminator.
95+
/// string length returned does include the null terminator.
9696
fn write_os_str_to_c_str(
9797
&mut self,
9898
os_str: &OsStr,
@@ -103,7 +103,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
103103
// If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required null
104104
// terminator to memory using the `ptr` pointer would cause an out-of-bounds access.
105105
let string_length = u64::try_from(bytes.len()).unwrap();
106-
if size <= string_length {
106+
let string_length = string_length.checked_add(1).unwrap();
107+
if size < string_length {
107108
return Ok((false, string_length));
108109
}
109110
self.eval_context_mut()
@@ -115,7 +116,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
115116
/// the Windows APIs usually handle. This function returns `Ok((false, length))` without trying
116117
/// to write if `size` is not large enough to fit the contents of `os_string` plus a null
117118
/// terminator. It returns `Ok((true, length))` if the writing process was successful. The
118-
/// string length returned does not include the null terminator.
119+
/// string length returned does include the null terminator. Length is measured in units of
120+
/// `u16.`
119121
fn write_os_str_to_wide_str(
120122
&mut self,
121123
os_str: &OsStr,
@@ -157,7 +159,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
157159
alloc
158160
.write_scalar(alloc_range(size2 * offset, size2), Scalar::from_u16(wchar).into())?;
159161
}
160-
Ok((true, string_length - 1))
162+
Ok((true, string_length))
161163
}
162164

163165
/// Allocate enough memory to store the given `OsStr` as a null-terminated sequence of bytes.

src/shims/unix/fs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1380,11 +1380,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
13801380
let name_place = this.mplace_field(&entry_place, 5)?;
13811381

13821382
let file_name = dir_entry.file_name(); // not a Path as there are no separators!
1383-
let (name_fits, file_name_len) = this.write_os_str_to_c_str(
1383+
let (name_fits, file_name_buf_len) = this.write_os_str_to_c_str(
13841384
&file_name,
13851385
name_place.ptr,
13861386
name_place.layout.size.bytes(),
13871387
)?;
1388+
let file_name_len = file_name_buf_len.checked_sub(1).unwrap();
13881389
if !name_fits {
13891390
throw_unsup_format!(
13901391
"a directory entry had a name too large to fit in libc::dirent"

src/shims/unix/macos/foreign_items.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,33 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
117117
dest,
118118
)?;
119119
}
120+
"_NSGetExecutablePath" => {
121+
let [buf, bufsize] =
122+
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
123+
this.check_no_isolation("`_NSGetExecutablePath`")?;
124+
125+
let buf_ptr = this.read_pointer(buf)?;
126+
let bufsize = this.deref_operand(bufsize)?;
127+
128+
// Using the host current_exe is a bit off, but consistent with Linux
129+
// (where stdlib reads /proc/self/exe).
130+
let path = std::env::current_exe().unwrap();
131+
let (written, size_needed) = this.write_path_to_c_str(
132+
&path,
133+
buf_ptr,
134+
this.read_scalar(&bufsize.into())?.to_u32()?.into(),
135+
)?;
136+
137+
if written {
138+
this.write_null(dest)?;
139+
} else {
140+
this.write_scalar(
141+
Scalar::from_u32(size_needed.try_into().unwrap()),
142+
&bufsize.into(),
143+
)?;
144+
this.write_int(-1, dest)?;
145+
}
146+
}
120147

121148
// Thread-local storage
122149
"_tlv_atexit" => {

tests/pass/current_exe.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@ignore-target-windows
2+
//@compile-flags: -Zmiri-disable-isolation
3+
use std::env;
4+
5+
fn main() {
6+
// The actual value we get is a bit odd: we get the Miri binary that interprets us.
7+
env::current_exe().unwrap();
8+
}

0 commit comments

Comments
 (0)