Skip to content

Commit b473d95

Browse files
committed
Auto merge of #3007 - rust-lang:rustup-2023-08-03, r=oli-obk
Automatic sync from rustc
2 parents 606adf9 + 7e2a413 commit b473d95

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+185
-176
lines changed

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
90bb4184f89a24d26787a9eada781bf3c4dd3dc6
1+
d8bbef50bbad789e26219f4ec88b5d73b05570a3

src/borrow_tracker/stacked_borrows/mod.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_middle::ty::{
1818
layout::{HasParamEnv, LayoutOf},
1919
Ty,
2020
};
21-
use rustc_target::abi::{Abi, Size};
21+
use rustc_target::abi::{Abi, Align, Size};
2222

2323
use crate::borrow_tracker::{
2424
stacked_borrows::diagnostics::{AllocHistory, DiagnosticCx, DiagnosticCxBuilder},
@@ -619,6 +619,8 @@ trait EvalContextPrivExt<'mir: 'ecx, 'tcx: 'mir, 'ecx>: crate::MiriInterpCxExt<'
619619
retag_info: RetagInfo, // diagnostics info about this retag
620620
) -> InterpResult<'tcx, Option<AllocId>> {
621621
let this = self.eval_context_mut();
622+
// Ensure we bail out if the pointer goes out-of-bounds (see miri#1050).
623+
this.check_ptr_access_align(place.ptr, size, Align::ONE, CheckInAllocMsg::InboundsTest)?;
622624

623625
// It is crucial that this gets called on all code paths, to ensure we track tag creation.
624626
let log_creation = |this: &MiriInterpCx<'mir, 'tcx>,
@@ -707,18 +709,6 @@ trait EvalContextPrivExt<'mir: 'ecx, 'tcx: 'mir, 'ecx>: crate::MiriInterpCxExt<'
707709
let (alloc_id, base_offset, orig_tag) = this.ptr_get_alloc_id(place.ptr)?;
708710
log_creation(this, Some((alloc_id, base_offset, orig_tag)))?;
709711

710-
// Ensure we bail out if the pointer goes out-of-bounds (see miri#1050).
711-
let (alloc_size, _) = this.get_live_alloc_size_and_align(alloc_id)?;
712-
if base_offset + size > alloc_size {
713-
throw_ub!(PointerOutOfBounds {
714-
alloc_id,
715-
alloc_size,
716-
ptr_offset: this.target_usize_to_isize(base_offset.bytes()),
717-
ptr_size: size,
718-
msg: CheckInAllocMsg::InboundsTest
719-
});
720-
}
721-
722712
trace!(
723713
"reborrow: reference {:?} derived from {:?} (pointee {}): {:?}, size {}",
724714
new_tag,

src/borrow_tracker/tree_borrows/mod.rs

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use log::trace;
22

3-
use rustc_target::abi::{Abi, Size};
3+
use rustc_target::abi::{Abi, Align, Size};
44

55
use crate::borrow_tracker::{AccessKind, GlobalStateInner, ProtectorKind, RetagFields};
66
use rustc_middle::{
@@ -182,6 +182,13 @@ trait EvalContextPrivExt<'mir: 'ecx, 'tcx: 'mir, 'ecx>: crate::MiriInterpCxExt<'
182182
new_tag: BorTag,
183183
) -> InterpResult<'tcx, Option<(AllocId, BorTag)>> {
184184
let this = self.eval_context_mut();
185+
// Ensure we bail out if the pointer goes out-of-bounds (see miri#1050).
186+
this.check_ptr_access_align(
187+
place.ptr,
188+
ptr_size,
189+
Align::ONE,
190+
CheckInAllocMsg::InboundsTest,
191+
)?;
185192

186193
// It is crucial that this gets called on all code paths, to ensure we track tag creation.
187194
let log_creation = |this: &MiriInterpCx<'mir, 'tcx>,
@@ -202,51 +209,33 @@ trait EvalContextPrivExt<'mir: 'ecx, 'tcx: 'mir, 'ecx>: crate::MiriInterpCxExt<'
202209
};
203210

