Skip to content
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

Stacked Borrows: alignment does not matter #1348

Merged
merged 2 commits into from
Apr 22, 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
2 changes: 1 addition & 1 deletion rust-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9b2f8dbba39dd4167f22a7026674a585c3d907d8
b2e36e6c2d229126b59e892c9147fbb68115d292
1 change: 1 addition & 0 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
start_id,
tcx.mk_substs(::std::iter::once(ty::subst::GenericArg::from(main_ret_ty))),
)
.unwrap()
.unwrap();

// First argument: pointer to `main()`.
Expand Down
10 changes: 6 additions & 4 deletions src/stacked_borrows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use log::trace;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_middle::mir::RetagKind;
use rustc_middle::ty;
use rustc_target::abi::{LayoutOf, Size};
use rustc_target::abi::{Align, LayoutOf, Size};
use rustc_hir::Mutability;

use crate::*;
Expand Down Expand Up @@ -577,11 +577,13 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
.size_and_align_of_mplace(place)?
.map(|(size, _)| size)
.unwrap_or_else(|| place.layout.size);
// `reborrow` relies on getting a `Pointer` and everything being in-bounds,
// so let's ensure that. However, we do not care about alignment.
// We can see dangling ptrs in here e.g. after a Box's `Unique` was
// updated using "self.0 = ..." (can happen in Box::from_raw); see miri#1050.
let place = this.mplace_access_checked(place)?;
// updated using "self.0 = ..." (can happen in Box::from_raw) so we cannot ICE; see miri#1050.
let place = this.mplace_access_checked(place, Some(Align::from_bytes(1).unwrap()))?;
Copy link
Contributor

@oli-obk oli-obk Apr 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could also just mutate the layout here before passing the place to mplace_access_checked instead of having to change the API.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean, change the value of place.layout.align? That feels even more hacky than the API extension...
Also that could trigger the assertion in mplace_access_checked.

// Nothing to do for ZSTs.
if size == Size::ZERO {
// Nothing to do for ZSTs.
return Ok(val);
}

Expand Down
8 changes: 7 additions & 1 deletion tests/run-pass/packed_struct.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#![feature(unsize, coerce_unsized)]
#![feature(unsize, coerce_unsized, raw_ref_op)]

use std::collections::hash_map::DefaultHasher;
use std::hash::Hash;

fn test_basic() {
#[repr(packed)]
struct S {
fill: u8,
a: i32,
b: i64,
}
Expand All @@ -30,16 +31,21 @@ fn test_basic() {
}

let mut x = S {
fill: 0,
a: 42,
b: 99,
};
let a = x.a;
let b = x.b;
assert_eq!(a, 42);
assert_eq!(b, 99);
assert_eq!(&x.fill, &0); // `fill` just requirs 1-byte-align, so this is fine
// can't do `assert_eq!(x.a, 42)`, because `assert_eq!` takes a reference
assert_eq!({x.a}, 42);
assert_eq!({x.b}, 99);
// but we *can* take a raw pointer!
assert_eq!(unsafe { (&raw const x.a).read_unaligned() }, 42);
assert_eq!(unsafe { (&raw const x.b).read_unaligned() }, 99);

x.b = 77;
assert_eq!({x.b}, 77);
Expand Down