diff --git a/src/eval.rs b/src/eval.rs index f4a8d17617..b87d735ce5 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -4,7 +4,7 @@ use rand::rngs::StdRng; use rand::SeedableRng; use rustc::hir::def_id::DefId; -use rustc::ty::layout::{Align, LayoutOf, Size}; +use rustc::ty::layout::{LayoutOf, Size}; use rustc::ty::{self, TyCtxt}; use syntax::source_map::DUMMY_SP; @@ -48,7 +48,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>( EnvVars::init(&mut ecx, config.excluded_env_vars); // Setup first stack-frame - let main_instance = ty::Instance::mono(ecx.tcx.tcx, main_id); + let main_instance = ty::Instance::mono(tcx, main_id); let main_mir = ecx.load_mir(main_instance.def, None)?; if !main_mir.return_ty().is_unit() || main_mir.arg_count != 0 { @@ -59,11 +59,10 @@ pub fn create_ecx<'mir, 'tcx: 'mir>( let main_ret_ty = tcx.fn_sig(main_id).output(); let main_ret_ty = main_ret_ty.no_bound_vars().unwrap(); let start_instance = ty::Instance::resolve( - ecx.tcx.tcx, + tcx, ty::ParamEnv::reveal_all(), start_id, - ecx.tcx - .mk_substs(::std::iter::once(ty::subst::GenericArg::from(main_ret_ty))), + tcx.mk_substs(::std::iter::once(ty::subst::GenericArg::from(main_ret_ty))), ) .unwrap(); let start_mir = ecx.load_mir(start_instance.def, None)?; @@ -106,7 +105,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>( { let argc_place = ecx.allocate(dest.layout, MiriMemoryKind::Env.into()); ecx.write_scalar(argc, argc_place.into())?; - ecx.machine.argc = Some(argc_place.ptr.to_ptr()?); + ecx.machine.argc = Some(argc_place.ptr); } // Third argument (`argv`): created from `config.args`. @@ -134,8 +133,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>( } // Make an array with all these pointers, in the Miri memory. let argvs_layout = ecx.layout_of( - ecx.tcx - .mk_array(ecx.tcx.mk_imm_ptr(ecx.tcx.types.u8), argvs.len() as u64), + tcx.mk_array(tcx.mk_imm_ptr(tcx.types.u8), argvs.len() as u64), )?; let argvs_place = ecx.allocate(argvs_layout, MiriMemoryKind::Env.into()); for (idx, arg) in argvs.into_iter().enumerate() { @@ -151,36 +149,26 @@ pub fn create_ecx<'mir, 'tcx: 'mir>( { let argv_place = ecx.allocate(dest.layout, MiriMemoryKind::Env.into()); ecx.write_scalar(argv, argv_place.into())?; - ecx.machine.argv = Some(argv_place.ptr.to_ptr()?); + ecx.machine.argv = Some(argv_place.ptr); } // Store command line as UTF-16 for Windows `GetCommandLineW`. { let cmd_utf16: Vec = cmd.encode_utf16().collect(); - let cmd_ptr = ecx.memory.allocate( - Size::from_bytes(cmd_utf16.len() as u64 * 2), - Align::from_bytes(2).unwrap(), - MiriMemoryKind::Env.into(), - ); - ecx.machine.cmd_line = Some(cmd_ptr); + let cmd_type = tcx.mk_array(tcx.types.u16, cmd_utf16.len() as u64); + let cmd_place = ecx.allocate(ecx.layout_of(cmd_type)?, MiriMemoryKind::Env.into()); + ecx.machine.cmd_line = Some(cmd_place.ptr); // Store the UTF-16 string. We just allocated so we know the bounds are fine. let char_size = Size::from_bytes(2); - let cmd_alloc = ecx.memory.get_mut(cmd_ptr.alloc_id)?; - let mut cur_ptr = cmd_ptr; - for &c in cmd_utf16.iter() { - cmd_alloc.write_scalar( - &*ecx.tcx, - cur_ptr, - Scalar::from_uint(c, char_size).into(), - char_size, - )?; - cur_ptr = cur_ptr.offset(char_size, &*ecx.tcx)?; + for (idx, &c) in cmd_utf16.iter().enumerate() { + let place = ecx.mplace_field(cmd_place, idx as u64)?; + ecx.write_scalar(Scalar::from_uint(c, char_size), place.into())?; } } args.next().expect_none("start lang item has more arguments than expected"); // Set the last_error to 0 - let errno_layout = ecx.layout_of(ecx.tcx.types.u32)?; + let errno_layout = ecx.layout_of(tcx.types.u32)?; let errno_place = ecx.allocate(errno_layout, MiriMemoryKind::Static.into()); ecx.write_scalar(Scalar::from_u32(0), errno_place.into())?; ecx.machine.last_error = Some(errno_place); diff --git a/src/machine.rs b/src/machine.rs index c07fca6819..c76cc2e568 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -86,9 +86,9 @@ pub struct Evaluator<'tcx> { /// Program arguments (`Option` because we can only initialize them after creating the ecx). /// These are *pointers* to argc/argv because macOS. /// We also need the full command line as one string because of Windows. - pub(crate) argc: Option>, - pub(crate) argv: Option>, - pub(crate) cmd_line: Option>, + pub(crate) argc: Option>, + pub(crate) argv: Option>, + pub(crate) cmd_line: Option>, /// Last OS error location in memory. It is a 32-bit integer. pub(crate) last_error: Option>, diff --git a/src/shims/foreign_items.rs b/src/shims/foreign_items.rs index dea01c63a0..74ce477b8e 100644 --- a/src/shims/foreign_items.rs +++ b/src/shims/foreign_items.rs @@ -765,10 +765,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx // FIXME: register the destructor. } "_NSGetArgc" => { - this.write_scalar(Scalar::Ptr(this.machine.argc.unwrap()), dest)?; + this.write_scalar(this.machine.argc.expect("machine must be initialized"), dest)?; } "_NSGetArgv" => { - this.write_scalar(Scalar::Ptr(this.machine.argv.unwrap()), dest)?; + this.write_scalar(this.machine.argv.expect("machine must be initialized"), dest)?; } "SecRandomCopyBytes" => { let len = this.read_scalar(args[1])?.to_usize(this)?; @@ -927,7 +927,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx this.write_null(dest)?; } "GetCommandLineW" => { - this.write_scalar(Scalar::Ptr(this.machine.cmd_line.unwrap()), dest)?; + this.write_scalar(this.machine.cmd_line.expect("machine must be initialized"), dest)?; } // The actual name of 'RtlGenRandom' "SystemFunction036" => {