204211
trace!("Reborrow of size {:?}", ptr_size);
205-
let (alloc_id, base_offset, parent_prov) = if ptr_size > Size::ZERO {
206-
this.ptr_get_alloc_id(place.ptr)?
207-
} else {
208-
match this.ptr_try_get_alloc_id(place.ptr) {
209-
Ok(data) => data,
210-
Err(_) => {
211-
// This pointer doesn't come with an AllocId, so there's no
212-
// memory to do retagging in.
213-
trace!(
214-
"reborrow of size 0: reference {:?} derived from {:?} (pointee {})",
215-
new_tag,
216-
place.ptr,
217-
place.layout.ty,
218-
);
219-
log_creation(this, None)?;
220-
return Ok(None);
221-
}
212+
let (alloc_id, base_offset, parent_prov) = match this.ptr_try_get_alloc_id(place.ptr) {
213+
Ok(data) => {
214+
// Unlike SB, we *do* a proper retag for size 0 if can identify the allocation.
215+
// After all, the pointer may be lazily initialized outside this initial range.
216+
data
217+
}
218+
Err(_) => {
219+
assert_eq!(ptr_size, Size::ZERO); // we did the deref check above, size has to be 0 here
220+
// This pointer doesn't come with an AllocId, so there's no
221+
// memory to do retagging in.
222+
trace!(
223+
"reborrow of size 0: reference {:?} derived from {:?} (pointee {})",
224+
new_tag,
225+
place.ptr,
226+
place.layout.ty,
227+
);
228+
log_creation(this, None)?;
229+
return Ok(None);
222230
}
223231
};
232+
log_creation(this, Some((alloc_id, base_offset, parent_prov)))?;
233+
224234
let orig_tag = match parent_prov {
225235
ProvenanceExtra::Wildcard => return Ok(None), // TODO: handle wildcard pointers
226236
ProvenanceExtra::Concrete(tag) => tag,
227237
};
228238

229-
// Protection against trying to get a reference to a vtable:
230-
// vtables do not have an alloc_extra so the call to
231-
// `get_alloc_extra` that follows fails.
232-
let (alloc_size, _align, alloc_kind) = this.get_alloc_info(alloc_id);
233-
if ptr_size == Size::ZERO && !matches!(alloc_kind, AllocKind::LiveData) {
234-
return Ok(Some((alloc_id, orig_tag)));
235-
}
236-
237-
log_creation(this, Some((alloc_id, base_offset, parent_prov)))?;
238-
239-
// Ensure we bail out if the pointer goes out-of-bounds (see miri#1050).
240-
if base_offset + ptr_size > alloc_size {
241-
throw_ub!(PointerOutOfBounds {
242-
alloc_id,
243-
alloc_size,
244-
ptr_offset: this.target_usize_to_isize(base_offset.bytes()),
245-
ptr_size,
246-
msg: CheckInAllocMsg::InboundsTest
247-
});
248-
}
249-
250239
trace!(
251240
"reborrow: reference {:?} derived from {:?} (pointee {}): {:?}, size {}",
252241
new_tag,

src/concurrency/sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ pub(super) trait EvalContextExtPriv<'mir, 'tcx: 'mir>:
206206
) -> InterpResult<'tcx, Option<Id>> {
207207
let this = self.eval_context_mut();
208208
let value_place =
209-
this.deref_operand_and_offset(lock_op, offset, lock_layout, this.machine.layouts.u32)?;
209+
this.deref_pointer_and_offset(lock_op, offset, lock_layout, this.machine.layouts.u32)?;
210210

211211
// Since we are lazy, this update has to be atomic.
212212
let (old, success) = this

src/helpers.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -715,9 +715,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
715715
}
716716

717717
/// Dereference a pointer operand to a place using `layout` instead of the pointer's declared type
718-
fn deref_operand_as(
718+
fn deref_pointer_as(
719719
&self,
720-
op: &OpTy<'tcx, Provenance>,
720+
op: &impl Readable<'tcx, Provenance>,
721721
layout: TyAndLayout<'tcx>,
722722
) -> InterpResult<'tcx, MPlaceTy<'tcx, Provenance>> {
723723
let this = self.eval_context_ref();
@@ -746,15 +746,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
746746
}
747747

748748
/// Calculates the MPlaceTy given the offset and layout of an access on an operand
749-
fn deref_operand_and_offset(
749+
fn deref_pointer_and_offset(
750750
&self,
751-
op: &OpTy<'tcx, Provenance>,
751+
op: &impl Readable<'tcx, Provenance>,
752752
offset: u64,
753753
base_layout: TyAndLayout<'tcx>,
754754
value_layout: TyAndLayout<'tcx>,
755755
) -> InterpResult<'tcx, MPlaceTy<'tcx, Provenance>> {
756756
let this = self.eval_context_ref();
757-
let op_place = this.deref_operand_as(op, base_layout)?;
757+
let op_place = this.deref_pointer_as(op, base_layout)?;
758758
let offset = Size::from_bytes(offset);
759759

760760
// Ensure that the access is within bounds.
@@ -763,28 +763,28 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
763763
Ok(value_place)
764764
}
765765

766-
fn read_scalar_at_offset(
766+
fn deref_pointer_and_read(
767767
&self,
768-
op: &OpTy<'tcx, Provenance>,
768+
op: &impl Readable<'tcx, Provenance>,
769769
offset: u64,
770770
base_layout: TyAndLayout<'tcx>,
771771
value_layout: TyAndLayout<'tcx>,
772772
) -> InterpResult<'tcx, Scalar<Provenance>> {
773773
let this = self.eval_context_ref();
774-
let value_place = this.deref_operand_and_offset(op, offset, base_layout, value_layout)?;
774+
let value_place = this.deref_pointer_and_offset(op, offset, base_layout, value_layout)?;
775775
this.read_scalar(&value_place)
776776
}
777777

778-
fn write_scalar_at_offset(
778+
fn deref_pointer_and_write(
779779
&mut self,
780-
op: &OpTy<'tcx, Provenance>,
780+
op: &impl Readable<'tcx, Provenance>,
781781
offset: u64,
782782
value: impl Into<Scalar<Provenance>>,
783783
base_layout: TyAndLayout<'tcx>,
784784
value_layout: TyAndLayout<'tcx>,
785785
) -> InterpResult<'tcx, ()> {
786786
let this = self.eval_context_mut();
787-
let value_place = this.deref_operand_and_offset(op, offset, base_layout, value_layout)?;
787+
let value_place = this.deref_pointer_and_offset(op, offset, base_layout, value_layout)?;
788788
this.write_scalar(value, &value_place)
789789
}
790790

src/shims/backtrace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
9797
1 => {
9898
let [_flags, buf] = this.check_shim(abi, Abi::Rust, link_name, args)?;
9999

100-
let buf_place = this.deref_operand(buf)?;
100+
let buf_place = this.deref_pointer(buf)?;
101101

102102
let ptr_layout = this.layout_of(ptr_ty)?;
103103

src/shims/foreign_items.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
418418
// // First thing: load all the arguments. Details depend on the shim.
419419
// let arg1 = this.read_scalar(arg1)?.to_u32()?;
420420
// let arg2 = this.read_pointer(arg2)?; // when you need to work with the pointer directly
421-
// let arg3 = this.deref_operand_as(arg3, this.libc_ty_layout("some_libc_struct"))?; // when you want to load/store
421+
// let arg3 = this.deref_pointer_as(arg3, this.libc_ty_layout("some_libc_struct"))?; // when you want to load/store
422422
// // through the pointer and supply the type information yourself
423-
// let arg4 = this.deref_operand(arg4)?; // when you want to load/store through the pointer and trust
423+
// let arg4 = this.deref_pointer(arg4)?; // when you want to load/store through the pointer and trust
424424
// // the user-given type (which you shouldn't usually do)
425425
//
426426
// // ...

src/shims/intrinsics/atomic.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: MiriInterpCxExt<'mir, 'tcx> {
130130
let this = self.eval_context_mut();
131131

132132
let [place] = check_arg_count(args)?;
133-
let place = this.deref_operand(place)?;
133+
let place = this.deref_pointer(place)?;
134134

135135
// Perform atomic load.
136136
let val = this.read_scalar_atomic(&place, atomic)?;
@@ -147,7 +147,7 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: MiriInterpCxExt<'mir, 'tcx> {
147147
let this = self.eval_context_mut();
148148

149149
let [place, val] = check_arg_count(args)?;
150-
let place = this.deref_operand(place)?;
150+
let place = this.deref_pointer(place)?;
151151

152152
// Perform regular load.
153153
let val = this.read_scalar(val)?;
@@ -188,7 +188,7 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: MiriInterpCxExt<'mir, 'tcx> {
188188
let this = self.eval_context_mut();
189189

190190
let [place, rhs] = check_arg_count(args)?;
191-
let place = this.deref_operand(place)?;
191+
let place = this.deref_pointer(place)?;
192192
let rhs = this.read_immediate(rhs)?;
193193

194194
if !place.layout.ty.is_integral() && !place.layout.ty.is_unsafe_ptr() {
@@ -229,7 +229,7 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: MiriInterpCxExt<'mir, 'tcx> {
229229
let this = self.eval_context_mut();
230230

231231
let [place, new] = check_arg_count(args)?;
232-
let place = this.deref_operand(place)?;
232+
let place = this.deref_pointer(place)?;
233233
let new = this.read_scalar(new)?;
234234

235235
let old = this.atomic_exchange_scalar(&place, new, atomic)?;
@@ -248,7 +248,7 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: MiriInterpCxExt<'mir, 'tcx> {
248248
let this = self.eval_context_mut();
249249

250250
let [place, expect_old, new] = check_arg_count(args)?;
251-
let place = this.deref_operand(place)?;
251+
let place = this.deref_pointer(place)?;
252252
let expect_old = this.read_immediate(expect_old)?; // read as immediate for the sake of `binary_op()`
253253
let new = this.read_scalar(new)?;
254254

src/shims/intrinsics/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
9696
// Raw memory accesses
9797
"volatile_load" => {
9898
let [place] = check_arg_count(args)?;
99-
let place = this.deref_operand(place)?;
99+
let place = this.deref_pointer(place)?;
100100
this.copy_op(&place, dest, /*allow_transmute*/ false)?;
101101
}
102102
"volatile_store" => {
103103
let [place, dest] = check_arg_count(args)?;
104-
let place = this.deref_operand(place)?;
104+
let place = this.deref_pointer(place)?;
105105
this.copy_op(dest, &place, /*allow_transmute*/ false)?;
106106
}
107107

src/shims/intrinsics/simd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
534534
let dest = this.project_index(&dest, i)?;
535535

536536
let val = if simd_element_to_bool(mask)? {
537-
let place = this.deref_operand(&ptr)?;
537+
let place = this.deref_pointer(&ptr)?;
538538
this.read_immediate(&place)?
539539
} else {
540540
passthru
@@ -557,7 +557,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
557557
let mask = this.read_immediate(&this.project_index(&mask, i)?)?;
558558

559559
if simd_element_to_bool(mask)? {
560-
let place = this.deref_operand(&ptr)?;
560+
let place = this.deref_pointer(&ptr)?;
561561
this.write_immediate(*value, &place)?;
562562
}
563563
}

0 commit comments

Comments
 (0)