diff --git a/.appveyor.yml b/.appveyor.yml index 391ff042daf..c3d575403cb 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -17,16 +17,21 @@ cache: - '%USERPROFILE%\.rustup' install: + # Compute the rust version we use + - set /p RUSTC_HASH=(TyCtxt<'tcx>); impl<'tcx, 'hir> itemlikevisit::ItemLikeVisitor<'hir> for Visitor<'tcx> { fn visit_item(&mut self, i: &'hir hir::Item) { - if let hir::ItemKind::Fn(.., body_id) = i.node { + if let hir::ItemKind::Fn(.., body_id) = i.kind { if i.attrs.iter().any(|attr| attr.check_name(syntax::symbol::sym::test)) { let config = MiriConfig { validate: true, diff --git a/src/eval.rs b/src/eval.rs index 667491f8d47..1b7c082ec37 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -63,7 +63,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>( ty::ParamEnv::reveal_all(), start_id, ecx.tcx.mk_substs( - ::std::iter::once(ty::subst::Kind::from(main_ret_ty))) + ::std::iter::once(ty::subst::GenericArg::from(main_ret_ty))) ).unwrap(); let start_mir = ecx.load_mir(start_instance.def, None)?; diff --git a/src/helpers.rs b/src/helpers.rs index ad30040c2dd..3bee028c5eb 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -211,7 +211,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx fn visit_value(&mut self, v: MPlaceTy<'tcx, Tag>) -> InterpResult<'tcx> { trace!("UnsafeCellVisitor: {:?} {:?}", *v, v.layout.ty); - let is_unsafe_cell = match v.layout.ty.sty { + let is_unsafe_cell = match v.layout.ty.kind { ty::Adt(adt, _) => Some(adt.did) == self.ecx.tcx.lang_items().unsafe_cell_type(), _ => false, }; diff --git a/src/shims/io.rs b/src/shims/io.rs index 180748a0755..6f8785891c2 100644 --- a/src/shims/io.rs +++ b/src/shims/io.rs @@ -5,13 +5,23 @@ use std::io::Read; use crate::stacked_borrows::Tag; use crate::*; -#[derive(Default)] pub struct FileHandler { files: HashMap, flags: HashMap, low: i32, } +impl Default for FileHandler { + fn default() -> Self { + FileHandler { + files: Default::default(), + flags: Default::default(), + // 0, 1 and 2 are reserved for stdin, stdout and stderr + low: 3, + } + } +} + impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {} pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> { fn open( diff --git a/src/shims/mod.rs b/src/shims/mod.rs index 981185d252e..3df5b839e5d 100644 --- a/src/shims/mod.rs +++ b/src/shims/mod.rs @@ -1,11 +1,11 @@ +pub mod dlsym; +pub mod env; pub mod foreign_items; pub mod intrinsics; pub mod tls; -pub mod dlsym; -pub mod env; pub mod io; -use rustc::{ty, mir}; +use rustc::{mir, ty}; use crate::*; @@ -19,7 +19,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx ret: Option, ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> { let this = self.eval_context_mut(); - trace!("eval_fn_call: {:#?}, {:?}", instance, dest.map(|place| *place)); + trace!( + "eval_fn_call: {:#?}, {:?}", + instance, + dest.map(|place| *place) + ); // First, run the common hooks also supported by CTFE. if this.hook_fn(instance, args, dest)? { @@ -28,27 +32,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx } // There are some more lang items we want to hook that CTFE does not hook (yet). if this.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) { - - let n = { - let ptr = this.force_ptr(this.read_scalar(args[0])?.not_undef()?)?; - let align = this.force_bits( - this.read_scalar(args[1])?.not_undef()?, - this.pointer_size() - )? as usize; - - let stride = this.memory().get(ptr.alloc_id)?.align.bytes() as usize; - // if the allocation alignment is at least the required alignment, we use the - // libcore implementation - if stride >= align { - ((stride + ptr.offset.bytes() as usize) as *const ()) - .align_offset(align) as u128 - } else { - u128::max_value() - } - }; - let dest = dest.unwrap(); - let n = this.truncate(n, dest.layout); + let n = this + .align_offset(args[0], args[1])? + .unwrap_or_else(|| this.truncate(u128::max_value(), dest.layout)); this.write_scalar(Scalar::from_uint(n, dest.layout.size), dest)?; this.goto_block(ret)?; return Ok(None); @@ -66,4 +53,39 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx // Otherwise, load the MIR. Ok(Some(this.load_mir(instance.def, None)?)) } + + fn align_offset( + &mut self, + ptr_op: OpTy<'tcx, Tag>, + align_op: OpTy<'tcx, Tag>, + ) -> InterpResult<'tcx, Option> { + let this = self.eval_context_mut(); + + let req_align = this.force_bits( + this.read_scalar(align_op)?.not_undef()?, + this.pointer_size(), + )? as usize; + + // FIXME: This should actually panic in the interpreted program + if !req_align.is_power_of_two() { + throw_unsup_format!("Required alignment should always be a power of two") + } + + let ptr_scalar = this.read_scalar(ptr_op)?.not_undef()?; + + if let Ok(ptr) = this.force_ptr(ptr_scalar) { + let cur_align = this.memory().get(ptr.alloc_id)?.align.bytes() as usize; + if cur_align >= req_align { + // if the allocation alignment is at least the required alignment we use the + // libcore implementation + return Ok(Some( + (this.force_bits(ptr_scalar, this.pointer_size())? as *const i8) + .align_offset(req_align) as u128, + )); + } + } + // If the allocation alignment is smaller than then required alignment or the pointer was + // actually an integer, we return `None` + Ok(None) + } } diff --git a/src/stacked_borrows.rs b/src/stacked_borrows.rs index 01ed6ec225d..5258cbb5485 100644 --- a/src/stacked_borrows.rs +++ b/src/stacked_borrows.rs @@ -435,7 +435,7 @@ impl<'tcx> Stacks { Stacks { stacks: RefCell::new(RangeMap::new(size, stack)), - global: extra, + global: extra, } } @@ -460,7 +460,7 @@ impl Stacks { pub fn new_allocation( id: AllocId, size: Size, - extra: MemoryExtra, + extra: MemoryExtra, kind: MemoryKind, ) -> (Self, Tag) { let (tag, perm) = match kind { @@ -616,7 +616,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx // Cannot use `builtin_deref` because that reports *immutable* for `Box`, // making it useless. fn qualify(ty: ty::Ty<'_>, kind: RetagKind) -> Option<(RefKind, bool)> { - match ty.sty { + match ty.kind { // References are simple. ty::Ref(_, _, MutMutable) => Some((RefKind::Unique { two_phase: kind == RetagKind::TwoPhase}, kind == RetagKind::FnEntry)), diff --git a/test-cargo-miri/Cargo.lock b/test-cargo-miri/Cargo.lock index 2a6a32a18f0..70476fbdcb0 100644 --- a/test-cargo-miri/Cargo.lock +++ b/test-cargo-miri/Cargo.lock @@ -25,22 +25,22 @@ version = "0.1.0" dependencies = [ "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "cfg-if" -version = "0.1.9" +version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "getrandom" -version = "0.1.11" +version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", - "wasi 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "wasi 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -68,13 +68,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "rand" -version = "0.7.0" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "getrandom 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "getrandom 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", "rand_chacha 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand_pcg 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -85,15 +85,15 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "c2-chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "rand_core" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "getrandom 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "getrandom 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -101,7 +101,7 @@ name = "rand_hc" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "rand_core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -110,27 +110,27 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "autocfg 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "wasi" -version = "0.5.0" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum autocfg 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "b671c8fb71b457dd4ae18c4ba1e59aa81793daacc361d82fcd410cef0d491875" "checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" "checksum c2-chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7d64d04786e0f528460fc884753cf8dddcc466be308f6026f8e355c41a0e4101" -"checksum cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33" -"checksum getrandom 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "fc344b02d3868feb131e8b5fe2b9b0a1cc42942679af493061fc13b853243872" +"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +"checksum getrandom 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "473a1265acc8ff1e808cd0a1af8cee3c2ee5200916058a2ca113c29f2d903571" "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba" "checksum num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bcef43580c035376c0705c42792c294b66974abbfd2789b511784023f71f3273" "checksum ppv-lite86 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e3cbf9f658cdb5000fcf6f362b8ea2ba154b9f146a61c7a20d647034c6b6561b" -"checksum rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d47eab0e83d9693d40f825f86948aa16eff6750ead4bdffc4ab95b8b3a7f052c" +"checksum rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3ae1b169243eaf61759b8475a998f0a385e42042370f3a7dbaf35246eacc8412" "checksum rand_chacha 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "03a2a90da8c7523f554344f921aa97283eadf6ac484a6d2a7d0212fa7f8d6853" -"checksum rand_core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "615e683324e75af5d43d8f7a39ffe3ee4a9dc42c5c701167a71dc59c3a493aca" +"checksum rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" "checksum rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" "checksum rand_pcg 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3e196346cbbc5c70c77e7b4926147ee8e383a38ee4d15d58a08098b169e492b6" -"checksum wasi 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fd5442abcac6525a045cc8c795aedb60da7a2e5e89c7bf18a0d5357849bb23c7" +"checksum wasi 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b89c3ce4ce14bdc6fb6beaf9ec7928ca331de5df7e5ea278375642a2f478570d" diff --git a/tests/run-pass/aligned_utf8_check.rs b/tests/run-pass/aligned_utf8_check.rs index 202856b3bde..6c6ff6b6173 100644 --- a/tests/run-pass/aligned_utf8_check.rs +++ b/tests/run-pass/aligned_utf8_check.rs @@ -1,13 +1,6 @@ fn main() { const N: usize = 10; - - let x = vec![0x4141u16; N]; - - let mut y: Vec = unsafe { std::mem::transmute(x) }; - unsafe { y.set_len(2 * N) }; - - println!("{:?}", std::str::from_utf8(&y).unwrap()); - - let mut x: Vec = unsafe { std::mem::transmute(y) }; - unsafe { x.set_len(N) }; + let vec = vec![0x4141414141414141u64; N]; + let content = unsafe { std::slice::from_raw_parts(vec.as_ptr() as *const u8, 8 * N) }; + println!("{:?}", std::str::from_utf8(content).unwrap()); } diff --git a/tests/run-pass/aligned_utf8_check.stdout b/tests/run-pass/aligned_utf8_check.stdout index 8d08312cac7..66d43994815 100644 --- a/tests/run-pass/aligned_utf8_check.stdout +++ b/tests/run-pass/aligned_utf8_check.stdout @@ -1 +1 @@ -"AAAAAAAAAAAAAAAAAAAA" +"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"