Skip to content

Commit 4b42579

Browse files
committed
Auto merge of #1019 - RalfJung:rustup, r=RalfJung
rustup: more flexible write_bytes avoids allocations and removes itertools dependency
2 parents a05f2aa + c87f106 commit 4b42579

File tree

8 files changed

+18
-33
lines changed

8 files changed

+18
-33
lines changed

Cargo.lock

Lines changed: 0 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ log = "0.4"
4040
shell-escape = "0.1.4"
4141
hex = "0.3.2"
4242
rand = "0.7"
43-
itertools = "0.8"
4443

4544
# A noop dependency that changes in the Rust repository, it's a bit of a hack.
4645
# See the `src/tools/rustc-workspace-hack/README.md` file in `rust-lang/rust`

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
f466f52c1bf8f2e4454e31c683a88625ad4b4033
1+
55e00631e5bc5b16d40232914e57deeea197a8e4

src/helpers.rs

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

44
use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
@@ -379,7 +379,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
379379
_ => throw_unsup_format!("The {} error cannot be transformed into a raw os error", e)
380380
})?
381381
} else {
382-
// FIXME: we have to implement the windows' equivalent of this.
382+
// FIXME: we have to implement the Windows equivalent of this.
383383
throw_unsup_format!("Setting the last OS error from an io::Error is unsupported for {}.", target.target_os)
384384
};
385385
this.set_last_error(last_error)
@@ -390,7 +390,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
390390
/// `Ok(-1)` and sets the last OS error accordingly.
391391
///
392392
/// This function uses `T: From<i32>` instead of `i32` directly because some IO related
393-
/// functions return different integer types (like `read`, that returns an `i64`)
393+
/// functions return different integer types (like `read`, that returns an `i64`).
394394
fn try_unwrap_io_result<T: From<i32>>(
395395
&mut self,
396396
result: std::io::Result<T>,
@@ -423,12 +423,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
423423
) -> InterpResult<'tcx, bool> {
424424
let bytes = os_str_to_bytes(os_str)?;
425425
// If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required null
426-
// terminator to memory using the `ptr` pointer would cause an overflow.
426+
// terminator to memory using the `ptr` pointer would cause an out-of-bounds access.
427427
if size <= bytes.len() as u64 {
428428
return Ok(false);
429429
}
430-
// FIXME: We should use `Iterator::chain` instead when rust-lang/rust#65704 lands.
431-
self.eval_context_mut().memory.write_bytes(scalar, [bytes, &[0]].concat())?;
430+
self.eval_context_mut().memory.write_bytes(scalar, bytes.iter().copied().chain(iter::once(0u8)))?;
432431
Ok(true)
433432
}
434433
}

src/operator.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::convert::TryFrom;
2+
13
use rustc::ty::{Ty, layout::LayoutOf};
24
use rustc::mir;
35

@@ -117,8 +119,7 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
117119
pointee_ty: Ty<'tcx>,
118120
offset: i64,
119121
) -> InterpResult<'tcx, Scalar<Tag>> {
120-
// FIXME: assuming here that type size is less than `i64::max_value()`.
121-
let pointee_size = self.layout_of(pointee_ty)?.size.bytes() as i64;
122+
let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap();
122123
let offset = offset
123124
.checked_mul(pointee_size)
124125
.ok_or_else(|| err_panic!(Overflow(mir::BinOp::Mul)))?;

src/shims/foreign_items.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::convert::TryInto;
1+
use std::{iter, convert::TryInto};
22

33
use rustc::hir::def_id::DefId;
44
use rustc::mir;
@@ -52,7 +52,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5252
if zero_init {
5353
// We just allocated this, the access is definitely in-bounds.
5454
this.memory
55-
.write_bytes(ptr.into(), itertools::repeat_n(0u8, size as usize))
55+
.write_bytes(ptr.into(), iter::repeat(0u8).take(size as usize))
5656
.unwrap();
5757
}
5858
Scalar::Ptr(ptr)
@@ -227,7 +227,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
227227
);
228228
// We just allocated this, the access is definitely in-bounds.
229229
this.memory
230-
.write_bytes(ptr.into(), itertools::repeat_n(0u8, size as usize))
230+
.write_bytes(ptr.into(), iter::repeat(0u8).take(size as usize))
231231
.unwrap();
232232
this.write_scalar(Scalar::Ptr(ptr), dest)?;
233233
}
@@ -839,7 +839,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
839839
let system_info = this.deref_operand(args[0])?;
840840
// Initialize with `0`.
841841
this.memory
842-
.write_bytes(system_info.ptr, itertools::repeat_n(0, system_info.layout.size.bytes() as usize))?;
842+
.write_bytes(system_info.ptr, iter::repeat(0u8).take(system_info.layout.size.bytes() as usize))?;
843843
// Set number of processors.
844844
let dword_size = Size::from_bytes(4);
845845
let num_cpus = this.mplace_field(system_info, 6)?;

src/shims/fs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
9595
throw_unsup_format!("unsupported flags {:#x}", flag & !mirror);
9696
}
9797

98-
let path: std::path::PathBuf = this.read_os_string_from_c_string(this.read_scalar(path_op)?.not_undef()?)?.into();
98+
let path = this.read_os_string_from_c_string(this.read_scalar(path_op)?.not_undef()?)?;
9999

100100
let fd = options.open(path).map(|file| {
101101
let mut fh = &mut this.machine.file_handler;

src/shims/intrinsics.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::iter;
2+
13
use rustc_apfloat::Float;
24
use rustc::mir;
35
use rustc::mir::interpret::{InterpResult, PointerArithmetic};
@@ -357,7 +359,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
357359
// Do it in memory
358360
let mplace = this.force_allocation(dest)?;
359361
mplace.meta.unwrap_none(); // must be sized
360-
this.memory.write_bytes(mplace.ptr, itertools::repeat_n(0, dest.layout.size.bytes() as usize))?;
362+
this.memory.write_bytes(mplace.ptr, iter::repeat(0u8).take(dest.layout.size.bytes() as usize))?;
361363
}
362364
}
363365
}
@@ -562,7 +564,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
562564
let ptr = this.read_scalar(args[0])?.not_undef()?;
563565
let count = this.read_scalar(args[2])?.to_usize(this)?;
564566
let byte_count = ty_layout.size * count;
565-
this.memory.write_bytes(ptr, itertools::repeat_n(val_byte, byte_count.bytes() as usize))?;
567+
this.memory.write_bytes(ptr, iter::repeat(val_byte).take(byte_count.bytes() as usize))?;
566568
}
567569

568570
name => throw_unsup_format!("unimplemented intrinsic: {}", name),

0 commit comments

Comments
 (0)