From 62280b4b1166830facf1b43d6dda13df687252c8 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Wed, 11 Sep 2019 11:09:56 -0500 Subject: [PATCH 1/4] Use libcore's align_offset --- src/shims/mod.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/shims/mod.rs b/src/shims/mod.rs index 5fbe6e37998..5b67d075a5c 100644 --- a/src/shims/mod.rs +++ b/src/shims/mod.rs @@ -1,3 +1,4 @@ + pub mod foreign_items; pub mod intrinsics; pub mod tls; @@ -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)?; From ed70617b9c78ab28c1c6616e112b4516b8d4a7de Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Wed, 11 Sep 2019 12:08:42 -0500 Subject: [PATCH 2/4] Add test for u8 align_offset --- tests/run-pass/aligned_utf8_check.rs | 13 +++++++++++++ tests/run-pass/aligned_utf8_check.stdout | 1 + 2 files changed, 14 insertions(+) create mode 100644 tests/run-pass/aligned_utf8_check.rs create mode 100644 tests/run-pass/aligned_utf8_check.stdout diff --git a/tests/run-pass/aligned_utf8_check.rs b/tests/run-pass/aligned_utf8_check.rs new file mode 100644 index 00000000000..0d2b2bf66a9 --- /dev/null +++ b/tests/run-pass/aligned_utf8_check.rs @@ -0,0 +1,13 @@ +fn main() { + const N: usize = 10; + + let x = vec![0x4141u16; N]; + + let mut y: Vec = unsafe { std::mem::transmute(x) }; + unsafe { y.set_len(2 * N) }; + + println!("{:?}", String::from_utf8_lossy(&y)); + + let mut x: Vec = unsafe { std::mem::transmute(y) }; + unsafe { x.set_len(N) }; +} diff --git a/tests/run-pass/aligned_utf8_check.stdout b/tests/run-pass/aligned_utf8_check.stdout new file mode 100644 index 00000000000..8d08312cac7 --- /dev/null +++ b/tests/run-pass/aligned_utf8_check.stdout @@ -0,0 +1 @@ +"AAAAAAAAAAAAAAAAAAAA" From fa20338c9aa4a64d984c61ae5f4332db89c78de1 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Thu, 12 Sep 2019 09:25:39 -0500 Subject: [PATCH 3/4] Use str::from_utf8 instead --- src/shims/mod.rs | 1 - tests/run-pass/aligned_utf8_check.rs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/shims/mod.rs b/src/shims/mod.rs index 5b67d075a5c..728634a7f6f 100644 --- a/src/shims/mod.rs +++ b/src/shims/mod.rs @@ -1,4 +1,3 @@ - pub mod foreign_items; pub mod intrinsics; pub mod tls; diff --git a/tests/run-pass/aligned_utf8_check.rs b/tests/run-pass/aligned_utf8_check.rs index 0d2b2bf66a9..202856b3bde 100644 --- a/tests/run-pass/aligned_utf8_check.rs +++ b/tests/run-pass/aligned_utf8_check.rs @@ -6,7 +6,7 @@ fn main() { let mut y: Vec = unsafe { std::mem::transmute(x) }; unsafe { y.set_len(2 * N) }; - println!("{:?}", String::from_utf8_lossy(&y)); + println!("{:?}", std::str::from_utf8(&y).unwrap()); let mut x: Vec = unsafe { std::mem::transmute(y) }; unsafe { x.set_len(N) }; From 55863cb88effdaa2198a22149cfae99c2377dd76 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 16 Sep 2019 10:16:06 -0500 Subject: [PATCH 4/4] Use force_ptr instead of assert_ptr --- src/shims/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shims/mod.rs b/src/shims/mod.rs index 728634a7f6f..a0da364ff0b 100644 --- a/src/shims/mod.rs +++ b/src/shims/mod.rs @@ -29,7 +29,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx if this.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) { let n = { - let ptr = this.read_scalar(args[0])?.not_undef()?.assert_ptr(); + 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()