Auto merge of #945 - christianpoveda:ptr-align-offset, r=oli-obk

Use libcore's align_offset

Related issue: https://github.com/rust-lang/miri/issues/873
This commit is contained in:
bors 2019-09-16 15:52:22 +00:00
commit d0a10507e4
3 changed files with 33 additions and 3 deletions

View File

@ -27,9 +27,25 @@ 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()) {
// FIXME: return a real value in case the target allocation has an
// alignment bigger than the one requested.
let n = u128::max_value();
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);
this.write_scalar(Scalar::from_uint(n, dest.layout.size), dest)?;

View File

@ -0,0 +1,13 @@
fn main() {
const N: usize = 10;
let x = vec![0x4141u16; N];
let mut y: Vec<u8> = unsafe { std::mem::transmute(x) };
unsafe { y.set_len(2 * N) };
println!("{:?}", std::str::from_utf8(&y).unwrap());
let mut x: Vec<u16> = unsafe { std::mem::transmute(y) };
unsafe { x.set_len(N) };
}

View File

@ -0,0 +1 @@
"AAAAAAAAAAAAAAAAAAAA"