From 37b1190defc78a8d2d4139bfcb4816a1a7faf253 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 8 Nov 2019 22:07:52 +0100 Subject: [PATCH] rustup --- rust-version | 2 +- src/intptrcast.rs | 2 +- src/operator.rs | 2 +- src/shims/dlsym.rs | 2 +- src/shims/env.rs | 2 +- src/shims/foreign_items.rs | 58 +++++++++++++++++++------------------- src/shims/fs.rs | 10 ++++--- src/shims/intrinsics.rs | 10 +++---- src/shims/mod.rs | 2 +- src/stacked_borrows.rs | 4 +-- 10 files changed, 48 insertions(+), 46 deletions(-) diff --git a/rust-version b/rust-version index 1377ee94dfc3..ec45e1f97307 100644 --- a/rust-version +++ b/rust-version @@ -1 +1 @@ -c34472b77084c9f76f872871aeab121daf81fb99 +9e346646e93cc243567e27bb0f4e8716d56ad1f1 diff --git a/src/intptrcast.rs b/src/intptrcast.rs index e08166b8c2b2..a55c58c13add 100644 --- a/src/intptrcast.rs +++ b/src/intptrcast.rs @@ -63,7 +63,7 @@ pub fn int_to_ptr( // This never overflows because `int >= glb` let offset = int - glb; // If the offset exceeds the size of the allocation, this access is illegal - if offset <= memory.get(alloc_id)?.size.bytes() { + if offset <= memory.get_size_and_align(alloc_id, AllocCheck::MaybeDead)?.0.bytes() { // This pointer is untagged because it was created from a cast Pointer::new_with_tag(alloc_id, Size::from_bytes(offset), Tag::Untagged) } else { diff --git a/src/operator.rs b/src/operator.rs index 2a90d3e12f4c..6b2c12e6b0b0 100644 --- a/src/operator.rs +++ b/src/operator.rs @@ -75,7 +75,7 @@ fn binary_ptr_op( let ptr = self.pointer_offset_inbounds( left.to_scalar()?, pointee_ty, - right.to_scalar()?.to_isize(self)?, + right.to_scalar()?.to_machine_isize(self)?, )?; (ptr, false, left.layout.ty) } diff --git a/src/shims/dlsym.rs b/src/shims/dlsym.rs index 307de29f2203..ca53f5d23015 100644 --- a/src/shims/dlsym.rs +++ b/src/shims/dlsym.rs @@ -40,7 +40,7 @@ fn call_dlsym( match dlsym { GetEntropy => { let ptr = this.read_scalar(args[0])?.not_undef()?; - let len = this.read_scalar(args[1])?.to_usize(this)?; + let len = this.read_scalar(args[1])?.to_machine_usize(this)?; this.gen_random(ptr, len as usize)?; this.write_null(dest)?; } diff --git a/src/shims/env.rs b/src/shims/env.rs index 2dc47d74ffb8..44896fd9bbd5 100644 --- a/src/shims/env.rs +++ b/src/shims/env.rs @@ -124,7 +124,7 @@ fn getcwd( this.check_no_isolation("getcwd")?; let buf = this.read_scalar(buf_op)?.not_undef()?; - let size = this.read_scalar(size_op)?.to_usize(&*this.tcx)?; + let size = this.read_scalar(size_op)?.to_machine_usize(&*this.tcx)?; // If we cannot get the current directory, we return null match env::current_dir() { Ok(cwd) => { diff --git a/src/shims/foreign_items.rs b/src/shims/foreign_items.rs index 74ce477b8e35..1f43a83576f6 100644 --- a/src/shims/foreign_items.rs +++ b/src/shims/foreign_items.rs @@ -144,13 +144,13 @@ fn emulate_foreign_item( let ret = ret.expect("dest is `Some` but ret is `None`"); match link_name { "malloc" => { - let size = this.read_scalar(args[0])?.to_usize(this)?; + let size = this.read_scalar(args[0])?.to_machine_usize(this)?; let res = this.malloc(size, /*zero_init:*/ false, MiriMemoryKind::C); this.write_scalar(res, dest)?; } "calloc" => { - let items = this.read_scalar(args[0])?.to_usize(this)?; - let len = this.read_scalar(args[1])?.to_usize(this)?; + let items = this.read_scalar(args[0])?.to_machine_usize(this)?; + let len = this.read_scalar(args[1])?.to_machine_usize(this)?; let size = items .checked_mul(len) .ok_or_else(|| err_panic!(Overflow(mir::BinOp::Mul)))?; @@ -159,8 +159,8 @@ fn emulate_foreign_item( } "posix_memalign" => { let ret = this.deref_operand(args[0])?; - let align = this.read_scalar(args[1])?.to_usize(this)?; - let size = this.read_scalar(args[2])?.to_usize(this)?; + let align = this.read_scalar(args[1])?.to_machine_usize(this)?; + let size = this.read_scalar(args[2])?.to_machine_usize(this)?; // Align must be power of 2, and also at least ptr-sized (POSIX rules). if !align.is_power_of_two() { throw_unsup!(HeapAllocNonPowerOfTwoAlignment(align)); @@ -190,14 +190,14 @@ fn emulate_foreign_item( } "realloc" => { let old_ptr = this.read_scalar(args[0])?.not_undef()?; - let new_size = this.read_scalar(args[1])?.to_usize(this)?; + let new_size = this.read_scalar(args[1])?.to_machine_usize(this)?; let res = this.realloc(old_ptr, new_size, MiriMemoryKind::C)?; this.write_scalar(res, dest)?; } "__rust_alloc" => { - let size = this.read_scalar(args[0])?.to_usize(this)?; - let align = this.read_scalar(args[1])?.to_usize(this)?; + let size = this.read_scalar(args[0])?.to_machine_usize(this)?; + let align = this.read_scalar(args[1])?.to_machine_usize(this)?; if size == 0 { throw_unsup!(HeapAllocZeroBytes); } @@ -212,8 +212,8 @@ fn emulate_foreign_item( this.write_scalar(Scalar::Ptr(ptr), dest)?; } "__rust_alloc_zeroed" => { - let size = this.read_scalar(args[0])?.to_usize(this)?; - let align = this.read_scalar(args[1])?.to_usize(this)?; + let size = this.read_scalar(args[0])?.to_machine_usize(this)?; + let align = this.read_scalar(args[1])?.to_machine_usize(this)?; if size == 0 { throw_unsup!(HeapAllocZeroBytes); } @@ -233,8 +233,8 @@ fn emulate_foreign_item( } "__rust_dealloc" => { let ptr = this.read_scalar(args[0])?.not_undef()?; - let old_size = this.read_scalar(args[1])?.to_usize(this)?; - let align = this.read_scalar(args[2])?.to_usize(this)?; + let old_size = this.read_scalar(args[1])?.to_machine_usize(this)?; + let align = this.read_scalar(args[2])?.to_machine_usize(this)?; if old_size == 0 { throw_unsup!(HeapAllocZeroBytes); } @@ -253,9 +253,9 @@ fn emulate_foreign_item( } "__rust_realloc" => { let ptr = this.read_scalar(args[0])?.to_ptr()?; - let old_size = this.read_scalar(args[1])?.to_usize(this)?; - let align = this.read_scalar(args[2])?.to_usize(this)?; - let new_size = this.read_scalar(args[3])?.to_usize(this)?; + let old_size = this.read_scalar(args[1])?.to_machine_usize(this)?; + let align = this.read_scalar(args[2])?.to_machine_usize(this)?; + let new_size = this.read_scalar(args[3])?.to_machine_usize(this)?; if old_size == 0 || new_size == 0 { throw_unsup!(HeapAllocZeroBytes); } @@ -277,11 +277,11 @@ fn emulate_foreign_item( let sys_getrandom = this .eval_path_scalar(&["libc", "SYS_getrandom"])? .expect("Failed to get libc::SYS_getrandom") - .to_usize(this)?; + .to_machine_usize(this)?; // `libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)` // is called if a `HashMap` is created the regular way (e.g. HashMap). - match this.read_scalar(args[0])?.to_usize(this)? { + match this.read_scalar(args[0])?.to_machine_usize(this)? { id if id == sys_getrandom => { // The first argument is the syscall id, // so skip over it. @@ -357,7 +357,7 @@ fn emulate_foreign_item( "memcmp" => { let left = this.read_scalar(args[0])?.not_undef()?; let right = this.read_scalar(args[1])?.not_undef()?; - let n = Size::from_bytes(this.read_scalar(args[2])?.to_usize(this)?); + let n = Size::from_bytes(this.read_scalar(args[2])?.to_machine_usize(this)?); let result = { let left_bytes = this.memory.read_bytes(left, n)?; @@ -377,7 +377,7 @@ fn emulate_foreign_item( "memrchr" => { let ptr = this.read_scalar(args[0])?.not_undef()?; let val = this.read_scalar(args[1])?.to_i32()? as u8; - let num = this.read_scalar(args[2])?.to_usize(this)?; + let num = this.read_scalar(args[2])?.to_machine_usize(this)?; if let Some(idx) = this .memory .read_bytes(ptr, Size::from_bytes(num))? @@ -395,7 +395,7 @@ fn emulate_foreign_item( "memchr" => { let ptr = this.read_scalar(args[0])?.not_undef()?; let val = this.read_scalar(args[1])?.to_i32()? as u8; - let num = this.read_scalar(args[2])?.to_usize(this)?; + let num = this.read_scalar(args[2])?.to_machine_usize(this)?; let idx = this .memory .read_bytes(ptr, Size::from_bytes(num))? @@ -462,7 +462,7 @@ fn emulate_foreign_item( "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(tcx)?; + let n = this.read_scalar(args[2])?.to_machine_usize(tcx)?; trace!("Called write({:?}, {:?}, {:?})", fd, buf, n); let result = if fd == 1 || fd == 2 { // stdout/stderr @@ -771,7 +771,7 @@ fn emulate_foreign_item( this.write_scalar(this.machine.argv.expect("machine must be initialized"), dest)?; } "SecRandomCopyBytes" => { - let len = this.read_scalar(args[1])?.to_usize(this)?; + let len = this.read_scalar(args[1])?.to_machine_usize(this)?; let ptr = this.read_scalar(args[2])?.not_undef()?; this.gen_random(ptr, len as usize)?; this.write_null(dest)?; @@ -786,25 +786,25 @@ fn emulate_foreign_item( this.write_scalar(Scalar::from_int(1, this.pointer_size()), dest)?; } "HeapAlloc" => { - let _handle = this.read_scalar(args[0])?.to_isize(this)?; + let _handle = this.read_scalar(args[0])?.to_machine_isize(this)?; let flags = this.read_scalar(args[1])?.to_u32()?; - let size = this.read_scalar(args[2])?.to_usize(this)?; + let size = this.read_scalar(args[2])?.to_machine_usize(this)?; let zero_init = (flags & 0x00000008) != 0; // HEAP_ZERO_MEMORY let res = this.malloc(size, zero_init, MiriMemoryKind::WinHeap); this.write_scalar(res, dest)?; } "HeapFree" => { - let _handle = this.read_scalar(args[0])?.to_isize(this)?; + let _handle = this.read_scalar(args[0])?.to_machine_isize(this)?; let _flags = this.read_scalar(args[1])?.to_u32()?; let ptr = this.read_scalar(args[2])?.not_undef()?; this.free(ptr, MiriMemoryKind::WinHeap)?; this.write_scalar(Scalar::from_int(1, Size::from_bytes(4)), dest)?; } "HeapReAlloc" => { - let _handle = this.read_scalar(args[0])?.to_isize(this)?; + let _handle = this.read_scalar(args[0])?.to_machine_isize(this)?; let _flags = this.read_scalar(args[1])?.to_u32()?; let ptr = this.read_scalar(args[2])?.not_undef()?; - let size = this.read_scalar(args[3])?.to_usize(this)?; + let size = this.read_scalar(args[3])?.to_machine_usize(this)?; let res = this.realloc(ptr, size, MiriMemoryKind::WinHeap)?; this.write_scalar(res, dest)?; } @@ -883,7 +883,7 @@ fn emulate_foreign_item( this.write_scalar(Scalar::from_int(which, this.pointer_size()), dest)?; } "WriteFile" => { - let handle = this.read_scalar(args[0])?.to_isize(this)?; + let handle = this.read_scalar(args[0])?.to_machine_isize(this)?; let buf = this.read_scalar(args[1])?.not_undef()?; let n = this.read_scalar(args[2])?.to_u32()?; let written_place = this.deref_operand(args[3])?; @@ -973,7 +973,7 @@ fn linux_getrandom<'tcx>( dest: PlaceTy<'tcx, Tag>, ) -> InterpResult<'tcx> { let ptr = this.read_scalar(args[0])?.not_undef()?; - let len = this.read_scalar(args[1])?.to_usize(this)?; + let len = this.read_scalar(args[1])?.to_machine_usize(this)?; // The only supported flags are GRND_RANDOM and GRND_NONBLOCK, // neither of which have any effect on our current PRNG. diff --git a/src/shims/fs.rs b/src/shims/fs.rs index c484795d8fec..764f345904fa 100644 --- a/src/shims/fs.rs +++ b/src/shims/fs.rs @@ -154,7 +154,7 @@ fn read( this.check_no_isolation("read")?; - let count = this.read_scalar(count_op)?.to_usize(&*this.tcx)?; + let count = this.read_scalar(count_op)?.to_machine_usize(&*this.tcx)?; // Reading zero bytes should not change `buf`. if count == 0 { return Ok(0); @@ -166,8 +166,9 @@ fn read( this.remove_handle_and(fd, |mut handle, this| { // Don't use `?` to avoid returning before reinserting the handle. let bytes = this.force_ptr(buf_scalar).and_then(|buf| { + // FIXME: Don't use raw methods this.memory - .get_mut(buf.alloc_id)? + .get_raw_mut(buf.alloc_id)? .get_bytes_mut(&*this.tcx, buf, Size::from_bytes(count)) .map(|buffer| handle.file.read(buffer)) }); @@ -186,7 +187,7 @@ fn write( this.check_no_isolation("write")?; - let count = this.read_scalar(count_op)?.to_usize(&*this.tcx)?; + let count = this.read_scalar(count_op)?.to_machine_usize(&*this.tcx)?; // Writing zero bytes should not change `buf`. if count == 0 { return Ok(0); @@ -195,7 +196,8 @@ fn write( let buf = this.force_ptr(this.read_scalar(buf_op)?.not_undef()?)?; this.remove_handle_and(fd, |mut handle, this| { - let bytes = this.memory.get(buf.alloc_id).and_then(|alloc| { + // FIXME: Don't use raw methods + let bytes = this.memory.get_raw(buf.alloc_id).and_then(|alloc| { alloc .get_bytes(&*this.tcx, buf, Size::from_bytes(count)) .map(|bytes| handle.file.write(bytes).map(|bytes| bytes as i64)) diff --git a/src/shims/intrinsics.rs b/src/shims/intrinsics.rs index 7c8c06cbbfd5..7470090f5208 100644 --- a/src/shims/intrinsics.rs +++ b/src/shims/intrinsics.rs @@ -35,7 +35,7 @@ fn call_intrinsic( 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)?; + let offset = this.read_scalar(args[1])?.to_machine_isize(this)?; let ptr = this.read_scalar(args[0])?.not_undef()?; let pointee_ty = substs.type_at(0); @@ -206,7 +206,7 @@ fn call_intrinsic( let elem_ty = substs.type_at(0); let elem_layout = this.layout_of(elem_ty)?; let elem_size = elem_layout.size.bytes(); - let count = this.read_scalar(args[2])?.to_usize(this)?; + let count = this.read_scalar(args[2])?.to_machine_usize(this)?; let elem_align = elem_layout.align.abi; let size = Size::from_bytes(count * elem_size); @@ -371,7 +371,7 @@ fn call_intrinsic( } "offset" => { - let offset = this.read_scalar(args[1])?.to_isize(this)?; + let offset = this.read_scalar(args[1])?.to_machine_isize(this)?; let ptr = this.read_scalar(args[0])?.not_undef()?; let result_ptr = this.pointer_offset_inbounds(ptr, substs.type_at(0), offset)?; this.write_scalar(result_ptr, dest)?; @@ -542,7 +542,7 @@ fn call_intrinsic( let ptr = mplace.ptr.to_ptr()?; // We know the return place is in-bounds this.memory - .get_mut(ptr.alloc_id)? + .get_raw_mut(ptr.alloc_id)? .mark_definedness(ptr, dest.layout.size, false); } } @@ -554,7 +554,7 @@ fn call_intrinsic( let ty_layout = this.layout_of(ty)?; let val_byte = this.read_scalar(args[1])?.to_u8()?; let ptr = this.read_scalar(args[0])?.not_undef()?; - let count = this.read_scalar(args[2])?.to_usize(this)?; + let count = this.read_scalar(args[2])?.to_machine_usize(this)?; let byte_count = ty_layout.size * count; this.memory.write_bytes(ptr, iter::repeat(val_byte).take(byte_count.bytes() as usize))?; } diff --git a/src/shims/mod.rs b/src/shims/mod.rs index 60974958c42a..3302143f48cb 100644 --- a/src/shims/mod.rs +++ b/src/shims/mod.rs @@ -75,7 +75,7 @@ fn align_offset( 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; + let cur_align = this.memory.get_size_and_align(ptr.alloc_id, AllocCheck::MaybeDead)?.1.bytes() as usize; if cur_align >= req_align { // if the allocation alignment is at least the required alignment we use the // libcore implementation diff --git a/src/stacked_borrows.rs b/src/stacked_borrows.rs index 6e63bb073c8c..94e69203437b 100644 --- a/src/stacked_borrows.rs +++ b/src/stacked_borrows.rs @@ -540,8 +540,8 @@ fn reborrow( kind, new_tag, ptr.tag, place.layout.ty, ptr.erase_tag(), size.bytes()); // Get the allocation. It might not be mutable, so we cannot use `get_mut`. - let alloc = this.memory.get(ptr.alloc_id)?; - let stacked_borrows = alloc.extra.stacked_borrows.as_ref().expect("we should have Stacked Borrows data"); + let extra = &this.memory.get_raw(ptr.alloc_id)?.extra; + let stacked_borrows = extra.stacked_borrows.as_ref().expect("we should have Stacked Borrows data"); // Update the stacks. // Make sure that raw pointers and mutable shared references are reborrowed "weak": // There could be existing unique pointers reborrowed from them that should remain valid!