Skip to content

Commit 53fcf47

Browse files
committed
Auto merge of #1099 - RalfJung:os_str, r=RalfJung
tweak and use OsStr interfaces
2 parents 6e49f4a + 01f060b commit 53fcf47

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

src/eval.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Main evaluator loop and setting up the initial stack frame.
22
3+
use std::ffi::OsStr;
4+
35
use rand::rngs::StdRng;
46
use rand::SeedableRng;
57

@@ -75,26 +77,15 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
7577
let argc = Scalar::from_uint(config.args.len() as u128, ecx.pointer_size());
7678
// Third argument (`argv`): created from `config.args`.
7779
let argv = {
78-
// For Windows, construct a command string with all the aguments (before we take apart `config.args`).
79-
let mut cmd = String::new();
80+
// Put each argument in memory, collect pointers.
81+
let mut argvs = Vec::<Scalar<Tag>>::new();
8082
for arg in config.args.iter() {
81-
if !cmd.is_empty() {
82-
cmd.push(' ');
83-
}
84-
cmd.push_str(&*shell_escape::windows::escape(arg.as_str().into()));
85-
}
86-
// Don't forget `0` terminator.
87-
cmd.push(std::char::from_u32(0).unwrap());
88-
// Collect the pointers to the individual strings.
89-
let mut argvs = Vec::<Pointer<Tag>>::new();
90-
for arg in config.args {
91-
// Add `0` terminator.
92-
let mut arg = arg.into_bytes();
93-
arg.push(0);
94-
argvs.push(
95-
ecx.memory
96-
.allocate_static_bytes(arg.as_slice(), MiriMemoryKind::Env.into()),
97-
);
83+
// Make space for `0` terminator.
84+
let size = arg.len() as u64 + 1;
85+
let arg_type = tcx.mk_array(tcx.types.u8, size);
86+
let arg_place = ecx.allocate(ecx.layout_of(arg_type)?, MiriMemoryKind::Env.into());
87+
ecx.write_os_str_to_c_string(OsStr::new(arg), arg_place.ptr, size)?;
88+
argvs.push(arg_place.ptr);
9889
}
9990
// Make an array with all these pointers, in the Miri memory.
10091
let argvs_layout = ecx.layout_of(
@@ -107,7 +98,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
10798
}
10899
ecx.memory
109100
.mark_immutable(argvs_place.ptr.assert_ptr().alloc_id)?;
110-
// A pointer to that place is the argument.
101+
// A pointer to that place is the 3rd argument for main.
111102
let argv = argvs_place.ptr;
112103
// Store `argc` and `argv` for macOS `_NSGetArg{c,v}`.
113104
{
@@ -127,6 +118,17 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
127118
}
128119
// Store command line as UTF-16 for Windows `GetCommandLineW`.
129120
{
121+
// Construct a command string with all the aguments.
122+
let mut cmd = String::new();
123+
for arg in config.args.iter() {
124+
if !cmd.is_empty() {
125+
cmd.push(' ');
126+
}
127+
cmd.push_str(&*shell_escape::windows::escape(arg.as_str().into()));
128+
}
129+
// Don't forget `0` terminator.
130+
cmd.push(std::char::from_u32(0).unwrap());
131+
130132
let cmd_utf16: Vec<u16> = cmd.encode_utf16().collect();
131133
let cmd_type = tcx.mk_array(tcx.types.u16, cmd_utf16.len() as u64);
132134
let cmd_place = ecx.allocate(ecx.layout_of(cmd_type)?, MiriMemoryKind::Env.into());

src/helpers.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::{mem, iter};
2-
use std::ffi::{OsStr, OsString};
2+
use std::ffi::OsStr;
33

44
use syntax::source_map::DUMMY_SP;
55
use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
@@ -453,9 +453,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
453453

454454
/// Helper function to read an OsString from a null-terminated sequence of bytes, which is what
455455
/// the Unix APIs usually handle.
456-
fn read_os_string_from_c_string(&mut self, scalar: Scalar<Tag>) -> InterpResult<'tcx, OsString> {
457-
let bytes = self.eval_context_mut().memory.read_c_str(scalar)?;
458-
Ok(bytes_to_os_str(bytes)?.into())
456+
fn read_os_string_from_c_string<'a>(&'a self, scalar: Scalar<Tag>) -> InterpResult<'tcx, &'a OsStr>
457+
where 'tcx: 'a, 'mir: 'a
458+
{
459+
let this = self.eval_context_ref();
460+
let bytes = this.memory.read_c_str(scalar)?;
461+
bytes_to_os_str(bytes)
459462
}
460463

461464
/// Helper function to write an OsStr as a null-terminated sequence of bytes, which is what
@@ -501,7 +504,7 @@ fn os_str_to_bytes<'tcx, 'a>(os_str: &'a OsStr) -> InterpResult<'tcx, &'a [u8]>
501504
}
502505

503506
#[cfg(not(target_os = "unix"))]
504-
fn bytes_to_os_str<'tcx, 'a>(bytes: &'a[u8]) -> InterpResult<'tcx, &'a OsStr> {
507+
fn bytes_to_os_str<'tcx, 'a>(bytes: &'a [u8]) -> InterpResult<'tcx, &'a OsStr> {
505508
let s = std::str::from_utf8(bytes)
506509
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", bytes))?;
507510
Ok(&OsStr::new(s))

src/machine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ pub type MiriEvalContext<'mir, 'tcx> = InterpCx<'mir, 'tcx, Evaluator<'tcx>>;
140140

141141
/// A little trait that's useful to be inherited by extension traits.
142142
pub trait MiriEvalContextExt<'mir, 'tcx> {
143-
fn eval_context_ref(&self) -> &MiriEvalContext<'mir, 'tcx>;
144-
fn eval_context_mut(&mut self) -> &mut MiriEvalContext<'mir, 'tcx>;
143+
fn eval_context_ref<'a>(&'a self) -> &'a MiriEvalContext<'mir, 'tcx>;
144+
fn eval_context_mut<'a>(&'a mut self) -> &'a mut MiriEvalContext<'mir, 'tcx>;
145145
}
146146
impl<'mir, 'tcx> MiriEvalContextExt<'mir, 'tcx> for MiriEvalContext<'mir, 'tcx> {
147147
#[inline(always)]

0 commit comments

Comments
 (0)