Skip to content

Commit 22d0546

Browse files
committed
Auto merge of #1028 - RalfJung:place-apis, r=RalfJung
use Place API instead of Allocation API
2 parents d4e4fe7 + ddb1fc9 commit 22d0546

File tree

3 files changed

+20
-32
lines changed

3 files changed

+20
-32
lines changed

src/eval.rs

Lines changed: 14 additions & 26 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)?;
@@ -106,7 +105,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
106105
{
107106
let argc_place = ecx.allocate(dest.layout, MiriMemoryKind::Env.into());
108107
ecx.write_scalar(argc, argc_place.into())?;
109-
ecx.machine.argc = Some(argc_place.ptr.to_ptr()?);
108+
ecx.machine.argc = Some(argc_place.ptr);
110109
}
111110

112111
// Third argument (`argv`): created from `config.args`.
@@ -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() {
@@ -151,36 +149,26 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
151149
{
152150
let argv_place = ecx.allocate(dest.layout, MiriMemoryKind::Env.into());
153151
ecx.write_scalar(argv, argv_place.into())?;
154-
ecx.machine.argv = Some(argv_place.ptr.to_ptr()?);
152+
ecx.machine.argv = Some(argv_place.ptr);
155153
}
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);
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);

src/machine.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ pub struct Evaluator<'tcx> {
8686
/// Program arguments (`Option` because we can only initialize them after creating the ecx).
8787
/// These are *pointers* to argc/argv because macOS.
8888
/// We also need the full command line as one string because of Windows.
89-
pub(crate) argc: Option<Pointer<Tag>>,
90-
pub(crate) argv: Option<Pointer<Tag>>,
91-
pub(crate) cmd_line: Option<Pointer<Tag>>,
89+
pub(crate) argc: Option<Scalar<Tag>>,
90+
pub(crate) argv: Option<Scalar<Tag>>,
91+
pub(crate) cmd_line: Option<Scalar<Tag>>,
9292

9393
/// Last OS error location in memory. It is a 32-bit integer.
9494
pub(crate) last_error: Option<MPlaceTy<'tcx, Tag>>,

src/shims/foreign_items.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -765,10 +765,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
765765
// FIXME: register the destructor.
766766
}
767767
"_NSGetArgc" => {
768-
this.write_scalar(Scalar::Ptr(this.machine.argc.unwrap()), dest)?;
768+
this.write_scalar(this.machine.argc.expect("machine must be initialized"), dest)?;
769769
}
770770
"_NSGetArgv" => {
771-
this.write_scalar(Scalar::Ptr(this.machine.argv.unwrap()), dest)?;
771+
this.write_scalar(this.machine.argv.expect("machine must be initialized"), dest)?;
772772
}
773773
"SecRandomCopyBytes" => {
774774
let len = this.read_scalar(args[1])?.to_usize(this)?;
@@ -927,7 +927,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
927927
this.write_null(dest)?;
928928
}
929929
"GetCommandLineW" => {
930-
this.write_scalar(Scalar::Ptr(this.machine.cmd_line.unwrap()), dest)?;
930+
this.write_scalar(this.machine.cmd_line.expect("machine must be initialized"), dest)?;
931931
}
932932
// The actual name of 'RtlGenRandom'
933933
"SystemFunction036" => {

0 commit comments

Comments
 (0)