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_middle::mir::interpret::{ErrorHandled, InvalidMetaKind, ReportedErrorInfo};
8use rustc_middle::query::TyCtxtAt;
9use rustc_middle::ty::layout::{
10 self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers, TyAndLayout,
11};
12use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt, TypeFoldable, TypingEnv, Variance};
13use rustc_middle::{mir, span_bug};
14use rustc_session::Limit;
15use rustc_span::Span;
16use rustc_target::callconv::FnAbi;
17use tracing::{debug, trace};
18
19use super::{
20 Frame, FrameInfo, GlobalId, InterpErrorInfo, InterpErrorKind, InterpResult, MPlaceTy, Machine,
21 MemPlaceMeta, Memory, OpTy, Place, PlaceTy, PointerArithmetic, Projectable, Provenance,
22 err_inval, interp_ok, throw_inval, throw_ub, throw_ub_custom,
23};
24use crate::{ReportErrorExt, fluent_generated as fluent, util};
25
26pub struct InterpCx<'tcx, M: Machine<'tcx>> {
27 pub machine: M,
31
32 pub tcx: TyCtxtAt<'tcx>,
36
37 pub(super) typing_env: ty::TypingEnv<'tcx>,
40
41 pub memory: Memory<'tcx, M>,
43
44 pub recursion_limit: Limit,
46}
47
48impl<'tcx, M: Machine<'tcx>> HasDataLayout for InterpCx<'tcx, M> {
49 #[inline]
50 fn data_layout(&self) -> &TargetDataLayout {
51 &self.tcx.data_layout
52 }
53}
54
55impl<'tcx, M> layout::HasTyCtxt<'tcx> for InterpCx<'tcx, M>
56where
57 M: Machine<'tcx>,
58{
59 #[inline]
60 fn tcx(&self) -> TyCtxt<'tcx> {
61 *self.tcx
62 }
63}
64
65impl<'tcx, M> layout::HasTypingEnv<'tcx> for InterpCx<'tcx, M>
66where
67 M: Machine<'tcx>,
68{
69 fn typing_env(&self) -> ty::TypingEnv<'tcx> {
70 self.typing_env
71 }
72}
73
74impl<'tcx, M: Machine<'tcx>> LayoutOfHelpers<'tcx> for InterpCx<'tcx, M> {
75 type LayoutOfResult = Result<TyAndLayout<'tcx>, InterpErrorKind<'tcx>>;
76
77 #[inline]
78 fn layout_tcx_at_span(&self) -> Span {
79 self.tcx.span
81 }
82
83 #[inline]
84 fn handle_layout_err(
85 &self,
86 err: LayoutError<'tcx>,
87 _: Span,
88 _: Ty<'tcx>,
89 ) -> InterpErrorKind<'tcx> {
90 err_inval!(Layout(err))
91 }
92}
93
94impl<'tcx, M: Machine<'tcx>> FnAbiOfHelpers<'tcx> for InterpCx<'tcx, M> {
95 type FnAbiOfResult = Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, InterpErrorKind<'tcx>>;
96
97 fn handle_fn_abi_err(
98 &self,
99 err: FnAbiError<'tcx>,
100 _span: Span,
101 _fn_abi_request: FnAbiRequest<'tcx>,
102 ) -> InterpErrorKind<'tcx> {
103 match err {
104 FnAbiError::Layout(err) => err_inval!(Layout(err)),
105 }
106 }
107}
108
109pub(super) fn mir_assign_valid_types<'tcx>(
112 tcx: TyCtxt<'tcx>,
113 typing_env: TypingEnv<'tcx>,
114 src: TyAndLayout<'tcx>,
115 dest: TyAndLayout<'tcx>,
116) -> bool {
117 if util::relate_types(tcx, typing_env, Variance::Covariant, src.ty, dest.ty) {
122 if cfg!(debug_assertions) || src.ty != dest.ty {
128 assert_eq!(src.layout, dest.layout);
129 }
130 true
131 } else {
132 false
133 }
134}
135
136#[cfg_attr(not(debug_assertions), inline(always))]
139pub(super) fn from_known_layout<'tcx>(
140 tcx: TyCtxtAt<'tcx>,
141 typing_env: TypingEnv<'tcx>,
142 known_layout: Option<TyAndLayout<'tcx>>,
143 compute: impl FnOnce() -> InterpResult<'tcx, TyAndLayout<'tcx>>,
144) -> InterpResult<'tcx, TyAndLayout<'tcx>> {
145 match known_layout {
146 None => compute(),
147 Some(known_layout) => {
148 if cfg!(debug_assertions) {
149 let check_layout = compute()?;
150 if !mir_assign_valid_types(tcx.tcx, typing_env, check_layout, known_layout) {
151 span_bug!(
152 tcx.span,
153 "expected type differs from actual type.\nexpected: {}\nactual: {}",
154 known_layout.ty,
155 check_layout.ty,
156 );
157 }
158 }
159 interp_ok(known_layout)
160 }
161 }
162}
163
164pub fn format_interp_error<'tcx>(dcx: DiagCtxtHandle<'_>, e: InterpErrorInfo<'tcx>) -> String {
171 let (e, backtrace) = e.into_parts();
172 backtrace.print_backtrace();
173 #[allow(rustc::untranslatable_diagnostic)]
176 let mut diag = dcx.struct_allow("");
177 let msg = e.diagnostic_message();
178 e.add_args(&mut diag);
179 let s = dcx.eagerly_translate_to_string(msg, diag.args.iter());
180 diag.cancel();
181 s
182}
183
184impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
185 pub fn new(
186 tcx: TyCtxt<'tcx>,
187 root_span: Span,
188 typing_env: ty::TypingEnv<'tcx>,
189 machine: M,
190 ) -> Self {
191 debug_assert_matches!(typing_env.typing_mode, ty::TypingMode::PostAnalysis);
196 InterpCx {
197 machine,
198 tcx: tcx.at(root_span),
199 typing_env,
200 memory: Memory::new(),
201 recursion_limit: tcx.recursion_limit(),
202 }
203 }
204
205 #[inline(always)]
208 pub fn cur_span(&self) -> Span {
209 self.stack().last().map_or(self.tcx.span, |f| f.current_span())
212 }
213
214 pub(crate) fn stack(&self) -> &[Frame<'tcx, M::Provenance, M::FrameExtra>] {
215 M::stack(self)
216 }
217
218 #[inline(always)]
219 pub(crate) fn stack_mut(&mut self) -> &mut Vec<Frame<'tcx, M::Provenance, M::FrameExtra>> {
220 M::stack_mut(self)
221 }
222
223 #[inline(always)]
224 pub fn frame_idx(&self) -> usize {
225 let stack = self.stack();
226 assert!(!stack.is_empty());
227 stack.len() - 1
228 }
229
230 #[inline(always)]
231 pub fn frame(&self) -> &Frame<'tcx, M::Provenance, M::FrameExtra> {
232 self.stack().last().expect("no call frames exist")
233 }
234
235 #[inline(always)]
236 pub fn frame_mut(&mut self) -> &mut Frame<'tcx, M::Provenance, M::FrameExtra> {
237 self.stack_mut().last_mut().expect("no call frames exist")
238 }
239
240 #[inline(always)]
241 pub fn body(&self) -> &'tcx mir::Body<'tcx> {
242 self.frame().body
243 }
244
245 #[inline]
246 pub fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
247 ty.is_freeze(*self.tcx, self.typing_env)
248 }
249
250 pub fn load_mir(
251 &self,
252 instance: ty::InstanceKind<'tcx>,
253 promoted: Option<mir::Promoted>,
254 ) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
255 trace!("load mir(instance={:?}, promoted={:?})", instance, promoted);
256 let body = if let Some(promoted) = promoted {
257 let def = instance.def_id();
258 &self.tcx.promoted_mir(def)[promoted]
259 } else {
260 M::load_mir(self, instance)?
261 };
262 if let Some(err) = body.tainted_by_errors {
264 throw_inval!(AlreadyReported(ReportedErrorInfo::non_const_eval_error(err)));
265 }
266 interp_ok(body)
267 }
268
269 pub fn instantiate_from_current_frame_and_normalize_erasing_regions<
272 T: TypeFoldable<TyCtxt<'tcx>>,
273 >(
274 &self,
275 value: T,
276 ) -> Result<T, ErrorHandled> {
277 self.instantiate_from_frame_and_normalize_erasing_regions(self.frame(), value)
278 }
279
280 pub fn instantiate_from_frame_and_normalize_erasing_regions<T: TypeFoldable<TyCtxt<'tcx>>>(
283 &self,
284 frame: &Frame<'tcx, M::Provenance, M::FrameExtra>,
285 value: T,
286 ) -> Result<T, ErrorHandled> {
287 frame
288 .instance
289 .try_instantiate_mir_and_normalize_erasing_regions(
290 *self.tcx,
291 self.typing_env,
292 ty::EarlyBinder::bind(value),
293 )
294 .map_err(|_| ErrorHandled::TooGeneric(self.cur_span()))
295 }
296
297 pub(super) fn resolve(
299 &self,
300 def: DefId,
301 args: GenericArgsRef<'tcx>,
302 ) -> InterpResult<'tcx, ty::Instance<'tcx>> {
303 trace!("resolve: {:?}, {:#?}", def, args);
304 trace!("typing_env: {:#?}", self.typing_env);
305 trace!("args: {:#?}", args);
306 match ty::Instance::try_resolve(*self.tcx, self.typing_env, def, args) {
307 Ok(Some(instance)) => interp_ok(instance),
308 Ok(None) => throw_inval!(TooGeneric),
309
310 Err(error_guaranteed) => throw_inval!(AlreadyReported(
312 ReportedErrorInfo::non_const_eval_error(error_guaranteed)
313 )),
314 }
315 }
316
317 pub(crate) fn find_closest_untracked_caller_location(&self) -> Span {
321 for frame in self.stack().iter().rev() {
322 debug!("find_closest_untracked_caller_location: checking frame {:?}", frame.instance);
323
324 let loc = frame.loc.left().unwrap();
327
328 let mut source_info = *frame.body.source_info(loc);
331
332 let block = &frame.body.basic_blocks[loc.block];
334 if loc.statement_index == block.statements.len() {
335 debug!(
336 "find_closest_untracked_caller_location: got terminator {:?} ({:?})",
337 block.terminator(),
338 block.terminator().kind,
339 );
340 if let mir::TerminatorKind::Call { fn_span, .. } = block.terminator().kind {
341 source_info.span = fn_span;
342 }
343 }
344
345 let caller_location = if frame.instance.def.requires_caller_location(*self.tcx) {
346 Some(Err(()))
349 } else {
350 None
351 };
352 if let Ok(span) =
353 frame.body.caller_location_span(source_info, caller_location, *self.tcx, Ok)
354 {
355 return span;
356 }
357 }
358
359 span_bug!(self.cur_span(), "no non-`#[track_caller]` frame found")
360 }
361
362 pub(super) fn size_and_align_of(
366 &self,
367 metadata: &MemPlaceMeta<M::Provenance>,
368 layout: &TyAndLayout<'tcx>,
369 ) -> InterpResult<'tcx, Option<(Size, Align)>> {
370 if layout.is_sized() {
371 return interp_ok(Some((layout.size, layout.align.abi)));
372 }
373 match layout.ty.kind() {
374 ty::Adt(..) | ty::Tuple(..) => {
375 assert!(!layout.ty.is_simd());
380 assert!(layout.fields.count() > 0);
381 trace!("DST layout: {:?}", layout);
382
383 let unsized_offset_unadjusted = layout.fields.offset(layout.fields.count() - 1);
384 let sized_align = layout.align.abi;
385
386 let field = layout.field(self, layout.fields.count() - 1);
390 let Some((unsized_size, mut unsized_align)) =
391 self.size_and_align_of(metadata, &field)?
392 else {
393 return interp_ok(None);
396 };
397
398 if let ty::Adt(def, _) = layout.ty.kind() {
402 if let Some(packed) = def.repr().pack {
403 unsized_align = unsized_align.min(packed);
404 }
405 }
406
407 let full_align = sized_align.max(unsized_align);
410
411 let unsized_offset_adjusted = unsized_offset_unadjusted.align_to(unsized_align);
414 let full_size = (unsized_offset_adjusted + unsized_size).align_to(full_align);
415
416 assert_eq!(
418 full_size,
419 (unsized_offset_unadjusted + unsized_size).align_to(full_align)
420 );
421
422 if full_size > self.max_size_of_val() {
424 throw_ub!(InvalidMeta(InvalidMetaKind::TooBig));
425 }
426 interp_ok(Some((full_size, full_align)))
427 }
428 ty::Dynamic(expected_trait, _, ty::Dyn) => {
429 let vtable = metadata.unwrap_meta().to_pointer(self)?;
430 interp_ok(Some(self.get_vtable_size_and_align(vtable, Some(expected_trait))?))
432 }
433
434 ty::Slice(_) | ty::Str => {
435 let len = metadata.unwrap_meta().to_target_usize(self)?;
436 let elem = layout.field(self, 0);
437
438 let size = elem.size.bytes().saturating_mul(len); let size = Size::from_bytes(size);
441 if size > self.max_size_of_val() {
442 throw_ub!(InvalidMeta(InvalidMetaKind::SliceTooBig));
443 }
444 interp_ok(Some((size, elem.align.abi)))
445 }
446
447 ty::Foreign(_) => interp_ok(None),
448
449 _ => span_bug!(self.cur_span(), "size_and_align_of::<{}> not supported", layout.ty),
450 }
451 }
452 #[inline]
453 pub fn size_and_align_of_mplace(
454 &self,
455 mplace: &MPlaceTy<'tcx, M::Provenance>,
456 ) -> InterpResult<'tcx, Option<(Size, Align)>> {
457 self.size_and_align_of(&mplace.meta(), &mplace.layout)
458 }
459
460 #[inline]
462 pub fn go_to_block(&mut self, target: mir::BasicBlock) {
463 self.frame_mut().loc = Left(mir::Location { block: target, statement_index: 0 });
464 }
465
466 pub fn return_to_block(&mut self, target: Option<mir::BasicBlock>) -> InterpResult<'tcx> {
471 if let Some(target) = target {
472 self.go_to_block(target);
473 interp_ok(())
474 } else {
475 throw_ub!(Unreachable)
476 }
477 }
478
479 #[cold] pub fn unwind_to_block(&mut self, target: mir::UnwindAction) -> InterpResult<'tcx> {
489 self.frame_mut().loc = match target {
490 mir::UnwindAction::Cleanup(block) => Left(mir::Location { block, statement_index: 0 }),
491 mir::UnwindAction::Continue => Right(self.frame_mut().body.span),
492 mir::UnwindAction::Unreachable => {
493 throw_ub_custom!(fluent::const_eval_unreachable_unwind);
494 }
495 mir::UnwindAction::Terminate(reason) => {
496 self.frame_mut().loc = Right(self.frame_mut().body.span);
497 M::unwind_terminate(self, reason)?;
498 return interp_ok(());
501 }
502 };
503 interp_ok(())
504 }
505
506 pub fn ctfe_query<T>(
509 &self,
510 query: impl FnOnce(TyCtxtAt<'tcx>) -> Result<T, ErrorHandled>,
511 ) -> Result<T, ErrorHandled> {
512 query(self.tcx.at(self.cur_span())).map_err(|err| {
514 err.emit_note(*self.tcx);
515 err
516 })
517 }
518
519 pub fn eval_global(
520 &self,
521 instance: ty::Instance<'tcx>,
522 ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
523 let gid = GlobalId { instance, promoted: None };
524 let val = if self.tcx.is_static(gid.instance.def_id()) {
525 let alloc_id = self.tcx.reserve_and_set_static_alloc(gid.instance.def_id());
526
527 let ty = instance.ty(self.tcx.tcx, self.typing_env);
528 mir::ConstAlloc { alloc_id, ty }
529 } else {
530 self.ctfe_query(|tcx| tcx.eval_to_allocation_raw(self.typing_env.as_query_input(gid)))?
531 };
532 self.raw_const_to_mplace(val)
533 }
534
535 pub fn eval_mir_constant(
536 &self,
537 val: &mir::Const<'tcx>,
538 span: Span,
539 layout: Option<TyAndLayout<'tcx>>,
540 ) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
541 M::eval_mir_constant(self, *val, span, layout, |ecx, val, span, layout| {
542 let const_val = val.eval(*ecx.tcx, ecx.typing_env, span).map_err(|err| {
543 if M::ALL_CONSTS_ARE_PRECHECKED {
544 match err {
545 ErrorHandled::TooGeneric(..) => {},
546 ErrorHandled::Reported(reported, span) => {
547 if reported.is_allowed_in_infallible() {
548 } else {
552 span_bug!(span, "interpret const eval failure of {val:?} which is not in required_consts");
554 }
555 }
556 }
557 }
558 err.emit_note(*ecx.tcx);
559 err
560 })?;
561 ecx.const_val_to_op(const_val, val.ty(), layout)
562 })
563 }
564
565 #[must_use]
566 pub fn dump_place(&self, place: &PlaceTy<'tcx, M::Provenance>) -> PlacePrinter<'_, 'tcx, M> {
567 PlacePrinter { ecx: self, place: *place.place() }
568 }
569
570 #[must_use]
571 pub fn generate_stacktrace(&self) -> Vec<FrameInfo<'tcx>> {
572 Frame::generate_stacktrace_from_stack(self.stack())
573 }
574
575 pub fn adjust_nan<F1, F2>(&self, f: F2, inputs: &[F1]) -> F2
576 where
577 F1: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F2>,
578 F2: rustc_apfloat::Float,
579 {
580 if f.is_nan() { M::generate_nan(self, inputs) } else { f }
581 }
582}
583
584#[doc(hidden)]
585pub struct PlacePrinter<'a, 'tcx, M: Machine<'tcx>> {
587 ecx: &'a InterpCx<'tcx, M>,
588 place: Place<M::Provenance>,
589}
590
591impl<'a, 'tcx, M: Machine<'tcx>> std::fmt::Debug for PlacePrinter<'a, 'tcx, M> {
592 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
593 match self.place {
594 Place::Local { local, offset, locals_addr } => {
595 debug_assert_eq!(locals_addr, self.ecx.frame().locals_addr());
596 let mut allocs = Vec::new();
597 write!(fmt, "{local:?}")?;
598 if let Some(offset) = offset {
599 write!(fmt, "+{:#x}", offset.bytes())?;
600 }
601 write!(fmt, ":")?;
602
603 self.ecx.frame().locals[local].print(&mut allocs, fmt)?;
604
605 write!(fmt, ": {:?}", self.ecx.dump_allocs(allocs.into_iter().flatten().collect()))
606 }
607 Place::Ptr(mplace) => match mplace.ptr.provenance.and_then(Provenance::get_alloc_id) {
608 Some(alloc_id) => {
609 write!(fmt, "by ref {:?}: {:?}", mplace.ptr, self.ecx.dump_alloc(alloc_id))
610 }
611 ptr => write!(fmt, " integral by ref: {ptr:?}"),
612 },
613 }
614 }
615}