Skip to content

Commit 52f8aec

Browse files
committed
Auto merge of rust-lang#121985 - RalfJung:interpret-return-place, r=oli-obk
interpret: avoid a long-lived PlaceTy in stack frames `PlaceTy` uses a representation that's not very stable under changes to the stack. I'd feel better if we didn't have one in the long-term machine state. r? `@oli-obk`
2 parents 8c9a75b + 3f0b6a0 commit 52f8aec

32 files changed

+140
-145
lines changed

Diff for: compiler/rustc_const_eval/src/const_eval/machine.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::errors::{LongRunning, LongRunningWarn};
2424
use crate::fluent_generated as fluent;
2525
use crate::interpret::{
2626
self, compile_time_machine, AllocId, AllocRange, ConstAllocation, CtfeProvenance, FnArg, FnVal,
27-
Frame, ImmTy, InterpCx, InterpResult, OpTy, PlaceTy, Pointer, PointerArithmetic, Scalar,
27+
Frame, ImmTy, InterpCx, InterpResult, MPlaceTy, OpTy, Pointer, PointerArithmetic, Scalar,
2828
};
2929

3030
use super::error::*;
@@ -219,7 +219,7 @@ impl<'mir, 'tcx: 'mir> CompileTimeEvalContext<'mir, 'tcx> {
219219
&mut self,
220220
instance: ty::Instance<'tcx>,
221221
args: &[FnArg<'tcx>],
222-
dest: &PlaceTy<'tcx>,
222+
dest: &MPlaceTy<'tcx>,
223223
ret: Option<mir::BasicBlock>,
224224
) -> InterpResult<'tcx, Option<ty::Instance<'tcx>>> {
225225
let def_id = instance.def_id();
@@ -280,7 +280,7 @@ impl<'mir, 'tcx: 'mir> CompileTimeEvalContext<'mir, 'tcx> {
280280
&mut self,
281281
instance: ty::Instance<'tcx>,
282282
args: &[OpTy<'tcx>],
283-
dest: &PlaceTy<'tcx>,
283+
dest: &MPlaceTy<'tcx>,
284284
ret: Option<mir::BasicBlock>,
285285
) -> InterpResult<'tcx, ControlFlow<()>> {
286286
assert_eq!(args.len(), 2);
@@ -410,7 +410,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
410410
orig_instance: ty::Instance<'tcx>,
411411
_abi: CallAbi,
412412
args: &[FnArg<'tcx>],
413-
dest: &PlaceTy<'tcx>,
413+
dest: &MPlaceTy<'tcx>,
414414
ret: Option<mir::BasicBlock>,
415415
_unwind: mir::UnwindAction, // unwinding is not supported in consts
416416
) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>> {
@@ -455,7 +455,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
455455
ecx: &mut InterpCx<'mir, 'tcx, Self>,
456456
instance: ty::Instance<'tcx>,
457457
args: &[OpTy<'tcx>],
458-
dest: &PlaceTy<'tcx, Self::Provenance>,
458+
dest: &MPlaceTy<'tcx, Self::Provenance>,
459459
target: Option<mir::BasicBlock>,
460460
_unwind: mir::UnwindAction,
461461
) -> InterpResult<'tcx> {

Diff for: compiler/rustc_const_eval/src/interpret/eval_context.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub struct Frame<'mir, 'tcx, Prov: Provenance = CtfeProvenance, Extra = ()> {
108108

109109
/// The location where the result of the current stack frame should be written to,
110110
/// and its layout in the caller.
111-
pub return_place: PlaceTy<'tcx, Prov>,
111+
pub return_place: MPlaceTy<'tcx, Prov>,
112112

113113
/// The list of locals for this stack frame, stored in order as
114114
/// `[return_ptr, arguments..., variables..., temporaries...]`.
@@ -771,7 +771,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
771771
&mut self,
772772
instance: ty::Instance<'tcx>,
773773
body: &'mir mir::Body<'tcx>,
774-
return_place: &PlaceTy<'tcx, M::Provenance>,
774+
return_place: &MPlaceTy<'tcx, M::Provenance>,
775775
return_to_block: StackPopCleanup,
776776
) -> InterpResult<'tcx> {
777777
trace!("body: {:#?}", body);
@@ -912,7 +912,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
912912
} else {
913913
self.copy_op_allow_transmute(&op, &dest)
914914
};
915-
trace!("return value: {:?}", self.dump_place(&dest));
915+
trace!("return value: {:?}", self.dump_place(&dest.into()));
916916
// We delay actually short-circuiting on this error until *after* the stack frame is
917917
// popped, since we want this error to be attributed to the caller, whose type defines
918918
// this transmute.

Diff for: compiler/rustc_const_eval/src/interpret/intrinsics.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_span::symbol::{sym, Symbol};
2121
use rustc_target::abi::Size;
2222

2323
use super::{
24-
util::ensure_monomorphic_enough, CheckInAllocMsg, ImmTy, InterpCx, Machine, OpTy, PlaceTy,
24+
util::ensure_monomorphic_enough, CheckInAllocMsg, ImmTy, InterpCx, MPlaceTy, Machine, OpTy,
2525
Pointer,
2626
};
2727

@@ -104,7 +104,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
104104
&mut self,
105105
instance: ty::Instance<'tcx>,
106106
args: &[OpTy<'tcx, M::Provenance>],
107-
dest: &PlaceTy<'tcx, M::Provenance>,
107+
dest: &MPlaceTy<'tcx, M::Provenance>,
108108
ret: Option<mir::BasicBlock>,
109109
) -> InterpResult<'tcx, bool> {
110110
let instance_args = instance.args;
@@ -377,7 +377,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
377377
let index = u64::from(self.read_scalar(&args[1])?.to_u32()?);
378378
let elem = &args[2];
379379
let (input, input_len) = self.operand_to_simd(&args[0])?;
380-
let (dest, dest_len) = self.place_to_simd(dest)?;
380+
let (dest, dest_len) = self.mplace_to_simd(dest)?;
381381
assert_eq!(input_len, dest_len, "Return vector length must match input length");
382382
// Bounds are not checked by typeck so we have to do it ourselves.
383383
if index >= input_len {
@@ -430,7 +430,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
430430
_ => return Ok(false),
431431
}
432432

433-
trace!("{:?}", self.dump_place(dest));
433+
trace!("{:?}", self.dump_place(&dest.clone().into()));
434434
self.go_to_block(ret);
435435
Ok(true)
436436
}
@@ -488,7 +488,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
488488
&mut self,
489489
a: &ImmTy<'tcx, M::Provenance>,
490490
b: &ImmTy<'tcx, M::Provenance>,
491-
dest: &PlaceTy<'tcx, M::Provenance>,
491+
dest: &MPlaceTy<'tcx, M::Provenance>,
492492
) -> InterpResult<'tcx> {
493493
assert_eq!(a.layout.ty, b.layout.ty);
494494
assert!(matches!(a.layout.ty.kind(), ty::Int(..) | ty::Uint(..)));
@@ -506,7 +506,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
506506
)
507507
}
508508
// `Rem` says this is all right, so we can let `Div` do its job.
509-
self.binop_ignore_overflow(BinOp::Div, a, b, dest)
509+
self.binop_ignore_overflow(BinOp::Div, a, b, &dest.clone().into())
510510
}
511511

