Skip to content

Commit

Permalink
Auto merge of #1005 - RalfJung:cleanup, r=RalfJung
Browse files Browse the repository at this point in the history
cleanup now that borrow checker knows memory is a field

@christianpoveda you said, I think, that `fs.rs` could also be cleaned up to longer remove-and-then-add file descriptors from the table? Could you make a PR for that?
  • Loading branch information
bors committed Oct 18, 2019
2 parents fbc1d91 + 5481afb commit dfcdc85
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 33 deletions.
5 changes: 2 additions & 3 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
}
// Store command line as UTF-16 for Windows `GetCommandLineW`.
{
let tcx = &{ ecx.tcx.tcx };
let cmd_utf16: Vec<u16> = cmd.encode_utf16().collect();
let cmd_ptr = ecx.memory.allocate(
Size::from_bytes(cmd_utf16.len() as u64 * 2),
Expand All @@ -169,12 +168,12 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
let mut cur_ptr = cmd_ptr;
for &c in cmd_utf16.iter() {
cmd_alloc.write_scalar(
tcx,
&*ecx.tcx,
cur_ptr,
Scalar::from_uint(c, char_size).into(),
char_size,
)?;
cur_ptr = cur_ptr.offset(char_size, tcx)?;
cur_ptr = cur_ptr.offset(char_size, &*ecx.tcx)?;
}
}

Expand Down
10 changes: 3 additions & 7 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
rng.fill_bytes(&mut data);
}

let tcx = &{this.tcx.tcx};
this.memory.get_mut(ptr.alloc_id)?.write_bytes(tcx, ptr, &data)
this.memory.get_mut(ptr.alloc_id)?.write_bytes(&*this.tcx, ptr, &data)
}

/// Visits the memory covered by `place`, sensitive to freezing: the 3rd parameter
Expand Down Expand Up @@ -311,8 +310,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
/// Helper function to get the `TyLayout` of a `libc` type
fn libc_ty_layout(&mut self, name: &str) -> InterpResult<'tcx, TyLayout<'tcx>> {
let this = self.eval_context_mut();
let tcx = &{ this.tcx.tcx };
let ty = this.resolve_path(&["libc", name])?.ty(*tcx);
let ty = this.resolve_path(&["libc", name])?.ty(*this.tcx);
this.layout_of(ty)
}

Expand All @@ -325,14 +323,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
) -> InterpResult<'tcx> {
let this = self.eval_context_mut();

let tcx = &{ this.tcx.tcx };

let mut offset = Size::from_bytes(0);

for &imm in imms {
this.write_immediate_to_mplace(
*imm,
place.offset(offset, None, imm.layout, tcx)?,
place.offset(offset, None, imm.layout, &*this.tcx)?,
)?;
offset += imm.layout.size;
}
Expand Down
8 changes: 3 additions & 5 deletions src/shims/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

this.check_no_isolation("getcwd")?;

let tcx = &{ this.tcx.tcx };

let buf = this.force_ptr(this.read_scalar(buf_op)?.not_undef()?)?;
let size = this.read_scalar(size_op)?.to_usize(&*tcx)?;
let size = this.read_scalar(size_op)?.to_usize(&*this.tcx)?;
// If we cannot get the current directory, we return null
match env::current_dir() {
Ok(cwd) => {
Expand All @@ -142,15 +140,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
// `bytes.len()`, meaning that `bytes` actually fit inside tbe buffer.
this.memory
.get_mut(buf.alloc_id)?
.write_bytes(tcx, buf, &bytes)?;
.write_bytes(&*this.tcx, buf, &bytes)?;
return Ok(Scalar::Ptr(buf));
}
let erange = this.eval_libc("ERANGE")?;
this.set_last_error(erange)?;
}
Err(e) => this.consume_io_error(e)?,
}
Ok(Scalar::ptr_null(&*tcx))
Ok(Scalar::ptr_null(&*this.tcx))
}

