Skip to content

Commit aefaa5b

Browse files
committed
Auto merge of rust-lang#2278 - RalfJung:rustup, r=RalfJung
Rustup
2 parents 7fafbde + ea30b6b commit aefaa5b

File tree

6 files changed

+21
-35
lines changed

6 files changed

+21
-35
lines changed

rust-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8aab472d52ba7314dc193c73abcd384e2586123c
1+
7f08d04d60d03e1a52dae61ce6aa50996898702b

src/intptrcast.rs

+8-19
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,17 @@ impl<'mir, 'tcx> GlobalStateInner {
7676
// This never overflows because `addr >= glb`
7777
let offset = addr - glb;
7878
// If the offset exceeds the size of the allocation, don't use this `alloc_id`.
79-
80-
if offset
81-
<= ecx
82-
.get_alloc_size_and_align(alloc_id, AllocCheck::MaybeDead)
83-
.unwrap()
84-
.0
85-
.bytes()
86-
{
87-
Some(alloc_id)
88-
} else {
89-
None
90-
}
79+
let size = ecx.get_alloc_info(alloc_id).0;
80+
if offset <= size.bytes() { Some(alloc_id) } else { None }
9181
}
9282
}?;
9383

9484
// We only use this provenance if it has been exposed, *and* is still live.
9585
if global_state.exposed.contains(&alloc_id) {
96-
// FIXME: this catches `InterpError`, which we should not usually do.
97-
// We might need a proper fallible API from `memory.rs` to avoid this though.
98-
if ecx.get_alloc_size_and_align(alloc_id, AllocCheck::Live).is_ok() {
99-
return Some(alloc_id);
86+
let (_size, _align, kind) = ecx.get_alloc_info(alloc_id);
87+
match kind {
88+
AllocKind::LiveData | AllocKind::Function => return Some(alloc_id),
89+
AllocKind::Dead => {}
10090
}
10191
}
10292

@@ -174,9 +164,8 @@ impl<'mir, 'tcx> GlobalStateInner {
174164
Entry::Occupied(entry) => *entry.get(),
175165
Entry::Vacant(entry) => {
176166
// There is nothing wrong with a raw pointer being cast to an integer only after
177-
// it became dangling. Hence `MaybeDead`.
178-
let (size, align) =
179-
ecx.get_alloc_size_and_align(alloc_id, AllocCheck::MaybeDead).unwrap();
167+
// it became dangling. Hence we allow dead allocations.
168+
let (size, align, _kind) = ecx.get_alloc_info(alloc_id);
180169

181170
// This allocation does not have a base address yet, pick one.
182171
// Leave some space to the previous allocation, to give it some chance to be less aligned.

src/shims/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
8787
let ptr = this.read_pointer(ptr_op)?;
8888
if let Ok((alloc_id, _offset, _)) = this.ptr_try_get_alloc_id(ptr) {
8989
// Only do anything if we can identify the allocation this goes to.
90-
let (_, cur_align) = this.get_alloc_size_and_align(alloc_id, AllocCheck::MaybeDead)?;
90+
let (_size, cur_align, _kind) = this.get_alloc_info(alloc_id);
9191
if cur_align.bytes() >= req_align {
9292
// If the allocation alignment is at least the required alignment we use the
9393
// real implementation.

src/stacked_borrows.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -849,8 +849,7 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
849849
log_creation(this, current_span, alloc_id, base_offset, orig_tag)?;
850850

851851
// Ensure we bail out if the pointer goes out-of-bounds (see miri#1050).
852-
let (alloc_size, _) =
853-
this.get_alloc_size_and_align(alloc_id, AllocCheck::Dereferenceable)?;
852+
let (alloc_size, _) = this.get_live_alloc_size_and_align(alloc_id)?;
854853
if base_offset + size > alloc_size {
855854
throw_ub!(PointerOutOfBounds {
856855
alloc_id,
@@ -1088,18 +1087,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
10881087
// Function pointers and dead objects don't have an alloc_extra so we ignore them.
10891088
// This is okay because accessing them is UB anyway, no need for any Stacked Borrows checks.
10901089
// NOT using `get_alloc_extra_mut` since this might be a read-only allocation!
1091-
// FIXME: this catches `InterpError`, which we should not usually do.
1092-
// We might need a proper fallible API from `memory.rs` to avoid this though.
1093-
match this.get_alloc_extra(alloc_id) {
1094-
Ok(alloc_extra) => {
1090+
let (_size, _align, kind) = this.get_alloc_info(alloc_id);
1091+
match kind {
1092+
AllocKind::LiveData => {
1093+
// This should have alloc_extra data.
1094+
let alloc_extra = this.get_alloc_extra(alloc_id).unwrap();
10951095
trace!("Stacked Borrows tag {tag:?} exposed in {alloc_id}");
10961096
alloc_extra.stacked_borrows.as_ref().unwrap().borrow_mut().exposed_tags.insert(tag);
10971097
}
1098-
Err(err) => {
1099-
trace!(
1100-
"Not exposing Stacked Borrows tag {tag:?} due to error \
1101-
when accessing {alloc_id}: {err}"
1102-
);
1098+
AllocKind::Function | AllocKind::Dead => {
1099+
// No stacked borrows on these allocations.
11031100
}
11041101
}
11051102
}

tests/fail/function_pointers/deref_fn_ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ fn f() {}
22

33
fn main() {
44
let x: u8 = unsafe {
5-
*std::mem::transmute::<fn(), *const u8>(f) //~ ERROR contains a function
5+
*std::mem::transmute::<fn(), *const u8>(f) //~ ERROR out-of-bounds
66
};
77
panic!("this should never print: {}", x);
88
}

tests/fail/function_pointers/deref_fn_ptr.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: accessing ALLOC which contains a function
1+
error: Undefined Behavior: dereferencing pointer failed: ALLOC has size 0, so pointer to 1 byte starting at offset 0 is out-of-bounds
22
--> $DIR/deref_fn_ptr.rs:LL:CC
33
|
44
LL | *std::mem::transmute::<fn(), *const u8>(f)
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ accessing ALLOC which contains a function
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ dereferencing pointer failed: ALLOC has size 0, so pointer to 1 byte starting at offset 0 is out-of-bounds
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

0 commit comments

Comments
 (0)