512512
pub fn saturating_arith(

Diff for: compiler/rustc_const_eval/src/interpret/machine.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ pub trait Machine<'mir, 'tcx: 'mir>: Sized {
196196
instance: ty::Instance<'tcx>,
197197
abi: CallAbi,
198198
args: &[FnArg<'tcx, Self::Provenance>],
199-
destination: &PlaceTy<'tcx, Self::Provenance>,
199+
destination: &MPlaceTy<'tcx, Self::Provenance>,
200200
target: Option<mir::BasicBlock>,
201201
unwind: mir::UnwindAction,
202202
) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>>;
@@ -208,7 +208,7 @@ pub trait Machine<'mir, 'tcx: 'mir>: Sized {
208208
fn_val: Self::ExtraFnVal,
209209
abi: CallAbi,
210210
args: &[FnArg<'tcx, Self::Provenance>],
211-
destination: &PlaceTy<'tcx, Self::Provenance>,
211+
destination: &MPlaceTy<'tcx, Self::Provenance>,
212212
target: Option<mir::BasicBlock>,
213213
unwind: mir::UnwindAction,
214214
) -> InterpResult<'tcx>;
@@ -219,7 +219,7 @@ pub trait Machine<'mir, 'tcx: 'mir>: Sized {
219219
ecx: &mut InterpCx<'mir, 'tcx, Self>,
220220
instance: ty::Instance<'tcx>,
221221
args: &[OpTy<'tcx, Self::Provenance>],
222-
destination: &PlaceTy<'tcx, Self::Provenance>,
222+
destination: &MPlaceTy<'tcx, Self::Provenance>,
223223
target: Option<mir::BasicBlock>,
224224
unwind: mir::UnwindAction,
225225
) -> InterpResult<'tcx>;
@@ -584,7 +584,7 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
584584
fn_val: !,
585585
_abi: CallAbi,
586586
_args: &[FnArg<$tcx>],
587-
_destination: &PlaceTy<$tcx, Self::Provenance>,
587+
_destination: &MPlaceTy<$tcx, Self::Provenance>,
588588
_target: Option<mir::BasicBlock>,
589589
_unwind: mir::UnwindAction,
590590
) -> InterpResult<$tcx> {

Diff for: compiler/rustc_const_eval/src/interpret/place.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ pub(super) enum Place<Prov: Provenance = CtfeProvenance> {
194194
Local { frame: usize, local: mir::Local, offset: Option<Size> },
195195
}
196196

197+
/// An evaluated place, together with its type.
198+
///
199+
/// This may reference a stack frame by its index, so `PlaceTy` should generally not be kept around
200+
/// for longer than a single operation. Popping and then pushing a stack frame can make `PlaceTy`
201+
/// point to the wrong destination. If the interpreter has multiple stacks, stack switching will
202+
/// also invalidate a `PlaceTy`.
197203
#[derive(Clone)]
198204
pub struct PlaceTy<'tcx, Prov: Provenance = CtfeProvenance> {
199205
place: Place<Prov>, // Keep this private; it helps enforce invariants.
@@ -495,16 +501,6 @@ where
495501
Ok((mplace, len))
496502
}
497503

