Skip to content

Commit b324cbf

Browse files
committed
Merge remote-tracking branch 'origin/master' into rustup
2 parents e82693f + ad83707 commit b324cbf

26 files changed

+367
-222
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ Several `-Z` flags are relevant for Miri:
279279
* `-Zalways-encode-mir` makes rustc dump MIR even for completely monomorphic
280280
functions. This is needed so that Miri can execute such functions, so Miri
281281
sets this flag per default.
282+
* `-Zmir-emit-retag` controls whether `Retag` statements are emitted. Miri
283+
enables this per default because it is needed for validation.
282284

283285
Moreover, Miri recognizes some environment variables:
284286

@@ -327,6 +329,7 @@ Miri has already found a number of bugs in the Rust standard library and beyond,
327329
Definite bugs found:
328330

329331
* [`Debug for vec_deque::Iter` accessing uninitialized memory](https://github.com/rust-lang/rust/issues/53566)
332+
* [`Vec::into_iter` doing an unaligned ZST read](https://github.com/rust-lang/rust/pull/53804)
330333
* [`From<&[T]> for Rc` creating a not sufficiently aligned reference](https://github.com/rust-lang/rust/issues/54908)
331334
* [`BTreeMap` creating a shared reference pointing to a too small allocation](https://github.com/rust-lang/rust/issues/54957)
332335
* [`Vec::append` creating a dangling reference](https://github.com/rust-lang/rust/pull/61082)

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7e08576e4276a97b523c25bfd196d419c39c7b87
1+
24a9bcbb7cb0d8bdc11b8252a9c13f7562c7e4ca

src/eval.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc::hir::def_id::DefId;
1010
use rustc::mir;
1111

1212
use crate::{
13-
InterpResult, InterpError, InterpretCx, StackPopCleanup, struct_error,
13+
InterpResult, InterpError, InterpCx, StackPopCleanup, struct_error,
1414
Scalar, Tag, Pointer,
1515
MemoryExtra, MiriMemoryKind, Evaluator, TlsEvalContextExt,
1616
};
@@ -30,14 +30,23 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
3030
tcx: TyCtxt<'tcx>,
3131
main_id: DefId,
3232
config: MiriConfig,
33-
) -> InterpResult<'tcx, InterpretCx<'mir, 'tcx, Evaluator<'tcx>>> {
34-
let mut ecx = InterpretCx::new(
33+
) -> InterpResult<'tcx, InterpCx<'mir, 'tcx, Evaluator<'tcx>>> {
34+
35+
// FIXME(https://github.com/rust-lang/miri/pull/803): no validation on Windows.
36+
let target_os = tcx.sess.target.target.target_os.to_lowercase();
37+
let validate = if target_os == "windows" {
38+
false
39+
} else {
40+
config.validate
41+
};
42+
43+
let mut ecx = InterpCx::new(
3544
tcx.at(syntax::source_map::DUMMY_SP),
3645
ty::ParamEnv::reveal_all(),
37-
Evaluator::new(config.validate),
38-
MemoryExtra::with_rng(config.seed.map(StdRng::seed_from_u64)),
46+
Evaluator::new(),
47+
MemoryExtra::new(config.seed.map(StdRng::seed_from_u64), validate),
3948
);
40-
49+
4150
let main_instance = ty::Instance::mono(ecx.tcx.tcx, main_id);
4251
let main_mir = ecx.load_mir(main_instance.def)?;
4352

src/helpers.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
4343
})
4444
}
4545

46+
/// Write a 0 of the appropriate size to `dest`.
47+
fn write_null(&mut self, dest: PlaceTy<'tcx, Tag>) -> InterpResult<'tcx> {
48+
self.eval_context_mut().write_scalar(Scalar::from_int(0, dest.layout.size), dest)
49+
}
50+
51+
/// Test if this immediate equals 0.
52+
fn is_null(&self, val: Scalar<Tag>) -> InterpResult<'tcx, bool> {
53+
let this = self.eval_context_ref();
54+
let null = Scalar::from_int(0, this.memory().pointer_size());
55+
this.ptr_eq(val, null)
56+
}
57+
58+
/// Turn a Scalar into an Option<NonNullScalar>
59+
fn test_null(&self, val: Scalar<Tag>) -> InterpResult<'tcx, Option<Scalar<Tag>>> {
60+
let this = self.eval_context_ref();
61+
Ok(if this.is_null(val)? {
62+
None
63+
} else {
64+
Some(val)
65+
})
66+
}
67+
4668
/// Visits the memory covered by `place`, sensitive to freezing: the 3rd parameter
4769
/// will be true if this is frozen, false if this is in an `UnsafeCell`.
4870
fn visit_freeze_sensitive(
@@ -58,6 +80,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5880
.map(|(size, _)| size)
5981
.unwrap_or_else(|| place.layout.size)
6082
);
83+
assert!(size.bytes() > 0);
6184
// Store how far we proceeded into the place so far. Everything to the left of
6285
// this offset has already been handled, in the sense that the frozen parts
6386
// have had `action` called on them.

src/intptrcast.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use std::cmp::max;
44

55
use rand::Rng;
66

7-
use rustc_mir::interpret::{AllocId, Pointer, InterpResult, Memory, AllocCheck};
7+
use rustc::ty::layout::HasDataLayout;
8+
use rustc_mir::interpret::{AllocId, Pointer, InterpResult, Memory, AllocCheck, PointerArithmetic};
89
use rustc_target::abi::Size;
910

1011
use crate::{Evaluator, Tag, STACK_ADDR};
@@ -75,7 +76,9 @@ impl<'mir, 'tcx> GlobalState {
7576
let mut global_state = memory.extra.intptrcast.borrow_mut();
7677
let global_state = &mut *global_state;
7778

78-
let (size, align) = memory.get_size_and_align(ptr.alloc_id, AllocCheck::Live)?;
79+
// There is nothing wrong with a raw pointer being cast to an integer only after
80+
// it became dangling. Hence `MaybeDead`.
81+
let (size, align) = memory.get_size_and_align(ptr.alloc_id, AllocCheck::MaybeDead)?;
7982

8083
let base_addr = match global_state.base_addr.entry(ptr.alloc_id) {
8184
Entry::Occupied(entry) => *entry.get(),
@@ -107,7 +110,9 @@ impl<'mir, 'tcx> GlobalState {
107110
};
108111

109112
debug_assert_eq!(base_addr % align.bytes(), 0); // sanity check
110-
Ok(base_addr + ptr.offset.bytes())
113+
// Add offset with the right kind of pointer-overflowing arithmetic.
114+
let dl = memory.data_layout();
115+
Ok(dl.overflowing_offset(base_addr, ptr.offset.bytes()).0)
111116
}
112117

113118
/// Shifts `addr` to make it aligned with `align` by rounding `addr` to the smallest multiple

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ extern crate rustc_target;
1515
mod shims;
1616
mod operator;
1717
mod helpers;
18-
mod tls;
1918
mod range_map;
2019
mod mono_hash_map;
2120
mod stacked_borrows;
@@ -28,10 +27,11 @@ pub use rustc_mir::interpret::*;
2827
// Resolve ambiguity.
2928
pub use rustc_mir::interpret::{self, AllocMap, PlaceTy};
3029

30+
pub use crate::shims::{EvalContextExt as ShimsEvalContextExt};
3131
pub use crate::shims::foreign_items::EvalContextExt as ForeignItemsEvalContextExt;
3232
pub use crate::shims::intrinsics::EvalContextExt as IntrinsicsEvalContextExt;
33+
pub use crate::shims::tls::{EvalContextExt as TlsEvalContextExt, TlsData};
3334
pub use crate::operator::EvalContextExt as OperatorEvalContextExt;
34-
pub use crate::tls::{EvalContextExt as TlsEvalContextExt, TlsData};
3535
pub use crate::range_map::RangeMap;
3636
pub use crate::helpers::{EvalContextExt as HelpersEvalContextExt};
3737
pub use crate::mono_hash_map::MonoHashMap;

0 commit comments

Comments
 (0)