Skip to content

Commit

Permalink
Auto merge of #957 - christianpoveda:ptr-align-offset, r=RalfJung
Browse files Browse the repository at this point in the history
Fixes for align_offset

This addresses @RalfJung's comments in #945
  • Loading branch information
bors committed Sep 28, 2019
2 parents 918a9af + e2c54e6 commit b625812
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 35 deletions.
70 changes: 46 additions & 24 deletions src/shims/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
pub mod dlsym;
pub mod env;
pub mod foreign_items;
pub mod intrinsics;
pub mod tls;
pub mod dlsym;
pub mod env;

use rustc::{ty, mir};
use rustc::{mir, ty};

use crate::*;

Expand All @@ -18,7 +18,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
ret: Option<mir::BasicBlock>,
) -> 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)? {
Expand All @@ -27,27 +31,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);
Expand All @@ -65,4 +52,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<u128>> {
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)
}
}
13 changes: 3 additions & 10 deletions tests/run-pass/aligned_utf8_check.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
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) };
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());
}
2 changes: 1 addition & 1 deletion tests/run-pass/aligned_utf8_check.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"AAAAAAAAAAAAAAAAAAAA"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"

0 comments on commit b625812

Please sign in to comment.