2019-06-27 09:36:01 +00:00
|
|
|
//! This module contains the `InterpCx` methods for executing a single step of the interpreter.
|
2016-06-23 06:02:47 +00:00
|
|
|
//!
|
|
|
|
//! The main entry point is the `step` method.
|
|
|
|
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::mir;
|
|
|
|
use rustc_middle::mir::interpret::{InterpResult, Scalar};
|
2020-03-31 16:16:47 +00:00
|
|
|
use rustc_target::abi::LayoutOf;
|
2016-12-08 04:30:37 +00:00
|
|
|
|
2019-09-14 11:00:16 +00:00
|
|
|
use super::{InterpCx, Machine};
|
2017-07-21 11:39:06 +00:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
/// Classify whether an operator is "left-homogeneous", i.e., the LHS has the
|
2018-08-20 18:08:24 +00:00
|
|
|
/// same type as the result.
|
|
|
|
#[inline]
|
|
|
|
fn binop_left_homogeneous(op: mir::BinOp) -> bool {
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::mir::BinOp::*;
|
2018-08-20 18:08:24 +00:00
|
|
|
match op {
|
2019-12-22 22:42:04 +00:00
|
|
|
Add | Sub | Mul | Div | Rem | BitXor | BitAnd | BitOr | Offset | Shl | Shr => true,
|
|
|
|
Eq | Ne | Lt | Le | Gt | Ge => false,
|
2018-08-20 18:08:24 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-27 02:59:49 +00:00
|
|
|
/// Classify whether an operator is "right-homogeneous", i.e., the RHS has the
|
2018-08-20 18:08:24 +00:00
|
|
|
/// same type as the LHS.
|
|
|
|
#[inline]
|
|
|
|
fn binop_right_homogeneous(op: mir::BinOp) -> bool {
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::mir::BinOp::*;
|
2018-08-20 18:08:24 +00:00
|
|
|
match op {
|
2019-12-22 22:42:04 +00:00
|
|
|
Add | Sub | Mul | Div | Rem | BitXor | BitAnd | BitOr | Eq | Ne | Lt | Le | Gt | Ge => true,
|
|
|
|
Offset | Shl | Shr => false,
|
2018-08-20 18:08:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 22:12:42 +00:00
|
|
|
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
2019-06-07 16:56:27 +00:00
|
|
|
pub fn run(&mut self) -> InterpResult<'tcx> {
|
2018-08-23 17:04:33 +00:00
|
|
|
while self.step()? {}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Returns `true` as long as there are more things to do.
|
2018-10-14 09:50:05 +00:00
|
|
|
///
|
|
|
|
/// This is used by [priroda](https://github.com/oli-obk/priroda)
|
2020-01-04 23:40:36 +00:00
|
|
|
///
|
|
|
|
/// This is marked `#inline(always)` to work around adverserial codegen when `opt-level = 3`
|
|
|
|
#[inline(always)]
|
2019-06-07 16:56:27 +00:00
|
|
|
pub fn step(&mut self) -> InterpResult<'tcx, bool> {
|
2020-03-16 22:12:42 +00:00
|
|
|
if self.stack().is_empty() {
|
2016-06-09 08:52:45 +00:00
|
|
|
return Ok(false);
|
2016-06-03 13:48:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 17:40:41 +00:00
|
|
|
let loc = match self.frame().loc {
|
2020-08-11 12:54:02 +00:00
|
|
|
Ok(loc) => loc,
|
|
|
|
Err(_) => {
|
2019-10-20 16:51:25 +00:00
|
|
|
// We are unwinding and this fn has no cleanup code.
|
|
|
|
// Just go on unwinding.
|
|
|
|
trace!("unwinding: skipping frame");
|
|
|
|
self.pop_stack_frame(/* unwinding */ true)?;
|
2019-12-22 22:42:04 +00:00
|
|
|
return Ok(true);
|
2019-10-20 16:51:25 +00:00
|
|
|
}
|
|
|
|
};
|
2020-04-23 17:40:41 +00:00
|
|
|
let basic_block = &self.body().basic_blocks()[loc.block];
|
2016-06-01 15:05:20 +00:00
|
|
|
|
2020-04-14 21:40:08 +00:00
|
|
|
let old_frames = self.frame_idx();
|
2017-12-06 08:25:29 +00:00
|
|
|
|
2020-04-23 17:40:41 +00:00
|
|
|
if let Some(stmt) = basic_block.statements.get(loc.statement_index) {
|
2020-04-14 21:40:08 +00:00
|
|
|
assert_eq!(old_frames, self.frame_idx());
|
2018-01-16 08:31:48 +00:00
|
|
|
self.statement(stmt)?;
|
2016-06-09 08:52:45 +00:00
|
|
|
return Ok(true);
|
2016-06-01 15:05:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 08:12:21 +00:00
|
|
|
M::before_terminator(self)?;
|
2018-02-19 11:00:15 +00:00
|
|
|
|
2016-06-03 13:48:56 +00:00
|
|
|
let terminator = basic_block.terminator();
|
2020-04-14 21:40:08 +00:00
|
|
|
assert_eq!(old_frames, self.frame_idx());
|
2018-01-16 08:31:48 +00:00
|
|
|
self.terminator(terminator)?;
|
2016-06-09 08:52:45 +00:00
|
|
|
Ok(true)
|
2016-06-01 15:05:20 +00:00
|
|
|
}
|
2016-06-23 06:02:47 +00:00
|
|
|
|
2020-05-17 13:39:35 +00:00
|
|
|
/// Runs the interpretation logic for the given `mir::Statement` at the current frame and
|
|
|
|
/// statement counter. This also moves the statement counter forward.
|
|
|
|
crate fn statement(&mut self, stmt: &mir::Statement<'tcx>) -> InterpResult<'tcx> {
|
2018-12-19 09:10:39 +00:00
|
|
|
info!("{:?}", stmt);
|
2016-08-27 07:44:46 +00:00
|
|
|
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::mir::StatementKind::*;
|
2017-09-25 13:55:21 +00:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Some statements (e.g., box) push new stack frames.
|
2018-08-22 19:58:39 +00:00
|
|
|
// We have to record the stack frame number *before* executing the statement.
|
2020-04-14 21:40:08 +00:00
|
|
|
let frame_idx = self.frame_idx();
|
2017-09-25 13:55:21 +00:00
|
|
|
|
2020-03-31 15:19:29 +00:00
|
|
|
match &stmt.kind {
|
|
|
|
Assign(box (place, rvalue)) => self.eval_rvalue_into_place(rvalue, *place)?,
|
2016-12-19 07:31:23 +00:00
|
|
|
|
2020-03-31 15:19:29 +00:00
|
|
|
SetDiscriminant { place, variant_index } => {
|
|
|
|
let dest = self.eval_place(**place)?;
|
2020-05-23 11:22:45 +00:00
|
|
|
self.write_discriminant(*variant_index, dest)?;
|
2016-12-19 07:31:23 +00:00
|
|
|
}
|
2016-08-27 07:44:46 +00:00
|
|
|
|
2017-09-05 15:18:48 +00:00
|
|
|
// Mark locals as alive
|
|
|
|
StorageLive(local) => {
|
2020-11-21 19:23:00 +00:00
|
|
|
self.storage_live(*local)?;
|
2017-09-05 15:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mark locals as dead
|
|
|
|
StorageDead(local) => {
|
2020-11-21 19:23:00 +00:00
|
|
|
self.storage_dead(*local)?;
|
2017-06-01 00:41:33 +00:00
|
|
|
}
|
2016-09-22 05:16:31 +00:00
|
|
|
|
2018-09-14 19:05:31 +00:00
|
|
|
// No dynamic semantics attached to `FakeRead`; MIR
|
2018-05-22 13:05:02 +00:00
|
|
|
// interpreter is solely intended for borrowck'ed code.
|
2018-09-14 19:05:31 +00:00
|
|
|
FakeRead(..) => {}
|
2018-05-04 10:04:33 +00:00
|
|
|
|
2018-11-06 10:04:10 +00:00
|
|
|
// Stacked Borrows.
|
2020-03-31 15:19:29 +00:00
|
|
|
Retag(kind, place) => {
|
|
|
|
let dest = self.eval_place(**place)?;
|
|
|
|
M::retag(self, *kind, dest)?;
|
2018-11-06 10:04:10 +00:00
|
|
|
}
|
2017-06-20 10:35:46 +00:00
|
|
|
|
2018-11-06 10:04:10 +00:00
|
|
|
// Statements we do not track.
|
2018-08-31 22:59:35 +00:00
|
|
|
AscribeUserType(..) => {}
|
2018-02-23 20:52:05 +00:00
|
|
|
|
2020-08-15 11:42:13 +00:00
|
|
|
// Currently, Miri discards Coverage statements. Coverage statements are only injected
|
|
|
|
// via an optional compile time MIR pass and have no side effects. Since Coverage
|
|
|
|
// statements don't exist at the source level, it is safe for Miri to ignore them, even
|
|
|
|
// for undefined behavior (UB) checks.
|
|
|
|
//
|
|
|
|
// A coverage counter inside a const expression (for example, a counter injected in a
|
|
|
|
// const function) is discarded when the const is evaluated at compile time. Whether
|
|
|
|
// this should change, and/or how to implement a const eval counter, is a subject of the
|
|
|
|
// following issue:
|
|
|
|
//
|
|
|
|
// FIXME(#73156): Handle source code coverage in const eval
|
|
|
|
Coverage(..) => {}
|
|
|
|
|
2016-09-22 05:16:31 +00:00
|
|
|
// Defined to do nothing. These are added by optimization passes, to avoid changing the
|
|
|
|
// size of MIR constantly.
|
|
|
|
Nop => {}
|
2017-02-24 09:39:55 +00:00
|
|
|
|
2020-01-14 13:40:42 +00:00
|
|
|
LlvmInlineAsm { .. } => throw_unsup_format!("inline assembly is not supported"),
|
2016-08-27 07:44:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 17:40:41 +00:00
|
|
|
self.stack_mut()[frame_idx].loc.as_mut().unwrap().statement_index += 1;
|
2016-06-23 06:02:47 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-08-09 13:04:53 +00:00
|
|
|
/// Evaluate an assignment statement.
|
|
|
|
///
|
|
|
|
/// There is no separate `eval_rvalue` function. Instead, the code for handling each rvalue
|
|
|
|
/// type writes its results directly into the memory specified by the place.
|
2019-09-06 01:06:57 +00:00
|
|
|
pub fn eval_rvalue_into_place(
|
2018-08-09 13:04:53 +00:00
|
|
|
&mut self,
|
|
|
|
rvalue: &mir::Rvalue<'tcx>,
|
2020-03-31 15:19:29 +00:00
|
|
|
place: mir::Place<'tcx>,
|
2019-06-07 16:56:27 +00:00
|
|
|
) -> InterpResult<'tcx> {
|
2018-08-09 13:04:53 +00:00
|
|
|
let dest = self.eval_place(place)?;
|
|
|
|
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::mir::Rvalue::*;
|
2018-08-09 13:04:53 +00:00
|
|
|
match *rvalue {
|
2020-05-02 19:44:25 +00:00
|
|
|
ThreadLocalRef(did) => {
|
2020-07-26 09:11:17 +00:00
|
|
|
let id = M::thread_local_static_alloc_id(self, did)?;
|
|
|
|
let val = self.global_base_pointer(id.into())?;
|
2020-05-02 19:44:25 +00:00
|
|
|
self.write_scalar(val, dest)?;
|
|
|
|
}
|
|
|
|
|
2018-08-09 13:04:53 +00:00
|
|
|
Use(ref operand) => {
|
2018-08-20 13:21:04 +00:00
|
|
|
// Avoid recomputing the layout
|
|
|
|
let op = self.eval_operand(operand, Some(dest.layout))?;
|
2018-08-13 14:14:22 +00:00
|
|
|
self.copy_op(op, dest)?;
|
2018-08-09 13:04:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BinaryOp(bin_op, ref left, ref right) => {
|
2019-12-06 12:18:32 +00:00
|
|
|
let layout = binop_left_homogeneous(bin_op).then_some(dest.layout);
|
2018-10-26 10:33:26 +00:00
|
|
|
let left = self.read_immediate(self.eval_operand(left, layout)?)?;
|
2019-12-06 12:18:32 +00:00
|
|
|
let layout = binop_right_homogeneous(bin_op).then_some(left.layout);
|
2018-10-26 10:33:26 +00:00
|
|
|
let right = self.read_immediate(self.eval_operand(right, layout)?)?;
|
2019-12-22 22:42:04 +00:00
|
|
|
self.binop_ignore_overflow(bin_op, left, right, dest)?;
|
2018-08-09 13:04:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CheckedBinaryOp(bin_op, ref left, ref right) => {
|
2018-08-20 18:08:24 +00:00
|
|
|
// Due to the extra boolean in the result, we can never reuse the `dest.layout`.
|
2018-10-26 10:33:26 +00:00
|
|
|
let left = self.read_immediate(self.eval_operand(left, None)?)?;
|
2019-12-06 12:18:32 +00:00
|
|
|
let layout = binop_right_homogeneous(bin_op).then_some(left.layout);
|
2018-10-26 10:33:26 +00:00
|
|
|
let right = self.read_immediate(self.eval_operand(right, layout)?)?;
|
2019-12-22 22:42:04 +00:00
|
|
|
self.binop_with_overflow(bin_op, left, right, dest)?;
|
2018-08-09 13:04:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnaryOp(un_op, ref operand) => {
|
2018-08-20 18:08:24 +00:00
|
|
|
// The operand always has the same type as the result.
|
2018-10-26 10:33:26 +00:00
|
|
|
let val = self.read_immediate(self.eval_operand(operand, Some(dest.layout))?)?;
|
2019-02-08 13:00:52 +00:00
|
|
|
let val = self.unary_op(un_op, val)?;
|
2019-08-10 17:40:56 +00:00
|
|
|
assert_eq!(val.layout, dest.layout, "layout mismatch for result of {:?}", un_op);
|
|
|
|
self.write_immediate(*val, dest)?;
|
2018-08-09 13:04:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Aggregate(ref kind, ref operands) => {
|
|
|
|
let (dest, active_field_index) = match **kind {
|
2018-08-09 15:56:53 +00:00
|
|
|
mir::AggregateKind::Adt(adt_def, variant_index, _, _, active_field_index) => {
|
2020-05-23 11:22:45 +00:00
|
|
|
self.write_discriminant(variant_index, dest)?;
|
2018-08-09 13:04:53 +00:00
|
|
|
if adt_def.is_enum() {
|
|
|
|
(self.place_downcast(dest, variant_index)?, active_field_index)
|
|
|
|
} else {
|
|
|
|
(dest, active_field_index)
|
|
|
|
}
|
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
_ => (dest, None),
|
2018-08-09 13:04:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (i, operand) in operands.iter().enumerate() {
|
2018-08-20 13:21:04 +00:00
|
|
|
let op = self.eval_operand(operand, None)?;
|
2018-08-09 13:04:53 +00:00
|
|
|
// Ignore zero-sized fields.
|
2018-08-13 14:14:22 +00:00
|
|
|
if !op.layout.is_zst() {
|
2018-08-09 13:04:53 +00:00
|
|
|
let field_index = active_field_index.unwrap_or(i);
|
2020-03-21 16:17:01 +00:00
|
|
|
let field_dest = self.place_field(dest, field_index)?;
|
2018-08-13 14:14:22 +00:00
|
|
|
self.copy_op(op, field_dest)?;
|
2018-08-09 13:04:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Repeat(ref operand, _) => {
|
2018-08-20 13:21:04 +00:00
|
|
|
let op = self.eval_operand(operand, None)?;
|
2018-08-13 14:14:22 +00:00
|
|
|
let dest = self.force_allocation(dest)?;
|
2018-11-03 20:57:53 +00:00
|
|
|
let length = dest.len(self)?;
|
2018-08-09 13:04:53 +00:00
|
|
|
|
2019-07-06 10:46:08 +00:00
|
|
|
if let Some(first_ptr) = self.check_mplace_access(dest, None)? {
|
|
|
|
// Write the first.
|
2018-08-13 14:14:22 +00:00
|
|
|
let first = self.mplace_field(dest, 0)?;
|
|
|
|
self.copy_op(op, first.into())?;
|
2018-08-09 13:04:53 +00:00
|
|
|
|
|
|
|
if length > 1 {
|
2019-07-06 10:46:08 +00:00
|
|
|
let elem_size = first.layout.size;
|
|
|
|
// Copy the rest. This is performance-sensitive code
|
|
|
|
// for big static/const arrays!
|
|
|
|
let rest_ptr = first_ptr.offset(elem_size, self)?;
|
2018-08-13 14:14:22 +00:00
|
|
|
self.memory.copy_repeatedly(
|
2019-12-22 22:42:04 +00:00
|
|
|
first_ptr,
|
|
|
|
rest_ptr,
|
|
|
|
elem_size,
|
|
|
|
length - 1,
|
|
|
|
/*nonoverlapping:*/ true,
|
2018-08-13 14:14:22 +00:00
|
|
|
)?;
|
2018-08-09 13:04:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 15:19:29 +00:00
|
|
|
Len(place) => {
|
2018-08-09 13:04:53 +00:00
|
|
|
// FIXME(CTFE): don't allow computing the length of arrays in const eval
|
|
|
|
let src = self.eval_place(place)?;
|
2018-08-13 14:14:22 +00:00
|
|
|
let mplace = self.force_allocation(src)?;
|
2018-11-03 20:57:53 +00:00
|
|
|
let len = mplace.len(self)?;
|
2020-03-28 18:29:46 +00:00
|
|
|
self.write_scalar(Scalar::from_machine_usize(len, self), dest)?;
|
2018-08-09 13:04:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 15:19:29 +00:00
|
|
|
AddressOf(_, place) | Ref(_, _, place) => {
|
2018-08-09 13:04:53 +00:00
|
|
|
let src = self.eval_place(place)?;
|
2019-07-28 20:57:50 +00:00
|
|
|
let place = self.force_allocation(src)?;
|
|
|
|
if place.layout.size.bytes() > 0 {
|
|
|
|
// definitely not a ZST
|
|
|
|
assert!(place.ptr.is_ptr(), "non-ZST places should be normalized to `Pointer`");
|
|
|
|
}
|
|
|
|
self.write_immediate(place.to_ref(), dest)?;
|
2018-08-09 13:04:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 14:14:22 +00:00
|
|
|
NullaryOp(mir::NullOp::Box, _) => {
|
|
|
|
M::box_alloc(self, dest)?;
|
2018-08-09 13:04:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NullaryOp(mir::NullOp::SizeOf, ty) => {
|
2020-03-11 19:36:07 +00:00
|
|
|
let ty = self.subst_from_current_frame_and_normalize_erasing_regions(ty);
|
2018-08-09 13:04:53 +00:00
|
|
|
let layout = self.layout_of(ty)?;
|
2021-01-19 07:11:24 +00:00
|
|
|
if layout.is_unsized() {
|
|
|
|
// FIXME: This should be a span_bug (#80742)
|
|
|
|
self.tcx.sess.delay_span_bug(
|
|
|
|
self.frame().current_span(),
|
|
|
|
&format!("SizeOf nullary MIR operator called for unsized type {}", ty),
|
|
|
|
);
|
2021-01-21 17:10:40 +00:00
|
|
|
throw_inval!(SizeOfUnsizedType(ty));
|
2021-01-19 07:11:24 +00:00
|
|
|
}
|
2020-03-28 18:29:46 +00:00
|
|
|
self.write_scalar(Scalar::from_machine_usize(layout.size.bytes(), self), dest)?;
|
2018-08-09 13:04:53 +00:00
|
|
|
}
|
|
|
|
|
2020-05-24 07:38:29 +00:00
|
|
|
Cast(cast_kind, ref operand, cast_ty) => {
|
2018-08-20 13:21:04 +00:00
|
|
|
let src = self.eval_operand(operand, None)?;
|
2020-05-24 07:38:29 +00:00
|
|
|
let cast_ty = self.subst_from_current_frame_and_normalize_erasing_regions(cast_ty);
|
|
|
|
self.cast(src, cast_kind, cast_ty, dest)?;
|
2018-08-09 13:04:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 15:19:29 +00:00
|
|
|
Discriminant(place) => {
|
2019-02-16 11:36:23 +00:00
|
|
|
let op = self.eval_place_to_op(place, None)?;
|
|
|
|
let discr_val = self.read_discriminant(op)?.0;
|
2020-04-14 10:49:15 +00:00
|
|
|
self.write_scalar(discr_val, dest)?;
|
2018-08-09 13:04:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 14:15:40 +00:00
|
|
|
trace!("{:?}", self.dump_place(*dest));
|
2018-08-09 13:04:53 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-06-07 16:56:27 +00:00
|
|
|
fn terminator(&mut self, terminator: &mir::Terminator<'tcx>) -> InterpResult<'tcx> {
|
2018-12-19 09:10:39 +00:00
|
|
|
info!("{:?}", terminator.kind);
|
2018-08-23 17:04:33 +00:00
|
|
|
|
2016-06-23 06:02:47 +00:00
|
|
|
self.eval_terminator(terminator)?;
|
2020-03-16 22:12:42 +00:00
|
|
|
if !self.stack().is_empty() {
|
2020-08-11 12:54:02 +00:00
|
|
|
if let Ok(loc) = self.frame().loc {
|
2020-04-23 17:40:41 +00:00
|
|
|
info!("// executing {:?}", loc.block);
|
2019-12-08 09:48:06 +00:00
|
|
|
}
|
2016-06-23 06:02:47 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-06-01 15:05:20 +00:00
|
|
|
}
|