498-
/// Converts a repr(simd) place into a place where `place_index` accesses the SIMD elements.
499-
/// Also returns the number of elements.
500-
pub fn place_to_simd(
501-
&mut self,
502-
place: &PlaceTy<'tcx, M::Provenance>,
503-
) -> InterpResult<'tcx, (MPlaceTy<'tcx, M::Provenance>, u64)> {
504-
let mplace = self.force_allocation(place)?;
505-
self.mplace_to_simd(&mplace)
506-
}
507-
508504
pub fn local_to_place(
509505
&self,
510506
frame: usize,

Diff for: compiler/rustc_const_eval/src/interpret/terminator.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
153153
),
154154
};
155155

156-
let destination = self.eval_place(destination)?;
156+
let destination = self.force_allocation(&self.eval_place(destination)?)?;
157157
self.eval_fn_call(
158158
fn_val,
159159
(fn_sig.abi, fn_abi),
@@ -497,7 +497,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
497497
(caller_abi, caller_fn_abi): (Abi, &FnAbi<'tcx, Ty<'tcx>>),
498498
args: &[FnArg<'tcx, M::Provenance>],
499499
with_caller_location: bool,
500-
destination: &PlaceTy<'tcx, M::Provenance>,
500+
destination: &MPlaceTy<'tcx, M::Provenance>,
501501
target: Option<mir::BasicBlock>,
502502
mut unwind: mir::UnwindAction,
503503
) -> InterpResult<'tcx> {
@@ -726,7 +726,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
726726
});
727727
}
728728
// Protect return place for in-place return value passing.
729-
M::protect_in_place_function_argument(self, destination)?;
729+
M::protect_in_place_function_argument(self, &destination.clone().into())?;
730730

731731
// Don't forget to mark "initially live" locals as live.
732732
self.storage_live_for_always_live_locals()?;

