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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
90bb4184f89a24d26787a9eada781bf3c4dd3dc6
1+
d8bbef50bbad789e26219f4ec88b5d73b05570a3

src/borrow_tracker/stacked_borrows/mod.rs

+3-13
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

+28-39
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

+1-1
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

+11-11
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

+1-1
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

+2-2
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

+5-5
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

+2-2
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

+2-2
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
}

src/shims/time.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
2525
this.assert_target_os_is_unix("clock_gettime");
2626

2727
let clk_id = this.read_scalar(clk_id_op)?.to_i32()?;
28-
let tp = this.deref_operand_as(tp_op, this.libc_ty_layout("timespec"))?;
28+
let tp = this.deref_pointer_as(tp_op, this.libc_ty_layout("timespec"))?;
2929

3030
let absolute_clocks;
3131
let mut relative_clocks;
@@ -92,7 +92,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
9292
this.assert_target_os_is_unix("gettimeofday");
9393
this.check_no_isolation("`gettimeofday`")?;
9494

95-
let tv = this.deref_operand_as(tv_op, this.libc_ty_layout("timeval"))?;
95+
let tv = this.deref_pointer_as(tv_op, this.libc_ty_layout("timeval"))?;
9696

9797
// Using tz is obsolete and should always be null
9898
let tz = this.read_pointer(tz_op)?;
@@ -121,7 +121,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
121121
this.assert_target_os("windows", "GetSystemTimeAsFileTime");
122122
this.check_no_isolation("`GetSystemTimeAsFileTime`")?;
123123

124-
let filetime = this.deref_operand_as(LPFILETIME_op, this.windows_ty_layout("FILETIME"))?;
124+
let filetime = this.deref_pointer_as(LPFILETIME_op, this.windows_ty_layout("FILETIME"))?;
125125

126126
let NANOS_PER_SEC = this.eval_windows_u64("time", "NANOS_PER_SEC");
127127
let INTERVALS_PER_SEC = this.eval_windows_u64("time", "INTERVALS_PER_SEC");
@@ -156,7 +156,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
156156
let qpc = i64::try_from(duration.as_nanos()).map_err(|_| {
157157
err_unsup_format!("programs running longer than 2^63 nanoseconds are not supported")
158158
})?;
159-
this.write_scalar(Scalar::from_i64(qpc), &this.deref_operand(lpPerformanceCount_op)?)?;
159+
this.write_scalar(Scalar::from_i64(qpc), &this.deref_pointer(lpPerformanceCount_op)?)?;
160160
Ok(Scalar::from_i32(-1)) // return non-zero on success
161161
}
162162

@@ -176,7 +176,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
176176
// and thus 10^9 counts per second.
177177
this.write_scalar(
178178
Scalar::from_i64(1_000_000_000),
179-
&this.deref_operand_as(lpFrequency_op, this.machine.layouts.u64)?,
179+
&this.deref_pointer_as(lpFrequency_op, this.machine.layouts.u64)?,
180180
)?;
181181
Ok(Scalar::from_i32(-1)) // Return non-zero on success
182182
}
@@ -203,7 +203,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
203203

204204
this.assert_target_os("macos", "mach_timebase_info");
205205

206-
let info = this.deref_operand_as(info_op, this.libc_ty_layout("mach_timebase_info"))?;
206+
let info = this.deref_pointer_as(info_op, this.libc_ty_layout("mach_timebase_info"))?;
207207

208208
// Since our emulated ticks in `mach_absolute_time` *are* nanoseconds,
209209
// no scaling needs to happen.
@@ -222,7 +222,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
222222

223223
this.assert_target_os_is_unix("nanosleep");
224224

225-
let req = this.deref_operand_as(req_op, this.libc_ty_layout("timespec"))?;
225+
let req = this.deref_pointer_as(req_op, this.libc_ty_layout("timespec"))?;
226226

227227
let duration = match this.read_timespec(&req)? {
228228
Some(duration) => duration,

0 commit comments

Comments
 (0)