1use std::assert_matches::debug_assert_matches;
2
3use either::{Left, Right};
4use rustc_abi::{Align, HasDataLayout, Size, TargetDataLayout};
5use rustc_errors::DiagCtxtHandle;
6use rustc_hir::def_id::DefId;
7use rustc_hir::limit::Limit;
8use rustc_middle::mir::interpret::{ErrorHandled, InvalidMetaKind, ReportedErrorInfo};
9use rustc_middle::query::TyCtxtAt;
10use rustc_middle::ty::layout::{
11 self, FnAbiError, FnAbiOf, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOf,
12 LayoutOfHelpers, TyAndLayout,
13};
14use rustc_middle::ty::{
15 self, GenericArgsRef, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, TypingEnv, Variance,
16};
17use rustc_middle::{mir, span_bug};
18use rustc_span::Span;
19use rustc_target::callconv::FnAbi;
20use tracing::{debug, trace};
21
22use super::{
23 Frame, FrameInfo, GlobalId, InterpErrorInfo, InterpErrorKind, InterpResult, MPlaceTy, Machine,
24 MemPlaceMeta, Memory, OpTy, Place, PlaceTy, PointerArithmetic, Projectable, Provenance,
25 err_inval, interp_ok, throw_inval, throw_ub, throw_ub_custom,
26};
27use crate::{ReportErrorExt, enter_trace_span, fluent_generated as fluent, util};
28
29pub struct InterpCx<'tcx, M: Machine<'tcx>> {
30 pub machine: M,
34
35 pub tcx: TyCtxtAt<'tcx>,
39
40 pub(super) typing_env: ty::TypingEnv<'tcx>,
43
44 pub memory: Memory<'tcx, M>,
46
47 pub recursion_limit: Limit,
49}
50
51impl<'tcx, M: Machine<'tcx>> HasDataLayout for InterpCx<'tcx, M> {
52 #[inline]
53 fn data_layout(&self) -> &TargetDataLayout {
54 &self.tcx.data_layout
55 }
56}
57
58impl<'tcx, M> layout::HasTyCtxt<'tcx> for InterpCx<'tcx, M>
59where
60 M: Machine<'tcx>,
61{
62 #[inline]
63 fn tcx(&self) -> TyCtxt<'tcx> {
64 *self.tcx
65 }
66}
67
68impl<'tcx, M> layout::HasTypingEnv<'tcx> for InterpCx<'tcx, M>
69where
70 M: Machine<'tcx>,
71{
72 fn typing_env(&self) -> ty::TypingEnv<'tcx> {
73 self.typing_env
74 }
75}
76
77impl<'tcx, M: Machine<'tcx>> LayoutOfHelpers<'tcx> for InterpCx<'tcx, M> {
78 type LayoutOfResult = Result<TyAndLayout<'tcx>, InterpErrorKind<'tcx>>;
79
80 #[inline]
81 fn layout_tcx_at_span(&self) -> Span {
82 self.tcx.span
84 }
85
86 #[inline]
87 fn handle_layout_err(
88 &self,
89 mut err: LayoutError<'tcx>,
90 _: Span,
91 _: Ty<'tcx>,
92 ) -> InterpErrorKind<'tcx> {
93 match err {
101 LayoutError::NormalizationFailure(ty, _) => {
102 if ty.has_non_region_param() {
103 err = LayoutError::TooGeneric(ty);
104 }
105 }
106
107 LayoutError::Unknown(_)
108 | LayoutError::SizeOverflow(_)
109 | LayoutError::InvalidSimd { .. }
110 | LayoutError::TooGeneric(_)
111 | LayoutError::ReferencesError(_)
112 | LayoutError::Cycle(_) => {}
113 }
114 err_inval!(Layout(err))
115 }
116}
117
118impl<'tcx, M: Machine<'tcx>> FnAbiOfHelpers<'tcx> for InterpCx<'tcx, M> {
119 type FnAbiOfResult = Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, InterpErrorKind<'tcx>>;
120
121 fn handle_fn_abi_err(
122 &self,
123 err: FnAbiError<'tcx>,
124 _span: Span,
125 _fn_abi_request: FnAbiRequest<'tcx>,
126 ) -> InterpErrorKind<'tcx> {
127 match err {
128 FnAbiError::Layout(err) => err_inval!(Layout(err)),
129 }
130 }
131}
132
133impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
134 #[inline(always)]
138 pub fn layout_of(&self, ty: Ty<'tcx>) -> Result<TyAndLayout<'tcx>, InterpErrorKind<'tcx>> {
139 let _trace = enter_trace_span!(M, layouting::layout_of, ty = ?ty.kind());
140 LayoutOf::layout_of(self, ty)
141 }
142
143 #[inline(always)]
147 pub fn fn_abi_of_fn_ptr(
148 &self,
149 sig: ty::PolyFnSig<'tcx>,
150 extra_args: &'tcx ty::List<Ty<'tcx>>,
151 ) -> <Self as FnAbiOfHelpers<'tcx>>::FnAbiOfResult {
152 let _trace = enter_trace_span!(M, layouting::fn_abi_of_fn_ptr, ?sig, ?extra_args);
153 FnAbiOf::fn_abi_of_fn_ptr(self, sig, extra_args)
154 }
155
156 #[inline(always)]
160 pub fn fn_abi_of_instance(
161 &self,
162 instance: ty::Instance<'tcx>,
163 extra_args: &'tcx ty::List<Ty<'tcx>>,
164 ) -> <Self as FnAbiOfHelpers<'tcx>>::FnAbiOfResult {
165 let _trace = enter_trace_span!(M, layouting::fn_abi_of_instance, ?instance, ?extra_args);
166 FnAbiOf::fn_abi_of_instance(self, instance, extra_args)
167 }
168}
169
170pub(super) fn mir_assign_valid_types<'tcx>(
173 tcx: TyCtxt<'tcx>,
174 typing_env: TypingEnv<'tcx>,
175 src: TyAndLayout<'tcx>,
176 dest: TyAndLayout<'tcx>,
177) -> bool {
178 if util::relate_types(tcx, typing_env, Variance::Covariant, src.ty, dest.ty) {
183 if cfg!(debug_assertions) || src.ty != dest.ty {
189 assert_eq!(src.layout, dest.layout);
190 }
191 true
192 } else {
193 false
194 }
195}
196
197#[cfg_attr(not(debug_assertions), inline(always))]
200pub(super) fn from_known_layout<'tcx>(
201 tcx: TyCtxtAt<'tcx>,
202 typing_env: TypingEnv<'tcx>,
203 known_layout: Option<TyAndLayout<'tcx>>,
204 compute: impl FnOnce() -> InterpResult<'tcx, TyAndLayout<'tcx>>,
205) -> InterpResult<'tcx, TyAndLayout<'tcx>> {
206 match known_layout {
207 None => compute(),
208 Some(known_layout) => {
209 if cfg!(debug_assertions) {
210 let check_layout = compute()?;
211 if !mir_assign_valid_types(tcx.tcx, typing_env, check_layout, known_layout) {
212 span_bug!(
213 tcx.span,
214 "expected type differs from actual type.\nexpected: {}\nactual: {}",
215 known_layout.ty,
216 check_layout.ty,
217 );
218 }
219 }
220 interp_ok(known_layout)
221 }
222 }
223}
224
225pub fn format_interp_error<'tcx>(dcx: DiagCtxtHandle<'_>, e: InterpErrorInfo<'tcx>) -> String {
232 let (e, backtrace) = e.into_parts();
233 backtrace.print_backtrace();
234 #[allow(rustc::untranslatable_diagnostic)]
237 let mut diag = dcx.struct_allow("");
238 let msg = e.diagnostic_message();
239 e.add_args(&mut diag);
240 let s = dcx.eagerly_translate_to_string(msg, diag.args.iter());
241 diag.cancel();
242 s
243}
244
245impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
246 pub fn new(
247 tcx: TyCtxt<'tcx>,
248 root_span: Span,
249 typing_env: ty::TypingEnv<'tcx>,
250 machine: M,
251 ) -> Self {
252 debug_assert_matches!(typing_env.typing_mode, ty::TypingMode::PostAnalysis);
257 InterpCx {
258 machine,
259 tcx: tcx.at(root_span),
260 typing_env,
261 memory: Memory::new(),
262 recursion_limit: tcx.recursion_limit(),
263 }
264 }
265
266 #[inline(always)]
269 pub fn cur_span(&self) -> Span {
270 self.stack().last().map_or(self.tcx.span, |f| f.current_span())
273 }
274
275 pub(crate) fn stack(&self) -> &[Frame<'tcx, M::Provenance, M::FrameExtra>] {
276 M::stack(self)
277 }
278
279 #[inline(always)]
280 pub(crate) fn stack_mut(&mut self) -> &mut Vec<Frame<'tcx, M::Provenance, M::FrameExtra>> {
281 M::stack_mut(self)
282 }
283
284 #[inline(always)]
285 pub fn frame_idx(&self) -> usize {
286 let stack = self.stack();
287 assert!(!stack.is_empty());
288 stack.len() - 1
289 }
290
291 #[inline(always)]
292 pub fn frame(&self) -> &Frame<'tcx, M::Provenance, M::FrameExtra> {
293 self.stack().last().expect("no call frames exist")
294 }
295
296 #[inline(always)]
297 pub fn frame_mut(&mut self) -> &mut Frame<'tcx, M::Provenance, M::FrameExtra> {
298 self.stack_mut().last_mut().expect("no call frames exist")
299 }
300
301 #[inline(always)]
302 pub fn body(&self) -> &'tcx mir::Body<'tcx> {
303 self.frame().body
304 }
305
306 #[inline]
307 pub fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
308 ty.is_freeze(*self.tcx, self.typing_env)
309 }
310
311 pub fn load_mir(
312 &self,
313 instance: ty::InstanceKind<'tcx>,
314 promoted: Option<mir::Promoted>,
315 ) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
316 trace!("load mir(instance={:?}, promoted={:?})", instance, promoted);
317 let body = if let Some(promoted) = promoted {
318 let def = instance.def_id();
319 &self.tcx.promoted_mir(def)[promoted]
320 } else {
321 M::load_mir(self, instance)
322 };
323 if let Some(err) = body.tainted_by_errors {
325 throw_inval!(AlreadyReported(ReportedErrorInfo::non_const_eval_error(err)));
326 }
327 interp_ok(body)
328 }
329
330 pub fn instantiate_from_current_frame_and_normalize_erasing_regions<
333 T: TypeFoldable<TyCtxt<'tcx>>,
334 >(
335 &self,
336 value: T,
337 ) -> Result<T, ErrorHandled> {
338 self.instantiate_from_frame_and_normalize_erasing_regions(self.frame(), value)
339 }
340
341 pub fn instantiate_from_frame_and_normalize_erasing_regions<T: TypeFoldable<TyCtxt<'tcx>>>(
344 &self,
345 frame: &Frame<'tcx, M::Provenance, M::FrameExtra>,
346 value: T,
347 ) -> Result<T, ErrorHandled> {
348 let _trace = enter_trace_span!(
349 M,
350 "instantiate_from_frame_and_normalize_erasing_regions",
351 %frame.instance
352 );
353 frame
354 .instance
355 .try_instantiate_mir_and_normalize_erasing_regions(
356 *self.tcx,
357 self.typing_env,
358 ty::EarlyBinder::bind(value),
359 )
360 .map_err(|_| ErrorHandled::TooGeneric(self.cur_span()))
361 }
362
363 pub(super) fn resolve(
365 &self,
366 def: DefId,
367 args: GenericArgsRef<'tcx>,
368 ) -> InterpResult<'tcx, ty::Instance<'tcx>> {
369 let _trace = enter_trace_span!(M, resolve::try_resolve, def = ?def);
370 trace!("resolve: {:?}, {:#?}", def, args);
371 trace!("typing_env: {:#?}", self.typing_env);
372 trace!("args: {:#?}", args);
373 match ty::Instance::try_resolve(*self.tcx, self.typing_env, def, args) {
374 Ok(Some(instance)) => interp_ok(instance),
375 Ok(None) => throw_inval!(TooGeneric),
376
377 Err(error_guaranteed) => throw_inval!(AlreadyReported(
379 ReportedErrorInfo::non_const_eval_error(error_guaranteed)
380 )),
381 }
382 }
383
384 pub(crate) fn find_closest_untracked_caller_location(&self) -> Span {
388 for frame in self.stack().iter().rev() {
389 debug!("find_closest_untracked_caller_location: checking frame {:?}", frame.instance);
390
391 let loc = frame.loc.left().unwrap();
394
395 let mut source_info = *frame.body.source_info(loc);
398
399 let block = &frame.body.basic_blocks[loc.block];
401 if loc.statement_index == block.statements.len() {
402 debug!(
403 "find_closest_untracked_caller_location: got terminator {:?} ({:?})",
404 block.terminator(),
405 block.terminator().kind,
406 );
407 if let mir::TerminatorKind::Call { fn_span, .. } = block.terminator().kind {
408 source_info.span = fn_span;
409 }
410 }
411
412 let caller_location = if frame.instance.def.requires_caller_location(*self.tcx) {
413 Some(Err(()))
416 } else {
417 None
418 };
419 if let Ok(span) =
420 frame.body.caller_location_span(source_info, caller_location, *self.tcx, Ok)
421 {
422 return span;
423 }
424 }
425
426 span_bug!(self.cur_span(), "no non-`#[track_caller]` frame found")
427 }
428
429 pub(super) fn size_and_align_from_meta(
433 &self,
434 metadata: &MemPlaceMeta<M::Provenance>,
435 layout: &TyAndLayout<'tcx>,
436 ) -> InterpResult<'tcx, Option<(Size, Align)>> {
437 if layout.is_sized() {
438 return interp_ok(Some((layout.size, layout.align.abi)));
439 }
440 match layout.ty.kind() {
441 ty::Adt(..) | ty::Tuple(..) => {
442 assert!(!layout.ty.is_simd());
447 assert!(layout.fields.count() > 0);
448 trace!("DST layout: {:?}", layout);
449
450 let unsized_offset_unadjusted = layout.fields.offset(layout.fields.count() - 1);
451 let sized_align = layout.align.abi;
452
453 let field = layout.field(self, layout.fields.count() - 1);
457 let Some((unsized_size, mut unsized_align)) =
458 self.size_and_align_from_meta(metadata, &field)?
459 else {
460 return interp_ok(None);
463 };
464
465 if let ty::Adt(def, _) = layout.ty.kind()
469 && let Some(packed) = def.repr().pack
470 {
471 unsized_align = unsized_align.min(packed);
472 }
473
474 let full_align = sized_align.max(unsized_align);
477
478 let unsized_offset_adjusted = unsized_offset_unadjusted.align_to(unsized_align);
481 let full_size = (unsized_offset_adjusted + unsized_size).align_to(full_align);
482
483 assert_eq!(
485 full_size,
486 (unsized_offset_unadjusted + unsized_size).align_to(full_align)
487 );
488
489 if full_size > self.max_size_of_val() {
491 throw_ub!(InvalidMeta(InvalidMetaKind::TooBig));
492 }
493 interp_ok(Some((full_size, full_align)))
494 }
495 ty::Dynamic(expected_trait, _) => {
496 let vtable = metadata.unwrap_meta().to_pointer(self)?;
497 interp_ok(Some(self.get_vtable_size_and_align(vtable, Some(expected_trait))?))
499 }
500
501 ty::Slice(_) | ty::Str => {
502 let len = metadata.unwrap_meta().to_target_usize(self)?;
503 let elem = layout.field(self, 0);
504
505 let size = elem.size.bytes().saturating_mul(len); let size = Size::from_bytes(size);
508 if size > self.max_size_of_val() {
509 throw_ub!(InvalidMeta(InvalidMetaKind::SliceTooBig));
510 }
511 interp_ok(Some((size, elem.align.abi)))
512 }
513
514 ty::Foreign(_) => interp_ok(None),
515
516 _ => span_bug!(self.cur_span(), "size_and_align_of::<{}> not supported", layout.ty),
517 }
518 }
519 #[inline]
520 pub fn size_and_align_of_val(
521 &self,
522 val: &impl Projectable<'tcx, M::Provenance>,
523 ) -> InterpResult<'tcx, Option<(Size, Align)>> {
524 self.size_and_align_from_meta(&val.meta(), &val.layout())
525 }
526
527 #[inline]
529 pub fn go_to_block(&mut self, target: mir::BasicBlock) {
530 self.frame_mut().loc = Left(mir::Location { block: target, statement_index: 0 });
531 }
532
533 pub fn return_to_block(&mut self, target: Option<mir::BasicBlock>) -> InterpResult<'tcx> {
538 if let Some(target) = target {
539 self.go_to_block(target);
540 interp_ok(())
541 } else {
542 throw_ub!(Unreachable)
543 }
544 }
545
546 #[cold] pub fn unwind_to_block(&mut self, target: mir::UnwindAction) -> InterpResult<'tcx> {
556 self.frame_mut().loc = match target {
557 mir::UnwindAction::Cleanup(block) => Left(mir::Location { block, statement_index: 0 }),
558 mir::UnwindAction::Continue => Right(self.frame_mut().body.span),
559 mir::UnwindAction::Unreachable => {
560 throw_ub_custom!(fluent::const_eval_unreachable_unwind);
561 }
562 mir::UnwindAction::Terminate(reason) => {
563 self.frame_mut().loc = Right(self.frame_mut().body.span);
564 M::unwind_terminate(self, reason)?;
565 return interp_ok(());
568 }
569 };
570 interp_ok(())
571 }
572
573 pub fn ctfe_query<T>(
576 &self,
577 query: impl FnOnce(TyCtxtAt<'tcx>) -> Result<T, ErrorHandled>,
578 ) -> Result<T, ErrorHandled> {
579 query(self.tcx.at(self.cur_span())).map_err(|err| {
581 err.emit_note(*self.tcx);
582 err
583 })
584 }
585
586 pub fn eval_global(
587 &self,
588 instance: ty::Instance<'tcx>,
589 ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
590 let gid = GlobalId { instance, promoted: None };
591 let val = if self.tcx.is_static(gid.instance.def_id()) {
592 let alloc_id = self.tcx.reserve_and_set_static_alloc(gid.instance.def_id());
593
594 let ty = instance.ty(self.tcx.tcx, self.typing_env);
595 mir::ConstAlloc { alloc_id, ty }
596 } else {
597 self.ctfe_query(|tcx| tcx.eval_to_allocation_raw(self.typing_env.as_query_input(gid)))?
598 };
599 self.raw_const_to_mplace(val)
600 }
601
602 pub fn eval_mir_constant(
603 &self,
604 val: &mir::Const<'tcx>,
605 span: Span,
606 layout: Option<TyAndLayout<'tcx>>,
607 ) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
608 let _trace = enter_trace_span!(M, const_eval::eval_mir_constant, ?val);
609 let const_val = val.eval(*self.tcx, self.typing_env, span).map_err(|err| {
610 if M::ALL_CONSTS_ARE_PRECHECKED {
611 match err {
612 ErrorHandled::TooGeneric(..) => {},
613 ErrorHandled::Reported(reported, span) => {
614 if reported.is_allowed_in_infallible() {
615 } else {
619 span_bug!(span, "interpret const eval failure of {val:?} which is not in required_consts");
621 }
622 }
623 }
624 }
625 err.emit_note(*self.tcx);
626 err
627 })?;
628 self.const_val_to_op(const_val, val.ty(), layout)
629 }
630
631 #[must_use]
632 pub fn dump_place(&self, place: &PlaceTy<'tcx, M::Provenance>) -> PlacePrinter<'_, 'tcx, M> {
633 PlacePrinter { ecx: self, place: *place.place() }
634 }
635
636 #[must_use]
637 pub fn generate_stacktrace(&self) -> Vec<FrameInfo<'tcx>> {
638 Frame::generate_stacktrace_from_stack(self.stack())
639 }
640
641 pub fn adjust_nan<F1, F2>(&self, f: F2, inputs: &[F1]) -> F2
642 where
643 F1: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F2>,
644 F2: rustc_apfloat::Float,
645 {
646 if f.is_nan() { M::generate_nan(self, inputs) } else { f }
647 }
648}
649
650#[doc(hidden)]
651pub struct PlacePrinter<'a, 'tcx, M: Machine<'tcx>> {
653 ecx: &'a InterpCx<'tcx, M>,
654 place: Place<M::Provenance>,
655}
656
657impl<'a, 'tcx, M: Machine<'tcx>> std::fmt::Debug for PlacePrinter<'a, 'tcx, M> {
658 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
659 match self.place {
660 Place::Local { local, offset, locals_addr } => {
661 debug_assert_eq!(locals_addr, self.ecx.frame().locals_addr());
662 let mut allocs = Vec::new();
663 write!(fmt, "{local:?}")?;
664 if let Some(offset) = offset {
665 write!(fmt, "+{:#x}", offset.bytes())?;
666 }
667 write!(fmt, ":")?;
668
669 self.ecx.frame().locals[local].print(&mut allocs, fmt)?;
670
671 write!(fmt, ": {:?}", self.ecx.dump_allocs(allocs.into_iter().flatten().collect()))
672 }
673 Place::Ptr(mplace) => match mplace.ptr.provenance.and_then(Provenance::get_alloc_id) {
674 Some(alloc_id) => {
675 write!(fmt, "by ref {:?}: {:?}", mplace.ptr, self.ecx.dump_alloc(alloc_id))
676 }
677 ptr => write!(fmt, " integral by ref: {ptr:?}"),
678 },
679 }
680 }
681}