Diff for: compiler/rustc_mir_transform/src/dataflow_const_prop.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ impl<'mir, 'tcx: 'mir> rustc_const_eval::interpret::Machine<'mir, 'tcx> for Dumm
929929
_instance: ty::Instance<'tcx>,
930930
_abi: rustc_target::spec::abi::Abi,
931931
_args: &[rustc_const_eval::interpret::FnArg<'tcx, Self::Provenance>],
932-
_destination: &rustc_const_eval::interpret::PlaceTy<'tcx, Self::Provenance>,
932+
_destination: &rustc_const_eval::interpret::MPlaceTy<'tcx, Self::Provenance>,
933933
_target: Option<BasicBlock>,
934934
_unwind: UnwindAction,
935935
) -> interpret::InterpResult<'tcx, Option<(&'mir Body<'tcx>, ty::Instance<'tcx>)>> {
@@ -947,7 +947,7 @@ impl<'mir, 'tcx: 'mir> rustc_const_eval::interpret::Machine<'mir, 'tcx> for Dumm
947947
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
948948
_instance: ty::Instance<'tcx>,
949949
_args: &[rustc_const_eval::interpret::OpTy<'tcx, Self::Provenance>],
950-
_destination: &rustc_const_eval::interpret::PlaceTy<'tcx, Self::Provenance>,
950+
_destination: &rustc_const_eval::interpret::MPlaceTy<'tcx, Self::Provenance>,
951951
_target: Option<BasicBlock>,
952952
_unwind: UnwindAction,
953953
) -> interpret::InterpResult<'tcx> {

Diff for: src/tools/miri/src/helpers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
381381
f: ty::Instance<'tcx>,
382382
caller_abi: Abi,
383383
args: &[Immediate<Provenance>],
384-
dest: Option<&PlaceTy<'tcx, Provenance>>,
384+
dest: Option<&MPlaceTy<'tcx, Provenance>>,
385385
stack_pop: StackPopCleanup,
386386
) -> InterpResult<'tcx> {
387387
let this = self.eval_context_mut();

