Skip to content

Commit 2b7cf80

Browse files
committed
Auto merge of #1014 - RalfJung:rustup, r=RalfJung
rustup: fix for write_bytes and new union rules
2 parents 1a42107 + 2690f59 commit 2b7cf80

File tree

9 files changed

+41
-56
lines changed

9 files changed

+41
-56
lines changed

Cargo.lock

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

Cargo.toml

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

4445
# A noop dependency that changes in the Rust repository, it's a bit of a hack.
4546
# 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-
7979016aff545f7b41cc517031026020b340989d
1+
6576f4be5af31a5e61dfc0cf50b7130e6c6dfb35

src/eval.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,9 @@ pub fn eval_main<'tcx>(tcx: TyCtxt<'tcx>, main_id: DefId, config: MiriConfig) {
257257
trace!("-------------------");
258258
trace!("Frame {}", i);
259259
trace!(" return: {:?}", frame.return_place.map(|p| *p));
260-
for (i, local) in frame.locals.iter().enumerate() {
261-
trace!(" local {}: {:?}", i, local.value);
260+
for (_i, _local) in frame.locals.iter().enumerate() {
261+
//trace!(" local {}: {:?}", i, local.value);
262+
//FIXME: enable this again when the LocalValue Debug impl is back
262263
}
263264
}
264265
}

src/helpers.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
44
use rustc::mir;
55
use rustc::ty::{
66
self,
7-
layout::{self, Align, LayoutOf, Size, TyLayout},
7+
layout::{self, LayoutOf, Size, TyLayout},
88
};
99

1010
use rand::RngCore;
@@ -94,13 +94,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
9494
}
9595
let this = self.eval_context_mut();
9696

97-
// Don't forget the bounds check.
98-
let ptr = this.memory.check_ptr_access(
99-
ptr,
100-
Size::from_bytes(len as u64),
101-
Align::from_bytes(1).unwrap()
102-
)?.expect("we already checked for size 0");
103-
10497
let mut data = vec![0; len];
10598

10699
if this.machine.communicate {
@@ -113,7 +106,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
113106
rng.fill_bytes(&mut data);
114107
}
115108

116-
this.memory.get_mut(ptr.alloc_id)?.write_bytes(&*this.tcx, ptr, &data)
109+
this.memory.write_bytes(ptr, data.iter().copied())
117110
}
118111

119112
/// Visits the memory covered by `place`, sensitive to freezing: the 3rd parameter

src/shims/env.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
122122

123123
this.check_no_isolation("getcwd")?;
124124

125-
let buf = this.force_ptr(this.read_scalar(buf_op)?.not_undef()?)?;
125+
let buf = this.read_scalar(buf_op)?.not_undef()?;
126126
let size = this.read_scalar(size_op)?.to_usize(&*this.tcx)?;
127127
// If we cannot get the current directory, we return null
128128
match env::current_dir() {
@@ -138,10 +138,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
138138
// This is ok because the buffer was strictly larger than `bytes`, so after
139139
// adding the null terminator, the buffer size is larger or equal to
140140
// `bytes.len()`, meaning that `bytes` actually fit inside tbe buffer.
141-
this.memory
142-
.get_mut(buf.alloc_id)?
143-
.write_bytes(&*this.tcx, buf, &bytes)?;
144-
return Ok(Scalar::Ptr(buf));
141+
this.memory.write_bytes(buf, bytes.iter().copied())?;
142+
return Ok(buf);
145143
}
146144
let erange = this.eval_libc("ERANGE")?;
147145
this.set_last_error(erange)?;

