Skip to content

Commit 1e0b398

Browse files
committed
Windows cmdline: avoid accessing allocations directly
1 parent d4e4fe7 commit 1e0b398

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

src/eval.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rand::rngs::StdRng;
44
use rand::SeedableRng;
55

66
use rustc::hir::def_id::DefId;
7-
use rustc::ty::layout::{Align, LayoutOf, Size};
7+
use rustc::ty::layout::{LayoutOf, Size};
88
use rustc::ty::{self, TyCtxt};
99
use syntax::source_map::DUMMY_SP;
1010

@@ -48,7 +48,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
4848
EnvVars::init(&mut ecx, config.excluded_env_vars);
4949

5050
// Setup first stack-frame
51-
let main_instance = ty::Instance::mono(ecx.tcx.tcx, main_id);
51+
let main_instance = ty::Instance::mono(tcx, main_id);
5252
let main_mir = ecx.load_mir(main_instance.def, None)?;
5353

5454
if !main_mir.return_ty().is_unit() || main_mir.arg_count != 0 {
@@ -59,11 +59,10 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
5959
let main_ret_ty = tcx.fn_sig(main_id).output();
6060
let main_ret_ty = main_ret_ty.no_bound_vars().unwrap();
6161
let start_instance = ty::Instance::resolve(
62-
ecx.tcx.tcx,
62+
tcx,
6363
ty::ParamEnv::reveal_all(),
6464
start_id,
65-
ecx.tcx
66-
.mk_substs(::std::iter::once(ty::subst::GenericArg::from(main_ret_ty))),
65+
tcx.mk_substs(::std::iter::once(ty::subst::GenericArg::from(main_ret_ty))),
6766
)
6867
.unwrap();
6968
let start_mir = ecx.load_mir(start_instance.def, None)?;
@@ -134,8 +133,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
134133
}
135134
// Make an array with all these pointers, in the Miri memory.
136135
let argvs_layout = ecx.layout_of(
137-
ecx.tcx
138-
.mk_array(ecx.tcx.mk_imm_ptr(ecx.tcx.types.u8), argvs.len() as u64),
136+
tcx.mk_array(tcx.mk_imm_ptr(tcx.types.u8), argvs.len() as u64),
139137
)?;
140138
let argvs_place = ecx.allocate(argvs_layout, MiriMemoryKind::Env.into());
141139
for (idx, arg) in argvs.into_iter().enumerate() {
@@ -156,31 +154,21 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
156154
// Store command line as UTF-16 for Windows `GetCommandLineW`.
157155
{
158156
let cmd_utf16: Vec<u16> = cmd.encode_utf16().collect();
159-
let cmd_ptr = ecx.memory.allocate(
160-
Size::from_bytes(cmd_utf16.len() as u64 * 2),
161-
Align::from_bytes(2).unwrap(),
162-
MiriMemoryKind::Env.into(),
163-
);
164-
ecx.machine.cmd_line = Some(cmd_ptr);
157+
let cmd_type = tcx.mk_array(tcx.types.u16, cmd_utf16.len() as u64);
158+
let cmd_place = ecx.allocate(ecx.layout_of(cmd_type)?, MiriMemoryKind::Env.into());
159+
ecx.machine.cmd_line = Some(cmd_place.ptr.to_ptr()?);
165160
// Store the UTF-16 string. We just allocated so we know the bounds are fine.
166161
let char_size = Size::from_bytes(2);
167-
let cmd_alloc = ecx.memory.get_mut(cmd_ptr.alloc_id)?;
168-
let mut cur_ptr = cmd_ptr;
169-
for &c in cmd_utf16.iter() {
170-
cmd_alloc.write_scalar(
171-
&*ecx.tcx,
172-
cur_ptr,
173-
Scalar::from_uint(c, char_size).into(),
174-
char_size,
175-
)?;
176-
cur_ptr = cur_ptr.offset(char_size, &*ecx.tcx)?;
162+
for (idx, &c) in cmd_utf16.iter().enumerate() {
163+
let place = ecx.mplace_field(cmd_place, idx as u64)?;
164+
ecx.write_scalar(Scalar::from_uint(c, char_size), place.into())?;
177165
}
178166
}
179167

180168
args.next().expect_none("start lang item has more arguments than expected");
181169

182170
// Set the last_error to 0
183-
let errno_layout = ecx.layout_of(ecx.tcx.types.u32)?;
171+
let errno_layout = ecx.layout_of(tcx.types.u32)?;
184172
let errno_place = ecx.allocate(errno_layout, MiriMemoryKind::Static.into());
185173
ecx.write_scalar(Scalar::from_u32(0), errno_place.into())?;
186174
ecx.machine.last_error = Some(errno_place);

0 commit comments

Comments
 (0)