Skip to content

Commit

Permalink
Use libcore's align_offset
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdrz committed Sep 11, 2019
1 parent dd94c7c commit 8f72164
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/shims/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

pub mod foreign_items;
pub mod intrinsics;
pub mod tls;
Expand Down Expand Up @@ -27,9 +28,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.read_scalar(args[0])?.not_undef()?.assert_ptr();
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)?;
Expand Down

0 comments on commit 8f72164

Please sign in to comment.