src/shims/foreign_items.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +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-
.get_mut(ptr.alloc_id)
56-
.unwrap()
57-
.write_repeat(&*this.tcx, ptr, 0, Size::from_bytes(size))
55+
.write_bytes(ptr.into(), itertools::repeat_n(0u8, size as usize))
5856
.unwrap();
5957
}
6058
Scalar::Ptr(ptr)
@@ -229,9 +227,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
229227
);
230228
// We just allocated this, the access is definitely in-bounds.
231229
this.memory
232-
.get_mut(ptr.alloc_id)
233-
.unwrap()
234-
.write_repeat(tcx, ptr, 0, Size::from_bytes(size))
230+
.write_bytes(ptr.into(), itertools::repeat_n(0u8, size as usize))
235231
.unwrap();
236232
this.write_scalar(Scalar::Ptr(ptr), dest)?;
237233
}
@@ -841,25 +837,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
841837
}
842838
"GetSystemInfo" => {
843839
let system_info = this.deref_operand(args[0])?;
844-
let system_info_ptr = this
845-
.check_mplace_access(system_info, None)?
846-
.expect("cannot be a ZST");
847-
// We rely on `deref_operand` doing bounds checks for us.
848840
// Initialize with `0`.
849841
this.memory
850-
.get_mut(system_info_ptr.alloc_id)?
851-
.write_repeat(tcx, system_info_ptr, 0, system_info.layout.size)?;
842+
.write_bytes(system_info.ptr, itertools::repeat_n(0, system_info.layout.size.bytes() as usize))?;
852843
// Set number of processors.
853844
let dword_size = Size::from_bytes(4);
854-
let offset = 2 * dword_size + 3 * tcx.pointer_size();
855-
this.memory
856-
.get_mut(system_info_ptr.alloc_id)?
857-
.write_scalar(
858-
tcx,
859-
system_info_ptr.offset(offset, tcx)?,
860-
Scalar::from_int(NUM_CPUS, dword_size).into(),
861-
dword_size,
862-
)?;
845+
let num_cpus = this.mplace_field(system_info, 5)?;
846+
this.write_scalar(
847+
Scalar::from_int(NUM_CPUS, dword_size),
848+
num_cpus.into(),
849+
)?;
863850
}
864851

865852
"TlsAlloc" => {

src/shims/intrinsics.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
356356
_ => {
357357
// Do it in memory
358358
let mplace = this.force_allocation(dest)?;
359-
mplace.meta.unwrap_none();
360-
// not a zst, must be valid pointer
361-
let ptr = mplace.ptr.to_ptr()?;
362-
// we know the return place is in-bounds
363-
this.memory.get_mut(ptr.alloc_id)?.write_repeat(tcx, ptr, 0, dest.layout.size)?;
359+
mplace.meta.unwrap_none(); // must be sized
360+
this.memory.write_bytes(mplace.ptr, itertools::repeat_n(0, dest.layout.size.bytes() as usize))?;
364361
}
365362
}
366363
}
@@ -565,16 +562,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
565562
let ptr = this.read_scalar(args[0])?.not_undef()?;
566563
let count = this.read_scalar(args[2])?.to_usize(this)?;
567564
let byte_count = ty_layout.size * count;
568-
match this.memory.check_ptr_access(ptr, byte_count, ty_layout.align.abi)? {
569-
Some(ptr) => {
570-
this.memory
571-
.get_mut(ptr.alloc_id)?
572-
.write_repeat(tcx, ptr, val_byte, byte_count)?;
573-
}
574-
None => {
575-
// Size is 0, nothing to do.
576-
}
577-
}
565+
this.memory.write_bytes(ptr, itertools::repeat_n(val_byte, byte_count.bytes() as usize))?;
578566
}
579567

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

tests/run-pass/union-overwrite.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
#![feature(untagged_unions)]
2-
#![allow(unions_with_drop_fields)]
32

43
#[repr(C)]
4+
#[derive(Clone, Copy)]
55
struct Pair<T, U>(T, U);
66
#[repr(C)]
7+
#[derive(Clone, Copy)]
78
struct Triple<T>(T, T, T);
89

910
#[repr(C)]
10-
union U<A, B> {
11+
union U<A: Copy, B: Copy> {
1112
a: Pair<A, A>,
1213
b: B,
1314
}
1415

1516
#[repr(C)]
16-
union W<A, B> {
17+
union W<A: Copy, B: Copy> {
1718
a: A,
1819
b: B,
1920
}

0 commit comments

Comments
 (0)