Skip to content

Commit d0a1050

Browse files
committed
Auto merge of #945 - christianpoveda:ptr-align-offset, r=oli-obk
Use libcore's align_offset Related issue: #873
2 parents d881387 + 55863cb commit d0a1050

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/shims/mod.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,25 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
2727
}
2828
// There are some more lang items we want to hook that CTFE does not hook (yet).
2929
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();
30+
31+
let n = {
32+
let ptr = this.force_ptr(this.read_scalar(args[0])?.not_undef()?)?;
33+
let align = this.force_bits(
34+
this.read_scalar(args[1])?.not_undef()?,
35+
this.pointer_size()
36+
)? as usize;
37+
38+
let stride = this.memory().get(ptr.alloc_id)?.align.bytes() as usize;
39+
// if the allocation alignment is at least the required alignment, we use the
40+
// libcore implementation
41+
if stride >= align {
42+
((stride + ptr.offset.bytes() as usize) as *const ())
43+
.align_offset(align) as u128
44+
} else {
45+
u128::max_value()
46+
}
47+
};
48+
3349
let dest = dest.unwrap();
3450
let n = this.truncate(n, dest.layout);
3551
this.write_scalar(Scalar::from_uint(n, dest.layout.size), dest)?;

tests/run-pass/aligned_utf8_check.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn main() {
2+
const N: usize = 10;
3+
4+
let x = vec![0x4141u16; N];
5+
6+
let mut y: Vec<u8> = unsafe { std::mem::transmute(x) };
7+
unsafe { y.set_len(2 * N) };
8+
9+
println!("{:?}", std::str::from_utf8(&y).unwrap());
10+
11+
let mut x: Vec<u16> = unsafe { std::mem::transmute(y) };
12+
unsafe { x.set_len(N) };
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"AAAAAAAAAAAAAAAAAAAA"

0 commit comments

Comments
 (0)