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_infer::infer::TyCtxtInferExt;
8use rustc_infer::infer::at::ToTrace;
9use rustc_infer::traits::ObligationCause;
10use rustc_middle::mir::interpret::{ErrorHandled, InvalidMetaKind, ReportedErrorInfo};
11use rustc_middle::query::TyCtxtAt;
12use rustc_middle::ty::layout::{
13 self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers, TyAndLayout,
14};
15use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt, TypeFoldable, TypingEnv, Variance};
16use rustc_middle::{mir, span_bug};
17use rustc_session::Limit;
18use rustc_span::Span;
19use rustc_target::callconv::FnAbi;
20use rustc_trait_selection::traits::ObligationCtxt;
21use tracing::{debug, instrument, trace};
22
23use super::{
24 Frame, FrameInfo, GlobalId, InterpErrorInfo, InterpErrorKind, InterpResult, MPlaceTy, Machine,
25 MemPlaceMeta, Memory, OpTy, Place, PlaceTy, PointerArithmetic, Projectable, Provenance,
26 err_inval, interp_ok, throw_inval, throw_ub, throw_ub_custom,
27};
28use crate::{ReportErrorExt, fluent_generated as fluent, util};
29
30pub struct InterpCx<'tcx, M: Machine<'tcx>> {
31 pub machine: M,
35
36 pub tcx: TyCtxtAt<'tcx>,
40
41 pub(super) typing_env: ty::TypingEnv<'tcx>,
44
45 pub memory: Memory<'tcx, M>,
47
48 pub recursion_limit: Limit,
50}
51
52impl<'tcx, M: Machine<'tcx>> HasDataLayout for InterpCx<'tcx, M> {
53 #[inline]
54 fn data_layout(&self) -> &TargetDataLayout {
55 &self.tcx.data_layout
56 }
57}
58
59impl<'tcx, M> layout::HasTyCtxt<'tcx> for InterpCx<'tcx, M>
60where
61 M: Machine<'tcx>,
62{
63 #[inline]
64 fn tcx(&self) -> TyCtxt<'tcx> {
65 *self.tcx
66 }
67}
68
69impl<'tcx, M> layout::HasTypingEnv<'tcx> for InterpCx<'tcx, M>
70where
71 M: Machine<'tcx>,
72{
73 fn typing_env(&self) -> ty::TypingEnv<'tcx> {
74 self.typing_env
75 }
76}
77
78impl<'tcx, M: Machine<'tcx>> LayoutOfHelpers<'tcx> for InterpCx<'tcx, M> {
79 type LayoutOfResult = Result<TyAndLayout<'tcx>, InterpErrorKind<'tcx>>;
80
81 #[inline]
82 fn layout_tcx_at_span(&self) -> Span {
83 self.tcx.span
85 }
86
87 #[inline]
88 fn handle_layout_err(
89 &self,
90 err: LayoutError<'tcx>,
91 _: Span,
92 _: Ty<'tcx>,
93 ) -> InterpErrorKind<'tcx> {
94 err_inval!(Layout(err))
95 }
96}
97
98impl<'tcx, M: Machine<'tcx>> FnAbiOfHelpers<'tcx> for InterpCx<'tcx, M> {
99 type FnAbiOfResult = Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, InterpErrorKind<'tcx>>;
100
101 fn handle_fn_abi_err(
102 &self,
103 err: FnAbiError<'tcx>,
104 _span: Span,
105 _fn_abi_request: FnAbiRequest<'tcx>,
106 ) -> InterpErrorKind<'tcx> {
107 match err {
108 FnAbiError::Layout(err) => err_inval!(Layout(err)),
109 }
110 }
111}
112
113pub(super) fn mir_assign_valid_types<'tcx>(
116 tcx: TyCtxt<'tcx>,
117 typing_env: TypingEnv<'tcx>,
118 src: TyAndLayout<'tcx>,
119 dest: TyAndLayout<'tcx>,
120) -> bool {
121 if util::relate_types(tcx, typing_env, Variance::Covariant, src.ty, dest.ty) {
126 if cfg!(debug_assertions) || src.ty != dest.ty {
132 assert_eq!(src.layout, dest.layout);
133 }
134 true
135 } else {
136 false
137 }
138}
139
140#[cfg_attr(not(debug_assertions), inline(always))]
143pub(super) fn from_known_layout<'tcx>(
144 tcx: TyCtxtAt<'tcx>,
145 typing_env: TypingEnv<'tcx>,
146 known_layout: Option<TyAndLayout<'tcx>>,
147 compute: impl FnOnce() -> InterpResult<'tcx, TyAndLayout<'tcx>>,
148) -> InterpResult<'tcx, TyAndLayout<'tcx>> {
149 match known_layout {
150 None => compute(),
151 Some(known_layout) => {
152 if cfg!(debug_assertions) {
153 let check_layout = compute()?;
154 if !mir_assign_valid_types(tcx.tcx, typing_env, check_layout, known_layout) {
155 span_bug!(
156 tcx.span,
157 "expected type differs from actual type.\nexpected: {}\nactual: {}",
158 known_layout.ty,
159 check_layout.ty,
160 );
161 }
162 }
163 interp_ok(known_layout)
164 }
165 }
166}
167
168pub fn format_interp_error<'tcx>(dcx: DiagCtxtHandle<'_>, e: InterpErrorInfo<'tcx>) -> String {
175 let (e, backtrace) = e.into_parts();
176 backtrace.print_backtrace();
177 #[allow(rustc::untranslatable_diagnostic)]
180 let mut diag = dcx.struct_allow("");
181 let msg = e.diagnostic_message();
182 e.add_args(&mut diag);
183 let s = dcx.eagerly_translate_to_string(msg, diag.args.iter());
184 diag.cancel();
185 s
186}
187
188impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
189 pub fn new(
190 tcx: TyCtxt<'tcx>,
191 root_span: Span,
192 typing_env: ty::TypingEnv<'tcx>,
193 machine: M,
194 ) -> Self {
195 debug_assert_matches!(typing_env.typing_mode, ty::TypingMode::PostAnalysis);
200 InterpCx {
201 machine,
202 tcx: tcx.at(root_span),
203 typing_env,
204 memory: Memory::new(),
205 recursion_limit: tcx.recursion_limit(),
206 }
207 }
208
209 #[inline(always)]
212 pub fn cur_span(&self) -> Span {
213 self.stack().last().map_or(self.tcx.span, |f| f.current_span())
216 }
217
218 pub(crate) fn stack(&self) -> &[Frame<'tcx, M::Provenance, M::FrameExtra>] {
219 M::stack(self)
220 }
221
222 #[inline(always)]
223 pub(crate) fn stack_mut(&mut self) -> &mut Vec<Frame<'tcx, M::Provenance, M::FrameExtra>> {
224 M::stack_mut(self)
225 }
226
227 #[inline(always)]
228 pub fn frame_idx(&self) -> usize {
229 let stack = self.stack();
230 assert!(!stack.is_empty());
231 stack.len() - 1
232 }
233
234 #[inline(always)]
235 pub fn frame(&self) -> &Frame<'tcx, M::Provenance, M::FrameExtra> {
236 self.stack().last().expect("no call frames exist")
237 }
238
239 #[inline(always)]
240 pub fn frame_mut(&mut self) -> &mut Frame<'tcx, M::Provenance, M::FrameExtra> {
241 self.stack_mut().last_mut().expect("no call frames exist")
242 }
243
244 #[inline(always)]
245 pub fn body(&self) -> &'tcx mir::Body<'tcx> {
246 self.frame().body
247 }
248
249 #[inline]
250 pub fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
251 ty.is_freeze(*self.tcx, self.typing_env)
252 }
253
254 pub fn load_mir(
255 &self,
256 instance: ty::InstanceKind<'tcx>,
257 promoted: Option<mir::Promoted>,
258 ) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
259 trace!("load mir(instance={:?}, promoted={:?})", instance, promoted);
260 let body = if let Some(promoted) = promoted {
261 let def = instance.def_id();
262 &self.tcx.promoted_mir(def)[promoted]
263 } else {
264 M::load_mir(self, instance)?
265 };
266 if let Some(err) = body.tainted_by_errors {
268 throw_inval!(AlreadyReported(ReportedErrorInfo::non_const_eval_error(err)));
269 }
270 interp_ok(body)
271 }
272
273 pub(super) fn instantiate_from_current_frame_and_normalize_erasing_regions<
276 T: TypeFoldable<TyCtxt<'tcx>>,
277 >(
278 &self,
279 value: T,
280 ) -> Result<T, ErrorHandled> {
281 self.instantiate_from_frame_and_normalize_erasing_regions(self.frame(), value)
282 }
283
284 pub(super) fn instantiate_from_frame_and_normalize_erasing_regions<
287 T: TypeFoldable<TyCtxt<'tcx>>,
288 >(
289 &self,
290 frame: &Frame<'tcx, M::Provenance, M::FrameExtra>,
291 value: T,
292 ) -> Result<T, ErrorHandled> {
293 frame
294 .instance
295 .try_instantiate_mir_and_normalize_erasing_regions(
296 *self.tcx,
297 self.typing_env,
298 ty::EarlyBinder::bind(value),
299 )
300 .map_err(|_| ErrorHandled::TooGeneric(self.cur_span()))
301 }
302
303 pub(super) fn resolve(
305 &self,
306 def: DefId,
307 args: GenericArgsRef<'tcx>,
308 ) -> InterpResult<'tcx, ty::Instance<'tcx>> {
309 trace!("resolve: {:?}, {:#?}", def, args);
310 trace!("typing_env: {:#?}", self.typing_env);
311 trace!("args: {:#?}", args);
312 match ty::Instance::try_resolve(*self.tcx, self.typing_env, def, args) {
313 Ok(Some(instance)) => interp_ok(instance),
314 Ok(None) => throw_inval!(TooGeneric),
315
316 Err(error_guaranteed) => throw_inval!(AlreadyReported(
318 ReportedErrorInfo::non_const_eval_error(error_guaranteed)
319 )),
320 }
321 }
322
323 #[instrument(level = "trace", skip(self), ret)]
326 pub(super) fn eq_in_param_env<T>(&self, a: T, b: T) -> bool
327 where
328 T: PartialEq + TypeFoldable<TyCtxt<'tcx>> + ToTrace<'tcx>,
329 {
330 if a == b {
332 return true;
333 }
334 let (infcx, param_env) = self.tcx.infer_ctxt().build_with_typing_env(self.typing_env);
336 let ocx = ObligationCtxt::new(&infcx);
337 let cause = ObligationCause::dummy_with_span(self.cur_span());
338 let a = ocx.normalize(&cause, param_env, a);
340 let b = ocx.normalize(&cause, param_env, b);
341
342 if let Err(terr) = ocx.eq(&cause, param_env, a, b) {
343 trace!(?terr);
344 return false;
345 }
346
347 let errors = ocx.select_all_or_error();
348 if !errors.is_empty() {
349 trace!(?errors);
350 return false;
351 }
352
353 true
355 }
356
357 pub(crate) fn find_closest_untracked_caller_location(&self) -> Span {
361 for frame in self.stack().iter().rev() {
362 debug!("find_closest_untracked_caller_location: checking frame {:?}", frame.instance);
363
364 let loc = frame.loc.left().unwrap();
367
368 let mut source_info = *frame.body.source_info(loc);
371
372 let block = &frame.body.basic_blocks[loc.block];
374 if loc.statement_index == block.statements.len() {
375 debug!(
376 "find_closest_untracked_caller_location: got terminator {:?} ({:?})",
377 block.terminator(),
378 block.terminator().kind,
379 );
380 if let mir::TerminatorKind::Call { fn_span, .. } = block.terminator().kind {
381 source_info.span = fn_span;
382 }
383 }
384
385 let caller_location = if frame.instance.def.requires_caller_location(*self.tcx) {
386 Some(Err(()))
389 } else {
390 None
391 };
392 if let Ok(span) =
393 frame.body.caller_location_span(source_info, caller_location, *self.tcx, Ok)
394 {
395 return span;
396 }
397 }
398
399 span_bug!(self.cur_span(), "no non-`#[track_caller]` frame found")
400 }
401
402 pub(super) fn size_and_align_of(
406 &self,
407 metadata: &MemPlaceMeta<M::Provenance>,
408 layout: &TyAndLayout<'tcx>,
409 ) -> InterpResult<'tcx, Option<(Size, Align)>> {
410 if layout.is_sized() {
411 return interp_ok(Some((layout.size, layout.align.abi)));
412 }
413 match layout.ty.kind() {
414 ty::Adt(..) | ty::Tuple(..) => {
415 assert!(!layout.ty.is_simd());
420 assert!(layout.fields.count() > 0);
421 trace!("DST layout: {:?}", layout);
422
423 let unsized_offset_unadjusted = layout.fields.offset(layout.fields.count() - 1);
424 let sized_align = layout.align.abi;
425
426 let field = layout.field(self, layout.fields.count() - 1);
430 let Some((unsized_size, mut unsized_align)) =
431 self.size_and_align_of(metadata, &field)?
432 else {
433 return interp_ok(None);
436 };
437
438 if let ty::Adt(def, _) = layout.ty.kind() {
442 if let Some(packed) = def.repr().pack {
443 unsized_align = unsized_align.min(packed);
444 }
445 }
446
447 let full_align = sized_align.max(unsized_align);
450
451 let unsized_offset_adjusted = unsized_offset_unadjusted.align_to(unsized_align);
454 let full_size = (unsized_offset_adjusted + unsized_size).align_to(full_align);
455
456 assert_eq!(
458 full_size,
459 (unsized_offset_unadjusted + unsized_size).align_to(full_align)
460 );
461
462 if full_size > self.max_size_of_val() {
464 throw_ub!(InvalidMeta(InvalidMetaKind::TooBig));
465 }
466 interp_ok(Some((full_size, full_align)))
467 }
468 ty::Dynamic(expected_trait, _, ty::Dyn) => {
469 let vtable = metadata.unwrap_meta().to_pointer(self)?;
470 interp_ok(Some(self.get_vtable_size_and_align(vtable, Some(expected_trait))?))
472 }
473
474 ty::Slice(_) | ty::Str => {
475 let len = metadata.unwrap_meta().to_target_usize(self)?;
476 let elem = layout.field(self, 0);
477
478 let size = elem.size.bytes().saturating_mul(len); let size = Size::from_bytes(size);
481 if size > self.max_size_of_val() {
482 throw_ub!(InvalidMeta(InvalidMetaKind::SliceTooBig));
483 }
484 interp_ok(Some((size, elem.align.abi)))
485 }
486
487 ty::Foreign(_) => interp_ok(None),
488
489 _ => span_bug!(self.cur_span(), "size_and_align_of::<{}> not supported", layout.ty),
490 }
491 }
492 #[inline]
493 pub fn size_and_align_of_mplace(
494 &self,
495 mplace: &MPlaceTy<'tcx, M::Provenance>,
496 ) -> InterpResult<'tcx, Option<(Size, Align)>> {
497 self.size_and_align_of(&mplace.meta(), &mplace.layout)
498 }
499
500 #[inline]
502 pub fn go_to_block(&mut self, target: mir::BasicBlock) {
503 self.frame_mut().loc = Left(mir::Location { block: target, statement_index: 0 });
504 }
505
506 pub fn return_to_block(&mut self, target: Option<mir::BasicBlock>) -> InterpResult<'tcx> {
511 if let Some(target) = target {
512 self.go_to_block(target);
513 interp_ok(())
514 } else {
515 throw_ub!(Unreachable)
516 }
517 }
518
519 #[cold] pub fn unwind_to_block(&mut self, target: mir::UnwindAction) -> InterpResult<'tcx> {
529 self.frame_mut().loc = match target {
530 mir::UnwindAction::Cleanup(block) => Left(mir::Location { block, statement_index: 0 }),
531 mir::UnwindAction::Continue => Right(self.frame_mut().body.span),
532 mir::UnwindAction::Unreachable => {
533 throw_ub_custom!(fluent::const_eval_unreachable_unwind);
534 }
535 mir::UnwindAction::Terminate(reason) => {
536 self.frame_mut().loc = Right(self.frame_mut().body.span);
537 M::unwind_terminate(self, reason)?;
538 return interp_ok(());
541 }
542 };
543 interp_ok(())
544 }
545
546 pub fn ctfe_query<T>(
549 &self,
550 query: impl FnOnce(TyCtxtAt<'tcx>) -> Result<T, ErrorHandled>,
551 ) -> Result<T, ErrorHandled> {
552 query(self.tcx.at(self.cur_span())).map_err(|err| {
554 err.emit_note(*self.tcx);
555 err
556 })
557 }
558
559 pub fn eval_global(
560 &self,
561 instance: ty::Instance<'tcx>,
562 ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
563 let gid = GlobalId { instance, promoted: None };
564 let val = if self.tcx.is_static(gid.instance.def_id()) {
565 let alloc_id = self.tcx.reserve_and_set_static_alloc(gid.instance.def_id());
566
567 let ty = instance.ty(self.tcx.tcx, self.typing_env);
568 mir::ConstAlloc { alloc_id, ty }
569 } else {
570 self.ctfe_query(|tcx| tcx.eval_to_allocation_raw(self.typing_env.as_query_input(gid)))?
571 };
572 self.raw_const_to_mplace(val)
573 }
574
575 pub fn eval_mir_constant(
576 &self,
577 val: &mir::Const<'tcx>,
578 span: Span,
579 layout: Option<TyAndLayout<'tcx>>,
580 ) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
581 M::eval_mir_constant(self, *val, span, layout, |ecx, val, span, layout| {
582 let const_val = val.eval(*ecx.tcx, ecx.typing_env, span).map_err(|err| {
583 if M::ALL_CONSTS_ARE_PRECHECKED {
584 match err {
585 ErrorHandled::TooGeneric(..) => {},
586 ErrorHandled::Reported(reported, span) => {
587 if reported.is_allowed_in_infallible() {
588 } else {
592 span_bug!(span, "interpret const eval failure of {val:?} which is not in required_consts");
594 }
595 }
596 }
597 }
598 err.emit_note(*ecx.tcx);
599 err
600 })?;
601 ecx.const_val_to_op(const_val, val.ty(), layout)
602 })
603 }
604
605 #[must_use]
606 pub fn dump_place(&self, place: &PlaceTy<'tcx, M::Provenance>) -> PlacePrinter<'_, 'tcx, M> {
607 PlacePrinter { ecx: self, place: *place.place() }
608 }
609
610 #[must_use]
611 pub fn generate_stacktrace(&self) -> Vec<FrameInfo<'tcx>> {
612 Frame::generate_stacktrace_from_stack(self.stack())
613 }
614
615 pub fn adjust_nan<F1, F2>(&self, f: F2, inputs: &[F1]) -> F2
616 where
617 F1: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F2>,
618 F2: rustc_apfloat::Float,
619 {
620 if f.is_nan() { M::generate_nan(self, inputs) } else { f }
621 }
622}
623
624#[doc(hidden)]
625pub struct PlacePrinter<'a, 'tcx, M: Machine<'tcx>> {
627 ecx: &'a InterpCx<'tcx, M>,
628 place: Place<M::Provenance>,
629}
630
631impl<'a, 'tcx, M: Machine<'tcx>> std::fmt::Debug for PlacePrinter<'a, 'tcx, M> {
632 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
633 match self.place {
634 Place::Local { local, offset, locals_addr } => {
635 debug_assert_eq!(locals_addr, self.ecx.frame().locals_addr());
636 let mut allocs = Vec::new();
637 write!(fmt, "{local:?}")?;
638 if let Some(offset) = offset {
639 write!(fmt, "+{:#x}", offset.bytes())?;
640 }
641 write!(fmt, ":")?;
642
643 self.ecx.frame().locals[local].print(&mut allocs, fmt)?;
644
645 write!(fmt, ": {:?}", self.ecx.dump_allocs(allocs.into_iter().flatten().collect()))
646 }
647 Place::Ptr(mplace) => match mplace.ptr.provenance.and_then(Provenance::get_alloc_id) {
648 Some(alloc_id) => {
649 write!(fmt, "by ref {:?}: {:?}", mplace.ptr, self.ecx.dump_alloc(alloc_id))
650 }
651 ptr => write!(fmt, " integral by ref: {ptr:?}"),
652 },
653 }
654 }
655}