1 //! This module contains the `InterpCx` methods for executing a single step of the interpreter.
3 //! The main entry point is the `step` method.
6 use rustc_middle::mir::interpret::{InterpResult, Scalar};
7 use rustc_middle::ty::layout::LayoutOf;
9 use super::{InterpCx, Machine};
11 /// Classify whether an operator is "left-homogeneous", i.e., the LHS has the
12 /// same type as the result.
14 fn binop_left_homogeneous(op: mir::BinOp) -> bool {
15 use rustc_middle::mir::BinOp::*;
17 Add | Sub | Mul | Div | Rem | BitXor | BitAnd | BitOr | Offset | Shl | Shr => true,
18 Eq | Ne | Lt | Le | Gt | Ge => false,
21 /// Classify whether an operator is "right-homogeneous", i.e., the RHS has the
22 /// same type as the LHS.
24 fn binop_right_homogeneous(op: mir::BinOp) -> bool {
25 use rustc_middle::mir::BinOp::*;
27 Add | Sub | Mul | Div | Rem | BitXor | BitAnd | BitOr | Eq | Ne | Lt | Le | Gt | Ge => true,
28 Offset | Shl | Shr => false,
32 impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
33 pub fn run(&mut self) -> InterpResult<'tcx> {
38 /// Returns `true` as long as there are more things to do.
40 /// This is used by [priroda](https://github.com/oli-obk/priroda)
42 /// This is marked `#inline(always)` to work around adverserial codegen when `opt-level = 3`
44 pub fn step(&mut self) -> InterpResult<'tcx, bool> {
45 if self.stack().is_empty() {
49 let Ok(loc) = self.frame().loc else {
50 // We are unwinding and this fn has no cleanup code.
51 // Just go on unwinding.
52 trace!("unwinding: skipping frame");
53 self.pop_stack_frame(/* unwinding */ true)?;
56 let basic_block = &self.body().basic_blocks()[loc.block];
58 let old_frames = self.frame_idx();
60 if let Some(stmt) = basic_block.statements.get(loc.statement_index) {
61 assert_eq!(old_frames, self.frame_idx());
62 self.statement(stmt)?;
66 M::before_terminator(self)?;
68 let terminator = basic_block.terminator();
69 assert_eq!(old_frames, self.frame_idx());
70 self.terminator(terminator)?;
74 /// Runs the interpretation logic for the given `mir::Statement` at the current frame and
75 /// statement counter. This also moves the statement counter forward.
76 pub fn statement(&mut self, stmt: &mir::Statement<'tcx>) -> InterpResult<'tcx> {
79 use rustc_middle::mir::StatementKind::*;
81 // Some statements (e.g., box) push new stack frames.
82 // We have to record the stack frame number *before* executing the statement.
83 let frame_idx = self.frame_idx();
86 Assign(box (place, rvalue)) => self.eval_rvalue_into_place(rvalue, *place)?,
88 SetDiscriminant { place, variant_index } => {
89 let dest = self.eval_place(**place)?;
90 self.write_discriminant(*variant_index, &dest)?;
93 // Mark locals as alive
94 StorageLive(local) => {
95 self.storage_live(*local)?;
98 // Mark locals as dead
99 StorageDead(local) => {
100 self.storage_dead(*local)?;
103 // No dynamic semantics attached to `FakeRead`; MIR
104 // interpreter is solely intended for borrowck'ed code.
108 Retag(kind, place) => {
109 let dest = self.eval_place(**place)?;
110 M::retag(self, *kind, &dest)?;
113 // Call CopyNonOverlapping
114 CopyNonOverlapping(box rustc_middle::mir::CopyNonOverlapping { src, dst, count }) => {
115 let src = self.eval_operand(src, None)?;
116 let dst = self.eval_operand(dst, None)?;
117 let count = self.eval_operand(count, None)?;
118 self.copy_intrinsic(&src, &dst, &count, /* nonoverlapping */ true)?;
121 // Statements we do not track.
122 AscribeUserType(..) => {}
124 // Currently, Miri discards Coverage statements. Coverage statements are only injected
125 // via an optional compile time MIR pass and have no side effects. Since Coverage
126 // statements don't exist at the source level, it is safe for Miri to ignore them, even
127 // for undefined behavior (UB) checks.
129 // A coverage counter inside a const expression (for example, a counter injected in a
130 // const function) is discarded when the const is evaluated at compile time. Whether
131 // this should change, and/or how to implement a const eval counter, is a subject of the
134 // FIXME(#73156): Handle source code coverage in const eval
137 // Defined to do nothing. These are added by optimization passes, to avoid changing the
138 // size of MIR constantly.
142 self.stack_mut()[frame_idx].loc.as_mut().unwrap().statement_index += 1;
146 /// Evaluate an assignment statement.
148 /// There is no separate `eval_rvalue` function. Instead, the code for handling each rvalue
149 /// type writes its results directly into the memory specified by the place.
150 pub fn eval_rvalue_into_place(
152 rvalue: &mir::Rvalue<'tcx>,
153 place: mir::Place<'tcx>,
154 ) -> InterpResult<'tcx> {
155 let dest = self.eval_place(place)?;
157 use rustc_middle::mir::Rvalue::*;
159 ThreadLocalRef(did) => {
160 let ptr = M::thread_local_static_base_pointer(self, did)?;
161 self.write_pointer(ptr, &dest)?;
164 Use(ref operand) => {
165 // Avoid recomputing the layout
166 let op = self.eval_operand(operand, Some(dest.layout))?;
167 self.copy_op(&op, &dest)?;
170 BinaryOp(bin_op, box (ref left, ref right)) => {
171 let layout = binop_left_homogeneous(bin_op).then_some(dest.layout);
172 let left = self.read_immediate(&self.eval_operand(left, layout)?)?;
173 let layout = binop_right_homogeneous(bin_op).then_some(left.layout);
174 let right = self.read_immediate(&self.eval_operand(right, layout)?)?;
175 self.binop_ignore_overflow(bin_op, &left, &right, &dest)?;
178 CheckedBinaryOp(bin_op, box (ref left, ref right)) => {
179 // Due to the extra boolean in the result, we can never reuse the `dest.layout`.
180 let left = self.read_immediate(&self.eval_operand(left, None)?)?;
181 let layout = binop_right_homogeneous(bin_op).then_some(left.layout);
182 let right = self.read_immediate(&self.eval_operand(right, layout)?)?;
183 self.binop_with_overflow(bin_op, &left, &right, &dest)?;
186 UnaryOp(un_op, ref operand) => {
187 // The operand always has the same type as the result.
188 let val = self.read_immediate(&self.eval_operand(operand, Some(dest.layout))?)?;
189 let val = self.unary_op(un_op, &val)?;
190 assert_eq!(val.layout, dest.layout, "layout mismatch for result of {:?}", un_op);
191 self.write_immediate(*val, &dest)?;
194 Aggregate(ref kind, ref operands) => {
195 // active_field_index is for union initialization.
196 let (dest, active_field_index) = match **kind {
197 mir::AggregateKind::Adt(adt_did, variant_index, _, _, active_field_index) => {
198 self.write_discriminant(variant_index, &dest)?;
199 if self.tcx.adt_def(adt_did).is_enum() {
200 assert!(active_field_index.is_none());
201 (self.place_downcast(&dest, variant_index)?, None)
203 if active_field_index.is_some() {
204 assert_eq!(operands.len(), 1);
206 (dest, active_field_index)
212 for (i, operand) in operands.iter().enumerate() {
213 let op = self.eval_operand(operand, None)?;
214 let field_index = active_field_index.unwrap_or(i);
215 let field_dest = self.place_field(&dest, field_index)?;
216 self.copy_op(&op, &field_dest)?;
220 Repeat(ref operand, _) => {
221 let src = self.eval_operand(operand, None)?;
222 assert!(!src.layout.is_unsized());
223 let dest = self.force_allocation(&dest)?;
224 let length = dest.len(self)?;
227 // Nothing to copy... but let's still make sure that `dest` as a place is valid.
228 self.get_alloc_mut(&dest)?;
230 // Write the src to the first element.
231 let first = self.mplace_field(&dest, 0)?;
232 self.copy_op(&src, &first.into())?;
234 // This is performance-sensitive code for big static/const arrays! So we
235 // avoid writing each operand individually and instead just make many copies
236 // of the first element.
237 let elem_size = first.layout.size;
238 let first_ptr = first.ptr;
239 let rest_ptr = first_ptr.offset(elem_size, self)?;
240 // For the alignment of `rest_ptr`, we crucially do *not* use `first.align` as
241 // that place might be more aligned than its type mandates (a `u8` array could
242 // be 4-aligned if it sits at the right spot in a struct). Instead we use
243 // `first.layout.align`, i.e., the alignment given by the type.
244 self.memory.copy_repeatedly(
248 first.layout.align.abi,
251 /*nonoverlapping:*/ true,
257 let src = self.eval_place(place)?;
258 let mplace = self.force_allocation(&src)?;
259 let len = mplace.len(self)?;
260 self.write_scalar(Scalar::from_machine_usize(len, self), &dest)?;
263 AddressOf(_, place) | Ref(_, _, place) => {
264 let src = self.eval_place(place)?;
265 let place = self.force_allocation(&src)?;
266 self.write_immediate(place.to_ref(self), &dest)?;
269 NullaryOp(null_op, ty) => {
270 let ty = self.subst_from_current_frame_and_normalize_erasing_regions(ty)?;
271 let layout = self.layout_of(ty)?;
272 if layout.is_unsized() {
273 // FIXME: This should be a span_bug (#80742)
274 self.tcx.sess.delay_span_bug(
275 self.frame().current_span(),
276 &format!("Nullary MIR operator called for unsized type {}", ty),
278 throw_inval!(SizeOfUnsizedType(ty));
280 let val = match null_op {
281 mir::NullOp::SizeOf => layout.size.bytes(),
282 mir::NullOp::AlignOf => layout.align.abi.bytes(),
284 self.write_scalar(Scalar::from_machine_usize(val, self), &dest)?;
287 ShallowInitBox(ref operand, _) => {
288 let src = self.eval_operand(operand, None)?;
289 let v = self.read_immediate(&src)?;
290 self.write_immediate(*v, &dest)?;
293 Cast(cast_kind, ref operand, cast_ty) => {
294 let src = self.eval_operand(operand, None)?;
296 self.subst_from_current_frame_and_normalize_erasing_regions(cast_ty)?;
297 self.cast(&src, cast_kind, cast_ty, &dest)?;
300 Discriminant(place) => {
301 let op = self.eval_place_to_op(place, None)?;
302 let discr_val = self.read_discriminant(&op)?.0;
303 self.write_scalar(discr_val, &dest)?;
307 trace!("{:?}", self.dump_place(*dest));
312 fn terminator(&mut self, terminator: &mir::Terminator<'tcx>) -> InterpResult<'tcx> {
313 info!("{:?}", terminator.kind);
315 self.eval_terminator(terminator)?;
316 if !self.stack().is_empty() {
317 if let Ok(loc) = self.frame().loc {
318 info!("// executing {:?}", loc.block);