Diff for: src/tools/miri/src/machine.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
950950
instance: ty::Instance<'tcx>,
951951
abi: Abi,
952952
args: &[FnArg<'tcx, Provenance>],
953-
dest: &PlaceTy<'tcx, Provenance>,
953+
dest: &MPlaceTy<'tcx, Provenance>,
954954
ret: Option<mir::BasicBlock>,
955955
unwind: mir::UnwindAction,
956956
) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>> {
@@ -977,7 +977,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
977977
fn_val: DynSym,
978978
abi: Abi,
979979
args: &[FnArg<'tcx, Provenance>],
980-
dest: &PlaceTy<'tcx, Provenance>,
980+
dest: &MPlaceTy<'tcx, Provenance>,
981981
ret: Option<mir::BasicBlock>,
982982
unwind: mir::UnwindAction,
983983
) -> InterpResult<'tcx> {
@@ -990,7 +990,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
990990
ecx: &mut MiriInterpCx<'mir, 'tcx>,
991991
instance: ty::Instance<'tcx>,
992992
args: &[OpTy<'tcx, Provenance>],
993-
dest: &PlaceTy<'tcx, Provenance>,
993+
dest: &MPlaceTy<'tcx, Provenance>,
994994
ret: Option<mir::BasicBlock>,
995995
unwind: mir::UnwindAction,
996996
) -> InterpResult<'tcx> {

Diff for: src/tools/miri/src/shims/backtrace.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
1212
abi: Abi,
1313
link_name: Symbol,
1414
args: &[OpTy<'tcx, Provenance>],
15-
dest: &PlaceTy<'tcx, Provenance>,
15+
dest: &MPlaceTy<'tcx, Provenance>,
1616
) -> InterpResult<'tcx> {
1717
let this = self.eval_context_mut();
1818
let [flags] = this.check_shim(abi, Abi::Rust, link_name, args)?;
@@ -32,7 +32,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
3232
abi: Abi,
3333
link_name: Symbol,
3434
args: &[OpTy<'tcx, Provenance>],
35-
dest: &PlaceTy<'tcx, Provenance>,
35+
dest: &MPlaceTy<'tcx, Provenance>,
3636
) -> InterpResult<'tcx> {
3737
let this = self.eval_context_mut();
3838
let tcx = this.tcx;
@@ -145,7 +145,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
145145
abi: Abi,
146146
link_name: Symbol,
147147
args: &[OpTy<'tcx, Provenance>],
148-
dest: &PlaceTy<'tcx, Provenance>,
148+
dest: &MPlaceTy<'tcx, Provenance>,
149149
) -> InterpResult<'tcx> {
150150
let this = self.eval_context_mut();
151151
let [ptr, flags] = this.check_shim(abi, Abi::Rust, link_name, args)?;
@@ -174,7 +174,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
174174
// `lo.col` is 0-based - add 1 to make it 1-based for the caller.
175175
let colno: u32 = u32::try_from(lo.col.0.saturating_add(1)).unwrap_or(0);
176176

177-
let dest = this.force_allocation(dest)?;
178177
if let ty::Adt(adt, _) = dest.layout.ty.kind() {
179178
if !adt.repr().c() {
180179
throw_ub_format!(
@@ -191,29 +190,29 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
191190
let filename_alloc =
192191
this.allocate_str(&filename, MiriMemoryKind::Rust.into(), Mutability::Mut)?;
193192

194-
this.write_immediate(name_alloc.to_ref(this), &this.project_field(&dest, 0)?)?;
195-
this.write_immediate(filename_alloc.to_ref(this), &this.project_field(&dest, 1)?)?;
193+
this.write_immediate(name_alloc.to_ref(this), &this.project_field(dest, 0)?)?;
194+
this.write_immediate(filename_alloc.to_ref(this), &this.project_field(dest, 1)?)?;
196195
}
197196
1 => {
198197
this.write_scalar(
199198
Scalar::from_target_usize(name.len().try_into().unwrap(), this),
200-
&this.project_field(&dest, 0)?,
199+
&this.project_field(dest, 0)?,
201200
)?;
202201
this.write_scalar(
203202
Scalar::from_target_usize(filename.len().try_into().unwrap(), this),
204-
&this.project_field(&dest, 1)?,
203+
&this.project_field(dest, 1)?,
205204
)?;
206205
}
207206
_ => throw_unsup_format!("unknown `miri_resolve_frame` flags {}", flags),
208207
}
209208

210-
this.write_scalar(Scalar::from_u32(lineno), &this.project_field(&dest, 2)?)?;
211-
this.write_scalar(Scalar::from_u32(colno), &this.project_field(&dest, 3)?)?;
209+
this.write_scalar(Scalar::from_u32(lineno), &this.project_field(dest, 2)?)?;
210+
this.write_scalar(Scalar::from_u32(colno), &this.project_field(dest, 3)?)?;
212211

213212
// Support a 4-field struct for now - this is deprecated
214213
// and slated for removal.
215214
if num_fields == 5 {
216-
this.write_pointer(fn_ptr, &this.project_field(&dest, 4)?)?;
215+
this.write_pointer(fn_ptr, &this.project_field(dest, 4)?)?;
217216
}
218217

219218
Ok(())

Diff for: src/tools/miri/src/shims/ffi_support.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
7070
fn call_external_c_and_store_return<'a>(
7171
&mut self,
7272
link_name: Symbol,
73-
dest: &PlaceTy<'tcx, Provenance>,
73+
dest: &MPlaceTy<'tcx, Provenance>,
7474
ptr: CodePtr,
7575
libffi_args: Vec<libffi::high::Arg<'a>>,
7676
) -> InterpResult<'tcx, ()> {
@@ -205,7 +205,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
205205
fn call_external_c_fct(
206206
&mut self,
207207
link_name: Symbol,
208-
dest: &PlaceTy<'tcx, Provenance>,
208+
dest: &MPlaceTy<'tcx, Provenance>,
209209
args: &[OpTy<'tcx, Provenance>],
210210
) -> InterpResult<'tcx, bool> {
211211
// Get the pointer to the function in the shared object file if it exists.

0 commit comments

Comments
 (0)