Skip to content

Miri: make size/align_of_val work for dangling raw ptrs #80491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions compiler/rustc_mir/src/interpret/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
}

sym::min_align_of_val | sym::size_of_val => {
let place = self.deref_operand(args[0])?;
// Avoid `deref_operand` -- this is not a deref, the ptr does not have to be
// dereferencable!
let place = self.ref_to_mplace(self.read_immediate(args[0])?)?;
let (size, align) = self
.size_and_align_of(place.meta, place.layout)?
.size_and_align_of_mplace(place)?
.ok_or_else(|| err_unsup_format!("`extern type` does not have known layout"))?;

let result = match intrinsic_name {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir/src/interpret/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
}
// Make sure this is dereferenceable and all.
let size_and_align = try_validation!(
self.ecx.size_and_align_of(place.meta, place.layout),
self.ecx.size_and_align_of_mplace(place),
self.path,
err_ub!(InvalidMeta(msg)) => { "invalid {} metadata: {}", kind, msg },
);
Expand Down
6 changes: 4 additions & 2 deletions library/core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ pub const fn size_of_val<T: ?Sized>(val: &T) -> usize {
/// ```
#[inline]
#[unstable(feature = "layout_for_ptr", issue = "69835")]
pub unsafe fn size_of_val_raw<T: ?Sized>(val: *const T) -> usize {
#[rustc_const_unstable(feature = "const_size_of_val_raw", issue = "46571")]
pub const unsafe fn size_of_val_raw<T: ?Sized>(val: *const T) -> usize {
intrinsics::size_of_val(val)
}

Expand Down Expand Up @@ -505,7 +506,8 @@ pub const fn align_of_val<T: ?Sized>(val: &T) -> usize {
/// ```
#[inline]
#[unstable(feature = "layout_for_ptr", issue = "69835")]
pub unsafe fn align_of_val_raw<T: ?Sized>(val: *const T) -> usize {
#[rustc_const_unstable(feature = "const_align_of_val_raw", issue = "46571")]
pub const unsafe fn align_of_val_raw<T: ?Sized>(val: *const T) -> usize {
intrinsics::min_align_of_val(val)
}

Expand Down
7 changes: 7 additions & 0 deletions src/test/ui/consts/const-size_of_val-align_of_val.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// run-pass

#![feature(const_size_of_val, const_align_of_val)]
#![feature(const_size_of_val_raw, const_align_of_val_raw, layout_for_ptr)]

use std::mem;

Expand Down Expand Up @@ -32,6 +33,9 @@ const ALIGN_OF_UGH: usize = mem::align_of_val(&UGH);

const SIZE_OF_SLICE: usize = mem::size_of_val("foobar".as_bytes());

const SIZE_OF_DANGLING: usize = unsafe { mem::size_of_val_raw(0x100 as *const i32) };
const ALIGN_OF_DANGLING: usize = unsafe { mem::align_of_val_raw(0x100 as *const i16) };

fn main() {
assert_eq!(SIZE_OF_FOO, mem::size_of::<Foo>());
assert_eq!(SIZE_OF_BAR, mem::size_of::<Bar>());
Expand All @@ -41,5 +45,8 @@ fn main() {
assert_eq!(ALIGN_OF_BAR, mem::align_of::<Bar>());
assert_eq!(ALIGN_OF_UGH, mem::align_of::<Ugh>());

assert_eq!(SIZE_OF_DANGLING, mem::size_of::<i32>());
assert_eq!(ALIGN_OF_DANGLING, mem::align_of::<i16>());

assert_eq!(SIZE_OF_SLICE, "foobar".len());
}