fn chdir(&mut self, path_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
Expand Down
18 changes: 7 additions & 11 deletions src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

fn malloc(&mut self, size: u64, zero_init: bool, kind: MiriMemoryKind) -> Scalar<Tag> {
let this = self.eval_context_mut();
let tcx = &{ this.tcx.tcx };
if size == 0 {
Scalar::from_int(0, this.pointer_size())
} else {
Expand All @@ -55,7 +54,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.memory
.get_mut(ptr.alloc_id)
.unwrap()
.write_repeat(tcx, ptr, 0, Size::from_bytes(size))
.write_repeat(&*this.tcx, ptr, 0, Size::from_bytes(size))
.unwrap();
}
Scalar::Ptr(ptr)
Expand Down Expand Up @@ -90,12 +89,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
} else {
let old_ptr = this.force_ptr(old_ptr)?;
let memory = &mut this.memory;
if new_size == 0 {
memory.deallocate(old_ptr, None, kind.into())?;
this.memory.deallocate(old_ptr, None, kind.into())?;
Ok(Scalar::from_int(0, this.pointer_size()))
} else {
let new_ptr = memory.reallocate(
let new_ptr = this.memory.reallocate(
old_ptr,
None,
Size::from_bytes(new_size),
Expand Down Expand Up @@ -334,7 +332,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
// for the TLS destructors, and of course `eval_main`.
let mir = this.load_mir(f_instance.def, None)?;
let ret_place =
MPlaceTy::dangling(this.layout_of(this.tcx.mk_unit())?, this).into();
MPlaceTy::dangling(this.layout_of(tcx.mk_unit())?, this).into();
this.push_stack_frame(
f_instance,
mir.span,
Expand Down Expand Up @@ -471,7 +469,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
"write" => {
let fd = this.read_scalar(args[0])?.to_i32()?;
let buf = this.read_scalar(args[1])?.not_undef()?;
let n = this.read_scalar(args[2])?.to_usize(&*this.tcx)?;
let n = this.read_scalar(args[2])?.to_usize(tcx)?;
trace!("Called write({:?}, {:?}, {:?})", fd, buf, n);
let result = if fd == 1 || fd == 2 {
// stdout/stderr
Expand Down Expand Up @@ -993,10 +991,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

fn set_last_error(&mut self, scalar: Scalar<Tag>) -> InterpResult<'tcx> {
let this = self.eval_context_mut();
let tcx = &{ this.tcx.tcx };
let errno_ptr = this.machine.last_error.unwrap();
this.memory.get_mut(errno_ptr.alloc_id)?.write_scalar(
tcx,
&*this.tcx,
errno_ptr,
scalar.into(),
Size::from_bits(32),
Expand All @@ -1005,11 +1002,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

fn get_last_error(&mut self) -> InterpResult<'tcx, Scalar<Tag>> {
let this = self.eval_context_mut();
let tcx = &{ this.tcx.tcx };
let errno_ptr = this.machine.last_error.unwrap();
this.memory
.get(errno_ptr.alloc_id)?
.read_scalar(tcx, errno_ptr, Size::from_bits(32))?
.read_scalar(&*this.tcx, errno_ptr, Size::from_bits(32))?
.not_undef()
}

Expand Down
8 changes: 2 additions & 6 deletions src/shims/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

this.check_no_isolation("read")?;

let tcx = &{ this.tcx.tcx };

let count = this.read_scalar(count_op)?.to_usize(&*this.tcx)?;
// Reading zero bytes should not change `buf`
if count == 0 {
Expand All @@ -173,7 +171,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let bytes = this.force_ptr(buf_scalar).and_then(|buf| {
this.memory
.get_mut(buf.alloc_id)?
.get_bytes_mut(tcx, buf, Size::from_bytes(count))
.get_bytes_mut(&*this.tcx, buf, Size::from_bytes(count))
.map(|buffer| handle.file.read(buffer))
});
// Reinsert the file handle
Expand All @@ -192,8 +190,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

this.check_no_isolation("write")?;

let tcx = &{ this.tcx.tcx };

let count = this.read_scalar(count_op)?.to_usize(&*this.tcx)?;
// Writing zero bytes should not change `buf`
if count == 0 {
Expand All @@ -205,7 +201,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.remove_handle_and(fd, |mut handle, this| {
let bytes = this.memory.get(buf.alloc_id).and_then(|alloc| {
alloc
.get_bytes(tcx, buf, Size::from_bytes(count))
.get_bytes(&*this.tcx, buf, Size::from_bytes(count))
.map(|bytes| handle.file.write(bytes).map(|bytes| bytes as i64))
});
this.machine.file_handler.handles.insert(fd, handle);
Expand Down
2 changes: 1 addition & 1 deletion src/shims/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
// (as opposed to through a place), we have to remember to erase any tag
// that might still hang around!

let intrinsic_name = &*this.tcx.item_name(instance.def_id()).as_str();
let intrinsic_name = &*tcx.item_name(instance.def_id()).as_str();
match intrinsic_name {
"arith_offset" => {
let offset = this.read_scalar(args[1])?.to_isize(this)?;
Expand Down

0 comments on commit dfcdc85

Please sign in to comment.