1use std::cell::Cell;
4use std::{fmt, mem};
5
6use either::{Either, Left, Right};
7use rustc_hir as hir;
8use rustc_hir::definitions::DefPathData;
9use rustc_index::IndexVec;
10use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
11use rustc_middle::ty::{self, Ty, TyCtxt};
12use rustc_middle::{bug, mir};
13use rustc_mir_dataflow::impls::always_storage_live_locals;
14use rustc_span::Span;
15use tracing::{info_span, instrument, trace};
16
17use super::{
18 AllocId, CtfeProvenance, Immediate, InterpCx, InterpResult, MPlaceTy, Machine, MemPlace,
19 MemPlaceMeta, MemoryKind, Operand, Pointer, Provenance, ReturnAction, Scalar,
20 from_known_layout, interp_ok, throw_ub, throw_unsup,
21};
22use crate::errors;
23
24struct SpanGuard(tracing::Span, std::marker::PhantomData<*const u8>);
27
28impl SpanGuard {
29 fn new() -> Self {
31 Self(tracing::Span::none(), std::marker::PhantomData)
32 }
33
34 fn enter(&mut self, span: tracing::Span) {
39 *self = Self(span, std::marker::PhantomData);
44 self.0.with_subscriber(|(id, dispatch)| {
45 dispatch.enter(id);
46 });
47 }
48}
49
50impl Drop for SpanGuard {
51 fn drop(&mut self) {
52 self.0.with_subscriber(|(id, dispatch)| {
53 dispatch.exit(id);
54 });
55 }
56}
57
58pub struct Frame<'tcx, Prov: Provenance = CtfeProvenance, Extra = ()> {
60 pub(super) body: &'tcx mir::Body<'tcx>,
65
66 pub(super) instance: ty::Instance<'tcx>,
68
69 pub extra: Extra,
71
72 return_to_block: StackPopCleanup,
77
78 pub return_place: MPlaceTy<'tcx, Prov>,
81
82 pub locals: IndexVec<mir::Local, LocalState<'tcx, Prov>>,
90
91 tracing_span: SpanGuard,
95
96 pub(super) loc: Either<mir::Location, Span>,
105}
106
107#[derive(Clone, Copy, Eq, PartialEq, Debug)] pub enum StackPopCleanup {
109 Goto { ret: Option<mir::BasicBlock>, unwind: mir::UnwindAction },
115 Root { cleanup: bool },
120}
121
122pub struct StackPopInfo<'tcx, Prov: Provenance> {
124 pub return_action: ReturnAction,
127
128 pub return_to_block: StackPopCleanup,
130
131 pub return_place: MPlaceTy<'tcx, Prov>,
133}
134
135#[derive(Clone)]
137pub struct LocalState<'tcx, Prov: Provenance = CtfeProvenance> {
138 value: LocalValue<Prov>,
139 layout: Cell<Option<TyAndLayout<'tcx>>>,
142}
143
144impl<Prov: Provenance> std::fmt::Debug for LocalState<'_, Prov> {
145 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
146 f.debug_struct("LocalState")
147 .field("value", &self.value)
148 .field("ty", &self.layout.get().map(|l| l.ty))
149 .finish()
150 }
151}
152
153#[derive(Copy, Clone, Debug)] pub(super) enum LocalValue<Prov: Provenance = CtfeProvenance> {
159 Dead,
161 Live(Operand<Prov>),
167}
168
169impl<'tcx, Prov: Provenance> LocalState<'tcx, Prov> {
170 pub fn make_live_uninit(&mut self) {
171 self.value = LocalValue::Live(Operand::Immediate(Immediate::Uninit));
172 }
173
174 pub fn as_mplace_or_imm(
178 &self,
179 ) -> Option<Either<(Pointer<Option<Prov>>, MemPlaceMeta<Prov>), Immediate<Prov>>> {
180 match self.value {
181 LocalValue::Dead => None,
182 LocalValue::Live(Operand::Indirect(mplace)) => Some(Left((mplace.ptr, mplace.meta))),
183 LocalValue::Live(Operand::Immediate(imm)) => Some(Right(imm)),
184 }
185 }
186
187 #[inline(always)]
189 pub(super) fn access(&self) -> InterpResult<'tcx, &Operand<Prov>> {
190 match &self.value {
191 LocalValue::Dead => throw_ub!(DeadLocal), LocalValue::Live(val) => interp_ok(val),
193 }
194 }
195
196 #[inline(always)]
199 pub(super) fn access_mut(&mut self) -> InterpResult<'tcx, &mut Operand<Prov>> {
200 match &mut self.value {
201 LocalValue::Dead => throw_ub!(DeadLocal), LocalValue::Live(val) => interp_ok(val),
203 }
204 }
205}
206
207#[derive(Clone, Debug)]
209pub struct FrameInfo<'tcx> {
210 pub instance: ty::Instance<'tcx>,
211 pub span: Span,
212}
213
214impl<'tcx> fmt::Display for FrameInfo<'tcx> {
216 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
217 ty::tls::with(|tcx| {
218 if tcx.def_key(self.instance.def_id()).disambiguated_data.data == DefPathData::Closure {
219 write!(f, "inside closure")
220 } else {
221 write!(f, "inside `{}`", self.instance)
225 }
226 })
227 }
228}
229
230impl<'tcx> FrameInfo<'tcx> {
231 pub fn as_note(&self, tcx: TyCtxt<'tcx>) -> errors::FrameNote {
232 let span = self.span;
233 if tcx.def_key(self.instance.def_id()).disambiguated_data.data == DefPathData::Closure {
234 errors::FrameNote {
235 where_: "closure",
236 span,
237 instance: String::new(),
238 times: 0,
239 has_label: false,
240 }
241 } else {
242 let instance = format!("{}", self.instance);
243 errors::FrameNote { where_: "instance", span, instance, times: 0, has_label: false }
247 }
248 }
249}
250
251impl<'tcx, Prov: Provenance> Frame<'tcx, Prov> {
252 pub fn with_extra<Extra>(self, extra: Extra) -> Frame<'tcx, Prov, Extra> {
253 Frame {
254 body: self.body,
255 instance: self.instance,
256 return_to_block: self.return_to_block,
257 return_place: self.return_place,
258 locals: self.locals,
259 loc: self.loc,
260 extra,
261 tracing_span: self.tracing_span,
262 }
263 }
264}
265
266impl<'tcx, Prov: Provenance, Extra> Frame<'tcx, Prov, Extra> {
267 pub fn current_loc(&self) -> Either<mir::Location, Span> {
275 self.loc
276 }
277
278 pub fn body(&self) -> &'tcx mir::Body<'tcx> {
279 self.body
280 }
281
282 pub fn instance(&self) -> ty::Instance<'tcx> {
283 self.instance
284 }
285
286 pub fn current_source_info(&self) -> Option<&mir::SourceInfo> {
288 self.loc.left().map(|loc| self.body.source_info(loc))
289 }
290
291 pub fn current_span(&self) -> Span {
292 match self.loc {
293 Left(loc) => self.body.source_info(loc).span,
294 Right(span) => span,
295 }
296 }
297
298 pub fn lint_root(&self, tcx: TyCtxt<'tcx>) -> Option<hir::HirId> {
299 self.current_source_info()
302 .and_then(|source_info| match &self.body.source_scopes[source_info.scope].local_data {
303 mir::ClearCrossCrate::Set(data) => Some(data.lint_root),
304 mir::ClearCrossCrate::Clear => None,
305 })
306 .or_else(|| {
307 let def_id = self.body.source.def_id().as_local();
308 def_id.map(|def_id| tcx.local_def_id_to_hir_id(def_id))
309 })
310 }
311
312 #[inline(always)]
315 pub(super) fn locals_addr(&self) -> usize {
316 self.locals.raw.as_ptr().addr()
317 }
318
319 #[must_use]
320 pub fn generate_stacktrace_from_stack(stack: &[Self]) -> Vec<FrameInfo<'tcx>> {
321 let mut frames = Vec::new();
322 for frame in stack.iter().rev() {
325 let span = match frame.loc {
326 Left(loc) => {
327 let mir::SourceInfo { mut span, scope } = *frame.body.source_info(loc);
329 let mut scope_data = &frame.body.source_scopes[scope];
330 while let Some((instance, call_span)) = scope_data.inlined {
331 frames.push(FrameInfo { span, instance });
332 span = call_span;
333 scope_data = &frame.body.source_scopes[scope_data.parent_scope.unwrap()];
334 }
335 span
336 }
337 Right(span) => span,
338 };
339 frames.push(FrameInfo { span, instance: frame.instance });
340 }
341 trace!("generate stacktrace: {:#?}", frames);
342 frames
343 }
344}
345
346impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
347 #[instrument(skip(self, body, return_place, return_to_block), level = "debug")]
352 pub(crate) fn push_stack_frame_raw(
353 &mut self,
354 instance: ty::Instance<'tcx>,
355 body: &'tcx mir::Body<'tcx>,
356 return_place: &MPlaceTy<'tcx, M::Provenance>,
357 return_to_block: StackPopCleanup,
358 ) -> InterpResult<'tcx> {
359 trace!("body: {:#?}", body);
360
361 debug_assert_eq!(
363 self.stack().is_empty(),
364 matches!(return_to_block, StackPopCleanup::Root { .. })
365 );
366
367 let dead_local = LocalState { value: LocalValue::Dead, layout: Cell::new(None) };
370 let locals = IndexVec::from_elem(dead_local, &body.local_decls);
371 let pre_frame = Frame {
372 body,
373 loc: Right(body.span), return_to_block,
375 return_place: return_place.clone(),
376 locals,
377 instance,
378 tracing_span: SpanGuard::new(),
379 extra: (),
380 };
381 let frame = M::init_frame(self, pre_frame)?;
382 self.stack_mut().push(frame);
383
384 for &const_ in body.required_consts() {
386 let c =
387 self.instantiate_from_current_frame_and_normalize_erasing_regions(const_.const_)?;
388 c.eval(*self.tcx, self.typing_env, const_.span).map_err(|err| {
389 err.emit_note(*self.tcx);
390 err
391 })?;
392 }
393
394 M::after_stack_push(self)?;
396 self.frame_mut().loc = Left(mir::Location::START);
397 let span = info_span!("frame", "{}", instance);
398 self.frame_mut().tracing_span.enter(span);
399
400 interp_ok(())
401 }
402
403 pub(super) fn pop_stack_frame_raw(
416 &mut self,
417 unwinding: bool,
418 ) -> InterpResult<'tcx, StackPopInfo<'tcx, M::Provenance>> {
419 let cleanup = self.cleanup_current_frame_locals()?;
420
421 let frame =
422 self.stack_mut().pop().expect("tried to pop a stack frame, but there were none");
423
424 let return_to_block = frame.return_to_block;
425 let return_place = frame.return_place.clone();
426
427 let return_action;
428 if cleanup {
429 return_action = M::after_stack_pop(self, frame, unwinding)?;
430 assert_ne!(return_action, ReturnAction::NoCleanup);
431 } else {
432 return_action = ReturnAction::NoCleanup;
433 };
434
435 interp_ok(StackPopInfo { return_action, return_to_block, return_place })
436 }
437
438 fn cleanup_current_frame_locals(&mut self) -> InterpResult<'tcx, bool> {
441 let return_to_block = self.frame().return_to_block;
445 let cleanup = match return_to_block {
446 StackPopCleanup::Goto { .. } => true,
447 StackPopCleanup::Root { cleanup, .. } => cleanup,
448 };
449
450 if cleanup {
451 let locals = mem::take(&mut self.frame_mut().locals);
453 for local in &locals {
454 self.deallocate_local(local.value)?;
455 }
456 }
457
458 interp_ok(cleanup)
459 }
460
461 pub(crate) fn storage_live_for_always_live_locals(&mut self) -> InterpResult<'tcx> {
464 self.storage_live(mir::RETURN_PLACE)?;
465
466 let body = self.body();
467 let always_live = always_storage_live_locals(body);
468 for local in body.vars_and_temps_iter() {
469 if always_live.contains(local) {
470 self.storage_live(local)?;
471 }
472 }
473 interp_ok(())
474 }
475
476 pub fn storage_live_dyn(
477 &mut self,
478 local: mir::Local,
479 meta: MemPlaceMeta<M::Provenance>,
480 ) -> InterpResult<'tcx> {
481 trace!("{:?} is now live", local);
482
483 fn is_very_trivially_sized(ty: Ty<'_>) -> bool {
485 match ty.kind() {
486 ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
487 | ty::Uint(_)
488 | ty::Int(_)
489 | ty::Bool
490 | ty::Float(_)
491 | ty::FnDef(..)
492 | ty::FnPtr(..)
493 | ty::RawPtr(..)
494 | ty::Char
495 | ty::Ref(..)
496 | ty::Coroutine(..)
497 | ty::CoroutineWitness(..)
498 | ty::Array(..)
499 | ty::Closure(..)
500 | ty::CoroutineClosure(..)
501 | ty::Never
502 | ty::Error(_)
503 | ty::Dynamic(_, _, ty::DynStar) => true,
504
505 ty::Str | ty::Slice(_) | ty::Dynamic(_, _, ty::Dyn) | ty::Foreign(..) => false,
506
507 ty::Tuple(tys) => tys.last().is_none_or(|ty| is_very_trivially_sized(*ty)),
508
509 ty::Pat(ty, ..) => is_very_trivially_sized(*ty),
510
511 ty::Adt(..) => false,
513
514 ty::UnsafeBinder(ty) => is_very_trivially_sized(ty.skip_binder()),
515
516 ty::Alias(..) | ty::Param(_) | ty::Placeholder(..) => false,
517
518 ty::Infer(ty::TyVar(_)) => false,
519
520 ty::Bound(..)
521 | ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
522 bug!("`is_very_trivially_sized` applied to unexpected type: {}", ty)
523 }
524 }
525 }
526
527 let unsized_ = if is_very_trivially_sized(self.body().local_decls[local].ty) {
530 None
531 } else {
532 let layout = self.layout_of_local(self.frame(), local, None)?;
534 if layout.is_sized() { None } else { Some(layout) }
535 };
536
537 let local_val = LocalValue::Live(if let Some(layout) = unsized_ {
538 if !meta.has_meta() {
539 throw_unsup!(UnsizedLocal);
540 }
541 let dest_place = self.allocate_dyn(layout, MemoryKind::Stack, meta)?;
543 Operand::Indirect(*dest_place.mplace())
544 } else {
545 assert!(!meta.has_meta()); M::after_local_write(self, local, true)?;
550 Operand::Immediate(Immediate::Uninit)
556 });
557
558 let old = mem::replace(&mut self.frame_mut().locals[local].value, local_val);
560 self.deallocate_local(old)?;
561 interp_ok(())
562 }
563
564 #[inline(always)]
566 pub fn storage_live(&mut self, local: mir::Local) -> InterpResult<'tcx> {
567 self.storage_live_dyn(local, MemPlaceMeta::None)
568 }
569
570 pub fn storage_dead(&mut self, local: mir::Local) -> InterpResult<'tcx> {
571 assert!(local != mir::RETURN_PLACE, "Cannot make return place dead");
572 trace!("{:?} is now dead", local);
573
574 let old = mem::replace(&mut self.frame_mut().locals[local].value, LocalValue::Dead);
576 self.deallocate_local(old)?;
577 interp_ok(())
578 }
579
580 fn deallocate_local(&mut self, local: LocalValue<M::Provenance>) -> InterpResult<'tcx> {
581 if let LocalValue::Live(Operand::Indirect(MemPlace { ptr, .. })) = local {
582 trace!(
585 "deallocating local {:?}: {:?}",
586 local,
587 self.dump_alloc(ptr.provenance.unwrap().get_alloc_id().unwrap())
589 );
590 self.deallocate_ptr(ptr, None, MemoryKind::Stack)?;
591 };
592 interp_ok(())
593 }
594
595 #[inline(always)]
598 pub fn layout_of_local(
599 &self,
600 frame: &Frame<'tcx, M::Provenance, M::FrameExtra>,
601 local: mir::Local,
602 layout: Option<TyAndLayout<'tcx>>,
603 ) -> InterpResult<'tcx, TyAndLayout<'tcx>> {
604 let state = &frame.locals[local];
605 if let Some(layout) = state.layout.get() {
606 return interp_ok(layout);
607 }
608
609 let layout = from_known_layout(self.tcx, self.typing_env, layout, || {
610 let local_ty = frame.body.local_decls[local].ty;
611 let local_ty =
612 self.instantiate_from_frame_and_normalize_erasing_regions(frame, local_ty)?;
613 self.layout_of(local_ty).into()
614 })?;
615
616 state.layout.set(Some(layout));
618 interp_ok(layout)
619 }
620}
621
622impl<'tcx, Prov: Provenance> LocalState<'tcx, Prov> {
623 pub(super) fn print(
624 &self,
625 allocs: &mut Vec<Option<AllocId>>,
626 fmt: &mut std::fmt::Formatter<'_>,
627 ) -> std::fmt::Result {
628 match self.value {
629 LocalValue::Dead => write!(fmt, " is dead")?,
630 LocalValue::Live(Operand::Immediate(Immediate::Uninit)) => {
631 write!(fmt, " is uninitialized")?
632 }
633 LocalValue::Live(Operand::Indirect(mplace)) => {
634 write!(
635 fmt,
636 " by {} ref {:?}:",
637 match mplace.meta {
638 MemPlaceMeta::Meta(meta) => format!(" meta({meta:?})"),
639 MemPlaceMeta::None => String::new(),
640 },
641 mplace.ptr,
642 )?;
643 allocs.extend(mplace.ptr.provenance.map(Provenance::get_alloc_id));
644 }
645 LocalValue::Live(Operand::Immediate(Immediate::Scalar(val))) => {
646 write!(fmt, " {val:?}")?;
647 if let Scalar::Ptr(ptr, _size) = val {
648 allocs.push(ptr.provenance.get_alloc_id());
649 }
650 }
651 LocalValue::Live(Operand::Immediate(Immediate::ScalarPair(val1, val2))) => {
652 write!(fmt, " ({val1:?}, {val2:?})")?;
653 if let Scalar::Ptr(ptr, _size) = val1 {
654 allocs.push(ptr.provenance.get_alloc_id());
655 }
656 if let Scalar::Ptr(ptr, _size) = val2 {
657 allocs.push(ptr.provenance.get_alloc_id());
658 }
659 }
660 }
661
662 Ok(())
663 }
664}