Skip to content

Commit 62280b4

Browse files
committed
Use libcore's align_offset
1 parent 7278385 commit 62280b4

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/shims/mod.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
pub mod foreign_items;
23
pub mod intrinsics;
34
pub mod tls;
@@ -27,9 +28,25 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
2728
}
2829
// There are some more lang items we want to hook that CTFE does not hook (yet).
2930
if this.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) {
30-
// FIXME: return a real value in case the target allocation has an
31-
// alignment bigger than the one requested.
32-
let n = u128::max_value();
31+
32+
let n = {
33+
let ptr = this.read_scalar(args[0])?.not_undef()?.assert_ptr();
34+
let align = this.force_bits(
35+
this.read_scalar(args[1])?.not_undef()?,
36+
this.pointer_size()
37+
)? as usize;
38+
39+
let stride = this.memory().get(ptr.alloc_id)?.align.bytes() as usize;
40+
// if the allocation alignment is at least the required alignment, we use the
41+
// libcore implementation
42+
if stride >= align {
43+
((stride + ptr.offset.bytes() as usize) as *const ())
44+
.align_offset(align) as u128
45+
} else {
46+
u128::max_value()
47+
}
48+
};
49+
3350
let dest = dest.unwrap();
3451
let n = this.truncate(n, dest.layout);
3552
this.write_scalar(Scalar::from_uint(n, dest.layout.size), dest)?;

0 commit comments

Comments
 (0)